1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.datamodel
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collection
;
24 import java
.util
.Collections
;
25 import java
.util
.Iterator
;
26 import java
.util
.Vector
;
28 import com
.owncloud
.android
.MainApp
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
31 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
32 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
33 import com
.owncloud
.android
.utils
.FileStorageUtils
;
34 import com
.owncloud
.android
.utils
.Log_OC
;
36 import android
.accounts
.Account
;
37 import android
.content
.ContentProviderClient
;
38 import android
.content
.ContentProviderOperation
;
39 import android
.content
.ContentProviderResult
;
40 import android
.content
.ContentResolver
;
41 import android
.content
.ContentUris
;
42 import android
.content
.ContentValues
;
43 import android
.content
.OperationApplicationException
;
44 import android
.database
.Cursor
;
45 import android
.net
.Uri
;
46 import android
.os
.RemoteException
;
48 public class FileDataStorageManager
{
50 public static final int ROOT_PARENT_ID
= 0;
52 private ContentResolver mContentResolver
;
53 private ContentProviderClient mContentProviderClient
;
54 private Account mAccount
;
56 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
59 public FileDataStorageManager(Account account
, ContentResolver cr
) {
60 mContentProviderClient
= null
;
61 mContentResolver
= cr
;
65 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
66 mContentProviderClient
= cp
;
67 mContentResolver
= null
;
72 public void setAccount(Account account
) {
76 public Account
getAccount() {
80 public void setContentResolver(ContentResolver cr
) {
81 mContentResolver
= cr
;
84 public ContentResolver
getContentResolver() {
85 return mContentResolver
;
88 public void setContentProviderClient(ContentProviderClient cp
) {
89 mContentProviderClient
= cp
;
92 public ContentProviderClient
getContentProviderClient() {
93 return mContentProviderClient
;
97 public OCFile
getFileByPath(String path
) {
98 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
100 if (c
.moveToFirst()) {
101 file
= createFileInstance(c
);
104 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
105 return createRootDir(); // root should always exist
111 public OCFile
getFileById(long id
) {
112 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
114 if (c
.moveToFirst()) {
115 file
= createFileInstance(c
);
121 public OCFile
getFileByLocalPath(String path
) {
122 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
124 if (c
.moveToFirst()) {
125 file
= createFileInstance(c
);
131 public boolean fileExists(long id
) {
132 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
135 public boolean fileExists(String path
) {
136 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
140 public Vector
<OCFile
> getFolderContent(OCFile f
) {
141 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
142 return getFolderContent(f
.getFileId());
145 return new Vector
<OCFile
>();
150 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
151 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
152 if (folder
!= null
) {
153 // TODO better implementation, filtering in the access to database (if possible) instead of here
154 Vector
<OCFile
> tmp
= getFolderContent(folder
);
155 OCFile current
= null
;
156 for (int i
=0; i
<tmp
.size(); i
++) {
157 current
= tmp
.get(i
);
158 if (current
.isImage()) {
167 public boolean saveFile(OCFile file
) {
168 boolean overriden
= false
;
169 ContentValues cv
= new ContentValues();
170 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
171 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
172 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
173 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
174 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
175 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
176 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
177 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
178 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
179 if (!file
.isFolder())
180 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
181 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
182 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
183 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
184 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
185 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
186 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
187 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
189 boolean sameRemotePath
= fileExists(file
.getRemotePath());
190 if (sameRemotePath
||
191 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
193 OCFile oldFile
= null
;
194 if (sameRemotePath
) {
195 oldFile
= getFileByPath(file
.getRemotePath());
196 file
.setFileId(oldFile
.getFileId());
198 oldFile
= getFileById(file
.getFileId());
202 if (getContentResolver() != null
) {
203 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
204 ProviderTableMeta
._ID
+ "=?",
205 new String
[] { String
.valueOf(file
.getFileId()) });
208 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
209 cv
, ProviderTableMeta
._ID
+ "=?",
210 new String
[] { String
.valueOf(file
.getFileId()) });
211 } catch (RemoteException e
) {
213 "Fail to insert insert file to database "
218 Uri result_uri
= null
;
219 if (getContentResolver() != null
) {
220 result_uri
= getContentResolver().insert(
221 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
224 result_uri
= getContentProviderClient().insert(
225 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
226 } catch (RemoteException e
) {
228 "Fail to insert insert file to database "
232 if (result_uri
!= null
) {
233 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
235 file
.setFileId(new_id
);
239 // if (file.isFolder()) {
240 // updateFolderSize(file.getFileId());
242 // updateFolderSize(file.getParentId());
250 * Inserts or updates the list of files contained in a given folder.
252 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
253 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
257 * @param removeNotUpdated
259 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
261 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
263 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
265 // prepare operations to insert or update files to save in the given folder
266 for (OCFile file
: updatedFiles
) {
267 ContentValues cv
= new ContentValues();
268 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
269 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
270 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
271 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
272 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
273 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
274 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
275 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
276 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
277 if (!file
.isFolder()) {
278 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
280 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
281 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
282 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
283 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
284 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
285 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
286 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
288 boolean existsByPath
= fileExists(file
.getRemotePath());
289 if (existsByPath
|| fileExists(file
.getFileId())) {
290 // updating an existing file
291 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
293 withSelection( ProviderTableMeta
._ID
+ "=?",
294 new String
[] { String
.valueOf(file
.getFileId()) })
299 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
303 // prepare operations to remove files in the given folder
304 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
305 String
[] whereArgs
= null
;
306 for (OCFile file
: filesToRemove
) {
307 if (file
.getParentId() == folder
.getFileId()) {
308 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
309 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
310 if (file
.isFolder()) {
311 operations
.add(ContentProviderOperation
312 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
314 // TODO remove local folder
316 operations
.add(ContentProviderOperation
317 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
320 new File(file
.getStoragePath()).delete();
321 // TODO move the deletion of local contents after success of deletions
327 // update metadata of folder
328 ContentValues cv
= new ContentValues();
329 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
330 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
331 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
332 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
333 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
334 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
335 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
336 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
337 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
338 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
339 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
340 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
341 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
342 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
343 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
345 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
347 withSelection( ProviderTableMeta
._ID
+ "=?",
348 new String
[] { String
.valueOf(folder
.getFileId()) })
351 // apply operations in batch
352 ContentProviderResult
[] results
= null
;
353 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
355 if (getContentResolver() != null
) {
356 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
359 results
= getContentProviderClient().applyBatch(operations
);
362 } catch (OperationApplicationException e
) {
363 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
365 } catch (RemoteException e
) {
366 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
369 // update new id in file objects for insertions
370 if (results
!= null
) {
372 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
374 for (int i
=0; i
<results
.length
; i
++) {
375 if (filesIt
.hasNext()) {
376 file
= filesIt
.next();
380 if (results
[i
].uri
!= null
) {
381 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
382 //updatedFiles.get(i).setFileId(newId);
384 file
.setFileId(newId
);
390 //updateFolderSize(folder.getFileId());
399 // private void updateFolderSize(long id) {
400 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
401 // Log_OC.d(TAG, "Updating size of " + id);
402 // if (getContentResolver() != null) {
403 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
404 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
405 // ProviderTableMeta._ID + "=?",
406 // new String[] { String.valueOf(id) });
409 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
410 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
411 // ProviderTableMeta._ID + "=?",
412 // new String[] { String.valueOf(id) });
414 // } catch (RemoteException e) {
415 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
419 // Log_OC.e(TAG, "not updating size for folder " + id);
424 public void removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
426 if (file
.isFolder()) {
427 removeFolder(file
, removeDBData
, removeLocalCopy
);
431 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
432 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
433 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
434 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
435 if (getContentProviderClient() != null
) {
437 getContentProviderClient().delete(file_uri
, where
, whereArgs
);
438 } catch (RemoteException e
) {
442 getContentResolver().delete(file_uri
, where
, whereArgs
);
444 //updateFolderSize(file.getParentId());
446 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
) {
447 boolean success
= new File(file
.getStoragePath()).delete();
448 if (!removeDBData
&& success
) {
449 // maybe unnecessary, but should be checked TODO remove if unnecessary
450 file
.setStoragePath(null
);
459 public void removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
460 if (folder
!= null
&& folder
.isFolder()) {
461 if (removeDBData
&& folder
.getFileId() != -1) {
462 removeFolderInDb(folder
);
464 if (removeLocalContent
) {
465 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
466 removeLocalFolder(localFolder
);
471 private void removeFolderInDb(OCFile folder
) {
472 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
473 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
474 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
475 if (getContentProviderClient() != null
) {
477 getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
478 } catch (RemoteException e
) {
482 getContentResolver().delete(folder_uri
, where
, whereArgs
);
484 //updateFolderSize(folder.getParentId());
487 private void removeLocalFolder(File folder
) {
488 if (folder
.exists()) {
489 File
[] files
= folder
.listFiles();
491 for (File file
: files
) {
492 if (file
.isDirectory()) {
493 removeLocalFolder(file
);
504 * Updates database for a folder that was moved to a different location.
506 * TODO explore better (faster) implementations
507 * TODO throw exceptions up !
509 public void moveFolder(OCFile folder
, String newPath
) {
510 // TODO check newPath
512 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
513 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
515 if (getContentProviderClient() != null
) {
517 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
519 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
520 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
521 } catch (RemoteException e
) {
522 Log_OC
.e(TAG
, e
.getMessage());
525 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
527 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
528 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
531 /// 2. prepare a batch of update operations to change all the descendants
532 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
533 int lengthOfOldPath
= folder
.getRemotePath().length();
534 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
535 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
536 if (c
.moveToFirst()) {
538 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
539 OCFile child
= createFileInstance(c
);
540 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
541 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
542 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
544 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
546 withSelection( ProviderTableMeta
._ID
+ "=?",
547 new String
[] { String
.valueOf(child
.getFileId()) })
549 } while (c
.moveToNext());
553 /// 3. apply updates in batch
555 if (getContentResolver() != null
) {
556 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
559 getContentProviderClient().applyBatch(operations
);
562 } catch (OperationApplicationException e
) {
563 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
565 } catch (RemoteException e
) {
566 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
573 private Vector
<OCFile
> getFolderContent(long parentId
) {
575 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
577 Uri req_uri
= Uri
.withAppendedPath(
578 ProviderTableMeta
.CONTENT_URI_DIR
,
579 String
.valueOf(parentId
));
582 if (getContentProviderClient() != null
) {
584 c
= getContentProviderClient().query(req_uri
, null
,
585 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
586 new String
[] { String
.valueOf(parentId
)}, null
);
587 } catch (RemoteException e
) {
588 Log_OC
.e(TAG
, e
.getMessage());
592 c
= getContentResolver().query(req_uri
, null
,
593 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
594 new String
[] { String
.valueOf(parentId
)}, null
);
597 if (c
.moveToFirst()) {
599 OCFile child
= createFileInstance(c
);
601 } while (c
.moveToNext());
606 Collections
.sort(ret
);
612 public Cursor
getContent(long parentId
) {
613 Log_OC
.d(TAG
, "getContent start");
614 Uri req_uri
= Uri
.withAppendedPath(
615 ProviderTableMeta
.CONTENT_URI_DIR
,
616 String
.valueOf(parentId
));
619 if (getContentProviderClient() != null
) {
621 c
= getContentProviderClient().query(req_uri
, null
,
622 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
623 new String
[] { String
.valueOf(parentId
)}, null
);
624 } catch (RemoteException e
) {
625 Log_OC
.e(TAG
, e
.getMessage());
629 c
= getContentResolver().query(req_uri
, null
,
630 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
631 new String
[] { String
.valueOf(parentId
)}, null
);
633 //Log_OC.d(TAG, "getContent Uri " + req_uri);
634 //c.setNotificationUri(getContentResolver(), req_uri);
637 Log_OC
.d(TAG
, "getContent end");
641 private OCFile
createRootDir() {
642 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
643 file
.setMimetype("DIR");
644 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
649 private boolean fileExists(String cmp_key
, String value
) {
651 if (getContentResolver() != null
) {
652 c
= getContentResolver()
653 .query(ProviderTableMeta
.CONTENT_URI
,
656 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
658 new String
[] { value
, mAccount
.name
}, null
);
661 c
= getContentProviderClient().query(
662 ProviderTableMeta
.CONTENT_URI
,
665 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
666 new String
[] { value
, mAccount
.name
}, null
);
667 } catch (RemoteException e
) {
669 "Couldn't determine file existance, assuming non existance: "
674 boolean retval
= c
.moveToFirst();
679 private Cursor
getCursorForValue(String key
, String value
) {
681 if (getContentResolver() != null
) {
682 c
= getContentResolver()
683 .query(ProviderTableMeta
.CONTENT_URI
,
686 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
688 new String
[] { value
, mAccount
.name
}, null
);
691 c
= getContentProviderClient().query(
692 ProviderTableMeta
.CONTENT_URI
,
694 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
695 + "=?", new String
[] { value
, mAccount
.name
},
697 } catch (RemoteException e
) {
698 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
705 // private Cursor getShareCursorForValue(String key, String value) {
707 // if (getContentResolver() != null) {
708 // c = getContentResolver()
709 // .query(ProviderTableMeta.CONTENT_URI_SHARE,
712 // + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER
714 // new String[] { value, mAccount.name }, null);
717 // c = getContentProviderClient().query(
718 // ProviderTableMeta.CONTENT_URI_SHARE,
720 // key + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER
721 // + "=?", new String[] { value, mAccount.name },
723 // } catch (RemoteException e) {
724 // Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
731 public OCFile
createFileInstance(Cursor c
) {
734 file
= new OCFile(c
.getString(c
735 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
736 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
737 file
.setParentId(c
.getLong(c
738 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
739 file
.setMimetype(c
.getString(c
740 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
741 if (!file
.isFolder()) {
742 file
.setStoragePath(c
.getString(c
743 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
744 if (file
.getStoragePath() == null
) {
745 // try to find existing file and bind it with current account; - with the current update of SynchronizeFolderOperation, this won't be necessary anymore after a full synchronization of the account
746 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
748 file
.setStoragePath(f
.getAbsolutePath());
749 file
.setLastSyncDateForData(f
.lastModified());
753 file
.setFileLength(c
.getLong(c
754 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
755 file
.setCreationTimestamp(c
.getLong(c
756 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
757 file
.setModificationTimestamp(c
.getLong(c
758 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
759 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
760 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
761 file
.setLastSyncDateForProperties(c
.getLong(c
762 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
763 file
.setLastSyncDateForData(c
.getLong(c
.
764 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
765 file
.setKeepInSync(c
.getInt(
766 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
767 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
768 file
.setShareByLink(c
.getInt(
769 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
770 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
777 * Returns if the file/folder is shared by link or not
778 * @param path Path of the file/folder
781 public boolean isShareByLink(String path
) {
782 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
784 if (c
.moveToFirst()) {
785 file
= createFileInstance(c
);
788 return file
.isShareByLink();
792 * Returns the public link of the file/folder
793 * @param path Path of the file/folder
796 public String
getPublicLink(String path
) {
797 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
799 if (c
.moveToFirst()) {
800 file
= createFileInstance(c
);
803 return file
.getPublicLink();
807 // Methods for Shares
808 public boolean saveShare(OCShare share
) {
809 boolean overriden
= false
;
810 ContentValues cv
= new ContentValues();
811 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
812 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
813 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
814 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
815 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
816 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
817 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
818 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
819 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
820 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
821 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
822 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
823 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
824 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
826 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
829 if (getContentResolver() != null
) {
830 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
831 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
832 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
835 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
836 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
837 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
838 } catch (RemoteException e
) {
840 "Fail to insert insert file to database "
845 Uri result_uri
= null
;
846 if (getContentResolver() != null
) {
847 result_uri
= getContentResolver().insert(
848 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
851 result_uri
= getContentProviderClient().insert(
852 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
853 } catch (RemoteException e
) {
855 "Fail to insert insert file to database "
859 if (result_uri
!= null
) {
860 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
869 // private OCShare getShareById(long id) {
870 // Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
871 // OCShare share = null;
872 // if (c.moveToFirst()) {
873 // share = createShareInstance(c);
879 // private OCShare getShareByRemoteId(long remoteId) {
880 // Cursor c = getShareCursorForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
881 // OCShare share = null;
882 // if (c.moveToFirst()) {
883 // share = createShareInstance(c);
889 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
891 if (getContentResolver() != null
) {
892 c
= getContentResolver().query(
893 ProviderTableMeta
.CONTENT_URI_SHARE
,
895 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
896 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
897 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
898 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
902 c
= getContentProviderClient().query(
903 ProviderTableMeta
.CONTENT_URI_SHARE
,
905 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
906 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
907 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
908 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
911 } catch (RemoteException e
) {
912 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
916 OCShare share
= null
;
917 if (c
.moveToFirst()) {
918 share
= createShareInstance(c
);
924 private OCShare
createShareInstance(Cursor c
) {
925 OCShare share
= null
;
927 share
= new OCShare(c
.getString(c
928 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
929 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
930 share
.setFileSource(c
.getLong(c
931 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
932 share
.setShareType(ShareType
.fromValue(c
.getInt(c
933 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
934 share
.setPermissions(c
.getInt(c
935 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
936 share
.setSharedDate(c
.getLong(c
937 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
938 share
.setExpirationDate(c
.getLong(c
939 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
940 share
.setToken(c
.getString(c
941 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
942 share
.setSharedWithDisplayName(c
.getString(c
943 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
944 share
.setIsFolder(c
.getInt(
945 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
946 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
947 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
953 private boolean shareExists(String cmp_key
, String value
) {
955 if (getContentResolver() != null
) {
956 c
= getContentResolver()
957 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
960 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
962 new String
[] { value
, mAccount
.name
}, null
);
965 c
= getContentProviderClient().query(
966 ProviderTableMeta
.CONTENT_URI_SHARE
,
969 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
970 new String
[] { value
, mAccount
.name
}, null
);
971 } catch (RemoteException e
) {
973 "Couldn't determine file existance, assuming non existance: "
978 boolean retval
= c
.moveToFirst();
983 private boolean shareExists(long remoteId
) {
984 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
987 private void cleanSharedFiles() {
988 ContentValues cv
= new ContentValues();
989 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
990 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
991 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
992 String
[] whereArgs
= new String
[]{mAccount
.name
};
994 if (getContentResolver() != null
) {
995 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
999 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1001 } catch (RemoteException e
) {
1002 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1007 private void cleanSharedFilesInFolder(OCFile folder
) {
1008 ContentValues cv
= new ContentValues();
1009 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1010 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1011 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
1012 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1014 if (getContentResolver() != null
) {
1015 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1019 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1021 } catch (RemoteException e
) {
1022 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1027 private void cleanShares() {
1028 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1029 String
[] whereArgs
= new String
[]{mAccount
.name
};
1031 if (getContentResolver() != null
) {
1032 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1036 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1038 } catch (RemoteException e
) {
1039 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1044 public void saveShares(Collection
<OCShare
> shares
) {
1046 if (shares
!= null
) {
1047 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1049 // prepare operations to insert or update files to save in the given folder
1050 for (OCShare share
: shares
) {
1051 ContentValues cv
= new ContentValues();
1052 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1053 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1054 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1055 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1056 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1057 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1058 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1059 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1060 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1061 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1062 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1063 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1064 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1065 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1067 if (shareExists(share
.getIdRemoteShared())) {
1068 // updating an existing file
1069 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1071 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1072 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1076 // adding a new file
1077 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1081 // apply operations in batch
1082 if (operations
.size() > 0) {
1083 @SuppressWarnings("unused")
1084 ContentProviderResult
[] results
= null
;
1085 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1087 if (getContentResolver() != null
) {
1088 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1091 results
= getContentProviderClient().applyBatch(operations
);
1094 } catch (OperationApplicationException e
) {
1095 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1097 } catch (RemoteException e
) {
1098 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1105 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1108 if (sharedFiles
!= null
) {
1109 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1111 // prepare operations to insert or update files to save in the given folder
1112 for (OCFile file
: sharedFiles
) {
1113 ContentValues cv
= new ContentValues();
1114 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1115 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1116 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1117 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1118 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1119 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1120 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1121 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1122 if (!file
.isFolder()) {
1123 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1125 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1126 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1127 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1128 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1129 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1130 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1131 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1133 boolean existsByPath
= fileExists(file
.getRemotePath());
1134 if (existsByPath
|| fileExists(file
.getFileId())) {
1135 // updating an existing file
1136 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1138 withSelection( ProviderTableMeta
._ID
+ "=?",
1139 new String
[] { String
.valueOf(file
.getFileId()) })
1143 // adding a new file
1144 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1148 // apply operations in batch
1149 if (operations
.size() > 0) {
1150 @SuppressWarnings("unused")
1151 ContentProviderResult
[] results
= null
;
1152 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1154 if (getContentResolver() != null
) {
1155 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1158 results
= getContentProviderClient().applyBatch(operations
);
1161 } catch (OperationApplicationException e
) {
1162 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1164 } catch (RemoteException e
) {
1165 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1172 public void removeShare(OCShare share
){
1173 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1174 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1175 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1176 if (getContentProviderClient() != null
) {
1178 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1179 } catch (RemoteException e
) {
1180 e
.printStackTrace();
1183 getContentResolver().delete(share_uri
, where
, whereArgs
);
1187 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1190 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1192 for (OCShare share
: shares
) {
1194 String path
= share
.getPath();
1195 if (share
.isFolder()) {
1196 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1199 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1200 OCFile file
= getFileByPath(path
);
1202 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1203 file
.setShareByLink(true
);
1204 sharedFiles
.add(file
);
1209 updateSharedFiles(sharedFiles
);
1213 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1214 cleanSharedFilesInFolder(folder
);
1215 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1216 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1218 if (shares
!= null
) {
1219 // prepare operations to insert or update files to save in the given folder
1220 for (OCShare share
: shares
) {
1221 ContentValues cv
= new ContentValues();
1222 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1223 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1224 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1225 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1226 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1227 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1228 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1229 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1230 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1231 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1232 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1233 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1234 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1235 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1238 if (shareExists(share.getIdRemoteShared())) {
1239 // updating an existing share resource
1240 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1242 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1243 new String[] { String.valueOf(share.getIdRemoteShared()) })
1248 // adding a new share resource
1249 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1254 // apply operations in batch
1255 if (operations
.size() > 0) {
1256 @SuppressWarnings("unused")
1257 ContentProviderResult
[] results
= null
;
1258 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1260 if (getContentResolver() != null
) {
1261 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1264 results
= getContentProviderClient().applyBatch(operations
);
1267 } catch (OperationApplicationException e
) {
1268 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1270 } catch (RemoteException e
) {
1271 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1278 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1279 if (folder
!= null
) {
1280 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1281 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1283 Vector
<OCFile
> files
= getFolderContent(folder
);
1285 for (OCFile file
: files
) {
1286 whereArgs
[0] = file
.getRemotePath();
1287 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1288 .withSelection(where
, whereArgs
)
1292 return preparedOperations
;
1295 if (operations.size() > 0) {
1297 if (getContentResolver() != null) {
1298 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1301 getContentProviderClient().applyBatch(operations);
1304 } catch (OperationApplicationException e) {
1305 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1307 } catch (RemoteException e) {
1308 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1314 if (getContentResolver() != null) {
1316 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1321 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1325 } catch (RemoteException e) {
1326 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());