1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.datamodel
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collection
;
24 import java
.util
.Collections
;
25 import java
.util
.Iterator
;
26 import java
.util
.Vector
;
28 import com
.owncloud
.android
.MainApp
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
31 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
32 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
33 import com
.owncloud
.android
.utils
.FileStorageUtils
;
34 import com
.owncloud
.android
.utils
.Log_OC
;
37 import android
.accounts
.Account
;
38 import android
.content
.ContentProviderClient
;
39 import android
.content
.ContentProviderOperation
;
40 import android
.content
.ContentProviderResult
;
41 import android
.content
.ContentResolver
;
42 import android
.content
.ContentUris
;
43 import android
.content
.ContentValues
;
44 import android
.content
.OperationApplicationException
;
45 import android
.database
.Cursor
;
46 import android
.net
.Uri
;
47 import android
.os
.RemoteException
;
49 public class FileDataStorageManager
{
51 public static final int ROOT_PARENT_ID
= 0;
53 private ContentResolver mContentResolver
;
54 private ContentProviderClient mContentProviderClient
;
55 private Account mAccount
;
57 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
60 public FileDataStorageManager(Account account
, ContentResolver cr
) {
61 mContentProviderClient
= null
;
62 mContentResolver
= cr
;
66 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
67 mContentProviderClient
= cp
;
68 mContentResolver
= null
;
73 public void setAccount(Account account
) {
77 public Account
getAccount() {
81 public void setContentResolver(ContentResolver cr
) {
82 mContentResolver
= cr
;
85 public ContentResolver
getContentResolver() {
86 return mContentResolver
;
89 public void setContentProviderClient(ContentProviderClient cp
) {
90 mContentProviderClient
= cp
;
93 public ContentProviderClient
getContentProviderClient() {
94 return mContentProviderClient
;
98 public OCFile
getFileByPath(String path
) {
99 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
101 if (c
.moveToFirst()) {
102 file
= createFileInstance(c
);
105 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
106 return createRootDir(); // root should always exist
112 public OCFile
getFileById(long id
) {
113 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
115 if (c
.moveToFirst()) {
116 file
= createFileInstance(c
);
122 public OCFile
getFileByLocalPath(String path
) {
123 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
125 if (c
.moveToFirst()) {
126 file
= createFileInstance(c
);
132 public boolean fileExists(long id
) {
133 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
136 public boolean fileExists(String path
) {
137 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
141 public Vector
<OCFile
> getFolderContent(OCFile f
) {
142 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
143 return getFolderContent(f
.getFileId());
146 return new Vector
<OCFile
>();
151 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
152 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
153 if (folder
!= null
) {
154 // TODO better implementation, filtering in the access to database (if possible) instead of here
155 Vector
<OCFile
> tmp
= getFolderContent(folder
);
156 OCFile current
= null
;
157 for (int i
=0; i
<tmp
.size(); i
++) {
158 current
= tmp
.get(i
);
159 if (current
.isImage()) {
168 public boolean saveFile(OCFile file
) {
169 boolean overriden
= false
;
170 ContentValues cv
= new ContentValues();
171 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
172 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
173 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
174 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
175 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
176 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
177 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
178 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
179 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
180 if (!file
.isFolder())
181 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
182 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
183 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
184 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
185 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
186 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
187 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
188 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
190 boolean sameRemotePath
= fileExists(file
.getRemotePath());
191 if (sameRemotePath
||
192 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
194 OCFile oldFile
= null
;
195 if (sameRemotePath
) {
196 oldFile
= getFileByPath(file
.getRemotePath());
197 file
.setFileId(oldFile
.getFileId());
199 oldFile
= getFileById(file
.getFileId());
203 if (getContentResolver() != null
) {
204 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
205 ProviderTableMeta
._ID
+ "=?",
206 new String
[] { String
.valueOf(file
.getFileId()) });
209 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
210 cv
, ProviderTableMeta
._ID
+ "=?",
211 new String
[] { String
.valueOf(file
.getFileId()) });
212 } catch (RemoteException e
) {
214 "Fail to insert insert file to database "
219 Uri result_uri
= null
;
220 if (getContentResolver() != null
) {
221 result_uri
= getContentResolver().insert(
222 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
225 result_uri
= getContentProviderClient().insert(
226 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
227 } catch (RemoteException e
) {
229 "Fail to insert insert file to database "
233 if (result_uri
!= null
) {
234 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
236 file
.setFileId(new_id
);
240 // if (file.isFolder()) {
241 // updateFolderSize(file.getFileId());
243 // updateFolderSize(file.getParentId());
251 * Inserts or updates the list of files contained in a given folder.
253 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
254 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
258 * @param removeNotUpdated
260 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
262 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
264 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
266 // prepare operations to insert or update files to save in the given folder
267 for (OCFile file
: updatedFiles
) {
268 ContentValues cv
= new ContentValues();
269 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
270 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
271 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
272 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
273 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
274 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
275 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
276 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
277 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
278 if (!file
.isFolder()) {
279 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
281 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
282 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
283 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
284 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
285 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
286 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
287 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
289 boolean existsByPath
= fileExists(file
.getRemotePath());
290 if (existsByPath
|| fileExists(file
.getFileId())) {
291 // updating an existing file
292 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
294 withSelection( ProviderTableMeta
._ID
+ "=?",
295 new String
[] { String
.valueOf(file
.getFileId()) })
300 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
304 // prepare operations to remove files in the given folder
305 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
306 String
[] whereArgs
= null
;
307 for (OCFile file
: filesToRemove
) {
308 if (file
.getParentId() == folder
.getFileId()) {
309 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
310 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
311 if (file
.isFolder()) {
312 operations
.add(ContentProviderOperation
313 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
315 // TODO remove local folder
317 operations
.add(ContentProviderOperation
318 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
321 new File(file
.getStoragePath()).delete();
322 // TODO move the deletion of local contents after success of deletions
328 // update metadata of folder
329 ContentValues cv
= new ContentValues();
330 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
331 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
332 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
333 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
334 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
335 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
336 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
337 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
338 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
339 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
340 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
341 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
342 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
343 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
344 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
346 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
348 withSelection( ProviderTableMeta
._ID
+ "=?",
349 new String
[] { String
.valueOf(folder
.getFileId()) })
352 // apply operations in batch
353 ContentProviderResult
[] results
= null
;
354 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
356 if (getContentResolver() != null
) {
357 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
360 results
= getContentProviderClient().applyBatch(operations
);
363 } catch (OperationApplicationException e
) {
364 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
366 } catch (RemoteException e
) {
367 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
370 // update new id in file objects for insertions
371 if (results
!= null
) {
373 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
375 for (int i
=0; i
<results
.length
; i
++) {
376 if (filesIt
.hasNext()) {
377 file
= filesIt
.next();
381 if (results
[i
].uri
!= null
) {
382 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
383 //updatedFiles.get(i).setFileId(newId);
385 file
.setFileId(newId
);
391 //updateFolderSize(folder.getFileId());
400 // private void updateFolderSize(long id) {
401 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
402 // Log_OC.d(TAG, "Updating size of " + id);
403 // if (getContentResolver() != null) {
404 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
405 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
406 // ProviderTableMeta._ID + "=?",
407 // new String[] { String.valueOf(id) });
410 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
411 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
412 // ProviderTableMeta._ID + "=?",
413 // new String[] { String.valueOf(id) });
415 // } catch (RemoteException e) {
416 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
420 // Log_OC.e(TAG, "not updating size for folder " + id);
425 public void removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
427 if (file
.isFolder()) {
428 removeFolder(file
, removeDBData
, removeLocalCopy
);
432 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
433 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
434 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
435 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
436 if (getContentProviderClient() != null
) {
438 getContentProviderClient().delete(file_uri
, where
, whereArgs
);
439 } catch (RemoteException e
) {
443 getContentResolver().delete(file_uri
, where
, whereArgs
);
445 //updateFolderSize(file.getParentId());
447 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
) {
448 boolean success
= new File(file
.getStoragePath()).delete();
449 if (!removeDBData
&& success
) {
450 // maybe unnecessary, but should be checked TODO remove if unnecessary
451 file
.setStoragePath(null
);
460 public void removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
461 if (folder
!= null
&& folder
.isFolder()) {
462 if (removeDBData
&& folder
.getFileId() != -1) {
463 removeFolderInDb(folder
);
465 if (removeLocalContent
) {
466 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
467 removeLocalFolder(localFolder
);
472 private void removeFolderInDb(OCFile folder
) {
473 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
474 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
475 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
476 if (getContentProviderClient() != null
) {
478 getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
479 } catch (RemoteException e
) {
483 getContentResolver().delete(folder_uri
, where
, whereArgs
);
485 //updateFolderSize(folder.getParentId());
488 private void removeLocalFolder(File folder
) {
489 if (folder
.exists()) {
490 File
[] files
= folder
.listFiles();
492 for (File file
: files
) {
493 if (file
.isDirectory()) {
494 removeLocalFolder(file
);
505 * Updates database for a folder that was moved to a different location.
507 * TODO explore better (faster) implementations
508 * TODO throw exceptions up !
510 public void moveFolder(OCFile folder
, String newPath
) {
511 // TODO check newPath
513 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
514 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
516 if (getContentProviderClient() != null
) {
518 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
520 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
521 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
522 } catch (RemoteException e
) {
523 Log_OC
.e(TAG
, e
.getMessage());
526 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
528 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
529 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
532 /// 2. prepare a batch of update operations to change all the descendants
533 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
534 int lengthOfOldPath
= folder
.getRemotePath().length();
535 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
536 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
537 if (c
.moveToFirst()) {
539 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
540 OCFile child
= createFileInstance(c
);
541 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
542 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
543 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
545 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
547 withSelection( ProviderTableMeta
._ID
+ "=?",
548 new String
[] { String
.valueOf(child
.getFileId()) })
550 } while (c
.moveToNext());
554 /// 3. apply updates in batch
556 if (getContentResolver() != null
) {
557 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
560 getContentProviderClient().applyBatch(operations
);
563 } catch (OperationApplicationException e
) {
564 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
566 } catch (RemoteException e
) {
567 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
574 private Vector
<OCFile
> getFolderContent(long parentId
) {
576 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
578 Uri req_uri
= Uri
.withAppendedPath(
579 ProviderTableMeta
.CONTENT_URI_DIR
,
580 String
.valueOf(parentId
));
583 if (getContentProviderClient() != null
) {
585 c
= getContentProviderClient().query(req_uri
, null
,
586 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
587 new String
[] { String
.valueOf(parentId
)}, null
);
588 } catch (RemoteException e
) {
589 Log_OC
.e(TAG
, e
.getMessage());
593 c
= getContentResolver().query(req_uri
, null
,
594 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
595 new String
[] { String
.valueOf(parentId
)}, null
);
598 if (c
.moveToFirst()) {
600 OCFile child
= createFileInstance(c
);
602 } while (c
.moveToNext());
607 Collections
.sort(ret
);
613 private OCFile
createRootDir() {
614 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
615 file
.setMimetype("DIR");
616 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
621 private boolean fileExists(String cmp_key
, String value
) {
623 if (getContentResolver() != null
) {
624 c
= getContentResolver()
625 .query(ProviderTableMeta
.CONTENT_URI
,
628 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
630 new String
[] { value
, mAccount
.name
}, null
);
633 c
= getContentProviderClient().query(
634 ProviderTableMeta
.CONTENT_URI
,
637 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
638 new String
[] { value
, mAccount
.name
}, null
);
639 } catch (RemoteException e
) {
641 "Couldn't determine file existance, assuming non existance: "
646 boolean retval
= c
.moveToFirst();
651 private Cursor
getCursorForValue(String key
, String value
) {
653 if (getContentResolver() != null
) {
654 c
= getContentResolver()
655 .query(ProviderTableMeta
.CONTENT_URI
,
658 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
660 new String
[] { value
, mAccount
.name
}, null
);
663 c
= getContentProviderClient().query(
664 ProviderTableMeta
.CONTENT_URI
,
666 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
667 + "=?", new String
[] { value
, mAccount
.name
},
669 } catch (RemoteException e
) {
670 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
677 private Cursor
getShareCursorForValue(String key
, String value
) {
679 if (getContentResolver() != null
) {
680 c
= getContentResolver()
681 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
684 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
686 new String
[] { value
, mAccount
.name
}, null
);
689 c
= getContentProviderClient().query(
690 ProviderTableMeta
.CONTENT_URI_SHARE
,
692 key
+ "=? AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
693 + "=?", new String
[] { value
, mAccount
.name
},
695 } catch (RemoteException e
) {
696 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
703 private OCFile
createFileInstance(Cursor c
) {
706 file
= new OCFile(c
.getString(c
707 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
708 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
709 file
.setParentId(c
.getLong(c
710 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
711 file
.setMimetype(c
.getString(c
712 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
713 if (!file
.isFolder()) {
714 file
.setStoragePath(c
.getString(c
715 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
716 if (file
.getStoragePath() == null
) {
717 // try to find existing file and bind it with current account; - with the current update of SynchronizeFolderOperation, this won't be necessary anymore after a full synchronization of the account
718 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
720 file
.setStoragePath(f
.getAbsolutePath());
721 file
.setLastSyncDateForData(f
.lastModified());
725 file
.setFileLength(c
.getLong(c
726 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
727 file
.setCreationTimestamp(c
.getLong(c
728 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
729 file
.setModificationTimestamp(c
.getLong(c
730 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
731 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
732 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
733 file
.setLastSyncDateForProperties(c
.getLong(c
734 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
735 file
.setLastSyncDateForData(c
.getLong(c
.
736 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
737 file
.setKeepInSync(c
.getInt(
738 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
739 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
740 file
.setShareByLink(c
.getInt(
741 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
742 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
749 * Returns if the file/folder is shared by link or not
750 * @param path Path of the file/folder
753 public boolean isShareByLink(String path
) {
754 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
756 if (c
.moveToFirst()) {
757 file
= createFileInstance(c
);
760 return file
.isShareByLink();
764 * Returns the public link of the file/folder
765 * @param path Path of the file/folder
768 public String
getPublicLink(String path
) {
769 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
771 if (c
.moveToFirst()) {
772 file
= createFileInstance(c
);
775 return file
.getPublicLink();
779 // Methods for Shares
780 public boolean saveShare(OCShare share
) {
781 boolean overriden
= false
;
782 ContentValues cv
= new ContentValues();
783 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
784 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
785 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
786 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
787 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
788 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
789 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
790 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
791 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
792 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
793 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
794 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
795 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
796 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
798 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
801 if (getContentResolver() != null
) {
802 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
803 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
804 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
807 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
808 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
809 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
810 } catch (RemoteException e
) {
812 "Fail to insert insert file to database "
817 Uri result_uri
= null
;
818 if (getContentResolver() != null
) {
819 result_uri
= getContentResolver().insert(
820 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
823 result_uri
= getContentProviderClient().insert(
824 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
825 } catch (RemoteException e
) {
827 "Fail to insert insert file to database "
831 if (result_uri
!= null
) {
832 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
841 private OCShare
getShareById(long id
) {
842 Cursor c
= getShareCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
843 OCShare share
= null
;
844 if (c
.moveToFirst()) {
845 share
= createShareInstance(c
);
851 private OCShare
getShareByRemoteId(long remoteId
) {
852 Cursor c
= getShareCursorForValue(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
853 OCShare share
= null
;
854 if (c
.moveToFirst()) {
855 share
= createShareInstance(c
);
861 public OCShare
getShareByPath(String path
) {
862 Cursor c
= getShareCursorForValue(ProviderTableMeta
.OCSHARES_PATH
, path
);
863 OCShare share
= null
;
864 if (c
.moveToFirst()) {
865 share
= createShareInstance(c
);
871 private OCShare
createShareInstance(Cursor c
) {
872 OCShare share
= null
;
874 share
= new OCShare(c
.getString(c
875 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
876 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
877 share
.setFileSource(c
.getLong(c
878 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
879 share
.setShareType(ShareType
.fromValue(c
.getInt(c
880 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
881 share
.setPermissions(c
.getInt(c
882 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
883 share
.setSharedDate(c
.getLong(c
884 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
885 share
.setExpirationDate(c
.getLong(c
886 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
887 share
.setToken(c
.getString(c
888 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
889 share
.setSharedWithDisplayName(c
.getString(c
890 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
891 share
.setIsFolder(c
.getInt(
892 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
893 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
894 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
900 private boolean shareExists(String cmp_key
, String value
) {
902 if (getContentResolver() != null
) {
903 c
= getContentResolver()
904 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
907 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
909 new String
[] { value
, mAccount
.name
}, null
);
912 c
= getContentProviderClient().query(
913 ProviderTableMeta
.CONTENT_URI_SHARE
,
916 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
917 new String
[] { value
, mAccount
.name
}, null
);
918 } catch (RemoteException e
) {
920 "Couldn't determine file existance, assuming non existance: "
925 boolean retval
= c
.moveToFirst();
930 private boolean shareExists(long remoteId
) {
931 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
934 private void cleanSharedFiles() {
935 ContentValues cv
= new ContentValues();
936 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
937 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
938 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
939 String
[] whereArgs
= new String
[]{mAccount
.name
};
941 if (getContentResolver() != null
) {
942 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
946 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
948 } catch (RemoteException e
) {
949 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
954 private void cleanSharedFilesInFolder(OCFile folder
) {
955 ContentValues cv
= new ContentValues();
956 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
957 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
958 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
959 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
961 if (getContentResolver() != null
) {
962 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
966 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
968 } catch (RemoteException e
) {
969 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
974 private void cleanShares() {
975 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
976 String
[] whereArgs
= new String
[]{mAccount
.name
};
978 if (getContentResolver() != null
) {
979 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
983 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
985 } catch (RemoteException e
) {
986 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
991 public void saveShares(Collection
<OCShare
> shares
) {
993 if (shares
!= null
) {
994 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
996 // prepare operations to insert or update files to save in the given folder
997 for (OCShare share
: shares
) {
998 ContentValues cv
= new ContentValues();
999 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1000 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1001 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1002 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1003 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1004 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1005 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1006 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1007 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1008 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1009 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1010 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1011 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1012 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1014 if (shareExists(share
.getIdRemoteShared())) {
1015 // updating an existing file
1016 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1018 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1019 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1023 // adding a new file
1024 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1028 // apply operations in batch
1029 if (operations
.size() > 0) {
1030 @SuppressWarnings("unused")
1031 ContentProviderResult
[] results
= null
;
1032 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1034 if (getContentResolver() != null
) {
1035 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1038 results
= getContentProviderClient().applyBatch(operations
);
1041 } catch (OperationApplicationException e
) {
1042 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1044 } catch (RemoteException e
) {
1045 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1052 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1055 if (sharedFiles
!= null
) {
1056 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1058 // prepare operations to insert or update files to save in the given folder
1059 for (OCFile file
: sharedFiles
) {
1060 ContentValues cv
= new ContentValues();
1061 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1062 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1063 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1064 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1065 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1066 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1067 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1068 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1069 if (!file
.isFolder()) {
1070 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1072 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1073 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1074 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1075 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1076 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1077 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1078 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1080 boolean existsByPath
= fileExists(file
.getRemotePath());
1081 if (existsByPath
|| fileExists(file
.getFileId())) {
1082 // updating an existing file
1083 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1085 withSelection( ProviderTableMeta
._ID
+ "=?",
1086 new String
[] { String
.valueOf(file
.getFileId()) })
1090 // adding a new file
1091 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1095 // apply operations in batch
1096 if (operations
.size() > 0) {
1097 @SuppressWarnings("unused")
1098 ContentProviderResult
[] results
= null
;
1099 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1101 if (getContentResolver() != null
) {
1102 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1105 results
= getContentProviderClient().applyBatch(operations
);
1108 } catch (OperationApplicationException e
) {
1109 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1111 } catch (RemoteException e
) {
1112 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1119 public void removeShare(OCShare share
){
1120 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1121 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1122 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1123 if (getContentProviderClient() != null
) {
1125 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1126 } catch (RemoteException e
) {
1127 e
.printStackTrace();
1130 getContentResolver().delete(share_uri
, where
, whereArgs
);
1134 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1137 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1139 for (OCShare share
: shares
) {
1141 String path
= share
.getPath();
1142 if (share
.isFolder()) {
1143 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1146 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1147 OCFile file
= getFileByPath(path
);
1149 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1150 file
.setShareByLink(true
);
1151 sharedFiles
.add(file
);
1156 updateSharedFiles(sharedFiles
);
1160 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1161 cleanSharedFilesInFolder(folder
);
1162 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1163 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1165 if (shares
!= null
) {
1166 // prepare operations to insert or update files to save in the given folder
1167 for (OCShare share
: shares
) {
1168 ContentValues cv
= new ContentValues();
1169 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1170 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1171 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1172 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1173 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1174 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1175 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1176 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1177 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1178 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1179 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1180 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1181 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1182 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1185 if (shareExists(share.getIdRemoteShared())) {
1186 // updating an existing share resource
1187 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1189 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1190 new String[] { String.valueOf(share.getIdRemoteShared()) })
1195 // adding a new share resource
1196 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1201 // apply operations in batch
1202 if (operations
.size() > 0) {
1203 @SuppressWarnings("unused")
1204 ContentProviderResult
[] results
= null
;
1205 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1207 if (getContentResolver() != null
) {
1208 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1211 results
= getContentProviderClient().applyBatch(operations
);
1214 } catch (OperationApplicationException e
) {
1215 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1217 } catch (RemoteException e
) {
1218 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1225 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1226 if (folder
!= null
) {
1227 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1228 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1230 Vector
<OCFile
> files
= getFolderContent(folder
);
1232 for (OCFile file
: files
) {
1233 whereArgs
[0] = file
.getRemotePath();
1234 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1235 .withSelection(where
, whereArgs
)
1239 return preparedOperations
;
1242 if (operations.size() > 0) {
1244 if (getContentResolver() != null) {
1245 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1248 getContentProviderClient().applyBatch(operations);
1251 } catch (OperationApplicationException e) {
1252 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1254 } catch (RemoteException e) {
1255 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1261 if (getContentResolver() != null) {
1263 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1268 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1272 } catch (RemoteException e) {
1273 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());