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());
304 boolean existsByPath
= fileExists(file
.getRemotePath());
305 if (existsByPath
|| fileExists(file
.getFileId())) {
306 // updating an existing file
307 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
309 withSelection( ProviderTableMeta
._ID
+ "=?",
310 new String
[] { String
.valueOf(file
.getFileId()) })
315 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
316 withValues(cv
).build());
320 // prepare operations to remove files in the given folder
321 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
322 ProviderTableMeta
.FILE_PATH
+ "=?";
323 String
[] whereArgs
= null
;
324 for (OCFile file
: filesToRemove
) {
325 if (file
.getParentId() == folder
.getFileId()) {
326 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
327 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
328 if (file
.isFolder()) {
329 operations
.add(ContentProviderOperation
.newDelete(
330 ContentUris
.withAppendedId(
331 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
333 ).withSelection(where
, whereArgs
).build());
336 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
337 if (localFolder
.exists()) {
338 removeLocalFolder(localFolder
);
341 operations
.add(ContentProviderOperation
.newDelete(
342 ContentUris
.withAppendedId(
343 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
345 ).withSelection(where
, whereArgs
).build());
348 new File(file
.getStoragePath()).delete();
354 // update metadata of folder
355 ContentValues cv
= new ContentValues();
356 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
358 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
359 folder
.getModificationTimestampAtLastSyncForData()
361 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
362 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
363 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
364 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
365 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
366 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
367 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
368 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
369 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
370 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
371 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
372 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
373 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
374 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
375 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
377 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
379 withSelection( ProviderTableMeta
._ID
+ "=?",
380 new String
[] { String
.valueOf(folder
.getFileId()) })
383 // apply operations in batch
384 ContentProviderResult
[] results
= null
;
385 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
387 if (getContentResolver() != null
) {
388 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
391 results
= getContentProviderClient().applyBatch(operations
);
394 } catch (OperationApplicationException e
) {
395 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
397 } catch (RemoteException e
) {
398 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
401 // update new id in file objects for insertions
402 if (results
!= null
) {
404 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
406 for (int i
=0; i
<results
.length
; i
++) {
407 if (filesIt
.hasNext()) {
408 file
= filesIt
.next();
412 if (results
[i
].uri
!= null
) {
413 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
414 //updatedFiles.get(i).setFileId(newId);
416 file
.setFileId(newId
);
422 //updateFolderSize(folder.getFileId());
431 // private void updateFolderSize(long id) {
432 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
433 // Log_OC.d(TAG, "Updating size of " + id);
434 // if (getContentResolver() != null) {
435 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
436 // new ContentValues(),
437 // won't be used, but cannot be null; crashes in KLP
438 // ProviderTableMeta._ID + "=?",
439 // new String[] { String.valueOf(id) });
442 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
443 // new ContentValues(),
444 // won't be used, but cannot be null; crashes in KLP
445 // ProviderTableMeta._ID + "=?",
446 // new String[] { String.valueOf(id) });
448 // } catch (RemoteException e) {
450 // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
454 // Log_OC.e(TAG, "not updating size for folder " + id);
459 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
460 boolean success
= true
;
462 if (file
.isFolder()) {
463 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
467 Uri file_uri
= ContentUris
.withAppendedId(
468 ProviderTableMeta
.CONTENT_URI_FILE
,
471 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
472 ProviderTableMeta
.FILE_PATH
+ "=?";
473 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
475 if (getContentProviderClient() != null
) {
477 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
478 } catch (RemoteException e
) {
482 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
484 success
&= (deleted
> 0);
486 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
487 success
= new File(file
.getStoragePath()).delete();
488 if (!removeDBData
&& success
) {
489 // maybe unnecessary, but should be checked TODO remove if unnecessary
490 file
.setStoragePath(null
);
500 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
501 boolean success
= true
;
502 if (folder
!= null
&& folder
.isFolder()) {
503 if (removeDBData
&& folder
.getFileId() != -1) {
504 success
= removeFolderInDb(folder
);
506 if (removeLocalContent
&& success
) {
507 success
= removeLocalFolder(folder
);
513 private boolean removeFolderInDb(OCFile folder
) {
514 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
515 folder
.getFileId()); // URI for recursive deletion
516 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
517 ProviderTableMeta
.FILE_PATH
+ "=?";
518 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
520 if (getContentProviderClient() != null
) {
522 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
523 } catch (RemoteException e
) {
527 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
532 private boolean removeLocalFolder(OCFile folder
) {
533 boolean success
= true
;
534 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
535 if (localFolder
.exists()) {
536 // stage 1: remove the local files already registered in the files database
537 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
539 for (OCFile file
: files
) {
540 if (file
.isFolder()) {
541 success
&= removeLocalFolder(file
);
544 File localFile
= new File(file
.getStoragePath());
545 success
&= localFile
.delete();
547 file
.setStoragePath(null
);
555 // stage 2: remove the folder itself and any local file inside out of sync;
556 // for instance, after clearing the app cache or reinstalling
557 success
&= removeLocalFolder(localFolder
);
562 private boolean removeLocalFolder(File localFolder
) {
563 boolean success
= true
;
564 File
[] localFiles
= localFolder
.listFiles();
565 if (localFiles
!= null
) {
566 for (File localFile
: localFiles
) {
567 if (localFile
.isDirectory()) {
568 success
&= removeLocalFolder(localFile
);
570 success
&= localFile
.delete();
574 success
&= localFolder
.delete();
579 * Updates database for a folder that was moved to a different location.
581 * TODO explore better (faster) implementations
582 * TODO throw exceptions up !
584 public void moveFolder(OCFile folder
, String newPath
) {
585 // TODO check newPath
587 if ( folder
!= null
&& folder
.isFolder() &&
588 folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())
590 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
592 if (getContentProviderClient() != null
) {
594 c
= getContentProviderClient().query (
595 ProviderTableMeta
.CONTENT_URI
,
597 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
598 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
599 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" },
600 ProviderTableMeta
.FILE_PATH
+ " ASC "
602 } catch (RemoteException e
) {
603 Log_OC
.e(TAG
, e
.getMessage());
606 c
= getContentResolver().query (
607 ProviderTableMeta
.CONTENT_URI
,
609 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
610 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
611 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" },
612 ProviderTableMeta
.FILE_PATH
+ " ASC "
616 /// 2. prepare a batch of update operations to change all the descendants
617 ArrayList
<ContentProviderOperation
> operations
=
618 new ArrayList
<ContentProviderOperation
>(c
.getCount());
619 int lengthOfOldPath
= folder
.getRemotePath().length();
620 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
621 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
622 if (c
.moveToFirst()) {
624 ContentValues cv
= new ContentValues(); // keep the constructor in the loop
625 OCFile child
= createFileInstance(c
);
627 ProviderTableMeta
.FILE_PATH
,
628 newPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
630 if ( child
.getStoragePath() != null
&&
631 child
.getStoragePath().startsWith(defaultSavePath
) ) {
633 ProviderTableMeta
.FILE_STORAGE_PATH
,
634 defaultSavePath
+ newPath
+
635 child
.getStoragePath().substring(lengthOfOldStoragePath
)
639 ContentProviderOperation
.
640 newUpdate(ProviderTableMeta
.CONTENT_URI
).
643 ProviderTableMeta
._ID
+ "=?",
644 new String
[] { String
.valueOf(child
.getFileId()) }
648 } while (c
.moveToNext());
652 /// 3. apply updates in batch
654 if (getContentResolver() != null
) {
655 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
658 getContentProviderClient().applyBatch(operations
);
661 } catch (OperationApplicationException e
) {
662 Log_OC
.e(TAG
, "Fail to update descendants of " +
663 folder
.getFileId() + " in database", e
);
665 } catch (RemoteException e
) {
666 Log_OC
.e(TAG
, "Fail to update desendants of " +
667 folder
.getFileId() + " in database", e
);
674 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
676 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
678 OCFile targetParent
= getFileByPath(targetParentPath
);
679 if (targetParent
== null
) {
683 /// 1. get all the descendants of the moved element in a single QUERY
685 if (getContentProviderClient() != null
) {
687 c
= getContentProviderClient().query(
688 ProviderTableMeta
.CONTENT_URI
,
690 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
691 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
694 file
.getRemotePath() + "%"
696 ProviderTableMeta
.FILE_PATH
+ " ASC "
698 } catch (RemoteException e
) {
699 Log_OC
.e(TAG
, e
.getMessage());
703 c
= getContentResolver().query(
704 ProviderTableMeta
.CONTENT_URI
,
706 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
707 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
710 file
.getRemotePath() + "%"
712 ProviderTableMeta
.FILE_PATH
+ " ASC "
716 /// 2. prepare a batch of update operations to change all the descendants
717 ArrayList
<ContentProviderOperation
> operations
=
718 new ArrayList
<ContentProviderOperation
>(c
.getCount());
719 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
720 if (c
.moveToFirst()) {
721 int lengthOfOldPath
= file
.getRemotePath().length();
722 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
724 ContentValues cv
= new ContentValues(); // keep construction in the loop
725 OCFile child
= createFileInstance(c
);
727 ProviderTableMeta
.FILE_PATH
,
728 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
730 if (child
.getStoragePath() != null
&&
731 child
.getStoragePath().startsWith(defaultSavePath
)) {
732 // update link to downloaded content - but local move is not done here!
734 ProviderTableMeta
.FILE_STORAGE_PATH
,
735 defaultSavePath
+ targetPath
+
736 child
.getStoragePath().substring(lengthOfOldStoragePath
)
739 if (child
.getRemotePath().equals(file
.getRemotePath())) {
741 ProviderTableMeta
.FILE_PARENT
,
742 targetParent
.getFileId()
746 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
749 ProviderTableMeta
._ID
+ "=?",
750 new String
[] { String
.valueOf(child
.getFileId()) }
754 } while (c
.moveToNext());
758 /// 3. apply updates in batch
760 if (getContentResolver() != null
) {
761 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
764 getContentProviderClient().applyBatch(operations
);
767 } catch (Exception e
) {
770 "Fail to update " + file
.getFileId() + " and descendants in database",
775 /// 4. move in local file system
776 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
777 File localFile
= new File(localPath
);
778 boolean renamed
= false
;
779 if (localFile
.exists()) {
780 File targetFile
= new File(defaultSavePath
+ targetPath
);
781 File targetFolder
= targetFile
.getParentFile();
782 if (!targetFolder
.exists()) {
783 targetFolder
.mkdirs();
785 renamed
= localFile
.renameTo(targetFile
);
787 Log_OC
.d(TAG
, "Local file RENAMED : " + renamed
);
794 private Vector
<OCFile
> getFolderContent(long parentId
) {
796 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
798 Uri req_uri
= Uri
.withAppendedPath(
799 ProviderTableMeta
.CONTENT_URI_DIR
,
800 String
.valueOf(parentId
));
803 if (getContentProviderClient() != null
) {
805 c
= getContentProviderClient().query(req_uri
, null
,
806 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
807 new String
[] { String
.valueOf(parentId
)}, null
);
808 } catch (RemoteException e
) {
809 Log_OC
.e(TAG
, e
.getMessage());
813 c
= getContentResolver().query(req_uri
, null
,
814 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
815 new String
[] { String
.valueOf(parentId
)}, null
);
818 if (c
.moveToFirst()) {
820 OCFile child
= createFileInstance(c
);
822 } while (c
.moveToNext());
827 Collections
.sort(ret
);
833 private OCFile
createRootDir() {
834 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
835 file
.setMimetype("DIR");
836 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
841 private boolean fileExists(String cmp_key
, String value
) {
843 if (getContentResolver() != null
) {
844 c
= getContentResolver()
845 .query(ProviderTableMeta
.CONTENT_URI
,
848 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
850 new String
[] { value
, mAccount
.name
}, null
);
853 c
= getContentProviderClient().query(
854 ProviderTableMeta
.CONTENT_URI
,
857 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
858 new String
[] { value
, mAccount
.name
}, null
);
859 } catch (RemoteException e
) {
861 "Couldn't determine file existance, assuming non existance: "
866 boolean retval
= c
.moveToFirst();
871 private Cursor
getCursorForValue(String key
, String value
) {
873 if (getContentResolver() != null
) {
874 c
= getContentResolver()
875 .query(ProviderTableMeta
.CONTENT_URI
,
878 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
880 new String
[] { value
, mAccount
.name
}, null
);
883 c
= getContentProviderClient().query(
884 ProviderTableMeta
.CONTENT_URI
,
886 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
887 + "=?", new String
[] { value
, mAccount
.name
},
889 } catch (RemoteException e
) {
890 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
898 private OCFile
createFileInstance(Cursor c
) {
901 file
= new OCFile(c
.getString(c
902 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
903 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
904 file
.setParentId(c
.getLong(c
905 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
906 file
.setMimetype(c
.getString(c
907 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
908 if (!file
.isFolder()) {
909 file
.setStoragePath(c
.getString(c
910 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
911 if (file
.getStoragePath() == null
) {
912 // try to find existing file and bind it with current account;
913 // with the current update of SynchronizeFolderOperation, this won't be
914 // necessary anymore after a full synchronization of the account
915 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
917 file
.setStoragePath(f
.getAbsolutePath());
918 file
.setLastSyncDateForData(f
.lastModified());
922 file
.setFileLength(c
.getLong(c
923 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
924 file
.setCreationTimestamp(c
.getLong(c
925 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
926 file
.setModificationTimestamp(c
.getLong(c
927 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
928 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
929 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
930 file
.setLastSyncDateForProperties(c
.getLong(c
931 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
932 file
.setLastSyncDateForData(c
.getLong(c
.
933 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
934 file
.setKeepInSync(c
.getInt(
935 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
936 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
937 file
.setShareByLink(c
.getInt(
938 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
939 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
940 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
941 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
942 file
.setNeedsUpdateThumbnail(c
.getInt(
943 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
950 * Returns if the file/folder is shared by link or not
951 * @param path Path of the file/folder
954 public boolean isShareByLink(String path
) {
955 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
957 if (c
.moveToFirst()) {
958 file
= createFileInstance(c
);
961 return file
.isShareByLink();
965 * Returns the public link of the file/folder
966 * @param path Path of the file/folder
969 public String
getPublicLink(String path
) {
970 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
972 if (c
.moveToFirst()) {
973 file
= createFileInstance(c
);
976 return file
.getPublicLink();
980 // Methods for Shares
981 public boolean saveShare(OCShare share
) {
982 boolean overriden
= false
;
983 ContentValues cv
= new ContentValues();
984 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
985 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
986 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
987 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
988 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
989 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
990 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
991 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
992 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
994 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
995 share
.getSharedWithDisplayName()
997 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
998 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
999 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1000 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1002 if (shareExists(share
.getIdRemoteShared())) { // for renamed files
1005 if (getContentResolver() != null
) {
1006 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
1007 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1008 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
1011 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
1012 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1013 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
1014 } catch (RemoteException e
) {
1016 "Fail to insert insert file to database "
1021 Uri result_uri
= null
;
1022 if (getContentResolver() != null
) {
1023 result_uri
= getContentResolver().insert(
1024 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1027 result_uri
= getContentProviderClient().insert(
1028 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
1029 } catch (RemoteException e
) {
1031 "Fail to insert insert file to database "
1035 if (result_uri
!= null
) {
1036 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
1038 share
.setId(new_id
);
1046 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
1048 if (getContentResolver() != null
) {
1049 c
= getContentResolver().query(
1050 ProviderTableMeta
.CONTENT_URI_SHARE
,
1052 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1053 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1054 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1055 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1059 c
= getContentProviderClient().query(
1060 ProviderTableMeta
.CONTENT_URI_SHARE
,
1062 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1063 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1064 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1065 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1068 } catch (RemoteException e
) {
1069 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1073 OCShare share
= null
;
1074 if (c
.moveToFirst()) {
1075 share
= createShareInstance(c
);
1081 private OCShare
createShareInstance(Cursor c
) {
1082 OCShare share
= null
;
1084 share
= new OCShare(c
.getString(c
1085 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1086 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1087 share
.setFileSource(c
.getLong(c
1088 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1089 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1090 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1091 share
.setPermissions(c
.getInt(c
1092 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1093 share
.setSharedDate(c
.getLong(c
1094 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1095 share
.setExpirationDate(c
.getLong(c
1096 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1097 share
.setToken(c
.getString(c
1098 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1099 share
.setSharedWithDisplayName(c
.getString(c
1100 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1101 share
.setIsFolder(c
.getInt(
1102 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1103 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1104 share
.setIdRemoteShared(
1105 c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
))
1112 private boolean shareExists(String cmp_key
, String value
) {
1114 if (getContentResolver() != null
) {
1115 c
= getContentResolver()
1116 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1119 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1121 new String
[] { value
, mAccount
.name
}, null
);
1124 c
= getContentProviderClient().query(
1125 ProviderTableMeta
.CONTENT_URI_SHARE
,
1128 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1129 new String
[] { value
, mAccount
.name
}, null
);
1130 } catch (RemoteException e
) {
1132 "Couldn't determine file existance, assuming non existance: "
1137 boolean retval
= c
.moveToFirst();
1142 private boolean shareExists(long remoteId
) {
1143 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1146 private void cleanSharedFiles() {
1147 ContentValues cv
= new ContentValues();
1148 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1149 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1150 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1151 String
[] whereArgs
= new String
[]{mAccount
.name
};
1153 if (getContentResolver() != null
) {
1154 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1158 getContentProviderClient().update(
1159 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1162 } catch (RemoteException e
) {
1163 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1168 private void cleanSharedFilesInFolder(OCFile folder
) {
1169 ContentValues cv
= new ContentValues();
1170 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1171 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1172 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1173 ProviderTableMeta
.FILE_PARENT
+ "=?";
1174 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1176 if (getContentResolver() != null
) {
1177 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1181 getContentProviderClient().update(
1182 ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
1185 } catch (RemoteException e
) {
1186 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1191 private void cleanShares() {
1192 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1193 String
[] whereArgs
= new String
[]{mAccount
.name
};
1195 if (getContentResolver() != null
) {
1196 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1200 getContentProviderClient().delete(
1201 ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
1204 } catch (RemoteException e
) {
1205 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1210 public void saveShares(Collection
<OCShare
> shares
) {
1212 if (shares
!= null
) {
1213 ArrayList
<ContentProviderOperation
> operations
=
1214 new ArrayList
<ContentProviderOperation
>(shares
.size());
1216 // prepare operations to insert or update files to save in the given folder
1217 for (OCShare share
: shares
) {
1218 ContentValues cv
= new ContentValues();
1219 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1220 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1221 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1222 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1223 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1224 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1225 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1226 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1227 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1229 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1230 share
.getSharedWithDisplayName()
1232 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1233 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1234 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1235 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1237 if (shareExists(share
.getIdRemoteShared())) {
1238 // updating an existing file
1240 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1243 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1244 new String
[] { String
.valueOf(share
.getIdRemoteShared()) }
1250 // adding a new file
1252 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1259 // apply operations in batch
1260 if (operations
.size() > 0) {
1261 @SuppressWarnings("unused")
1262 ContentProviderResult
[] results
= null
;
1263 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1264 " operations to FileContentProvider");
1266 if (getContentResolver() != null
) {
1267 results
= getContentResolver().applyBatch(
1268 MainApp
.getAuthority(), operations
1272 results
= getContentProviderClient().applyBatch(operations
);
1275 } catch (OperationApplicationException e
) {
1276 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1278 } catch (RemoteException e
) {
1279 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1286 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1289 if (sharedFiles
!= null
) {
1290 ArrayList
<ContentProviderOperation
> operations
=
1291 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1293 // prepare operations to insert or update files to save in the given folder
1294 for (OCFile file
: sharedFiles
) {
1295 ContentValues cv
= new ContentValues();
1296 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1298 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1299 file
.getModificationTimestampAtLastSyncForData()
1301 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1302 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1303 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1304 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1305 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1306 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1307 if (!file
.isFolder()) {
1308 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1310 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1311 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1313 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1314 file
.getLastSyncDateForData()
1316 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1317 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1318 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1319 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1320 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1321 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1323 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1324 file
.needsUpdateThumbnail() ?
1 : 0
1327 boolean existsByPath
= fileExists(file
.getRemotePath());
1328 if (existsByPath
|| fileExists(file
.getFileId())) {
1329 // updating an existing file
1331 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1334 ProviderTableMeta
._ID
+ "=?",
1335 new String
[] { String
.valueOf(file
.getFileId()) }
1340 // adding a new file
1342 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1349 // apply operations in batch
1350 if (operations
.size() > 0) {
1351 @SuppressWarnings("unused")
1352 ContentProviderResult
[] results
= null
;
1353 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1354 " operations to FileContentProvider");
1356 if (getContentResolver() != null
) {
1357 results
= getContentResolver().applyBatch(
1358 MainApp
.getAuthority(), operations
1362 results
= getContentProviderClient().applyBatch(operations
);
1365 } catch (OperationApplicationException e
) {
1366 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1368 } catch (RemoteException e
) {
1369 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1376 public void removeShare(OCShare share
){
1377 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1378 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1379 ProviderTableMeta
.FILE_PATH
+ "=?";
1380 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1381 if (getContentProviderClient() != null
) {
1383 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1384 } catch (RemoteException e
) {
1385 e
.printStackTrace();
1388 getContentResolver().delete(share_uri
, where
, whereArgs
);
1392 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1395 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1397 for (OCShare share
: shares
) {
1399 String path
= share
.getPath();
1400 if (share
.isFolder()) {
1401 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1404 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1405 OCFile file
= getFileByPath(path
);
1407 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1408 file
.setShareByLink(true
);
1409 sharedFiles
.add(file
);
1414 updateSharedFiles(sharedFiles
);
1418 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1419 cleanSharedFilesInFolder(folder
);
1420 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1421 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1423 if (shares
!= null
) {
1424 // prepare operations to insert or update files to save in the given folder
1425 for (OCShare share
: shares
) {
1426 ContentValues cv
= new ContentValues();
1427 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1428 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1429 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1430 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1431 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1432 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1433 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1434 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1435 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1437 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1438 share
.getSharedWithDisplayName()
1440 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1441 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1442 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1443 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1446 if (shareExists(share.getIdRemoteShared())) {
1447 // updating an existing share resource
1449 ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1451 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1452 new String[] { String.valueOf(share.getIdRemoteShared()) })
1457 // adding a new share resource
1459 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1467 // apply operations in batch
1468 if (operations
.size() > 0) {
1469 @SuppressWarnings("unused")
1470 ContentProviderResult
[] results
= null
;
1471 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1473 if (getContentResolver() != null
) {
1474 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1477 results
= getContentProviderClient().applyBatch(operations
);
1480 } catch (OperationApplicationException e
) {
1481 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1483 } catch (RemoteException e
) {
1484 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1491 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1492 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
1494 if (folder
!= null
) {
1495 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1496 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1497 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1499 Vector
<OCFile
> files
= getFolderContent(folder
);
1501 for (OCFile file
: files
) {
1502 whereArgs
[0] = file
.getRemotePath();
1503 preparedOperations
.add(
1504 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1505 withSelection(where
, whereArgs
).
1510 return preparedOperations
;
1513 if (operations.size() > 0) {
1515 if (getContentResolver() != null) {
1516 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1519 getContentProviderClient().applyBatch(operations);
1522 } catch (OperationApplicationException e) {
1523 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1525 } catch (RemoteException e) {
1526 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1532 if (getContentResolver() != null) {
1534 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1539 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1543 } catch (RemoteException e) {
1544 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());