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
.HashSet
;
28 import java
.util
.Iterator
;
29 import java
.util
.List
;
31 import java
.util
.Vector
;
33 import android
.accounts
.Account
;
34 import android
.content
.ContentProviderClient
;
35 import android
.content
.ContentProviderOperation
;
36 import android
.content
.ContentProviderResult
;
37 import android
.content
.ContentResolver
;
38 import android
.content
.ContentUris
;
39 import android
.content
.ContentValues
;
40 import android
.content
.Intent
;
41 import android
.content
.OperationApplicationException
;
42 import android
.database
.Cursor
;
43 import android
.net
.Uri
;
44 import android
.os
.RemoteException
;
45 import android
.provider
.MediaStore
;
47 import com
.owncloud
.android
.MainApp
;
48 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
49 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
50 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
51 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
52 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
53 import com
.owncloud
.android
.utils
.FileStorageUtils
;
55 import java
.io
.FileInputStream
;
56 import java
.io
.FileOutputStream
;
57 import java
.io
.IOException
;
58 import java
.io
.InputStream
;
59 import java
.io
.OutputStream
;
61 public class FileDataStorageManager
{
63 public static final int ROOT_PARENT_ID
= 0;
65 private ContentResolver mContentResolver
;
66 private ContentProviderClient mContentProviderClient
;
67 private Account mAccount
;
69 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
72 public FileDataStorageManager(Account account
, ContentResolver cr
) {
73 mContentProviderClient
= null
;
74 mContentResolver
= cr
;
78 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
79 mContentProviderClient
= cp
;
80 mContentResolver
= null
;
85 public void setAccount(Account account
) {
89 public Account
getAccount() {
93 public void setContentResolver(ContentResolver cr
) {
94 mContentResolver
= cr
;
97 public ContentResolver
getContentResolver() {
98 return mContentResolver
;
101 public void setContentProviderClient(ContentProviderClient cp
) {
102 mContentProviderClient
= cp
;
105 public ContentProviderClient
getContentProviderClient() {
106 return mContentProviderClient
;
110 public OCFile
getFileByPath(String path
) {
111 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
113 if (c
.moveToFirst()) {
114 file
= createFileInstance(c
);
117 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
118 return createRootDir(); // root should always exist
124 public OCFile
getFileById(long id
) {
125 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
127 if (c
.moveToFirst()) {
128 file
= createFileInstance(c
);
134 public OCFile
getFileByLocalPath(String path
) {
135 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
137 if (c
.moveToFirst()) {
138 file
= createFileInstance(c
);
144 public boolean fileExists(long id
) {
145 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
148 public boolean fileExists(String path
) {
149 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
153 public Vector
<OCFile
> getFolderContent(OCFile f
/*, boolean onlyOnDevice*/) {
154 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
155 // TODO Enable when "On Device" is recovered ?
156 return getFolderContent(f
.getFileId()/*, onlyOnDevice*/);
159 return new Vector
<OCFile
>();
164 public Vector
<OCFile
> getFolderImages(OCFile folder
/*, boolean onlyOnDevice*/) {
165 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
166 if (folder
!= null
) {
167 // TODO better implementation, filtering in the access to database instead of here
168 // TODO Enable when "On Device" is recovered ?
169 Vector
<OCFile
> tmp
= getFolderContent(folder
/*, onlyOnDevice*/);
170 OCFile current
= null
;
171 for (int i
=0; i
<tmp
.size(); i
++) {
172 current
= tmp
.get(i
);
173 if (current
.isImage()) {
181 public boolean saveFile(OCFile file
) {
182 boolean overriden
= false
;
183 ContentValues cv
= new ContentValues();
184 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
186 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
187 file
.getModificationTimestampAtLastSyncForData()
189 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
190 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
191 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
192 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
193 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
194 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
195 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
196 if (!file
.isFolder())
197 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
198 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
199 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
200 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
201 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
202 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
203 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
204 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
205 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
206 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
207 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
208 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
209 cv
.put(ProviderTableMeta
.FILE_IN_CONFLICT
, file
.isInConflict());
211 boolean sameRemotePath
= fileExists(file
.getRemotePath());
212 if (sameRemotePath
|| fileExists(file
.getFileId())) { // for renamed files; no more delete and create
214 OCFile oldFile
= null
;
215 if (sameRemotePath
) {
216 oldFile
= getFileByPath(file
.getRemotePath());
217 file
.setFileId(oldFile
.getFileId());
219 oldFile
= getFileById(file
.getFileId());
223 if (getContentResolver() != null
) {
224 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
225 ProviderTableMeta
._ID
+ "=?",
226 new String
[]{String
.valueOf(file
.getFileId())});
229 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
230 cv
, ProviderTableMeta
._ID
+ "=?",
231 new String
[]{String
.valueOf(file
.getFileId())});
232 } catch (RemoteException e
) {
234 "Fail to insert insert file to database "
239 Uri result_uri
= null
;
240 if (getContentResolver() != null
) {
241 result_uri
= getContentResolver().insert(
242 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
245 result_uri
= getContentProviderClient().insert(
246 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
247 } catch (RemoteException e
) {
249 "Fail to insert insert file to database "
253 if (result_uri
!= null
) {
254 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
256 file
.setFileId(new_id
);
260 // if (file.isFolder()) {
261 // updateFolderSize(file.getFileId());
263 // updateFolderSize(file.getParentId());
271 * Inserts or updates the list of files contained in a given folder.
273 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
274 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
277 * @param updatedFiles
278 * @param filesToRemove
280 public void saveFolder(
281 OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
284 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size()
285 + " children and " + filesToRemove
.size() + " files to remove");
287 ArrayList
<ContentProviderOperation
> operations
=
288 new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
290 // prepare operations to insert or update files to save in the given folder
291 for (OCFile file
: updatedFiles
) {
292 ContentValues cv
= new ContentValues();
293 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
295 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
296 file
.getModificationTimestampAtLastSyncForData()
298 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
299 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
300 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
301 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
302 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
303 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
304 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
305 if (!file
.isFolder()) {
306 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
308 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
309 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
310 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
311 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
312 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
313 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
314 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
315 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
316 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
317 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
318 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
319 cv
.put(ProviderTableMeta
.FILE_IN_CONFLICT
, file
.isInConflict());
321 boolean existsByPath
= fileExists(file
.getRemotePath());
322 if (existsByPath
|| fileExists(file
.getFileId())) {
323 // updating an existing file
324 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
326 withSelection(ProviderTableMeta
._ID
+ "=?",
327 new String
[]{String
.valueOf(file
.getFileId())})
332 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
333 withValues(cv
).build());
337 // prepare operations to remove files in the given folder
338 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
339 ProviderTableMeta
.FILE_PATH
+ "=?";
340 String
[] whereArgs
= null
;
341 for (OCFile file
: filesToRemove
) {
342 if (file
.getParentId() == folder
.getFileId()) {
343 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
344 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
345 if (file
.isFolder()) {
346 operations
.add(ContentProviderOperation
.newDelete(
347 ContentUris
.withAppendedId(
348 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
350 ).withSelection(where
, whereArgs
).build());
353 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
354 if (localFolder
.exists()) {
355 removeLocalFolder(localFolder
);
358 operations
.add(ContentProviderOperation
.newDelete(
359 ContentUris
.withAppendedId(
360 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
362 ).withSelection(where
, whereArgs
).build());
365 String path
= file
.getStoragePath();
366 new File(path
).delete();
367 triggerMediaScan(path
); // notify MediaScanner about removed file
373 // update metadata of folder
374 ContentValues cv
= new ContentValues();
375 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
377 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
378 folder
.getModificationTimestampAtLastSyncForData()
380 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
381 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
382 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
383 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
384 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
385 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
386 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
387 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
388 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
389 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.isFavorite() ?
1 : 0);
390 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
391 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
392 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
393 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
394 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
396 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
398 withSelection(ProviderTableMeta
._ID
+ "=?",
399 new String
[]{String
.valueOf(folder
.getFileId())})
402 // apply operations in batch
403 ContentProviderResult
[] results
= null
;
404 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
406 if (getContentResolver() != null
) {
407 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
410 results
= getContentProviderClient().applyBatch(operations
);
413 } catch (OperationApplicationException e
) {
414 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
416 } catch (RemoteException e
) {
417 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
420 // update new id in file objects for insertions
421 if (results
!= null
) {
423 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
425 for (int i
= 0; i
< results
.length
; i
++) {
426 if (filesIt
.hasNext()) {
427 file
= filesIt
.next();
431 if (results
[i
].uri
!= null
) {
432 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
433 //updatedFiles.get(i).setFileId(newId);
435 file
.setFileId(newId
);
441 //updateFolderSize(folder.getFileId());
450 // private void updateFolderSize(long id) {
451 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
452 // Log_OC.d(TAG, "Updating size of " + id);
453 // if (getContentResolver() != null) {
454 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
455 // new ContentValues(),
456 // won't be used, but cannot be null; crashes in KLP
457 // ProviderTableMeta._ID + "=?",
458 // new String[] { String.valueOf(id) });
461 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
462 // new ContentValues(),
463 // won't be used, but cannot be null; crashes in KLP
464 // ProviderTableMeta._ID + "=?",
465 // new String[] { String.valueOf(id) });
467 // } catch (RemoteException e) {
469 // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
473 // Log_OC.e(TAG, "not updating size for folder " + id);
478 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
479 boolean success
= true
;
481 if (file
.isFolder()) {
482 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
486 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
487 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
488 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
489 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
491 if (getContentProviderClient() != null
) {
493 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
494 } catch (RemoteException e
) {
498 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
500 success
&= (deleted
> 0);
502 String localPath
= file
.getStoragePath();
503 if (removeLocalCopy
&& file
.isDown() && localPath
!= null
&& success
) {
504 success
= new File(localPath
).delete();
506 deleteFileInMediaScan(localPath
);
508 if (!removeDBData
&& success
) {
509 // maybe unnecessary, but should be checked TODO remove if unnecessary
510 file
.setStoragePath(null
);
512 saveConflict(file
, false
);
521 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
522 boolean success
= true
;
523 if (folder
!= null
&& folder
.isFolder()) {
524 if (removeDBData
&& folder
.getFileId() != -1) {
525 success
= removeFolderInDb(folder
);
527 if (removeLocalContent
&& success
) {
528 success
= removeLocalFolder(folder
);
534 private boolean removeFolderInDb(OCFile folder
) {
535 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
536 folder
.getFileId()); // URI for recursive deletion
537 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
538 ProviderTableMeta
.FILE_PATH
+ "=?";
539 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
541 if (getContentProviderClient() != null
) {
543 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
544 } catch (RemoteException e
) {
548 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
553 private boolean removeLocalFolder(OCFile folder
) {
554 boolean success
= true
;
555 String localFolderPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
);
556 File localFolder
= new File(localFolderPath
);
557 if (localFolder
.exists()) {
558 // stage 1: remove the local files already registered in the files database
559 // TODO Enable when "On Device" is recovered ?
560 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId()/*, false*/);
562 for (OCFile file
: files
) {
563 if (file
.isFolder()) {
564 success
&= removeLocalFolder(file
);
567 File localFile
= new File(file
.getStoragePath());
568 success
&= localFile
.delete();
570 // notify MediaScanner about removed file
571 deleteFileInMediaScan(file
.getStoragePath());
572 file
.setStoragePath(null
);
580 // stage 2: remove the folder itself and any local file inside out of sync;
581 // for instance, after clearing the app cache or reinstalling
582 success
&= removeLocalFolder(localFolder
);
587 private boolean removeLocalFolder(File localFolder
) {
588 boolean success
= true
;
589 File
[] localFiles
= localFolder
.listFiles();
590 if (localFiles
!= null
) {
591 for (File localFile
: localFiles
) {
592 if (localFile
.isDirectory()) {
593 success
&= removeLocalFolder(localFile
);
595 String path
= localFile
.getAbsolutePath();
596 success
&= localFile
.delete();
600 success
&= localFolder
.delete();
606 * Updates database and file system for a file or folder that was moved to a different location.
608 * TODO explore better (faster) implementations
609 * TODO throw exceptions up !
611 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
613 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
615 OCFile targetParent
= getFileByPath(targetParentPath
);
616 if (targetParent
== null
) {
617 throw new IllegalStateException("Parent folder of the target path does not exist!!");
620 /// 1. get all the descendants of the moved element in a single QUERY
622 if (getContentProviderClient() != null
) {
624 c
= getContentProviderClient().query(
625 ProviderTableMeta
.CONTENT_URI
,
627 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
628 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
631 file
.getRemotePath() + "%"
633 ProviderTableMeta
.FILE_PATH
+ " ASC "
635 } catch (RemoteException e
) {
636 Log_OC
.e(TAG
, e
.getMessage());
640 c
= getContentResolver().query(
641 ProviderTableMeta
.CONTENT_URI
,
643 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
644 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
647 file
.getRemotePath() + "%"
649 ProviderTableMeta
.FILE_PATH
+ " ASC "
653 /// 2. prepare a batch of update operations to change all the descendants
654 ArrayList
<ContentProviderOperation
> operations
=
655 new ArrayList
<ContentProviderOperation
>(c
.getCount());
656 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
657 List
<String
> originalPathsToTriggerMediaScan
= new ArrayList
<String
>();
658 List
<String
> newPathsToTriggerMediaScan
= new ArrayList
<String
>();
659 if (c
.moveToFirst()) {
660 int lengthOfOldPath
= file
.getRemotePath().length();
661 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
663 ContentValues cv
= new ContentValues(); // keep construction in the loop
664 OCFile child
= createFileInstance(c
);
666 ProviderTableMeta
.FILE_PATH
,
667 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
669 if (child
.getStoragePath() != null
&&
670 child
.getStoragePath().startsWith(defaultSavePath
)) {
671 // update link to downloaded content - but local move is not done here!
672 String targetLocalPath
= defaultSavePath
+ targetPath
+
673 child
.getStoragePath().substring(lengthOfOldStoragePath
);
675 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, targetLocalPath
);
677 originalPathsToTriggerMediaScan
.add(child
.getStoragePath());
678 newPathsToTriggerMediaScan
.add(targetLocalPath
);
681 if (child
.getRemotePath().equals(file
.getRemotePath())) {
683 ProviderTableMeta
.FILE_PARENT
,
684 targetParent
.getFileId()
688 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
691 ProviderTableMeta
._ID
+ "=?",
692 new String
[]{String
.valueOf(child
.getFileId())}
696 } while (c
.moveToNext());
700 /// 3. apply updates in batch
702 if (getContentResolver() != null
) {
703 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
706 getContentProviderClient().applyBatch(operations
);
709 } catch (Exception e
) {
710 Log_OC
.e(TAG
, "Fail to update " + file
.getFileId() + " and descendants in database", e
);
713 /// 4. move in local file system
714 String originalLocalPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
715 String targetLocalPath
= defaultSavePath
+ targetPath
;
716 File localFile
= new File(originalLocalPath
);
717 boolean renamed
= false
;
718 if (localFile
.exists()) {
719 File targetFile
= new File(targetLocalPath
);
720 File targetFolder
= targetFile
.getParentFile();
721 if (!targetFolder
.exists()) {
722 targetFolder
.mkdirs();
724 renamed
= localFile
.renameTo(targetFile
);
728 Iterator
<String
> it
= originalPathsToTriggerMediaScan
.iterator();
729 while (it
.hasNext()) {
730 // Notify MediaScanner about removed file
731 deleteFileInMediaScan(it
.next());
733 it
= newPathsToTriggerMediaScan
.iterator();
734 while (it
.hasNext()) {
735 // Notify MediaScanner about new file/folder
736 triggerMediaScan(it
.next());
743 public void copyLocalFile(OCFile file
, String targetPath
) {
745 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
746 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
747 File localFile
= new File(localPath
);
748 boolean copied
= false
;
749 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
750 if (localFile
.exists()) {
751 File targetFile
= new File(defaultSavePath
+ targetPath
);
752 File targetFolder
= targetFile
.getParentFile();
753 if (!targetFolder
.exists()) {
754 targetFolder
.mkdirs();
756 copied
= copyFile(localFile
, targetFile
);
758 Log_OC
.d(TAG
, "Local file COPIED : " + copied
);
762 private boolean copyFile(File src
, File target
) {
765 InputStream
in = null
;
766 OutputStream out
= null
;
769 in = new FileInputStream(src
);
770 out
= new FileOutputStream(target
);
771 byte[] buf
= new byte[1024];
773 while ((len
= in.read(buf
)) > 0) {
774 out
.write(buf
, 0, len
);
776 } catch (IOException ex
) {
779 if (in != null
) try {
781 } catch (IOException e
) {
782 e
.printStackTrace(System
.err
);
784 if (out
!= null
) try {
786 } catch (IOException e
) {
787 e
.printStackTrace(System
.err
);
795 private Vector
<OCFile
> getFolderContent(long parentId
/*, boolean onlyOnDevice*/) {
797 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
799 Uri req_uri
= Uri
.withAppendedPath(
800 ProviderTableMeta
.CONTENT_URI_DIR
,
801 String
.valueOf(parentId
));
804 if (getContentProviderClient() != null
) {
806 c
= getContentProviderClient().query(req_uri
, null
,
807 ProviderTableMeta
.FILE_PARENT
+ "=?",
808 new String
[]{String
.valueOf(parentId
)}, null
);
809 } catch (RemoteException e
) {
810 Log_OC
.e(TAG
, e
.getMessage());
814 c
= getContentResolver().query(req_uri
, null
,
815 ProviderTableMeta
.FILE_PARENT
+ "=?",
816 new String
[]{String
.valueOf(parentId
)}, null
);
819 if (c
.moveToFirst()) {
821 OCFile child
= createFileInstance(c
);
822 // TODO Enable when "On Device" is recovered ?
823 // if (child.isFolder() || !onlyOnDevice || onlyOnDevice && child.isDown()){
826 } while (c
.moveToNext());
831 Collections
.sort(ret
);
837 private OCFile
createRootDir() {
838 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
839 file
.setMimetype("DIR");
840 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
845 private boolean fileExists(String cmp_key
, String value
) {
847 if (getContentResolver() != null
) {
848 c
= getContentResolver()
849 .query(ProviderTableMeta
.CONTENT_URI
,
852 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
854 new String
[]{value
, mAccount
.name
}, null
);
857 c
= getContentProviderClient().query(
858 ProviderTableMeta
.CONTENT_URI
,
861 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
862 new String
[]{value
, mAccount
.name
}, null
);
863 } catch (RemoteException e
) {
865 "Couldn't determine file existance, assuming non existance: "
870 boolean retval
= c
.moveToFirst();
875 private Cursor
getCursorForValue(String key
, String value
) {
877 if (getContentResolver() != null
) {
878 c
= getContentResolver()
879 .query(ProviderTableMeta
.CONTENT_URI
,
882 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
884 new String
[]{value
, mAccount
.name
}, null
);
887 c
= getContentProviderClient().query(
888 ProviderTableMeta
.CONTENT_URI
,
890 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
891 + "=?", new String
[]{value
, mAccount
.name
},
893 } catch (RemoteException e
) {
894 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
902 private OCFile
createFileInstance(Cursor c
) {
905 file
= new OCFile(c
.getString(c
906 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
907 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
908 file
.setParentId(c
.getLong(c
909 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
910 file
.setMimetype(c
.getString(c
911 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
912 if (!file
.isFolder()) {
913 file
.setStoragePath(c
.getString(c
914 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
915 if (file
.getStoragePath() == null
) {
916 // try to find existing file and bind it with current account;
917 // with the current update of SynchronizeFolderOperation, this won't be
918 // necessary anymore after a full synchronization of the account
919 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
921 file
.setStoragePath(f
.getAbsolutePath());
922 file
.setLastSyncDateForData(f
.lastModified());
926 file
.setFileLength(c
.getLong(c
927 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
928 file
.setCreationTimestamp(c
.getLong(c
929 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
930 file
.setModificationTimestamp(c
.getLong(c
931 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
932 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
933 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
934 file
.setLastSyncDateForProperties(c
.getLong(c
935 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
936 file
.setLastSyncDateForData(c
.getLong(c
.
937 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
938 file
.setFavorite(c
.getInt(
939 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
940 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
941 file
.setShareByLink(c
.getInt(
942 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
943 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
944 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
945 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
946 file
.setNeedsUpdateThumbnail(c
.getInt(
947 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
948 file
.setDownloading(c
.getInt(
949 c
.getColumnIndex(ProviderTableMeta
.FILE_IS_DOWNLOADING
)) == 1 ? true
: false
);
950 file
.setInConflict(c
.getInt(
951 c
.getColumnIndex(ProviderTableMeta
.FILE_IN_CONFLICT
)) == 1 ? true
: false
);
958 * Returns if the file/folder is shared by link or not
960 * @param path Path of the file/folder
963 public boolean isShareByLink(String path
) {
964 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
966 if (c
.moveToFirst()) {
967 file
= createFileInstance(c
);
970 return file
.isShareByLink();
974 * Returns the public link of the file/folder
976 * @param path Path of the file/folder
979 public String
getPublicLink(String path
) {
980 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
982 if (c
.moveToFirst()) {
983 file
= createFileInstance(c
);
986 return file
.getPublicLink();
990 // Methods for Shares
991 public boolean saveShare(OCShare share
) {
992 boolean overriden
= false
;
993 ContentValues cv
= new ContentValues();
994 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
995 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
996 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
997 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
998 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
999 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1000 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1001 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1002 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1004 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1005 share
.getSharedWithDisplayName()
1007 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1008 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1009 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1010 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1012 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
1014 if (getContentResolver() != null
) {
1015 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
1016 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1017 new String
[]{String
.valueOf(share
.getIdRemoteShared())});
1020 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
1021 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1022 new String
[]{String
.valueOf(share
.getIdRemoteShared())});
1023 } catch (RemoteException e
) {
1025 "Fail to insert insert file to database "
1030 Uri result_uri
= null
;
1031 if (getContentResolver() != null
) {
1032 result_uri
= getContentResolver().insert(
1033 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1036 result_uri
= getContentProviderClient().insert(
1037 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1038 } catch (RemoteException e
) {
1040 "Fail to insert insert file to database "
1044 if (result_uri
!= null
) {
1045 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
1047 share
.setId(new_id
);
1055 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
1057 if (getContentResolver() != null
) {
1058 c
= getContentResolver().query(
1059 ProviderTableMeta
.CONTENT_URI_SHARE
,
1061 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1062 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1063 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1064 new String
[]{path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1068 c
= getContentProviderClient().query(
1069 ProviderTableMeta
.CONTENT_URI_SHARE
,
1071 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1072 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1073 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1074 new String
[]{path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1077 } catch (RemoteException e
) {
1078 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1082 OCShare share
= null
;
1083 if (c
.moveToFirst()) {
1084 share
= createShareInstance(c
);
1090 private OCShare
createShareInstance(Cursor c
) {
1091 OCShare share
= null
;
1093 share
= new OCShare(c
.getString(c
1094 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1095 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1096 share
.setFileSource(c
.getLong(c
1097 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1098 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1099 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1100 share
.setPermissions(c
.getInt(c
1101 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1102 share
.setSharedDate(c
.getLong(c
1103 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1104 share
.setExpirationDate(c
.getLong(c
1105 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1106 share
.setToken(c
.getString(c
1107 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1108 share
.setSharedWithDisplayName(c
.getString(c
1109 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1110 share
.setIsFolder(c
.getInt(
1111 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1);
1112 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1113 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
1118 private boolean shareExists(String cmp_key
, String value
) {
1120 if (getContentResolver() != null
) {
1121 c
= getContentResolver()
1122 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1125 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1127 new String
[]{value
, mAccount
.name
}, null
);
1130 c
= getContentProviderClient().query(
1131 ProviderTableMeta
.CONTENT_URI_SHARE
,
1134 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1135 new String
[]{value
, mAccount
.name
}, null
);
1136 } catch (RemoteException e
) {
1138 "Couldn't determine file existance, assuming non existance: "
1143 boolean retval
= c
.moveToFirst();
1148 private boolean shareExists(long remoteId
) {
1149 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1152 private void cleanSharedFiles() {
1153 ContentValues cv
= new ContentValues();
1154 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1155 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1156 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1157 String
[] whereArgs
= new String
[]{mAccount
.name
};
1159 if (getContentResolver() != null
) {
1160 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1164 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1165 } catch (RemoteException e
) {
1166 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1171 private void cleanSharedFilesInFolder(OCFile folder
) {
1172 ContentValues cv
= new ContentValues();
1173 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1174 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1175 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1176 ProviderTableMeta
.FILE_PARENT
+ "=?";
1177 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1179 if (getContentResolver() != null
) {
1180 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1184 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1185 } catch (RemoteException e
) {
1186 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1191 private void cleanShares() {
1192 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1193 String
[] whereArgs
= new String
[]{mAccount
.name
};
1195 if (getContentResolver() != null
) {
1196 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1200 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1201 } catch (RemoteException e
) {
1202 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1207 public void saveShares(Collection
<OCShare
> shares
) {
1209 if (shares
!= null
) {
1210 ArrayList
<ContentProviderOperation
> operations
=
1211 new ArrayList
<ContentProviderOperation
>(shares
.size());
1213 // prepare operations to insert or update files to save in the given folder
1214 for (OCShare share
: shares
) {
1215 ContentValues cv
= new ContentValues();
1216 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1217 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1218 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1219 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1220 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1221 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1222 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1223 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1224 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1226 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1227 share
.getSharedWithDisplayName()
1229 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1230 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1231 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1232 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1234 if (shareExists(share
.getIdRemoteShared())) {
1235 // updating an existing file
1237 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1239 withSelection(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1240 new String
[]{String
.valueOf(share
.getIdRemoteShared())})
1243 // adding a new file
1245 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1252 // apply operations in batch
1253 if (operations
.size() > 0) {
1254 @SuppressWarnings("unused")
1255 ContentProviderResult
[] results
= null
;
1256 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1257 " operations to FileContentProvider");
1259 if (getContentResolver() != null
) {
1260 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1262 results
= getContentProviderClient().applyBatch(operations
);
1265 } catch (OperationApplicationException e
) {
1266 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1268 } catch (RemoteException e
) {
1269 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1276 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1279 if (sharedFiles
!= null
) {
1280 ArrayList
<ContentProviderOperation
> operations
=
1281 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1283 // prepare operations to insert or update files to save in the given folder
1284 for (OCFile file
: sharedFiles
) {
1285 ContentValues cv
= new ContentValues();
1286 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1288 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1289 file
.getModificationTimestampAtLastSyncForData()
1291 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1292 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1293 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1294 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1295 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1296 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1297 if (!file
.isFolder()) {
1298 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1300 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1301 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1303 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1304 file
.getLastSyncDateForData()
1306 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
1307 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1308 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1309 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1310 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1311 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1313 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1314 file
.needsUpdateThumbnail() ?
1 : 0
1317 ProviderTableMeta
.FILE_IS_DOWNLOADING
,
1318 file
.isDownloading() ?
1 : 0
1321 ProviderTableMeta
.FILE_IN_CONFLICT
,
1322 file
.isInConflict() ?
1 : 0
1325 boolean existsByPath
= fileExists(file
.getRemotePath());
1326 if (existsByPath
|| fileExists(file
.getFileId())) {
1327 // updating an existing file
1329 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1331 withSelection(ProviderTableMeta
._ID
+ "=?",
1332 new String
[]{String
.valueOf(file
.getFileId())})
1336 // adding a new file
1338 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1345 // apply operations in batch
1346 if (operations
.size() > 0) {
1347 @SuppressWarnings("unused")
1348 ContentProviderResult
[] results
= null
;
1349 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1350 " operations to FileContentProvider");
1352 if (getContentResolver() != null
) {
1353 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1355 results
= getContentProviderClient().applyBatch(operations
);
1358 } catch (OperationApplicationException e
) {
1359 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1361 } catch (RemoteException e
) {
1362 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1369 public void removeShare(OCShare share
) {
1370 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1371 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1372 ProviderTableMeta
.FILE_PATH
+ "=?";
1373 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1374 if (getContentProviderClient() != null
) {
1376 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1377 } catch (RemoteException e
) {
1378 e
.printStackTrace();
1381 getContentResolver().delete(share_uri
, where
, whereArgs
);
1385 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1388 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1390 for (OCShare share
: shares
) {
1392 String path
= share
.getPath();
1393 if (share
.isFolder()) {
1394 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1397 // Update OCFile with data from share: ShareByLink and publicLink
1398 OCFile file
= getFileByPath(path
);
1400 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1401 file
.setShareByLink(true
);
1402 sharedFiles
.add(file
);
1407 updateSharedFiles(sharedFiles
);
1411 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1412 cleanSharedFilesInFolder(folder
);
1413 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1414 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1416 if (shares
!= null
) {
1417 // prepare operations to insert or update files to save in the given folder
1418 for (OCShare share
: shares
) {
1419 ContentValues cv
= new ContentValues();
1420 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1421 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1422 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1423 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1424 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1425 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1426 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1427 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1428 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1430 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1431 share
.getSharedWithDisplayName()
1433 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1434 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1435 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1436 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1439 if (shareExists(share.getIdRemoteShared())) {
1440 // updating an existing share resource
1442 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1444 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1445 new String[] { String.valueOf(share.getIdRemoteShared()) })
1450 // adding a new share resource
1452 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1460 // apply operations in batch
1461 if (operations
.size() > 0) {
1462 @SuppressWarnings("unused")
1463 ContentProviderResult
[] results
= null
;
1464 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1466 if (getContentResolver() != null
) {
1467 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1470 results
= getContentProviderClient().applyBatch(operations
);
1473 } catch (OperationApplicationException e
) {
1474 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1476 } catch (RemoteException e
) {
1477 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1484 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1485 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1486 if (folder
!= null
) {
1487 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1488 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1489 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1491 // TODO Enable when "On Device" is recovered ?
1492 Vector
<OCFile
> files
= getFolderContent(folder
/*, false*/);
1494 for (OCFile file
: files
) {
1495 whereArgs
[0] = file
.getRemotePath();
1496 preparedOperations
.add(
1497 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1498 withSelection(where
, whereArgs
).
1503 return preparedOperations
;
1506 if (operations.size() > 0) {
1508 if (getContentResolver() != null) {
1509 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1512 getContentProviderClient().applyBatch(operations);
1515 } catch (OperationApplicationException e) {
1516 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1518 } catch (RemoteException e) {
1519 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1525 if (getContentResolver() != null) {
1527 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1532 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1536 } catch (RemoteException e) {
1537 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());
1544 public void triggerMediaScan(String path
) {
1545 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
1546 intent
.setData(Uri
.fromFile(new File(path
)));
1547 MainApp
.getAppContext().sendBroadcast(intent
);
1550 public void deleteFileInMediaScan(String path
) {
1552 String mimetypeString
= FileStorageUtils
.getMimeTypeFromName(path
);
1553 ContentResolver contentResolver
= getContentResolver();
1555 if (contentResolver
!= null
) {
1556 if (mimetypeString
.startsWith("image/")) {
1558 contentResolver
.delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1559 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1560 } else if (mimetypeString
.startsWith("audio/")) {
1562 contentResolver
.delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1563 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1564 } else if (mimetypeString
.startsWith("video/")) {
1566 contentResolver
.delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1567 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});
1570 ContentProviderClient contentProviderClient
= getContentProviderClient();
1572 if (mimetypeString
.startsWith("image/")) {
1574 contentProviderClient
.delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1575 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1576 } else if (mimetypeString
.startsWith("audio/")) {
1578 contentProviderClient
.delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1579 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1580 } else if (mimetypeString
.startsWith("video/")) {
1582 contentProviderClient
.delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1583 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});
1585 } catch (RemoteException e
) {
1586 Log_OC
.e(TAG
, "Exception deleting media file in MediaStore " + e
.getMessage());
1592 public void saveConflict(OCFile file
, boolean inConflict
) {
1593 if (!file
.isDown()) {
1596 ContentValues cv
= new ContentValues();
1597 cv
.put(ProviderTableMeta
.FILE_IN_CONFLICT
, inConflict
);
1599 if (getContentResolver() != null
) {
1600 updated
= getContentResolver().update(
1601 ProviderTableMeta
.CONTENT_URI_FILE
,
1603 ProviderTableMeta
._ID
+ "=?",
1604 new String
[] { String
.valueOf(file
.getFileId())}
1608 updated
= getContentProviderClient().update(
1609 ProviderTableMeta
.CONTENT_URI_FILE
,
1611 ProviderTableMeta
._ID
+ "=?",
1612 new String
[]{String
.valueOf(file
.getFileId())}
1614 } catch (RemoteException e
) {
1615 Log_OC
.e(TAG
, "Failed saving conflict in database " + e
.getMessage());
1619 Log_OC
.d(TAG
, "Number of files updated with CONFLICT: " + updated
);
1623 /// set conflict in all ancestor folders
1625 long parentId
= file
.getParentId();
1626 Set
<String
> ancestorIds
= new HashSet
<String
>();
1627 while (parentId
!= FileDataStorageManager
.ROOT_PARENT_ID
) {
1628 ancestorIds
.add(Long
.toString(parentId
));
1629 parentId
= getFileById(parentId
).getParentId();
1632 if (ancestorIds
.size() > 0) {
1633 StringBuffer whereBuffer
= new StringBuffer();
1634 whereBuffer
.append(ProviderTableMeta
._ID
).append(" IN (");
1635 for (int i
= 0; i
< ancestorIds
.size() - 1; i
++) {
1636 whereBuffer
.append("?,");
1638 whereBuffer
.append("?");
1639 whereBuffer
.append(")");
1641 if (getContentResolver() != null
) {
1642 updated
= getContentResolver().update(
1643 ProviderTableMeta
.CONTENT_URI_FILE
,
1645 whereBuffer
.toString(),
1646 ancestorIds
.toArray(new String
[]{})
1650 updated
= getContentProviderClient().update(
1651 ProviderTableMeta
.CONTENT_URI_FILE
,
1653 whereBuffer
.toString(),
1654 ancestorIds
.toArray(new String
[]{})
1656 } catch (RemoteException e
) {
1657 Log_OC
.e(TAG
, "Failed saving conflict in database " + e
.getMessage());
1660 } // else file is ROOT folder, no parent to set in conflict
1663 /// TODO update conflict in ancestor folders
1664 // (not directly unset; maybe there are more conflicts below them)
1668 Log_OC
.d(TAG
, "Number of parents updated with CONFLICT: " + updated
);