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
.Intent
;
45 import android
.content
.OperationApplicationException
;
46 import android
.database
.Cursor
;
47 import android
.net
.Uri
;
48 import android
.os
.RemoteException
;
50 public class FileDataStorageManager
{
52 public static final int ROOT_PARENT_ID
= 0;
54 private ContentResolver mContentResolver
;
55 private ContentProviderClient mContentProviderClient
;
56 private Account mAccount
;
58 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
61 public FileDataStorageManager(Account account
, ContentResolver cr
) {
62 mContentProviderClient
= null
;
63 mContentResolver
= cr
;
67 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
68 mContentProviderClient
= cp
;
69 mContentResolver
= null
;
74 public void setAccount(Account account
) {
78 public Account
getAccount() {
82 public void setContentResolver(ContentResolver cr
) {
83 mContentResolver
= cr
;
86 public ContentResolver
getContentResolver() {
87 return mContentResolver
;
90 public void setContentProviderClient(ContentProviderClient cp
) {
91 mContentProviderClient
= cp
;
94 public ContentProviderClient
getContentProviderClient() {
95 return mContentProviderClient
;
99 public OCFile
getFileByPath(String path
) {
100 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
102 if (c
.moveToFirst()) {
103 file
= createFileInstance(c
);
106 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
107 return createRootDir(); // root should always exist
113 public OCFile
getFileById(long id
) {
114 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
116 if (c
.moveToFirst()) {
117 file
= createFileInstance(c
);
123 public OCFile
getFileByLocalPath(String path
) {
124 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
126 if (c
.moveToFirst()) {
127 file
= createFileInstance(c
);
133 public boolean fileExists(long id
) {
134 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
137 public boolean fileExists(String path
) {
138 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
142 public Vector
<OCFile
> getFolderContent(OCFile f
) {
143 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
144 return getFolderContent(f
.getFileId());
147 return new Vector
<OCFile
>();
152 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
153 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
154 if (folder
!= null
) {
155 // TODO better implementation, filtering in the access to database (if possible) instead of here
156 Vector
<OCFile
> tmp
= getFolderContent(folder
);
157 OCFile current
= null
;
158 for (int i
=0; i
<tmp
.size(); i
++) {
159 current
= tmp
.get(i
);
160 if (current
.isImage()) {
169 public boolean saveFile(OCFile file
) {
170 boolean overriden
= false
;
171 ContentValues cv
= new ContentValues();
172 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
173 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
174 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
175 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
176 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
177 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
178 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
179 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
180 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
181 if (!file
.isFolder())
182 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
183 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
184 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
185 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
186 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
187 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
188 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
189 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
190 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
191 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
193 boolean sameRemotePath
= fileExists(file
.getRemotePath());
194 if (sameRemotePath
||
195 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
197 OCFile oldFile
= null
;
198 if (sameRemotePath
) {
199 oldFile
= getFileByPath(file
.getRemotePath());
200 file
.setFileId(oldFile
.getFileId());
202 oldFile
= getFileById(file
.getFileId());
206 if (getContentResolver() != null
) {
207 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
208 ProviderTableMeta
._ID
+ "=?",
209 new String
[] { String
.valueOf(file
.getFileId()) });
212 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
213 cv
, ProviderTableMeta
._ID
+ "=?",
214 new String
[] { String
.valueOf(file
.getFileId()) });
215 } catch (RemoteException e
) {
217 "Fail to insert insert file to database "
222 Uri result_uri
= null
;
223 if (getContentResolver() != null
) {
224 result_uri
= getContentResolver().insert(
225 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
228 result_uri
= getContentProviderClient().insert(
229 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
230 } catch (RemoteException e
) {
232 "Fail to insert insert file to database "
236 if (result_uri
!= null
) {
237 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
239 file
.setFileId(new_id
);
243 // if (file.isFolder()) {
244 // updateFolderSize(file.getFileId());
246 // updateFolderSize(file.getParentId());
254 * Inserts or updates the list of files contained in a given folder.
256 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
257 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
261 * @param removeNotUpdated
263 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
265 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
267 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
269 // prepare operations to insert or update files to save in the given folder
270 for (OCFile file
: updatedFiles
) {
271 ContentValues cv
= new ContentValues();
272 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
273 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
274 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
275 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
276 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
277 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
278 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
279 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
280 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
281 if (!file
.isFolder()) {
282 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
284 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
285 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
286 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
287 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
288 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
289 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
290 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
291 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
292 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
294 boolean existsByPath
= fileExists(file
.getRemotePath());
295 if (existsByPath
|| fileExists(file
.getFileId())) {
296 // updating an existing file
297 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
299 withSelection( ProviderTableMeta
._ID
+ "=?",
300 new String
[] { String
.valueOf(file
.getFileId()) })
305 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
309 // prepare operations to remove files in the given folder
310 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
311 String
[] whereArgs
= null
;
312 for (OCFile file
: filesToRemove
) {
313 if (file
.getParentId() == folder
.getFileId()) {
314 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
315 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
316 if (file
.isFolder()) {
317 operations
.add(ContentProviderOperation
318 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
320 // TODO remove local folder
322 operations
.add(ContentProviderOperation
323 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
326 new File(file
.getStoragePath()).delete();
327 // TODO move the deletion of local contents after success of deletions
333 // update metadata of folder
334 ContentValues cv
= new ContentValues();
335 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
336 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
337 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
338 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
339 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
340 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
341 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
342 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
343 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
344 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
345 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
346 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
347 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
348 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
349 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
350 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
351 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
353 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
355 withSelection( ProviderTableMeta
._ID
+ "=?",
356 new String
[] { String
.valueOf(folder
.getFileId()) })
359 // apply operations in batch
360 ContentProviderResult
[] results
= null
;
361 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
363 if (getContentResolver() != null
) {
364 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
367 results
= getContentProviderClient().applyBatch(operations
);
370 } catch (OperationApplicationException e
) {
371 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
373 } catch (RemoteException e
) {
374 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
377 // update new id in file objects for insertions
378 if (results
!= null
) {
380 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
382 for (int i
=0; i
<results
.length
; i
++) {
383 if (filesIt
.hasNext()) {
384 file
= filesIt
.next();
388 if (results
[i
].uri
!= null
) {
389 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
390 //updatedFiles.get(i).setFileId(newId);
392 file
.setFileId(newId
);
398 //updateFolderSize(folder.getFileId());
407 // private void updateFolderSize(long id) {
408 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
409 // Log_OC.d(TAG, "Updating size of " + id);
410 // if (getContentResolver() != null) {
411 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
412 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
413 // ProviderTableMeta._ID + "=?",
414 // new String[] { String.valueOf(id) });
417 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
418 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
419 // ProviderTableMeta._ID + "=?",
420 // new String[] { String.valueOf(id) });
422 // } catch (RemoteException e) {
423 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
427 // Log_OC.e(TAG, "not updating size for folder " + id);
432 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
433 boolean success
= true
;
435 if (file
.isFolder()) {
436 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
440 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
441 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
442 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
443 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
445 if (getContentProviderClient() != null
) {
447 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
448 } catch (RemoteException e
) {
452 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
454 success
&= (deleted
> 0);
456 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
&& success
) {
457 success
= new File(file
.getStoragePath()).delete();
458 if (!removeDBData
&& success
) {
459 // maybe unnecessary, but should be checked TODO remove if unnecessary
460 file
.setStoragePath(null
);
470 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
471 boolean success
= true
;
472 if (folder
!= null
&& folder
.isFolder()) {
473 if (removeDBData
&& folder
.getFileId() != -1) {
474 success
= removeFolderInDb(folder
);
476 if (removeLocalContent
&& success
) {
477 success
= removeLocalFolder(folder
);
483 private boolean removeFolderInDb(OCFile folder
) {
484 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
485 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
486 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
488 if (getContentProviderClient() != null
) {
490 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
491 } catch (RemoteException e
) {
495 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
500 private boolean removeLocalFolder(OCFile folder
) {
501 boolean success
= true
;
502 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
503 if (localFolder
.exists()) {
504 // stage 1: remove the local files already registered in the files database
505 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId());
507 for (OCFile file
: files
) {
508 if (file
.isFolder()) {
509 success
&= removeLocalFolder(file
);
512 File localFile
= new File(file
.getStoragePath());
513 success
&= localFile
.delete();
515 file
.setStoragePath(null
);
523 // stage 2: remove the folder itself and any local file inside out of sync;
524 // for instance, after clearing the app cache or reinstalling
525 success
&= removeLocalFolder(localFolder
);
530 private boolean removeLocalFolder(File localFolder
) {
531 boolean success
= true
;
532 File
[] localFiles
= localFolder
.listFiles();
533 if (localFiles
!= null
) {
534 for (File localFile
: localFiles
) {
535 if (localFile
.isDirectory()) {
536 success
&= removeLocalFolder(localFile
);
538 success
&= localFile
.delete();
542 success
&= localFolder
.delete();
547 * Updates database for a folder that was moved to a different location.
549 * TODO explore better (faster) implementations
550 * TODO throw exceptions up !
552 public void moveFolder(OCFile folder
, String newPath
) {
553 // TODO check newPath
555 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
556 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
558 if (getContentProviderClient() != null
) {
560 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
562 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
563 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
564 } catch (RemoteException e
) {
565 Log_OC
.e(TAG
, e
.getMessage());
568 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
570 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
571 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
574 /// 2. prepare a batch of update operations to change all the descendants
575 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
576 int lengthOfOldPath
= folder
.getRemotePath().length();
577 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
578 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
579 if (c
.moveToFirst()) {
581 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
582 OCFile child
= createFileInstance(c
);
583 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
584 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
585 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
587 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
589 withSelection( ProviderTableMeta
._ID
+ "=?",
590 new String
[] { String
.valueOf(child
.getFileId()) })
592 } while (c
.moveToNext());
596 /// 3. apply updates in batch
598 if (getContentResolver() != null
) {
599 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
602 getContentProviderClient().applyBatch(operations
);
605 } catch (OperationApplicationException e
) {
606 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
608 } catch (RemoteException e
) {
609 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
616 //public void moveFolder(OCFile folder, String newPath) {
617 public void moveLocalFile(OCFile file
, String targetPath
) {
618 // TODO check newPath
619 if (file
!= null
&& file
.fileExists()) {
621 if (//file.isFolder() && // should work for regular files!!
622 !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
624 /// 0. move in local file system
626 /// 1. get all the descendants in a single QUERY (including the folder)
628 if (getContentProviderClient() != null
) {
630 c
= getContentProviderClient().query(
631 ProviderTableMeta
.CONTENT_URI
,
633 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
634 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
637 file
.getRemotePath() + "%"
639 ProviderTableMeta
.FILE_PATH
+ " ASC "
641 } catch (RemoteException e
) {
642 Log_OC
.e(TAG
, e
.getMessage());
646 c
= getContentResolver().query(
647 ProviderTableMeta
.CONTENT_URI
,
649 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
650 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
653 file
.getRemotePath() + "%"
655 ProviderTableMeta
.FILE_PATH
+ " ASC "
659 /// 2. prepare a batch of update operations to change all the descendants
660 ArrayList
<ContentProviderOperation
> operations
=
661 new ArrayList
<ContentProviderOperation
>(c
.getCount());
662 if (c
.moveToFirst()) {
663 int lengthOfOldPath
= file
.getRemotePath().length();
664 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
665 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
667 ContentValues cv
= new ContentValues(); // keep construction in the loop
668 OCFile child
= createFileInstance(c
);
670 ProviderTableMeta
.FILE_PATH
,
671 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
673 if (child
.getStoragePath() != null
&&
674 child
.getStoragePath().startsWith(defaultSavePath
)) {
675 // update link to downloaded content - but local move is not done here!
677 ProviderTableMeta
.FILE_STORAGE_PATH
,
678 defaultSavePath
+ targetPath
+
679 child
.getStoragePath().substring(lengthOfOldStoragePath
)
683 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
686 ProviderTableMeta
._ID
+ "=?",
687 new String
[] { String
.valueOf(child
.getFileId()) }
691 } while (c
.moveToNext());
695 /// 3. apply updates in batch
697 if (getContentResolver() != null
) {
698 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
701 getContentProviderClient().applyBatch(operations
);
704 } catch (Exception e
) {
707 "Fail to update " + file
.getFileId() + " and descendants in database",
716 private void saveLocalDirectory() {
717 * TODO implement local movement of folder
718 getStorageManager().moveFolder(mFile, mNewPath);
719 String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
720 File localDir = new File(localPath);
721 if (localDir.exists()) {
722 localDir.renameTo(new File(FileStorageUtils.getSavePath(mAccount.name) + mNewPath));
723 // TODO - if renameTo fails, children files that are already down will result unlinked
729 private void saveLocalFile() {
730 mFile.setRFileName(mNewName); <<< NO >
732 // try to move the local copy of the file
733 if (mFile.isDown()) {
734 File f = new File(mFile.getStoragePath());
735 String parentStoragePath = f.getParent();
736 if (!parentStoragePath.endsWith(File.separator))
737 parentStoragePath += File.separator;
738 if (f.renameTo(new File())) {
739 mFile.setStoragePath(parentStoragePath + mNewName);
741 // else - NOTHING: the link to the local file is kept although the local name can't be updated
742 // TODO - study conditions when this could be a problem
745 getStorageManager().saveFile(mFile);
752 private Vector
<OCFile
> getFolderContent(long parentId
) {
754 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
756 Uri req_uri
= Uri
.withAppendedPath(
757 ProviderTableMeta
.CONTENT_URI_DIR
,
758 String
.valueOf(parentId
));
761 if (getContentProviderClient() != null
) {
763 c
= getContentProviderClient().query(req_uri
, null
,
764 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
765 new String
[] { String
.valueOf(parentId
)}, null
);
766 } catch (RemoteException e
) {
767 Log_OC
.e(TAG
, e
.getMessage());
771 c
= getContentResolver().query(req_uri
, null
,
772 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
773 new String
[] { String
.valueOf(parentId
)}, null
);
776 if (c
.moveToFirst()) {
778 OCFile child
= createFileInstance(c
);
780 } while (c
.moveToNext());
785 Collections
.sort(ret
);
791 private OCFile
createRootDir() {
792 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
793 file
.setMimetype("DIR");
794 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
799 private boolean fileExists(String cmp_key
, String value
) {
801 if (getContentResolver() != null
) {
802 c
= getContentResolver()
803 .query(ProviderTableMeta
.CONTENT_URI
,
806 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
808 new String
[] { value
, mAccount
.name
}, null
);
811 c
= getContentProviderClient().query(
812 ProviderTableMeta
.CONTENT_URI
,
815 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
816 new String
[] { value
, mAccount
.name
}, null
);
817 } catch (RemoteException e
) {
819 "Couldn't determine file existance, assuming non existance: "
824 boolean retval
= c
.moveToFirst();
829 private Cursor
getCursorForValue(String key
, String value
) {
831 if (getContentResolver() != null
) {
832 c
= getContentResolver()
833 .query(ProviderTableMeta
.CONTENT_URI
,
836 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
838 new String
[] { value
, mAccount
.name
}, null
);
841 c
= getContentProviderClient().query(
842 ProviderTableMeta
.CONTENT_URI
,
844 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
845 + "=?", new String
[] { value
, mAccount
.name
},
847 } catch (RemoteException e
) {
848 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
856 private OCFile
createFileInstance(Cursor c
) {
859 file
= new OCFile(c
.getString(c
860 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
861 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
862 file
.setParentId(c
.getLong(c
863 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
864 file
.setMimetype(c
.getString(c
865 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
866 if (!file
.isFolder()) {
867 file
.setStoragePath(c
.getString(c
868 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
869 if (file
.getStoragePath() == null
) {
870 // 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
871 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
873 file
.setStoragePath(f
.getAbsolutePath());
874 file
.setLastSyncDateForData(f
.lastModified());
878 file
.setFileLength(c
.getLong(c
879 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
880 file
.setCreationTimestamp(c
.getLong(c
881 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
882 file
.setModificationTimestamp(c
.getLong(c
883 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
884 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
885 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
886 file
.setLastSyncDateForProperties(c
.getLong(c
887 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
888 file
.setLastSyncDateForData(c
.getLong(c
.
889 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
890 file
.setKeepInSync(c
.getInt(
891 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
892 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
893 file
.setShareByLink(c
.getInt(
894 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
895 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
896 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
897 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
904 * Returns if the file/folder is shared by link or not
905 * @param path Path of the file/folder
908 public boolean isShareByLink(String path
) {
909 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
911 if (c
.moveToFirst()) {
912 file
= createFileInstance(c
);
915 return file
.isShareByLink();
919 * Returns the public link of the file/folder
920 * @param path Path of the file/folder
923 public String
getPublicLink(String path
) {
924 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
926 if (c
.moveToFirst()) {
927 file
= createFileInstance(c
);
930 return file
.getPublicLink();
934 // Methods for Shares
935 public boolean saveShare(OCShare share
) {
936 boolean overriden
= false
;
937 ContentValues cv
= new ContentValues();
938 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
939 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
940 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
941 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
942 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
943 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
944 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
945 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
946 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
947 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
948 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
949 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
950 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
951 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
953 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
956 if (getContentResolver() != null
) {
957 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
958 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
959 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
962 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
963 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
964 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
965 } catch (RemoteException e
) {
967 "Fail to insert insert file to database "
972 Uri result_uri
= null
;
973 if (getContentResolver() != null
) {
974 result_uri
= getContentResolver().insert(
975 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
978 result_uri
= getContentProviderClient().insert(
979 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
980 } catch (RemoteException e
) {
982 "Fail to insert insert file to database "
986 if (result_uri
!= null
) {
987 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
997 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
999 if (getContentResolver() != null
) {
1000 c
= getContentResolver().query(
1001 ProviderTableMeta
.CONTENT_URI_SHARE
,
1003 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1004 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1005 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1006 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1010 c
= getContentProviderClient().query(
1011 ProviderTableMeta
.CONTENT_URI_SHARE
,
1013 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
1014 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
1015 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1016 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
1019 } catch (RemoteException e
) {
1020 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1024 OCShare share
= null
;
1025 if (c
.moveToFirst()) {
1026 share
= createShareInstance(c
);
1032 private OCShare
createShareInstance(Cursor c
) {
1033 OCShare share
= null
;
1035 share
= new OCShare(c
.getString(c
1036 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1037 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1038 share
.setFileSource(c
.getLong(c
1039 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1040 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1041 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1042 share
.setPermissions(c
.getInt(c
1043 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1044 share
.setSharedDate(c
.getLong(c
1045 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1046 share
.setExpirationDate(c
.getLong(c
1047 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1048 share
.setToken(c
.getString(c
1049 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1050 share
.setSharedWithDisplayName(c
.getString(c
1051 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1052 share
.setIsFolder(c
.getInt(
1053 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
1054 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1055 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
1061 private boolean shareExists(String cmp_key
, String value
) {
1063 if (getContentResolver() != null
) {
1064 c
= getContentResolver()
1065 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1068 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1070 new String
[] { value
, mAccount
.name
}, null
);
1073 c
= getContentProviderClient().query(
1074 ProviderTableMeta
.CONTENT_URI_SHARE
,
1077 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1078 new String
[] { value
, mAccount
.name
}, null
);
1079 } catch (RemoteException e
) {
1081 "Couldn't determine file existance, assuming non existance: "
1086 boolean retval
= c
.moveToFirst();
1091 private boolean shareExists(long remoteId
) {
1092 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1095 private void cleanSharedFiles() {
1096 ContentValues cv
= new ContentValues();
1097 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1098 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1099 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1100 String
[] whereArgs
= new String
[]{mAccount
.name
};
1102 if (getContentResolver() != null
) {
1103 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1107 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1109 } catch (RemoteException e
) {
1110 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1115 private void cleanSharedFilesInFolder(OCFile folder
) {
1116 ContentValues cv
= new ContentValues();
1117 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1118 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1119 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
1120 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1122 if (getContentResolver() != null
) {
1123 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1127 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1129 } catch (RemoteException e
) {
1130 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1135 private void cleanShares() {
1136 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1137 String
[] whereArgs
= new String
[]{mAccount
.name
};
1139 if (getContentResolver() != null
) {
1140 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1144 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1146 } catch (RemoteException e
) {
1147 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1152 public void saveShares(Collection
<OCShare
> shares
) {
1154 if (shares
!= null
) {
1155 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1157 // prepare operations to insert or update files to save in the given folder
1158 for (OCShare share
: shares
) {
1159 ContentValues cv
= new ContentValues();
1160 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1161 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1162 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1163 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1164 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1165 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1166 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1167 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1168 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1169 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1170 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1171 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1172 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1173 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1175 if (shareExists(share
.getIdRemoteShared())) {
1176 // updating an existing file
1177 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1179 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1180 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1184 // adding a new file
1185 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1189 // apply operations in batch
1190 if (operations
.size() > 0) {
1191 @SuppressWarnings("unused")
1192 ContentProviderResult
[] results
= null
;
1193 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1195 if (getContentResolver() != null
) {
1196 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1199 results
= getContentProviderClient().applyBatch(operations
);
1202 } catch (OperationApplicationException e
) {
1203 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1205 } catch (RemoteException e
) {
1206 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1213 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1216 if (sharedFiles
!= null
) {
1217 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1219 // prepare operations to insert or update files to save in the given folder
1220 for (OCFile file
: sharedFiles
) {
1221 ContentValues cv
= new ContentValues();
1222 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1223 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1224 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1225 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1226 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1227 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1228 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1229 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1230 if (!file
.isFolder()) {
1231 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1233 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1234 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1235 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1236 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1237 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1238 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1239 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1240 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1241 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1243 boolean existsByPath
= fileExists(file
.getRemotePath());
1244 if (existsByPath
|| fileExists(file
.getFileId())) {
1245 // updating an existing file
1246 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1248 withSelection( ProviderTableMeta
._ID
+ "=?",
1249 new String
[] { String
.valueOf(file
.getFileId()) })
1253 // adding a new file
1254 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1258 // apply operations in batch
1259 if (operations
.size() > 0) {
1260 @SuppressWarnings("unused")
1261 ContentProviderResult
[] results
= null
;
1262 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1264 if (getContentResolver() != null
) {
1265 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1268 results
= getContentProviderClient().applyBatch(operations
);
1271 } catch (OperationApplicationException e
) {
1272 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1274 } catch (RemoteException e
) {
1275 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1282 public void removeShare(OCShare share
){
1283 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1284 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1285 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1286 if (getContentProviderClient() != null
) {
1288 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1289 } catch (RemoteException e
) {
1290 e
.printStackTrace();
1293 getContentResolver().delete(share_uri
, where
, whereArgs
);
1297 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1300 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1302 for (OCShare share
: shares
) {
1304 String path
= share
.getPath();
1305 if (share
.isFolder()) {
1306 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1309 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1310 OCFile file
= getFileByPath(path
);
1312 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1313 file
.setShareByLink(true
);
1314 sharedFiles
.add(file
);
1319 updateSharedFiles(sharedFiles
);
1323 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1324 cleanSharedFilesInFolder(folder
);
1325 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1326 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1328 if (shares
!= null
) {
1329 // prepare operations to insert or update files to save in the given folder
1330 for (OCShare share
: shares
) {
1331 ContentValues cv
= new ContentValues();
1332 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1333 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1334 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1335 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1336 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1337 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1338 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1339 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1340 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1341 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1342 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1343 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1344 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1345 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1348 if (shareExists(share.getIdRemoteShared())) {
1349 // updating an existing share resource
1350 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1352 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1353 new String[] { String.valueOf(share.getIdRemoteShared()) })
1358 // adding a new share resource
1359 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1364 // apply operations in batch
1365 if (operations
.size() > 0) {
1366 @SuppressWarnings("unused")
1367 ContentProviderResult
[] results
= null
;
1368 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1370 if (getContentResolver() != null
) {
1371 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1374 results
= getContentProviderClient().applyBatch(operations
);
1377 } catch (OperationApplicationException e
) {
1378 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1380 } catch (RemoteException e
) {
1381 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1388 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1389 if (folder
!= null
) {
1390 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1391 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1393 Vector
<OCFile
> files
= getFolderContent(folder
);
1395 for (OCFile file
: files
) {
1396 whereArgs
[0] = file
.getRemotePath();
1397 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1398 .withSelection(where
, whereArgs
)
1402 return preparedOperations
;
1405 if (operations.size() > 0) {
1407 if (getContentResolver() != null) {
1408 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1411 getContentProviderClient().applyBatch(operations);
1414 } catch (OperationApplicationException e) {
1415 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1417 } catch (RemoteException e) {
1418 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1424 if (getContentResolver() != null) {
1426 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1431 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1435 } catch (RemoteException e) {
1436 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());