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());
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 success
= removeLocalFolder(folder
);
476 private boolean removeFolderInDb(OCFile folder
) {
477 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
478 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
479 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
481 if (getContentProviderClient() != null
) {
483 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
484 } catch (RemoteException e
) {
488 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
493 private boolean removeLocalFolder(OCFile folder
) {
494 boolean success
= true
;
495 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
496 if (localFolder
.exists()) {
497 // stage 1: remove the local files already registered in the files database
498 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
500 for (OCFile file
: files
) {
501 if (file
.isFolder()) {
502 success
&= removeLocalFolder(file
);
505 File localFile
= new File(file
.getStoragePath());
506 success
&= localFile
.delete();
508 file
.setStoragePath(null
);
516 // stage 2: remove the folder itself and any local file inside out of sync;
517 // for instance, after clearing the app cache or reinstalling
518 success
&= removeLocalFolder(localFolder
);
523 private boolean removeLocalFolder(File localFolder
) {
524 boolean success
= true
;
525 File
[] localFiles
= localFolder
.listFiles();
526 if (localFiles
!= null
) {
527 for (File localFile
: localFiles
) {
528 if (localFile
.isDirectory()) {
529 success
&= removeLocalFolder(localFile
);
531 success
&= localFile
.delete();
535 success
&= localFolder
.delete();
540 * Updates database for a folder that was moved to a different location.
542 * TODO explore better (faster) implementations
543 * TODO throw exceptions up !
545 public void moveFolder(OCFile folder
, String newPath
) {
546 // TODO check newPath
548 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
549 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
551 if (getContentProviderClient() != null
) {
553 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
555 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
556 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
557 } catch (RemoteException e
) {
558 Log_OC
.e(TAG
, e
.getMessage());
561 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
563 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
564 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
567 /// 2. prepare a batch of update operations to change all the descendants
568 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
569 int lengthOfOldPath
= folder
.getRemotePath().length();
570 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
571 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
572 if (c
.moveToFirst()) {
574 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
575 OCFile child
= createFileInstance(c
);
576 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
577 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
578 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
580 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
582 withSelection( ProviderTableMeta
._ID
+ "=?",
583 new String
[] { String
.valueOf(child
.getFileId()) })
585 } while (c
.moveToNext());
589 /// 3. apply updates in batch
591 if (getContentResolver() != null
) {
592 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
595 getContentProviderClient().applyBatch(operations
);
598 } catch (OperationApplicationException e
) {
599 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
601 } catch (RemoteException e
) {
602 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
609 private Vector
<OCFile
> getFolderContent(long parentId
) {
611 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
613 Uri req_uri
= Uri
.withAppendedPath(
614 ProviderTableMeta
.CONTENT_URI_DIR
,
615 String
.valueOf(parentId
));
618 if (getContentProviderClient() != null
) {
620 c
= getContentProviderClient().query(req_uri
, null
,
621 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
622 new String
[] { String
.valueOf(parentId
)}, null
);
623 } catch (RemoteException e
) {
624 Log_OC
.e(TAG
, e
.getMessage());
628 c
= getContentResolver().query(req_uri
, null
,
629 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
630 new String
[] { String
.valueOf(parentId
)}, null
);
633 if (c
.moveToFirst()) {
635 OCFile child
= createFileInstance(c
);
637 } while (c
.moveToNext());
642 Collections
.sort(ret
);
648 private OCFile
createRootDir() {
649 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
650 file
.setMimetype("DIR");
651 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
656 private boolean fileExists(String cmp_key
, String value
) {
658 if (getContentResolver() != null
) {
659 c
= getContentResolver()
660 .query(ProviderTableMeta
.CONTENT_URI
,
663 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
665 new String
[] { value
, mAccount
.name
}, null
);
668 c
= getContentProviderClient().query(
669 ProviderTableMeta
.CONTENT_URI
,
672 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
673 new String
[] { value
, mAccount
.name
}, null
);
674 } catch (RemoteException e
) {
676 "Couldn't determine file existance, assuming non existance: "
681 boolean retval
= c
.moveToFirst();
686 private Cursor
getCursorForValue(String key
, String value
) {
688 if (getContentResolver() != null
) {
689 c
= getContentResolver()
690 .query(ProviderTableMeta
.CONTENT_URI
,
693 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
695 new String
[] { value
, mAccount
.name
}, null
);
698 c
= getContentProviderClient().query(
699 ProviderTableMeta
.CONTENT_URI
,
701 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
702 + "=?", new String
[] { value
, mAccount
.name
},
704 } catch (RemoteException e
) {
705 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
712 private Cursor
getShareCursorForValue(String key
, String value
) {
714 if (getContentResolver() != null
) {
715 c
= getContentResolver()
716 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
719 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
721 new String
[] { value
, mAccount
.name
}, null
);
724 c
= getContentProviderClient().query(
725 ProviderTableMeta
.CONTENT_URI_SHARE
,
727 key
+ "=? AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
728 + "=?", new String
[] { value
, mAccount
.name
},
730 } catch (RemoteException e
) {
731 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
738 private OCFile
createFileInstance(Cursor c
) {
741 file
= new OCFile(c
.getString(c
742 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
743 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
744 file
.setParentId(c
.getLong(c
745 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
746 file
.setMimetype(c
.getString(c
747 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
748 if (!file
.isFolder()) {
749 file
.setStoragePath(c
.getString(c
750 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
751 if (file
.getStoragePath() == null
) {
752 // 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
753 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
755 file
.setStoragePath(f
.getAbsolutePath());
756 file
.setLastSyncDateForData(f
.lastModified());
760 file
.setFileLength(c
.getLong(c
761 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
762 file
.setCreationTimestamp(c
.getLong(c
763 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
764 file
.setModificationTimestamp(c
.getLong(c
765 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
766 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
767 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
768 file
.setLastSyncDateForProperties(c
.getLong(c
769 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
770 file
.setLastSyncDateForData(c
.getLong(c
.
771 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
772 file
.setKeepInSync(c
.getInt(
773 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
774 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
775 file
.setShareByLink(c
.getInt(
776 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
777 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
784 * Returns if the file/folder is shared by link or not
785 * @param path Path of the file/folder
788 public boolean isShareByLink(String path
) {
789 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
791 if (c
.moveToFirst()) {
792 file
= createFileInstance(c
);
795 return file
.isShareByLink();
799 * Returns the public link of the file/folder
800 * @param path Path of the file/folder
803 public String
getPublicLink(String path
) {
804 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
806 if (c
.moveToFirst()) {
807 file
= createFileInstance(c
);
810 return file
.getPublicLink();
814 // Methods for Shares
815 public boolean saveShare(OCShare share
) {
816 boolean overriden
= false
;
817 ContentValues cv
= new ContentValues();
818 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
819 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
820 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
821 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
822 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
823 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
824 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
825 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
826 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
827 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
828 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
829 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
830 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
831 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
833 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
836 if (getContentResolver() != null
) {
837 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
838 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
839 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
842 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
843 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
844 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
845 } catch (RemoteException e
) {
847 "Fail to insert insert file to database "
852 Uri result_uri
= null
;
853 if (getContentResolver() != null
) {
854 result_uri
= getContentResolver().insert(
855 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
858 result_uri
= getContentProviderClient().insert(
859 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
860 } catch (RemoteException e
) {
862 "Fail to insert insert file to database "
866 if (result_uri
!= null
) {
867 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
876 // private OCShare getShareById(long id) {
877 // Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
878 // OCShare share = null;
879 // if (c.moveToFirst()) {
880 // share = createShareInstance(c);
886 // private OCShare getShareByRemoteId(long remoteId) {
887 // Cursor c = getShareCursorForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
888 // OCShare share = null;
889 // if (c.moveToFirst()) {
890 // share = createShareInstance(c);
896 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
898 if (getContentResolver() != null
) {
899 c
= getContentResolver().query(
900 ProviderTableMeta
.CONTENT_URI_SHARE
,
902 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
903 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
904 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
905 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
909 c
= getContentProviderClient().query(
910 ProviderTableMeta
.CONTENT_URI_SHARE
,
912 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
913 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
914 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
915 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
918 } catch (RemoteException e
) {
919 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
923 OCShare share
= null
;
924 if (c
.moveToFirst()) {
925 share
= createShareInstance(c
);
931 private OCShare
createShareInstance(Cursor c
) {
932 OCShare share
= null
;
934 share
= new OCShare(c
.getString(c
935 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
936 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
937 share
.setFileSource(c
.getLong(c
938 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
939 share
.setShareType(ShareType
.fromValue(c
.getInt(c
940 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
941 share
.setPermissions(c
.getInt(c
942 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
943 share
.setSharedDate(c
.getLong(c
944 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
945 share
.setExpirationDate(c
.getLong(c
946 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
947 share
.setToken(c
.getString(c
948 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
949 share
.setSharedWithDisplayName(c
.getString(c
950 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
951 share
.setIsFolder(c
.getInt(
952 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
953 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
954 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
960 private boolean shareExists(String cmp_key
, String value
) {
962 if (getContentResolver() != null
) {
963 c
= getContentResolver()
964 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
967 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
969 new String
[] { value
, mAccount
.name
}, null
);
972 c
= getContentProviderClient().query(
973 ProviderTableMeta
.CONTENT_URI_SHARE
,
976 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
977 new String
[] { value
, mAccount
.name
}, null
);
978 } catch (RemoteException e
) {
980 "Couldn't determine file existance, assuming non existance: "
985 boolean retval
= c
.moveToFirst();
990 private boolean shareExists(long remoteId
) {
991 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
994 private void cleanSharedFiles() {
995 ContentValues cv
= new ContentValues();
996 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
997 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
998 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
999 String
[] whereArgs
= new String
[]{mAccount
.name
};
1001 if (getContentResolver() != null
) {
1002 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1006 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1008 } catch (RemoteException e
) {
1009 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1014 private void cleanSharedFilesInFolder(OCFile folder
) {
1015 ContentValues cv
= new ContentValues();
1016 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1017 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1018 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
1019 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1021 if (getContentResolver() != null
) {
1022 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1026 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1028 } catch (RemoteException e
) {
1029 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1034 private void cleanShares() {
1035 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1036 String
[] whereArgs
= new String
[]{mAccount
.name
};
1038 if (getContentResolver() != null
) {
1039 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1043 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1045 } catch (RemoteException e
) {
1046 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1051 public void saveShares(Collection
<OCShare
> shares
) {
1053 if (shares
!= null
) {
1054 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1056 // prepare operations to insert or update files to save in the given folder
1057 for (OCShare share
: shares
) {
1058 ContentValues cv
= new ContentValues();
1059 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1060 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1061 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1062 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1063 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1064 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1065 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1066 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1067 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1068 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1069 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1070 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1071 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1072 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1074 if (shareExists(share
.getIdRemoteShared())) {
1075 // updating an existing file
1076 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1078 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1079 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1083 // adding a new file
1084 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1088 // apply operations in batch
1089 if (operations
.size() > 0) {
1090 @SuppressWarnings("unused")
1091 ContentProviderResult
[] results
= null
;
1092 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1094 if (getContentResolver() != null
) {
1095 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1098 results
= getContentProviderClient().applyBatch(operations
);
1101 } catch (OperationApplicationException e
) {
1102 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1104 } catch (RemoteException e
) {
1105 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1112 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1115 if (sharedFiles
!= null
) {
1116 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1118 // prepare operations to insert or update files to save in the given folder
1119 for (OCFile file
: sharedFiles
) {
1120 ContentValues cv
= new ContentValues();
1121 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1122 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1123 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1124 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1125 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1126 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1127 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1128 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1129 if (!file
.isFolder()) {
1130 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1132 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1133 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1134 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1135 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1136 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1137 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1138 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1140 boolean existsByPath
= fileExists(file
.getRemotePath());
1141 if (existsByPath
|| fileExists(file
.getFileId())) {
1142 // updating an existing file
1143 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1145 withSelection( ProviderTableMeta
._ID
+ "=?",
1146 new String
[] { String
.valueOf(file
.getFileId()) })
1150 // adding a new file
1151 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1155 // apply operations in batch
1156 if (operations
.size() > 0) {
1157 @SuppressWarnings("unused")
1158 ContentProviderResult
[] results
= null
;
1159 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1161 if (getContentResolver() != null
) {
1162 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1165 results
= getContentProviderClient().applyBatch(operations
);
1168 } catch (OperationApplicationException e
) {
1169 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1171 } catch (RemoteException e
) {
1172 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1179 public void removeShare(OCShare share
){
1180 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1181 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1182 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1183 if (getContentProviderClient() != null
) {
1185 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1186 } catch (RemoteException e
) {
1187 e
.printStackTrace();
1190 getContentResolver().delete(share_uri
, where
, whereArgs
);
1194 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1197 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1199 for (OCShare share
: shares
) {
1201 String path
= share
.getPath();
1202 if (share
.isFolder()) {
1203 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1206 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1207 OCFile file
= getFileByPath(path
);
1209 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1210 file
.setShareByLink(true
);
1211 sharedFiles
.add(file
);
1216 updateSharedFiles(sharedFiles
);
1220 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1221 cleanSharedFilesInFolder(folder
);
1222 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1223 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1225 if (shares
!= null
) {
1226 // prepare operations to insert or update files to save in the given folder
1227 for (OCShare share
: shares
) {
1228 ContentValues cv
= new ContentValues();
1229 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1230 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1231 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1232 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1233 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1234 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1235 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1236 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1237 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1238 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1239 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1240 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1241 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1242 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1245 if (shareExists(share.getIdRemoteShared())) {
1246 // updating an existing share resource
1247 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1249 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1250 new String[] { String.valueOf(share.getIdRemoteShared()) })
1255 // adding a new share resource
1256 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1261 // apply operations in batch
1262 if (operations
.size() > 0) {
1263 @SuppressWarnings("unused")
1264 ContentProviderResult
[] results
= null
;
1265 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1267 if (getContentResolver() != null
) {
1268 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1271 results
= getContentProviderClient().applyBatch(operations
);
1274 } catch (OperationApplicationException e
) {
1275 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1277 } catch (RemoteException e
) {
1278 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1285 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1286 if (folder
!= null
) {
1287 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1288 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1290 Vector
<OCFile
> files
= getFolderContent(folder
);
1292 for (OCFile file
: files
) {
1293 whereArgs
[0] = file
.getRemotePath();
1294 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1295 .withSelection(where
, whereArgs
)
1299 return preparedOperations
;
1302 if (operations.size() > 0) {
1304 if (getContentResolver() != null) {
1305 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1308 getContentProviderClient().applyBatch(operations);
1311 } catch (OperationApplicationException e) {
1312 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1314 } catch (RemoteException e) {
1315 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1321 if (getContentResolver() != null) {
1323 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1328 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1332 } catch (RemoteException e) {
1333 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());