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());
713 private OCFile
createFileInstance(Cursor c
) {
716 file
= new OCFile(c
.getString(c
717 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
718 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
719 file
.setParentId(c
.getLong(c
720 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
721 file
.setMimetype(c
.getString(c
722 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
723 if (!file
.isFolder()) {
724 file
.setStoragePath(c
.getString(c
725 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
726 if (file
.getStoragePath() == null
) {
727 // 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
728 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
730 file
.setStoragePath(f
.getAbsolutePath());
731 file
.setLastSyncDateForData(f
.lastModified());
735 file
.setFileLength(c
.getLong(c
736 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
737 file
.setCreationTimestamp(c
.getLong(c
738 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
739 file
.setModificationTimestamp(c
.getLong(c
740 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
741 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
742 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
743 file
.setLastSyncDateForProperties(c
.getLong(c
744 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
745 file
.setLastSyncDateForData(c
.getLong(c
.
746 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
747 file
.setKeepInSync(c
.getInt(
748 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
749 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
750 file
.setShareByLink(c
.getInt(
751 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
752 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
759 * Returns if the file/folder is shared by link or not
760 * @param path Path of the file/folder
763 public boolean isShareByLink(String path
) {
764 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
766 if (c
.moveToFirst()) {
767 file
= createFileInstance(c
);
770 return file
.isShareByLink();
774 * Returns the public link of the file/folder
775 * @param path Path of the file/folder
778 public String
getPublicLink(String path
) {
779 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
781 if (c
.moveToFirst()) {
782 file
= createFileInstance(c
);
785 return file
.getPublicLink();
789 // Methods for Shares
790 public boolean saveShare(OCShare share
) {
791 boolean overriden
= false
;
792 ContentValues cv
= new ContentValues();
793 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
794 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
795 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
796 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
797 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
798 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
799 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
800 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
801 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
802 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
803 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
804 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
805 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
806 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
808 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
811 if (getContentResolver() != null
) {
812 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
813 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
814 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
817 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
818 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
819 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
820 } catch (RemoteException e
) {
822 "Fail to insert insert file to database "
827 Uri result_uri
= null
;
828 if (getContentResolver() != null
) {
829 result_uri
= getContentResolver().insert(
830 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
833 result_uri
= getContentProviderClient().insert(
834 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
835 } catch (RemoteException e
) {
837 "Fail to insert insert file to database "
841 if (result_uri
!= null
) {
842 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
852 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
854 if (getContentResolver() != null
) {
855 c
= getContentResolver().query(
856 ProviderTableMeta
.CONTENT_URI_SHARE
,
858 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
859 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
860 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
861 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
865 c
= getContentProviderClient().query(
866 ProviderTableMeta
.CONTENT_URI_SHARE
,
868 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
869 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
870 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
871 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
874 } catch (RemoteException e
) {
875 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
879 OCShare share
= null
;
880 if (c
.moveToFirst()) {
881 share
= createShareInstance(c
);
887 private OCShare
createShareInstance(Cursor c
) {
888 OCShare share
= null
;
890 share
= new OCShare(c
.getString(c
891 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
892 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
893 share
.setFileSource(c
.getLong(c
894 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
895 share
.setShareType(ShareType
.fromValue(c
.getInt(c
896 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
897 share
.setPermissions(c
.getInt(c
898 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
899 share
.setSharedDate(c
.getLong(c
900 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
901 share
.setExpirationDate(c
.getLong(c
902 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
903 share
.setToken(c
.getString(c
904 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
905 share
.setSharedWithDisplayName(c
.getString(c
906 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
907 share
.setIsFolder(c
.getInt(
908 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
909 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
910 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
916 private boolean shareExists(String cmp_key
, String value
) {
918 if (getContentResolver() != null
) {
919 c
= getContentResolver()
920 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
923 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
925 new String
[] { value
, mAccount
.name
}, null
);
928 c
= getContentProviderClient().query(
929 ProviderTableMeta
.CONTENT_URI_SHARE
,
932 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
933 new String
[] { value
, mAccount
.name
}, null
);
934 } catch (RemoteException e
) {
936 "Couldn't determine file existance, assuming non existance: "
941 boolean retval
= c
.moveToFirst();
946 private boolean shareExists(long remoteId
) {
947 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
950 private void cleanSharedFiles() {
951 ContentValues cv
= new ContentValues();
952 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
953 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
954 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
955 String
[] whereArgs
= new String
[]{mAccount
.name
};
957 if (getContentResolver() != null
) {
958 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
962 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
964 } catch (RemoteException e
) {
965 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
970 private void cleanSharedFilesInFolder(OCFile folder
) {
971 ContentValues cv
= new ContentValues();
972 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
973 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
974 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
975 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
977 if (getContentResolver() != null
) {
978 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
982 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
984 } catch (RemoteException e
) {
985 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
990 private void cleanShares() {
991 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
992 String
[] whereArgs
= new String
[]{mAccount
.name
};
994 if (getContentResolver() != null
) {
995 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
999 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1001 } catch (RemoteException e
) {
1002 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1007 public void saveShares(Collection
<OCShare
> shares
) {
1009 if (shares
!= null
) {
1010 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1012 // prepare operations to insert or update files to save in the given folder
1013 for (OCShare share
: shares
) {
1014 ContentValues cv
= new ContentValues();
1015 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1016 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1017 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1018 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1019 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1020 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1021 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1022 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1023 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1024 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1025 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1026 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1027 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1028 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1030 if (shareExists(share
.getIdRemoteShared())) {
1031 // updating an existing file
1032 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1034 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1035 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1039 // adding a new file
1040 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1044 // apply operations in batch
1045 if (operations
.size() > 0) {
1046 @SuppressWarnings("unused")
1047 ContentProviderResult
[] results
= null
;
1048 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1050 if (getContentResolver() != null
) {
1051 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1054 results
= getContentProviderClient().applyBatch(operations
);
1057 } catch (OperationApplicationException e
) {
1058 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1060 } catch (RemoteException e
) {
1061 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1068 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1071 if (sharedFiles
!= null
) {
1072 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1074 // prepare operations to insert or update files to save in the given folder
1075 for (OCFile file
: sharedFiles
) {
1076 ContentValues cv
= new ContentValues();
1077 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1078 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1079 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1080 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1081 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1082 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1083 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1084 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1085 if (!file
.isFolder()) {
1086 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1088 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1089 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1090 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1091 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1092 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1093 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1094 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1096 boolean existsByPath
= fileExists(file
.getRemotePath());
1097 if (existsByPath
|| fileExists(file
.getFileId())) {
1098 // updating an existing file
1099 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1101 withSelection( ProviderTableMeta
._ID
+ "=?",
1102 new String
[] { String
.valueOf(file
.getFileId()) })
1106 // adding a new file
1107 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1111 // apply operations in batch
1112 if (operations
.size() > 0) {
1113 @SuppressWarnings("unused")
1114 ContentProviderResult
[] results
= null
;
1115 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1117 if (getContentResolver() != null
) {
1118 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1121 results
= getContentProviderClient().applyBatch(operations
);
1124 } catch (OperationApplicationException e
) {
1125 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1127 } catch (RemoteException e
) {
1128 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1135 public void removeShare(OCShare share
){
1136 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1137 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1138 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1139 if (getContentProviderClient() != null
) {
1141 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1142 } catch (RemoteException e
) {
1143 e
.printStackTrace();
1146 getContentResolver().delete(share_uri
, where
, whereArgs
);
1150 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1153 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1155 for (OCShare share
: shares
) {
1157 String path
= share
.getPath();
1158 if (share
.isFolder()) {
1159 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1162 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1163 OCFile file
= getFileByPath(path
);
1165 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1166 file
.setShareByLink(true
);
1167 sharedFiles
.add(file
);
1172 updateSharedFiles(sharedFiles
);
1176 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1177 cleanSharedFilesInFolder(folder
);
1178 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1179 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1181 if (shares
!= null
) {
1182 // prepare operations to insert or update files to save in the given folder
1183 for (OCShare share
: shares
) {
1184 ContentValues cv
= new ContentValues();
1185 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1186 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1187 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1188 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1189 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1190 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1191 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1192 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1193 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1194 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1195 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1196 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1197 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1198 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1201 if (shareExists(share.getIdRemoteShared())) {
1202 // updating an existing share resource
1203 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1205 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1206 new String[] { String.valueOf(share.getIdRemoteShared()) })
1211 // adding a new share resource
1212 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1217 // apply operations in batch
1218 if (operations
.size() > 0) {
1219 @SuppressWarnings("unused")
1220 ContentProviderResult
[] results
= null
;
1221 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1223 if (getContentResolver() != null
) {
1224 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1227 results
= getContentProviderClient().applyBatch(operations
);
1230 } catch (OperationApplicationException e
) {
1231 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1233 } catch (RemoteException e
) {
1234 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1241 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1242 if (folder
!= null
) {
1243 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1244 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1246 Vector
<OCFile
> files
= getFolderContent(folder
);
1248 for (OCFile file
: files
) {
1249 whereArgs
[0] = file
.getRemotePath();
1250 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1251 .withSelection(where
, whereArgs
)
1255 return preparedOperations
;
1258 if (operations.size() > 0) {
1260 if (getContentResolver() != null) {
1261 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1264 getContentProviderClient().applyBatch(operations);
1267 } catch (OperationApplicationException e) {
1268 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1270 } catch (RemoteException e) {
1271 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1277 if (getContentResolver() != null) {
1279 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1284 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1288 } catch (RemoteException e) {
1289 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());