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
getFirstShareByPathAndType(String path
, ShareType type
) {
863 if (getContentResolver() != null
) {
864 c
= getContentResolver().query(
865 ProviderTableMeta
.CONTENT_URI_SHARE
,
867 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
868 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
869 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
870 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
874 c
= getContentProviderClient().query(
875 ProviderTableMeta
.CONTENT_URI_SHARE
,
877 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
878 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
879 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
880 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
883 } catch (RemoteException e
) {
884 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
888 OCShare share
= null
;
889 if (c
.moveToFirst()) {
890 share
= createShareInstance(c
);
896 private OCShare
createShareInstance(Cursor c
) {
897 OCShare share
= null
;
899 share
= new OCShare(c
.getString(c
900 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
901 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
902 share
.setFileSource(c
.getLong(c
903 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
904 share
.setShareType(ShareType
.fromValue(c
.getInt(c
905 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
906 share
.setPermissions(c
.getInt(c
907 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
908 share
.setSharedDate(c
.getLong(c
909 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
910 share
.setExpirationDate(c
.getLong(c
911 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
912 share
.setToken(c
.getString(c
913 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
914 share
.setSharedWithDisplayName(c
.getString(c
915 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
916 share
.setIsFolder(c
.getInt(
917 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
918 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
919 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
925 private boolean shareExists(String cmp_key
, String value
) {
927 if (getContentResolver() != null
) {
928 c
= getContentResolver()
929 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
932 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
934 new String
[] { value
, mAccount
.name
}, null
);
937 c
= getContentProviderClient().query(
938 ProviderTableMeta
.CONTENT_URI_SHARE
,
941 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
942 new String
[] { value
, mAccount
.name
}, null
);
943 } catch (RemoteException e
) {
945 "Couldn't determine file existance, assuming non existance: "
950 boolean retval
= c
.moveToFirst();
955 private boolean shareExists(long remoteId
) {
956 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
959 private void cleanSharedFiles() {
960 ContentValues cv
= new ContentValues();
961 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
962 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
963 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
964 String
[] whereArgs
= new String
[]{mAccount
.name
};
966 if (getContentResolver() != null
) {
967 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
971 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
973 } catch (RemoteException e
) {
974 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
979 private void cleanSharedFilesInFolder(OCFile folder
) {
980 ContentValues cv
= new ContentValues();
981 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
982 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
983 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
984 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
986 if (getContentResolver() != null
) {
987 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
991 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
993 } catch (RemoteException e
) {
994 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
999 private void cleanShares() {
1000 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1001 String
[] whereArgs
= new String
[]{mAccount
.name
};
1003 if (getContentResolver() != null
) {
1004 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1008 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1010 } catch (RemoteException e
) {
1011 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1016 public void saveShares(Collection
<OCShare
> shares
) {
1018 if (shares
!= null
) {
1019 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1021 // prepare operations to insert or update files to save in the given folder
1022 for (OCShare share
: shares
) {
1023 ContentValues cv
= new ContentValues();
1024 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1025 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1026 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1027 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1028 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1029 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1030 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1031 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1032 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1033 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1034 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1035 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1036 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1037 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1039 if (shareExists(share
.getIdRemoteShared())) {
1040 // updating an existing file
1041 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1043 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1044 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1048 // adding a new file
1049 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1053 // apply operations in batch
1054 if (operations
.size() > 0) {
1055 @SuppressWarnings("unused")
1056 ContentProviderResult
[] results
= null
;
1057 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1059 if (getContentResolver() != null
) {
1060 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1063 results
= getContentProviderClient().applyBatch(operations
);
1066 } catch (OperationApplicationException e
) {
1067 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1069 } catch (RemoteException e
) {
1070 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1077 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1080 if (sharedFiles
!= null
) {
1081 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1083 // prepare operations to insert or update files to save in the given folder
1084 for (OCFile file
: sharedFiles
) {
1085 ContentValues cv
= new ContentValues();
1086 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1087 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1088 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1089 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1090 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1091 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1092 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1093 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1094 if (!file
.isFolder()) {
1095 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1097 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1098 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1099 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1100 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1101 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1102 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1103 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1105 boolean existsByPath
= fileExists(file
.getRemotePath());
1106 if (existsByPath
|| fileExists(file
.getFileId())) {
1107 // updating an existing file
1108 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1110 withSelection( ProviderTableMeta
._ID
+ "=?",
1111 new String
[] { String
.valueOf(file
.getFileId()) })
1115 // adding a new file
1116 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1120 // apply operations in batch
1121 if (operations
.size() > 0) {
1122 @SuppressWarnings("unused")
1123 ContentProviderResult
[] results
= null
;
1124 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1126 if (getContentResolver() != null
) {
1127 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1130 results
= getContentProviderClient().applyBatch(operations
);
1133 } catch (OperationApplicationException e
) {
1134 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1136 } catch (RemoteException e
) {
1137 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1144 public void removeShare(OCShare share
){
1145 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1146 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1147 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1148 if (getContentProviderClient() != null
) {
1150 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1151 } catch (RemoteException e
) {
1152 e
.printStackTrace();
1155 getContentResolver().delete(share_uri
, where
, whereArgs
);
1159 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1162 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1164 for (OCShare share
: shares
) {
1166 String path
= share
.getPath();
1167 if (share
.isFolder()) {
1168 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1171 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1172 OCFile file
= getFileByPath(path
);
1174 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1175 file
.setShareByLink(true
);
1176 sharedFiles
.add(file
);
1181 updateSharedFiles(sharedFiles
);
1185 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1186 cleanSharedFilesInFolder(folder
);
1187 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1188 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1190 if (shares
!= null
) {
1191 // prepare operations to insert or update files to save in the given folder
1192 for (OCShare share
: shares
) {
1193 ContentValues cv
= new ContentValues();
1194 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1195 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1196 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1197 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1198 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1199 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1200 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1201 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1202 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1203 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1204 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1205 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1206 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1207 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1210 if (shareExists(share.getIdRemoteShared())) {
1211 // updating an existing share resource
1212 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1214 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1215 new String[] { String.valueOf(share.getIdRemoteShared()) })
1220 // adding a new share resource
1221 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1226 // apply operations in batch
1227 if (operations
.size() > 0) {
1228 @SuppressWarnings("unused")
1229 ContentProviderResult
[] results
= null
;
1230 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1232 if (getContentResolver() != null
) {
1233 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1236 results
= getContentProviderClient().applyBatch(operations
);
1239 } catch (OperationApplicationException e
) {
1240 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1242 } catch (RemoteException e
) {
1243 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1250 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1251 if (folder
!= null
) {
1252 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1253 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1255 Vector
<OCFile
> files
= getFolderContent(folder
);
1257 for (OCFile file
: files
) {
1258 whereArgs
[0] = file
.getRemotePath();
1259 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1260 .withSelection(where
, whereArgs
)
1264 return preparedOperations
;
1267 if (operations.size() > 0) {
1269 if (getContentResolver() != null) {
1270 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1273 getContentProviderClient().applyBatch(operations);
1276 } catch (OperationApplicationException e) {
1277 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1279 } catch (RemoteException e) {
1280 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1286 if (getContentResolver() != null) {
1288 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1293 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1297 } catch (RemoteException e) {
1298 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());