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
.shares
.OCShare
;
51 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
52 import com
.owncloud
.android
.lib
.resources
.status
.CapabilityBooleanType
;
53 import com
.owncloud
.android
.lib
.resources
.status
.OCCapability
;
54 import com
.owncloud
.android
.utils
.FileStorageUtils
;
56 import java
.io
.FileInputStream
;
57 import java
.io
.FileOutputStream
;
58 import java
.io
.IOException
;
59 import java
.io
.InputStream
;
60 import java
.io
.OutputStream
;
62 public class FileDataStorageManager
{
64 public static final int ROOT_PARENT_ID
= 0;
66 private ContentResolver mContentResolver
;
67 private ContentProviderClient mContentProviderClient
;
68 private Account mAccount
;
70 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
73 public FileDataStorageManager(Account account
, ContentResolver cr
) {
74 mContentProviderClient
= null
;
75 mContentResolver
= cr
;
79 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
80 mContentProviderClient
= cp
;
81 mContentResolver
= null
;
86 public void setAccount(Account account
) {
90 public Account
getAccount() {
94 public ContentResolver
getContentResolver() {
95 return mContentResolver
;
98 public ContentProviderClient
getContentProviderClient() {
99 return mContentProviderClient
;
103 public OCFile
getFileByPath(String path
) {
104 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
106 if (c
.moveToFirst()) {
107 file
= createFileInstance(c
);
110 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
111 return createRootDir(); // root should always exist
117 public OCFile
getFileById(long id
) {
118 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
120 if (c
.moveToFirst()) {
121 file
= createFileInstance(c
);
127 public OCFile
getFileByLocalPath(String path
) {
128 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
130 if (c
.moveToFirst()) {
131 file
= createFileInstance(c
);
137 public boolean fileExists(long id
) {
138 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
141 public boolean fileExists(String path
) {
142 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
146 public Vector
<OCFile
> getFolderContent(OCFile f
/*, boolean onlyOnDevice*/) {
147 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
148 // TODO Enable when "On Device" is recovered ?
149 return getFolderContent(f
.getFileId()/*, onlyOnDevice*/);
152 return new Vector
<OCFile
>();
157 public Vector
<OCFile
> getFolderImages(OCFile folder
/*, boolean onlyOnDevice*/) {
158 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
159 if (folder
!= null
) {
160 // TODO better implementation, filtering in the access to database instead of here
161 // TODO Enable when "On Device" is recovered ?
162 Vector
<OCFile
> tmp
= getFolderContent(folder
/*, onlyOnDevice*/);
163 OCFile current
= null
;
164 for (int i
=0; i
<tmp
.size(); i
++) {
165 current
= tmp
.get(i
);
166 if (current
.isImage()) {
174 public boolean saveFile(OCFile file
) {
175 boolean overriden
= false
;
176 ContentValues cv
= new ContentValues();
177 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
179 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
180 file
.getModificationTimestampAtLastSyncForData()
182 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
183 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
184 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
185 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
186 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
187 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
188 if (!file
.isFolder())
189 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
190 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
191 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
192 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
193 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
194 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
195 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, file
.isSharedViaLink() ?
1 : 0);
196 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, file
.isSharedWithSharee() ?
1 : 0);
197 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
198 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
199 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
200 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
201 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
202 cv
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
, file
.getEtagInConflict());
204 boolean sameRemotePath
= fileExists(file
.getRemotePath());
205 if (sameRemotePath
||
206 fileExists(file
.getFileId())) { // for renamed files; no more delete and create
209 if (sameRemotePath
) {
210 oldFile
= getFileByPath(file
.getRemotePath());
211 file
.setFileId(oldFile
.getFileId());
213 oldFile
= getFileById(file
.getFileId());
217 if (getContentResolver() != null
) {
218 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
219 ProviderTableMeta
._ID
+ "=?",
220 new String
[]{String
.valueOf(file
.getFileId())});
223 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
224 cv
, ProviderTableMeta
._ID
+ "=?",
225 new String
[]{String
.valueOf(file
.getFileId())});
226 } catch (RemoteException e
) {
228 "Fail to insert insert file to database "
233 Uri result_uri
= null
;
234 if (getContentResolver() != null
) {
235 result_uri
= getContentResolver().insert(
236 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
239 result_uri
= getContentProviderClient().insert(
240 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
241 } catch (RemoteException e
) {
243 "Fail to insert insert file to database "
247 if (result_uri
!= null
) {
248 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
250 file
.setFileId(new_id
);
259 * Inserts or updates the list of files contained in a given folder.
261 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
262 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
265 * @param updatedFiles
266 * @param filesToRemove
268 public void saveFolder(
269 OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
272 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size()
273 + " children and " + filesToRemove
.size() + " files to remove");
275 ArrayList
<ContentProviderOperation
> operations
=
276 new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
278 // prepare operations to insert or update files to save in the given folder
279 for (OCFile file
: updatedFiles
) {
280 ContentValues cv
= new ContentValues();
281 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
283 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
284 file
.getModificationTimestampAtLastSyncForData()
286 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
287 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
288 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
289 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
290 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
291 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
292 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
293 if (!file
.isFolder()) {
294 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
296 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
297 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
298 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
299 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
300 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
301 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, file
.isSharedViaLink() ?
1 : 0);
302 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, file
.isSharedWithSharee() ?
1 : 0);
303 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
304 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
305 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
306 cv
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
, file
.needsUpdateThumbnail());
307 cv
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
, file
.isDownloading());
308 cv
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
, file
.getEtagInConflict());
310 boolean existsByPath
= fileExists(file
.getRemotePath());
311 if (existsByPath
|| fileExists(file
.getFileId())) {
312 // updating an existing file
313 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
315 withSelection(ProviderTableMeta
._ID
+ "=?",
316 new String
[]{String
.valueOf(file
.getFileId())})
321 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
322 withValues(cv
).build());
326 // prepare operations to remove files in the given folder
327 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
328 ProviderTableMeta
.FILE_PATH
+ "=?";
329 String
[] whereArgs
= null
;
330 for (OCFile file
: filesToRemove
) {
331 if (file
.getParentId() == folder
.getFileId()) {
332 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
333 if (file
.isFolder()) {
334 operations
.add(ContentProviderOperation
.newDelete(
335 ContentUris
.withAppendedId(
336 ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId()
338 ).withSelection(where
, whereArgs
).build());
341 new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
342 if (localFolder
.exists()) {
343 removeLocalFolder(localFolder
);
346 operations
.add(ContentProviderOperation
.newDelete(
347 ContentUris
.withAppendedId(
348 ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId()
350 ).withSelection(where
, whereArgs
).build());
353 String path
= file
.getStoragePath();
354 new File(path
).delete();
355 triggerMediaScan(path
); // notify MediaScanner about removed file
361 // update metadata of folder
362 ContentValues cv
= new ContentValues();
363 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
365 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
366 folder
.getModificationTimestampAtLastSyncForData()
368 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
369 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0);
370 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
371 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
372 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
373 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
374 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
375 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
376 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
377 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.isFavorite() ?
1 : 0);
378 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
379 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, folder
.isSharedViaLink() ?
1 : 0);
380 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, folder
.isSharedWithSharee() ?
1 : 0);
381 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
382 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, folder
.getPermissions());
383 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, folder
.getRemoteId());
385 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
387 withSelection(ProviderTableMeta
._ID
+ "=?",
388 new String
[]{String
.valueOf(folder
.getFileId())})
391 // apply operations in batch
392 ContentProviderResult
[] results
= null
;
393 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
395 if (getContentResolver() != null
) {
396 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
399 results
= getContentProviderClient().applyBatch(operations
);
402 } catch (OperationApplicationException e
) {
403 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
405 } catch (RemoteException e
) {
406 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
409 // update new id in file objects for insertions
410 if (results
!= null
) {
412 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
414 for (int i
= 0; i
< results
.length
; i
++) {
415 if (filesIt
.hasNext()) {
416 file
= filesIt
.next();
420 if (results
[i
].uri
!= null
) {
421 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
422 //updatedFiles.get(i).setFileId(newId);
424 file
.setFileId(newId
);
433 public boolean removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
434 boolean success
= true
;
436 if (file
.isFolder()) {
437 success
= removeFolder(file
, removeDBData
, removeLocalCopy
);
441 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE,
442 // ""+file.getFileId());
443 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
,
445 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
446 ProviderTableMeta
.FILE_PATH
+ "=?";
447 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
449 if (getContentProviderClient() != null
) {
451 deleted
= getContentProviderClient().delete(file_uri
, where
, whereArgs
);
452 } catch (RemoteException e
) {
456 deleted
= getContentResolver().delete(file_uri
, where
, whereArgs
);
458 success
&= (deleted
> 0);
460 String localPath
= file
.getStoragePath();
461 if (removeLocalCopy
&& file
.isDown() && localPath
!= null
&& success
) {
462 success
= new File(localPath
).delete();
464 deleteFileInMediaScan(localPath
);
466 if (!removeDBData
&& success
) {
467 // maybe unnecessary, but should be checked TODO remove if unnecessary
468 file
.setStoragePath(null
);
470 saveConflict(file
, null
);
479 public boolean removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
480 boolean success
= true
;
481 if (folder
!= null
&& folder
.isFolder()) {
482 if (removeDBData
&& folder
.getFileId() != -1) {
483 success
= removeFolderInDb(folder
);
485 if (removeLocalContent
&& success
) {
486 success
= removeLocalFolder(folder
);
492 private boolean removeFolderInDb(OCFile folder
) {
493 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, "" +
494 folder
.getFileId()); // URI for recursive deletion
495 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " +
496 ProviderTableMeta
.FILE_PATH
+ "=?";
497 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
499 if (getContentProviderClient() != null
) {
501 deleted
= getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
502 } catch (RemoteException e
) {
506 deleted
= getContentResolver().delete(folder_uri
, where
, whereArgs
);
511 private boolean removeLocalFolder(OCFile folder
) {
512 boolean success
= true
;
513 String localFolderPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
);
514 File localFolder
= new File(localFolderPath
);
515 if (localFolder
.exists()) {
516 // stage 1: remove the local files already registered in the files database
517 // TODO Enable when "On Device" is recovered ?
518 Vector
<OCFile
> files
= getFolderContent(folder
.getFileId()/*, false*/);
520 for (OCFile file
: files
) {
521 if (file
.isFolder()) {
522 success
&= removeLocalFolder(file
);
525 File localFile
= new File(file
.getStoragePath());
526 success
&= localFile
.delete();
528 // notify MediaScanner about removed file
529 deleteFileInMediaScan(file
.getStoragePath());
530 file
.setStoragePath(null
);
538 // stage 2: remove the folder itself and any local file inside out of sync;
539 // for instance, after clearing the app cache or reinstalling
540 success
&= removeLocalFolder(localFolder
);
545 private boolean removeLocalFolder(File localFolder
) {
546 boolean success
= true
;
547 File
[] localFiles
= localFolder
.listFiles();
548 if (localFiles
!= null
) {
549 for (File localFile
: localFiles
) {
550 if (localFile
.isDirectory()) {
551 success
&= removeLocalFolder(localFile
);
553 String path
= localFile
.getAbsolutePath();
554 success
&= localFile
.delete();
558 success
&= localFolder
.delete();
564 * Updates database and file system for a file or folder that was moved to a different location.
566 * TODO explore better (faster) implementations
567 * TODO throw exceptions up !
569 public void moveLocalFile(OCFile file
, String targetPath
, String targetParentPath
) {
571 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
573 OCFile targetParent
= getFileByPath(targetParentPath
);
574 if (targetParent
== null
) {
575 throw new IllegalStateException(
576 "Parent folder of the target path does not exist!!");
579 /// 1. get all the descendants of the moved element in a single QUERY
581 if (getContentProviderClient() != null
) {
583 c
= getContentProviderClient().query(
584 ProviderTableMeta
.CONTENT_URI
,
586 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
587 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
590 file
.getRemotePath() + "%"
592 ProviderTableMeta
.FILE_PATH
+ " ASC "
594 } catch (RemoteException e
) {
595 Log_OC
.e(TAG
, e
.getMessage());
599 c
= getContentResolver().query(
600 ProviderTableMeta
.CONTENT_URI
,
602 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
603 ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
606 file
.getRemotePath() + "%"
608 ProviderTableMeta
.FILE_PATH
+ " ASC "
612 /// 2. prepare a batch of update operations to change all the descendants
613 ArrayList
<ContentProviderOperation
> operations
=
614 new ArrayList
<ContentProviderOperation
>(c
.getCount());
615 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
616 List
<String
> originalPathsToTriggerMediaScan
= new ArrayList
<String
>();
617 List
<String
> newPathsToTriggerMediaScan
= new ArrayList
<String
>();
618 if (c
.moveToFirst()) {
619 int lengthOfOldPath
= file
.getRemotePath().length();
620 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
622 ContentValues cv
= new ContentValues(); // keep construction in the loop
623 OCFile child
= createFileInstance(c
);
625 ProviderTableMeta
.FILE_PATH
,
626 targetPath
+ child
.getRemotePath().substring(lengthOfOldPath
)
628 if (child
.getStoragePath() != null
&&
629 child
.getStoragePath().startsWith(defaultSavePath
)) {
630 // update link to downloaded content - but local move is not done here!
631 String targetLocalPath
= defaultSavePath
+ targetPath
+
632 child
.getStoragePath().substring(lengthOfOldStoragePath
);
634 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, targetLocalPath
);
636 originalPathsToTriggerMediaScan
.add(child
.getStoragePath());
637 newPathsToTriggerMediaScan
.add(targetLocalPath
);
640 if (child
.getRemotePath().equals(file
.getRemotePath())) {
642 ProviderTableMeta
.FILE_PARENT
,
643 targetParent
.getFileId()
647 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
650 ProviderTableMeta
._ID
+ "=?",
651 new String
[]{String
.valueOf(child
.getFileId())}
655 } while (c
.moveToNext());
659 /// 3. apply updates in batch
661 if (getContentResolver() != null
) {
662 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
665 getContentProviderClient().applyBatch(operations
);
668 } catch (Exception e
) {
669 Log_OC
.e(TAG
, "Fail to update " + file
.getFileId() + " and descendants in database",
673 /// 4. move in local file system
674 String originalLocalPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
675 String targetLocalPath
= defaultSavePath
+ targetPath
;
676 File localFile
= new File(originalLocalPath
);
677 boolean renamed
= false
;
678 if (localFile
.exists()) {
679 File targetFile
= new File(targetLocalPath
);
680 File targetFolder
= targetFile
.getParentFile();
681 if (!targetFolder
.exists()) {
682 targetFolder
.mkdirs();
684 renamed
= localFile
.renameTo(targetFile
);
688 Iterator
<String
> it
= originalPathsToTriggerMediaScan
.iterator();
689 while (it
.hasNext()) {
690 // Notify MediaScanner about removed file
691 deleteFileInMediaScan(it
.next());
693 it
= newPathsToTriggerMediaScan
.iterator();
694 while (it
.hasNext()) {
695 // Notify MediaScanner about new file/folder
696 triggerMediaScan(it
.next());
703 public void copyLocalFile(OCFile file
, String targetPath
) {
705 if (file
!= null
&& file
.fileExists() && !OCFile
.ROOT_PATH
.equals(file
.getFileName())) {
706 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
707 File localFile
= new File(localPath
);
708 boolean copied
= false
;
709 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
710 if (localFile
.exists()) {
711 File targetFile
= new File(defaultSavePath
+ targetPath
);
712 File targetFolder
= targetFile
.getParentFile();
713 if (!targetFolder
.exists()) {
714 targetFolder
.mkdirs();
716 copied
= copyFile(localFile
, targetFile
);
718 Log_OC
.d(TAG
, "Local file COPIED : " + copied
);
722 private boolean copyFile(File src
, File target
) {
725 InputStream
in = null
;
726 OutputStream out
= null
;
729 in = new FileInputStream(src
);
730 out
= new FileOutputStream(target
);
731 byte[] buf
= new byte[1024];
733 while ((len
= in.read(buf
)) > 0) {
734 out
.write(buf
, 0, len
);
736 } catch (IOException ex
) {
739 if (in != null
) try {
741 } catch (IOException e
) {
742 e
.printStackTrace(System
.err
);
744 if (out
!= null
) try {
746 } catch (IOException e
) {
747 e
.printStackTrace(System
.err
);
755 private Vector
<OCFile
> getFolderContent(long parentId
/*, boolean onlyOnDevice*/) {
757 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
759 Uri req_uri
= Uri
.withAppendedPath(
760 ProviderTableMeta
.CONTENT_URI_DIR
,
761 String
.valueOf(parentId
));
764 if (getContentProviderClient() != null
) {
766 c
= getContentProviderClient().query(req_uri
, null
,
767 ProviderTableMeta
.FILE_PARENT
+ "=?",
768 new String
[]{String
.valueOf(parentId
)}, null
);
769 } catch (RemoteException e
) {
770 Log_OC
.e(TAG
, e
.getMessage());
774 c
= getContentResolver().query(req_uri
, null
,
775 ProviderTableMeta
.FILE_PARENT
+ "=?",
776 new String
[]{String
.valueOf(parentId
)}, null
);
779 if (c
.moveToFirst()) {
781 OCFile child
= createFileInstance(c
);
782 // TODO Enable when "On Device" is recovered ?
783 // if (child.isFolder() || !onlyOnDevice || onlyOnDevice && child.isDown()){
786 } while (c
.moveToNext());
791 Collections
.sort(ret
);
797 private OCFile
createRootDir() {
798 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
799 file
.setMimetype("DIR");
800 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
805 private boolean fileExists(String cmp_key
, String value
) {
807 if (getContentResolver() != null
) {
808 c
= getContentResolver()
809 .query(ProviderTableMeta
.CONTENT_URI
,
812 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
814 new String
[]{value
, mAccount
.name
}, null
);
817 c
= getContentProviderClient().query(
818 ProviderTableMeta
.CONTENT_URI
,
821 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
822 new String
[]{value
, mAccount
.name
}, null
);
823 } catch (RemoteException e
) {
825 "Couldn't determine file existance, assuming non existance: "
830 boolean retval
= c
.moveToFirst();
835 private Cursor
getCursorForValue(String key
, String value
) {
837 if (getContentResolver() != null
) {
838 c
= getContentResolver()
839 .query(ProviderTableMeta
.CONTENT_URI
,
842 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
844 new String
[]{value
, mAccount
.name
}, null
);
847 c
= getContentProviderClient().query(
848 ProviderTableMeta
.CONTENT_URI
,
850 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
851 + "=?", new String
[]{value
, mAccount
.name
},
853 } catch (RemoteException e
) {
854 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
862 private OCFile
createFileInstance(Cursor c
) {
865 file
= new OCFile(c
.getString(c
866 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
867 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
868 file
.setParentId(c
.getLong(c
869 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
870 file
.setMimetype(c
.getString(c
871 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
872 if (!file
.isFolder()) {
873 file
.setStoragePath(c
.getString(c
874 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
875 if (file
.getStoragePath() == null
) {
876 // try to find existing file and bind it with current account;
877 // with the current update of SynchronizeFolderOperation, this won't be
878 // necessary anymore after a full synchronization of the account
879 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
881 file
.setStoragePath(f
.getAbsolutePath());
882 file
.setLastSyncDateForData(f
.lastModified());
886 file
.setFileLength(c
.getLong(c
887 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
888 file
.setCreationTimestamp(c
.getLong(c
889 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
890 file
.setModificationTimestamp(c
.getLong(c
891 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
892 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
893 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
894 file
.setLastSyncDateForProperties(c
.getLong(c
895 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
896 file
.setLastSyncDateForData(c
.getLong(c
.
897 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
898 file
.setFavorite(c
.getInt(
899 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
900 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
901 file
.setShareViaLink(c
.getInt(
902 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARED_VIA_LINK
)) == 1 ? true
: false
);
903 file
.setShareWithSharee(c
.getInt(
904 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
)) == 1 ? true
: false
);
905 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
906 file
.setPermissions(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PERMISSIONS
)));
907 file
.setRemoteId(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
)));
908 file
.setNeedsUpdateThumbnail(c
.getInt(
909 c
.getColumnIndex(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
)) == 1 ? true
: false
);
910 file
.setDownloading(c
.getInt(
911 c
.getColumnIndex(ProviderTableMeta
.FILE_IS_DOWNLOADING
)) == 1 ? true
: false
);
912 file
.setEtagInConflict(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
)));
918 // Methods for Shares
919 public boolean saveShare(OCShare share
) {
920 boolean overriden
= false
;
921 ContentValues cv
= new ContentValues();
922 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
923 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
924 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
925 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
926 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
927 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
928 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
929 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
930 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
932 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
933 share
.getSharedWithDisplayName()
935 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
936 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
937 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getRemoteId());
938 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
940 if (shareExists(share
.getRemoteId())) {// for renamed files; no more delete and create
942 if (getContentResolver() != null
) {
943 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
944 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
945 new String
[]{String
.valueOf(share
.getRemoteId())});
948 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
949 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
950 new String
[]{String
.valueOf(share
.getRemoteId())});
951 } catch (RemoteException e
) {
953 "Fail to insert insert file to database "
958 Uri result_uri
= null
;
959 if (getContentResolver() != null
) {
960 result_uri
= getContentResolver().insert(
961 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
964 result_uri
= getContentProviderClient().insert(
965 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
966 } catch (RemoteException e
) {
968 "Fail to insert insert file to database "
972 if (result_uri
!= null
) {
973 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
984 * Get first share bound to a file with a known path and given {@link ShareType}.
986 * @param path Path of the file.
987 * @param type Type of the share to get
988 * @param shareWith Target of the share. Ignored in type is {@link ShareType#PUBLIC_LINK}
989 * @return First {@OCShare} instance found in DB bound to the file in 'path'
991 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
, String shareWith
) {
993 if (shareWith
== null
) {
997 String selection
= ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
998 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
999 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" ;
1000 if (!ShareType
.PUBLIC_LINK
.equals(type
)) {
1001 selection
+= ProviderTableMeta
.OCSHARES_SHARE_WITH
+ "=? AND ";
1004 String
[] selectionArgs
;
1005 if (ShareType
.PUBLIC_LINK
.equals(type
)) {
1006 selectionArgs
= new String
[]{
1008 Integer
.toString(type
.getValue()),
1012 selectionArgs
= new String
[]{
1014 Integer
.toString(type
.getValue()),
1020 if (getContentResolver() != null
) {
1021 c
= getContentResolver().query(
1022 ProviderTableMeta
.CONTENT_URI_SHARE
,
1024 selection
, selectionArgs
,
1028 c
= getContentProviderClient().query(
1029 ProviderTableMeta
.CONTENT_URI_SHARE
,
1031 selection
, selectionArgs
,
1034 } catch (RemoteException e
) {
1035 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
1039 OCShare share
= null
;
1040 if (c
.moveToFirst()) {
1041 share
= createShareInstance(c
);
1047 private OCShare
createShareInstance(Cursor c
) {
1048 OCShare share
= null
;
1050 share
= new OCShare(c
.getString(c
1051 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
1052 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1053 share
.setFileSource(c
.getLong(c
1054 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
1055 share
.setShareType(ShareType
.fromValue(c
.getInt(c
1056 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
1057 share
.setShareWith(c
.getString(c
1058 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH
)));
1059 share
.setPermissions(c
.getInt(c
1060 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
1061 share
.setSharedDate(c
.getLong(c
1062 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
1063 share
.setExpirationDate(c
.getLong(c
1064 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
1065 share
.setToken(c
.getString(c
1066 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
1067 share
.setSharedWithDisplayName(c
.getString(c
1068 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
1069 share
.setIsFolder(c
.getInt(
1070 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1);
1071 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
1072 share
.setIdRemoteShared(c
.getLong(
1073 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
1078 private boolean shareExists(String cmp_key
, String value
) {
1080 if (getContentResolver() != null
) {
1081 c
= getContentResolver()
1082 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
1085 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
1087 new String
[]{value
, mAccount
.name
}, null
);
1090 c
= getContentProviderClient().query(
1091 ProviderTableMeta
.CONTENT_URI_SHARE
,
1094 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
1095 new String
[]{value
, mAccount
.name
}, null
);
1096 } catch (RemoteException e
) {
1098 "Couldn't determine file existance, assuming non existance: "
1103 boolean retval
= c
.moveToFirst();
1108 private boolean shareExists(long remoteId
) {
1109 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
1112 private void resetShareFlagsInAllFiles() {
1113 ContentValues cv
= new ContentValues();
1114 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, false
);
1115 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, false
);
1116 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1117 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
1118 String
[] whereArgs
= new String
[]{mAccount
.name
};
1120 if (getContentResolver() != null
) {
1121 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1125 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
,
1127 } catch (RemoteException e
) {
1128 Log_OC
.e(TAG
, "Exception in resetShareFlagsInAllFiles" + e
.getMessage());
1133 private void resetShareFlagsInFolder(OCFile folder
) {
1134 ContentValues cv
= new ContentValues();
1135 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, false
);
1136 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, false
);
1137 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1138 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1139 ProviderTableMeta
.FILE_PARENT
+ "=?";
1140 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1142 if (getContentResolver() != null
) {
1143 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1147 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
,
1149 } catch (RemoteException e
) {
1150 Log_OC
.e(TAG
, "Exception in resetShareFlagsInFolder " + e
.getMessage());
1155 private void resetShareFlagInAFile(String filePath
){
1156 ContentValues cv
= new ContentValues();
1157 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, false
);
1158 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, false
);
1159 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1160 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1161 ProviderTableMeta
.FILE_PATH
+ "=?";
1162 String
[] whereArgs
= new String
[] { mAccount
.name
, filePath
};
1164 if (getContentResolver() != null
) {
1165 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1169 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
,
1171 } catch (RemoteException e
) {
1172 Log_OC
.e(TAG
, "Exception in resetShareFlagsInFolder " + e
.getMessage());
1177 private void cleanShares() {
1178 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1179 String
[] whereArgs
= new String
[]{mAccount
.name
};
1181 if (getContentResolver() != null
) {
1182 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1186 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
,
1188 } catch (RemoteException e
) {
1189 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1194 public void saveShares(Collection
<OCShare
> shares
) {
1196 if (shares
!= null
) {
1197 ArrayList
<ContentProviderOperation
> operations
=
1198 new ArrayList
<ContentProviderOperation
>(shares
.size());
1200 // prepare operations to insert or update files to save in the given folder
1201 for (OCShare share
: shares
) {
1202 ContentValues cv
= new ContentValues();
1203 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1204 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1205 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1206 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1207 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1208 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1209 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1210 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1211 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1213 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1214 share
.getSharedWithDisplayName()
1216 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1217 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1218 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getRemoteId());
1219 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1221 if (shareExists(share
.getRemoteId())) {
1222 // updating an existing file
1224 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1226 withSelection(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1227 new String
[]{String
.valueOf(share
.getRemoteId())})
1230 // adding a new file
1232 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1239 // apply operations in batch
1240 if (operations
.size() > 0) {
1241 @SuppressWarnings("unused")
1242 ContentProviderResult
[] results
= null
;
1243 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1244 " operations to FileContentProvider");
1246 if (getContentResolver() != null
) {
1247 results
= getContentResolver().applyBatch(MainApp
.getAuthority(),
1250 results
= getContentProviderClient().applyBatch(operations
);
1253 } catch (OperationApplicationException e
) {
1254 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1256 } catch (RemoteException e
) {
1257 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1264 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1265 resetShareFlagsInAllFiles();
1267 if (sharedFiles
!= null
) {
1268 ArrayList
<ContentProviderOperation
> operations
=
1269 new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1271 // prepare operations to insert or update files to save in the given folder
1272 for (OCFile file
: sharedFiles
) {
1273 ContentValues cv
= new ContentValues();
1274 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1276 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
1277 file
.getModificationTimestampAtLastSyncForData()
1279 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1280 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1281 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1282 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1283 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1284 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1285 if (!file
.isFolder()) {
1286 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1288 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1289 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1291 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
1292 file
.getLastSyncDateForData()
1294 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.isFavorite() ?
1 : 0);
1295 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1296 cv
.put(ProviderTableMeta
.FILE_SHARED_VIA_LINK
, file
.isSharedViaLink() ?
1 : 0);
1297 cv
.put(ProviderTableMeta
.FILE_SHARED_WITH_SHAREE
, file
.isSharedWithSharee() ?
1 : 0);
1298 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1299 cv
.put(ProviderTableMeta
.FILE_PERMISSIONS
, file
.getPermissions());
1300 cv
.put(ProviderTableMeta
.FILE_REMOTE_ID
, file
.getRemoteId());
1302 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
1303 file
.needsUpdateThumbnail() ?
1 : 0
1306 ProviderTableMeta
.FILE_IS_DOWNLOADING
,
1307 file
.isDownloading() ?
1 : 0
1309 cv
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
, file
.getEtagInConflict());
1311 boolean existsByPath
= fileExists(file
.getRemotePath());
1312 if (existsByPath
|| fileExists(file
.getFileId())) {
1313 // updating an existing file
1315 ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1317 withSelection(ProviderTableMeta
._ID
+ "=?",
1318 new String
[]{String
.valueOf(file
.getFileId())})
1322 // adding a new file
1324 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).
1331 // apply operations in batch
1332 if (operations
.size() > 0) {
1333 @SuppressWarnings("unused")
1334 ContentProviderResult
[] results
= null
;
1335 Log_OC
.d(TAG
, "Sending " + operations
.size() +
1336 " operations to FileContentProvider");
1338 if (getContentResolver() != null
) {
1339 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1341 results
= getContentProviderClient().applyBatch(operations
);
1344 } catch (OperationApplicationException e
) {
1345 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1347 } catch (RemoteException e
) {
1348 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1355 public void removeShare(OCShare share
) {
1356 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1357 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " +
1358 ProviderTableMeta
._ID
+ "=?";
1359 String
[] whereArgs
= new String
[]{mAccount
.name
, Long
.toString(share
.getId())};
1360 if (getContentProviderClient() != null
) {
1362 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1363 } catch (RemoteException e
) {
1364 e
.printStackTrace();
1367 getContentResolver().delete(share_uri
, where
, whereArgs
);
1371 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1372 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1374 // Reset flags & Remove shares for this files
1375 String filePath
= "";
1376 for (OCShare share
: shares
) {
1377 if (filePath
!= share
.getPath()){
1378 filePath
= share
.getPath();
1379 resetShareFlagInAFile(filePath
);
1380 operations
= prepareRemoveSharesInFile(filePath
, operations
);
1384 // Add operations to insert shares
1385 operations
= prepareInsertShares(shares
, operations
);
1387 // apply operations in batch
1388 if (operations
.size() > 0) {
1389 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1391 if (getContentResolver() != null
) {
1392 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1395 getContentProviderClient().applyBatch(operations
);
1398 } catch (OperationApplicationException e
) {
1399 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1401 } catch (RemoteException e
) {
1402 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1406 // // TODO: review if it is needed
1407 // // Update shared files
1408 // ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
1410 // for (OCShare share : shares) {
1412 // String path = share.getPath();
1413 // if (share.isFolder()) {
1414 // path = path + FileUtils.PATH_SEPARATOR;
1417 // // Update OCFile with data from share: ShareByLink, publicLink and
1418 // OCFile file = getFileByPath(path);
1419 // if (file != null) {
1420 // if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
1421 // file.setShareViaLink(true);
1422 // sharedFiles.add(file);
1428 // updateSharedFiles(sharedFiles);
1432 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1433 resetShareFlagsInFolder(folder
);
1434 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1435 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1437 if (shares
!= null
) {
1438 // prepare operations to insert or update files to save in the given folder
1439 operations
= prepareInsertShares(shares
, operations
);
1442 // apply operations in batch
1443 if (operations
.size() > 0) {
1444 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1446 if (getContentResolver() != null
) {
1447 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1450 getContentProviderClient().applyBatch(operations
);
1453 } catch (OperationApplicationException e
) {
1454 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1456 } catch (RemoteException e
) {
1464 * Prepare operations to insert or update files to save in the given folder
1465 * @param shares List of shares to insert
1466 * @param operations List of operations
1469 private ArrayList
<ContentProviderOperation
> prepareInsertShares(
1470 ArrayList
<OCShare
> shares
, ArrayList
<ContentProviderOperation
> operations
) {
1472 if (shares
!= null
) {
1473 // prepare operations to insert or update files to save in the given folder
1474 for (OCShare share
: shares
) {
1475 ContentValues cv
= new ContentValues();
1476 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1477 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1478 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1479 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1480 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1481 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1482 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1483 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1484 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1486 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
1487 share
.getSharedWithDisplayName()
1489 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1490 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1491 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getRemoteId());
1492 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1494 // adding a new share resource
1496 ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).
1505 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(
1506 OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1507 if (folder
!= null
) {
1508 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1509 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1510 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1512 // TODO Enable when "On Device" is recovered ?
1513 Vector
<OCFile
> files
= getFolderContent(folder
/*, false*/);
1515 for (OCFile file
: files
) {
1516 whereArgs
[0] = file
.getRemotePath();
1517 preparedOperations
.add(
1518 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1519 withSelection(where
, whereArgs
).
1524 return preparedOperations
;
1528 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFile(
1529 String filePath
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1531 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1532 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1533 String
[] whereArgs
= new String
[]{filePath
, mAccount
.name
};
1535 preparedOperations
.add(
1536 ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
).
1537 withSelection(where
, whereArgs
).
1541 return preparedOperations
;
1545 public ArrayList
<OCShare
> getSharesWithForAFile(String filePath
, String accountName
){
1547 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND "
1548 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?"+ "AND"
1549 + " (" + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? OR "
1550 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? ) ";
1551 String
[] whereArgs
= new String
[]{ filePath
, accountName
,
1552 Integer
.toString(ShareType
.USER
.getValue()),
1553 Integer
.toString(ShareType
.GROUP
.getValue()) };
1556 if (getContentResolver() != null
) {
1557 c
= getContentResolver().query(
1558 ProviderTableMeta
.CONTENT_URI_SHARE
,
1559 null
, where
, whereArgs
, null
);
1562 c
= getContentProviderClient().query(
1563 ProviderTableMeta
.CONTENT_URI_SHARE
,
1564 null
, where
, whereArgs
, null
);
1566 } catch (RemoteException e
) {
1567 Log_OC
.e(TAG
, "Could not get list of shares with: " + e
.getMessage());
1571 ArrayList
<OCShare
> shares
= new ArrayList
<OCShare
>();
1572 OCShare share
= null
;
1573 if (c
.moveToFirst()) {
1575 share
= createShareInstance(c
);
1578 } while (c
.moveToNext());
1585 public void triggerMediaScan(String path
) {
1586 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
1587 intent
.setData(Uri
.fromFile(new File(path
)));
1588 MainApp
.getAppContext().sendBroadcast(intent
);
1591 public void deleteFileInMediaScan(String path
) {
1593 String mimetypeString
= FileStorageUtils
.getMimeTypeFromName(path
);
1594 ContentResolver contentResolver
= getContentResolver();
1596 if (contentResolver
!= null
) {
1597 if (mimetypeString
.startsWith("image/")) {
1599 contentResolver
.delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1600 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1601 } else if (mimetypeString
.startsWith("audio/")) {
1603 contentResolver
.delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1604 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1605 } else if (mimetypeString
.startsWith("video/")) {
1607 contentResolver
.delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1608 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});
1611 ContentProviderClient contentProviderClient
= getContentProviderClient();
1613 if (mimetypeString
.startsWith("image/")) {
1615 contentProviderClient
.delete(MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
,
1616 MediaStore
.Images
.Media
.DATA
+ "=?", new String
[]{path
});
1617 } else if (mimetypeString
.startsWith("audio/")) {
1619 contentProviderClient
.delete(MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
,
1620 MediaStore
.Audio
.Media
.DATA
+ "=?", new String
[]{path
});
1621 } else if (mimetypeString
.startsWith("video/")) {
1623 contentProviderClient
.delete(MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
,
1624 MediaStore
.Video
.Media
.DATA
+ "=?", new String
[]{path
});
1626 } catch (RemoteException e
) {
1627 Log_OC
.e(TAG
, "Exception deleting media file in MediaStore " + e
.getMessage());
1633 public void saveConflict(OCFile file
, String etagInConflict
) {
1634 if (!file
.isDown()) {
1635 etagInConflict
= null
;
1637 ContentValues cv
= new ContentValues();
1638 cv
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
, etagInConflict
);
1640 if (getContentResolver() != null
) {
1641 updated
= getContentResolver().update(
1642 ProviderTableMeta
.CONTENT_URI_FILE
,
1644 ProviderTableMeta
._ID
+ "=?",
1645 new String
[] { String
.valueOf(file
.getFileId())}
1649 updated
= getContentProviderClient().update(
1650 ProviderTableMeta
.CONTENT_URI_FILE
,
1652 ProviderTableMeta
._ID
+ "=?",
1653 new String
[]{String
.valueOf(file
.getFileId())}
1655 } catch (RemoteException e
) {
1656 Log_OC
.e(TAG
, "Failed saving conflict in database " + e
.getMessage());
1660 Log_OC
.d(TAG
, "Number of files updated with CONFLICT: " + updated
);
1663 if (etagInConflict
!= null
) {
1664 /// set conflict in all ancestor folders
1666 long parentId
= file
.getParentId();
1667 Set
<String
> ancestorIds
= new HashSet
<String
>();
1668 while (parentId
!= FileDataStorageManager
.ROOT_PARENT_ID
) {
1669 ancestorIds
.add(Long
.toString(parentId
));
1670 parentId
= getFileById(parentId
).getParentId();
1673 if (ancestorIds
.size() > 0) {
1674 StringBuffer whereBuffer
= new StringBuffer();
1675 whereBuffer
.append(ProviderTableMeta
._ID
).append(" IN (");
1676 for (int i
= 0; i
< ancestorIds
.size() - 1; i
++) {
1677 whereBuffer
.append("?,");
1679 whereBuffer
.append("?");
1680 whereBuffer
.append(")");
1682 if (getContentResolver() != null
) {
1683 updated
= getContentResolver().update(
1684 ProviderTableMeta
.CONTENT_URI_FILE
,
1686 whereBuffer
.toString(),
1687 ancestorIds
.toArray(new String
[]{})
1691 updated
= getContentProviderClient().update(
1692 ProviderTableMeta
.CONTENT_URI_FILE
,
1694 whereBuffer
.toString(),
1695 ancestorIds
.toArray(new String
[]{})
1697 } catch (RemoteException e
) {
1698 Log_OC
.e(TAG
, "Failed saving conflict in database " + e
.getMessage());
1701 } // else file is ROOT folder, no parent to set in conflict
1704 /// update conflict in ancestor folders
1705 // (not directly unset; maybe there are more conflicts below them)
1706 String parentPath
= file
.getRemotePath();
1707 if (parentPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
1708 parentPath
= parentPath
.substring(0, parentPath
.length() - 1);
1710 parentPath
= parentPath
.substring(0, parentPath
.lastIndexOf(OCFile
.PATH_SEPARATOR
) + 1);
1712 Log_OC
.d(TAG
, "checking parents to remove conflict; STARTING with " + parentPath
);
1713 while (parentPath
.length() > 0) {
1716 ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
+ " IS NOT NULL AND " +
1717 ProviderTableMeta
.FILE_CONTENT_TYPE
+ " != 'DIR' AND " +
1718 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " = ? AND " +
1719 ProviderTableMeta
.FILE_PATH
+ " LIKE ?";
1720 Cursor descendentsInConflict
= getContentResolver().query(
1721 ProviderTableMeta
.CONTENT_URI_FILE
,
1722 new String
[]{ProviderTableMeta
._ID
},
1724 new String
[]{mAccount
.name
, parentPath
+ "%"},
1727 if (descendentsInConflict
== null
|| descendentsInConflict
.getCount() == 0) {
1728 Log_OC
.d(TAG
, "NO MORE conflicts in " + parentPath
);
1729 if (getContentResolver() != null
) {
1730 updated
= getContentResolver().update(
1731 ProviderTableMeta
.CONTENT_URI_FILE
,
1733 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1734 ProviderTableMeta
.FILE_PATH
+ "=?",
1735 new String
[]{mAccount
.name
, parentPath
}
1739 updated
= getContentProviderClient().update(
1740 ProviderTableMeta
.CONTENT_URI_FILE
,
1742 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
1743 ProviderTableMeta
.FILE_PATH
+ "=?"
1744 , new String
[]{mAccount
.name
, parentPath
}
1746 } catch (RemoteException e
) {
1747 Log_OC
.e(TAG
, "Failed saving conflict in database " + e
.getMessage());
1752 Log_OC
.d(TAG
, "STILL " + descendentsInConflict
.getCount() + " in " + parentPath
);
1755 if (descendentsInConflict
!= null
) {
1756 descendentsInConflict
.close();
1759 parentPath
= parentPath
.substring(0, parentPath
.length() - 1); // trim last /
1760 parentPath
= parentPath
.substring(0, parentPath
.lastIndexOf(OCFile
.PATH_SEPARATOR
) + 1);
1761 Log_OC
.d(TAG
, "checking parents to remove conflict; NEXT " + parentPath
);
1768 public OCCapability
saveCapabilities(OCCapability capability
){
1770 // Prepare capabilities data
1771 ContentValues cv
= new ContentValues();
1772 cv
.put(ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
, mAccount
.name
);
1773 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_MAYOR
, capability
.getVersionMayor());
1774 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_MINOR
, capability
.getVersionMinor());
1775 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_MICRO
, capability
.getVersionMicro());
1776 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_STRING
, capability
.getVersionString());
1777 cv
.put(ProviderTableMeta
.CAPABILITIES_VERSION_EDITION
, capability
.getVersionEdition());
1778 cv
.put(ProviderTableMeta
.CAPABILITIES_CORE_POLLINTERVAL
, capability
.getCorePollinterval());
1779 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_API_ENABLED
, capability
.getFilesSharingApiEnabled().getValue());
1780 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_ENABLED
,
1781 capability
.getFilesSharingPublicEnabled().getValue());
1782 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED
,
1783 capability
.getFilesSharingPublicPasswordEnforced().getValue());
1784 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED
,
1785 capability
.getFilesSharingPublicExpireDateEnabled().getValue());
1786 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS
,
1787 capability
.getFilesSharingPublicExpireDateDays());
1788 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED
,
1789 capability
.getFilesSharingPublicExpireDateEnforced().getValue());
1790 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL
,
1791 capability
.getFilesSharingPublicSendMail().getValue());
1792 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_UPLOAD
,
1793 capability
.getFilesSharingPublicUpload().getValue());
1794 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_USER_SEND_MAIL
,
1795 capability
.getFilesSharingUserSendMail().getValue());
1796 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_RESHARING
, capability
.getFilesSharingResharing().getValue());
1797 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_FEDERATION_OUTGOING
,
1798 capability
.getFilesSharingFederationOutgoing().getValue());
1799 cv
.put(ProviderTableMeta
.CAPABILITIES_SHARING_FEDERATION_INCOMING
,
1800 capability
.getFilesSharingFederationIncoming().getValue());
1801 cv
.put(ProviderTableMeta
.CAPABILITIES_FILES_BIGFILECHUNKING
, capability
.getFilesBigFileChuncking().getValue());
1802 cv
.put(ProviderTableMeta
.CAPABILITIES_FILES_UNDELETE
, capability
.getFilesUndelete().getValue());
1803 cv
.put(ProviderTableMeta
.CAPABILITIES_FILES_VERSIONING
, capability
.getFilesVersioning().getValue());
1805 if (capabilityExists(mAccount
.name
)) {
1806 if (getContentResolver() != null
) {
1807 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_CAPABILITIES
, cv
,
1808 ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
+ "=?",
1809 new String
[]{mAccount
.name
});
1812 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_CAPABILITIES
,
1813 cv
, ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
+ "=?",
1814 new String
[]{mAccount
.name
});
1815 } catch (RemoteException e
) {
1817 "Fail to insert insert file to database "
1822 Uri result_uri
= null
;
1823 if (getContentResolver() != null
) {
1824 result_uri
= getContentResolver().insert(
1825 ProviderTableMeta
.CONTENT_URI_CAPABILITIES
, cv
);
1828 result_uri
= getContentProviderClient().insert(
1829 ProviderTableMeta
.CONTENT_URI_CAPABILITIES
, cv
);
1830 } catch (RemoteException e
) {
1832 "Fail to insert insert capability to database "
1836 if (result_uri
!= null
) {
1837 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
1839 capability
.setId(new_id
);
1840 capability
.setAccountName(mAccount
.name
);
1847 private boolean capabilityExists(String accountName
) {
1848 Cursor c
= getCapabilityCursorForAccount(accountName
);
1849 boolean exists
= false
;
1851 exists
= c
.moveToFirst();
1857 private Cursor
getCapabilityCursorForAccount(String accountName
){
1859 if (getContentResolver() != null
) {
1860 c
= getContentResolver()
1861 .query(ProviderTableMeta
.CONTENT_URI_CAPABILITIES
,
1863 ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
+ "=? ",
1864 new String
[]{accountName
}, null
);
1867 c
= getContentProviderClient().query(
1868 ProviderTableMeta
.CONTENT_URI_CAPABILITIES
,
1870 ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
+ "=? ",
1871 new String
[]{accountName
}, null
);
1872 } catch (RemoteException e
) {
1874 "Couldn't determine capability existance, assuming non existance: "
1882 public OCCapability
getCapability(String accountName
){
1883 OCCapability capability
= null
;
1884 Cursor c
= getCapabilityCursorForAccount(accountName
);
1886 if (c
.moveToFirst()) {
1887 capability
= createCapabilityInstance(c
);
1893 private OCCapability
createCapabilityInstance(Cursor c
) {
1894 OCCapability capability
= null
;
1896 capability
= new OCCapability();
1897 capability
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
1898 capability
.setAccountName(c
.getString(c
1899 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_ACCOUNT_NAME
)));
1900 capability
.setVersionMayor(c
.getInt(c
1901 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_MAYOR
)));
1902 capability
.setVersionMinor(c
.getInt(c
1903 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_MINOR
)));
1904 capability
.setVersionMicro(c
.getInt(c
1905 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_MICRO
)));
1906 capability
.setVersionString(c
.getString(c
1907 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_STRING
)));
1908 capability
.setVersionEdition(c
.getString(c
1909 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_VERSION_EDITION
)));
1910 capability
.setCorePollinterval(c
.getInt(c
1911 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_CORE_POLLINTERVAL
)));
1912 capability
.setFilesSharingApiEnabled(CapabilityBooleanType
.fromValue(c
.getInt(c
1913 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_API_ENABLED
))));
1914 capability
.setFilesSharingPublicEnabled(CapabilityBooleanType
.fromValue(c
.getInt(c
1915 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_ENABLED
))));
1916 capability
.setFilesSharingPublicPasswordEnforced(CapabilityBooleanType
.fromValue(c
.getInt(c
1917 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED
))));
1918 capability
.setFilesSharingPublicExpireDateEnabled(CapabilityBooleanType
.fromValue(c
.getInt(c
1919 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED
))));
1920 capability
.setFilesSharingPublicExpireDateDays(c
.getInt(c
1921 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS
)));
1922 capability
.setFilesSharingPublicExpireDateEnforced(CapabilityBooleanType
.fromValue(c
.getInt(c
1923 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED
))));
1924 capability
.setFilesSharingPublicSendMail(CapabilityBooleanType
.fromValue(c
.getInt(c
1925 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL
))));
1926 capability
.setFilesSharingPublicUpload(CapabilityBooleanType
.fromValue(c
.getInt(c
1927 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_PUBLIC_UPLOAD
))));
1928 capability
.setFilesSharingUserSendMail(CapabilityBooleanType
.fromValue(c
.getInt(c
1929 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_USER_SEND_MAIL
))));
1930 capability
.setFilesSharingResharing(CapabilityBooleanType
.fromValue(c
.getInt(c
1931 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_RESHARING
))));
1932 capability
.setFilesSharingFederationOutgoing(CapabilityBooleanType
.fromValue(c
.getInt(c
1933 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_FEDERATION_OUTGOING
))));
1934 capability
.setFilesSharingFederationIncoming(CapabilityBooleanType
.fromValue(c
.getInt(c
1935 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_SHARING_FEDERATION_INCOMING
))));
1936 capability
.setFilesBigFileChuncking(CapabilityBooleanType
.fromValue(c
.getInt(c
1937 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_FILES_BIGFILECHUNKING
))));
1938 capability
.setFilesUndelete(CapabilityBooleanType
.fromValue(c
.getInt(c
1939 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_FILES_UNDELETE
))));
1940 capability
.setFilesVersioning(CapabilityBooleanType
.fromValue(c
.getInt(c
1941 .getColumnIndex(ProviderTableMeta
.CAPABILITIES_FILES_VERSIONING
))));