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
;
37 import android
.accounts
.Account
;
38 import android
.content
.ContentProviderClient
;
39 import android
.content
.ContentProviderOperation
;
40 import android
.content
.ContentProviderResult
;
41 import android
.content
.ContentResolver
;
42 import android
.content
.ContentUris
;
43 import android
.content
.ContentValues
;
44 import android
.content
.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
);
795 private Vector
<OCFile
> getFolderContent(long parentId
) {
797 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
799 Uri req_uri
= Uri
.withAppendedPath(
800 ProviderTableMeta
.CONTENT_URI_DIR
,
801 String
.valueOf(parentId
));
804 if (getContentProviderClient() != null
) {
806 c
= getContentProviderClient().query(req_uri
, null
,
807 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
808 new String
[] { String
.valueOf(parentId
)}, null
);
809 } catch (RemoteException e
) {
810 Log_OC
.e(TAG
, e
.getMessage());
814 c
= getContentResolver().query(req_uri
, null
,
815 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
816 new String
[] { String
.valueOf(parentId
)}, null
);
819 if (c
.moveToFirst()) {
821 OCFile child
= createFileInstance(c
);
823 } while (c
.moveToNext());
828 Collections
.sort(ret
);
834 private OCFile
createRootDir() {
835 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
836 file
.setMimetype("DIR");
837 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
842 private boolean fileExists(String cmp_key
, String value
) {
844 if (getContentResolver() != null
) {
845 c
= getContentResolver()
846 .query(ProviderTableMeta
.CONTENT_URI
,
849 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
851 new String
[] { value
, mAccount
.name
}, null
);
854 c
= getContentProviderClient().query(
855 ProviderTableMeta
.CONTENT_URI
,
858 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
859 new String
[] { value
, mAccount
.name
}, null
);
860 } catch (RemoteException e
) {
862 "Couldn't determine file existance, assuming non existance: "
867 boolean retval
= c
.moveToFirst();
872 private Cursor
getCursorForValue(String 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
,
887 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
888 + "=?", new String
[] { value
, mAccount
.name
},
890 } catch (RemoteException e
) {
891 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
899 private OCFile
createFileInstance(Cursor c
) {
902 file
= new OCFile(c
.getString(c
903 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
904 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
905 file
.setParentId(c
.getLong(c
906 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
907 file
.setMimetype(c
.getString(c
908 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
909 if (!file
.isFolder()) {
910 file
.setStoragePath(c
.getString(c
911 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
912 if (file
.getStoragePath() == null
) {
913 // try to find existing file and bind it with current account;
914 // with the current update of SynchronizeFolderOperation, this won't be
915 // necessary anymore after a full synchronization of the account
916 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
918 file
.setStoragePath(f
.getAbsolutePath());
919 file
.setLastSyncDateForData(f
.lastModified());
923 file
.setFileLength(c
.getLong(c
924 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
925 file
.setCreationTimestamp(c
.getLong(c
926 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
927 file
.setModificationTimestamp(c
.getLong(c
928 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
929 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
930 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
931 file
.setLastSyncDateForProperties(c
.getLong(c
932 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
933 file
.setLastSyncDateForData(c
.getLong(c
.
934 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
935 file
.setKeepInSync(c
.getInt(
936 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
937 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
938 file
.setShareByLink(c
.getInt(
939 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
940 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
941 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
942 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
943 file
.setNeedsUpdateThumbnail(c
.getInt(
944 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
951 * Returns if the file/folder is shared by link or not
952 * @param path Path of the file/folder
955 public boolean isShareByLink(String path
) {
956 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
958 if (c
.moveToFirst()) {
959 file
= createFileInstance(c
);
962 return file
.isShareByLink();
966 * Returns the public link of the file/folder
967 * @param path Path of the file/folder
970 public String
getPublicLink(String path
) {
971 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
973 if (c
.moveToFirst()) {
974 file
= createFileInstance(c
);
977 return file
.getPublicLink();
981 // Methods for Shares
982 public boolean saveShare(OCShare share
) {
983 boolean overriden
= false
;
984 ContentValues cv
= new ContentValues();
985 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
986 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
987 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
988 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
989 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
990 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
991 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
992 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
993 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
995 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
996 share
.getSharedWithDisplayName()
998 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
999 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1000 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1001 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1003 if (shareExists(share
.getIdRemoteShared())) { // for renamed files
1006 if (getContentResolver() != null
) {
1007 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
1008 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1009 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
1012 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
1013 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1014 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
1015 } catch (RemoteException e
) {
1017 "Fail to insert insert file to database "
1022 Uri result_uri
= null
;
1023 if (getContentResolver() != null
) {
1024 result_uri
= getContentResolver().insert(
1025 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1028 result_uri
= getContentProviderClient().insert(
1029 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1030 } catch (RemoteException e
) {
1032 "Fail to insert insert file to database "
1036 if (result_uri
!= null
) {
1037 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
1039 share
.setId(new_id
);
1047 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
1049 if (getContentResolver() != null
) {
1050 c
= getContentResolver().query(
1051 ProviderTableMeta
.CONTENT_URI_SHARE
,
1053 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1054 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1055 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1056 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1060 c
= getContentProviderClient().query(
1061 ProviderTableMeta
.CONTENT_URI_SHARE
,
1063 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1064 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1065 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1066 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1069 } catch (RemoteException e
) {
1070 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1074 OCShare share
= null
;
1075 if (c
.moveToFirst()) {
1076 share
= createShareInstance(c
);
1082 private OCShare
createShareInstance(Cursor c
) {
1083 OCShare share
= null
;
1085 share
= new OCShare(c
.getString(c
1086 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1087 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1088 share
.setFileSource(c
.getLong(c
1089 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1090 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1091 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1092 share
.setPermissions(c
.getInt(c
1093 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1094 share
.setSharedDate(c
.getLong(c
1095 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1096 share
.setExpirationDate(c
.getLong(c
1097 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1098 share
.setToken(c
.getString(c
1099 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1100 share
.setSharedWithDisplayName(c
.getString(c
1101 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1102 share
.setIsFolder(c
.getInt(
1103 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1104 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1105 share
.setIdRemoteShared(
1106 c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
))
1113 private boolean shareExists(String cmp_key
, String value
) {
1115 if (getContentResolver() != null
) {
1116 c
= getContentResolver()
1117 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1120 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1122 new String
[] { value
, mAccount
.name
}, null
);
1125 c
= getContentProviderClient().query(
1126 ProviderTableMeta
.CONTENT_URI_SHARE
,
1129 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1130 new String
[] { value
, mAccount
.name
}, null
);
1131 } catch (RemoteException e
) {
1133 "Couldn't determine file existance, assuming non existance: "
1138 boolean retval
= c
.moveToFirst();
1143 private boolean shareExists(long remoteId
) {
1144 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1147 private void cleanSharedFiles() {
1148 ContentValues cv
= new ContentValues();
1149 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1150 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1151 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1152 String
[] whereArgs
= new String
[]{mAccount
.name
};
1154 if (getContentResolver() != null
) {
1155 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1159 getContentProviderClient().update(
1160 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1163 } catch (RemoteException e
) {
1164 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1169 private void cleanSharedFilesInFolder(OCFile folder
) {
1170 ContentValues cv
= new ContentValues();
1171 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1172 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1173 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1174 ProviderTableMeta
.FILE_PARENT
+ "=?";
1175 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1177 if (getContentResolver() != null
) {
1178 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1182 getContentProviderClient().update(
1183 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1186 } catch (RemoteException e
) {
1187 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1192 private void cleanShares() {
1193 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1194 String
[] whereArgs
= new String
[]{mAccount
.name
};
1196 if (getContentResolver() != null
) {
1197 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1201 getContentProviderClient().delete(
1202 ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
1205 } catch (RemoteException e
) {
1206 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1211 public void saveShares(Collection
<OCShare
> shares
) {
1213 if (shares
!= null
) {
1214 ArrayList
<ContentProviderOperation
> operations
=
1215 new ArrayList
<ContentProviderOperation
>(shares
.size());
1217 // prepare operations to insert or update files to save in the given folder
1218 for (OCShare share
: shares
) {
1219 ContentValues cv
= new ContentValues();
1220 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1221 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1222 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1223 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1224 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1225 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1226 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1227 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1228 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1230 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1231 share
.getSharedWithDisplayName()
1233 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1234 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1235 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1236 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1238 if (shareExists(share
.getIdRemoteShared())) {
1239 // updating an existing file
1241 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1244 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1245 new String
[] { String
.valueOf(share
.getIdRemoteShared()) }
1251 // adding a new file
1253 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1260 // apply operations in batch
1261 if (operations
.size() > 0) {
1262 @SuppressWarnings("unused")
1263 ContentProviderResult
[] results
= null
;
1264 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1265 " operations to FileContentProvider");
1267 if (getContentResolver() != null
) {
1268 results
= getContentResolver().applyBatch(
1269 MainApp
.getAuthority(), operations
1273 results
= getContentProviderClient().applyBatch(operations
);
1276 } catch (OperationApplicationException e
) {
1277 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1279 } catch (RemoteException e
) {
1280 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1287 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1290 if (sharedFiles
!= null
) {
1291 ArrayList
<ContentProviderOperation
> operations
=
1292 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1294 // prepare operations to insert or update files to save in the given folder
1295 for (OCFile file
: sharedFiles
) {
1296 ContentValues cv
= new ContentValues();
1297 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1299 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1300 file
.getModificationTimestampAtLastSyncForData()
1302 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1303 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1304 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1305 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1306 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1307 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1308 if (!file
.isFolder()) {
1309 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1311 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1312 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1314 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1315 file
.getLastSyncDateForData()
1317 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1318 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1319 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1320 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1321 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1322 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1324 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1325 file
.needsUpdateThumbnail() ?
1 : 0
1328 boolean existsByPath
= fileExists(file
.getRemotePath());
1329 if (existsByPath
|| fileExists(file
.getFileId())) {
1330 // updating an existing file
1332 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1335 ProviderTableMeta
._ID
+ "=?",
1336 new String
[] { String
.valueOf(file
.getFileId()) }
1341 // adding a new file
1343 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1350 // apply operations in batch
1351 if (operations
.size() > 0) {
1352 @SuppressWarnings("unused")
1353 ContentProviderResult
[] results
= null
;
1354 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1355 " operations to FileContentProvider");
1357 if (getContentResolver() != null
) {
1358 results
= getContentResolver().applyBatch(
1359 MainApp
.getAuthority(), operations
1363 results
= getContentProviderClient().applyBatch(operations
);
1366 } catch (OperationApplicationException e
) {
1367 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1369 } catch (RemoteException e
) {
1370 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1377 public void removeShare(OCShare share
){
1378 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1379 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1380 ProviderTableMeta
.FILE_PATH
+ "=?";
1381 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1382 if (getContentProviderClient() != null
) {
1384 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1385 } catch (RemoteException e
) {
1386 e
.printStackTrace();
1389 getContentResolver().delete(share_uri
, where
, whereArgs
);
1393 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1396 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1398 for (OCShare share
: shares
) {
1400 String path
= share
.getPath();
1401 if (share
.isFolder()) {
1402 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1405 // Update OCFile with data from share: ShareByLink and publicLink
1406 OCFile file
= getFileByPath(path
);
1408 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1409 file
.setShareByLink(true
);
1410 sharedFiles
.add(file
);
1415 updateSharedFiles(sharedFiles
);
1419 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1420 cleanSharedFilesInFolder(folder
);
1421 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1422 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1424 if (shares
!= null
) {
1425 // prepare operations to insert or update files to save in the given folder
1426 for (OCShare share
: shares
) {
1427 ContentValues cv
= new ContentValues();
1428 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1429 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1430 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1431 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1432 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1433 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1434 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1435 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1436 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1438 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1439 share
.getSharedWithDisplayName()
1441 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1442 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1443 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1444 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1447 if (shareExists(share.getIdRemoteShared())) {
1448 // updating an existing share resource
1450 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1452 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1453 new String[] { String.valueOf(share.getIdRemoteShared()) })
1458 // adding a new share resource
1460 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1468 // apply operations in batch
1469 if (operations
.size() > 0) {
1470 @SuppressWarnings("unused")
1471 ContentProviderResult
[] results
= null
;
1472 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1474 if (getContentResolver() != null
) {
1475 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1478 results
= getContentProviderClient().applyBatch(operations
);
1481 } catch (OperationApplicationException e
) {
1482 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1484 } catch (RemoteException e
) {
1485 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1492 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1493 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
1495 if (folder
!= null
) {
1496 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1497 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1498 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1500 Vector
<OCFile
> files
= getFolderContent(folder
);
1502 for (OCFile file
: files
) {
1503 whereArgs
[0] = file
.getRemotePath();
1504 preparedOperations
.add(
1505 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1506 withSelection(where
, whereArgs
).
1511 return preparedOperations
;
1514 if (operations.size() > 0) {
1516 if (getContentResolver() != null) {
1517 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1520 getContentProviderClient().applyBatch(operations);
1523 } catch (OperationApplicationException e) {
1524 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1526 } catch (RemoteException e) {
1527 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1533 if (getContentResolver() != null) {
1535 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1540 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1544 } catch (RemoteException e) {
1545 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());