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
;
50 public class FileDataStorageManager
{
52 public static final int ROOT_PARENT_ID
= 0;
54 private ContentResolver mContentResolver
;
55 private ContentProviderClient mContentProviderClient
;
56 private Account mAccount
;
58 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
61 public FileDataStorageManager(Account account
, ContentResolver cr
) {
62 mContentProviderClient
= null
;
63 mContentResolver
= cr
;
67 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
68 mContentProviderClient
= cp
;
69 mContentResolver
= null
;
74 public void setAccount(Account account
) {
78 public Account
getAccount() {
82 public void setContentResolver(ContentResolver cr
) {
83 mContentResolver
= cr
;
86 public ContentResolver
getContentResolver() {
87 return mContentResolver
;
90 public void setContentProviderClient(ContentProviderClient cp
) {
91 mContentProviderClient
= cp
;
94 public ContentProviderClient
getContentProviderClient() {
95 return mContentProviderClient
;
99 public OCFile
getFileByPath(String path
) {
100 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
102 if (c
.moveToFirst()) {
103 file
= createFileInstance(c
);
106 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
107 return createRootDir(); // root should always exist
113 public OCFile
getFileById(long id
) {
114 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
116 if (c
.moveToFirst()) {
117 file
= createFileInstance(c
);
123 public OCFile
getFileByLocalPath(String path
) {
124 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
126 if (c
.moveToFirst()) {
127 file
= createFileInstance(c
);
133 public boolean fileExists(long id
) {
134 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
137 public boolean fileExists(String path
) {
138 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
142 public Vector
<OCFile
> getFolderContent(OCFile f
) {
143 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
144 return getFolderContent(f
.getFileId());
147 return new Vector
<OCFile
>();
152 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
153 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
154 if (folder
!= null
) {
155 // TODO better implementation, filtering in the access to database instead of here
156 Vector
<OCFile
> tmp
= getFolderContent(folder
);
157 OCFile current
= null
;
158 for (int i
=0; i
<tmp
.size(); i
++) {
159 current
= tmp
.get(i
);
160 if (current
.isImage()) {
169 public boolean saveFile(OCFile file
) {
170 boolean overriden
= false
;
171 ContentValues cv
= new ContentValues();
172 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
174 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
175 file
.getModificationTimestampAtLastSyncForData()
177 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
178 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
179 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
180 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
181 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
182 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
183 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
184 if (!file
.isFolder())
185 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
186 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
187 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
188 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
189 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
190 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
191 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
192 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
193 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
194 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
195 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
196 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
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
266 * @param removeNotUpdated
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());
306 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
308 boolean existsByPath
= fileExists(file
.getRemotePath());
309 if (existsByPath
|| fileExists(file
.getFileId())) {
310 // updating an existing file
311 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
313 withSelection( ProviderTableMeta
._ID
+ "=?",
314 new String
[] { String
.valueOf(file
.getFileId()) })
319 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
320 withValues(cv
).build());
324 // prepare operations to remove files in the given folder
325 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
326 ProviderTableMeta
.FILE_PATH
+ "=?";
327 String
[] whereArgs
= null
;
328 for (OCFile file
: filesToRemove
) {
329 if (file
.getParentId() == folder
.getFileId()) {
330 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
331 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
332 if (file
.isFolder()) {
333 operations
.add(ContentProviderOperation
.newDelete(
334 ContentUris
.withAppendedId(
335 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
337 ).withSelection(where
, whereArgs
).build());
340 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
341 if (localFolder
.exists()) {
342 removeLocalFolder(localFolder
);
345 operations
.add(ContentProviderOperation
.newDelete(
346 ContentUris
.withAppendedId(
347 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
349 ).withSelection(where
, whereArgs
).build());
352 String path
= file
.getStoragePath();
353 new File(path
).delete();
354 triggerMediaScan(path
); // notify MediaScanner about removed file
360 // update metadata of folder
361 ContentValues cv
= new ContentValues();
362 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
364 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
365 folder
.getModificationTimestampAtLastSyncForData()
367 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
368 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
369 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
370 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
371 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
372 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
373 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
374 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
375 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
376 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
377 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
378 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
379 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
380 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
381 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
383 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
385 withSelection( ProviderTableMeta
._ID
+ "=?",
386 new String
[] { String
.valueOf(folder
.getFileId()) })
389 // apply operations in batch
390 ContentProviderResult
[] results
= null
;
391 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
393 if (getContentResolver() != null
) {
394 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
397 results
= getContentProviderClient().applyBatch(operations
);
400 } catch (OperationApplicationException e
) {
401 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
403 } catch (RemoteException e
) {
404 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
407 // update new id in file objects for insertions
408 if (results
!= null
) {
410 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
412 for (int i
=0; i
<results
.length
; i
++) {
413 if (filesIt
.hasNext()) {
414 file
= filesIt
.next();
418 if (results
[i
].uri
!= null
) {
419 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
420 //updatedFiles.get(i).setFileId(newId);
422 file
.setFileId(newId
);
428 //updateFolderSize(folder.getFileId());
437 // private void updateFolderSize(long id) {
438 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
439 // Log_OC.d(TAG, "Updating size of " + id);
440 // if (getContentResolver() != null) {
441 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
442 // new ContentValues(),
443 // won't be used, but cannot be null; crashes in KLP
444 // ProviderTableMeta._ID + "=?",
445 // new String[] { String.valueOf(id) });
448 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
449 // new ContentValues(),
450 // won't be used, but cannot be null; crashes in KLP
451 // ProviderTableMeta._ID + "=?",
452 // new String[] { String.valueOf(id) });
454 // } catch (RemoteException e) {
456 // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
460 // Log_OC.e(TAG, "not updating size for folder " + id);
465 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
466 boolean success
= true
;
468 if (file
.isFolder()) {
469 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
473 Uri file_uri
= ContentUris
.withAppendedId(
474 ProviderTableMeta
.CONTENT_URI_FILE
,
477 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
478 ProviderTableMeta
.FILE_PATH
+ "=?";
479 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
481 if (getContentProviderClient() != null
) {
483 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
484 } catch (RemoteException e
) {
488 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
490 success
&= (deleted
> 0);
492 String localPath
= file
.getStoragePath();
493 if (removeLocalCopy
&& file
.isDown() && localPath
!= null
&& success
) {
494 success
= new File(localPath
).delete();
496 triggerMediaScan(localPath
);
498 if (!removeDBData
&& success
) {
499 // maybe unnecessary, but should be checked TODO remove if unnecessary
500 file
.setStoragePath(null
);
510 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
511 boolean success
= true
;
512 if (folder
!= null
&& folder
.isFolder()) {
513 if (removeDBData
&& folder
.getFileId() != -1) {
514 success
= removeFolderInDb(folder
);
516 if (removeLocalContent
&& success
) {
517 success
= removeLocalFolder(folder
);
523 private boolean removeFolderInDb(OCFile folder
) {
524 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
525 folder
.getFileId()); // URI for recursive deletion
526 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
527 ProviderTableMeta
.FILE_PATH
+ "=?";
528 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
530 if (getContentProviderClient() != null
) {
532 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
533 } catch (RemoteException e
) {
537 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
542 private boolean removeLocalFolder(OCFile folder
) {
543 boolean success
= true
;
544 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
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 String path
= file
.getStoragePath();
555 File localFile
= new File(file
.getStoragePath());
556 success
&= localFile
.delete();
558 file
.setStoragePath(null
);
560 triggerMediaScan(path
); // notify MediaScanner about removed file
567 // stage 2: remove the folder itself and any local file inside out of sync;
568 // for instance, after clearing the app cache or reinstalling
569 success
&= removeLocalFolder(localFolder
);
574 private boolean removeLocalFolder(File localFolder
) {
575 boolean success
= true
;
576 File
[] localFiles
= localFolder
.listFiles();
577 if (localFiles
!= null
) {
578 for (File localFile
: localFiles
) {
579 if (localFile
.isDirectory()) {
580 success
&= removeLocalFolder(localFile
);
582 String path
= localFile
.getAbsolutePath();
583 success
&= localFile
.delete();
584 triggerMediaScan(path
); // notify MediaScanner about removed file
588 success
&= localFolder
.delete();
594 * Updates database and file system for a file or folder that was moved to a different location.
596 * TODO explore better (faster) implementations
597 * TODO throw exceptions up !
599 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
601 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
603 OCFile targetParent
= getFileByPath(targetParentPath
);
604 if (targetParent
== null
) {
605 throw new IllegalStateException("Parent folder of the target path does not exist!!");
608 /// 1. get all the descendants of the moved element in a single QUERY
610 if (getContentProviderClient() != null
) {
612 c
= getContentProviderClient().query(
613 ProviderTableMeta
.CONTENT_URI
,
615 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
616 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
619 file
.getRemotePath() + "%"
621 ProviderTableMeta
.FILE_PATH
+ " ASC "
623 } catch (RemoteException e
) {
624 Log_OC
.e(TAG
, e
.getMessage());
628 c
= getContentResolver().query(
629 ProviderTableMeta
.CONTENT_URI
,
631 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
632 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
635 file
.getRemotePath() + "%"
637 ProviderTableMeta
.FILE_PATH
+ " ASC "
641 /// 2. prepare a batch of update operations to change all the descendants
642 ArrayList
<ContentProviderOperation
> operations
=
643 new ArrayList
<ContentProviderOperation
>(c
.getCount());
644 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
645 List
<String
> originalPathsToTriggerMediaScan
= new ArrayList
<String
>();
646 List
<String
> newPathsToTriggerMediaScan
= new ArrayList
<String
>();
647 if (c
.moveToFirst()) {
648 int lengthOfOldPath
= file
.getRemotePath().length();
649 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
651 ContentValues cv
= new ContentValues(); // keep construction in the loop
652 OCFile child
= createFileInstance(c
);
654 ProviderTableMeta
.FILE_PATH
,
655 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
657 if (child
.getStoragePath() != null
&&
658 child
.getStoragePath().startsWith(defaultSavePath
)) {
659 // update link to downloaded content - but local move is not done here!
660 String targetLocalPath
= defaultSavePath
+ targetPath
+
661 child
.getStoragePath().substring(lengthOfOldStoragePath
);
663 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, targetLocalPath
);
665 originalPathsToTriggerMediaScan
.add(child
.getStoragePath());
666 newPathsToTriggerMediaScan
.add(targetLocalPath
);
669 if (child
.getRemotePath().equals(file
.getRemotePath())) {
671 ProviderTableMeta
.FILE_PARENT
,
672 targetParent
.getFileId()
676 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
679 ProviderTableMeta
._ID
+ "=?",
680 new String
[] { String
.valueOf(child
.getFileId()) }
684 } while (c
.moveToNext());
688 /// 3. apply updates in batch
690 if (getContentResolver() != null
) {
691 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
694 getContentProviderClient().applyBatch(operations
);
697 } catch (Exception e
) {
698 Log_OC
.e(TAG
, "Fail to update " + file
.getFileId() + " and descendants in database", e
);
701 /// 4. move in local file system
702 String originalLocalPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
703 String targetLocalPath
= defaultSavePath
+ targetPath
;
704 File localFile
= new File(originalLocalPath
);
705 boolean renamed
= false
;
706 if (localFile
.exists()) {
707 File targetFile
= new File(targetLocalPath
);
708 File targetFolder
= targetFile
.getParentFile();
709 if (!targetFolder
.exists()) {
710 targetFolder
.mkdirs();
712 renamed
= localFile
.renameTo(targetFile
);
716 Iterator
<String
> it
= originalPathsToTriggerMediaScan
.iterator();
717 while (it
.hasNext()) {
718 // Notify MediaScanner about removed file
719 triggerMediaScan(it
.next());
721 it
= newPathsToTriggerMediaScan
.iterator();
722 while (it
.hasNext()) {
723 // Notify MediaScanner about new file/folder
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
);
882 file
.setDownloading(c
.getInt(
883 c
.getColumnIndex(ProviderTableMeta
.FILE_IS_DOWNLOADING
)) == 1 ? true
: false
);
890 * Returns if the file/folder is shared by link or not
891 * @param path Path of the file/folder
894 public boolean isShareByLink(String path
) {
895 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
897 if (c
.moveToFirst()) {
898 file
= createFileInstance(c
);
901 return file
.isShareByLink();
905 * Returns the public link of the file/folder
906 * @param path Path of the file/folder
909 public String
getPublicLink(String path
) {
910 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
912 if (c
.moveToFirst()) {
913 file
= createFileInstance(c
);
916 return file
.getPublicLink();
920 // Methods for Shares
921 public boolean saveShare(OCShare share
) {
922 boolean overriden
= false
;
923 ContentValues cv
= new ContentValues();
924 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
925 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
926 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
927 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
928 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
929 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
930 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
931 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
932 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
934 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
935 share
.getSharedWithDisplayName()
937 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
938 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
939 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
940 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
942 if (shareExists(share
.getIdRemoteShared())) { // for renamed files
945 if (getContentResolver() != null
) {
946 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
947 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
948 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
951 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
952 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
953 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
954 } catch (RemoteException e
) {
956 "Fail to insert insert file to database "
961 Uri result_uri
= null
;
962 if (getContentResolver() != null
) {
963 result_uri
= getContentResolver().insert(
964 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
967 result_uri
= getContentProviderClient().insert(
968 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
969 } catch (RemoteException e
) {
971 "Fail to insert insert file to database "
975 if (result_uri
!= null
) {
976 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
986 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
988 if (getContentResolver() != null
) {
989 c
= getContentResolver().query(
990 ProviderTableMeta
.CONTENT_URI_SHARE
,
992 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
993 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
994 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
995 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
999 c
= getContentProviderClient().query(
1000 ProviderTableMeta
.CONTENT_URI_SHARE
,
1002 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1003 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1004 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1005 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1008 } catch (RemoteException e
) {
1009 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1013 OCShare share
= null
;
1014 if (c
.moveToFirst()) {
1015 share
= createShareInstance(c
);
1021 private OCShare
createShareInstance(Cursor c
) {
1022 OCShare share
= null
;
1024 share
= new OCShare(c
.getString(c
1025 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1026 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1027 share
.setFileSource(c
.getLong(c
1028 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1029 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1030 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1031 share
.setPermissions(c
.getInt(c
1032 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1033 share
.setSharedDate(c
.getLong(c
1034 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1035 share
.setExpirationDate(c
.getLong(c
1036 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1037 share
.setToken(c
.getString(c
1038 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1039 share
.setSharedWithDisplayName(c
.getString(c
1040 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1041 share
.setIsFolder(c
.getInt(
1042 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1043 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1044 share
.setIdRemoteShared(
1045 c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
))
1052 private boolean shareExists(String cmp_key
, String value
) {
1054 if (getContentResolver() != null
) {
1055 c
= getContentResolver()
1056 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1059 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1061 new String
[] { value
, mAccount
.name
}, null
);
1064 c
= getContentProviderClient().query(
1065 ProviderTableMeta
.CONTENT_URI_SHARE
,
1068 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1069 new String
[] { value
, mAccount
.name
}, null
);
1070 } catch (RemoteException e
) {
1072 "Couldn't determine file existance, assuming non existance: "
1077 boolean retval
= c
.moveToFirst();
1082 private boolean shareExists(long remoteId
) {
1083 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1086 private void cleanSharedFiles() {
1087 ContentValues cv
= new ContentValues();
1088 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1089 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1090 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1091 String
[] whereArgs
= new String
[]{mAccount
.name
};
1093 if (getContentResolver() != null
) {
1094 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1098 getContentProviderClient().update(
1099 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1102 } catch (RemoteException e
) {
1103 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1108 private void cleanSharedFilesInFolder(OCFile folder
) {
1109 ContentValues cv
= new ContentValues();
1110 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1111 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1112 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1113 ProviderTableMeta
.FILE_PARENT
+ "=?";
1114 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1116 if (getContentResolver() != null
) {
1117 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1121 getContentProviderClient().update(
1122 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1125 } catch (RemoteException e
) {
1126 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1131 private void cleanShares() {
1132 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1133 String
[] whereArgs
= new String
[]{mAccount
.name
};
1135 if (getContentResolver() != null
) {
1136 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1140 getContentProviderClient().delete(
1141 ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
1144 } catch (RemoteException e
) {
1145 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1150 public void saveShares(Collection
<OCShare
> shares
) {
1152 if (shares
!= null
) {
1153 ArrayList
<ContentProviderOperation
> operations
=
1154 new ArrayList
<ContentProviderOperation
>(shares
.size());
1156 // prepare operations to insert or update files to save in the given folder
1157 for (OCShare share
: shares
) {
1158 ContentValues cv
= new ContentValues();
1159 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1160 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1161 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1162 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1163 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1164 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1165 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1166 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1167 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1169 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1170 share
.getSharedWithDisplayName()
1172 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1173 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1174 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1175 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1177 if (shareExists(share
.getIdRemoteShared())) {
1178 // updating an existing file
1180 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1183 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1184 new String
[] { String
.valueOf(share
.getIdRemoteShared()) }
1190 // adding a new file
1192 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1199 // apply operations in batch
1200 if (operations
.size() > 0) {
1201 @SuppressWarnings("unused")
1202 ContentProviderResult
[] results
= null
;
1203 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1204 " operations to FileContentProvider");
1206 if (getContentResolver() != null
) {
1207 results
= getContentResolver().applyBatch(
1208 MainApp
.getAuthority(), operations
1212 results
= getContentProviderClient().applyBatch(operations
);
1215 } catch (OperationApplicationException e
) {
1216 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1218 } catch (RemoteException e
) {
1219 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1226 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1229 if (sharedFiles
!= null
) {
1230 ArrayList
<ContentProviderOperation
> operations
=
1231 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1233 // prepare operations to insert or update files to save in the given folder
1234 for (OCFile file
: sharedFiles
) {
1235 ContentValues cv
= new ContentValues();
1236 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1238 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1239 file
.getModificationTimestampAtLastSyncForData()
1241 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1242 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1243 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1244 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1245 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1246 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1247 if (!file
.isFolder()) {
1248 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1250 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1251 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1253 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1254 file
.getLastSyncDateForData()
1256 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1257 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1258 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1259 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1260 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1261 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1263 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1264 file
.needsUpdateThumbnail() ?
1 : 0
1267 ProviderTableMeta
.FILE_IS_DOWNLOADING
,
1268 file
.isDownloading() ?
1 : 0
1271 boolean existsByPath
= fileExists(file
.getRemotePath());
1272 if (existsByPath
|| fileExists(file
.getFileId())) {
1273 // updating an existing file
1275 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1278 ProviderTableMeta
._ID
+ "=?",
1279 new String
[] { String
.valueOf(file
.getFileId()) }
1284 // adding a new file
1286 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1293 // apply operations in batch
1294 if (operations
.size() > 0) {
1295 @SuppressWarnings("unused")
1296 ContentProviderResult
[] results
= null
;
1297 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1298 " operations to FileContentProvider");
1300 if (getContentResolver() != null
) {
1301 results
= getContentResolver().applyBatch(
1302 MainApp
.getAuthority(), operations
1306 results
= getContentProviderClient().applyBatch(operations
);
1309 } catch (OperationApplicationException e
) {
1310 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1312 } catch (RemoteException e
) {
1313 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1320 public void removeShare(OCShare share
){
1321 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1322 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1323 ProviderTableMeta
.FILE_PATH
+ "=?";
1324 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1325 if (getContentProviderClient() != null
) {
1327 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1328 } catch (RemoteException e
) {
1329 e
.printStackTrace();
1332 getContentResolver().delete(share_uri
, where
, whereArgs
);
1336 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1339 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1341 for (OCShare share
: shares
) {
1343 String path
= share
.getPath();
1344 if (share
.isFolder()) {
1345 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1348 // Update OCFile with data from share: ShareByLink and publicLink
1349 OCFile file
= getFileByPath(path
);
1351 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1352 file
.setShareByLink(true
);
1353 sharedFiles
.add(file
);
1358 updateSharedFiles(sharedFiles
);
1362 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1363 cleanSharedFilesInFolder(folder
);
1364 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1365 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1367 if (shares
!= null
) {
1368 // prepare operations to insert or update files to save in the given folder
1369 for (OCShare share
: shares
) {
1370 ContentValues cv
= new ContentValues();
1371 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1372 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1373 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1374 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1375 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1376 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1377 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1378 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1379 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1381 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1382 share
.getSharedWithDisplayName()
1384 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1385 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1386 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1387 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1390 if (shareExists(share.getIdRemoteShared())) {
1391 // updating an existing share resource
1393 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1395 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1396 new String[] { String.valueOf(share.getIdRemoteShared()) })
1401 // adding a new share resource
1403 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1411 // apply operations in batch
1412 if (operations
.size() > 0) {
1413 @SuppressWarnings("unused")
1414 ContentProviderResult
[] results
= null
;
1415 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1417 if (getContentResolver() != null
) {
1418 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1421 results
= getContentProviderClient().applyBatch(operations
);
1424 } catch (OperationApplicationException e
) {
1425 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1427 } catch (RemoteException e
) {
1428 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1435 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1436 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
1438 if (folder
!= null
) {
1439 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1440 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1441 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1443 Vector
<OCFile
> files
= getFolderContent(folder
);
1445 for (OCFile file
: files
) {
1446 whereArgs
[0] = file
.getRemotePath();
1447 preparedOperations
.add(
1448 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1449 withSelection(where
, whereArgs
).
1454 return preparedOperations
;
1457 if (operations.size() > 0) {
1459 if (getContentResolver() != null) {
1460 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1463 getContentProviderClient().applyBatch(operations);
1466 } catch (OperationApplicationException e) {
1467 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1469 } catch (RemoteException e) {
1470 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1476 if (getContentResolver() != null) {
1478 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1483 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1487 } catch (RemoteException e) {
1488 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());
1495 public void triggerMediaScan(String path
) {
1496 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
1497 intent
.setData(Uri
.fromFile(new File(path
)));
1498 MainApp
.getAppContext().sendBroadcast(intent
);