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 boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
426 boolean success
= true
;
428 if (file
.isFolder()) {
429 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
433 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
434 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
435 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
436 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
438 if (getContentProviderClient() != null
) {
440 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
441 } catch (RemoteException e
) {
445 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
447 success
&= (deleted
> 0);
449 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
450 success
= new File(file
.getStoragePath()).delete();
451 if (!removeDBData
&& success
) {
452 // maybe unnecessary, but should be checked TODO remove if unnecessary
453 file
.setStoragePath(null
);
463 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
464 boolean success
= true
;
465 if (folder
!= null
&& folder
.isFolder()) {
466 if (removeDBData
&& folder
.getFileId() != -1) {
467 success
= removeFolderInDb(folder
);
469 if (removeLocalContent
&& success
) {
470 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
471 success
= removeLocalFolder(localFolder
);
477 private boolean removeFolderInDb(OCFile folder
) {
478 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
479 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
480 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
482 if (getContentProviderClient() != null
) {
484 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
485 } catch (RemoteException e
) {
489 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
494 private boolean removeLocalFolder(File folder
) {
495 boolean success
= true
;
496 if (folder
.exists()) {
497 File
[] files
= folder
.listFiles();
499 for (File file
: files
) {
500 if (file
.isDirectory()) {
501 success
&= removeLocalFolder(file
);
503 success
&= file
.delete();
507 success
&= folder
.delete();
513 * Updates database for a folder that was moved to a different location.
515 * TODO explore better (faster) implementations
516 * TODO throw exceptions up !
518 public void moveFolder(OCFile folder
, String newPath
) {
519 // TODO check newPath
521 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
522 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
524 if (getContentProviderClient() != null
) {
526 c
= getContentProviderClient().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 ");
530 } catch (RemoteException e
) {
531 Log_OC
.e(TAG
, e
.getMessage());
534 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
536 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
537 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
540 /// 2. prepare a batch of update operations to change all the descendants
541 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
542 int lengthOfOldPath
= folder
.getRemotePath().length();
543 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
544 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
545 if (c
.moveToFirst()) {
547 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
548 OCFile child
= createFileInstance(c
);
549 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
550 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
551 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
553 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
555 withSelection( ProviderTableMeta
._ID
+ "=?",
556 new String
[] { String
.valueOf(child
.getFileId()) })
558 } while (c
.moveToNext());
562 /// 3. apply updates in batch
564 if (getContentResolver() != null
) {
565 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
568 getContentProviderClient().applyBatch(operations
);
571 } catch (OperationApplicationException e
) {
572 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
574 } catch (RemoteException e
) {
575 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
582 private Vector
<OCFile
> getFolderContent(long parentId
) {
584 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
586 Uri req_uri
= Uri
.withAppendedPath(
587 ProviderTableMeta
.CONTENT_URI_DIR
,
588 String
.valueOf(parentId
));
591 if (getContentProviderClient() != null
) {
593 c
= getContentProviderClient().query(req_uri
, null
,
594 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
595 new String
[] { String
.valueOf(parentId
)}, null
);
596 } catch (RemoteException e
) {
597 Log_OC
.e(TAG
, e
.getMessage());
601 c
= getContentResolver().query(req_uri
, null
,
602 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
603 new String
[] { String
.valueOf(parentId
)}, null
);
606 if (c
.moveToFirst()) {
608 OCFile child
= createFileInstance(c
);
610 } while (c
.moveToNext());
615 Collections
.sort(ret
);
621 private OCFile
createRootDir() {
622 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
623 file
.setMimetype("DIR");
624 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
629 private boolean fileExists(String cmp_key
, String value
) {
631 if (getContentResolver() != null
) {
632 c
= getContentResolver()
633 .query(ProviderTableMeta
.CONTENT_URI
,
636 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
638 new String
[] { value
, mAccount
.name
}, null
);
641 c
= getContentProviderClient().query(
642 ProviderTableMeta
.CONTENT_URI
,
645 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
646 new String
[] { value
, mAccount
.name
}, null
);
647 } catch (RemoteException e
) {
649 "Couldn't determine file existance, assuming non existance: "
654 boolean retval
= c
.moveToFirst();
659 private Cursor
getCursorForValue(String key
, String value
) {
661 if (getContentResolver() != null
) {
662 c
= getContentResolver()
663 .query(ProviderTableMeta
.CONTENT_URI
,
666 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
668 new String
[] { value
, mAccount
.name
}, null
);
671 c
= getContentProviderClient().query(
672 ProviderTableMeta
.CONTENT_URI
,
674 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
675 + "=?", new String
[] { value
, mAccount
.name
},
677 } catch (RemoteException e
) {
678 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
685 private Cursor
getShareCursorForValue(String key
, String value
) {
687 if (getContentResolver() != null
) {
688 c
= getContentResolver()
689 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
692 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
694 new String
[] { value
, mAccount
.name
}, null
);
697 c
= getContentProviderClient().query(
698 ProviderTableMeta
.CONTENT_URI_SHARE
,
700 key
+ "=? AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
701 + "=?", new String
[] { value
, mAccount
.name
},
703 } catch (RemoteException e
) {
704 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
711 private OCFile
createFileInstance(Cursor c
) {
714 file
= new OCFile(c
.getString(c
715 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
716 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
717 file
.setParentId(c
.getLong(c
718 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
719 file
.setMimetype(c
.getString(c
720 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
721 if (!file
.isFolder()) {
722 file
.setStoragePath(c
.getString(c
723 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
724 if (file
.getStoragePath() == null
) {
725 // 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
726 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
728 file
.setStoragePath(f
.getAbsolutePath());
729 file
.setLastSyncDateForData(f
.lastModified());
733 file
.setFileLength(c
.getLong(c
734 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
735 file
.setCreationTimestamp(c
.getLong(c
736 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
737 file
.setModificationTimestamp(c
.getLong(c
738 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
739 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
740 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
741 file
.setLastSyncDateForProperties(c
.getLong(c
742 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
743 file
.setLastSyncDateForData(c
.getLong(c
.
744 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
745 file
.setKeepInSync(c
.getInt(
746 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
747 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
748 file
.setShareByLink(c
.getInt(
749 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
750 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
757 * Returns if the file/folder is shared by link or not
758 * @param path Path of the file/folder
761 public boolean isShareByLink(String path
) {
762 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
764 if (c
.moveToFirst()) {
765 file
= createFileInstance(c
);
768 return file
.isShareByLink();
772 * Returns the public link of the file/folder
773 * @param path Path of the file/folder
776 public String
getPublicLink(String path
) {
777 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
779 if (c
.moveToFirst()) {
780 file
= createFileInstance(c
);
783 return file
.getPublicLink();
787 // Methods for Shares
788 public boolean saveShare(OCShare share
) {
789 boolean overriden
= false
;
790 ContentValues cv
= new ContentValues();
791 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
792 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
793 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
794 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
795 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
796 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
797 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
798 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
799 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
800 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
801 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
802 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
803 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
804 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
806 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
809 if (getContentResolver() != null
) {
810 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
811 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
812 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
815 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
816 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
817 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
818 } catch (RemoteException e
) {
820 "Fail to insert insert file to database "
825 Uri result_uri
= null
;
826 if (getContentResolver() != null
) {
827 result_uri
= getContentResolver().insert(
828 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
831 result_uri
= getContentProviderClient().insert(
832 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
833 } catch (RemoteException e
) {
835 "Fail to insert insert file to database "
839 if (result_uri
!= null
) {
840 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
849 // private OCShare getShareById(long id) {
850 // Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
851 // OCShare share = null;
852 // if (c.moveToFirst()) {
853 // share = createShareInstance(c);
859 // private OCShare getShareByRemoteId(long remoteId) {
860 // Cursor c = getShareCursorForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
861 // OCShare share = null;
862 // if (c.moveToFirst()) {
863 // share = createShareInstance(c);
869 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
871 if (getContentResolver() != null
) {
872 c
= getContentResolver().query(
873 ProviderTableMeta
.CONTENT_URI_SHARE
,
875 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
876 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
877 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
878 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
882 c
= getContentProviderClient().query(
883 ProviderTableMeta
.CONTENT_URI_SHARE
,
885 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
886 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
887 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
888 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
891 } catch (RemoteException e
) {
892 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
896 OCShare share
= null
;
897 if (c
.moveToFirst()) {
898 share
= createShareInstance(c
);
904 private OCShare
createShareInstance(Cursor c
) {
905 OCShare share
= null
;
907 share
= new OCShare(c
.getString(c
908 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
909 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
910 share
.setFileSource(c
.getLong(c
911 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
912 share
.setShareType(ShareType
.fromValue(c
.getInt(c
913 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
914 share
.setPermissions(c
.getInt(c
915 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
916 share
.setSharedDate(c
.getLong(c
917 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
918 share
.setExpirationDate(c
.getLong(c
919 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
920 share
.setToken(c
.getString(c
921 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
922 share
.setSharedWithDisplayName(c
.getString(c
923 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
924 share
.setIsFolder(c
.getInt(
925 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
926 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
927 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
933 private boolean shareExists(String cmp_key
, String value
) {
935 if (getContentResolver() != null
) {
936 c
= getContentResolver()
937 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
940 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
942 new String
[] { value
, mAccount
.name
}, null
);
945 c
= getContentProviderClient().query(
946 ProviderTableMeta
.CONTENT_URI_SHARE
,
949 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
950 new String
[] { value
, mAccount
.name
}, null
);
951 } catch (RemoteException e
) {
953 "Couldn't determine file existance, assuming non existance: "
958 boolean retval
= c
.moveToFirst();
963 private boolean shareExists(long remoteId
) {
964 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
967 private void cleanSharedFiles() {
968 ContentValues cv
= new ContentValues();
969 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
970 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
971 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
972 String
[] whereArgs
= new String
[]{mAccount
.name
};
974 if (getContentResolver() != null
) {
975 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
979 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
981 } catch (RemoteException e
) {
982 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
987 private void cleanSharedFilesInFolder(OCFile folder
) {
988 ContentValues cv
= new ContentValues();
989 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
990 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
991 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
992 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
994 if (getContentResolver() != null
) {
995 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
999 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1001 } catch (RemoteException e
) {
1002 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1007 private void cleanShares() {
1008 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1009 String
[] whereArgs
= new String
[]{mAccount
.name
};
1011 if (getContentResolver() != null
) {
1012 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1016 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1018 } catch (RemoteException e
) {
1019 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1024 public void saveShares(Collection
<OCShare
> shares
) {
1026 if (shares
!= null
) {
1027 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1029 // prepare operations to insert or update files to save in the given folder
1030 for (OCShare share
: shares
) {
1031 ContentValues cv
= new ContentValues();
1032 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1033 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1034 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1035 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1036 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1037 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1038 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1039 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1040 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1041 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1042 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1043 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1044 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1045 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1047 if (shareExists(share
.getIdRemoteShared())) {
1048 // updating an existing file
1049 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1051 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1052 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1056 // adding a new file
1057 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1061 // apply operations in batch
1062 if (operations
.size() > 0) {
1063 @SuppressWarnings("unused")
1064 ContentProviderResult
[] results
= null
;
1065 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1067 if (getContentResolver() != null
) {
1068 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1071 results
= getContentProviderClient().applyBatch(operations
);
1074 } catch (OperationApplicationException e
) {
1075 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1077 } catch (RemoteException e
) {
1078 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1085 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1088 if (sharedFiles
!= null
) {
1089 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1091 // prepare operations to insert or update files to save in the given folder
1092 for (OCFile file
: sharedFiles
) {
1093 ContentValues cv
= new ContentValues();
1094 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1095 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1096 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1097 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1098 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1099 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1100 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1101 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1102 if (!file
.isFolder()) {
1103 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1105 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1106 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1107 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1108 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1109 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1110 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1111 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1113 boolean existsByPath
= fileExists(file
.getRemotePath());
1114 if (existsByPath
|| fileExists(file
.getFileId())) {
1115 // updating an existing file
1116 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1118 withSelection( ProviderTableMeta
._ID
+ "=?",
1119 new String
[] { String
.valueOf(file
.getFileId()) })
1123 // adding a new file
1124 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1128 // apply operations in batch
1129 if (operations
.size() > 0) {
1130 @SuppressWarnings("unused")
1131 ContentProviderResult
[] results
= null
;
1132 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1134 if (getContentResolver() != null
) {
1135 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1138 results
= getContentProviderClient().applyBatch(operations
);
1141 } catch (OperationApplicationException e
) {
1142 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1144 } catch (RemoteException e
) {
1145 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1152 public void removeShare(OCShare share
){
1153 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1154 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1155 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1156 if (getContentProviderClient() != null
) {
1158 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1159 } catch (RemoteException e
) {
1160 e
.printStackTrace();
1163 getContentResolver().delete(share_uri
, where
, whereArgs
);
1167 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1170 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1172 for (OCShare share
: shares
) {
1174 String path
= share
.getPath();
1175 if (share
.isFolder()) {
1176 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1179 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1180 OCFile file
= getFileByPath(path
);
1182 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1183 file
.setShareByLink(true
);
1184 sharedFiles
.add(file
);
1189 updateSharedFiles(sharedFiles
);
1193 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1194 cleanSharedFilesInFolder(folder
);
1195 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1196 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1198 if (shares
!= null
) {
1199 // prepare operations to insert or update files to save in the given folder
1200 for (OCShare share
: shares
) {
1201 ContentValues cv
= new ContentValues();
1202 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1203 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1204 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1205 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1206 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1207 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1208 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1209 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1210 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1211 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1212 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1213 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1214 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1215 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1218 if (shareExists(share.getIdRemoteShared())) {
1219 // updating an existing share resource
1220 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1222 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1223 new String[] { String.valueOf(share.getIdRemoteShared()) })
1228 // adding a new share resource
1229 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1234 // apply operations in batch
1235 if (operations
.size() > 0) {
1236 @SuppressWarnings("unused")
1237 ContentProviderResult
[] results
= null
;
1238 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1240 if (getContentResolver() != null
) {
1241 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1244 results
= getContentProviderClient().applyBatch(operations
);
1247 } catch (OperationApplicationException e
) {
1248 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1250 } catch (RemoteException e
) {
1251 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1258 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1259 if (folder
!= null
) {
1260 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1261 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1263 Vector
<OCFile
> files
= getFolderContent(folder
);
1265 for (OCFile file
: files
) {
1266 whereArgs
[0] = file
.getRemotePath();
1267 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1268 .withSelection(where
, whereArgs
)
1272 return preparedOperations
;
1275 if (operations.size() > 0) {
1277 if (getContentResolver() != null) {
1278 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1281 getContentProviderClient().applyBatch(operations);
1284 } catch (OperationApplicationException e) {
1285 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1287 } catch (RemoteException e) {
1288 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1294 if (getContentResolver() != null) {
1296 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1301 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1305 } catch (RemoteException e) {
1306 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());