1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2014 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());
189 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
191 boolean sameRemotePath
= fileExists(file
.getRemotePath());
192 if (sameRemotePath
||
193 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
195 OCFile oldFile
= null
;
196 if (sameRemotePath
) {
197 oldFile
= getFileByPath(file
.getRemotePath());
198 file
.setFileId(oldFile
.getFileId());
200 oldFile
= getFileById(file
.getFileId());
204 if (getContentResolver() != null
) {
205 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
206 ProviderTableMeta
._ID
+ "=?",
207 new String
[] { String
.valueOf(file
.getFileId()) });
210 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
211 cv
, ProviderTableMeta
._ID
+ "=?",
212 new String
[] { String
.valueOf(file
.getFileId()) });
213 } catch (RemoteException e
) {
215 "Fail to insert insert file to database "
220 Uri result_uri
= null
;
221 if (getContentResolver() != null
) {
222 result_uri
= getContentResolver().insert(
223 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
226 result_uri
= getContentProviderClient().insert(
227 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
228 } catch (RemoteException e
) {
230 "Fail to insert insert file to database "
234 if (result_uri
!= null
) {
235 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
237 file
.setFileId(new_id
);
241 // if (file.isFolder()) {
242 // updateFolderSize(file.getFileId());
244 // updateFolderSize(file.getParentId());
252 * Inserts or updates the list of files contained in a given folder.
254 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
255 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
259 * @param removeNotUpdated
261 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
263 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
265 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
267 // prepare operations to insert or update files to save in the given folder
268 for (OCFile file
: updatedFiles
) {
269 ContentValues cv
= new ContentValues();
270 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
271 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
272 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
273 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
274 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
275 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
276 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
277 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
278 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
279 if (!file
.isFolder()) {
280 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
282 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
283 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
284 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
285 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
286 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
287 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
288 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
289 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
291 boolean existsByPath
= fileExists(file
.getRemotePath());
292 if (existsByPath
|| fileExists(file
.getFileId())) {
293 // updating an existing file
294 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
296 withSelection( ProviderTableMeta
._ID
+ "=?",
297 new String
[] { String
.valueOf(file
.getFileId()) })
302 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
306 // prepare operations to remove files in the given folder
307 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
308 String
[] whereArgs
= null
;
309 for (OCFile file
: filesToRemove
) {
310 if (file
.getParentId() == folder
.getFileId()) {
311 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
312 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
313 if (file
.isFolder()) {
314 operations
.add(ContentProviderOperation
315 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
317 // TODO remove local folder
319 operations
.add(ContentProviderOperation
320 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
323 new File(file
.getStoragePath()).delete();
324 // TODO move the deletion of local contents after success of deletions
330 // update metadata of folder
331 ContentValues cv
= new ContentValues();
332 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
333 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
334 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
335 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
336 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
337 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
338 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
339 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
340 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
341 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
342 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
343 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
344 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
345 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
346 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
347 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
349 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
351 withSelection( ProviderTableMeta
._ID
+ "=?",
352 new String
[] { String
.valueOf(folder
.getFileId()) })
355 // apply operations in batch
356 ContentProviderResult
[] results
= null
;
357 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
359 if (getContentResolver() != null
) {
360 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
363 results
= getContentProviderClient().applyBatch(operations
);
366 } catch (OperationApplicationException e
) {
367 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
369 } catch (RemoteException e
) {
370 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
373 // update new id in file objects for insertions
374 if (results
!= null
) {
376 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
378 for (int i
=0; i
<results
.length
; i
++) {
379 if (filesIt
.hasNext()) {
380 file
= filesIt
.next();
384 if (results
[i
].uri
!= null
) {
385 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
386 //updatedFiles.get(i).setFileId(newId);
388 file
.setFileId(newId
);
394 //updateFolderSize(folder.getFileId());
403 // private void updateFolderSize(long id) {
404 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
405 // Log_OC.d(TAG, "Updating size of " + id);
406 // if (getContentResolver() != null) {
407 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
408 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
409 // ProviderTableMeta._ID + "=?",
410 // new String[] { String.valueOf(id) });
413 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
414 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
415 // ProviderTableMeta._ID + "=?",
416 // new String[] { String.valueOf(id) });
418 // } catch (RemoteException e) {
419 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
423 // Log_OC.e(TAG, "not updating size for folder " + id);
428 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
429 boolean success
= true
;
431 if (file
.isFolder()) {
432 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
436 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
437 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
438 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
439 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
441 if (getContentProviderClient() != null
) {
443 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
444 } catch (RemoteException e
) {
448 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
450 success
&= (deleted
> 0);
452 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
453 success
= new File(file
.getStoragePath()).delete();
454 if (!removeDBData
&& success
) {
455 // maybe unnecessary, but should be checked TODO remove if unnecessary
456 file
.setStoragePath(null
);
466 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
467 boolean success
= true
;
468 if (folder
!= null
&& folder
.isFolder()) {
469 if (removeDBData
&& folder
.getFileId() != -1) {
470 success
= removeFolderInDb(folder
);
472 if (removeLocalContent
&& success
) {
473 success
= removeLocalFolder(folder
);
479 private boolean removeFolderInDb(OCFile folder
) {
480 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
481 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
482 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
484 if (getContentProviderClient() != null
) {
486 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
487 } catch (RemoteException e
) {
491 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
496 private boolean removeLocalFolder(OCFile folder
) {
497 boolean success
= true
;
498 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
499 if (localFolder
.exists()) {
500 // stage 1: remove the local files already registered in the files database
501 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
503 for (OCFile file
: files
) {
504 if (file
.isFolder()) {
505 success
&= removeLocalFolder(file
);
508 File localFile
= new File(file
.getStoragePath());
509 success
&= localFile
.delete();
511 file
.setStoragePath(null
);
519 // stage 2: remove the folder itself and any local file inside out of sync;
520 // for instance, after clearing the app cache or reinstalling
521 success
&= removeLocalFolder(localFolder
);
526 private boolean removeLocalFolder(File localFolder
) {
527 boolean success
= true
;
528 File
[] localFiles
= localFolder
.listFiles();
529 if (localFiles
!= null
) {
530 for (File localFile
: localFiles
) {
531 if (localFile
.isDirectory()) {
532 success
&= removeLocalFolder(localFile
);
534 success
&= localFile
.delete();
538 success
&= localFolder
.delete();
543 * Updates database for a folder that was moved to a different location.
545 * TODO explore better (faster) implementations
546 * TODO throw exceptions up !
548 public void moveFolder(OCFile folder
, String newPath
) {
549 // TODO check newPath
551 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
552 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
554 if (getContentProviderClient() != null
) {
556 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
558 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
559 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
560 } catch (RemoteException e
) {
561 Log_OC
.e(TAG
, e
.getMessage());
564 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
566 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
567 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
570 /// 2. prepare a batch of update operations to change all the descendants
571 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
572 int lengthOfOldPath
= folder
.getRemotePath().length();
573 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
574 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
575 if (c
.moveToFirst()) {
577 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
578 OCFile child
= createFileInstance(c
);
579 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
580 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
581 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
583 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
585 withSelection( ProviderTableMeta
._ID
+ "=?",
586 new String
[] { String
.valueOf(child
.getFileId()) })
588 } while (c
.moveToNext());
592 /// 3. apply updates in batch
594 if (getContentResolver() != null
) {
595 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
598 getContentProviderClient().applyBatch(operations
);
601 } catch (OperationApplicationException e
) {
602 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
604 } catch (RemoteException e
) {
605 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
612 private Vector
<OCFile
> getFolderContent(long parentId
) {
614 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
616 Uri req_uri
= Uri
.withAppendedPath(
617 ProviderTableMeta
.CONTENT_URI_DIR
,
618 String
.valueOf(parentId
));
621 if (getContentProviderClient() != null
) {
623 c
= getContentProviderClient().query(req_uri
, null
,
624 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
625 new String
[] { String
.valueOf(parentId
)}, null
);
626 } catch (RemoteException e
) {
627 Log_OC
.e(TAG
, e
.getMessage());
631 c
= getContentResolver().query(req_uri
, null
,
632 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
633 new String
[] { String
.valueOf(parentId
)}, null
);
636 if (c
.moveToFirst()) {
638 OCFile child
= createFileInstance(c
);
640 } while (c
.moveToNext());
645 Collections
.sort(ret
);
651 private OCFile
createRootDir() {
652 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
653 file
.setMimetype("DIR");
654 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
659 private boolean fileExists(String cmp_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
,
675 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
676 new String
[] { value
, mAccount
.name
}, null
);
677 } catch (RemoteException e
) {
679 "Couldn't determine file existance, assuming non existance: "
684 boolean retval
= c
.moveToFirst();
689 private Cursor
getCursorForValue(String key
, String value
) {
691 if (getContentResolver() != null
) {
692 c
= getContentResolver()
693 .query(ProviderTableMeta
.CONTENT_URI
,
696 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
698 new String
[] { value
, mAccount
.name
}, null
);
701 c
= getContentProviderClient().query(
702 ProviderTableMeta
.CONTENT_URI
,
704 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
705 + "=?", new String
[] { value
, mAccount
.name
},
707 } catch (RemoteException e
) {
708 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
716 private OCFile
createFileInstance(Cursor c
) {
719 file
= new OCFile(c
.getString(c
720 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
721 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
722 file
.setParentId(c
.getLong(c
723 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
724 file
.setMimetype(c
.getString(c
725 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
726 if (!file
.isFolder()) {
727 file
.setStoragePath(c
.getString(c
728 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
729 if (file
.getStoragePath() == null
) {
730 // 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
731 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
733 file
.setStoragePath(f
.getAbsolutePath());
734 file
.setLastSyncDateForData(f
.lastModified());
738 file
.setFileLength(c
.getLong(c
739 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
740 file
.setCreationTimestamp(c
.getLong(c
741 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
742 file
.setModificationTimestamp(c
.getLong(c
743 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
744 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
745 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
746 file
.setLastSyncDateForProperties(c
.getLong(c
747 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
748 file
.setLastSyncDateForData(c
.getLong(c
.
749 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
750 file
.setKeepInSync(c
.getInt(
751 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
752 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
753 file
.setShareByLink(c
.getInt(
754 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
755 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
756 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
763 * Returns if the file/folder is shared by link or not
764 * @param path Path of the file/folder
767 public boolean isShareByLink(String path
) {
768 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
770 if (c
.moveToFirst()) {
771 file
= createFileInstance(c
);
774 return file
.isShareByLink();
778 * Returns the public link of the file/folder
779 * @param path Path of the file/folder
782 public String
getPublicLink(String path
) {
783 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
785 if (c
.moveToFirst()) {
786 file
= createFileInstance(c
);
789 return file
.getPublicLink();
793 // Methods for Shares
794 public boolean saveShare(OCShare share
) {
795 boolean overriden
= false
;
796 ContentValues cv
= new ContentValues();
797 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
798 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
799 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
800 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
801 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
802 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
803 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
804 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
805 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
806 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
807 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
808 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
809 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
810 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
812 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
815 if (getContentResolver() != null
) {
816 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
817 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
818 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
821 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
822 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
823 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
824 } catch (RemoteException e
) {
826 "Fail to insert insert file to database "
831 Uri result_uri
= null
;
832 if (getContentResolver() != null
) {
833 result_uri
= getContentResolver().insert(
834 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
837 result_uri
= getContentProviderClient().insert(
838 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
839 } catch (RemoteException e
) {
841 "Fail to insert insert file to database "
845 if (result_uri
!= null
) {
846 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
856 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
858 if (getContentResolver() != null
) {
859 c
= getContentResolver().query(
860 ProviderTableMeta
.CONTENT_URI_SHARE
,
862 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
863 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
864 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
865 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
869 c
= getContentProviderClient().query(
870 ProviderTableMeta
.CONTENT_URI_SHARE
,
872 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
873 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
874 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
875 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
878 } catch (RemoteException e
) {
879 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
883 OCShare share
= null
;
884 if (c
.moveToFirst()) {
885 share
= createShareInstance(c
);
891 private OCShare
createShareInstance(Cursor c
) {
892 OCShare share
= null
;
894 share
= new OCShare(c
.getString(c
895 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
896 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
897 share
.setFileSource(c
.getLong(c
898 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
899 share
.setShareType(ShareType
.fromValue(c
.getInt(c
900 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
901 share
.setPermissions(c
.getInt(c
902 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
903 share
.setSharedDate(c
.getLong(c
904 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
905 share
.setExpirationDate(c
.getLong(c
906 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
907 share
.setToken(c
.getString(c
908 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
909 share
.setSharedWithDisplayName(c
.getString(c
910 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
911 share
.setIsFolder(c
.getInt(
912 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
913 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
914 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
920 private boolean shareExists(String cmp_key
, String value
) {
922 if (getContentResolver() != null
) {
923 c
= getContentResolver()
924 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
927 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
929 new String
[] { value
, mAccount
.name
}, null
);
932 c
= getContentProviderClient().query(
933 ProviderTableMeta
.CONTENT_URI_SHARE
,
936 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
937 new String
[] { value
, mAccount
.name
}, null
);
938 } catch (RemoteException e
) {
940 "Couldn't determine file existance, assuming non existance: "
945 boolean retval
= c
.moveToFirst();
950 private boolean shareExists(long remoteId
) {
951 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
954 private void cleanSharedFiles() {
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
+ "=?";
959 String
[] whereArgs
= new String
[]{mAccount
.name
};
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 cleanSharedFiles" + e
.getMessage());
974 private void cleanSharedFilesInFolder(OCFile folder
) {
975 ContentValues cv
= new ContentValues();
976 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
977 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
978 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
979 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
981 if (getContentResolver() != null
) {
982 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
986 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
988 } catch (RemoteException e
) {
989 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
994 private void cleanShares() {
995 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
996 String
[] whereArgs
= new String
[]{mAccount
.name
};
998 if (getContentResolver() != null
) {
999 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1003 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1005 } catch (RemoteException e
) {
1006 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1011 public void saveShares(Collection
<OCShare
> shares
) {
1013 if (shares
!= null
) {
1014 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1016 // prepare operations to insert or update files to save in the given folder
1017 for (OCShare share
: shares
) {
1018 ContentValues cv
= new ContentValues();
1019 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1020 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1021 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1022 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1023 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1024 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1025 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1026 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1027 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1028 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1029 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1030 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1031 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1032 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1034 if (shareExists(share
.getIdRemoteShared())) {
1035 // updating an existing file
1036 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1038 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1039 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1043 // adding a new file
1044 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1048 // apply operations in batch
1049 if (operations
.size() > 0) {
1050 @SuppressWarnings("unused")
1051 ContentProviderResult
[] results
= null
;
1052 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1054 if (getContentResolver() != null
) {
1055 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1058 results
= getContentProviderClient().applyBatch(operations
);
1061 } catch (OperationApplicationException e
) {
1062 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1064 } catch (RemoteException e
) {
1065 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1072 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1075 if (sharedFiles
!= null
) {
1076 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1078 // prepare operations to insert or update files to save in the given folder
1079 for (OCFile file
: sharedFiles
) {
1080 ContentValues cv
= new ContentValues();
1081 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1082 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1083 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1084 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1085 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1086 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1087 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1088 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1089 if (!file
.isFolder()) {
1090 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1092 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1093 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1094 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1095 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1096 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1097 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1098 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1099 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1101 boolean existsByPath
= fileExists(file
.getRemotePath());
1102 if (existsByPath
|| fileExists(file
.getFileId())) {
1103 // updating an existing file
1104 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1106 withSelection( ProviderTableMeta
._ID
+ "=?",
1107 new String
[] { String
.valueOf(file
.getFileId()) })
1111 // adding a new file
1112 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1116 // apply operations in batch
1117 if (operations
.size() > 0) {
1118 @SuppressWarnings("unused")
1119 ContentProviderResult
[] results
= null
;
1120 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1122 if (getContentResolver() != null
) {
1123 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1126 results
= getContentProviderClient().applyBatch(operations
);
1129 } catch (OperationApplicationException e
) {
1130 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1132 } catch (RemoteException e
) {
1133 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1140 public void removeShare(OCShare share
){
1141 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1142 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1143 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1144 if (getContentProviderClient() != null
) {
1146 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1147 } catch (RemoteException e
) {
1148 e
.printStackTrace();
1151 getContentResolver().delete(share_uri
, where
, whereArgs
);
1155 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1158 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1160 for (OCShare share
: shares
) {
1162 String path
= share
.getPath();
1163 if (share
.isFolder()) {
1164 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1167 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1168 OCFile file
= getFileByPath(path
);
1170 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1171 file
.setShareByLink(true
);
1172 sharedFiles
.add(file
);
1177 updateSharedFiles(sharedFiles
);
1181 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1182 cleanSharedFilesInFolder(folder
);
1183 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1184 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1186 if (shares
!= null
) {
1187 // prepare operations to insert or update files to save in the given folder
1188 for (OCShare share
: shares
) {
1189 ContentValues cv
= new ContentValues();
1190 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1191 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1192 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1193 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1194 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1195 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1196 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1197 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1198 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1199 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1200 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1201 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1202 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1203 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1206 if (shareExists(share.getIdRemoteShared())) {
1207 // updating an existing share resource
1208 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1210 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1211 new String[] { String.valueOf(share.getIdRemoteShared()) })
1216 // adding a new share resource
1217 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1222 // apply operations in batch
1223 if (operations
.size() > 0) {
1224 @SuppressWarnings("unused")
1225 ContentProviderResult
[] results
= null
;
1226 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1228 if (getContentResolver() != null
) {
1229 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1232 results
= getContentProviderClient().applyBatch(operations
);
1235 } catch (OperationApplicationException e
) {
1236 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1238 } catch (RemoteException e
) {
1239 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1246 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1247 if (folder
!= null
) {
1248 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1249 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1251 Vector
<OCFile
> files
= getFolderContent(folder
);
1253 for (OCFile file
: files
) {
1254 whereArgs
[0] = file
.getRemotePath();
1255 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1256 .withSelection(where
, whereArgs
)
1260 return preparedOperations
;
1263 if (operations.size() > 0) {
1265 if (getContentResolver() != null) {
1266 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1269 getContentProviderClient().applyBatch(operations);
1272 } catch (OperationApplicationException e) {
1273 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1275 } catch (RemoteException e) {
1276 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1282 if (getContentResolver() != null) {
1284 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1289 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1293 } catch (RemoteException e) {
1294 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());