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
.Vector
;
28 import com
.owncloud
.android
.MainApp
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
31 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
32 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
33 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
34 import com
.owncloud
.android
.utils
.FileStorageUtils
;
36 import android
.accounts
.Account
;
37 import android
.content
.ContentProviderClient
;
38 import android
.content
.ContentProviderOperation
;
39 import android
.content
.ContentProviderResult
;
40 import android
.content
.ContentResolver
;
41 import android
.content
.ContentUris
;
42 import android
.content
.ContentValues
;
43 import android
.content
.Intent
;
44 import android
.content
.OperationApplicationException
;
45 import android
.database
.Cursor
;
46 import android
.net
.Uri
;
47 import android
.os
.RemoteException
;
49 public class FileDataStorageManager
{
51 public static final int ROOT_PARENT_ID
= 0;
53 private ContentResolver mContentResolver
;
54 private ContentProviderClient mContentProviderClient
;
55 private Account mAccount
;
57 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
60 public FileDataStorageManager(Account account
, ContentResolver cr
) {
61 mContentProviderClient
= null
;
62 mContentResolver
= cr
;
66 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
67 mContentProviderClient
= cp
;
68 mContentResolver
= null
;
73 public void setAccount(Account account
) {
77 public Account
getAccount() {
81 public void setContentResolver(ContentResolver cr
) {
82 mContentResolver
= cr
;
85 public ContentResolver
getContentResolver() {
86 return mContentResolver
;
89 public void setContentProviderClient(ContentProviderClient cp
) {
90 mContentProviderClient
= cp
;
93 public ContentProviderClient
getContentProviderClient() {
94 return mContentProviderClient
;
98 public OCFile
getFileByPath(String path
) {
99 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
101 if (c
.moveToFirst()) {
102 file
= createFileInstance(c
);
105 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
106 return createRootDir(); // root should always exist
112 public OCFile
getFileById(long id
) {
113 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
115 if (c
.moveToFirst()) {
116 file
= createFileInstance(c
);
122 public OCFile
getFileByLocalPath(String path
) {
123 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
125 if (c
.moveToFirst()) {
126 file
= createFileInstance(c
);
132 public boolean fileExists(long id
) {
133 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
136 public boolean fileExists(String path
) {
137 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
141 public Vector
<OCFile
> getFolderContent(OCFile f
) {
142 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
143 return getFolderContent(f
.getFileId());
146 return new Vector
<OCFile
>();
151 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
152 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
153 if (folder
!= null
) {
154 // TODO better implementation, filtering in the access to database instead of here
155 Vector
<OCFile
> tmp
= getFolderContent(folder
);
156 OCFile current
= null
;
157 for (int i
=0; i
<tmp
.size(); i
++) {
158 current
= tmp
.get(i
);
159 if (current
.isImage()) {
168 public boolean saveFile(OCFile file
) {
169 boolean overriden
= false
;
170 ContentValues cv
= new ContentValues();
171 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
173 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
174 file
.getModificationTimestampAtLastSyncForData()
176 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
177 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
178 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
179 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
180 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
181 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
182 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
183 if (!file
.isFolder())
184 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
185 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
186 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
187 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
188 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
189 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
190 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
191 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
192 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
193 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
194 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
196 boolean sameRemotePath
= fileExists(file
.getRemotePath());
197 if (sameRemotePath
||
198 fileExists(file
.getFileId()) ) { // for renamed files
200 OCFile oldFile
= null
;
201 if (sameRemotePath
) {
202 oldFile
= getFileByPath(file
.getRemotePath());
203 file
.setFileId(oldFile
.getFileId());
205 oldFile
= getFileById(file
.getFileId());
209 if (getContentResolver() != null
) {
210 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
211 ProviderTableMeta
._ID
+ "=?",
212 new String
[] { String
.valueOf(file
.getFileId()) });
215 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
216 cv
, ProviderTableMeta
._ID
+ "=?",
217 new String
[] { String
.valueOf(file
.getFileId()) });
218 } catch (RemoteException e
) {
220 "Fail to insert insert file to database "
225 Uri result_uri
= null
;
226 if (getContentResolver() != null
) {
227 result_uri
= getContentResolver().insert(
228 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
231 result_uri
= getContentProviderClient().insert(
232 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
233 } catch (RemoteException e
) {
235 "Fail to insert insert file to database "
239 if (result_uri
!= null
) {
240 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
242 file
.setFileId(new_id
);
246 // if (file.isFolder()) {
247 // updateFolderSize(file.getFileId());
249 // updateFolderSize(file.getParentId());
257 * Inserts or updates the list of files contained in a given folder.
259 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
260 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
264 * @param removeNotUpdated
266 public void saveFolder(
267 OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
270 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size()
271 + " children and " + filesToRemove
.size() + " files to remove");
273 ArrayList
<ContentProviderOperation
> operations
=
274 new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
276 // prepare operations to insert or update files to save in the given folder
277 for (OCFile file
: updatedFiles
) {
278 ContentValues cv
= new ContentValues();
279 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
281 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
282 file
.getModificationTimestampAtLastSyncForData()
284 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
285 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
286 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
287 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
288 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
289 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
290 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
291 if (!file
.isFolder()) {
292 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
294 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
295 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
296 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
297 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
298 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
299 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
300 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
301 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
302 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
303 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
305 boolean existsByPath
= fileExists(file
.getRemotePath());
306 if (existsByPath
|| fileExists(file
.getFileId())) {
307 // updating an existing file
308 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
310 withSelection( ProviderTableMeta
._ID
+ "=?",
311 new String
[] { String
.valueOf(file
.getFileId()) })
316 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
317 withValues(cv
).build());
321 // prepare operations to remove files in the given folder
322 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
323 ProviderTableMeta
.FILE_PATH
+ "=?";
324 String
[] whereArgs
= null
;
325 for (OCFile file
: filesToRemove
) {
326 if (file
.getParentId() == folder
.getFileId()) {
327 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
328 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
329 if (file
.isFolder()) {
330 operations
.add(ContentProviderOperation
.newDelete(
331 ContentUris
.withAppendedId(
332 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
334 ).withSelection(where
, whereArgs
).build());
337 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
338 if (localFolder
.exists()) {
339 removeLocalFolder(localFolder
);
342 operations
.add(ContentProviderOperation
.newDelete(
343 ContentUris
.withAppendedId(
344 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
346 ).withSelection(where
, whereArgs
).build());
349 String path
= file
.getStoragePath();
350 new File(file
.getStoragePath()).delete();
352 // Notify MediaScanner about removed file
353 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
354 intent
.setData(Uri
.fromFile(new File(path
)));
355 MainApp
.getAppContext().sendBroadcast(intent
);
361 // update metadata of folder
362 ContentValues cv
= new ContentValues();
363 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
365 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
366 folder
.getModificationTimestampAtLastSyncForData()
368 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
369 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
370 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
371 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
372 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
373 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
374 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
375 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
376 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
377 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
378 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
379 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
380 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
381 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
382 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
384 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
386 withSelection( ProviderTableMeta
._ID
+ "=?",
387 new String
[] { String
.valueOf(folder
.getFileId()) })
390 // apply operations in batch
391 ContentProviderResult
[] results
= null
;
392 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
394 if (getContentResolver() != null
) {
395 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
398 results
= getContentProviderClient().applyBatch(operations
);
401 } catch (OperationApplicationException e
) {
402 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
404 } catch (RemoteException e
) {
405 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
408 // update new id in file objects for insertions
409 if (results
!= null
) {
411 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
413 for (int i
=0; i
<results
.length
; i
++) {
414 if (filesIt
.hasNext()) {
415 file
= filesIt
.next();
419 if (results
[i
].uri
!= null
) {
420 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
421 //updatedFiles.get(i).setFileId(newId);
423 file
.setFileId(newId
);
429 //updateFolderSize(folder.getFileId());
438 // private void updateFolderSize(long id) {
439 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
440 // Log_OC.d(TAG, "Updating size of " + id);
441 // if (getContentResolver() != null) {
442 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
443 // new ContentValues(),
444 // won't be used, but cannot be null; crashes in KLP
445 // ProviderTableMeta._ID + "=?",
446 // new String[] { String.valueOf(id) });
449 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
450 // new ContentValues(),
451 // won't be used, but cannot be null; crashes in KLP
452 // ProviderTableMeta._ID + "=?",
453 // new String[] { String.valueOf(id) });
455 // } catch (RemoteException e) {
457 // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
461 // Log_OC.e(TAG, "not updating size for folder " + id);
466 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
467 boolean success
= true
;
469 if (file
.isFolder()) {
470 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
474 Uri file_uri
= ContentUris
.withAppendedId(
475 ProviderTableMeta
.CONTENT_URI_FILE
,
478 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
479 ProviderTableMeta
.FILE_PATH
+ "=?";
480 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
482 if (getContentProviderClient() != null
) {
484 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
485 } catch (RemoteException e
) {
489 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
491 success
&= (deleted
> 0);
493 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
494 success
= new File(file
.getStoragePath()).delete();
495 if (!removeDBData
&& success
) {
496 // maybe unnecessary, but should be checked TODO remove if unnecessary
497 file
.setStoragePath(null
);
507 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
508 boolean success
= true
;
509 if (folder
!= null
&& folder
.isFolder()) {
510 if (removeDBData
&& folder
.getFileId() != -1) {
511 success
= removeFolderInDb(folder
);
513 if (removeLocalContent
&& success
) {
514 success
= removeLocalFolder(folder
);
520 private boolean removeFolderInDb(OCFile folder
) {
521 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
522 folder
.getFileId()); // URI for recursive deletion
523 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
524 ProviderTableMeta
.FILE_PATH
+ "=?";
525 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
527 if (getContentProviderClient() != null
) {
529 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
530 } catch (RemoteException e
) {
534 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
539 private boolean removeLocalFolder(OCFile folder
) {
540 boolean success
= true
;
541 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
542 if (localFolder
.exists()) {
543 // stage 1: remove the local files already registered in the files database
544 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
546 for (OCFile file
: files
) {
547 if (file
.isFolder()) {
548 success
&= removeLocalFolder(file
);
551 String path
= file
.getStoragePath();
552 File localFile
= new File(file
.getStoragePath());
553 success
&= localFile
.delete();
555 file
.setStoragePath(null
);
558 // Notify MediaScanner about removed file
559 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
560 intent
.setData(Uri
.fromFile(new File(path
)));
561 MainApp
.getAppContext().sendBroadcast(intent
);
568 // stage 2: remove the folder itself and any local file inside out of sync;
569 // for instance, after clearing the app cache or reinstalling
570 success
&= removeLocalFolder(localFolder
);
575 private boolean removeLocalFolder(File localFolder
) {
576 boolean success
= true
;
577 File
[] localFiles
= localFolder
.listFiles();
578 if (localFiles
!= null
) {
579 for (File localFile
: localFiles
) {
580 if (localFile
.isDirectory()) {
581 success
&= removeLocalFolder(localFile
);
583 String path
= localFile
.getAbsolutePath();
584 success
&= localFile
.delete();
586 // Notify MediaScanner about removed file
587 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
588 intent
.setData(Uri
.fromFile(new File(path
)));
589 MainApp
.getAppContext().sendBroadcast(intent
);
593 success
&= localFolder
.delete();
598 * Updates database for a folder that was moved to a different location.
600 * TODO explore better (faster) implementations
601 * TODO throw exceptions up !
603 public void moveFolder(OCFile folder
, String newPath
) {
604 // TODO check newPath
606 if ( folder
!= null
&& folder
.isFolder() &&
607 folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())
609 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
611 if (getContentProviderClient() != null
) {
613 c
= getContentProviderClient().query (
614 ProviderTableMeta
.CONTENT_URI
,
616 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
617 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
618 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" },
619 ProviderTableMeta
.FILE_PATH
+ " ASC "
621 } catch (RemoteException e
) {
622 Log_OC
.e(TAG
, e
.getMessage());
625 c
= getContentResolver().query (
626 ProviderTableMeta
.CONTENT_URI
,
628 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
629 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
630 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" },
631 ProviderTableMeta
.FILE_PATH
+ " ASC "
635 /// 2. prepare a batch of update operations to change all the descendants
636 ArrayList
<ContentProviderOperation
> operations
=
637 new ArrayList
<ContentProviderOperation
>(c
.getCount());
638 int lengthOfOldPath
= folder
.getRemotePath().length();
639 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
640 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
641 if (c
.moveToFirst()) {
643 ContentValues cv
= new ContentValues(); // keep the constructor in the loop
644 OCFile child
= createFileInstance(c
);
646 ProviderTableMeta
.FILE_PATH
,
647 newPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
649 if ( child
.getStoragePath() != null
&&
650 child
.getStoragePath().startsWith(defaultSavePath
) ) {
652 ProviderTableMeta
.FILE_STORAGE_PATH
,
653 defaultSavePath
+ newPath
+
654 child
.getStoragePath().substring(lengthOfOldStoragePath
)
658 ContentProviderOperation
.
659 newUpdate(ProviderTableMeta
.CONTENT_URI
).
662 ProviderTableMeta
._ID
+ "=?",
663 new String
[] { String
.valueOf(child
.getFileId()) }
667 } while (c
.moveToNext());
671 /// 3. apply updates in batch
673 if (getContentResolver() != null
) {
674 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
677 getContentProviderClient().applyBatch(operations
);
680 } catch (OperationApplicationException e
) {
681 Log_OC
.e(TAG
, "Fail to update descendants of " +
682 folder
.getFileId() + " in database", e
);
684 } catch (RemoteException e
) {
685 Log_OC
.e(TAG
, "Fail to update desendants of " +
686 folder
.getFileId() + " in database", e
);
693 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
695 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
697 OCFile targetParent
= getFileByPath(targetParentPath
);
698 if (targetParent
== null
) {
702 /// 1. get all the descendants of the moved element in a single QUERY
704 if (getContentProviderClient() != null
) {
706 c
= getContentProviderClient().query(
707 ProviderTableMeta
.CONTENT_URI
,
709 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
710 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
713 file
.getRemotePath() + "%"
715 ProviderTableMeta
.FILE_PATH
+ " ASC "
717 } catch (RemoteException e
) {
718 Log_OC
.e(TAG
, e
.getMessage());
722 c
= getContentResolver().query(
723 ProviderTableMeta
.CONTENT_URI
,
725 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
726 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
729 file
.getRemotePath() + "%"
731 ProviderTableMeta
.FILE_PATH
+ " ASC "
735 /// 2. prepare a batch of update operations to change all the descendants
736 ArrayList
<ContentProviderOperation
> operations
=
737 new ArrayList
<ContentProviderOperation
>(c
.getCount());
738 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
739 if (c
.moveToFirst()) {
740 int lengthOfOldPath
= file
.getRemotePath().length();
741 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
743 ContentValues cv
= new ContentValues(); // keep construction in the loop
744 OCFile child
= createFileInstance(c
);
746 ProviderTableMeta
.FILE_PATH
,
747 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
749 if (child
.getStoragePath() != null
&&
750 child
.getStoragePath().startsWith(defaultSavePath
)) {
751 // update link to downloaded content - but local move is not done here!
753 ProviderTableMeta
.FILE_STORAGE_PATH
,
754 defaultSavePath
+ targetPath
+
755 child
.getStoragePath().substring(lengthOfOldStoragePath
)
758 if (child
.getRemotePath().equals(file
.getRemotePath())) {
760 ProviderTableMeta
.FILE_PARENT
,
761 targetParent
.getFileId()
765 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
768 ProviderTableMeta
._ID
+ "=?",
769 new String
[] { String
.valueOf(child
.getFileId()) }
773 } while (c
.moveToNext());
777 /// 3. apply updates in batch
779 if (getContentResolver() != null
) {
780 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
783 getContentProviderClient().applyBatch(operations
);
786 } catch (Exception e
) {
789 "Fail to update " + file
.getFileId() + " and descendants in database",
794 /// 4. move in local file system
795 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
796 File localFile
= new File(localPath
);
797 boolean renamed
= false
;
798 if (localFile
.exists()) {
799 File targetFile
= new File(defaultSavePath
+ targetPath
);
800 File targetFolder
= targetFile
.getParentFile();
801 if (!targetFolder
.exists()) {
802 targetFolder
.mkdirs();
804 renamed
= localFile
.renameTo(targetFile
);
806 Log_OC
.d(TAG
, "Local file RENAMED : " + renamed
);
808 // Notify MediaScanner about removed file
809 Intent intent1
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
810 intent1
.setData(Uri
.fromFile(new File(file
.getStoragePath())));
811 MainApp
.getAppContext().sendBroadcast(intent1
);
813 // Notify MediaScanner about new file/folder
814 Intent intent2
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
815 intent2
.setData(Uri
.fromFile(new File(defaultSavePath
+ targetPath
)));
816 MainApp
.getAppContext().sendBroadcast(intent2
);
818 Log_OC
.d(TAG
, "uri old: " + file
.getStoragePath());
819 Log_OC
.d(TAG
, "uri new: " + defaultSavePath
+ targetPath
);
825 private Vector
<OCFile
> getFolderContent(long parentId
) {
827 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
829 Uri req_uri
= Uri
.withAppendedPath(
830 ProviderTableMeta
.CONTENT_URI_DIR
,
831 String
.valueOf(parentId
));
834 if (getContentProviderClient() != null
) {
836 c
= getContentProviderClient().query(req_uri
, null
,
837 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
838 new String
[] { String
.valueOf(parentId
)}, null
);
839 } catch (RemoteException e
) {
840 Log_OC
.e(TAG
, e
.getMessage());
844 c
= getContentResolver().query(req_uri
, null
,
845 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
846 new String
[] { String
.valueOf(parentId
)}, null
);
849 if (c
.moveToFirst()) {
851 OCFile child
= createFileInstance(c
);
853 } while (c
.moveToNext());
858 Collections
.sort(ret
);
864 private OCFile
createRootDir() {
865 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
866 file
.setMimetype("DIR");
867 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
872 private boolean fileExists(String cmp_key
, String value
) {
874 if (getContentResolver() != null
) {
875 c
= getContentResolver()
876 .query(ProviderTableMeta
.CONTENT_URI
,
879 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
881 new String
[] { value
, mAccount
.name
}, null
);
884 c
= getContentProviderClient().query(
885 ProviderTableMeta
.CONTENT_URI
,
888 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
889 new String
[] { value
, mAccount
.name
}, null
);
890 } catch (RemoteException e
) {
892 "Couldn't determine file existance, assuming non existance: "
897 boolean retval
= c
.moveToFirst();
902 private Cursor
getCursorForValue(String key
, String value
) {
904 if (getContentResolver() != null
) {
905 c
= getContentResolver()
906 .query(ProviderTableMeta
.CONTENT_URI
,
909 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
911 new String
[] { value
, mAccount
.name
}, null
);
914 c
= getContentProviderClient().query(
915 ProviderTableMeta
.CONTENT_URI
,
917 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
918 + "=?", new String
[] { value
, mAccount
.name
},
920 } catch (RemoteException e
) {
921 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
929 private OCFile
createFileInstance(Cursor c
) {
932 file
= new OCFile(c
.getString(c
933 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
934 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
935 file
.setParentId(c
.getLong(c
936 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
937 file
.setMimetype(c
.getString(c
938 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
939 if (!file
.isFolder()) {
940 file
.setStoragePath(c
.getString(c
941 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
942 if (file
.getStoragePath() == null
) {
943 // try to find existing file and bind it with current account;
944 // with the current update of SynchronizeFolderOperation, this won't be
945 // necessary anymore after a full synchronization of the account
946 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
948 file
.setStoragePath(f
.getAbsolutePath());
949 file
.setLastSyncDateForData(f
.lastModified());
953 file
.setFileLength(c
.getLong(c
954 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
955 file
.setCreationTimestamp(c
.getLong(c
956 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
957 file
.setModificationTimestamp(c
.getLong(c
958 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
959 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
960 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
961 file
.setLastSyncDateForProperties(c
.getLong(c
962 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
963 file
.setLastSyncDateForData(c
.getLong(c
.
964 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
965 file
.setKeepInSync(c
.getInt(
966 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
967 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
968 file
.setShareByLink(c
.getInt(
969 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
970 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
971 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
972 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
973 file
.setNeedsUpdateThumbnail(c
.getInt(
974 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
981 * Returns if the file/folder is shared by link or not
982 * @param path Path of the file/folder
985 public boolean isShareByLink(String path
) {
986 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
988 if (c
.moveToFirst()) {
989 file
= createFileInstance(c
);
992 return file
.isShareByLink();
996 * Returns the public link of the file/folder
997 * @param path Path of the file/folder
1000 public String
getPublicLink(String path
) {
1001 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
1003 if (c
.moveToFirst()) {
1004 file
= createFileInstance(c
);
1007 return file
.getPublicLink();
1011 // Methods for Shares
1012 public boolean saveShare(OCShare share
) {
1013 boolean overriden
= false
;
1014 ContentValues cv
= new ContentValues();
1015 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1016 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1017 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1018 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1019 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1020 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1021 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1022 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1023 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1025 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1026 share
.getSharedWithDisplayName()
1028 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1029 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1030 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1031 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1033 if (shareExists(share
.getIdRemoteShared())) { // for renamed files
1036 if (getContentResolver() != null
) {
1037 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
1038 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1039 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
1042 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
1043 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1044 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
1045 } catch (RemoteException e
) {
1047 "Fail to insert insert file to database "
1052 Uri result_uri
= null
;
1053 if (getContentResolver() != null
) {
1054 result_uri
= getContentResolver().insert(
1055 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1058 result_uri
= getContentProviderClient().insert(
1059 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1060 } catch (RemoteException e
) {
1062 "Fail to insert insert file to database "
1066 if (result_uri
!= null
) {
1067 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
1069 share
.setId(new_id
);
1077 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
1079 if (getContentResolver() != null
) {
1080 c
= getContentResolver().query(
1081 ProviderTableMeta
.CONTENT_URI_SHARE
,
1083 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1084 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1085 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1086 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1090 c
= getContentProviderClient().query(
1091 ProviderTableMeta
.CONTENT_URI_SHARE
,
1093 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1094 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1095 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1096 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1099 } catch (RemoteException e
) {
1100 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1104 OCShare share
= null
;
1105 if (c
.moveToFirst()) {
1106 share
= createShareInstance(c
);
1112 private OCShare
createShareInstance(Cursor c
) {
1113 OCShare share
= null
;
1115 share
= new OCShare(c
.getString(c
1116 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1117 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1118 share
.setFileSource(c
.getLong(c
1119 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1120 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1121 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1122 share
.setPermissions(c
.getInt(c
1123 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1124 share
.setSharedDate(c
.getLong(c
1125 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1126 share
.setExpirationDate(c
.getLong(c
1127 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1128 share
.setToken(c
.getString(c
1129 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1130 share
.setSharedWithDisplayName(c
.getString(c
1131 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1132 share
.setIsFolder(c
.getInt(
1133 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1134 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1135 share
.setIdRemoteShared(
1136 c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
))
1143 private boolean shareExists(String cmp_key
, String value
) {
1145 if (getContentResolver() != null
) {
1146 c
= getContentResolver()
1147 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1150 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1152 new String
[] { value
, mAccount
.name
}, null
);
1155 c
= getContentProviderClient().query(
1156 ProviderTableMeta
.CONTENT_URI_SHARE
,
1159 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1160 new String
[] { value
, mAccount
.name
}, null
);
1161 } catch (RemoteException e
) {
1163 "Couldn't determine file existance, assuming non existance: "
1168 boolean retval
= c
.moveToFirst();
1173 private boolean shareExists(long remoteId
) {
1174 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1177 private void cleanSharedFiles() {
1178 ContentValues cv
= new ContentValues();
1179 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1180 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1181 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1182 String
[] whereArgs
= new String
[]{mAccount
.name
};
1184 if (getContentResolver() != null
) {
1185 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1189 getContentProviderClient().update(
1190 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1193 } catch (RemoteException e
) {
1194 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1199 private void cleanSharedFilesInFolder(OCFile folder
) {
1200 ContentValues cv
= new ContentValues();
1201 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1202 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1203 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1204 ProviderTableMeta
.FILE_PARENT
+ "=?";
1205 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1207 if (getContentResolver() != null
) {
1208 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1212 getContentProviderClient().update(
1213 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1216 } catch (RemoteException e
) {
1217 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1222 private void cleanShares() {
1223 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1224 String
[] whereArgs
= new String
[]{mAccount
.name
};
1226 if (getContentResolver() != null
) {
1227 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1231 getContentProviderClient().delete(
1232 ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
1235 } catch (RemoteException e
) {
1236 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1241 public void saveShares(Collection
<OCShare
> shares
) {
1243 if (shares
!= null
) {
1244 ArrayList
<ContentProviderOperation
> operations
=
1245 new ArrayList
<ContentProviderOperation
>(shares
.size());
1247 // prepare operations to insert or update files to save in the given folder
1248 for (OCShare share
: shares
) {
1249 ContentValues cv
= new ContentValues();
1250 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1251 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1252 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1253 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1254 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1255 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1256 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1257 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1258 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1260 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1261 share
.getSharedWithDisplayName()
1263 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1264 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1265 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1266 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1268 if (shareExists(share
.getIdRemoteShared())) {
1269 // updating an existing file
1271 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1274 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1275 new String
[] { String
.valueOf(share
.getIdRemoteShared()) }
1281 // adding a new file
1283 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1290 // apply operations in batch
1291 if (operations
.size() > 0) {
1292 @SuppressWarnings("unused")
1293 ContentProviderResult
[] results
= null
;
1294 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1295 " operations to FileContentProvider");
1297 if (getContentResolver() != null
) {
1298 results
= getContentResolver().applyBatch(
1299 MainApp
.getAuthority(), operations
1303 results
= getContentProviderClient().applyBatch(operations
);
1306 } catch (OperationApplicationException e
) {
1307 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1309 } catch (RemoteException e
) {
1310 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1317 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1320 if (sharedFiles
!= null
) {
1321 ArrayList
<ContentProviderOperation
> operations
=
1322 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1324 // prepare operations to insert or update files to save in the given folder
1325 for (OCFile file
: sharedFiles
) {
1326 ContentValues cv
= new ContentValues();
1327 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1329 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1330 file
.getModificationTimestampAtLastSyncForData()
1332 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1333 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1334 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1335 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1336 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1337 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1338 if (!file
.isFolder()) {
1339 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1341 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1342 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1344 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1345 file
.getLastSyncDateForData()
1347 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1348 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1349 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1350 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1351 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1352 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1354 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1355 file
.needsUpdateThumbnail() ?
1 : 0
1358 boolean existsByPath
= fileExists(file
.getRemotePath());
1359 if (existsByPath
|| fileExists(file
.getFileId())) {
1360 // updating an existing file
1362 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1365 ProviderTableMeta
._ID
+ "=?",
1366 new String
[] { String
.valueOf(file
.getFileId()) }
1371 // adding a new file
1373 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1380 // apply operations in batch
1381 if (operations
.size() > 0) {
1382 @SuppressWarnings("unused")
1383 ContentProviderResult
[] results
= null
;
1384 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1385 " operations to FileContentProvider");
1387 if (getContentResolver() != null
) {
1388 results
= getContentResolver().applyBatch(
1389 MainApp
.getAuthority(), operations
1393 results
= getContentProviderClient().applyBatch(operations
);
1396 } catch (OperationApplicationException e
) {
1397 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1399 } catch (RemoteException e
) {
1400 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1407 public void removeShare(OCShare share
){
1408 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1409 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1410 ProviderTableMeta
.FILE_PATH
+ "=?";
1411 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1412 if (getContentProviderClient() != null
) {
1414 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1415 } catch (RemoteException e
) {
1416 e
.printStackTrace();
1419 getContentResolver().delete(share_uri
, where
, whereArgs
);
1423 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1426 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1428 for (OCShare share
: shares
) {
1430 String path
= share
.getPath();
1431 if (share
.isFolder()) {
1432 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1435 // Update OCFile with data from share: ShareByLink and publicLink
1436 OCFile file
= getFileByPath(path
);
1438 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1439 file
.setShareByLink(true
);
1440 sharedFiles
.add(file
);
1445 updateSharedFiles(sharedFiles
);
1449 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1450 cleanSharedFilesInFolder(folder
);
1451 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1452 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1454 if (shares
!= null
) {
1455 // prepare operations to insert or update files to save in the given folder
1456 for (OCShare share
: shares
) {
1457 ContentValues cv
= new ContentValues();
1458 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1459 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1460 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1461 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1462 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1463 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1464 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1465 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1466 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1468 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1469 share
.getSharedWithDisplayName()
1471 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1472 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1473 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1474 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1477 if (shareExists(share.getIdRemoteShared())) {
1478 // updating an existing share resource
1480 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1482 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1483 new String[] { String.valueOf(share.getIdRemoteShared()) })
1488 // adding a new share resource
1490 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1498 // apply operations in batch
1499 if (operations
.size() > 0) {
1500 @SuppressWarnings("unused")
1501 ContentProviderResult
[] results
= null
;
1502 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1504 if (getContentResolver() != null
) {
1505 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1508 results
= getContentProviderClient().applyBatch(operations
);
1511 } catch (OperationApplicationException e
) {
1512 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1514 } catch (RemoteException e
) {
1515 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1522 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1523 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
1525 if (folder
!= null
) {
1526 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1527 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1528 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1530 Vector
<OCFile
> files
= getFolderContent(folder
);
1532 for (OCFile file
: files
) {
1533 whereArgs
[0] = file
.getRemotePath();
1534 preparedOperations
.add(
1535 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1536 withSelection(where
, whereArgs
).
1541 return preparedOperations
;
1544 if (operations.size() > 0) {
1546 if (getContentResolver() != null) {
1547 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1550 getContentProviderClient().applyBatch(operations);
1553 } catch (OperationApplicationException e) {
1554 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1556 } catch (RemoteException e) {
1557 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1563 if (getContentResolver() != null) {
1565 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1570 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1574 } catch (RemoteException e) {
1575 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());
1582 public void triggerMediaScan(String path
) {
1583 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
1584 intent
.setData(Uri
.fromFile(new File(path
)));
1585 MainApp
.getAppContext().sendBroadcast(intent
);