2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.datamodel
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Collection
;
26 import java
.util
.Collections
;
27 import java
.util
.Iterator
;
28 import java
.util
.List
;
29 import java
.util
.Vector
;
31 import android
.accounts
.Account
;
32 import android
.content
.ContentProviderClient
;
33 import android
.content
.ContentProviderOperation
;
34 import android
.content
.ContentProviderResult
;
35 import android
.content
.ContentResolver
;
36 import android
.content
.ContentUris
;
37 import android
.content
.ContentValues
;
38 import android
.content
.Intent
;
39 import android
.content
.OperationApplicationException
;
40 import android
.database
.Cursor
;
41 import android
.net
.Uri
;
42 import android
.os
.RemoteException
;
43 import android
.provider
.MediaStore
;
45 import com
.owncloud
.android
.MainApp
;
46 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
47 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
48 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
49 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
50 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
51 import com
.owncloud
.android
.utils
.FileStorageUtils
;
53 public class FileDataStorageManager
{
55 public static final int ROOT_PARENT_ID
= 0;
57 private ContentResolver mContentResolver
;
58 private ContentProviderClient mContentProviderClient
;
59 private Account mAccount
;
61 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
64 public FileDataStorageManager(Account account
, ContentResolver cr
) {
65 mContentProviderClient
= null
;
66 mContentResolver
= cr
;
70 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
71 mContentProviderClient
= cp
;
72 mContentResolver
= null
;
77 public void setAccount(Account account
) {
81 public Account
getAccount() {
85 public void setContentResolver(ContentResolver cr
) {
86 mContentResolver
= cr
;
89 public ContentResolver
getContentResolver() {
90 return mContentResolver
;
93 public void setContentProviderClient(ContentProviderClient cp
) {
94 mContentProviderClient
= cp
;
97 public ContentProviderClient
getContentProviderClient() {
98 return mContentProviderClient
;
102 public OCFile
getFileByPath(String path
) {
103 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
105 if (c
.moveToFirst()) {
106 file
= createFileInstance(c
);
109 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
110 return createRootDir(); // root should always exist
116 public OCFile
getFileById(long id
) {
117 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
119 if (c
.moveToFirst()) {
120 file
= createFileInstance(c
);
126 public OCFile
getFileByLocalPath(String path
) {
127 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
129 if (c
.moveToFirst()) {
130 file
= createFileInstance(c
);
136 public boolean fileExists(long id
) {
137 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
140 public boolean fileExists(String path
) {
141 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
145 public Vector
<OCFile
> getFolderContent(OCFile f
/*, boolean onlyOnDevice*/) {
146 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
147 // TODO Enable when "On Device" is recovered ?
148 return getFolderContent(f
.getFileId()/*, onlyOnDevice*/);
151 return new Vector
<OCFile
>();
156 public Vector
<OCFile
> getFolderImages(OCFile folder
/*, boolean onlyOnDevice*/) {
157 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
158 if (folder
!= null
) {
159 // TODO better implementation, filtering in the access to database instead of here
160 // TODO Enable when "On Device" is recovered ?
161 Vector
<OCFile
> tmp
= getFolderContent(folder
/*, onlyOnDevice*/);
162 OCFile current
= null
;
163 for (int i
=0; i
<tmp
.size(); i
++) {
164 current
= tmp
.get(i
);
165 if (current
.isImage()) {
173 public boolean saveFile(OCFile file
) {
174 boolean overriden
= false
;
175 ContentValues cv
= new ContentValues();
176 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
178 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
179 file
.getModificationTimestampAtLastSyncForData()
181 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
182 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
183 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
184 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
185 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
186 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
187 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
188 if (!file
.isFolder())
189 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
190 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
191 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
192 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
193 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
194 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
195 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
196 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
197 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
198 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
199 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
200 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
202 boolean sameRemotePath
= fileExists(file
.getRemotePath());
203 if (sameRemotePath
||
204 fileExists(file
.getFileId()) ) { // for renamed files
206 OCFile oldFile
= null
;
207 if (sameRemotePath
) {
208 oldFile
= getFileByPath(file
.getRemotePath());
209 file
.setFileId(oldFile
.getFileId());
211 oldFile
= getFileById(file
.getFileId());
215 if (getContentResolver() != null
) {
216 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
217 ProviderTableMeta
._ID
+ "=?",
218 new String
[] { String
.valueOf(file
.getFileId()) });
221 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
222 cv
, ProviderTableMeta
._ID
+ "=?",
223 new String
[] { String
.valueOf(file
.getFileId()) });
224 } catch (RemoteException e
) {
226 "Fail to insert insert file to database "
231 Uri result_uri
= null
;
232 if (getContentResolver() != null
) {
233 result_uri
= getContentResolver().insert(
234 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
237 result_uri
= getContentProviderClient().insert(
238 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
239 } catch (RemoteException e
) {
241 "Fail to insert insert file to database "
245 if (result_uri
!= null
) {
246 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
248 file
.setFileId(new_id
);
252 // if (file.isFolder()) {
253 // updateFolderSize(file.getFileId());
255 // updateFolderSize(file.getParentId());
263 * Inserts or updates the list of files contained in a given folder.
265 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
266 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
269 * @param updatedFiles
270 * @param filesToRemove
272 public void saveFolder(
273 OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
276 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size()
277 + " children and " + filesToRemove
.size() + " files to remove");
279 ArrayList
<ContentProviderOperation
> operations
=
280 new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
282 // prepare operations to insert or update files to save in the given folder
283 for (OCFile file
: updatedFiles
) {
284 ContentValues cv
= new ContentValues();
285 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
287 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
288 file
.getModificationTimestampAtLastSyncForData()
290 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
291 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
292 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
293 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
294 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
295 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
296 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
297 if (!file
.isFolder()) {
298 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
300 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
301 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
302 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
303 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
304 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
305 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
306 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
307 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
308 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
309 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
310 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
312 boolean existsByPath
= fileExists(file
.getRemotePath());
313 if (existsByPath
|| fileExists(file
.getFileId())) {
314 // updating an existing file
315 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
317 withSelection( ProviderTableMeta
._ID
+ "=?",
318 new String
[] { String
.valueOf(file
.getFileId()) })
323 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
324 withValues(cv
).build());
328 // prepare operations to remove files in the given folder
329 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
330 ProviderTableMeta
.FILE_PATH
+ "=?";
331 String
[] whereArgs
= null
;
332 for (OCFile file
: filesToRemove
) {
333 if (file
.getParentId() == folder
.getFileId()) {
334 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
335 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
336 if (file
.isFolder()) {
337 operations
.add(ContentProviderOperation
.newDelete(
338 ContentUris
.withAppendedId(
339 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
341 ).withSelection(where
, whereArgs
).build());
344 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
345 if (localFolder
.exists()) {
346 removeLocalFolder(localFolder
);
349 operations
.add(ContentProviderOperation
.newDelete(
350 ContentUris
.withAppendedId(
351 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
353 ).withSelection(where
, whereArgs
).build());
356 String path
= file
.getStoragePath();
357 new File(path
).delete();
358 triggerMediaScan(path
); // notify MediaScanner about removed file
364 // update metadata of folder
365 ContentValues cv
= new ContentValues();
366 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
368 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
369 folder
.getModificationTimestampAtLastSyncForData()
371 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
372 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
373 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
374 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
375 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
376 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
377 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
378 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
379 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
380 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.isFavorite() ?
1 : 0);
381 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
382 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
383 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
384 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
385 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
387 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
389 withSelection( ProviderTableMeta
._ID
+ "=?",
390 new String
[] { String
.valueOf(folder
.getFileId()) })
393 // apply operations in batch
394 ContentProviderResult
[] results
= null
;
395 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
397 if (getContentResolver() != null
) {
398 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
401 results
= getContentProviderClient().applyBatch(operations
);
404 } catch (OperationApplicationException e
) {
405 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
407 } catch (RemoteException e
) {
408 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
411 // update new id in file objects for insertions
412 if (results
!= null
) {
414 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
416 for (int i
=0; i
<results
.length
; i
++) {
417 if (filesIt
.hasNext()) {
418 file
= filesIt
.next();
422 if (results
[i
].uri
!= null
) {
423 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
424 //updatedFiles.get(i).setFileId(newId);
426 file
.setFileId(newId
);
432 //updateFolderSize(folder.getFileId());
441 // private void updateFolderSize(long id) {
442 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
443 // Log_OC.d(TAG, "Updating size of " + id);
444 // if (getContentResolver() != null) {
445 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
446 // new ContentValues(),
447 // won't be used, but cannot be null; crashes in KLP
448 // ProviderTableMeta._ID + "=?",
449 // new String[] { String.valueOf(id) });
452 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
453 // new ContentValues(),
454 // won't be used, but cannot be null; crashes in KLP
455 // ProviderTableMeta._ID + "=?",
456 // new String[] { String.valueOf(id) });
458 // } catch (RemoteException e) {
460 // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
464 // Log_OC.e(TAG, "not updating size for folder " + id);
469 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
470 boolean success
= true
;
472 if (file
.isFolder()) {
473 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
477 Uri file_uri
= ContentUris
.withAppendedId(
478 ProviderTableMeta
.CONTENT_URI_FILE
,
481 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
482 ProviderTableMeta
.FILE_PATH
+ "=?";
483 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
485 if (getContentProviderClient() != null
) {
487 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
488 } catch (RemoteException e
) {
492 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
494 success
&= (deleted
> 0);
496 String localPath
= file
.getStoragePath();
497 if (removeLocalCopy
&& file
.isDown() && localPath
!= null
&& success
) {
498 success
= new File(localPath
).delete();
500 deleteFileInMediaScan(localPath
);
502 if (!removeDBData
&& success
) {
503 // maybe unnecessary, but should be checked TODO remove if unnecessary
504 file
.setStoragePath(null
);
514 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
515 boolean success
= true
;
516 if (folder
!= null
&& folder
.isFolder()) {
517 if (removeDBData
&& folder
.getFileId() != -1) {
518 success
= removeFolderInDb(folder
);
520 if (removeLocalContent
&& success
) {
521 success
= removeLocalFolder(folder
);
527 private boolean removeFolderInDb(OCFile folder
) {
528 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
529 folder
.getFileId()); // URI for recursive deletion
530 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
531 ProviderTableMeta
.FILE_PATH
+ "=?";
532 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
534 if (getContentProviderClient() != null
) {
536 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
537 } catch (RemoteException e
) {
541 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
546 private boolean removeLocalFolder(OCFile folder
) {
547 boolean success
= true
;
548 String localFolderPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
);
549 File localFolder
= new File(localFolderPath
);
550 if (localFolder
.exists()) {
551 // stage 1: remove the local files already registered in the files database
552 // TODO Enable when "On Device" is recovered ?
553 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId()/*, false*/);
555 for (OCFile file
: files
) {
556 if (file
.isFolder()) {
557 success
&= removeLocalFolder(file
);
560 File localFile
= new File(file
.getStoragePath());
561 success
&= localFile
.delete();
563 // notify MediaScanner about removed file
564 deleteFileInMediaScan(file
.getStoragePath());
565 file
.setStoragePath(null
);
573 // stage 2: remove the folder itself and any local file inside out of sync;
574 // for instance, after clearing the app cache or reinstalling
575 success
&= removeLocalFolder(localFolder
);
580 private boolean removeLocalFolder(File localFolder
) {
581 boolean success
= true
;
582 File
[] localFiles
= localFolder
.listFiles();
583 if (localFiles
!= null
) {
584 for (File localFile
: localFiles
) {
585 if (localFile
.isDirectory()) {
586 success
&= removeLocalFolder(localFile
);
588 String path
= localFile
.getAbsolutePath();
589 success
&= localFile
.delete();
593 success
&= localFolder
.delete();
599 * Updates database and file system for a file or folder that was moved to a different location.
601 * TODO explore better (faster) implementations
602 * TODO throw exceptions up !
604 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
606 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
608 OCFile targetParent
= getFileByPath(targetParentPath
);
609 if (targetParent
== null
) {
610 throw new IllegalStateException("Parent folder of the target path does not exist!!");
613 /// 1. get all the descendants of the moved element in a single QUERY
615 if (getContentProviderClient() != null
) {
617 c
= getContentProviderClient().query(
618 ProviderTableMeta
.CONTENT_URI
,
620 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
621 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
624 file
.getRemotePath() + "%"
626 ProviderTableMeta
.FILE_PATH
+ " ASC "
628 } catch (RemoteException e
) {
629 Log_OC
.e(TAG
, e
.getMessage());
633 c
= getContentResolver().query(
634 ProviderTableMeta
.CONTENT_URI
,
636 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
637 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
640 file
.getRemotePath() + "%"
642 ProviderTableMeta
.FILE_PATH
+ " ASC "
646 /// 2. prepare a batch of update operations to change all the descendants
647 ArrayList
<ContentProviderOperation
> operations
=
648 new ArrayList
<ContentProviderOperation
>(c
.getCount());
649 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
650 List
<String
> originalPathsToTriggerMediaScan
= new ArrayList
<String
>();
651 List
<String
> newPathsToTriggerMediaScan
= new ArrayList
<String
>();
652 if (c
.moveToFirst()) {
653 int lengthOfOldPath
= file
.getRemotePath().length();
654 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
656 ContentValues cv
= new ContentValues(); // keep construction in the loop
657 OCFile child
= createFileInstance(c
);
659 ProviderTableMeta
.FILE_PATH
,
660 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
662 if (child
.getStoragePath() != null
&&
663 child
.getStoragePath().startsWith(defaultSavePath
)) {
664 // update link to downloaded content - but local move is not done here!
665 String targetLocalPath
= defaultSavePath
+ targetPath
+
666 child
.getStoragePath().substring(lengthOfOldStoragePath
);
668 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, targetLocalPath
);
670 originalPathsToTriggerMediaScan
.add(child
.getStoragePath());
671 newPathsToTriggerMediaScan
.add(targetLocalPath
);
674 if (child
.getRemotePath().equals(file
.getRemotePath())) {
676 ProviderTableMeta
.FILE_PARENT
,
677 targetParent
.getFileId()
681 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
684 ProviderTableMeta
._ID
+ "=?",
685 new String
[] { String
.valueOf(child
.getFileId()) }
689 } while (c
.moveToNext());
693 /// 3. apply updates in batch
695 if (getContentResolver() != null
) {
696 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
699 getContentProviderClient().applyBatch(operations
);
702 } catch (Exception e
) {
703 Log_OC
.e(TAG
, "Fail to update " + file
.getFileId() + " and descendants in database", e
);
706 /// 4. move in local file system
707 String originalLocalPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
708 String targetLocalPath
= defaultSavePath
+ targetPath
;
709 File localFile
= new File(originalLocalPath
);
710 boolean renamed
= false
;
711 if (localFile
.exists()) {
712 File targetFile
= new File(targetLocalPath
);
713 File targetFolder
= targetFile
.getParentFile();
714 if (!targetFolder
.exists()) {
715 targetFolder
.mkdirs();
717 renamed
= localFile
.renameTo(targetFile
);
721 Iterator
<String
> it
= originalPathsToTriggerMediaScan
.iterator();
722 while (it
.hasNext()) {
723 // Notify MediaScanner about removed file
724 deleteFileInMediaScan(it
.next());
726 it
= newPathsToTriggerMediaScan
.iterator();
727 while (it
.hasNext()) {
728 // Notify MediaScanner about new file/folder
729 triggerMediaScan(it
.next());
737 private Vector
<OCFile
> getFolderContent(long parentId
/*, boolean onlyOnDevice*/) {
739 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
741 Uri req_uri
= Uri
.withAppendedPath(
742 ProviderTableMeta
.CONTENT_URI_DIR
,
743 String
.valueOf(parentId
));
746 if (getContentProviderClient() != null
) {
748 c
= getContentProviderClient().query(req_uri
, null
,
749 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
750 new String
[] { String
.valueOf(parentId
)}, null
);
751 } catch (RemoteException e
) {
752 Log_OC
.e(TAG
, e
.getMessage());
756 c
= getContentResolver().query(req_uri
, null
,
757 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
758 new String
[] { String
.valueOf(parentId
)}, null
);
761 if (c
.moveToFirst()) {
763 OCFile child
= createFileInstance(c
);
764 // TODO Enable when "On Device" is recovered ?
765 // if (child.isFolder() || !onlyOnDevice || onlyOnDevice && child.isDown()){
768 } while (c
.moveToNext());
773 Collections
.sort(ret
);
779 private OCFile
createRootDir() {
780 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
781 file
.setMimetype("DIR");
782 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
787 private boolean fileExists(String cmp_key
, String value
) {
789 if (getContentResolver() != null
) {
790 c
= getContentResolver()
791 .query(ProviderTableMeta
.CONTENT_URI
,
794 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
796 new String
[] { value
, mAccount
.name
}, null
);
799 c
= getContentProviderClient().query(
800 ProviderTableMeta
.CONTENT_URI
,
803 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
804 new String
[] { value
, mAccount
.name
}, null
);
805 } catch (RemoteException e
) {
807 "Couldn't determine file existance, assuming non existance: "
812 boolean retval
= c
.moveToFirst();
817 private Cursor
getCursorForValue(String key
, String value
) {
819 if (getContentResolver() != null
) {
820 c
= getContentResolver()
821 .query(ProviderTableMeta
.CONTENT_URI
,
824 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
826 new String
[] { value
, mAccount
.name
}, null
);
829 c
= getContentProviderClient().query(
830 ProviderTableMeta
.CONTENT_URI
,
832 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
833 + "=?", new String
[] { value
, mAccount
.name
},
835 } catch (RemoteException e
) {
836 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
844 private OCFile
createFileInstance(Cursor c
) {
847 file
= new OCFile(c
.getString(c
848 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
849 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
850 file
.setParentId(c
.getLong(c
851 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
852 file
.setMimetype(c
.getString(c
853 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
854 if (!file
.isFolder()) {
855 file
.setStoragePath(c
.getString(c
856 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
857 if (file
.getStoragePath() == null
) {
858 // try to find existing file and bind it with current account;
859 // with the current update of SynchronizeFolderOperation, this won't be
860 // necessary anymore after a full synchronization of the account
861 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
863 file
.setStoragePath(f
.getAbsolutePath());
864 file
.setLastSyncDateForData(f
.lastModified());
868 file
.setFileLength(c
.getLong(c
869 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
870 file
.setCreationTimestamp(c
.getLong(c
871 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
872 file
.setModificationTimestamp(c
.getLong(c
873 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
874 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
875 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
876 file
.setLastSyncDateForProperties(c
.getLong(c
877 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
878 file
.setLastSyncDateForData(c
.getLong(c
.
879 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
880 file
.setFavorite(c
.getInt(
881 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
882 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
883 file
.setShareByLink(c
.getInt(
884 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
885 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
886 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
887 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
888 file
.setNeedsUpdateThumbnail(c
.getInt(
889 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
890 file
.setDownloading(c
.getInt(
891 c
.getColumnIndex(ProviderTableMeta
.FILE_IS_DOWNLOADING
)) == 1 ? true
: false
);
898 * Returns if the file/folder is shared by link or not
899 * @param path Path of the file/folder
902 public boolean isShareByLink(String path
) {
903 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
905 if (c
.moveToFirst()) {
906 file
= createFileInstance(c
);
909 return file
.isShareByLink();
913 * Returns the public link of the file/folder
914 * @param path Path of the file/folder
917 public String
getPublicLink(String path
) {
918 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
920 if (c
.moveToFirst()) {
921 file
= createFileInstance(c
);
924 return file
.getPublicLink();
928 // Methods for Shares
929 public boolean saveShare(OCShare share
) {
930 boolean overriden
= false
;
931 ContentValues cv
= new ContentValues();
932 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
933 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
934 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
935 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
936 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
937 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
938 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
939 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
940 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
942 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
943 share
.getSharedWithDisplayName()
945 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
946 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
947 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
948 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
950 if (shareExists(share
.getIdRemoteShared())) { // for renamed files
953 if (getContentResolver() != null
) {
954 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
955 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
956 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
959 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
960 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
961 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
962 } catch (RemoteException e
) {
964 "Fail to insert insert file to database "
969 Uri result_uri
= null
;
970 if (getContentResolver() != null
) {
971 result_uri
= getContentResolver().insert(
972 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
975 result_uri
= getContentProviderClient().insert(
976 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
977 } catch (RemoteException e
) {
979 "Fail to insert insert file to database "
983 if (result_uri
!= null
) {
984 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
994 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
996 if (getContentResolver() != null
) {
997 c
= getContentResolver().query(
998 ProviderTableMeta
.CONTENT_URI_SHARE
,
1000 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1001 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1002 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1003 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1007 c
= getContentProviderClient().query(
1008 ProviderTableMeta
.CONTENT_URI_SHARE
,
1010 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1011 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1012 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1013 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1016 } catch (RemoteException e
) {
1017 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1021 OCShare share
= null
;
1022 if (c
.moveToFirst()) {
1023 share
= createShareInstance(c
);
1029 private OCShare
createShareInstance(Cursor c
) {
1030 OCShare share
= null
;
1032 share
= new OCShare(c
.getString(c
1033 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1034 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1035 share
.setFileSource(c
.getLong(c
1036 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1037 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1038 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1039 share
.setPermissions(c
.getInt(c
1040 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1041 share
.setSharedDate(c
.getLong(c
1042 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1043 share
.setExpirationDate(c
.getLong(c
1044 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1045 share
.setToken(c
.getString(c
1046 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1047 share
.setSharedWithDisplayName(c
.getString(c
1048 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1049 share
.setIsFolder(c
.getInt(
1050 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1051 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1052 share
.setIdRemoteShared(
1053 c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
))
1060 private boolean shareExists(String cmp_key
, String value
) {
1062 if (getContentResolver() != null
) {
1063 c
= getContentResolver()
1064 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1067 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1069 new String
[] { value
, mAccount
.name
}, null
);
1072 c
= getContentProviderClient().query(
1073 ProviderTableMeta
.CONTENT_URI_SHARE
,
1076 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1077 new String
[] { value
, mAccount
.name
}, null
);
1078 } catch (RemoteException e
) {
1080 "Couldn't determine file existance, assuming non existance: "
1085 boolean retval
= c
.moveToFirst();
1090 private boolean shareExists(long remoteId
) {
1091 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1094 private void cleanSharedFiles() {
1095 ContentValues cv
= new ContentValues();
1096 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1097 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1098 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1099 String
[] whereArgs
= new String
[]{mAccount
.name
};
1101 if (getContentResolver() != null
) {
1102 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1106 getContentProviderClient().update(
1107 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1110 } catch (RemoteException e
) {
1111 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1116 private void cleanSharedFilesInFolder(OCFile folder
) {
1117 ContentValues cv
= new ContentValues();
1118 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1119 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1120 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1121 ProviderTableMeta
.FILE_PARENT
+ "=?";
1122 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1124 if (getContentResolver() != null
) {
1125 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1129 getContentProviderClient().update(
1130 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1133 } catch (RemoteException e
) {
1134 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1139 private void cleanShares() {
1140 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1141 String
[] whereArgs
= new String
[]{mAccount
.name
};
1143 if (getContentResolver() != null
) {
1144 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1148 getContentProviderClient().delete(
1149 ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
1152 } catch (RemoteException e
) {
1153 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1158 public void saveShares(Collection
<OCShare
> shares
) {
1160 if (shares
!= null
) {
1161 ArrayList
<ContentProviderOperation
> operations
=
1162 new ArrayList
<ContentProviderOperation
>(shares
.size());
1164 // prepare operations to insert or update files to save in the given folder
1165 for (OCShare share
: shares
) {
1166 ContentValues cv
= new ContentValues();
1167 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1168 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1169 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1170 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1171 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1172 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1173 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1174 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1175 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1177 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1178 share
.getSharedWithDisplayName()
1180 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1181 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1182 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1183 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1185 if (shareExists(share
.getIdRemoteShared())) {
1186 // updating an existing file
1188 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1191 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1192 new String
[] { String
.valueOf(share
.getIdRemoteShared()) }
1198 // adding a new file
1200 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1207 // apply operations in batch
1208 if (operations
.size() > 0) {
1209 @SuppressWarnings("unused")
1210 ContentProviderResult
[] results
= null
;
1211 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1212 " operations to FileContentProvider");
1214 if (getContentResolver() != null
) {
1215 results
= getContentResolver().applyBatch(
1216 MainApp
.getAuthority(), operations
1220 results
= getContentProviderClient().applyBatch(operations
);
1223 } catch (OperationApplicationException e
) {
1224 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1226 } catch (RemoteException e
) {
1227 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1234 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1237 if (sharedFiles
!= null
) {
1238 ArrayList
<ContentProviderOperation
> operations
=
1239 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1241 // prepare operations to insert or update files to save in the given folder
1242 for (OCFile file
: sharedFiles
) {
1243 ContentValues cv
= new ContentValues();
1244 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1246 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1247 file
.getModificationTimestampAtLastSyncForData()
1249 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1250 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1251 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1252 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1253 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1254 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1255 if (!file
.isFolder()) {
1256 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1258 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1259 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1261 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1262 file
.getLastSyncDateForData()
1264 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
1265 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1266 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1267 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1268 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1269 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1271 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1272 file
.needsUpdateThumbnail() ?
1 : 0
1275 ProviderTableMeta
.FILE_IS_DOWNLOADING
,
1276 file
.isDownloading() ?
1 : 0
1279 boolean existsByPath
= fileExists(file
.getRemotePath());
1280 if (existsByPath
|| fileExists(file
.getFileId())) {
1281 // updating an existing file
1283 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1286 ProviderTableMeta
._ID
+ "=?",
1287 new String
[] { String
.valueOf(file
.getFileId()) }
1292 // adding a new file
1294 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1301 // apply operations in batch
1302 if (operations
.size() > 0) {
1303 @SuppressWarnings("unused")
1304 ContentProviderResult
[] results
= null
;
1305 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1306 " operations to FileContentProvider");
1308 if (getContentResolver() != null
) {
1309 results
= getContentResolver().applyBatch(
1310 MainApp
.getAuthority(), operations
1314 results
= getContentProviderClient().applyBatch(operations
);
1317 } catch (OperationApplicationException e
) {
1318 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1320 } catch (RemoteException e
) {
1321 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1328 public void removeShare(OCShare share
){
1329 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1330 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1331 ProviderTableMeta
.FILE_PATH
+ "=?";
1332 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1333 if (getContentProviderClient() != null
) {
1335 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1336 } catch (RemoteException e
) {
1337 e
.printStackTrace();
1340 getContentResolver().delete(share_uri
, where
, whereArgs
);
1344 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1347 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1349 for (OCShare share
: shares
) {
1351 String path
= share
.getPath();
1352 if (share
.isFolder()) {
1353 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1356 // Update OCFile with data from share: ShareByLink and publicLink
1357 OCFile file
= getFileByPath(path
);
1359 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1360 file
.setShareByLink(true
);
1361 sharedFiles
.add(file
);
1366 updateSharedFiles(sharedFiles
);
1370 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1371 cleanSharedFilesInFolder(folder
);
1372 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1373 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1375 if (shares
!= null
) {
1376 // prepare operations to insert or update files to save in the given folder
1377 for (OCShare share
: shares
) {
1378 ContentValues cv
= new ContentValues();
1379 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1380 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1381 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1382 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1383 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1384 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1385 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1386 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1387 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1389 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1390 share
.getSharedWithDisplayName()
1392 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1393 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1394 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1395 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1398 if (shareExists(share.getIdRemoteShared())) {
1399 // updating an existing share resource
1401 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1403 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1404 new String[] { String.valueOf(share.getIdRemoteShared()) })
1409 // adding a new share resource
1411 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1419 // apply operations in batch
1420 if (operations
.size() > 0) {
1421 @SuppressWarnings("unused")
1422 ContentProviderResult
[] results
= null
;
1423 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1425 if (getContentResolver() != null
) {
1426 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1429 results
= getContentProviderClient().applyBatch(operations
);
1432 } catch (OperationApplicationException e
) {
1433 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1435 } catch (RemoteException e
) {
1436 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1443 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1444 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1445 if (folder
!= null
) {
1446 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1447 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1448 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1450 // TODO Enable when "On Device" is recovered ?
1451 Vector
<OCFile
> files
= getFolderContent(folder
/*, false*/);
1453 for (OCFile file
: files
) {
1454 whereArgs
[0] = file
.getRemotePath();
1455 preparedOperations
.add(
1456 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1457 withSelection(where
, whereArgs
).
1462 return preparedOperations
;
1465 if (operations.size() > 0) {
1467 if (getContentResolver() != null) {
1468 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1471 getContentProviderClient().applyBatch(operations);
1474 } catch (OperationApplicationException e) {
1475 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1477 } catch (RemoteException e) {
1478 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1484 if (getContentResolver() != null) {
1486 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1491 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1495 } catch (RemoteException e) {
1496 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());
1503 public void triggerMediaScan(String path
) {
1504 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
1505 intent
.setData(Uri
.fromFile(new File(path
)));
1506 MainApp
.getAppContext().sendBroadcast(intent
);
1509 public void deleteFileInMediaScan(String path
) {
1511 String mimetypeString
= FileStorageUtils
.getMimeTypeFromName(path
);
1512 ContentResolver contentResolver
= getContentResolver();
1514 if (contentResolver
!= null
) {
1515 if (mimetypeString
.startsWith("image/")) {
1517 contentResolver
.delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1518 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1519 } else if (mimetypeString
.startsWith("audio/")) {
1521 contentResolver
.delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1522 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1523 } else if (mimetypeString
.startsWith("video/")) {
1525 contentResolver
.delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1526 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});
1529 ContentProviderClient contentProviderClient
= getContentProviderClient();
1531 if (mimetypeString
.startsWith("image/")) {
1533 contentProviderClient
.delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1534 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1535 } else if (mimetypeString
.startsWith("audio/")) {
1537 contentProviderClient
.delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1538 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1539 } else if (mimetypeString
.startsWith("video/")) {
1541 contentProviderClient
.delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1542 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});
1544 } catch (RemoteException e
) {
1545 Log_OC
.e(TAG
, "Exception deleting media file in MediaStore " + e
.getMessage());