1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.datamodel
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collection
;
24 import java
.util
.Collections
;
25 import java
.util
.Iterator
;
26 import java
.util
.List
;
27 import java
.util
.Vector
;
29 import com
.owncloud
.android
.MainApp
;
30 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
31 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
32 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
33 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
34 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
35 import com
.owncloud
.android
.utils
.FileStorageUtils
;
37 import android
.accounts
.Account
;
38 import android
.content
.ContentProviderClient
;
39 import android
.content
.ContentProviderOperation
;
40 import android
.content
.ContentProviderResult
;
41 import android
.content
.ContentResolver
;
42 import android
.content
.ContentUris
;
43 import android
.content
.ContentValues
;
44 import android
.content
.Intent
;
45 import android
.content
.OperationApplicationException
;
46 import android
.database
.Cursor
;
47 import android
.net
.Uri
;
48 import android
.os
.RemoteException
;
49 import android
.provider
.MediaStore
;
51 public class FileDataStorageManager
{
53 public static final int ROOT_PARENT_ID
= 0;
55 private ContentResolver mContentResolver
;
56 private ContentProviderClient mContentProviderClient
;
57 private Account mAccount
;
59 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
62 public FileDataStorageManager(Account account
, ContentResolver cr
) {
63 mContentProviderClient
= null
;
64 mContentResolver
= cr
;
68 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
69 mContentProviderClient
= cp
;
70 mContentResolver
= null
;
75 public void setAccount(Account account
) {
79 public Account
getAccount() {
83 public void setContentResolver(ContentResolver cr
) {
84 mContentResolver
= cr
;
87 public ContentResolver
getContentResolver() {
88 return mContentResolver
;
91 public void setContentProviderClient(ContentProviderClient cp
) {
92 mContentProviderClient
= cp
;
95 public ContentProviderClient
getContentProviderClient() {
96 return mContentProviderClient
;
100 public OCFile
getFileByPath(String path
) {
101 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
103 if (c
.moveToFirst()) {
104 file
= createFileInstance(c
);
107 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
108 return createRootDir(); // root should always exist
114 public OCFile
getFileById(long id
) {
115 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
117 if (c
.moveToFirst()) {
118 file
= createFileInstance(c
);
124 public OCFile
getFileByLocalPath(String path
) {
125 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
127 if (c
.moveToFirst()) {
128 file
= createFileInstance(c
);
134 public boolean fileExists(long id
) {
135 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
138 public boolean fileExists(String path
) {
139 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
143 public Vector
<OCFile
> getFolderContent(OCFile f
) {
144 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
145 return getFolderContent(f
.getFileId());
148 return new Vector
<OCFile
>();
153 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
154 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
155 if (folder
!= null
) {
156 // TODO better implementation, filtering in the access to database instead of here
157 Vector
<OCFile
> tmp
= getFolderContent(folder
);
158 OCFile current
= null
;
159 for (int i
=0; i
<tmp
.size(); i
++) {
160 current
= tmp
.get(i
);
161 if (current
.isImage()) {
170 public boolean saveFile(OCFile file
) {
171 boolean overriden
= false
;
172 ContentValues cv
= new ContentValues();
173 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
175 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
176 file
.getModificationTimestampAtLastSyncForData()
178 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
179 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
180 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
181 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
182 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
183 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
184 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
185 if (!file
.isFolder())
186 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
187 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
188 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
189 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
190 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
191 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
192 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
193 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
194 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
195 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
196 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
198 boolean sameRemotePath
= fileExists(file
.getRemotePath());
199 if (sameRemotePath
||
200 fileExists(file
.getFileId()) ) { // for renamed files
202 OCFile oldFile
= null
;
203 if (sameRemotePath
) {
204 oldFile
= getFileByPath(file
.getRemotePath());
205 file
.setFileId(oldFile
.getFileId());
207 oldFile
= getFileById(file
.getFileId());
211 if (getContentResolver() != null
) {
212 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
213 ProviderTableMeta
._ID
+ "=?",
214 new String
[] { String
.valueOf(file
.getFileId()) });
217 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
218 cv
, ProviderTableMeta
._ID
+ "=?",
219 new String
[] { String
.valueOf(file
.getFileId()) });
220 } catch (RemoteException e
) {
222 "Fail to insert insert file to database "
227 Uri result_uri
= null
;
228 if (getContentResolver() != null
) {
229 result_uri
= getContentResolver().insert(
230 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
233 result_uri
= getContentProviderClient().insert(
234 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
235 } catch (RemoteException e
) {
237 "Fail to insert insert file to database "
241 if (result_uri
!= null
) {
242 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
244 file
.setFileId(new_id
);
248 // if (file.isFolder()) {
249 // updateFolderSize(file.getFileId());
251 // updateFolderSize(file.getParentId());
259 * Inserts or updates the list of files contained in a given folder.
261 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
262 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
265 * @param updatedFiles
266 * @param filesToRemove
268 public void saveFolder(
269 OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
272 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size()
273 + " children and " + filesToRemove
.size() + " files to remove");
275 ArrayList
<ContentProviderOperation
> operations
=
276 new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
278 // prepare operations to insert or update files to save in the given folder
279 for (OCFile file
: updatedFiles
) {
280 ContentValues cv
= new ContentValues();
281 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
283 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
284 file
.getModificationTimestampAtLastSyncForData()
286 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
287 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
288 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
289 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
290 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
291 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
292 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
293 if (!file
.isFolder()) {
294 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
296 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
297 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
298 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
299 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
300 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
301 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
302 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
303 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
304 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
305 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
307 boolean existsByPath
= fileExists(file
.getRemotePath());
308 if (existsByPath
|| fileExists(file
.getFileId())) {
309 // updating an existing file
310 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
312 withSelection( ProviderTableMeta
._ID
+ "=?",
313 new String
[] { String
.valueOf(file
.getFileId()) })
318 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
319 withValues(cv
).build());
323 // prepare operations to remove files in the given folder
324 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
325 ProviderTableMeta
.FILE_PATH
+ "=?";
326 String
[] whereArgs
= null
;
327 for (OCFile file
: filesToRemove
) {
328 if (file
.getParentId() == folder
.getFileId()) {
329 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
330 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
331 if (file
.isFolder()) {
332 operations
.add(ContentProviderOperation
.newDelete(
333 ContentUris
.withAppendedId(
334 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
336 ).withSelection(where
, whereArgs
).build());
339 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
340 if (localFolder
.exists()) {
341 removeLocalFolder(localFolder
);
344 operations
.add(ContentProviderOperation
.newDelete(
345 ContentUris
.withAppendedId(
346 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
348 ).withSelection(where
, whereArgs
).build());
351 String path
= file
.getStoragePath();
352 new File(path
).delete();
353 triggerMediaScan(path
); // notify MediaScanner about removed file
359 // update metadata of folder
360 ContentValues cv
= new ContentValues();
361 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
363 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
364 folder
.getModificationTimestampAtLastSyncForData()
366 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
367 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
368 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
369 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
370 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
371 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
372 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
373 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
374 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
375 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
376 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
377 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
378 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
379 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
380 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
382 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
384 withSelection( ProviderTableMeta
._ID
+ "=?",
385 new String
[] { String
.valueOf(folder
.getFileId()) })
388 // apply operations in batch
389 ContentProviderResult
[] results
= null
;
390 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
392 if (getContentResolver() != null
) {
393 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
396 results
= getContentProviderClient().applyBatch(operations
);
399 } catch (OperationApplicationException e
) {
400 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
402 } catch (RemoteException e
) {
403 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
406 // update new id in file objects for insertions
407 if (results
!= null
) {
409 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
411 for (int i
=0; i
<results
.length
; i
++) {
412 if (filesIt
.hasNext()) {
413 file
= filesIt
.next();
417 if (results
[i
].uri
!= null
) {
418 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
419 //updatedFiles.get(i).setFileId(newId);
421 file
.setFileId(newId
);
427 //updateFolderSize(folder.getFileId());
436 // private void updateFolderSize(long id) {
437 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
438 // Log_OC.d(TAG, "Updating size of " + id);
439 // if (getContentResolver() != null) {
440 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
441 // new ContentValues(),
442 // won't be used, but cannot be null; crashes in KLP
443 // ProviderTableMeta._ID + "=?",
444 // new String[] { String.valueOf(id) });
447 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
448 // new ContentValues(),
449 // won't be used, but cannot be null; crashes in KLP
450 // ProviderTableMeta._ID + "=?",
451 // new String[] { String.valueOf(id) });
453 // } catch (RemoteException e) {
455 // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
459 // Log_OC.e(TAG, "not updating size for folder " + id);
464 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
465 boolean success
= true
;
467 if (file
.isFolder()) {
468 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
472 Uri file_uri
= ContentUris
.withAppendedId(
473 ProviderTableMeta
.CONTENT_URI_FILE
,
476 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
477 ProviderTableMeta
.FILE_PATH
+ "=?";
478 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
480 if (getContentProviderClient() != null
) {
482 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
483 } catch (RemoteException e
) {
487 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
489 success
&= (deleted
> 0);
491 String localPath
= file
.getStoragePath();
492 if (removeLocalCopy
&& file
.isDown() && localPath
!= null
&& success
) {
493 success
= new File(localPath
).delete();
495 deleteFileInMediaScan(file
);
497 if (!removeDBData
&& success
) {
498 // maybe unnecessary, but should be checked TODO remove if unnecessary
499 file
.setStoragePath(null
);
509 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
510 boolean success
= true
;
511 if (folder
!= null
&& folder
.isFolder()) {
512 if (removeDBData
&& folder
.getFileId() != -1) {
513 success
= removeFolderInDb(folder
);
515 if (removeLocalContent
&& success
) {
516 success
= removeLocalFolder(folder
);
522 private boolean removeFolderInDb(OCFile folder
) {
523 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
524 folder
.getFileId()); // URI for recursive deletion
525 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
526 ProviderTableMeta
.FILE_PATH
+ "=?";
527 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
529 if (getContentProviderClient() != null
) {
531 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
532 } catch (RemoteException e
) {
536 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
541 private boolean removeLocalFolder(OCFile folder
) {
542 boolean success
= true
;
543 String localFolderPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
);
544 File localFolder
= new File(localFolderPath
);
545 if (localFolder
.exists()) {
546 // stage 1: remove the local files already registered in the files database
547 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
549 for (OCFile file
: files
) {
550 if (file
.isFolder()) {
551 success
&= removeLocalFolder(file
);
554 File localFile
= new File(file
.getStoragePath());
555 success
&= localFile
.delete();
557 deleteFileInMediaScan(file
); // notify MediaScanner about removed file
558 file
.setStoragePath(null
);
566 // stage 2: remove the folder itself and any local file inside out of sync;
567 // for instance, after clearing the app cache or reinstalling
568 success
&= removeLocalFolder(localFolder
);
573 private boolean removeLocalFolder(File localFolder
) {
574 boolean success
= true
;
575 File
[] localFiles
= localFolder
.listFiles();
576 if (localFiles
!= null
) {
577 for (File localFile
: localFiles
) {
578 if (localFile
.isDirectory()) {
579 success
&= removeLocalFolder(localFile
);
581 String path
= localFile
.getAbsolutePath();
582 success
&= localFile
.delete();
586 success
&= localFolder
.delete();
592 * Updates database and file system for a file or folder that was moved to a different location.
594 * TODO explore better (faster) implementations
595 * TODO throw exceptions up !
597 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
599 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
601 OCFile targetParent
= getFileByPath(targetParentPath
);
602 if (targetParent
== null
) {
603 throw new IllegalStateException("Parent folder of the target path does not exist!!");
606 /// 1. get all the descendants of the moved element in a single QUERY
608 if (getContentProviderClient() != null
) {
610 c
= getContentProviderClient().query(
611 ProviderTableMeta
.CONTENT_URI
,
613 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
614 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
617 file
.getRemotePath() + "%"
619 ProviderTableMeta
.FILE_PATH
+ " ASC "
621 } catch (RemoteException e
) {
622 Log_OC
.e(TAG
, e
.getMessage());
626 c
= getContentResolver().query(
627 ProviderTableMeta
.CONTENT_URI
,
629 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
630 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
633 file
.getRemotePath() + "%"
635 ProviderTableMeta
.FILE_PATH
+ " ASC "
639 /// 2. prepare a batch of update operations to change all the descendants
640 ArrayList
<ContentProviderOperation
> operations
=
641 new ArrayList
<ContentProviderOperation
>(c
.getCount());
642 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
643 List
<String
> originalPathsToTriggerMediaScan
= new ArrayList
<String
>();
644 List
<String
> newPathsToTriggerMediaScan
= new ArrayList
<String
>();
645 if (c
.moveToFirst()) {
646 int lengthOfOldPath
= file
.getRemotePath().length();
647 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
649 ContentValues cv
= new ContentValues(); // keep construction in the loop
650 OCFile child
= createFileInstance(c
);
652 ProviderTableMeta
.FILE_PATH
,
653 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
655 if (child
.getStoragePath() != null
&&
656 child
.getStoragePath().startsWith(defaultSavePath
)) {
657 // update link to downloaded content - but local move is not done here!
658 String targetLocalPath
= defaultSavePath
+ targetPath
+
659 child
.getStoragePath().substring(lengthOfOldStoragePath
);
661 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, targetLocalPath
);
663 originalPathsToTriggerMediaScan
.add(child
.getStoragePath());
664 newPathsToTriggerMediaScan
.add(targetLocalPath
);
667 if (child
.getRemotePath().equals(file
.getRemotePath())) {
669 ProviderTableMeta
.FILE_PARENT
,
670 targetParent
.getFileId()
674 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
677 ProviderTableMeta
._ID
+ "=?",
678 new String
[] { String
.valueOf(child
.getFileId()) }
682 } while (c
.moveToNext());
686 /// 3. apply updates in batch
688 if (getContentResolver() != null
) {
689 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
692 getContentProviderClient().applyBatch(operations
);
695 } catch (Exception e
) {
696 Log_OC
.e(TAG
, "Fail to update " + file
.getFileId() + " and descendants in database", e
);
699 /// 4. move in local file system
700 String originalLocalPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
701 String targetLocalPath
= defaultSavePath
+ targetPath
;
702 File localFile
= new File(originalLocalPath
);
703 boolean renamed
= false
;
704 if (localFile
.exists()) {
705 File targetFile
= new File(targetLocalPath
);
706 File targetFolder
= targetFile
.getParentFile();
707 if (!targetFolder
.exists()) {
708 targetFolder
.mkdirs();
710 renamed
= localFile
.renameTo(targetFile
);
714 Iterator
<String
> it
= originalPathsToTriggerMediaScan
.iterator();
715 while (it
.hasNext()) {
716 // Notify MediaScanner about removed file
717 deleteFileInMediaScan(file
);
718 triggerMediaScan(it
.next());
720 it
= newPathsToTriggerMediaScan
.iterator();
721 while (it
.hasNext()) {
722 // Notify MediaScanner about new file/folder
723 deleteFileInMediaScan(file
);
724 triggerMediaScan(it
.next());
732 private Vector
<OCFile
> getFolderContent(long parentId
) {
734 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
736 Uri req_uri
= Uri
.withAppendedPath(
737 ProviderTableMeta
.CONTENT_URI_DIR
,
738 String
.valueOf(parentId
));
741 if (getContentProviderClient() != null
) {
743 c
= getContentProviderClient().query(req_uri
, null
,
744 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
745 new String
[] { String
.valueOf(parentId
)}, null
);
746 } catch (RemoteException e
) {
747 Log_OC
.e(TAG
, e
.getMessage());
751 c
= getContentResolver().query(req_uri
, null
,
752 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
753 new String
[] { String
.valueOf(parentId
)}, null
);
756 if (c
.moveToFirst()) {
758 OCFile child
= createFileInstance(c
);
760 } while (c
.moveToNext());
765 Collections
.sort(ret
);
771 private OCFile
createRootDir() {
772 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
773 file
.setMimetype("DIR");
774 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
779 private boolean fileExists(String cmp_key
, String value
) {
781 if (getContentResolver() != null
) {
782 c
= getContentResolver()
783 .query(ProviderTableMeta
.CONTENT_URI
,
786 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
788 new String
[] { value
, mAccount
.name
}, null
);
791 c
= getContentProviderClient().query(
792 ProviderTableMeta
.CONTENT_URI
,
795 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
796 new String
[] { value
, mAccount
.name
}, null
);
797 } catch (RemoteException e
) {
799 "Couldn't determine file existance, assuming non existance: "
804 boolean retval
= c
.moveToFirst();
809 private Cursor
getCursorForValue(String key
, String value
) {
811 if (getContentResolver() != null
) {
812 c
= getContentResolver()
813 .query(ProviderTableMeta
.CONTENT_URI
,
816 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
818 new String
[] { value
, mAccount
.name
}, null
);
821 c
= getContentProviderClient().query(
822 ProviderTableMeta
.CONTENT_URI
,
824 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
825 + "=?", new String
[] { value
, mAccount
.name
},
827 } catch (RemoteException e
) {
828 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
836 private OCFile
createFileInstance(Cursor c
) {
839 file
= new OCFile(c
.getString(c
840 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
841 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
842 file
.setParentId(c
.getLong(c
843 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
844 file
.setMimetype(c
.getString(c
845 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
846 if (!file
.isFolder()) {
847 file
.setStoragePath(c
.getString(c
848 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
849 if (file
.getStoragePath() == null
) {
850 // try to find existing file and bind it with current account;
851 // with the current update of SynchronizeFolderOperation, this won't be
852 // necessary anymore after a full synchronization of the account
853 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
855 file
.setStoragePath(f
.getAbsolutePath());
856 file
.setLastSyncDateForData(f
.lastModified());
860 file
.setFileLength(c
.getLong(c
861 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
862 file
.setCreationTimestamp(c
.getLong(c
863 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
864 file
.setModificationTimestamp(c
.getLong(c
865 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
866 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
867 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
868 file
.setLastSyncDateForProperties(c
.getLong(c
869 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
870 file
.setLastSyncDateForData(c
.getLong(c
.
871 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
872 file
.setKeepInSync(c
.getInt(
873 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
874 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
875 file
.setShareByLink(c
.getInt(
876 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
877 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
878 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
879 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
880 file
.setNeedsUpdateThumbnail(c
.getInt(
881 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
888 * Returns if the file/folder is shared by link or not
889 * @param path Path of the file/folder
892 public boolean isShareByLink(String path
) {
893 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
895 if (c
.moveToFirst()) {
896 file
= createFileInstance(c
);
899 return file
.isShareByLink();
903 * Returns the public link of the file/folder
904 * @param path Path of the file/folder
907 public String
getPublicLink(String path
) {
908 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
910 if (c
.moveToFirst()) {
911 file
= createFileInstance(c
);
914 return file
.getPublicLink();
918 // Methods for Shares
919 public boolean saveShare(OCShare share
) {
920 boolean overriden
= false
;
921 ContentValues cv
= new ContentValues();
922 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
923 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
924 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
925 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
926 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
927 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
928 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
929 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
930 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
932 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
933 share
.getSharedWithDisplayName()
935 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
936 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
937 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
938 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
940 if (shareExists(share
.getIdRemoteShared())) { // for renamed files
943 if (getContentResolver() != null
) {
944 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
945 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
946 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
949 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
950 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
951 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
952 } catch (RemoteException e
) {
954 "Fail to insert insert file to database "
959 Uri result_uri
= null
;
960 if (getContentResolver() != null
) {
961 result_uri
= getContentResolver().insert(
962 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
965 result_uri
= getContentProviderClient().insert(
966 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
967 } catch (RemoteException e
) {
969 "Fail to insert insert file to database "
973 if (result_uri
!= null
) {
974 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
984 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
986 if (getContentResolver() != null
) {
987 c
= getContentResolver().query(
988 ProviderTableMeta
.CONTENT_URI_SHARE
,
990 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
991 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
992 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
993 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
997 c
= getContentProviderClient().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
},
1006 } catch (RemoteException e
) {
1007 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1011 OCShare share
= null
;
1012 if (c
.moveToFirst()) {
1013 share
= createShareInstance(c
);
1019 private OCShare
createShareInstance(Cursor c
) {
1020 OCShare share
= null
;
1022 share
= new OCShare(c
.getString(c
1023 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1024 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1025 share
.setFileSource(c
.getLong(c
1026 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1027 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1028 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1029 share
.setPermissions(c
.getInt(c
1030 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1031 share
.setSharedDate(c
.getLong(c
1032 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1033 share
.setExpirationDate(c
.getLong(c
1034 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1035 share
.setToken(c
.getString(c
1036 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1037 share
.setSharedWithDisplayName(c
.getString(c
1038 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1039 share
.setIsFolder(c
.getInt(
1040 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1041 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1042 share
.setIdRemoteShared(
1043 c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
))
1050 private boolean shareExists(String cmp_key
, String value
) {
1052 if (getContentResolver() != null
) {
1053 c
= getContentResolver()
1054 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1057 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1059 new String
[] { value
, mAccount
.name
}, null
);
1062 c
= getContentProviderClient().query(
1063 ProviderTableMeta
.CONTENT_URI_SHARE
,
1066 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1067 new String
[] { value
, mAccount
.name
}, null
);
1068 } catch (RemoteException e
) {
1070 "Couldn't determine file existance, assuming non existance: "
1075 boolean retval
= c
.moveToFirst();
1080 private boolean shareExists(long remoteId
) {
1081 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1084 private void cleanSharedFiles() {
1085 ContentValues cv
= new ContentValues();
1086 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1087 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1088 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1089 String
[] whereArgs
= new String
[]{mAccount
.name
};
1091 if (getContentResolver() != null
) {
1092 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1096 getContentProviderClient().update(
1097 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1100 } catch (RemoteException e
) {
1101 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1106 private void cleanSharedFilesInFolder(OCFile folder
) {
1107 ContentValues cv
= new ContentValues();
1108 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1109 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1110 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1111 ProviderTableMeta
.FILE_PARENT
+ "=?";
1112 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1114 if (getContentResolver() != null
) {
1115 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1119 getContentProviderClient().update(
1120 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1123 } catch (RemoteException e
) {
1124 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1129 private void cleanShares() {
1130 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1131 String
[] whereArgs
= new String
[]{mAccount
.name
};
1133 if (getContentResolver() != null
) {
1134 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1138 getContentProviderClient().delete(
1139 ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
1142 } catch (RemoteException e
) {
1143 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1148 public void saveShares(Collection
<OCShare
> shares
) {
1150 if (shares
!= null
) {
1151 ArrayList
<ContentProviderOperation
> operations
=
1152 new ArrayList
<ContentProviderOperation
>(shares
.size());
1154 // prepare operations to insert or update files to save in the given folder
1155 for (OCShare share
: shares
) {
1156 ContentValues cv
= new ContentValues();
1157 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1158 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1159 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1160 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1161 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1162 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1163 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1164 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1165 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1167 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1168 share
.getSharedWithDisplayName()
1170 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1171 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1172 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1173 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1175 if (shareExists(share
.getIdRemoteShared())) {
1176 // updating an existing file
1178 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1181 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1182 new String
[] { String
.valueOf(share
.getIdRemoteShared()) }
1188 // adding a new file
1190 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1197 // apply operations in batch
1198 if (operations
.size() > 0) {
1199 @SuppressWarnings("unused")
1200 ContentProviderResult
[] results
= null
;
1201 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1202 " operations to FileContentProvider");
1204 if (getContentResolver() != null
) {
1205 results
= getContentResolver().applyBatch(
1206 MainApp
.getAuthority(), operations
1210 results
= getContentProviderClient().applyBatch(operations
);
1213 } catch (OperationApplicationException e
) {
1214 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1216 } catch (RemoteException e
) {
1217 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1224 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1227 if (sharedFiles
!= null
) {
1228 ArrayList
<ContentProviderOperation
> operations
=
1229 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1231 // prepare operations to insert or update files to save in the given folder
1232 for (OCFile file
: sharedFiles
) {
1233 ContentValues cv
= new ContentValues();
1234 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1236 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1237 file
.getModificationTimestampAtLastSyncForData()
1239 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1240 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1241 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1242 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1243 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1244 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1245 if (!file
.isFolder()) {
1246 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1248 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1249 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1251 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1252 file
.getLastSyncDateForData()
1254 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1255 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1256 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1257 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1258 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1259 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1261 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1262 file
.needsUpdateThumbnail() ?
1 : 0
1265 boolean existsByPath
= fileExists(file
.getRemotePath());
1266 if (existsByPath
|| fileExists(file
.getFileId())) {
1267 // updating an existing file
1269 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1272 ProviderTableMeta
._ID
+ "=?",
1273 new String
[] { String
.valueOf(file
.getFileId()) }
1278 // adding a new file
1280 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1287 // apply operations in batch
1288 if (operations
.size() > 0) {
1289 @SuppressWarnings("unused")
1290 ContentProviderResult
[] results
= null
;
1291 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1292 " operations to FileContentProvider");
1294 if (getContentResolver() != null
) {
1295 results
= getContentResolver().applyBatch(
1296 MainApp
.getAuthority(), operations
1300 results
= getContentProviderClient().applyBatch(operations
);
1303 } catch (OperationApplicationException e
) {
1304 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1306 } catch (RemoteException e
) {
1307 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1314 public void removeShare(OCShare share
){
1315 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1316 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1317 ProviderTableMeta
.FILE_PATH
+ "=?";
1318 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1319 if (getContentProviderClient() != null
) {
1321 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1322 } catch (RemoteException e
) {
1323 e
.printStackTrace();
1326 getContentResolver().delete(share_uri
, where
, whereArgs
);
1330 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1333 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1335 for (OCShare share
: shares
) {
1337 String path
= share
.getPath();
1338 if (share
.isFolder()) {
1339 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1342 // Update OCFile with data from share: ShareByLink and publicLink
1343 OCFile file
= getFileByPath(path
);
1345 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1346 file
.setShareByLink(true
);
1347 sharedFiles
.add(file
);
1352 updateSharedFiles(sharedFiles
);
1356 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1357 cleanSharedFilesInFolder(folder
);
1358 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1359 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1361 if (shares
!= null
) {
1362 // prepare operations to insert or update files to save in the given folder
1363 for (OCShare share
: shares
) {
1364 ContentValues cv
= new ContentValues();
1365 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1366 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1367 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1368 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1369 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1370 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1371 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1372 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1373 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1375 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1376 share
.getSharedWithDisplayName()
1378 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1379 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1380 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1381 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1384 if (shareExists(share.getIdRemoteShared())) {
1385 // updating an existing share resource
1387 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1389 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1390 new String[] { String.valueOf(share.getIdRemoteShared()) })
1395 // adding a new share resource
1397 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1405 // apply operations in batch
1406 if (operations
.size() > 0) {
1407 @SuppressWarnings("unused")
1408 ContentProviderResult
[] results
= null
;
1409 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1411 if (getContentResolver() != null
) {
1412 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1415 results
= getContentProviderClient().applyBatch(operations
);
1418 } catch (OperationApplicationException e
) {
1419 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1421 } catch (RemoteException e
) {
1422 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1429 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1430 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
1432 if (folder
!= null
) {
1433 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1434 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1435 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1437 Vector
<OCFile
> files
= getFolderContent(folder
);
1439 for (OCFile file
: files
) {
1440 whereArgs
[0] = file
.getRemotePath();
1441 preparedOperations
.add(
1442 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1443 withSelection(where
, whereArgs
).
1448 return preparedOperations
;
1451 if (operations.size() > 0) {
1453 if (getContentResolver() != null) {
1454 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1457 getContentProviderClient().applyBatch(operations);
1460 } catch (OperationApplicationException e) {
1461 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1463 } catch (RemoteException e) {
1464 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1470 if (getContentResolver() != null) {
1472 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1477 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1481 } catch (RemoteException e) {
1482 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());
1489 public void triggerMediaScan(String path
) {
1490 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
1491 intent
.setData(Uri
.fromFile(new File(path
)));
1492 MainApp
.getAppContext().sendBroadcast(intent
);
1495 public void deleteFileInMediaScan(OCFile file
) {
1497 String path
= file
.getStoragePath();
1498 if (file
.isImage()) {
1500 getContentResolver().delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1501 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1502 } else if (file
.isAudio()) {
1504 getContentResolver().delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1505 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1506 } else if (file
.isVideo()) {
1508 getContentResolver().delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1509 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});