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 new File(file
.getStoragePath()).delete();
355 // update metadata of folder
356 ContentValues cv
= new ContentValues();
357 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
359 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
360 folder
.getModificationTimestampAtLastSyncForData()
362 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
363 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
364 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
365 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
366 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
367 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
368 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
369 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
370 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
371 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
372 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
373 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
374 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
375 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
376 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
378 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
380 withSelection( ProviderTableMeta
._ID
+ "=?",
381 new String
[] { String
.valueOf(folder
.getFileId()) })
384 // apply operations in batch
385 ContentProviderResult
[] results
= null
;
386 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
388 if (getContentResolver() != null
) {
389 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
392 results
= getContentProviderClient().applyBatch(operations
);
395 } catch (OperationApplicationException e
) {
396 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
398 } catch (RemoteException e
) {
399 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
402 // update new id in file objects for insertions
403 if (results
!= null
) {
405 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
407 for (int i
=0; i
<results
.length
; i
++) {
408 if (filesIt
.hasNext()) {
409 file
= filesIt
.next();
413 if (results
[i
].uri
!= null
) {
414 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
415 //updatedFiles.get(i).setFileId(newId);
417 file
.setFileId(newId
);
423 //updateFolderSize(folder.getFileId());
432 // private void updateFolderSize(long id) {
433 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
434 // Log_OC.d(TAG, "Updating size of " + id);
435 // if (getContentResolver() != null) {
436 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
437 // new ContentValues(),
438 // won't be used, but cannot be null; crashes in KLP
439 // ProviderTableMeta._ID + "=?",
440 // new String[] { String.valueOf(id) });
443 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
444 // new ContentValues(),
445 // won't be used, but cannot be null; crashes in KLP
446 // ProviderTableMeta._ID + "=?",
447 // new String[] { String.valueOf(id) });
449 // } catch (RemoteException e) {
451 // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
455 // Log_OC.e(TAG, "not updating size for folder " + id);
460 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
461 boolean success
= true
;
463 if (file
.isFolder()) {
464 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
468 Uri file_uri
= ContentUris
.withAppendedId(
469 ProviderTableMeta
.CONTENT_URI_FILE
,
472 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
473 ProviderTableMeta
.FILE_PATH
+ "=?";
474 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
476 if (getContentProviderClient() != null
) {
478 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
479 } catch (RemoteException e
) {
483 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
485 success
&= (deleted
> 0);
487 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
488 success
= new File(file
.getStoragePath()).delete();
489 if (!removeDBData
&& success
) {
490 // maybe unnecessary, but should be checked TODO remove if unnecessary
491 file
.setStoragePath(null
);
501 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
502 boolean success
= true
;
503 if (folder
!= null
&& folder
.isFolder()) {
504 if (removeDBData
&& folder
.getFileId() != -1) {
505 success
= removeFolderInDb(folder
);
507 if (removeLocalContent
&& success
) {
508 success
= removeLocalFolder(folder
);
514 private boolean removeFolderInDb(OCFile folder
) {
515 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
516 folder
.getFileId()); // URI for recursive deletion
517 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
518 ProviderTableMeta
.FILE_PATH
+ "=?";
519 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
521 if (getContentProviderClient() != null
) {
523 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
524 } catch (RemoteException e
) {
528 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
533 private boolean removeLocalFolder(OCFile folder
) {
534 boolean success
= true
;
535 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
536 if (localFolder
.exists()) {
537 // stage 1: remove the local files already registered in the files database
538 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
540 for (OCFile file
: files
) {
541 if (file
.isFolder()) {
542 success
&= removeLocalFolder(file
);
545 File localFile
= new File(file
.getStoragePath());
546 success
&= localFile
.delete();
548 file
.setStoragePath(null
);
556 // stage 2: remove the folder itself and any local file inside out of sync;
557 // for instance, after clearing the app cache or reinstalling
558 success
&= removeLocalFolder(localFolder
);
563 private boolean removeLocalFolder(File localFolder
) {
564 boolean success
= true
;
565 File
[] localFiles
= localFolder
.listFiles();
566 if (localFiles
!= null
) {
567 for (File localFile
: localFiles
) {
568 if (localFile
.isDirectory()) {
569 success
&= removeLocalFolder(localFile
);
571 success
&= localFile
.delete();
575 success
&= localFolder
.delete();
580 * Updates database for a folder that was moved to a different location.
582 * TODO explore better (faster) implementations
583 * TODO throw exceptions up !
585 public void moveFolder(OCFile folder
, String newPath
) {
586 // TODO check newPath
588 if ( folder
!= null
&& folder
.isFolder() &&
589 folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())
591 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
593 if (getContentProviderClient() != null
) {
595 c
= getContentProviderClient().query (
596 ProviderTableMeta
.CONTENT_URI
,
598 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
599 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
600 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" },
601 ProviderTableMeta
.FILE_PATH
+ " ASC "
603 } catch (RemoteException e
) {
604 Log_OC
.e(TAG
, e
.getMessage());
607 c
= getContentResolver().query (
608 ProviderTableMeta
.CONTENT_URI
,
610 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
611 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
612 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" },
613 ProviderTableMeta
.FILE_PATH
+ " ASC "
617 /// 2. prepare a batch of update operations to change all the descendants
618 ArrayList
<ContentProviderOperation
> operations
=
619 new ArrayList
<ContentProviderOperation
>(c
.getCount());
620 int lengthOfOldPath
= folder
.getRemotePath().length();
621 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
622 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
623 if (c
.moveToFirst()) {
625 ContentValues cv
= new ContentValues(); // keep the constructor in the loop
626 OCFile child
= createFileInstance(c
);
628 ProviderTableMeta
.FILE_PATH
,
629 newPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
631 if ( child
.getStoragePath() != null
&&
632 child
.getStoragePath().startsWith(defaultSavePath
) ) {
634 ProviderTableMeta
.FILE_STORAGE_PATH
,
635 defaultSavePath
+ newPath
+
636 child
.getStoragePath().substring(lengthOfOldStoragePath
)
640 ContentProviderOperation
.
641 newUpdate(ProviderTableMeta
.CONTENT_URI
).
644 ProviderTableMeta
._ID
+ "=?",
645 new String
[] { String
.valueOf(child
.getFileId()) }
649 } while (c
.moveToNext());
653 /// 3. apply updates in batch
655 if (getContentResolver() != null
) {
656 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
659 getContentProviderClient().applyBatch(operations
);
662 } catch (OperationApplicationException e
) {
663 Log_OC
.e(TAG
, "Fail to update descendants of " +
664 folder
.getFileId() + " in database", e
);
666 } catch (RemoteException e
) {
667 Log_OC
.e(TAG
, "Fail to update desendants of " +
668 folder
.getFileId() + " in database", e
);
675 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
677 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
679 OCFile targetParent
= getFileByPath(targetParentPath
);
680 if (targetParent
== null
) {
684 /// 1. get all the descendants of the moved element in a single QUERY
686 if (getContentProviderClient() != null
) {
688 c
= getContentProviderClient().query(
689 ProviderTableMeta
.CONTENT_URI
,
691 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
692 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
695 file
.getRemotePath() + "%"
697 ProviderTableMeta
.FILE_PATH
+ " ASC "
699 } catch (RemoteException e
) {
700 Log_OC
.e(TAG
, e
.getMessage());
704 c
= getContentResolver().query(
705 ProviderTableMeta
.CONTENT_URI
,
707 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
708 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
711 file
.getRemotePath() + "%"
713 ProviderTableMeta
.FILE_PATH
+ " ASC "
717 /// 2. prepare a batch of update operations to change all the descendants
718 ArrayList
<ContentProviderOperation
> operations
=
719 new ArrayList
<ContentProviderOperation
>(c
.getCount());
720 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
721 if (c
.moveToFirst()) {
722 int lengthOfOldPath
= file
.getRemotePath().length();
723 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
725 ContentValues cv
= new ContentValues(); // keep construction in the loop
726 OCFile child
= createFileInstance(c
);
728 ProviderTableMeta
.FILE_PATH
,
729 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
731 if (child
.getStoragePath() != null
&&
732 child
.getStoragePath().startsWith(defaultSavePath
)) {
733 // update link to downloaded content - but local move is not done here!
735 ProviderTableMeta
.FILE_STORAGE_PATH
,
736 defaultSavePath
+ targetPath
+
737 child
.getStoragePath().substring(lengthOfOldStoragePath
)
740 if (child
.getRemotePath().equals(file
.getRemotePath())) {
742 ProviderTableMeta
.FILE_PARENT
,
743 targetParent
.getFileId()
747 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
750 ProviderTableMeta
._ID
+ "=?",
751 new String
[] { String
.valueOf(child
.getFileId()) }
755 } while (c
.moveToNext());
759 /// 3. apply updates in batch
761 if (getContentResolver() != null
) {
762 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
765 getContentProviderClient().applyBatch(operations
);
768 } catch (Exception e
) {
771 "Fail to update " + file
.getFileId() + " and descendants in database",
776 /// 4. move in local file system
777 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
778 File localFile
= new File(localPath
);
779 boolean renamed
= false
;
780 if (localFile
.exists()) {
781 File targetFile
= new File(defaultSavePath
+ targetPath
);
782 File targetFolder
= targetFile
.getParentFile();
783 if (!targetFolder
.exists()) {
784 targetFolder
.mkdirs();
786 renamed
= localFile
.renameTo(targetFile
);
788 Log_OC
.d(TAG
, "Local file RENAMED : " + renamed
);
790 // Notify MediaScanner about removed file
791 Intent intent1
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
792 intent1
.setData(Uri
.fromFile(new File(file
.getStoragePath())));
793 MainApp
.getAppContext().sendBroadcast(intent1
);
795 // Notify MediaScanner about new file/folder
796 Intent intent2
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
797 intent2
.setData(Uri
.fromFile(new File(defaultSavePath
+ targetPath
)));
798 MainApp
.getAppContext().sendBroadcast(intent2
);
800 Log_OC
.d(TAG
, "uri old: " + file
.getStoragePath());
801 Log_OC
.d(TAG
, "uri new: " + defaultSavePath
+ targetPath
);
807 private Vector
<OCFile
> getFolderContent(long parentId
) {
809 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
811 Uri req_uri
= Uri
.withAppendedPath(
812 ProviderTableMeta
.CONTENT_URI_DIR
,
813 String
.valueOf(parentId
));
816 if (getContentProviderClient() != null
) {
818 c
= getContentProviderClient().query(req_uri
, null
,
819 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
820 new String
[] { String
.valueOf(parentId
)}, null
);
821 } catch (RemoteException e
) {
822 Log_OC
.e(TAG
, e
.getMessage());
826 c
= getContentResolver().query(req_uri
, null
,
827 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
828 new String
[] { String
.valueOf(parentId
)}, null
);
831 if (c
.moveToFirst()) {
833 OCFile child
= createFileInstance(c
);
835 } while (c
.moveToNext());
840 Collections
.sort(ret
);
846 private OCFile
createRootDir() {
847 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
848 file
.setMimetype("DIR");
849 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
854 private boolean fileExists(String cmp_key
, String value
) {
856 if (getContentResolver() != null
) {
857 c
= getContentResolver()
858 .query(ProviderTableMeta
.CONTENT_URI
,
861 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
863 new String
[] { value
, mAccount
.name
}, null
);
866 c
= getContentProviderClient().query(
867 ProviderTableMeta
.CONTENT_URI
,
870 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
871 new String
[] { value
, mAccount
.name
}, null
);
872 } catch (RemoteException e
) {
874 "Couldn't determine file existance, assuming non existance: "
879 boolean retval
= c
.moveToFirst();
884 private Cursor
getCursorForValue(String key
, String value
) {
886 if (getContentResolver() != null
) {
887 c
= getContentResolver()
888 .query(ProviderTableMeta
.CONTENT_URI
,
891 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
893 new String
[] { value
, mAccount
.name
}, null
);
896 c
= getContentProviderClient().query(
897 ProviderTableMeta
.CONTENT_URI
,
899 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
900 + "=?", new String
[] { value
, mAccount
.name
},
902 } catch (RemoteException e
) {
903 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
911 private OCFile
createFileInstance(Cursor c
) {
914 file
= new OCFile(c
.getString(c
915 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
916 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
917 file
.setParentId(c
.getLong(c
918 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
919 file
.setMimetype(c
.getString(c
920 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
921 if (!file
.isFolder()) {
922 file
.setStoragePath(c
.getString(c
923 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
924 if (file
.getStoragePath() == null
) {
925 // try to find existing file and bind it with current account;
926 // with the current update of SynchronizeFolderOperation, this won't be
927 // necessary anymore after a full synchronization of the account
928 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
930 file
.setStoragePath(f
.getAbsolutePath());
931 file
.setLastSyncDateForData(f
.lastModified());
935 file
.setFileLength(c
.getLong(c
936 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
937 file
.setCreationTimestamp(c
.getLong(c
938 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
939 file
.setModificationTimestamp(c
.getLong(c
940 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
941 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
942 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
943 file
.setLastSyncDateForProperties(c
.getLong(c
944 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
945 file
.setLastSyncDateForData(c
.getLong(c
.
946 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
947 file
.setKeepInSync(c
.getInt(
948 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
949 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
950 file
.setShareByLink(c
.getInt(
951 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
952 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
953 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
954 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
955 file
.setNeedsUpdateThumbnail(c
.getInt(
956 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
963 * Returns if the file/folder is shared by link or not
964 * @param path Path of the file/folder
967 public boolean isShareByLink(String path
) {
968 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
970 if (c
.moveToFirst()) {
971 file
= createFileInstance(c
);
974 return file
.isShareByLink();
978 * Returns the public link of the file/folder
979 * @param path Path of the file/folder
982 public String
getPublicLink(String path
) {
983 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
985 if (c
.moveToFirst()) {
986 file
= createFileInstance(c
);
989 return file
.getPublicLink();
993 // Methods for Shares
994 public boolean saveShare(OCShare share
) {
995 boolean overriden
= false
;
996 ContentValues cv
= new ContentValues();
997 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
998 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
999 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1000 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1001 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1002 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1003 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1004 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1005 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1007 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1008 share
.getSharedWithDisplayName()
1010 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1011 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1012 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1013 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1015 if (shareExists(share
.getIdRemoteShared())) { // for renamed files
1018 if (getContentResolver() != null
) {
1019 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
1020 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1021 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
1024 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
1025 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1026 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
1027 } catch (RemoteException e
) {
1029 "Fail to insert insert file to database "
1034 Uri result_uri
= null
;
1035 if (getContentResolver() != null
) {
1036 result_uri
= getContentResolver().insert(
1037 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1040 result_uri
= getContentProviderClient().insert(
1041 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1042 } catch (RemoteException e
) {
1044 "Fail to insert insert file to database "
1048 if (result_uri
!= null
) {
1049 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
1051 share
.setId(new_id
);
1059 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
1061 if (getContentResolver() != null
) {
1062 c
= getContentResolver().query(
1063 ProviderTableMeta
.CONTENT_URI_SHARE
,
1065 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1066 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1067 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1068 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1072 c
= getContentProviderClient().query(
1073 ProviderTableMeta
.CONTENT_URI_SHARE
,
1075 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1076 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1077 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1078 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1081 } catch (RemoteException e
) {
1082 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1086 OCShare share
= null
;
1087 if (c
.moveToFirst()) {
1088 share
= createShareInstance(c
);
1094 private OCShare
createShareInstance(Cursor c
) {
1095 OCShare share
= null
;
1097 share
= new OCShare(c
.getString(c
1098 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1099 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1100 share
.setFileSource(c
.getLong(c
1101 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1102 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1103 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1104 share
.setPermissions(c
.getInt(c
1105 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1106 share
.setSharedDate(c
.getLong(c
1107 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1108 share
.setExpirationDate(c
.getLong(c
1109 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1110 share
.setToken(c
.getString(c
1111 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1112 share
.setSharedWithDisplayName(c
.getString(c
1113 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1114 share
.setIsFolder(c
.getInt(
1115 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1116 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1117 share
.setIdRemoteShared(
1118 c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
))
1125 private boolean shareExists(String cmp_key
, String value
) {
1127 if (getContentResolver() != null
) {
1128 c
= getContentResolver()
1129 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1132 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1134 new String
[] { value
, mAccount
.name
}, null
);
1137 c
= getContentProviderClient().query(
1138 ProviderTableMeta
.CONTENT_URI_SHARE
,
1141 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1142 new String
[] { value
, mAccount
.name
}, null
);
1143 } catch (RemoteException e
) {
1145 "Couldn't determine file existance, assuming non existance: "
1150 boolean retval
= c
.moveToFirst();
1155 private boolean shareExists(long remoteId
) {
1156 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1159 private void cleanSharedFiles() {
1160 ContentValues cv
= new ContentValues();
1161 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1162 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1163 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1164 String
[] whereArgs
= new String
[]{mAccount
.name
};
1166 if (getContentResolver() != null
) {
1167 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1171 getContentProviderClient().update(
1172 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1175 } catch (RemoteException e
) {
1176 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1181 private void cleanSharedFilesInFolder(OCFile folder
) {
1182 ContentValues cv
= new ContentValues();
1183 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1184 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1185 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1186 ProviderTableMeta
.FILE_PARENT
+ "=?";
1187 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1189 if (getContentResolver() != null
) {
1190 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1194 getContentProviderClient().update(
1195 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1198 } catch (RemoteException e
) {
1199 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1204 private void cleanShares() {
1205 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1206 String
[] whereArgs
= new String
[]{mAccount
.name
};
1208 if (getContentResolver() != null
) {
1209 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1213 getContentProviderClient().delete(
1214 ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
1217 } catch (RemoteException e
) {
1218 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1223 public void saveShares(Collection
<OCShare
> shares
) {
1225 if (shares
!= null
) {
1226 ArrayList
<ContentProviderOperation
> operations
=
1227 new ArrayList
<ContentProviderOperation
>(shares
.size());
1229 // prepare operations to insert or update files to save in the given folder
1230 for (OCShare share
: shares
) {
1231 ContentValues cv
= new ContentValues();
1232 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1233 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1234 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1235 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1236 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1237 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1238 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1239 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1240 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1242 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1243 share
.getSharedWithDisplayName()
1245 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1246 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1247 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1248 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1250 if (shareExists(share
.getIdRemoteShared())) {
1251 // updating an existing file
1253 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1256 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1257 new String
[] { String
.valueOf(share
.getIdRemoteShared()) }
1263 // adding a new file
1265 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1272 // apply operations in batch
1273 if (operations
.size() > 0) {
1274 @SuppressWarnings("unused")
1275 ContentProviderResult
[] results
= null
;
1276 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1277 " operations to FileContentProvider");
1279 if (getContentResolver() != null
) {
1280 results
= getContentResolver().applyBatch(
1281 MainApp
.getAuthority(), operations
1285 results
= getContentProviderClient().applyBatch(operations
);
1288 } catch (OperationApplicationException e
) {
1289 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1291 } catch (RemoteException e
) {
1292 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1299 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1302 if (sharedFiles
!= null
) {
1303 ArrayList
<ContentProviderOperation
> operations
=
1304 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1306 // prepare operations to insert or update files to save in the given folder
1307 for (OCFile file
: sharedFiles
) {
1308 ContentValues cv
= new ContentValues();
1309 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1311 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1312 file
.getModificationTimestampAtLastSyncForData()
1314 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1315 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1316 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1317 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1318 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1319 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1320 if (!file
.isFolder()) {
1321 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1323 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1324 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1326 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1327 file
.getLastSyncDateForData()
1329 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1330 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1331 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1332 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1333 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1334 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1336 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1337 file
.needsUpdateThumbnail() ?
1 : 0
1340 boolean existsByPath
= fileExists(file
.getRemotePath());
1341 if (existsByPath
|| fileExists(file
.getFileId())) {
1342 // updating an existing file
1344 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1347 ProviderTableMeta
._ID
+ "=?",
1348 new String
[] { String
.valueOf(file
.getFileId()) }
1353 // adding a new file
1355 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1362 // apply operations in batch
1363 if (operations
.size() > 0) {
1364 @SuppressWarnings("unused")
1365 ContentProviderResult
[] results
= null
;
1366 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1367 " operations to FileContentProvider");
1369 if (getContentResolver() != null
) {
1370 results
= getContentResolver().applyBatch(
1371 MainApp
.getAuthority(), operations
1375 results
= getContentProviderClient().applyBatch(operations
);
1378 } catch (OperationApplicationException e
) {
1379 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1381 } catch (RemoteException e
) {
1382 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1389 public void removeShare(OCShare share
){
1390 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1391 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1392 ProviderTableMeta
.FILE_PATH
+ "=?";
1393 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1394 if (getContentProviderClient() != null
) {
1396 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1397 } catch (RemoteException e
) {
1398 e
.printStackTrace();
1401 getContentResolver().delete(share_uri
, where
, whereArgs
);
1405 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1408 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1410 for (OCShare share
: shares
) {
1412 String path
= share
.getPath();
1413 if (share
.isFolder()) {
1414 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1417 // Update OCFile with data from share: ShareByLink �and publicLink?
1418 OCFile file
= getFileByPath(path
);
1420 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1421 file
.setShareByLink(true
);
1422 sharedFiles
.add(file
);
1427 updateSharedFiles(sharedFiles
);
1431 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1432 cleanSharedFilesInFolder(folder
);
1433 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1434 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1436 if (shares
!= null
) {
1437 // prepare operations to insert or update files to save in the given folder
1438 for (OCShare share
: shares
) {
1439 ContentValues cv
= new ContentValues();
1440 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1441 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1442 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1443 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1444 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1445 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1446 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1447 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1448 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1450 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1451 share
.getSharedWithDisplayName()
1453 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1454 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1455 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1456 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1459 if (shareExists(share.getIdRemoteShared())) {
1460 // updating an existing share resource
1462 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1464 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1465 new String[] { String.valueOf(share.getIdRemoteShared()) })
1470 // adding a new share resource
1472 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1480 // apply operations in batch
1481 if (operations
.size() > 0) {
1482 @SuppressWarnings("unused")
1483 ContentProviderResult
[] results
= null
;
1484 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1486 if (getContentResolver() != null
) {
1487 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1490 results
= getContentProviderClient().applyBatch(operations
);
1493 } catch (OperationApplicationException e
) {
1494 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1496 } catch (RemoteException e
) {
1497 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1504 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1505 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
1507 if (folder
!= null
) {
1508 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1509 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1510 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1512 Vector
<OCFile
> files
= getFolderContent(folder
);
1514 for (OCFile file
: files
) {
1515 whereArgs
[0] = file
.getRemotePath();
1516 preparedOperations
.add(
1517 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1518 withSelection(where
, whereArgs
).
1523 return preparedOperations
;
1526 if (operations.size() > 0) {
1528 if (getContentResolver() != null) {
1529 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1532 getContentProviderClient().applyBatch(operations);
1535 } catch (OperationApplicationException e) {
1536 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1538 } catch (RemoteException e) {
1539 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1545 if (getContentResolver() != null) {
1547 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1552 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1556 } catch (RemoteException e) {
1557 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());