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());
190 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
192 boolean sameRemotePath
= fileExists(file
.getRemotePath());
193 if (sameRemotePath
||
194 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
196 OCFile oldFile
= null
;
197 if (sameRemotePath
) {
198 oldFile
= getFileByPath(file
.getRemotePath());
199 file
.setFileId(oldFile
.getFileId());
201 oldFile
= getFileById(file
.getFileId());
205 if (getContentResolver() != null
) {
206 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
207 ProviderTableMeta
._ID
+ "=?",
208 new String
[] { String
.valueOf(file
.getFileId()) });
211 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
212 cv
, ProviderTableMeta
._ID
+ "=?",
213 new String
[] { String
.valueOf(file
.getFileId()) });
214 } catch (RemoteException e
) {
216 "Fail to insert insert file to database "
221 Uri result_uri
= null
;
222 if (getContentResolver() != null
) {
223 result_uri
= getContentResolver().insert(
224 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
227 result_uri
= getContentProviderClient().insert(
228 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
229 } catch (RemoteException e
) {
231 "Fail to insert insert file to database "
235 if (result_uri
!= null
) {
236 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
238 file
.setFileId(new_id
);
242 // if (file.isFolder()) {
243 // updateFolderSize(file.getFileId());
245 // updateFolderSize(file.getParentId());
253 * Inserts or updates the list of files contained in a given folder.
255 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
256 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
260 * @param removeNotUpdated
262 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
264 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
266 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
268 // prepare operations to insert or update files to save in the given folder
269 for (OCFile file
: updatedFiles
) {
270 ContentValues cv
= new ContentValues();
271 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
272 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
273 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
274 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
275 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
276 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
277 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
278 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
279 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
280 if (!file
.isFolder()) {
281 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
283 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
284 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
285 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
286 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
287 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
288 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
289 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
290 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
291 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
293 boolean existsByPath
= fileExists(file
.getRemotePath());
294 if (existsByPath
|| fileExists(file
.getFileId())) {
295 // updating an existing file
296 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
298 withSelection( ProviderTableMeta
._ID
+ "=?",
299 new String
[] { String
.valueOf(file
.getFileId()) })
304 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
308 // prepare operations to remove files in the given folder
309 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
310 String
[] whereArgs
= null
;
311 for (OCFile file
: filesToRemove
) {
312 if (file
.getParentId() == folder
.getFileId()) {
313 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
314 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
315 if (file
.isFolder()) {
316 operations
.add(ContentProviderOperation
317 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
319 // TODO remove local folder
321 operations
.add(ContentProviderOperation
322 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
325 new File(file
.getStoragePath()).delete();
326 // TODO move the deletion of local contents after success of deletions
332 // update metadata of folder
333 ContentValues cv
= new ContentValues();
334 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
335 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
336 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
337 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
338 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
339 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
340 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
341 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
342 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
343 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
344 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
345 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
346 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
347 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
348 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
349 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
350 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
352 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
354 withSelection( ProviderTableMeta
._ID
+ "=?",
355 new String
[] { String
.valueOf(folder
.getFileId()) })
358 // apply operations in batch
359 ContentProviderResult
[] results
= null
;
360 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
362 if (getContentResolver() != null
) {
363 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
366 results
= getContentProviderClient().applyBatch(operations
);
369 } catch (OperationApplicationException e
) {
370 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
372 } catch (RemoteException e
) {
373 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
376 // update new id in file objects for insertions
377 if (results
!= null
) {
379 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
381 for (int i
=0; i
<results
.length
; i
++) {
382 if (filesIt
.hasNext()) {
383 file
= filesIt
.next();
387 if (results
[i
].uri
!= null
) {
388 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
389 //updatedFiles.get(i).setFileId(newId);
391 file
.setFileId(newId
);
397 //updateFolderSize(folder.getFileId());
406 // private void updateFolderSize(long id) {
407 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
408 // Log_OC.d(TAG, "Updating size of " + id);
409 // if (getContentResolver() != null) {
410 // getContentResolver().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) });
416 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
417 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
418 // ProviderTableMeta._ID + "=?",
419 // new String[] { String.valueOf(id) });
421 // } catch (RemoteException e) {
422 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
426 // Log_OC.e(TAG, "not updating size for folder " + id);
431 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
432 boolean success
= true
;
434 if (file
.isFolder()) {
435 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
439 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
440 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
441 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
442 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
444 if (getContentProviderClient() != null
) {
446 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
447 } catch (RemoteException e
) {
451 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
453 success
&= (deleted
> 0);
455 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
456 success
= new File(file
.getStoragePath()).delete();
457 if (!removeDBData
&& success
) {
458 // maybe unnecessary, but should be checked TODO remove if unnecessary
459 file
.setStoragePath(null
);
469 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
470 boolean success
= true
;
471 if (folder
!= null
&& folder
.isFolder()) {
472 if (removeDBData
&& folder
.getFileId() != -1) {
473 success
= removeFolderInDb(folder
);
475 if (removeLocalContent
&& success
) {
476 success
= removeLocalFolder(folder
);
482 private boolean removeFolderInDb(OCFile folder
) {
483 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
484 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
485 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
487 if (getContentProviderClient() != null
) {
489 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
490 } catch (RemoteException e
) {
494 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
499 private boolean removeLocalFolder(OCFile folder
) {
500 boolean success
= true
;
501 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
502 if (localFolder
.exists()) {
503 // stage 1: remove the local files already registered in the files database
504 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
506 for (OCFile file
: files
) {
507 if (file
.isFolder()) {
508 success
&= removeLocalFolder(file
);
511 File localFile
= new File(file
.getStoragePath());
512 success
&= localFile
.delete();
514 file
.setStoragePath(null
);
522 // stage 2: remove the folder itself and any local file inside out of sync;
523 // for instance, after clearing the app cache or reinstalling
524 success
&= removeLocalFolder(localFolder
);
529 private boolean removeLocalFolder(File localFolder
) {
530 boolean success
= true
;
531 File
[] localFiles
= localFolder
.listFiles();
532 if (localFiles
!= null
) {
533 for (File localFile
: localFiles
) {
534 if (localFile
.isDirectory()) {
535 success
&= removeLocalFolder(localFile
);
537 success
&= localFile
.delete();
541 success
&= localFolder
.delete();
546 * Updates database for a folder that was moved to a different location.
548 * TODO explore better (faster) implementations
549 * TODO throw exceptions up !
551 public void moveFolder(OCFile folder
, String newPath
) {
552 // TODO check newPath
554 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
555 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
557 if (getContentProviderClient() != null
) {
559 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
561 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
562 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
563 } catch (RemoteException e
) {
564 Log_OC
.e(TAG
, e
.getMessage());
567 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
569 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
570 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
573 /// 2. prepare a batch of update operations to change all the descendants
574 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
575 int lengthOfOldPath
= folder
.getRemotePath().length();
576 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
577 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
578 if (c
.moveToFirst()) {
580 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
581 OCFile child
= createFileInstance(c
);
582 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
583 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
584 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
586 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
588 withSelection( ProviderTableMeta
._ID
+ "=?",
589 new String
[] { String
.valueOf(child
.getFileId()) })
591 } while (c
.moveToNext());
595 /// 3. apply updates in batch
597 if (getContentResolver() != null
) {
598 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
601 getContentProviderClient().applyBatch(operations
);
604 } catch (OperationApplicationException e
) {
605 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
607 } catch (RemoteException e
) {
608 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
615 private Vector
<OCFile
> getFolderContent(long parentId
) {
617 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
619 Uri req_uri
= Uri
.withAppendedPath(
620 ProviderTableMeta
.CONTENT_URI_DIR
,
621 String
.valueOf(parentId
));
624 if (getContentProviderClient() != null
) {
626 c
= getContentProviderClient().query(req_uri
, null
,
627 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
628 new String
[] { String
.valueOf(parentId
)}, null
);
629 } catch (RemoteException e
) {
630 Log_OC
.e(TAG
, e
.getMessage());
634 c
= getContentResolver().query(req_uri
, null
,
635 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
636 new String
[] { String
.valueOf(parentId
)}, null
);
639 if (c
.moveToFirst()) {
641 OCFile child
= createFileInstance(c
);
643 } while (c
.moveToNext());
648 Collections
.sort(ret
);
654 private OCFile
createRootDir() {
655 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
656 file
.setMimetype("DIR");
657 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
662 private boolean fileExists(String cmp_key
, String value
) {
664 if (getContentResolver() != null
) {
665 c
= getContentResolver()
666 .query(ProviderTableMeta
.CONTENT_URI
,
669 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
671 new String
[] { value
, mAccount
.name
}, null
);
674 c
= getContentProviderClient().query(
675 ProviderTableMeta
.CONTENT_URI
,
678 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
679 new String
[] { value
, mAccount
.name
}, null
);
680 } catch (RemoteException e
) {
682 "Couldn't determine file existance, assuming non existance: "
687 boolean retval
= c
.moveToFirst();
692 private Cursor
getCursorForValue(String key
, String value
) {
694 if (getContentResolver() != null
) {
695 c
= getContentResolver()
696 .query(ProviderTableMeta
.CONTENT_URI
,
699 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
701 new String
[] { value
, mAccount
.name
}, null
);
704 c
= getContentProviderClient().query(
705 ProviderTableMeta
.CONTENT_URI
,
707 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
708 + "=?", new String
[] { value
, mAccount
.name
},
710 } catch (RemoteException e
) {
711 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
719 private OCFile
createFileInstance(Cursor c
) {
722 file
= new OCFile(c
.getString(c
723 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
724 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
725 file
.setParentId(c
.getLong(c
726 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
727 file
.setMimetype(c
.getString(c
728 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
729 if (!file
.isFolder()) {
730 file
.setStoragePath(c
.getString(c
731 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
732 if (file
.getStoragePath() == null
) {
733 // 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
734 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
736 file
.setStoragePath(f
.getAbsolutePath());
737 file
.setLastSyncDateForData(f
.lastModified());
741 file
.setFileLength(c
.getLong(c
742 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
743 file
.setCreationTimestamp(c
.getLong(c
744 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
745 file
.setModificationTimestamp(c
.getLong(c
746 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
747 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
748 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
749 file
.setLastSyncDateForProperties(c
.getLong(c
750 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
751 file
.setLastSyncDateForData(c
.getLong(c
.
752 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
753 file
.setKeepInSync(c
.getInt(
754 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
755 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
756 file
.setShareByLink(c
.getInt(
757 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
758 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
759 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
760 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
767 * Returns if the file/folder is shared by link or not
768 * @param path Path of the file/folder
771 public boolean isShareByLink(String path
) {
772 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
774 if (c
.moveToFirst()) {
775 file
= createFileInstance(c
);
778 return file
.isShareByLink();
782 * Returns the public link of the file/folder
783 * @param path Path of the file/folder
786 public String
getPublicLink(String path
) {
787 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
789 if (c
.moveToFirst()) {
790 file
= createFileInstance(c
);
793 return file
.getPublicLink();
797 // Methods for Shares
798 public boolean saveShare(OCShare share
) {
799 boolean overriden
= false
;
800 ContentValues cv
= new ContentValues();
801 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
802 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
803 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
804 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
805 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
806 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
807 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
808 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
809 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
810 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
811 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
812 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
813 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
814 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
816 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
819 if (getContentResolver() != null
) {
820 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
821 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
822 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
825 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
826 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
827 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
828 } catch (RemoteException e
) {
830 "Fail to insert insert file to database "
835 Uri result_uri
= null
;
836 if (getContentResolver() != null
) {
837 result_uri
= getContentResolver().insert(
838 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
841 result_uri
= getContentProviderClient().insert(
842 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
843 } catch (RemoteException e
) {
845 "Fail to insert insert file to database "
849 if (result_uri
!= null
) {
850 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
860 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
862 if (getContentResolver() != null
) {
863 c
= getContentResolver().query(
864 ProviderTableMeta
.CONTENT_URI_SHARE
,
866 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
867 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
868 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
869 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
873 c
= getContentProviderClient().query(
874 ProviderTableMeta
.CONTENT_URI_SHARE
,
876 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
877 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
878 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
879 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
882 } catch (RemoteException e
) {
883 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
887 OCShare share
= null
;
888 if (c
.moveToFirst()) {
889 share
= createShareInstance(c
);
895 private OCShare
createShareInstance(Cursor c
) {
896 OCShare share
= null
;
898 share
= new OCShare(c
.getString(c
899 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
900 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
901 share
.setFileSource(c
.getLong(c
902 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
903 share
.setShareType(ShareType
.fromValue(c
.getInt(c
904 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
905 share
.setPermissions(c
.getInt(c
906 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
907 share
.setSharedDate(c
.getLong(c
908 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
909 share
.setExpirationDate(c
.getLong(c
910 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
911 share
.setToken(c
.getString(c
912 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
913 share
.setSharedWithDisplayName(c
.getString(c
914 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
915 share
.setIsFolder(c
.getInt(
916 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
917 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
918 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
924 private boolean shareExists(String cmp_key
, String value
) {
926 if (getContentResolver() != null
) {
927 c
= getContentResolver()
928 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
931 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
933 new String
[] { value
, mAccount
.name
}, null
);
936 c
= getContentProviderClient().query(
937 ProviderTableMeta
.CONTENT_URI_SHARE
,
940 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
941 new String
[] { value
, mAccount
.name
}, null
);
942 } catch (RemoteException e
) {
944 "Couldn't determine file existance, assuming non existance: "
949 boolean retval
= c
.moveToFirst();
954 private boolean shareExists(long remoteId
) {
955 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
958 private void cleanSharedFiles() {
959 ContentValues cv
= new ContentValues();
960 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
961 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
962 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
963 String
[] whereArgs
= new String
[]{mAccount
.name
};
965 if (getContentResolver() != null
) {
966 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
970 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
972 } catch (RemoteException e
) {
973 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
978 private void cleanSharedFilesInFolder(OCFile folder
) {
979 ContentValues cv
= new ContentValues();
980 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
981 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
982 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
983 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
985 if (getContentResolver() != null
) {
986 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
990 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
992 } catch (RemoteException e
) {
993 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
998 private void cleanShares() {
999 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1000 String
[] whereArgs
= new String
[]{mAccount
.name
};
1002 if (getContentResolver() != null
) {
1003 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1007 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1009 } catch (RemoteException e
) {
1010 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1015 public void saveShares(Collection
<OCShare
> shares
) {
1017 if (shares
!= null
) {
1018 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1020 // prepare operations to insert or update files to save in the given folder
1021 for (OCShare share
: shares
) {
1022 ContentValues cv
= new ContentValues();
1023 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1024 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1025 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1026 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1027 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1028 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1029 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1030 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1031 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1032 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1033 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1034 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1035 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1036 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1038 if (shareExists(share
.getIdRemoteShared())) {
1039 // updating an existing file
1040 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1042 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1043 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1047 // adding a new file
1048 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1052 // apply operations in batch
1053 if (operations
.size() > 0) {
1054 @SuppressWarnings("unused")
1055 ContentProviderResult
[] results
= null
;
1056 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1058 if (getContentResolver() != null
) {
1059 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1062 results
= getContentProviderClient().applyBatch(operations
);
1065 } catch (OperationApplicationException e
) {
1066 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1068 } catch (RemoteException e
) {
1069 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1076 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1079 if (sharedFiles
!= null
) {
1080 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1082 // prepare operations to insert or update files to save in the given folder
1083 for (OCFile file
: sharedFiles
) {
1084 ContentValues cv
= new ContentValues();
1085 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1086 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1087 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1088 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1089 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1090 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1091 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1092 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1093 if (!file
.isFolder()) {
1094 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1096 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1097 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1098 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1099 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1100 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1101 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1102 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1103 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1104 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1106 boolean existsByPath
= fileExists(file
.getRemotePath());
1107 if (existsByPath
|| fileExists(file
.getFileId())) {
1108 // updating an existing file
1109 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1111 withSelection( ProviderTableMeta
._ID
+ "=?",
1112 new String
[] { String
.valueOf(file
.getFileId()) })
1116 // adding a new file
1117 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1121 // apply operations in batch
1122 if (operations
.size() > 0) {
1123 @SuppressWarnings("unused")
1124 ContentProviderResult
[] results
= null
;
1125 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1127 if (getContentResolver() != null
) {
1128 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1131 results
= getContentProviderClient().applyBatch(operations
);
1134 } catch (OperationApplicationException e
) {
1135 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1137 } catch (RemoteException e
) {
1138 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1145 public void removeShare(OCShare share
){
1146 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1147 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1148 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1149 if (getContentProviderClient() != null
) {
1151 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1152 } catch (RemoteException e
) {
1153 e
.printStackTrace();
1156 getContentResolver().delete(share_uri
, where
, whereArgs
);
1160 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1163 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1165 for (OCShare share
: shares
) {
1167 String path
= share
.getPath();
1168 if (share
.isFolder()) {
1169 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1172 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1173 OCFile file
= getFileByPath(path
);
1175 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1176 file
.setShareByLink(true
);
1177 sharedFiles
.add(file
);
1182 updateSharedFiles(sharedFiles
);
1186 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1187 cleanSharedFilesInFolder(folder
);
1188 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1189 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1191 if (shares
!= null
) {
1192 // prepare operations to insert or update files to save in the given folder
1193 for (OCShare share
: shares
) {
1194 ContentValues cv
= new ContentValues();
1195 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1196 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1197 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1198 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1199 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1200 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1201 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1202 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1203 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1204 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1205 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1206 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1207 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1208 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1211 if (shareExists(share.getIdRemoteShared())) {
1212 // updating an existing share resource
1213 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1215 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1216 new String[] { String.valueOf(share.getIdRemoteShared()) })
1221 // adding a new share resource
1222 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1227 // apply operations in batch
1228 if (operations
.size() > 0) {
1229 @SuppressWarnings("unused")
1230 ContentProviderResult
[] results
= null
;
1231 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1233 if (getContentResolver() != null
) {
1234 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1237 results
= getContentProviderClient().applyBatch(operations
);
1240 } catch (OperationApplicationException e
) {
1241 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1243 } catch (RemoteException e
) {
1244 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1251 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1252 if (folder
!= null
) {
1253 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1254 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1256 Vector
<OCFile
> files
= getFolderContent(folder
);
1258 for (OCFile file
: files
) {
1259 whereArgs
[0] = file
.getRemotePath();
1260 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1261 .withSelection(where
, whereArgs
)
1265 return preparedOperations
;
1268 if (operations.size() > 0) {
1270 if (getContentResolver() != null) {
1271 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1274 getContentProviderClient().applyBatch(operations);
1277 } catch (OperationApplicationException e) {
1278 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1280 } catch (RemoteException e) {
1281 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1287 if (getContentResolver() != null) {
1289 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1294 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1298 } catch (RemoteException e) {
1299 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());