2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.datamodel
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Collection
;
26 import java
.util
.Collections
;
27 import java
.util
.HashSet
;
28 import java
.util
.Iterator
;
29 import java
.util
.List
;
31 import java
.util
.Vector
;
33 import android
.accounts
.Account
;
34 import android
.content
.ContentProviderClient
;
35 import android
.content
.ContentProviderOperation
;
36 import android
.content
.ContentProviderResult
;
37 import android
.content
.ContentResolver
;
38 import android
.content
.ContentUris
;
39 import android
.content
.ContentValues
;
40 import android
.content
.Intent
;
41 import android
.content
.OperationApplicationException
;
42 import android
.database
.Cursor
;
43 import android
.net
.Uri
;
44 import android
.os
.RemoteException
;
45 import android
.provider
.MediaStore
;
47 import com
.owncloud
.android
.MainApp
;
48 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
49 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
50 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
51 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
52 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
53 import com
.owncloud
.android
.lib
.resources
.status
.CapabilityBooleanType
;
54 import com
.owncloud
.android
.lib
.resources
.status
.OCCapability
;
55 import com
.owncloud
.android
.utils
.FileStorageUtils
;
57 import java
.io
.FileInputStream
;
58 import java
.io
.FileOutputStream
;
59 import java
.io
.IOException
;
60 import java
.io
.InputStream
;
61 import java
.io
.OutputStream
;
63 public class FileDataStorageManager
{
65 public static final int ROOT_PARENT_ID
= 0;
67 private ContentResolver mContentResolver
;
68 private ContentProviderClient mContentProviderClient
;
69 private Account mAccount
;
71 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
74 public FileDataStorageManager(Account account
, ContentResolver cr
) {
75 mContentProviderClient
= null
;
76 mContentResolver
= cr
;
80 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
81 mContentProviderClient
= cp
;
82 mContentResolver
= null
;
87 public void setAccount(Account account
) {
91 public Account
getAccount() {
95 public ContentResolver
getContentResolver() {
96 return mContentResolver
;
99 public ContentProviderClient
getContentProviderClient() {
100 return mContentProviderClient
;
104 public OCFile
getFileByPath(String path
) {
105 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
107 if (c
.moveToFirst()) {
108 file
= createFileInstance(c
);
111 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
112 return createRootDir(); // root should always exist
118 public OCFile
getFileById(long id
) {
119 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
121 if (c
.moveToFirst()) {
122 file
= createFileInstance(c
);
128 public OCFile
getFileByLocalPath(String path
) {
129 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
131 if (c
.moveToFirst()) {
132 file
= createFileInstance(c
);
138 public boolean fileExists(long id
) {
139 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
142 public boolean fileExists(String path
) {
143 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
147 public Vector
<OCFile
> getFolderContent(OCFile f
/*, boolean onlyOnDevice*/) {
148 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
149 // TODO Enable when "On Device" is recovered ?
150 return getFolderContent(f
.getFileId()/*, onlyOnDevice*/);
153 return new Vector
<OCFile
>();
158 public Vector
<OCFile
> getFolderImages(OCFile folder
/*, boolean onlyOnDevice*/) {
159 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
160 if (folder
!= null
) {
161 // TODO better implementation, filtering in the access to database instead of here
162 // TODO Enable when "On Device" is recovered ?
163 Vector
<OCFile
> tmp
= getFolderContent(folder
/*, onlyOnDevice*/);
164 OCFile current
= null
;
165 for (int i
=0; i
<tmp
.size(); i
++) {
166 current
= tmp
.get(i
);
167 if (current
.isImage()) {
175 public boolean saveFile(OCFile file
) {
176 boolean overriden
= false
;
177 ContentValues cv
= new ContentValues();
178 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
180 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
181 file
.getModificationTimestampAtLastSyncForData()
183 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
184 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
185 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
186 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
187 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
188 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
189 if (!file
.isFolder())
190 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
191 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
192 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
193 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
194 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
195 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
196 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, file
.isSharedViaLink() ?
1 : 0);
197 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, file
.isSharedWithSharee() ?
1 : 0);
198 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
199 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
200 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
201 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
202 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
203 cv
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
, file
.getEtagInConflict());
205 boolean sameRemotePath
= fileExists(file
.getRemotePath());
206 if (sameRemotePath
||
207 fileExists(file
.getFileId())) { // for renamed files; no more delete and create
210 if (sameRemotePath
) {
211 oldFile
= getFileByPath(file
.getRemotePath());
212 file
.setFileId(oldFile
.getFileId());
214 oldFile
= getFileById(file
.getFileId());
218 if (getContentResolver() != null
) {
219 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
220 ProviderTableMeta
._ID
+ "=?",
221 new String
[]{String
.valueOf(file
.getFileId())});
224 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
225 cv
, ProviderTableMeta
._ID
+ "=?",
226 new String
[]{String
.valueOf(file
.getFileId())});
227 } catch (RemoteException e
) {
229 "Fail to insert insert file to database "
234 Uri result_uri
= null
;
235 if (getContentResolver() != null
) {
236 result_uri
= getContentResolver().insert(
237 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
240 result_uri
= getContentProviderClient().insert(
241 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
242 } catch (RemoteException e
) {
244 "Fail to insert insert file to database "
248 if (result_uri
!= null
) {
249 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
251 file
.setFileId(new_id
);
260 * Inserts or updates the list of files contained in a given folder.
262 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
263 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
266 * @param updatedFiles
267 * @param filesToRemove
269 public void saveFolder(
270 OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
273 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size()
274 + " children and " + filesToRemove
.size() + " files to remove");
276 ArrayList
<ContentProviderOperation
> operations
=
277 new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
279 // prepare operations to insert or update files to save in the given folder
280 for (OCFile file
: updatedFiles
) {
281 ContentValues cv
= new ContentValues();
282 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
284 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
285 file
.getModificationTimestampAtLastSyncForData()
287 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
288 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
289 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
290 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
291 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
292 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
293 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
294 if (!file
.isFolder()) {
295 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
297 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
298 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
299 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
300 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
301 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
302 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, file
.isSharedViaLink() ?
1 : 0);
303 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, file
.isSharedWithSharee() ?
1 : 0);
304 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
305 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
306 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
307 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
308 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
309 cv
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
, file
.getEtagInConflict());
311 boolean existsByPath
= fileExists(file
.getRemotePath());
312 if (existsByPath
|| fileExists(file
.getFileId())) {
313 // updating an existing file
314 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
316 withSelection(ProviderTableMeta
._ID
+ "=?",
317 new String
[]{String
.valueOf(file
.getFileId())})
322 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
323 withValues(cv
).build());
327 // prepare operations to remove files in the given folder
328 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
329 ProviderTableMeta
.FILE_PATH
+ "=?";
330 String
[] whereArgs
= null
;
331 for (OCFile file
: filesToRemove
) {
332 if (file
.getParentId() == folder
.getFileId()) {
333 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
334 if (file
.isFolder()) {
335 operations
.add(ContentProviderOperation
.newDelete(
336 ContentUris
.withAppendedId(
337 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
339 ).withSelection(where
, whereArgs
).build());
342 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
343 if (localFolder
.exists()) {
344 removeLocalFolder(localFolder
);
347 operations
.add(ContentProviderOperation
.newDelete(
348 ContentUris
.withAppendedId(
349 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
351 ).withSelection(where
, whereArgs
).build());
354 String path
= file
.getStoragePath();
355 new File(path
).delete();
356 triggerMediaScan(path
); // notify MediaScanner about removed file
362 // update metadata of folder
363 ContentValues cv
= new ContentValues();
364 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
366 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
367 folder
.getModificationTimestampAtLastSyncForData()
369 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
370 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
371 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
372 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
373 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
374 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
375 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
376 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
377 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
378 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.isFavorite() ?
1 : 0);
379 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
380 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, folder
.isSharedViaLink() ?
1 : 0);
381 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, folder
.isSharedWithSharee() ?
1 : 0);
382 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
383 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
384 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
386 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
388 withSelection(ProviderTableMeta
._ID
+ "=?",
389 new String
[]{String
.valueOf(folder
.getFileId())})
392 // apply operations in batch
393 ContentProviderResult
[] results
= null
;
394 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
396 if (getContentResolver() != null
) {
397 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
400 results
= getContentProviderClient().applyBatch(operations
);
403 } catch (OperationApplicationException e
) {
404 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
406 } catch (RemoteException e
) {
407 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
410 // update new id in file objects for insertions
411 if (results
!= null
) {
413 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
415 for (int i
= 0; i
< results
.length
; i
++) {
416 if (filesIt
.hasNext()) {
417 file
= filesIt
.next();
421 if (results
[i
].uri
!= null
) {
422 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
423 //updatedFiles.get(i).setFileId(newId);
425 file
.setFileId(newId
);
434 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
435 boolean success
= true
;
437 if (file
.isFolder()) {
438 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
442 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE,
443 // ""+file.getFileId());
444 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
,
446 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
447 ProviderTableMeta
.FILE_PATH
+ "=?";
448 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
450 if (getContentProviderClient() != null
) {
452 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
453 } catch (RemoteException e
) {
457 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
459 success
&= (deleted
> 0);
461 String localPath
= file
.getStoragePath();
462 if (removeLocalCopy
&& file
.isDown() && localPath
!= null
&& success
) {
463 success
= new File(localPath
).delete();
465 deleteFileInMediaScan(localPath
);
467 if (!removeDBData
&& success
) {
468 // maybe unnecessary, but should be checked TODO remove if unnecessary
469 file
.setStoragePath(null
);
471 saveConflict(file
, null
);
480 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
481 boolean success
= true
;
482 if (folder
!= null
&& folder
.isFolder()) {
483 if (removeDBData
&& folder
.getFileId() != -1) {
484 success
= removeFolderInDb(folder
);
486 if (removeLocalContent
&& success
) {
487 success
= removeLocalFolder(folder
);
493 private boolean removeFolderInDb(OCFile folder
) {
494 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
495 folder
.getFileId()); // URI for recursive deletion
496 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
497 ProviderTableMeta
.FILE_PATH
+ "=?";
498 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
500 if (getContentProviderClient() != null
) {
502 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
503 } catch (RemoteException e
) {
507 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
512 private boolean removeLocalFolder(OCFile folder
) {
513 boolean success
= true
;
514 String localFolderPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
);
515 File localFolder
= new File(localFolderPath
);
516 if (localFolder
.exists()) {
517 // stage 1: remove the local files already registered in the files database
518 // TODO Enable when "On Device" is recovered ?
519 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId()/*, false*/);
521 for (OCFile file
: files
) {
522 if (file
.isFolder()) {
523 success
&= removeLocalFolder(file
);
526 File localFile
= new File(file
.getStoragePath());
527 success
&= localFile
.delete();
529 // notify MediaScanner about removed file
530 deleteFileInMediaScan(file
.getStoragePath());
531 file
.setStoragePath(null
);
539 // stage 2: remove the folder itself and any local file inside out of sync;
540 // for instance, after clearing the app cache or reinstalling
541 success
&= removeLocalFolder(localFolder
);
546 private boolean removeLocalFolder(File localFolder
) {
547 boolean success
= true
;
548 File
[] localFiles
= localFolder
.listFiles();
549 if (localFiles
!= null
) {
550 for (File localFile
: localFiles
) {
551 if (localFile
.isDirectory()) {
552 success
&= removeLocalFolder(localFile
);
554 String path
= localFile
.getAbsolutePath();
555 success
&= localFile
.delete();
559 success
&= localFolder
.delete();
565 * Updates database and file system for a file or folder that was moved to a different location.
567 * TODO explore better (faster) implementations
568 * TODO throw exceptions up !
570 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
572 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
574 OCFile targetParent
= getFileByPath(targetParentPath
);
575 if (targetParent
== null
) {
576 throw new IllegalStateException(
577 "Parent folder of the target path does not exist!!");
580 /// 1. get all the descendants of the moved element in a single QUERY
582 if (getContentProviderClient() != null
) {
584 c
= getContentProviderClient().query(
585 ProviderTableMeta
.CONTENT_URI
,
587 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
588 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
591 file
.getRemotePath() + "%"
593 ProviderTableMeta
.FILE_PATH
+ " ASC "
595 } catch (RemoteException e
) {
596 Log_OC
.e(TAG
, e
.getMessage());
600 c
= getContentResolver().query(
601 ProviderTableMeta
.CONTENT_URI
,
603 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
604 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
607 file
.getRemotePath() + "%"
609 ProviderTableMeta
.FILE_PATH
+ " ASC "
613 /// 2. prepare a batch of update operations to change all the descendants
614 ArrayList
<ContentProviderOperation
> operations
=
615 new ArrayList
<ContentProviderOperation
>(c
.getCount());
616 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
617 List
<String
> originalPathsToTriggerMediaScan
= new ArrayList
<String
>();
618 List
<String
> newPathsToTriggerMediaScan
= new ArrayList
<String
>();
619 if (c
.moveToFirst()) {
620 int lengthOfOldPath
= file
.getRemotePath().length();
621 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
623 ContentValues cv
= new ContentValues(); // keep construction in the loop
624 OCFile child
= createFileInstance(c
);
626 ProviderTableMeta
.FILE_PATH
,
627 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
629 if (child
.getStoragePath() != null
&&
630 child
.getStoragePath().startsWith(defaultSavePath
)) {
631 // update link to downloaded content - but local move is not done here!
632 String targetLocalPath
= defaultSavePath
+ targetPath
+
633 child
.getStoragePath().substring(lengthOfOldStoragePath
);
635 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, targetLocalPath
);
637 originalPathsToTriggerMediaScan
.add(child
.getStoragePath());
638 newPathsToTriggerMediaScan
.add(targetLocalPath
);
641 if (child
.getRemotePath().equals(file
.getRemotePath())) {
643 ProviderTableMeta
.FILE_PARENT
,
644 targetParent
.getFileId()
648 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
651 ProviderTableMeta
._ID
+ "=?",
652 new String
[]{String
.valueOf(child
.getFileId())}
656 } while (c
.moveToNext());
660 /// 3. apply updates in batch
662 if (getContentResolver() != null
) {
663 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
666 getContentProviderClient().applyBatch(operations
);
669 } catch (Exception e
) {
670 Log_OC
.e(TAG
, "Fail to update " + file
.getFileId() + " and descendants in database",
674 /// 4. move in local file system
675 String originalLocalPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
676 String targetLocalPath
= defaultSavePath
+ targetPath
;
677 File localFile
= new File(originalLocalPath
);
678 boolean renamed
= false
;
679 if (localFile
.exists()) {
680 File targetFile
= new File(targetLocalPath
);
681 File targetFolder
= targetFile
.getParentFile();
682 if (!targetFolder
.exists()) {
683 targetFolder
.mkdirs();
685 renamed
= localFile
.renameTo(targetFile
);
689 Iterator
<String
> it
= originalPathsToTriggerMediaScan
.iterator();
690 while (it
.hasNext()) {
691 // Notify MediaScanner about removed file
692 deleteFileInMediaScan(it
.next());
694 it
= newPathsToTriggerMediaScan
.iterator();
695 while (it
.hasNext()) {
696 // Notify MediaScanner about new file/folder
697 triggerMediaScan(it
.next());
704 public void copyLocalFile(OCFile file
, String targetPath
) {
706 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
707 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
708 File localFile
= new File(localPath
);
709 boolean copied
= false
;
710 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
711 if (localFile
.exists()) {
712 File targetFile
= new File(defaultSavePath
+ targetPath
);
713 File targetFolder
= targetFile
.getParentFile();
714 if (!targetFolder
.exists()) {
715 targetFolder
.mkdirs();
717 copied
= copyFile(localFile
, targetFile
);
719 Log_OC
.d(TAG
, "Local file COPIED : " + copied
);
723 private boolean copyFile(File src
, File target
) {
726 InputStream
in = null
;
727 OutputStream out
= null
;
730 in = new FileInputStream(src
);
731 out
= new FileOutputStream(target
);
732 byte[] buf
= new byte[1024];
734 while ((len
= in.read(buf
)) > 0) {
735 out
.write(buf
, 0, len
);
737 } catch (IOException ex
) {
740 if (in != null
) try {
742 } catch (IOException e
) {
743 e
.printStackTrace(System
.err
);
745 if (out
!= null
) try {
747 } catch (IOException e
) {
748 e
.printStackTrace(System
.err
);
756 private Vector
<OCFile
> getFolderContent(long parentId
/*, boolean onlyOnDevice*/) {
758 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
760 Uri req_uri
= Uri
.withAppendedPath(
761 ProviderTableMeta
.CONTENT_URI_DIR
,
762 String
.valueOf(parentId
));
765 if (getContentProviderClient() != null
) {
767 c
= getContentProviderClient().query(req_uri
, null
,
768 ProviderTableMeta
.FILE_PARENT
+ "=?",
769 new String
[]{String
.valueOf(parentId
)}, null
);
770 } catch (RemoteException e
) {
771 Log_OC
.e(TAG
, e
.getMessage());
775 c
= getContentResolver().query(req_uri
, null
,
776 ProviderTableMeta
.FILE_PARENT
+ "=?",
777 new String
[]{String
.valueOf(parentId
)}, null
);
780 if (c
.moveToFirst()) {
782 OCFile child
= createFileInstance(c
);
783 // TODO Enable when "On Device" is recovered ?
784 // if (child.isFolder() || !onlyOnDevice || onlyOnDevice && child.isDown()){
787 } while (c
.moveToNext());
792 Collections
.sort(ret
);
798 private OCFile
createRootDir() {
799 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
800 file
.setMimetype("DIR");
801 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
806 private boolean fileExists(String cmp_key
, String value
) {
808 if (getContentResolver() != null
) {
809 c
= getContentResolver()
810 .query(ProviderTableMeta
.CONTENT_URI
,
813 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
815 new String
[]{value
, mAccount
.name
}, null
);
818 c
= getContentProviderClient().query(
819 ProviderTableMeta
.CONTENT_URI
,
822 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
823 new String
[]{value
, mAccount
.name
}, null
);
824 } catch (RemoteException e
) {
826 "Couldn't determine file existance, assuming non existance: "
831 boolean retval
= c
.moveToFirst();
836 private Cursor
getCursorForValue(String key
, String value
) {
838 if (getContentResolver() != null
) {
839 c
= getContentResolver()
840 .query(ProviderTableMeta
.CONTENT_URI
,
843 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
845 new String
[]{value
, mAccount
.name
}, null
);
848 c
= getContentProviderClient().query(
849 ProviderTableMeta
.CONTENT_URI
,
851 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
852 + "=?", new String
[]{value
, mAccount
.name
},
854 } catch (RemoteException e
) {
855 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
863 private OCFile
createFileInstance(Cursor c
) {
866 file
= new OCFile(c
.getString(c
867 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
868 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
869 file
.setParentId(c
.getLong(c
870 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
871 file
.setMimetype(c
.getString(c
872 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
873 if (!file
.isFolder()) {
874 file
.setStoragePath(c
.getString(c
875 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
876 if (file
.getStoragePath() == null
) {
877 // try to find existing file and bind it with current account;
878 // with the current update of SynchronizeFolderOperation, this won't be
879 // necessary anymore after a full synchronization of the account
880 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
882 file
.setStoragePath(f
.getAbsolutePath());
883 file
.setLastSyncDateForData(f
.lastModified());
887 file
.setFileLength(c
.getLong(c
888 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
889 file
.setCreationTimestamp(c
.getLong(c
890 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
891 file
.setModificationTimestamp(c
.getLong(c
892 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
893 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
894 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
895 file
.setLastSyncDateForProperties(c
.getLong(c
896 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
897 file
.setLastSyncDateForData(c
.getLong(c
.
898 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
899 file
.setFavorite(c
.getInt(
900 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
901 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
902 file
.setShareViaLink(c
.getInt(
903 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARED_VIA_LINK
)) == 1 ? true
: false
);
904 file
.setShareWithSharee(c
.getInt(
905 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
)) == 1 ? true
: false
);
906 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
907 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
908 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
909 file
.setNeedsUpdateThumbnail(c
.getInt(
910 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
911 file
.setDownloading(c
.getInt(
912 c
.getColumnIndex(ProviderTableMeta
.FILE_IS_DOWNLOADING
)) == 1 ? true
: false
);
913 file
.setEtagInConflict(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
)));
919 // Methods for Shares
920 public boolean saveShare(OCShare share
) {
921 boolean overriden
= false
;
922 ContentValues cv
= new ContentValues();
923 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
924 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
925 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
926 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
927 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
928 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
929 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
930 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
931 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
933 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
934 share
.getSharedWithDisplayName()
936 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
937 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
938 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
939 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
941 if (shareExists(share
.getIdRemoteShared())) {// for renamed files; no more delete and create
943 if (getContentResolver() != null
) {
944 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
945 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
946 new String
[]{String
.valueOf(share
.getIdRemoteShared())});
949 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
950 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
951 new String
[]{String
.valueOf(share
.getIdRemoteShared())});
952 } catch (RemoteException e
) {
954 "Fail to insert insert file to database "
959 Uri result_uri
= null
;
960 if (getContentResolver() != null
) {
961 result_uri
= getContentResolver().insert(
962 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
965 result_uri
= getContentProviderClient().insert(
966 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
967 } catch (RemoteException e
) {
969 "Fail to insert insert file to database "
973 if (result_uri
!= null
) {
974 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
984 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
, String shareWith
) {
987 String selection
= ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
988 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
989 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ "=? AND "
990 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" ;
992 String
[] selectionArgs
= new String
[]{path
, Integer
.toString(type
.getValue()),
993 shareWith
, mAccount
.name
};
995 if (getContentResolver() != null
) {
996 c
= getContentResolver().query(
997 ProviderTableMeta
.CONTENT_URI_SHARE
,
999 selection
, selectionArgs
,
1003 c
= getContentProviderClient().query(
1004 ProviderTableMeta
.CONTENT_URI_SHARE
,
1006 selection
, selectionArgs
,
1009 } catch (RemoteException e
) {
1010 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1014 OCShare share
= null
;
1015 if (c
.moveToFirst()) {
1016 share
= createShareInstance(c
);
1022 private OCShare
createShareInstance(Cursor c
) {
1023 OCShare share
= null
;
1025 share
= new OCShare(c
.getString(c
1026 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1027 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1028 share
.setFileSource(c
.getLong(c
1029 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1030 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1031 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1032 share
.setShareWith(c
.getString(c
1033 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH
)));
1034 share
.setPermissions(c
.getInt(c
1035 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1036 share
.setSharedDate(c
.getLong(c
1037 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1038 share
.setExpirationDate(c
.getLong(c
1039 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1040 share
.setToken(c
.getString(c
1041 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1042 share
.setSharedWithDisplayName(c
.getString(c
1043 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1044 share
.setIsFolder(c
.getInt(
1045 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1);
1046 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1047 share
.setIdRemoteShared(c
.getLong(
1048 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
1053 private boolean shareExists(String cmp_key
, String value
) {
1055 if (getContentResolver() != null
) {
1056 c
= getContentResolver()
1057 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1060 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1062 new String
[]{value
, mAccount
.name
}, null
);
1065 c
= getContentProviderClient().query(
1066 ProviderTableMeta
.CONTENT_URI_SHARE
,
1069 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1070 new String
[]{value
, mAccount
.name
}, null
);
1071 } catch (RemoteException e
) {
1073 "Couldn't determine file existance, assuming non existance: "
1078 boolean retval
= c
.moveToFirst();
1083 private boolean shareExists(long remoteId
) {
1084 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1087 private void resetShareFlagsInAllFiles() {
1088 ContentValues cv
= new ContentValues();
1089 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, false
);
1090 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, false
);
1091 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1092 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1093 String
[] whereArgs
= new String
[]{mAccount
.name
};
1095 if (getContentResolver() != null
) {
1096 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1100 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
,
1102 } catch (RemoteException e
) {
1103 Log_OC
.e(TAG
, "Exception in resetShareFlagsInAllFiles" + e
.getMessage());
1108 private void resetShareFlagsInFolder(OCFile folder
) {
1109 ContentValues cv
= new ContentValues();
1110 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, false
);
1111 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, false
);
1112 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1113 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1114 ProviderTableMeta
.FILE_PARENT
+ "=?";
1115 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1117 if (getContentResolver() != null
) {
1118 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1122 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
,
1124 } catch (RemoteException e
) {
1125 Log_OC
.e(TAG
, "Exception in resetShareFlagsInFolder " + e
.getMessage());
1130 private void resetShareFlagInAFile(String filePath
){
1131 ContentValues cv
= new ContentValues();
1132 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, false
);
1133 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, false
);
1134 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1135 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1136 ProviderTableMeta
.FILE_PATH
+ "=?";
1137 String
[] whereArgs
= new String
[] { mAccount
.name
, filePath
};
1139 if (getContentResolver() != null
) {
1140 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1144 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
,
1146 } catch (RemoteException e
) {
1147 Log_OC
.e(TAG
, "Exception in resetShareFlagsInFolder " + e
.getMessage());
1152 private void cleanShares() {
1153 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1154 String
[] whereArgs
= new String
[]{mAccount
.name
};
1156 if (getContentResolver() != null
) {
1157 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1161 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
,
1163 } catch (RemoteException e
) {
1164 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1169 public void saveShares(Collection
<OCShare
> shares
) {
1171 if (shares
!= null
) {
1172 ArrayList
<ContentProviderOperation
> operations
=
1173 new ArrayList
<ContentProviderOperation
>(shares
.size());
1175 // prepare operations to insert or update files to save in the given folder
1176 for (OCShare share
: shares
) {
1177 ContentValues cv
= new ContentValues();
1178 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1179 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1180 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1181 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1182 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1183 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1184 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1185 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1186 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1188 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1189 share
.getSharedWithDisplayName()
1191 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1192 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1193 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1194 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1196 if (shareExists(share
.getIdRemoteShared())) {
1197 // updating an existing file
1199 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1201 withSelection(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1202 new String
[]{String
.valueOf(share
.getIdRemoteShared())})
1205 // adding a new file
1207 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1214 // apply operations in batch
1215 if (operations
.size() > 0) {
1216 @SuppressWarnings("unused")
1217 ContentProviderResult
[] results
= null
;
1218 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1219 " operations to FileContentProvider");
1221 if (getContentResolver() != null
) {
1222 results
= getContentResolver().applyBatch(MainApp
.getAuthority(),
1225 results
= getContentProviderClient().applyBatch(operations
);
1228 } catch (OperationApplicationException e
) {
1229 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1231 } catch (RemoteException e
) {
1232 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1239 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1240 resetShareFlagsInAllFiles();
1242 if (sharedFiles
!= null
) {
1243 ArrayList
<ContentProviderOperation
> operations
=
1244 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1246 // prepare operations to insert or update files to save in the given folder
1247 for (OCFile file
: sharedFiles
) {
1248 ContentValues cv
= new ContentValues();
1249 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1251 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1252 file
.getModificationTimestampAtLastSyncForData()
1254 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1255 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1256 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1257 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1258 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1259 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1260 if (!file
.isFolder()) {
1261 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1263 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1264 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1266 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1267 file
.getLastSyncDateForData()
1269 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
1270 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1271 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, file
.isSharedViaLink() ?
1 : 0);
1272 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, file
.isSharedWithSharee() ?
1 : 0);
1273 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1274 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1275 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1277 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1278 file
.needsUpdateThumbnail() ?
1 : 0
1281 ProviderTableMeta
.FILE_IS_DOWNLOADING
,
1282 file
.isDownloading() ?
1 : 0
1284 cv
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
, file
.getEtagInConflict());
1286 boolean existsByPath
= fileExists(file
.getRemotePath());
1287 if (existsByPath
|| fileExists(file
.getFileId())) {
1288 // updating an existing file
1290 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1292 withSelection(ProviderTableMeta
._ID
+ "=?",
1293 new String
[]{String
.valueOf(file
.getFileId())})
1297 // adding a new file
1299 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1306 // apply operations in batch
1307 if (operations
.size() > 0) {
1308 @SuppressWarnings("unused")
1309 ContentProviderResult
[] results
= null
;
1310 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1311 " operations to FileContentProvider");
1313 if (getContentResolver() != null
) {
1314 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1316 results
= getContentProviderClient().applyBatch(operations
);
1319 } catch (OperationApplicationException e
) {
1320 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1322 } catch (RemoteException e
) {
1323 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1330 public void removeShare(OCShare share
) {
1331 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1332 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1333 ProviderTableMeta
._ID
+ "=?";
1334 String
[] whereArgs
= new String
[]{mAccount
.name
, Long
.toString(share
.getId())};
1335 if (getContentProviderClient() != null
) {
1337 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1338 } catch (RemoteException e
) {
1339 e
.printStackTrace();
1342 getContentResolver().delete(share_uri
, where
, whereArgs
);
1346 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1347 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1349 // Reset flags & Remove shares for this files
1350 String filePath
= "";
1351 for (OCShare share
: shares
) {
1352 if (filePath
!= share
.getPath()){
1353 filePath
= share
.getPath();
1354 resetShareFlagInAFile(filePath
);
1355 operations
= prepareRemoveSharesInFile(filePath
, operations
);
1359 // Add operations to insert shares
1360 operations
= prepareInsertShares(shares
, operations
);
1362 // apply operations in batch
1363 if (operations
.size() > 0) {
1364 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1366 if (getContentResolver() != null
) {
1367 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1370 getContentProviderClient().applyBatch(operations
);
1373 } catch (OperationApplicationException e
) {
1374 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1376 } catch (RemoteException e
) {
1377 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1381 // // TODO: review if it is needed
1382 // // Update shared files
1383 // ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
1385 // for (OCShare share : shares) {
1387 // String path = share.getPath();
1388 // if (share.isFolder()) {
1389 // path = path + FileUtils.PATH_SEPARATOR;
1392 // // Update OCFile with data from share: ShareByLink, publicLink and
1393 // OCFile file = getFileByPath(path);
1394 // if (file != null) {
1395 // if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
1396 // file.setShareViaLink(true);
1397 // sharedFiles.add(file);
1403 // updateSharedFiles(sharedFiles);
1407 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1408 resetShareFlagsInFolder(folder
);
1409 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1410 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1412 if (shares
!= null
) {
1413 // prepare operations to insert or update files to save in the given folder
1414 operations
= prepareInsertShares(shares
, operations
);
1417 // apply operations in batch
1418 if (operations
.size() > 0) {
1419 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1421 if (getContentResolver() != null
) {
1422 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1425 getContentProviderClient().applyBatch(operations
);
1428 } catch (OperationApplicationException e
) {
1429 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1431 } catch (RemoteException e
) {
1439 * Prepare operations to insert or update files to save in the given folder
1440 * @param shares List of shares to insert
1441 * @param operations List of operations
1444 private ArrayList
<ContentProviderOperation
> prepareInsertShares(
1445 ArrayList
<OCShare
> shares
, ArrayList
<ContentProviderOperation
> operations
) {
1447 if (shares
!= null
) {
1448 // prepare operations to insert or update files to save in the given folder
1449 for (OCShare share
: shares
) {
1450 ContentValues cv
= new ContentValues();
1451 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1452 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1453 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1454 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1455 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1456 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1457 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1458 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1459 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1461 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1462 share
.getSharedWithDisplayName()
1464 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1465 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1466 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1467 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1469 // adding a new share resource
1471 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1480 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1481 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1482 if (folder
!= null
) {
1483 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1484 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1485 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1487 // TODO Enable when "On Device" is recovered ?
1488 Vector
<OCFile
> files
= getFolderContent(folder
/*, false*/);
1490 for (OCFile file
: files
) {
1491 whereArgs
[0] = file
.getRemotePath();
1492 preparedOperations
.add(
1493 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1494 withSelection(where
, whereArgs
).
1499 return preparedOperations
;
1503 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFile(
1504 String filePath
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1506 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1507 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1508 String
[] whereArgs
= new String
[]{filePath
, mAccount
.name
};
1510 preparedOperations
.add(
1511 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1512 withSelection(where
, whereArgs
).
1516 return preparedOperations
;
1520 public ArrayList
<OCShare
> getSharesWithForAFile(String filePath
, String accountName
){
1522 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1523 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?"+ "AND"
1524 + " (" + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? OR "
1525 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? ) ";
1526 String
[] whereArgs
= new String
[]{ filePath
, accountName
,
1527 Integer
.toString(ShareType
.USER
.getValue()),
1528 Integer
.toString(ShareType
.GROUP
.getValue()) };
1531 if (getContentResolver() != null
) {
1532 c
= getContentResolver().query(
1533 ProviderTableMeta
.CONTENT_URI_SHARE
,
1534 null
, where
, whereArgs
, null
);
1537 c
= getContentProviderClient().query(
1538 ProviderTableMeta
.CONTENT_URI_SHARE
,
1539 null
, where
, whereArgs
, null
);
1541 } catch (RemoteException e
) {
1542 Log_OC
.e(TAG
, "Could not get list of shares with: " + e
.getMessage());
1546 ArrayList
<OCShare
> shares
= new ArrayList
<OCShare
>();
1547 OCShare share
= null
;
1548 if (c
.moveToFirst()) {
1550 share
= createShareInstance(c
);
1553 } while (c
.moveToNext());
1560 public void triggerMediaScan(String path
) {
1561 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
1562 intent
.setData(Uri
.fromFile(new File(path
)));
1563 MainApp
.getAppContext().sendBroadcast(intent
);
1566 public void deleteFileInMediaScan(String path
) {
1568 String mimetypeString
= FileStorageUtils
.getMimeTypeFromName(path
);
1569 ContentResolver contentResolver
= getContentResolver();
1571 if (contentResolver
!= null
) {
1572 if (mimetypeString
.startsWith("image/")) {
1574 contentResolver
.delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1575 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1576 } else if (mimetypeString
.startsWith("audio/")) {
1578 contentResolver
.delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1579 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1580 } else if (mimetypeString
.startsWith("video/")) {
1582 contentResolver
.delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1583 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});
1586 ContentProviderClient contentProviderClient
= getContentProviderClient();
1588 if (mimetypeString
.startsWith("image/")) {
1590 contentProviderClient
.delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1591 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1592 } else if (mimetypeString
.startsWith("audio/")) {
1594 contentProviderClient
.delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1595 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1596 } else if (mimetypeString
.startsWith("video/")) {
1598 contentProviderClient
.delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1599 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});
1601 } catch (RemoteException e
) {
1602 Log_OC
.e(TAG
, "Exception deleting media file in MediaStore " + e
.getMessage());
1608 public void saveConflict(OCFile file
, String etagInConflict
) {
1609 if (!file
.isDown()) {
1610 etagInConflict
= null
;
1612 ContentValues cv
= new ContentValues();
1613 cv
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
, etagInConflict
);
1615 if (getContentResolver() != null
) {
1616 updated
= getContentResolver().update(
1617 ProviderTableMeta
.CONTENT_URI_FILE
,
1619 ProviderTableMeta
._ID
+ "=?",
1620 new String
[] { String
.valueOf(file
.getFileId())}
1624 updated
= getContentProviderClient().update(
1625 ProviderTableMeta
.CONTENT_URI_FILE
,
1627 ProviderTableMeta
._ID
+ "=?",
1628 new String
[]{String
.valueOf(file
.getFileId())}
1630 } catch (RemoteException e
) {
1631 Log_OC
.e(TAG
, "Failed saving conflict in database " + e
.getMessage());
1635 Log_OC
.d(TAG
, "Number of files updated with CONFLICT: " + updated
);
1638 if (etagInConflict
!= null
) {
1639 /// set conflict in all ancestor folders
1641 long parentId
= file
.getParentId();
1642 Set
<String
> ancestorIds
= new HashSet
<String
>();
1643 while (parentId
!= FileDataStorageManager
.ROOT_PARENT_ID
) {
1644 ancestorIds
.add(Long
.toString(parentId
));
1645 parentId
= getFileById(parentId
).getParentId();
1648 if (ancestorIds
.size() > 0) {
1649 StringBuffer whereBuffer
= new StringBuffer();
1650 whereBuffer
.append(ProviderTableMeta
._ID
).append(" IN (");
1651 for (int i
= 0; i
< ancestorIds
.size() - 1; i
++) {
1652 whereBuffer
.append("?,");
1654 whereBuffer
.append("?");
1655 whereBuffer
.append(")");
1657 if (getContentResolver() != null
) {
1658 updated
= getContentResolver().update(
1659 ProviderTableMeta
.CONTENT_URI_FILE
,
1661 whereBuffer
.toString(),
1662 ancestorIds
.toArray(new String
[]{})
1666 updated
= getContentProviderClient().update(
1667 ProviderTableMeta
.CONTENT_URI_FILE
,
1669 whereBuffer
.toString(),
1670 ancestorIds
.toArray(new String
[]{})
1672 } catch (RemoteException e
) {
1673 Log_OC
.e(TAG
, "Failed saving conflict in database " + e
.getMessage());
1676 } // else file is ROOT folder, no parent to set in conflict
1679 /// update conflict in ancestor folders
1680 // (not directly unset; maybe there are more conflicts below them)
1681 String parentPath
= file
.getRemotePath();
1682 if (parentPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
1683 parentPath
= parentPath
.substring(0, parentPath
.length() - 1);
1685 parentPath
= parentPath
.substring(0, parentPath
.lastIndexOf(OCFile
.PATH_SEPARATOR
) + 1);
1687 Log_OC
.d(TAG
, "checking parents to remove conflict; STARTING with " + parentPath
);
1688 while (parentPath
.length() > 0) {
1691 ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
+ " IS NOT NULL AND " +
1692 ProviderTableMeta
.FILE_CONTENT_TYPE
+ " != 'DIR' AND " +
1693 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " = ? AND " +
1694 ProviderTableMeta
.FILE_PATH
+ " LIKE ?";
1695 Cursor descendentsInConflict
= getContentResolver().query(
1696 ProviderTableMeta
.CONTENT_URI_FILE
,
1697 new String
[]{ProviderTableMeta
._ID
},
1699 new String
[]{mAccount
.name
, parentPath
+ "%"},
1702 if (descendentsInConflict
== null
|| descendentsInConflict
.getCount() == 0) {
1703 Log_OC
.d(TAG
, "NO MORE conflicts in " + parentPath
);
1704 if (getContentResolver() != null
) {
1705 updated
= getContentResolver().update(
1706 ProviderTableMeta
.CONTENT_URI_FILE
,
1708 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1709 ProviderTableMeta
.FILE_PATH
+ "=?",
1710 new String
[]{mAccount
.name
, parentPath
}
1714 updated
= getContentProviderClient().update(
1715 ProviderTableMeta
.CONTENT_URI_FILE
,
1717 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1718 ProviderTableMeta
.FILE_PATH
+ "=?"
1719 , new String
[]{mAccount
.name
, parentPath
}
1721 } catch (RemoteException e
) {
1722 Log_OC
.e(TAG
, "Failed saving conflict in database " + e
.getMessage());
1727 Log_OC
.d(TAG
, "STILL " + descendentsInConflict
.getCount() + " in " + parentPath
);
1730 if (descendentsInConflict
!= null
) {
1731 descendentsInConflict
.close();
1734 parentPath
= parentPath
.substring(0, parentPath
.length() - 1); // trim last /
1735 parentPath
= parentPath
.substring(0, parentPath
.lastIndexOf(OCFile
.PATH_SEPARATOR
) + 1);
1736 Log_OC
.d(TAG
, "checking parents to remove conflict; NEXT " + parentPath
);
1743 public OCCapability
saveCapabilities(OCCapability capability
){
1745 // Prepare capabilities data
1746 ContentValues cv
= new ContentValues();
1747 cv
.put(ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
, mAccount
.name
);
1748 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_MAYOR
, capability
.getVersionMayor());
1749 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_MINOR
, capability
.getVersionMinor());
1750 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_MICRO
, capability
.getVersionMicro());
1751 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_STRING
, capability
.getVersionString());
1752 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_EDITION
, capability
.getVersionEdition());
1753 cv
.put(ProviderTableMeta
.CAPABILITIES_CORE_POLLINTERVAL
, capability
.getCorePollinterval());
1754 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_API_ENABLED
, capability
.getFilesSharingApiEnabled().getValue());
1755 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_ENABLED
,
1756 capability
.getFilesSharingPublicEnabled().getValue());
1757 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED
,
1758 capability
.getFilesSharingPublicPasswordEnforced().getValue());
1759 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED
,
1760 capability
.getFilesSharingPublicExpireDateEnabled().getValue());
1761 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS
,
1762 capability
.getFilesSharingPublicExpireDateDays());
1763 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED
,
1764 capability
.getFilesSharingPublicExpireDateEnforced().getValue());
1765 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL
,
1766 capability
.getFilesSharingPublicSendMail().getValue());
1767 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_UPLOAD
,
1768 capability
.getFilesSharingPublicUpload().getValue());
1769 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_USER_SEND_MAIL
,
1770 capability
.getFilesSharingUserSendMail().getValue());
1771 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_RESHARING
, capability
.getFilesSharingResharing().getValue());
1772 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_FEDERATION_OUTGOING
,
1773 capability
.getFilesSharingFederationOutgoing().getValue());
1774 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_FEDERATION_INCOMING
,
1775 capability
.getFilesSharingFederationIncoming().getValue());
1776 cv
.put(ProviderTableMeta
.CAPABILITIES_FILES_BIGFILECHUNKING
, capability
.getFilesBigFileChuncking().getValue());
1777 cv
.put(ProviderTableMeta
.CAPABILITIES_FILES_UNDELETE
, capability
.getFilesUndelete().getValue());
1778 cv
.put(ProviderTableMeta
.CAPABILITIES_FILES_VERSIONING
, capability
.getFilesVersioning().getValue());
1780 if (capabilityExists(mAccount
.name
)) {
1781 if (getContentResolver() != null
) {
1782 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_CAPABILITIES
, cv
,
1783 ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
+ "=?",
1784 new String
[]{mAccount
.name
});
1787 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_CAPABILITIES
,
1788 cv
, ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
+ "=?",
1789 new String
[]{mAccount
.name
});
1790 } catch (RemoteException e
) {
1792 "Fail to insert insert file to database "
1797 Uri result_uri
= null
;
1798 if (getContentResolver() != null
) {
1799 result_uri
= getContentResolver().insert(
1800 ProviderTableMeta
.CONTENT_URI_CAPABILITIES
, cv
);
1803 result_uri
= getContentProviderClient().insert(
1804 ProviderTableMeta
.CONTENT_URI_CAPABILITIES
, cv
);
1805 } catch (RemoteException e
) {
1807 "Fail to insert insert capability to database "
1811 if (result_uri
!= null
) {
1812 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
1814 capability
.setId(new_id
);
1815 capability
.setAccountName(mAccount
.name
);
1822 private boolean capabilityExists(String accountName
) {
1823 Cursor c
= getCapabilityCursorForAccount(accountName
);
1824 boolean exists
= false
;
1826 exists
= c
.moveToFirst();
1832 private Cursor
getCapabilityCursorForAccount(String accountName
){
1834 if (getContentResolver() != null
) {
1835 c
= getContentResolver()
1836 .query(ProviderTableMeta
.CONTENT_URI_CAPABILITIES
,
1838 ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
+ "=? ",
1839 new String
[]{accountName
}, null
);
1842 c
= getContentProviderClient().query(
1843 ProviderTableMeta
.CONTENT_URI_CAPABILITIES
,
1845 ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
+ "=? ",
1846 new String
[]{accountName
}, null
);
1847 } catch (RemoteException e
) {
1849 "Couldn't determine capability existance, assuming non existance: "
1857 public OCCapability
getCapability(String accountName
){
1858 OCCapability capability
= null
;
1859 Cursor c
= getCapabilityCursorForAccount(accountName
);
1861 if (c
.moveToFirst()) {
1862 capability
= createCapabilityInstance(c
);
1868 private OCCapability
createCapabilityInstance(Cursor c
) {
1869 OCCapability capability
= null
;
1871 capability
= new OCCapability();
1872 capability
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1873 capability
.setAccountName(c
.getString(c
1874 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
)));
1875 capability
.setVersionMayor(c
.getInt(c
1876 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_MAYOR
)));
1877 capability
.setVersionMinor(c
.getInt(c
1878 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_MINOR
)));
1879 capability
.setVersionMicro(c
.getInt(c
1880 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_MICRO
)));
1881 capability
.setVersionString(c
.getString(c
1882 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_STRING
)));
1883 capability
.setVersionEdition(c
.getString(c
1884 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_EDITION
)));
1885 capability
.setCorePollinterval(c
.getInt(c
1886 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_CORE_POLLINTERVAL
)));
1887 capability
.setFilesSharingApiEnabled(CapabilityBooleanType
.fromValue(c
.getInt(c
1888 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_API_ENABLED
))));
1889 capability
.setFilesSharingPublicEnabled(CapabilityBooleanType
.fromValue(c
.getInt(c
1890 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_ENABLED
))));
1891 capability
.setFilesSharingPublicPasswordEnforced(CapabilityBooleanType
.fromValue(c
.getInt(c
1892 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED
))));
1893 capability
.setFilesSharingPublicExpireDateEnabled(CapabilityBooleanType
.fromValue(c
.getInt(c
1894 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED
))));
1895 capability
.setFilesSharingPublicExpireDateDays(c
.getInt(c
1896 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS
)));
1897 capability
.setFilesSharingPublicExpireDateEnforced(CapabilityBooleanType
.fromValue(c
.getInt(c
1898 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED
))));
1899 capability
.setFilesSharingPublicSendMail(CapabilityBooleanType
.fromValue(c
.getInt(c
1900 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL
))));
1901 capability
.setFilesSharingPublicUpload(CapabilityBooleanType
.fromValue(c
.getInt(c
1902 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_UPLOAD
))));
1903 capability
.setFilesSharingUserSendMail(CapabilityBooleanType
.fromValue(c
.getInt(c
1904 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_USER_SEND_MAIL
))));
1905 capability
.setFilesSharingResharing(CapabilityBooleanType
.fromValue(c
.getInt(c
1906 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_RESHARING
))));
1907 capability
.setFilesSharingFederationOutgoing(CapabilityBooleanType
.fromValue(c
.getInt(c
1908 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_FEDERATION_OUTGOING
))));
1909 capability
.setFilesSharingFederationIncoming(CapabilityBooleanType
.fromValue(c
.getInt(c
1910 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_FEDERATION_INCOMING
))));
1911 capability
.setFilesBigFileChuncking(CapabilityBooleanType
.fromValue(c
.getInt(c
1912 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_FILES_BIGFILECHUNKING
))));
1913 capability
.setFilesUndelete(CapabilityBooleanType
.fromValue(c
.getInt(c
1914 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_FILES_UNDELETE
))));
1915 capability
.setFilesVersioning(CapabilityBooleanType
.fromValue(c
.getInt(c
1916 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_FILES_VERSIONING
))));