1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.datamodel
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collection
;
24 import java
.util
.Collections
;
25 import java
.util
.Iterator
;
26 import java
.util
.Vector
;
28 import com
.owncloud
.android
.MainApp
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
31 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
32 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
33 import com
.owncloud
.android
.utils
.FileStorageUtils
;
34 import com
.owncloud
.android
.utils
.Log_OC
;
37 import android
.accounts
.Account
;
38 import android
.content
.ContentProviderClient
;
39 import android
.content
.ContentProviderOperation
;
40 import android
.content
.ContentProviderResult
;
41 import android
.content
.ContentResolver
;
42 import android
.content
.ContentUris
;
43 import android
.content
.ContentValues
;
44 import android
.content
.OperationApplicationException
;
45 import android
.database
.Cursor
;
46 import android
.net
.Uri
;
47 import android
.os
.RemoteException
;
49 public class FileDataStorageManager
{
51 public static final int ROOT_PARENT_ID
= 0;
53 private ContentResolver mContentResolver
;
54 private ContentProviderClient mContentProviderClient
;
55 private Account mAccount
;
57 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
60 public FileDataStorageManager(Account account
, ContentResolver cr
) {
61 mContentProviderClient
= null
;
62 mContentResolver
= cr
;
66 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
67 mContentProviderClient
= cp
;
68 mContentResolver
= null
;
73 public void setAccount(Account account
) {
77 public Account
getAccount() {
81 public void setContentResolver(ContentResolver cr
) {
82 mContentResolver
= cr
;
85 public ContentResolver
getContentResolver() {
86 return mContentResolver
;
89 public void setContentProviderClient(ContentProviderClient cp
) {
90 mContentProviderClient
= cp
;
93 public ContentProviderClient
getContentProviderClient() {
94 return mContentProviderClient
;
98 public OCFile
getFileByPath(String path
) {
99 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
101 if (c
.moveToFirst()) {
102 file
= createFileInstance(c
);
105 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
106 return createRootDir(); // root should always exist
112 public OCFile
getFileById(long id
) {
113 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
115 if (c
.moveToFirst()) {
116 file
= createFileInstance(c
);
122 public OCFile
getFileByLocalPath(String path
) {
123 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
125 if (c
.moveToFirst()) {
126 file
= createFileInstance(c
);
132 public boolean fileExists(long id
) {
133 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
136 public boolean fileExists(String path
) {
137 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
141 public Vector
<OCFile
> getFolderContent(OCFile f
) {
142 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
143 return getFolderContent(f
.getFileId());
146 return new Vector
<OCFile
>();
151 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
152 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
153 if (folder
!= null
) {
154 // TODO better implementation, filtering in the access to database (if possible) instead of here
155 Vector
<OCFile
> tmp
= getFolderContent(folder
);
156 OCFile current
= null
;
157 for (int i
=0; i
<tmp
.size(); i
++) {
158 current
= tmp
.get(i
);
159 if (current
.isImage()) {
168 public boolean saveFile(OCFile file
) {
169 boolean overriden
= false
;
170 ContentValues cv
= new ContentValues();
171 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
172 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
173 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
174 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
175 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
176 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
177 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
178 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
179 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
180 if (!file
.isFolder())
181 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
182 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
183 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
184 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
185 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
186 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
187 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
188 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
190 boolean sameRemotePath
= fileExists(file
.getRemotePath());
191 if (sameRemotePath
||
192 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
194 OCFile oldFile
= null
;
195 if (sameRemotePath
) {
196 oldFile
= getFileByPath(file
.getRemotePath());
197 file
.setFileId(oldFile
.getFileId());
199 oldFile
= getFileById(file
.getFileId());
203 if (getContentResolver() != null
) {
204 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
205 ProviderTableMeta
._ID
+ "=?",
206 new String
[] { String
.valueOf(file
.getFileId()) });
209 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
210 cv
, ProviderTableMeta
._ID
+ "=?",
211 new String
[] { String
.valueOf(file
.getFileId()) });
212 } catch (RemoteException e
) {
214 "Fail to insert insert file to database "
219 Uri result_uri
= null
;
220 if (getContentResolver() != null
) {
221 result_uri
= getContentResolver().insert(
222 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
225 result_uri
= getContentProviderClient().insert(
226 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
227 } catch (RemoteException e
) {
229 "Fail to insert insert file to database "
233 if (result_uri
!= null
) {
234 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
236 file
.setFileId(new_id
);
240 // if (file.isFolder()) {
241 // updateFolderSize(file.getFileId());
243 // updateFolderSize(file.getParentId());
251 * Inserts or updates the list of files contained in a given folder.
253 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
254 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
258 * @param removeNotUpdated
260 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
262 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
264 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
266 // prepare operations to insert or update files to save in the given folder
267 for (OCFile file
: updatedFiles
) {
268 ContentValues cv
= new ContentValues();
269 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
270 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
271 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
272 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
273 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
274 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
275 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
276 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
277 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
278 if (!file
.isFolder()) {
279 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
281 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
282 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
283 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
284 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
285 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
286 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
287 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
289 boolean existsByPath
= fileExists(file
.getRemotePath());
290 if (existsByPath
|| fileExists(file
.getFileId())) {
291 // updating an existing file
292 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
294 withSelection( ProviderTableMeta
._ID
+ "=?",
295 new String
[] { String
.valueOf(file
.getFileId()) })
300 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
304 // prepare operations to remove files in the given folder
305 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
306 String
[] whereArgs
= null
;
307 for (OCFile file
: filesToRemove
) {
308 if (file
.getParentId() == folder
.getFileId()) {
309 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
310 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
311 if (file
.isFolder()) {
312 operations
.add(ContentProviderOperation
313 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
315 // TODO remove local folder
317 operations
.add(ContentProviderOperation
318 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
321 new File(file
.getStoragePath()).delete();
322 // TODO move the deletion of local contents after success of deletions
328 // update metadata of folder
329 ContentValues cv
= new ContentValues();
330 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
331 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
332 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
333 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
334 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
335 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
336 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
337 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
338 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
339 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
340 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
341 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
342 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
343 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
344 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
346 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
348 withSelection( ProviderTableMeta
._ID
+ "=?",
349 new String
[] { String
.valueOf(folder
.getFileId()) })
352 // apply operations in batch
353 ContentProviderResult
[] results
= null
;
354 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
356 if (getContentResolver() != null
) {
357 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
360 results
= getContentProviderClient().applyBatch(operations
);
363 } catch (OperationApplicationException e
) {
364 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
366 } catch (RemoteException e
) {
367 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
370 // update new id in file objects for insertions
371 if (results
!= null
) {
373 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
375 for (int i
=0; i
<results
.length
; i
++) {
376 if (filesIt
.hasNext()) {
377 file
= filesIt
.next();
381 if (results
[i
].uri
!= null
) {
382 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
383 //updatedFiles.get(i).setFileId(newId);
385 file
.setFileId(newId
);
391 //updateFolderSize(folder.getFileId());
400 // private void updateFolderSize(long id) {
401 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
402 // Log_OC.d(TAG, "Updating size of " + id);
403 // if (getContentResolver() != null) {
404 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
405 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
406 // ProviderTableMeta._ID + "=?",
407 // new String[] { String.valueOf(id) });
410 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
411 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
412 // ProviderTableMeta._ID + "=?",
413 // new String[] { String.valueOf(id) });
415 // } catch (RemoteException e) {
416 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
420 // Log_OC.e(TAG, "not updating size for folder " + id);
425 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
426 boolean success
= true
;
428 if (file
.isFolder()) {
429 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
433 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
434 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
435 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
436 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
438 if (getContentProviderClient() != null
) {
440 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
441 } catch (RemoteException e
) {
445 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
447 success
&= (deleted
> 0);
449 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
450 success
= new File(file
.getStoragePath()).delete();
451 if (!removeDBData
&& success
) {
452 // maybe unnecessary, but should be checked TODO remove if unnecessary
453 file
.setStoragePath(null
);
463 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
464 boolean success
= true
;
465 if (folder
!= null
&& folder
.isFolder()) {
466 if (removeDBData
&& folder
.getFileId() != -1) {
467 success
= removeFolderInDb(folder
);
469 if (removeLocalContent
&& success
) {
470 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 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
499 for (OCFile file
: files
) {
500 if (file
.isFolder()) {
501 success
&= removeLocalFolder(file
);
504 File localFile
= new File(file
.getStoragePath());
505 success
&= localFile
.delete();
507 file
.setStoragePath(null
);
514 success
&= localFolder
.delete();
520 * Updates database for a folder that was moved to a different location.
522 * TODO explore better (faster) implementations
523 * TODO throw exceptions up !
525 public void moveFolder(OCFile folder
, String newPath
) {
526 // TODO check newPath
528 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
529 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
531 if (getContentProviderClient() != null
) {
533 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
535 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
536 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
537 } catch (RemoteException e
) {
538 Log_OC
.e(TAG
, e
.getMessage());
541 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
543 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
544 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
547 /// 2. prepare a batch of update operations to change all the descendants
548 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
549 int lengthOfOldPath
= folder
.getRemotePath().length();
550 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
551 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
552 if (c
.moveToFirst()) {
554 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
555 OCFile child
= createFileInstance(c
);
556 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
557 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
558 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
560 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
562 withSelection( ProviderTableMeta
._ID
+ "=?",
563 new String
[] { String
.valueOf(child
.getFileId()) })
565 } while (c
.moveToNext());
569 /// 3. apply updates in batch
571 if (getContentResolver() != null
) {
572 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
575 getContentProviderClient().applyBatch(operations
);
578 } catch (OperationApplicationException e
) {
579 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
581 } catch (RemoteException e
) {
582 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
589 private Vector
<OCFile
> getFolderContent(long parentId
) {
591 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
593 Uri req_uri
= Uri
.withAppendedPath(
594 ProviderTableMeta
.CONTENT_URI_DIR
,
595 String
.valueOf(parentId
));
598 if (getContentProviderClient() != null
) {
600 c
= getContentProviderClient().query(req_uri
, null
,
601 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
602 new String
[] { String
.valueOf(parentId
)}, null
);
603 } catch (RemoteException e
) {
604 Log_OC
.e(TAG
, e
.getMessage());
608 c
= getContentResolver().query(req_uri
, null
,
609 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
610 new String
[] { String
.valueOf(parentId
)}, null
);
613 if (c
.moveToFirst()) {
615 OCFile child
= createFileInstance(c
);
617 } while (c
.moveToNext());
622 Collections
.sort(ret
);
628 private OCFile
createRootDir() {
629 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
630 file
.setMimetype("DIR");
631 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
636 private boolean fileExists(String cmp_key
, String value
) {
638 if (getContentResolver() != null
) {
639 c
= getContentResolver()
640 .query(ProviderTableMeta
.CONTENT_URI
,
643 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
645 new String
[] { value
, mAccount
.name
}, null
);
648 c
= getContentProviderClient().query(
649 ProviderTableMeta
.CONTENT_URI
,
652 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
653 new String
[] { value
, mAccount
.name
}, null
);
654 } catch (RemoteException e
) {
656 "Couldn't determine file existance, assuming non existance: "
661 boolean retval
= c
.moveToFirst();
666 private Cursor
getCursorForValue(String key
, String value
) {
668 if (getContentResolver() != null
) {
669 c
= getContentResolver()
670 .query(ProviderTableMeta
.CONTENT_URI
,
673 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
675 new String
[] { value
, mAccount
.name
}, null
);
678 c
= getContentProviderClient().query(
679 ProviderTableMeta
.CONTENT_URI
,
681 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
682 + "=?", new String
[] { value
, mAccount
.name
},
684 } catch (RemoteException e
) {
685 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
692 private Cursor
getShareCursorForValue(String key
, String value
) {
694 if (getContentResolver() != null
) {
695 c
= getContentResolver()
696 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
699 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
701 new String
[] { value
, mAccount
.name
}, null
);
704 c
= getContentProviderClient().query(
705 ProviderTableMeta
.CONTENT_URI_SHARE
,
707 key
+ "=? AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
708 + "=?", new String
[] { value
, mAccount
.name
},
710 } catch (RemoteException e
) {
711 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
718 private OCFile
createFileInstance(Cursor c
) {
721 file
= new OCFile(c
.getString(c
722 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
723 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
724 file
.setParentId(c
.getLong(c
725 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
726 file
.setMimetype(c
.getString(c
727 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
728 if (!file
.isFolder()) {
729 file
.setStoragePath(c
.getString(c
730 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
731 if (file
.getStoragePath() == null
) {
732 // 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
733 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
735 file
.setStoragePath(f
.getAbsolutePath());
736 file
.setLastSyncDateForData(f
.lastModified());
740 file
.setFileLength(c
.getLong(c
741 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
742 file
.setCreationTimestamp(c
.getLong(c
743 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
744 file
.setModificationTimestamp(c
.getLong(c
745 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
746 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
747 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
748 file
.setLastSyncDateForProperties(c
.getLong(c
749 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
750 file
.setLastSyncDateForData(c
.getLong(c
.
751 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
752 file
.setKeepInSync(c
.getInt(
753 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
754 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
755 file
.setShareByLink(c
.getInt(
756 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
757 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
764 * Returns if the file/folder is shared by link or not
765 * @param path Path of the file/folder
768 public boolean isShareByLink(String path
) {
769 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
771 if (c
.moveToFirst()) {
772 file
= createFileInstance(c
);
775 return file
.isShareByLink();
779 * Returns the public link of the file/folder
780 * @param path Path of the file/folder
783 public String
getPublicLink(String path
) {
784 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
786 if (c
.moveToFirst()) {
787 file
= createFileInstance(c
);
790 return file
.getPublicLink();
794 // Methods for Shares
795 public boolean saveShare(OCShare share
) {
796 boolean overriden
= false
;
797 ContentValues cv
= new ContentValues();
798 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
799 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
800 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
801 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
802 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
803 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
804 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
805 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
806 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
807 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
808 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
809 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
810 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
811 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
813 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
816 if (getContentResolver() != null
) {
817 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
818 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
819 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
822 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
823 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
824 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
825 } catch (RemoteException e
) {
827 "Fail to insert insert file to database "
832 Uri result_uri
= null
;
833 if (getContentResolver() != null
) {
834 result_uri
= getContentResolver().insert(
835 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
838 result_uri
= getContentProviderClient().insert(
839 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
840 } catch (RemoteException e
) {
842 "Fail to insert insert file to database "
846 if (result_uri
!= null
) {
847 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
856 // private OCShare getShareById(long id) {
857 // Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
858 // OCShare share = null;
859 // if (c.moveToFirst()) {
860 // share = createShareInstance(c);
866 // private OCShare getShareByRemoteId(long remoteId) {
867 // Cursor c = getShareCursorForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
868 // OCShare share = null;
869 // if (c.moveToFirst()) {
870 // share = createShareInstance(c);
876 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
878 if (getContentResolver() != null
) {
879 c
= getContentResolver().query(
880 ProviderTableMeta
.CONTENT_URI_SHARE
,
882 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
883 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
884 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
885 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
889 c
= getContentProviderClient().query(
890 ProviderTableMeta
.CONTENT_URI_SHARE
,
892 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
893 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
894 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
895 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
898 } catch (RemoteException e
) {
899 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
903 OCShare share
= null
;
904 if (c
.moveToFirst()) {
905 share
= createShareInstance(c
);
911 private OCShare
createShareInstance(Cursor c
) {
912 OCShare share
= null
;
914 share
= new OCShare(c
.getString(c
915 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
916 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
917 share
.setFileSource(c
.getLong(c
918 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
919 share
.setShareType(ShareType
.fromValue(c
.getInt(c
920 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
921 share
.setPermissions(c
.getInt(c
922 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
923 share
.setSharedDate(c
.getLong(c
924 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
925 share
.setExpirationDate(c
.getLong(c
926 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
927 share
.setToken(c
.getString(c
928 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
929 share
.setSharedWithDisplayName(c
.getString(c
930 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
931 share
.setIsFolder(c
.getInt(
932 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
933 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
934 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
940 private boolean shareExists(String cmp_key
, String value
) {
942 if (getContentResolver() != null
) {
943 c
= getContentResolver()
944 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
947 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
949 new String
[] { value
, mAccount
.name
}, null
);
952 c
= getContentProviderClient().query(
953 ProviderTableMeta
.CONTENT_URI_SHARE
,
956 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
957 new String
[] { value
, mAccount
.name
}, null
);
958 } catch (RemoteException e
) {
960 "Couldn't determine file existance, assuming non existance: "
965 boolean retval
= c
.moveToFirst();
970 private boolean shareExists(long remoteId
) {
971 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
974 private void cleanSharedFiles() {
975 ContentValues cv
= new ContentValues();
976 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
977 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
978 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
979 String
[] whereArgs
= new String
[]{mAccount
.name
};
981 if (getContentResolver() != null
) {
982 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
986 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
988 } catch (RemoteException e
) {
989 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
994 private void cleanSharedFilesInFolder(OCFile folder
) {
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
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
999 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
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 cleanSharedFilesInFolder " + e
.getMessage());
1014 private void cleanShares() {
1015 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1016 String
[] whereArgs
= new String
[]{mAccount
.name
};
1018 if (getContentResolver() != null
) {
1019 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1023 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1025 } catch (RemoteException e
) {
1026 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1031 public void saveShares(Collection
<OCShare
> shares
) {
1033 if (shares
!= null
) {
1034 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1036 // prepare operations to insert or update files to save in the given folder
1037 for (OCShare share
: shares
) {
1038 ContentValues cv
= new ContentValues();
1039 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1040 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1041 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1042 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1043 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1044 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1045 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1046 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1047 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1048 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1049 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1050 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1051 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1052 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1054 if (shareExists(share
.getIdRemoteShared())) {
1055 // updating an existing file
1056 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1058 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1059 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1063 // adding a new file
1064 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1068 // apply operations in batch
1069 if (operations
.size() > 0) {
1070 @SuppressWarnings("unused")
1071 ContentProviderResult
[] results
= null
;
1072 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1074 if (getContentResolver() != null
) {
1075 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1078 results
= getContentProviderClient().applyBatch(operations
);
1081 } catch (OperationApplicationException e
) {
1082 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1084 } catch (RemoteException e
) {
1085 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1092 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1095 if (sharedFiles
!= null
) {
1096 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1098 // prepare operations to insert or update files to save in the given folder
1099 for (OCFile file
: sharedFiles
) {
1100 ContentValues cv
= new ContentValues();
1101 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1102 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1103 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1104 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1105 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1106 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1107 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1108 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1109 if (!file
.isFolder()) {
1110 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1112 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1113 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1114 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1115 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1116 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1117 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1118 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1120 boolean existsByPath
= fileExists(file
.getRemotePath());
1121 if (existsByPath
|| fileExists(file
.getFileId())) {
1122 // updating an existing file
1123 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1125 withSelection( ProviderTableMeta
._ID
+ "=?",
1126 new String
[] { String
.valueOf(file
.getFileId()) })
1130 // adding a new file
1131 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1135 // apply operations in batch
1136 if (operations
.size() > 0) {
1137 @SuppressWarnings("unused")
1138 ContentProviderResult
[] results
= null
;
1139 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1141 if (getContentResolver() != null
) {
1142 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1145 results
= getContentProviderClient().applyBatch(operations
);
1148 } catch (OperationApplicationException e
) {
1149 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1151 } catch (RemoteException e
) {
1152 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1159 public void removeShare(OCShare share
){
1160 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1161 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1162 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1163 if (getContentProviderClient() != null
) {
1165 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1166 } catch (RemoteException e
) {
1167 e
.printStackTrace();
1170 getContentResolver().delete(share_uri
, where
, whereArgs
);
1174 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1177 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1179 for (OCShare share
: shares
) {
1181 String path
= share
.getPath();
1182 if (share
.isFolder()) {
1183 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1186 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1187 OCFile file
= getFileByPath(path
);
1189 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1190 file
.setShareByLink(true
);
1191 sharedFiles
.add(file
);
1196 updateSharedFiles(sharedFiles
);
1200 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1201 cleanSharedFilesInFolder(folder
);
1202 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1203 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1205 if (shares
!= null
) {
1206 // prepare operations to insert or update files to save in the given folder
1207 for (OCShare share
: shares
) {
1208 ContentValues cv
= new ContentValues();
1209 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1210 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1211 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1212 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1213 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1214 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1215 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1216 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1217 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1218 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1219 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1220 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1221 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1222 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1225 if (shareExists(share.getIdRemoteShared())) {
1226 // updating an existing share resource
1227 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1229 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1230 new String[] { String.valueOf(share.getIdRemoteShared()) })
1235 // adding a new share resource
1236 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1241 // apply operations in batch
1242 if (operations
.size() > 0) {
1243 @SuppressWarnings("unused")
1244 ContentProviderResult
[] results
= null
;
1245 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1247 if (getContentResolver() != null
) {
1248 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1251 results
= getContentProviderClient().applyBatch(operations
);
1254 } catch (OperationApplicationException e
) {
1255 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1257 } catch (RemoteException e
) {
1258 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1265 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1266 if (folder
!= null
) {
1267 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1268 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1270 Vector
<OCFile
> files
= getFolderContent(folder
);
1272 for (OCFile file
: files
) {
1273 whereArgs
[0] = file
.getRemotePath();
1274 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1275 .withSelection(where
, whereArgs
)
1279 return preparedOperations
;
1282 if (operations.size() > 0) {
1284 if (getContentResolver() != null) {
1285 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1288 getContentProviderClient().applyBatch(operations);
1291 } catch (OperationApplicationException e) {
1292 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1294 } catch (RemoteException e) {
1295 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1301 if (getContentResolver() != null) {
1303 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1308 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1312 } catch (RemoteException e) {
1313 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());