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
.resources
.shares
.OCShare
;
31 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
32 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
33 import com
.owncloud
.android
.utils
.FileStorageUtils
;
34 import com
.owncloud
.android
.utils
.Log_OC
;
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 (if possible) 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());
172 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
173 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
174 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
175 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
176 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
177 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
178 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
179 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
180 if (!file
.isFolder())
181 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
182 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
183 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
184 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
185 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
186 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
187 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
188 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
189 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
190 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
192 boolean sameRemotePath
= fileExists(file
.getRemotePath());
193 if (sameRemotePath
||
194 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
196 OCFile oldFile
= null
;
197 if (sameRemotePath
) {
198 oldFile
= getFileByPath(file
.getRemotePath());
199 file
.setFileId(oldFile
.getFileId());
201 oldFile
= getFileById(file
.getFileId());
205 if (getContentResolver() != null
) {
206 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
207 ProviderTableMeta
._ID
+ "=?",
208 new String
[] { String
.valueOf(file
.getFileId()) });
211 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
212 cv
, ProviderTableMeta
._ID
+ "=?",
213 new String
[] { String
.valueOf(file
.getFileId()) });
214 } catch (RemoteException e
) {
216 "Fail to insert insert file to database "
221 Uri result_uri
= null
;
222 if (getContentResolver() != null
) {
223 result_uri
= getContentResolver().insert(
224 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
227 result_uri
= getContentProviderClient().insert(
228 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
229 } catch (RemoteException e
) {
231 "Fail to insert insert file to database "
235 if (result_uri
!= null
) {
236 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
238 file
.setFileId(new_id
);
242 // if (file.isFolder()) {
243 // updateFolderSize(file.getFileId());
245 // updateFolderSize(file.getParentId());
253 * Inserts or updates the list of files contained in a given folder.
255 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
256 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
260 * @param removeNotUpdated
262 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
264 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
266 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
268 // prepare operations to insert or update files to save in the given folder
269 for (OCFile file
: updatedFiles
) {
270 ContentValues cv
= new ContentValues();
271 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
272 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
273 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
274 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
275 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
276 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
277 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
278 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
279 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
280 if (!file
.isFolder()) {
281 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
283 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
284 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
285 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
286 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
287 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
288 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
289 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
290 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
291 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
293 boolean existsByPath
= fileExists(file
.getRemotePath());
294 if (existsByPath
|| fileExists(file
.getFileId())) {
295 // updating an existing file
296 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
298 withSelection( ProviderTableMeta
._ID
+ "=?",
299 new String
[] { String
.valueOf(file
.getFileId()) })
304 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
308 // prepare operations to remove files in the given folder
309 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
310 String
[] whereArgs
= null
;
311 for (OCFile file
: filesToRemove
) {
312 if (file
.getParentId() == folder
.getFileId()) {
313 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
314 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
315 if (file
.isFolder()) {
316 operations
.add(ContentProviderOperation
317 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
319 // TODO remove local folder
321 operations
.add(ContentProviderOperation
322 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
325 new File(file
.getStoragePath()).delete();
326 // TODO move the deletion of local contents after success of deletions
332 // update metadata of folder
333 ContentValues cv
= new ContentValues();
334 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
335 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
336 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
337 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
338 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
339 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
340 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
341 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
342 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
343 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
344 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
345 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
346 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
347 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
348 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
349 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
350 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
352 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
354 withSelection( ProviderTableMeta
._ID
+ "=?",
355 new String
[] { String
.valueOf(folder
.getFileId()) })
358 // apply operations in batch
359 ContentProviderResult
[] results
= null
;
360 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
362 if (getContentResolver() != null
) {
363 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
366 results
= getContentProviderClient().applyBatch(operations
);
369 } catch (OperationApplicationException e
) {
370 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
372 } catch (RemoteException e
) {
373 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
376 // update new id in file objects for insertions
377 if (results
!= null
) {
379 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
381 for (int i
=0; i
<results
.length
; i
++) {
382 if (filesIt
.hasNext()) {
383 file
= filesIt
.next();
387 if (results
[i
].uri
!= null
) {
388 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
389 //updatedFiles.get(i).setFileId(newId);
391 file
.setFileId(newId
);
397 //updateFolderSize(folder.getFileId());
406 // private void updateFolderSize(long id) {
407 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
408 // Log_OC.d(TAG, "Updating size of " + id);
409 // if (getContentResolver() != null) {
410 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
411 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
412 // ProviderTableMeta._ID + "=?",
413 // new String[] { String.valueOf(id) });
416 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
417 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
418 // ProviderTableMeta._ID + "=?",
419 // new String[] { String.valueOf(id) });
421 // } catch (RemoteException e) {
422 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
426 // Log_OC.e(TAG, "not updating size for folder " + id);
431 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
432 boolean success
= true
;
434 if (file
.isFolder()) {
435 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
439 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
440 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
441 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
442 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
444 if (getContentProviderClient() != null
) {
446 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
447 } catch (RemoteException e
) {
451 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
453 success
&= (deleted
> 0);
455 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
456 success
= new File(file
.getStoragePath()).delete();
457 if (!removeDBData
&& success
) {
458 // maybe unnecessary, but should be checked TODO remove if unnecessary
459 file
.setStoragePath(null
);
469 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
470 boolean success
= true
;
471 if (folder
!= null
&& folder
.isFolder()) {
472 if (removeDBData
&& folder
.getFileId() != -1) {
473 success
= removeFolderInDb(folder
);
475 if (removeLocalContent
&& success
) {
476 success
= removeLocalFolder(folder
);
482 private boolean removeFolderInDb(OCFile folder
) {
483 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
484 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
485 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
487 if (getContentProviderClient() != null
) {
489 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
490 } catch (RemoteException e
) {
494 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
499 private boolean removeLocalFolder(OCFile folder
) {
500 boolean success
= true
;
501 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
502 if (localFolder
.exists()) {
503 // stage 1: remove the local files already registered in the files database
504 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
506 for (OCFile file
: files
) {
507 if (file
.isFolder()) {
508 success
&= removeLocalFolder(file
);
511 File localFile
= new File(file
.getStoragePath());
512 success
&= localFile
.delete();
514 file
.setStoragePath(null
);
522 // stage 2: remove the folder itself and any local file inside out of sync;
523 // for instance, after clearing the app cache or reinstalling
524 success
&= removeLocalFolder(localFolder
);
529 private boolean removeLocalFolder(File localFolder
) {
530 boolean success
= true
;
531 File
[] localFiles
= localFolder
.listFiles();
532 if (localFiles
!= null
) {
533 for (File localFile
: localFiles
) {
534 if (localFile
.isDirectory()) {
535 success
&= removeLocalFolder(localFile
);
537 success
&= localFile
.delete();
541 success
&= localFolder
.delete();
546 * Updates database for a folder that was moved to a different location.
548 * TODO explore better (faster) implementations
549 * TODO throw exceptions up !
551 public void moveFolder(OCFile folder
, String newPath
) {
552 // TODO check newPath
554 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
555 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
557 if (getContentProviderClient() != null
) {
559 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
561 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
562 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
563 } catch (RemoteException e
) {
564 Log_OC
.e(TAG
, e
.getMessage());
567 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
569 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
570 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
573 /// 2. prepare a batch of update operations to change all the descendants
574 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
575 int lengthOfOldPath
= folder
.getRemotePath().length();
576 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
577 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
578 if (c
.moveToFirst()) {
580 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
581 OCFile child
= createFileInstance(c
);
582 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
583 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
584 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
586 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
588 withSelection( ProviderTableMeta
._ID
+ "=?",
589 new String
[] { String
.valueOf(child
.getFileId()) })
591 } while (c
.moveToNext());
595 /// 3. apply updates in batch
597 if (getContentResolver() != null
) {
598 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
601 getContentProviderClient().applyBatch(operations
);
604 } catch (OperationApplicationException e
) {
605 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
607 } catch (RemoteException e
) {
608 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
615 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
617 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
619 OCFile targetParent
= getFileByPath(targetParentPath
);
620 if (targetParent
== null
) {
624 /// 1. get all the descendants of the moved element in a single QUERY
626 if (getContentProviderClient() != null
) {
628 c
= getContentProviderClient().query(
629 ProviderTableMeta
.CONTENT_URI
,
631 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
632 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
635 file
.getRemotePath() + "%"
637 ProviderTableMeta
.FILE_PATH
+ " ASC "
639 } catch (RemoteException e
) {
640 Log_OC
.e(TAG
, e
.getMessage());
644 c
= getContentResolver().query(
645 ProviderTableMeta
.CONTENT_URI
,
647 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
648 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
651 file
.getRemotePath() + "%"
653 ProviderTableMeta
.FILE_PATH
+ " ASC "
657 /// 2. prepare a batch of update operations to change all the descendants
658 ArrayList
<ContentProviderOperation
> operations
=
659 new ArrayList
<ContentProviderOperation
>(c
.getCount());
660 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
661 if (c
.moveToFirst()) {
662 int lengthOfOldPath
= file
.getRemotePath().length();
663 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
665 ContentValues cv
= new ContentValues(); // keep construction in the loop
666 OCFile child
= createFileInstance(c
);
668 ProviderTableMeta
.FILE_PATH
,
669 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
671 if (child
.getStoragePath() != null
&&
672 child
.getStoragePath().startsWith(defaultSavePath
)) {
673 // update link to downloaded content - but local move is not done here!
675 ProviderTableMeta
.FILE_STORAGE_PATH
,
676 defaultSavePath
+ targetPath
+
677 child
.getStoragePath().substring(lengthOfOldStoragePath
)
680 if (child
.getRemotePath().equals(file
.getRemotePath())) {
682 ProviderTableMeta
.FILE_PARENT
,
683 targetParent
.getFileId()
687 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
690 ProviderTableMeta
._ID
+ "=?",
691 new String
[] { String
.valueOf(child
.getFileId()) }
695 } while (c
.moveToNext());
699 /// 3. apply updates in batch
701 if (getContentResolver() != null
) {
702 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
705 getContentProviderClient().applyBatch(operations
);
708 } catch (Exception e
) {
711 "Fail to update " + file
.getFileId() + " and descendants in database",
716 /// 4. move in local file system
717 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
718 File localFile
= new File(localPath
);
719 boolean renamed
= false
;
720 if (localFile
.exists()) {
721 renamed
= localFile
.renameTo(new File(defaultSavePath
+ targetPath
));
723 Log_OC
.d(TAG
, "Local file RENAMED : " + renamed
);
730 private Vector
<OCFile
> getFolderContent(long parentId
) {
732 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
734 Uri req_uri
= Uri
.withAppendedPath(
735 ProviderTableMeta
.CONTENT_URI_DIR
,
736 String
.valueOf(parentId
));
739 if (getContentProviderClient() != null
) {
741 c
= getContentProviderClient().query(req_uri
, null
,
742 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
743 new String
[] { String
.valueOf(parentId
)}, null
);
744 } catch (RemoteException e
) {
745 Log_OC
.e(TAG
, e
.getMessage());
749 c
= getContentResolver().query(req_uri
, null
,
750 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
751 new String
[] { String
.valueOf(parentId
)}, null
);
754 if (c
.moveToFirst()) {
756 OCFile child
= createFileInstance(c
);
758 } while (c
.moveToNext());
763 Collections
.sort(ret
);
769 private OCFile
createRootDir() {
770 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
771 file
.setMimetype("DIR");
772 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
777 private boolean fileExists(String cmp_key
, String value
) {
779 if (getContentResolver() != null
) {
780 c
= getContentResolver()
781 .query(ProviderTableMeta
.CONTENT_URI
,
784 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
786 new String
[] { value
, mAccount
.name
}, null
);
789 c
= getContentProviderClient().query(
790 ProviderTableMeta
.CONTENT_URI
,
793 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
794 new String
[] { value
, mAccount
.name
}, null
);
795 } catch (RemoteException e
) {
797 "Couldn't determine file existance, assuming non existance: "
802 boolean retval
= c
.moveToFirst();
807 private Cursor
getCursorForValue(String key
, String value
) {
809 if (getContentResolver() != null
) {
810 c
= getContentResolver()
811 .query(ProviderTableMeta
.CONTENT_URI
,
814 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
816 new String
[] { value
, mAccount
.name
}, null
);
819 c
= getContentProviderClient().query(
820 ProviderTableMeta
.CONTENT_URI
,
822 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
823 + "=?", new String
[] { value
, mAccount
.name
},
825 } catch (RemoteException e
) {
826 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
834 private OCFile
createFileInstance(Cursor c
) {
837 file
= new OCFile(c
.getString(c
838 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
839 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
840 file
.setParentId(c
.getLong(c
841 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
842 file
.setMimetype(c
.getString(c
843 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
844 if (!file
.isFolder()) {
845 file
.setStoragePath(c
.getString(c
846 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
847 if (file
.getStoragePath() == null
) {
848 // try to find existing file and bind it with current account; - with the current update of SynchronizeFolderOperation, this won't be necessary anymore after a full synchronization of the account
849 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
851 file
.setStoragePath(f
.getAbsolutePath());
852 file
.setLastSyncDateForData(f
.lastModified());
856 file
.setFileLength(c
.getLong(c
857 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
858 file
.setCreationTimestamp(c
.getLong(c
859 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
860 file
.setModificationTimestamp(c
.getLong(c
861 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
862 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
863 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
864 file
.setLastSyncDateForProperties(c
.getLong(c
865 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
866 file
.setLastSyncDateForData(c
.getLong(c
.
867 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
868 file
.setKeepInSync(c
.getInt(
869 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
870 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
871 file
.setShareByLink(c
.getInt(
872 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
873 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
874 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
875 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
882 * Returns if the file/folder is shared by link or not
883 * @param path Path of the file/folder
886 public boolean isShareByLink(String path
) {
887 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
889 if (c
.moveToFirst()) {
890 file
= createFileInstance(c
);
893 return file
.isShareByLink();
897 * Returns the public link of the file/folder
898 * @param path Path of the file/folder
901 public String
getPublicLink(String path
) {
902 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
904 if (c
.moveToFirst()) {
905 file
= createFileInstance(c
);
908 return file
.getPublicLink();
912 // Methods for Shares
913 public boolean saveShare(OCShare share
) {
914 boolean overriden
= false
;
915 ContentValues cv
= new ContentValues();
916 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
917 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
918 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
919 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
920 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
921 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
922 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
923 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
924 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
925 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
926 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
927 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
928 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
929 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
931 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
934 if (getContentResolver() != null
) {
935 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
936 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
937 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
940 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
941 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
942 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
943 } catch (RemoteException e
) {
945 "Fail to insert insert file to database "
950 Uri result_uri
= null
;
951 if (getContentResolver() != null
) {
952 result_uri
= getContentResolver().insert(
953 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
956 result_uri
= getContentProviderClient().insert(
957 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
958 } catch (RemoteException e
) {
960 "Fail to insert insert file to database "
964 if (result_uri
!= null
) {
965 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
975 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
977 if (getContentResolver() != null
) {
978 c
= getContentResolver().query(
979 ProviderTableMeta
.CONTENT_URI_SHARE
,
981 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
982 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
983 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
984 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
988 c
= getContentProviderClient().query(
989 ProviderTableMeta
.CONTENT_URI_SHARE
,
991 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
992 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
993 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
994 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
997 } catch (RemoteException e
) {
998 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1002 OCShare share
= null
;
1003 if (c
.moveToFirst()) {
1004 share
= createShareInstance(c
);
1010 private OCShare
createShareInstance(Cursor c
) {
1011 OCShare share
= null
;
1013 share
= new OCShare(c
.getString(c
1014 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1015 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1016 share
.setFileSource(c
.getLong(c
1017 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1018 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1019 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1020 share
.setPermissions(c
.getInt(c
1021 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1022 share
.setSharedDate(c
.getLong(c
1023 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1024 share
.setExpirationDate(c
.getLong(c
1025 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1026 share
.setToken(c
.getString(c
1027 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1028 share
.setSharedWithDisplayName(c
.getString(c
1029 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1030 share
.setIsFolder(c
.getInt(
1031 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1032 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1033 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
1039 private boolean shareExists(String cmp_key
, String value
) {
1041 if (getContentResolver() != null
) {
1042 c
= getContentResolver()
1043 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1046 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1048 new String
[] { value
, mAccount
.name
}, null
);
1051 c
= getContentProviderClient().query(
1052 ProviderTableMeta
.CONTENT_URI_SHARE
,
1055 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1056 new String
[] { value
, mAccount
.name
}, null
);
1057 } catch (RemoteException e
) {
1059 "Couldn't determine file existance, assuming non existance: "
1064 boolean retval
= c
.moveToFirst();
1069 private boolean shareExists(long remoteId
) {
1070 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1073 private void cleanSharedFiles() {
1074 ContentValues cv
= new ContentValues();
1075 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1076 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1077 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1078 String
[] whereArgs
= new String
[]{mAccount
.name
};
1080 if (getContentResolver() != null
) {
1081 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1085 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1087 } catch (RemoteException e
) {
1088 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1093 private void cleanSharedFilesInFolder(OCFile folder
) {
1094 ContentValues cv
= new ContentValues();
1095 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1096 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1097 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
1098 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1100 if (getContentResolver() != null
) {
1101 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1105 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1107 } catch (RemoteException e
) {
1108 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1113 private void cleanShares() {
1114 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1115 String
[] whereArgs
= new String
[]{mAccount
.name
};
1117 if (getContentResolver() != null
) {
1118 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1122 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1124 } catch (RemoteException e
) {
1125 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1130 public void saveShares(Collection
<OCShare
> shares
) {
1132 if (shares
!= null
) {
1133 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1135 // prepare operations to insert or update files to save in the given folder
1136 for (OCShare share
: shares
) {
1137 ContentValues cv
= new ContentValues();
1138 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1139 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1140 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1141 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1142 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1143 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1144 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1145 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1146 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1147 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1148 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1149 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1150 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1151 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1153 if (shareExists(share
.getIdRemoteShared())) {
1154 // updating an existing file
1155 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1157 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1158 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1162 // adding a new file
1163 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1167 // apply operations in batch
1168 if (operations
.size() > 0) {
1169 @SuppressWarnings("unused")
1170 ContentProviderResult
[] results
= null
;
1171 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1173 if (getContentResolver() != null
) {
1174 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1177 results
= getContentProviderClient().applyBatch(operations
);
1180 } catch (OperationApplicationException e
) {
1181 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1183 } catch (RemoteException e
) {
1184 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1191 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1194 if (sharedFiles
!= null
) {
1195 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1197 // prepare operations to insert or update files to save in the given folder
1198 for (OCFile file
: sharedFiles
) {
1199 ContentValues cv
= new ContentValues();
1200 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1201 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1202 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1203 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1204 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1205 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1206 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1207 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1208 if (!file
.isFolder()) {
1209 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1211 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1212 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1213 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1214 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1215 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1216 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1217 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1218 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1219 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1221 boolean existsByPath
= fileExists(file
.getRemotePath());
1222 if (existsByPath
|| fileExists(file
.getFileId())) {
1223 // updating an existing file
1224 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1226 withSelection( ProviderTableMeta
._ID
+ "=?",
1227 new String
[] { String
.valueOf(file
.getFileId()) })
1231 // adding a new file
1232 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1236 // apply operations in batch
1237 if (operations
.size() > 0) {
1238 @SuppressWarnings("unused")
1239 ContentProviderResult
[] results
= null
;
1240 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1242 if (getContentResolver() != null
) {
1243 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1246 results
= getContentProviderClient().applyBatch(operations
);
1249 } catch (OperationApplicationException e
) {
1250 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1252 } catch (RemoteException e
) {
1253 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1260 public void removeShare(OCShare share
){
1261 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1262 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1263 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1264 if (getContentProviderClient() != null
) {
1266 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1267 } catch (RemoteException e
) {
1268 e
.printStackTrace();
1271 getContentResolver().delete(share_uri
, where
, whereArgs
);
1275 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1278 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1280 for (OCShare share
: shares
) {
1282 String path
= share
.getPath();
1283 if (share
.isFolder()) {
1284 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1287 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1288 OCFile file
= getFileByPath(path
);
1290 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1291 file
.setShareByLink(true
);
1292 sharedFiles
.add(file
);
1297 updateSharedFiles(sharedFiles
);
1301 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1302 cleanSharedFilesInFolder(folder
);
1303 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1304 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1306 if (shares
!= null
) {
1307 // prepare operations to insert or update files to save in the given folder
1308 for (OCShare share
: shares
) {
1309 ContentValues cv
= new ContentValues();
1310 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1311 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1312 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1313 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1314 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1315 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1316 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1317 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1318 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1319 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1320 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1321 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1322 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1323 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1326 if (shareExists(share.getIdRemoteShared())) {
1327 // updating an existing share resource
1328 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1330 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1331 new String[] { String.valueOf(share.getIdRemoteShared()) })
1336 // adding a new share resource
1337 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1342 // apply operations in batch
1343 if (operations
.size() > 0) {
1344 @SuppressWarnings("unused")
1345 ContentProviderResult
[] results
= null
;
1346 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1348 if (getContentResolver() != null
) {
1349 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1352 results
= getContentProviderClient().applyBatch(operations
);
1355 } catch (OperationApplicationException e
) {
1356 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1358 } catch (RemoteException e
) {
1359 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1366 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1367 if (folder
!= null
) {
1368 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1369 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1371 Vector
<OCFile
> files
= getFolderContent(folder
);
1373 for (OCFile file
: files
) {
1374 whereArgs
[0] = file
.getRemotePath();
1375 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1376 .withSelection(where
, whereArgs
)
1380 return preparedOperations
;
1383 if (operations.size() > 0) {
1385 if (getContentResolver() != null) {
1386 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1389 getContentProviderClient().applyBatch(operations);
1392 } catch (OperationApplicationException e) {
1393 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1395 } catch (RemoteException e) {
1396 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1402 if (getContentResolver() != null) {
1404 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1409 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1413 } catch (RemoteException e) {
1414 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());