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
.List
;
27 import java
.util
.Vector
;
29 import com
.owncloud
.android
.MainApp
;
30 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
31 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
32 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
33 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
34 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
35 import com
.owncloud
.android
.utils
.FileStorageUtils
;
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
.Intent
;
45 import android
.content
.OperationApplicationException
;
46 import android
.database
.Cursor
;
47 import android
.net
.Uri
;
48 import android
.os
.RemoteException
;
49 import android
.provider
.MediaStore
;
51 public class FileDataStorageManager
{
53 public static final int ROOT_PARENT_ID
= 0;
55 private ContentResolver mContentResolver
;
56 private ContentProviderClient mContentProviderClient
;
57 private Account mAccount
;
59 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
62 public FileDataStorageManager(Account account
, ContentResolver cr
) {
63 mContentProviderClient
= null
;
64 mContentResolver
= cr
;
68 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
69 mContentProviderClient
= cp
;
70 mContentResolver
= null
;
75 public void setAccount(Account account
) {
79 public Account
getAccount() {
83 public void setContentResolver(ContentResolver cr
) {
84 mContentResolver
= cr
;
87 public ContentResolver
getContentResolver() {
88 return mContentResolver
;
91 public void setContentProviderClient(ContentProviderClient cp
) {
92 mContentProviderClient
= cp
;
95 public ContentProviderClient
getContentProviderClient() {
96 return mContentProviderClient
;
100 public OCFile
getFileByPath(String path
) {
101 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
103 if (c
.moveToFirst()) {
104 file
= createFileInstance(c
);
107 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
108 return createRootDir(); // root should always exist
114 public OCFile
getFileById(long id
) {
115 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
117 if (c
.moveToFirst()) {
118 file
= createFileInstance(c
);
124 public OCFile
getFileByLocalPath(String path
) {
125 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
127 if (c
.moveToFirst()) {
128 file
= createFileInstance(c
);
134 public boolean fileExists(long id
) {
135 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
138 public boolean fileExists(String path
) {
139 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
143 public Vector
<OCFile
> getFolderContent(OCFile f
) {
144 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
145 return getFolderContent(f
.getFileId());
148 return new Vector
<OCFile
>();
153 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
154 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
155 if (folder
!= null
) {
156 // TODO better implementation, filtering in the access to database instead of here
157 Vector
<OCFile
> tmp
= getFolderContent(folder
);
158 OCFile current
= null
;
159 for (int i
=0; i
<tmp
.size(); i
++) {
160 current
= tmp
.get(i
);
161 if (current
.isImage()) {
170 public boolean saveFile(OCFile file
) {
171 boolean overriden
= false
;
172 ContentValues cv
= new ContentValues();
173 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
175 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
176 file
.getModificationTimestampAtLastSyncForData()
178 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
179 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
180 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
181 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
182 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
183 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
184 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
185 if (!file
.isFolder())
186 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
187 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
188 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
189 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
190 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
191 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
192 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
193 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
194 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
195 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
196 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
198 boolean sameRemotePath
= fileExists(file
.getRemotePath());
199 if (sameRemotePath
||
200 fileExists(file
.getFileId()) ) { // for renamed files
202 OCFile oldFile
= null
;
203 if (sameRemotePath
) {
204 oldFile
= getFileByPath(file
.getRemotePath());
205 file
.setFileId(oldFile
.getFileId());
207 oldFile
= getFileById(file
.getFileId());
211 if (getContentResolver() != null
) {
212 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
213 ProviderTableMeta
._ID
+ "=?",
214 new String
[] { String
.valueOf(file
.getFileId()) });
217 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
218 cv
, ProviderTableMeta
._ID
+ "=?",
219 new String
[] { String
.valueOf(file
.getFileId()) });
220 } catch (RemoteException e
) {
222 "Fail to insert insert file to database "
227 Uri result_uri
= null
;
228 if (getContentResolver() != null
) {
229 result_uri
= getContentResolver().insert(
230 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
233 result_uri
= getContentProviderClient().insert(
234 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
235 } catch (RemoteException e
) {
237 "Fail to insert insert file to database "
241 if (result_uri
!= null
) {
242 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
244 file
.setFileId(new_id
);
248 // if (file.isFolder()) {
249 // updateFolderSize(file.getFileId());
251 // updateFolderSize(file.getParentId());
259 * Inserts or updates the list of files contained in a given folder.
261 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
262 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
265 * @param updatedFiles
266 * @param filesToRemove
268 public void saveFolder(
269 OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
272 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size()
273 + " children and " + filesToRemove
.size() + " files to remove");
275 ArrayList
<ContentProviderOperation
> operations
=
276 new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
278 // prepare operations to insert or update files to save in the given folder
279 for (OCFile file
: updatedFiles
) {
280 ContentValues cv
= new ContentValues();
281 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
283 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
284 file
.getModificationTimestampAtLastSyncForData()
286 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
287 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
288 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
289 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
290 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
291 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
292 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
293 if (!file
.isFolder()) {
294 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
296 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
297 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
298 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
299 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
300 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
301 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
302 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
303 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
304 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
305 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
307 boolean existsByPath
= fileExists(file
.getRemotePath());
308 if (existsByPath
|| fileExists(file
.getFileId())) {
309 // updating an existing file
310 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
312 withSelection( ProviderTableMeta
._ID
+ "=?",
313 new String
[] { String
.valueOf(file
.getFileId()) })
318 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
319 withValues(cv
).build());
323 // prepare operations to remove files in the given folder
324 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
325 ProviderTableMeta
.FILE_PATH
+ "=?";
326 String
[] whereArgs
= null
;
327 for (OCFile file
: filesToRemove
) {
328 if (file
.getParentId() == folder
.getFileId()) {
329 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
330 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
331 if (file
.isFolder()) {
332 operations
.add(ContentProviderOperation
.newDelete(
333 ContentUris
.withAppendedId(
334 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
336 ).withSelection(where
, whereArgs
).build());
339 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
340 if (localFolder
.exists()) {
341 removeLocalFolder(localFolder
);
344 operations
.add(ContentProviderOperation
.newDelete(
345 ContentUris
.withAppendedId(
346 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
348 ).withSelection(where
, whereArgs
).build());
351 String path
= file
.getStoragePath();
352 new File(path
).delete();
353 triggerMediaScan(path
); // notify MediaScanner about removed file
359 // update metadata of folder
360 ContentValues cv
= new ContentValues();
361 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
363 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
364 folder
.getModificationTimestampAtLastSyncForData()
366 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
367 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
368 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
369 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
370 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
371 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
372 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
373 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
374 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
375 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
376 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
377 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
378 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
379 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
380 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
382 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
384 withSelection( ProviderTableMeta
._ID
+ "=?",
385 new String
[] { String
.valueOf(folder
.getFileId()) })
388 // apply operations in batch
389 ContentProviderResult
[] results
= null
;
390 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
392 if (getContentResolver() != null
) {
393 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
396 results
= getContentProviderClient().applyBatch(operations
);
399 } catch (OperationApplicationException e
) {
400 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
402 } catch (RemoteException e
) {
403 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
406 // update new id in file objects for insertions
407 if (results
!= null
) {
409 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
411 for (int i
=0; i
<results
.length
; i
++) {
412 if (filesIt
.hasNext()) {
413 file
= filesIt
.next();
417 if (results
[i
].uri
!= null
) {
418 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
419 //updatedFiles.get(i).setFileId(newId);
421 file
.setFileId(newId
);
427 //updateFolderSize(folder.getFileId());
436 // private void updateFolderSize(long id) {
437 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
438 // Log_OC.d(TAG, "Updating size of " + id);
439 // if (getContentResolver() != null) {
440 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
441 // new ContentValues(),
442 // won't be used, but cannot be null; crashes in KLP
443 // ProviderTableMeta._ID + "=?",
444 // new String[] { String.valueOf(id) });
447 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
448 // new ContentValues(),
449 // won't be used, but cannot be null; crashes in KLP
450 // ProviderTableMeta._ID + "=?",
451 // new String[] { String.valueOf(id) });
453 // } catch (RemoteException e) {
455 // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
459 // Log_OC.e(TAG, "not updating size for folder " + id);
464 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
465 boolean success
= true
;
467 if (file
.isFolder()) {
468 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
472 Uri file_uri
= ContentUris
.withAppendedId(
473 ProviderTableMeta
.CONTENT_URI_FILE
,
476 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
477 ProviderTableMeta
.FILE_PATH
+ "=?";
478 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
480 if (getContentProviderClient() != null
) {
482 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
483 } catch (RemoteException e
) {
487 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
489 success
&= (deleted
> 0);
491 String localPath
= file
.getStoragePath();
492 if (removeLocalCopy
&& file
.isDown() && localPath
!= null
&& success
) {
493 success
= new File(localPath
).delete();
495 deleteFileInMediaScan(localPath
);
497 if (!removeDBData
&& success
) {
498 // maybe unnecessary, but should be checked TODO remove if unnecessary
499 file
.setStoragePath(null
);
509 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
510 boolean success
= true
;
511 if (folder
!= null
&& folder
.isFolder()) {
512 if (removeDBData
&& folder
.getFileId() != -1) {
513 success
= removeFolderInDb(folder
);
515 if (removeLocalContent
&& success
) {
516 success
= removeLocalFolder(folder
);
522 private boolean removeFolderInDb(OCFile folder
) {
523 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
524 folder
.getFileId()); // URI for recursive deletion
525 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
526 ProviderTableMeta
.FILE_PATH
+ "=?";
527 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
529 if (getContentProviderClient() != null
) {
531 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
532 } catch (RemoteException e
) {
536 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
541 private boolean removeLocalFolder(OCFile folder
) {
542 boolean success
= true
;
543 String localFolderPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
);
544 File localFolder
= new File(localFolderPath
);
545 if (localFolder
.exists()) {
546 // stage 1: remove the local files already registered in the files database
547 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
549 for (OCFile file
: files
) {
550 if (file
.isFolder()) {
551 success
&= removeLocalFolder(file
);
554 File localFile
= new File(file
.getStoragePath());
555 success
&= localFile
.delete();
557 deleteFileInMediaScan(file
.getStoragePath()); // notify MediaScanner about removed file
558 file
.setStoragePath(null
);
564 //triggerMediaScan(localFolderPath); // notify MediaScanner about removed file in folder
567 // stage 2: remove the folder itself and any local file inside out of sync;
568 // for instance, after clearing the app cache or reinstalling
569 success
&= removeLocalFolder(localFolder
);
574 private boolean removeLocalFolder(File localFolder
) {
575 boolean success
= true
;
576 File
[] localFiles
= localFolder
.listFiles();
577 if (localFiles
!= null
) {
578 for (File localFile
: localFiles
) {
579 if (localFile
.isDirectory()) {
580 success
&= removeLocalFolder(localFile
);
582 String path
= localFile
.getAbsolutePath();
583 success
&= localFile
.delete();
584 deleteFileInMediaScan(path
); // notify MediaScanner about removed file
585 //triggerMediaScan(path); // notify MediaScanner about removed file
589 success
&= localFolder
.delete();
595 * Updates database and file system for a file or folder that was moved to a different location.
597 * TODO explore better (faster) implementations
598 * TODO throw exceptions up !
600 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
602 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
604 OCFile targetParent
= getFileByPath(targetParentPath
);
605 if (targetParent
== null
) {
606 throw new IllegalStateException("Parent folder of the target path does not exist!!");
609 /// 1. get all the descendants of the moved element in a single QUERY
611 if (getContentProviderClient() != null
) {
613 c
= getContentProviderClient().query(
614 ProviderTableMeta
.CONTENT_URI
,
616 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
617 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
620 file
.getRemotePath() + "%"
622 ProviderTableMeta
.FILE_PATH
+ " ASC "
624 } catch (RemoteException e
) {
625 Log_OC
.e(TAG
, e
.getMessage());
629 c
= getContentResolver().query(
630 ProviderTableMeta
.CONTENT_URI
,
632 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
633 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
636 file
.getRemotePath() + "%"
638 ProviderTableMeta
.FILE_PATH
+ " ASC "
642 /// 2. prepare a batch of update operations to change all the descendants
643 ArrayList
<ContentProviderOperation
> operations
=
644 new ArrayList
<ContentProviderOperation
>(c
.getCount());
645 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
646 List
<String
> originalPathsToTriggerMediaScan
= new ArrayList
<String
>();
647 List
<String
> newPathsToTriggerMediaScan
= new ArrayList
<String
>();
648 if (c
.moveToFirst()) {
649 int lengthOfOldPath
= file
.getRemotePath().length();
650 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
652 ContentValues cv
= new ContentValues(); // keep construction in the loop
653 OCFile child
= createFileInstance(c
);
655 ProviderTableMeta
.FILE_PATH
,
656 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
658 if (child
.getStoragePath() != null
&&
659 child
.getStoragePath().startsWith(defaultSavePath
)) {
660 // update link to downloaded content - but local move is not done here!
661 String targetLocalPath
= defaultSavePath
+ targetPath
+
662 child
.getStoragePath().substring(lengthOfOldStoragePath
);
664 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, targetLocalPath
);
666 originalPathsToTriggerMediaScan
.add(child
.getStoragePath());
667 newPathsToTriggerMediaScan
.add(targetLocalPath
);
670 if (child
.getRemotePath().equals(file
.getRemotePath())) {
672 ProviderTableMeta
.FILE_PARENT
,
673 targetParent
.getFileId()
677 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
680 ProviderTableMeta
._ID
+ "=?",
681 new String
[] { String
.valueOf(child
.getFileId()) }
685 } while (c
.moveToNext());
689 /// 3. apply updates in batch
691 if (getContentResolver() != null
) {
692 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
695 getContentProviderClient().applyBatch(operations
);
698 } catch (Exception e
) {
699 Log_OC
.e(TAG
, "Fail to update " + file
.getFileId() + " and descendants in database", e
);
702 /// 4. move in local file system
703 String originalLocalPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
704 String targetLocalPath
= defaultSavePath
+ targetPath
;
705 File localFile
= new File(originalLocalPath
);
706 boolean renamed
= false
;
707 if (localFile
.exists()) {
708 File targetFile
= new File(targetLocalPath
);
709 File targetFolder
= targetFile
.getParentFile();
710 if (!targetFolder
.exists()) {
711 targetFolder
.mkdirs();
713 renamed
= localFile
.renameTo(targetFile
);
717 Iterator
<String
> it
= originalPathsToTriggerMediaScan
.iterator();
718 while (it
.hasNext()) {
719 // Notify MediaScanner about removed file
720 deleteFileInMediaScan(file
.getStoragePath());
721 triggerMediaScan(it
.next());
723 it
= newPathsToTriggerMediaScan
.iterator();
724 while (it
.hasNext()) {
725 // Notify MediaScanner about new file/folder
726 deleteFileInMediaScan(file
.getStoragePath());
727 triggerMediaScan(it
.next());
735 private Vector
<OCFile
> getFolderContent(long parentId
) {
737 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
739 Uri req_uri
= Uri
.withAppendedPath(
740 ProviderTableMeta
.CONTENT_URI_DIR
,
741 String
.valueOf(parentId
));
744 if (getContentProviderClient() != null
) {
746 c
= getContentProviderClient().query(req_uri
, null
,
747 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
748 new String
[] { String
.valueOf(parentId
)}, null
);
749 } catch (RemoteException e
) {
750 Log_OC
.e(TAG
, e
.getMessage());
754 c
= getContentResolver().query(req_uri
, null
,
755 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
756 new String
[] { String
.valueOf(parentId
)}, null
);
759 if (c
.moveToFirst()) {
761 OCFile child
= createFileInstance(c
);
763 } while (c
.moveToNext());
768 Collections
.sort(ret
);
774 private OCFile
createRootDir() {
775 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
776 file
.setMimetype("DIR");
777 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
782 private boolean fileExists(String cmp_key
, String value
) {
784 if (getContentResolver() != null
) {
785 c
= getContentResolver()
786 .query(ProviderTableMeta
.CONTENT_URI
,
789 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
791 new String
[] { value
, mAccount
.name
}, null
);
794 c
= getContentProviderClient().query(
795 ProviderTableMeta
.CONTENT_URI
,
798 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
799 new String
[] { value
, mAccount
.name
}, null
);
800 } catch (RemoteException e
) {
802 "Couldn't determine file existance, assuming non existance: "
807 boolean retval
= c
.moveToFirst();
812 private Cursor
getCursorForValue(String key
, String value
) {
814 if (getContentResolver() != null
) {
815 c
= getContentResolver()
816 .query(ProviderTableMeta
.CONTENT_URI
,
819 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
821 new String
[] { value
, mAccount
.name
}, null
);
824 c
= getContentProviderClient().query(
825 ProviderTableMeta
.CONTENT_URI
,
827 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
828 + "=?", new String
[] { value
, mAccount
.name
},
830 } catch (RemoteException e
) {
831 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
839 private OCFile
createFileInstance(Cursor c
) {
842 file
= new OCFile(c
.getString(c
843 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
844 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
845 file
.setParentId(c
.getLong(c
846 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
847 file
.setMimetype(c
.getString(c
848 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
849 if (!file
.isFolder()) {
850 file
.setStoragePath(c
.getString(c
851 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
852 if (file
.getStoragePath() == null
) {
853 // try to find existing file and bind it with current account;
854 // with the current update of SynchronizeFolderOperation, this won't be
855 // necessary anymore after a full synchronization of the account
856 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
858 file
.setStoragePath(f
.getAbsolutePath());
859 file
.setLastSyncDateForData(f
.lastModified());
863 file
.setFileLength(c
.getLong(c
864 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
865 file
.setCreationTimestamp(c
.getLong(c
866 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
867 file
.setModificationTimestamp(c
.getLong(c
868 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
869 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
870 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
871 file
.setLastSyncDateForProperties(c
.getLong(c
872 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
873 file
.setLastSyncDateForData(c
.getLong(c
.
874 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
875 file
.setKeepInSync(c
.getInt(
876 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
877 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
878 file
.setShareByLink(c
.getInt(
879 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
880 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
881 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
882 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
883 file
.setNeedsUpdateThumbnail(c
.getInt(
884 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
891 * Returns if the file/folder is shared by link or not
892 * @param path Path of the file/folder
895 public boolean isShareByLink(String path
) {
896 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
898 if (c
.moveToFirst()) {
899 file
= createFileInstance(c
);
902 return file
.isShareByLink();
906 * Returns the public link of the file/folder
907 * @param path Path of the file/folder
910 public String
getPublicLink(String path
) {
911 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
913 if (c
.moveToFirst()) {
914 file
= createFileInstance(c
);
917 return file
.getPublicLink();
921 // Methods for Shares
922 public boolean saveShare(OCShare share
) {
923 boolean overriden
= false
;
924 ContentValues cv
= new ContentValues();
925 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
926 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
927 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
928 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
929 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
930 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
931 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
932 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
933 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
935 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
936 share
.getSharedWithDisplayName()
938 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
939 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
940 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
941 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
943 if (shareExists(share
.getIdRemoteShared())) { // for renamed files
946 if (getContentResolver() != null
) {
947 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
948 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
949 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
952 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
953 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
954 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
955 } catch (RemoteException e
) {
957 "Fail to insert insert file to database "
962 Uri result_uri
= null
;
963 if (getContentResolver() != null
) {
964 result_uri
= getContentResolver().insert(
965 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
968 result_uri
= getContentProviderClient().insert(
969 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
970 } catch (RemoteException e
) {
972 "Fail to insert insert file to database "
976 if (result_uri
!= null
) {
977 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
987 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
989 if (getContentResolver() != null
) {
990 c
= getContentResolver().query(
991 ProviderTableMeta
.CONTENT_URI_SHARE
,
993 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
994 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
995 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
996 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1000 c
= getContentProviderClient().query(
1001 ProviderTableMeta
.CONTENT_URI_SHARE
,
1003 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1004 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1005 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1006 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1009 } catch (RemoteException e
) {
1010 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1014 OCShare share
= null
;
1015 if (c
.moveToFirst()) {
1016 share
= createShareInstance(c
);
1022 private OCShare
createShareInstance(Cursor c
) {
1023 OCShare share
= null
;
1025 share
= new OCShare(c
.getString(c
1026 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1027 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1028 share
.setFileSource(c
.getLong(c
1029 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1030 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1031 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1032 share
.setPermissions(c
.getInt(c
1033 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1034 share
.setSharedDate(c
.getLong(c
1035 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1036 share
.setExpirationDate(c
.getLong(c
1037 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1038 share
.setToken(c
.getString(c
1039 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1040 share
.setSharedWithDisplayName(c
.getString(c
1041 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1042 share
.setIsFolder(c
.getInt(
1043 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1044 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1045 share
.setIdRemoteShared(
1046 c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
))
1053 private boolean shareExists(String cmp_key
, String value
) {
1055 if (getContentResolver() != null
) {
1056 c
= getContentResolver()
1057 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1060 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1062 new String
[] { value
, mAccount
.name
}, null
);
1065 c
= getContentProviderClient().query(
1066 ProviderTableMeta
.CONTENT_URI_SHARE
,
1069 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1070 new String
[] { value
, mAccount
.name
}, null
);
1071 } catch (RemoteException e
) {
1073 "Couldn't determine file existance, assuming non existance: "
1078 boolean retval
= c
.moveToFirst();
1083 private boolean shareExists(long remoteId
) {
1084 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1087 private void cleanSharedFiles() {
1088 ContentValues cv
= new ContentValues();
1089 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1090 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1091 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1092 String
[] whereArgs
= new String
[]{mAccount
.name
};
1094 if (getContentResolver() != null
) {
1095 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1099 getContentProviderClient().update(
1100 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1103 } catch (RemoteException e
) {
1104 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1109 private void cleanSharedFilesInFolder(OCFile folder
) {
1110 ContentValues cv
= new ContentValues();
1111 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1112 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1113 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1114 ProviderTableMeta
.FILE_PARENT
+ "=?";
1115 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1117 if (getContentResolver() != null
) {
1118 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1122 getContentProviderClient().update(
1123 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1126 } catch (RemoteException e
) {
1127 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1132 private void cleanShares() {
1133 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1134 String
[] whereArgs
= new String
[]{mAccount
.name
};
1136 if (getContentResolver() != null
) {
1137 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1141 getContentProviderClient().delete(
1142 ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
1145 } catch (RemoteException e
) {
1146 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1151 public void saveShares(Collection
<OCShare
> shares
) {
1153 if (shares
!= null
) {
1154 ArrayList
<ContentProviderOperation
> operations
=
1155 new ArrayList
<ContentProviderOperation
>(shares
.size());
1157 // prepare operations to insert or update files to save in the given folder
1158 for (OCShare share
: shares
) {
1159 ContentValues cv
= new ContentValues();
1160 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1161 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1162 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1163 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1164 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1165 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1166 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1167 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1168 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1170 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1171 share
.getSharedWithDisplayName()
1173 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1174 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1175 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1176 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1178 if (shareExists(share
.getIdRemoteShared())) {
1179 // updating an existing file
1181 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1184 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1185 new String
[] { String
.valueOf(share
.getIdRemoteShared()) }
1191 // adding a new file
1193 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1200 // apply operations in batch
1201 if (operations
.size() > 0) {
1202 @SuppressWarnings("unused")
1203 ContentProviderResult
[] results
= null
;
1204 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1205 " operations to FileContentProvider");
1207 if (getContentResolver() != null
) {
1208 results
= getContentResolver().applyBatch(
1209 MainApp
.getAuthority(), operations
1213 results
= getContentProviderClient().applyBatch(operations
);
1216 } catch (OperationApplicationException e
) {
1217 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1219 } catch (RemoteException e
) {
1220 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1227 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1230 if (sharedFiles
!= null
) {
1231 ArrayList
<ContentProviderOperation
> operations
=
1232 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1234 // prepare operations to insert or update files to save in the given folder
1235 for (OCFile file
: sharedFiles
) {
1236 ContentValues cv
= new ContentValues();
1237 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1239 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1240 file
.getModificationTimestampAtLastSyncForData()
1242 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1243 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1244 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1245 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1246 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1247 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1248 if (!file
.isFolder()) {
1249 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1251 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1252 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1254 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1255 file
.getLastSyncDateForData()
1257 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1258 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1259 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1260 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1261 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1262 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1264 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1265 file
.needsUpdateThumbnail() ?
1 : 0
1268 boolean existsByPath
= fileExists(file
.getRemotePath());
1269 if (existsByPath
|| fileExists(file
.getFileId())) {
1270 // updating an existing file
1272 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1275 ProviderTableMeta
._ID
+ "=?",
1276 new String
[] { String
.valueOf(file
.getFileId()) }
1281 // adding a new file
1283 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1290 // apply operations in batch
1291 if (operations
.size() > 0) {
1292 @SuppressWarnings("unused")
1293 ContentProviderResult
[] results
= null
;
1294 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1295 " operations to FileContentProvider");
1297 if (getContentResolver() != null
) {
1298 results
= getContentResolver().applyBatch(
1299 MainApp
.getAuthority(), operations
1303 results
= getContentProviderClient().applyBatch(operations
);
1306 } catch (OperationApplicationException e
) {
1307 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1309 } catch (RemoteException e
) {
1310 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1317 public void removeShare(OCShare share
){
1318 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1319 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1320 ProviderTableMeta
.FILE_PATH
+ "=?";
1321 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1322 if (getContentProviderClient() != null
) {
1324 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1325 } catch (RemoteException e
) {
1326 e
.printStackTrace();
1329 getContentResolver().delete(share_uri
, where
, whereArgs
);
1333 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1336 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1338 for (OCShare share
: shares
) {
1340 String path
= share
.getPath();
1341 if (share
.isFolder()) {
1342 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1345 // Update OCFile with data from share: ShareByLink and publicLink
1346 OCFile file
= getFileByPath(path
);
1348 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1349 file
.setShareByLink(true
);
1350 sharedFiles
.add(file
);
1355 updateSharedFiles(sharedFiles
);
1359 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1360 cleanSharedFilesInFolder(folder
);
1361 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1362 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1364 if (shares
!= null
) {
1365 // prepare operations to insert or update files to save in the given folder
1366 for (OCShare share
: shares
) {
1367 ContentValues cv
= new ContentValues();
1368 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1369 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1370 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1371 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1372 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1373 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1374 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1375 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1376 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1378 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1379 share
.getSharedWithDisplayName()
1381 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1382 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1383 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1384 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1387 if (shareExists(share.getIdRemoteShared())) {
1388 // updating an existing share resource
1390 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1392 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1393 new String[] { String.valueOf(share.getIdRemoteShared()) })
1398 // adding a new share resource
1400 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1408 // apply operations in batch
1409 if (operations
.size() > 0) {
1410 @SuppressWarnings("unused")
1411 ContentProviderResult
[] results
= null
;
1412 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1414 if (getContentResolver() != null
) {
1415 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1418 results
= getContentProviderClient().applyBatch(operations
);
1421 } catch (OperationApplicationException e
) {
1422 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1424 } catch (RemoteException e
) {
1425 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1432 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1433 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
1435 if (folder
!= null
) {
1436 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1437 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1438 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1440 Vector
<OCFile
> files
= getFolderContent(folder
);
1442 for (OCFile file
: files
) {
1443 whereArgs
[0] = file
.getRemotePath();
1444 preparedOperations
.add(
1445 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1446 withSelection(where
, whereArgs
).
1451 return preparedOperations
;
1454 if (operations.size() > 0) {
1456 if (getContentResolver() != null) {
1457 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1460 getContentProviderClient().applyBatch(operations);
1463 } catch (OperationApplicationException e) {
1464 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1466 } catch (RemoteException e) {
1467 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1473 if (getContentResolver() != null) {
1475 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1480 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1484 } catch (RemoteException e) {
1485 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());
1492 public void triggerMediaScan(String path
) {
1493 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
1494 intent
.setData(Uri
.fromFile(new File(path
)));
1495 MainApp
.getAppContext().sendBroadcast(intent
);
1498 public void deleteFileInMediaScan(String path
) {
1499 getContentResolver().delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1500 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});