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
;
37 import android
.accounts
.Account
;
38 import android
.content
.ContentProviderClient
;
39 import android
.content
.ContentProviderOperation
;
40 import android
.content
.ContentProviderResult
;
41 import android
.content
.ContentResolver
;
42 import android
.content
.ContentUris
;
43 import android
.content
.ContentValues
;
44 import android
.content
.OperationApplicationException
;
45 import android
.database
.Cursor
;
46 import android
.net
.Uri
;
47 import android
.os
.RemoteException
;
49 public class FileDataStorageManager
{
51 public static final int ROOT_PARENT_ID
= 0;
53 private ContentResolver mContentResolver
;
54 private ContentProviderClient mContentProviderClient
;
55 private Account mAccount
;
57 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
60 public FileDataStorageManager(Account account
, ContentResolver cr
) {
61 mContentProviderClient
= null
;
62 mContentResolver
= cr
;
66 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
67 mContentProviderClient
= cp
;
68 mContentResolver
= null
;
73 public void setAccount(Account account
) {
77 public Account
getAccount() {
81 public void setContentResolver(ContentResolver cr
) {
82 mContentResolver
= cr
;
85 public ContentResolver
getContentResolver() {
86 return mContentResolver
;
89 public void setContentProviderClient(ContentProviderClient cp
) {
90 mContentProviderClient
= cp
;
93 public ContentProviderClient
getContentProviderClient() {
94 return mContentProviderClient
;
98 public OCFile
getFileByPath(String path
) {
99 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
101 if (c
.moveToFirst()) {
102 file
= createFileInstance(c
);
105 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
106 return createRootDir(); // root should always exist
112 public OCFile
getFileById(long id
) {
113 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
115 if (c
.moveToFirst()) {
116 file
= createFileInstance(c
);
122 public OCFile
getFileByLocalPath(String path
) {
123 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
125 if (c
.moveToFirst()) {
126 file
= createFileInstance(c
);
132 public boolean fileExists(long id
) {
133 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
136 public boolean fileExists(String path
) {
137 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
141 public Vector
<OCFile
> getFolderContent(OCFile f
) {
142 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
143 return getFolderContent(f
.getFileId());
146 return new Vector
<OCFile
>();
151 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
152 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
153 if (folder
!= null
) {
154 // TODO better implementation, filtering in the access to database (if possible) instead of here
155 Vector
<OCFile
> tmp
= getFolderContent(folder
);
156 OCFile current
= null
;
157 for (int i
=0; i
<tmp
.size(); i
++) {
158 current
= tmp
.get(i
);
159 if (current
.isImage()) {
168 public boolean saveFile(OCFile file
) {
169 boolean overriden
= false
;
170 ContentValues cv
= new ContentValues();
171 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
172 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
173 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
174 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
175 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
176 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
177 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
178 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
179 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
180 if (!file
.isFolder())
181 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
182 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
183 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
184 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
185 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
186 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
187 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
188 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
190 boolean sameRemotePath
= fileExists(file
.getRemotePath());
191 if (sameRemotePath
||
192 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
194 OCFile oldFile
= null
;
195 if (sameRemotePath
) {
196 oldFile
= getFileByPath(file
.getRemotePath());
197 file
.setFileId(oldFile
.getFileId());
199 oldFile
= getFileById(file
.getFileId());
203 if (getContentResolver() != null
) {
204 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
205 ProviderTableMeta
._ID
+ "=?",
206 new String
[] { String
.valueOf(file
.getFileId()) });
209 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
210 cv
, ProviderTableMeta
._ID
+ "=?",
211 new String
[] { String
.valueOf(file
.getFileId()) });
212 } catch (RemoteException e
) {
214 "Fail to insert insert file to database "
219 Uri result_uri
= null
;
220 if (getContentResolver() != null
) {
221 result_uri
= getContentResolver().insert(
222 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
225 result_uri
= getContentProviderClient().insert(
226 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
227 } catch (RemoteException e
) {
229 "Fail to insert insert file to database "
233 if (result_uri
!= null
) {
234 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
236 file
.setFileId(new_id
);
240 // if (file.isFolder()) {
241 // updateFolderSize(file.getFileId());
243 // updateFolderSize(file.getParentId());
251 * Inserts or updates the list of files contained in a given folder.
253 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
254 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
258 * @param removeNotUpdated
260 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
262 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
264 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
266 // prepare operations to insert or update files to save in the given folder
267 for (OCFile file
: updatedFiles
) {
268 ContentValues cv
= new ContentValues();
269 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
270 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
271 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
272 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
273 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
274 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
275 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
276 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
277 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
278 if (!file
.isFolder()) {
279 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
281 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
282 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
283 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
284 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
285 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
286 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
287 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
289 boolean existsByPath
= fileExists(file
.getRemotePath());
290 if (existsByPath
|| fileExists(file
.getFileId())) {
291 // updating an existing file
292 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
294 withSelection( ProviderTableMeta
._ID
+ "=?",
295 new String
[] { String
.valueOf(file
.getFileId()) })
300 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
304 // prepare operations to remove files in the given folder
305 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
306 String
[] whereArgs
= null
;
307 for (OCFile file
: filesToRemove
) {
308 if (file
.getParentId() == folder
.getFileId()) {
309 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
310 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
311 if (file
.isFolder()) {
312 operations
.add(ContentProviderOperation
313 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
315 // TODO remove local folder
317 operations
.add(ContentProviderOperation
318 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
321 new File(file
.getStoragePath()).delete();
322 // TODO move the deletion of local contents after success of deletions
328 // update metadata of folder
329 ContentValues cv
= new ContentValues();
330 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
331 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
332 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
333 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
334 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
335 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
336 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
337 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
338 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
339 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
340 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
341 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
342 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
343 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
344 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
346 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
348 withSelection( ProviderTableMeta
._ID
+ "=?",
349 new String
[] { String
.valueOf(folder
.getFileId()) })
352 // apply operations in batch
353 ContentProviderResult
[] results
= null
;
354 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
356 if (getContentResolver() != null
) {
357 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
360 results
= getContentProviderClient().applyBatch(operations
);
363 } catch (OperationApplicationException e
) {
364 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
366 } catch (RemoteException e
) {
367 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
370 // update new id in file objects for insertions
371 if (results
!= null
) {
373 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
375 for (int i
=0; i
<results
.length
; i
++) {
376 if (filesIt
.hasNext()) {
377 file
= filesIt
.next();
381 if (results
[i
].uri
!= null
) {
382 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
383 //updatedFiles.get(i).setFileId(newId);
385 file
.setFileId(newId
);
391 //updateFolderSize(folder.getFileId());
400 // private void updateFolderSize(long id) {
401 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
402 // Log_OC.d(TAG, "Updating size of " + id);
403 // if (getContentResolver() != null) {
404 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
405 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
406 // ProviderTableMeta._ID + "=?",
407 // new String[] { String.valueOf(id) });
410 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
411 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
412 // ProviderTableMeta._ID + "=?",
413 // new String[] { String.valueOf(id) });
415 // } catch (RemoteException e) {
416 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
420 // Log_OC.e(TAG, "not updating size for folder " + id);
425 public void removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
427 if (file
.isFolder()) {
428 removeFolder(file
, removeDBData
, removeLocalCopy
);
432 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
433 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
434 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
435 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
436 if (getContentProviderClient() != null
) {
438 getContentProviderClient().delete(file_uri
, where
, whereArgs
);
439 } catch (RemoteException e
) {
443 getContentResolver().delete(file_uri
, where
, whereArgs
);
445 //updateFolderSize(file.getParentId());
447 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
) {
448 boolean success
= new File(file
.getStoragePath()).delete();
449 if (!removeDBData
&& success
) {
450 // maybe unnecessary, but should be checked TODO remove if unnecessary
451 file
.setStoragePath(null
);
460 public void removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
461 if (folder
!= null
&& folder
.isFolder()) {
462 if (removeDBData
&& folder
.getFileId() != -1) {
463 removeFolderInDb(folder
);
465 if (removeLocalContent
) {
466 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
467 removeLocalFolder(localFolder
);
472 private void removeFolderInDb(OCFile folder
) {
473 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
474 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
475 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
476 if (getContentProviderClient() != null
) {
478 getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
479 } catch (RemoteException e
) {
483 getContentResolver().delete(folder_uri
, where
, whereArgs
);
485 //updateFolderSize(folder.getParentId());
488 private void removeLocalFolder(File folder
) {
489 if (folder
.exists()) {
490 File
[] files
= folder
.listFiles();
492 for (File file
: files
) {
493 if (file
.isDirectory()) {
494 removeLocalFolder(file
);
505 * Updates database for a folder that was moved to a different location.
507 * TODO explore better (faster) implementations
508 * TODO throw exceptions up !
510 public void moveFolder(OCFile folder
, String newPath
) {
511 // TODO check newPath
513 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
514 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
516 if (getContentProviderClient() != null
) {
518 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
520 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
521 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
522 } catch (RemoteException e
) {
523 Log_OC
.e(TAG
, e
.getMessage());
526 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
528 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
529 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
532 /// 2. prepare a batch of update operations to change all the descendants
533 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
534 int lengthOfOldPath
= folder
.getRemotePath().length();
535 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
536 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
537 if (c
.moveToFirst()) {
539 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
540 OCFile child
= createFileInstance(c
);
541 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
542 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
543 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
545 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
547 withSelection( ProviderTableMeta
._ID
+ "=?",
548 new String
[] { String
.valueOf(child
.getFileId()) })
550 } while (c
.moveToNext());
554 /// 3. apply updates in batch
556 if (getContentResolver() != null
) {
557 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
560 getContentProviderClient().applyBatch(operations
);
563 } catch (OperationApplicationException e
) {
564 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
566 } catch (RemoteException e
) {
567 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
574 private Vector
<OCFile
> getFolderContent(long parentId
) {
576 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
578 Uri req_uri
= Uri
.withAppendedPath(
579 ProviderTableMeta
.CONTENT_URI_DIR
,
580 String
.valueOf(parentId
));
583 if (getContentProviderClient() != null
) {
585 c
= getContentProviderClient().query(req_uri
, null
,
586 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
587 new String
[] { String
.valueOf(parentId
)}, null
);
588 } catch (RemoteException e
) {
589 Log_OC
.e(TAG
, e
.getMessage());
593 c
= getContentResolver().query(req_uri
, null
,
594 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
595 new String
[] { String
.valueOf(parentId
)}, null
);
598 if (c
.moveToFirst()) {
600 OCFile child
= createFileInstance(c
);
602 } while (c
.moveToNext());
607 Collections
.sort(ret
);
613 public Cursor
getContent(long parentId
) {
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
);
637 private OCFile
createRootDir() {
638 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
639 file
.setMimetype("DIR");
640 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
645 private boolean fileExists(String cmp_key
, String value
) {
647 if (getContentResolver() != null
) {
648 c
= getContentResolver()
649 .query(ProviderTableMeta
.CONTENT_URI
,
652 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
654 new String
[] { value
, mAccount
.name
}, null
);
657 c
= getContentProviderClient().query(
658 ProviderTableMeta
.CONTENT_URI
,
661 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
662 new String
[] { value
, mAccount
.name
}, null
);
663 } catch (RemoteException e
) {
665 "Couldn't determine file existance, assuming non existance: "
670 boolean retval
= c
.moveToFirst();
675 private Cursor
getCursorForValue(String key
, String value
) {
677 if (getContentResolver() != null
) {
678 c
= getContentResolver()
679 .query(ProviderTableMeta
.CONTENT_URI
,
682 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
684 new String
[] { value
, mAccount
.name
}, null
);
687 c
= getContentProviderClient().query(
688 ProviderTableMeta
.CONTENT_URI
,
690 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
691 + "=?", new String
[] { value
, mAccount
.name
},
693 } catch (RemoteException e
) {
694 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
701 // private Cursor getShareCursorForValue(String key, String value) {
703 // if (getContentResolver() != null) {
704 // c = getContentResolver()
705 // .query(ProviderTableMeta.CONTENT_URI_SHARE,
708 // + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER
710 // new String[] { value, mAccount.name }, null);
713 // c = getContentProviderClient().query(
714 // ProviderTableMeta.CONTENT_URI_SHARE,
716 // key + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER
717 // + "=?", new String[] { value, mAccount.name },
719 // } catch (RemoteException e) {
720 // Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
727 public OCFile
createFileInstance(Cursor c
) {
730 file
= new OCFile(c
.getString(c
731 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
732 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
733 file
.setParentId(c
.getLong(c
734 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
735 file
.setMimetype(c
.getString(c
736 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
737 if (!file
.isFolder()) {
738 file
.setStoragePath(c
.getString(c
739 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
740 if (file
.getStoragePath() == null
) {
741 // 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
742 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
744 file
.setStoragePath(f
.getAbsolutePath());
745 file
.setLastSyncDateForData(f
.lastModified());
749 file
.setFileLength(c
.getLong(c
750 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
751 file
.setCreationTimestamp(c
.getLong(c
752 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
753 file
.setModificationTimestamp(c
.getLong(c
754 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
755 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
756 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
757 file
.setLastSyncDateForProperties(c
.getLong(c
758 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
759 file
.setLastSyncDateForData(c
.getLong(c
.
760 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
761 file
.setKeepInSync(c
.getInt(
762 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
763 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
764 file
.setShareByLink(c
.getInt(
765 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
766 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
773 * Returns if the file/folder is shared by link or not
774 * @param path Path of the file/folder
777 public boolean isShareByLink(String path
) {
778 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
780 if (c
.moveToFirst()) {
781 file
= createFileInstance(c
);
784 return file
.isShareByLink();
788 * Returns the public link of the file/folder
789 * @param path Path of the file/folder
792 public String
getPublicLink(String path
) {
793 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
795 if (c
.moveToFirst()) {
796 file
= createFileInstance(c
);
799 return file
.getPublicLink();
803 // Methods for Shares
804 public boolean saveShare(OCShare share
) {
805 boolean overriden
= false
;
806 ContentValues cv
= new ContentValues();
807 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
808 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
809 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
810 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
811 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
812 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
813 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
814 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
815 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
816 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
817 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
818 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
819 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
820 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
822 if (shareExists(share
.getIdRemoteShared())) { // for renamed files; no more delete and create
825 if (getContentResolver() != null
) {
826 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
827 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
828 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
831 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
832 cv
, ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
833 new String
[] { String
.valueOf(share
.getIdRemoteShared()) });
834 } catch (RemoteException e
) {
836 "Fail to insert insert file to database "
841 Uri result_uri
= null
;
842 if (getContentResolver() != null
) {
843 result_uri
= getContentResolver().insert(
844 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
847 result_uri
= getContentProviderClient().insert(
848 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
849 } catch (RemoteException e
) {
851 "Fail to insert insert file to database "
855 if (result_uri
!= null
) {
856 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
865 // private OCShare getShareById(long id) {
866 // Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
867 // OCShare share = null;
868 // if (c.moveToFirst()) {
869 // share = createShareInstance(c);
875 // private OCShare getShareByRemoteId(long remoteId) {
876 // Cursor c = getShareCursorForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
877 // OCShare share = null;
878 // if (c.moveToFirst()) {
879 // share = createShareInstance(c);
885 public OCShare
getFirstShareByPathAndType(String path
, ShareType type
) {
887 if (getContentResolver() != null
) {
888 c
= getContentResolver().query(
889 ProviderTableMeta
.CONTENT_URI_SHARE
,
891 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
892 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
893 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
894 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
898 c
= getContentProviderClient().query(
899 ProviderTableMeta
.CONTENT_URI_SHARE
,
901 ProviderTableMeta
.OCSHARES_PATH
+ "=? AND "
902 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ "=? AND "
903 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
904 new String
[] { path
, Integer
.toString(type
.getValue()), mAccount
.name
},
907 } catch (RemoteException e
) {
908 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
912 OCShare share
= null
;
913 if (c
.moveToFirst()) {
914 share
= createShareInstance(c
);
920 private OCShare
createShareInstance(Cursor c
) {
921 OCShare share
= null
;
923 share
= new OCShare(c
.getString(c
924 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
925 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
926 share
.setFileSource(c
.getLong(c
927 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
928 share
.setShareType(ShareType
.fromValue(c
.getInt(c
929 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
930 share
.setPermissions(c
.getInt(c
931 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
932 share
.setSharedDate(c
.getLong(c
933 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
934 share
.setExpirationDate(c
.getLong(c
935 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
936 share
.setToken(c
.getString(c
937 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
938 share
.setSharedWithDisplayName(c
.getString(c
939 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
940 share
.setIsFolder(c
.getInt(
941 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
942 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
943 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
949 private boolean shareExists(String cmp_key
, String value
) {
951 if (getContentResolver() != null
) {
952 c
= getContentResolver()
953 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
956 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
958 new String
[] { value
, mAccount
.name
}, null
);
961 c
= getContentProviderClient().query(
962 ProviderTableMeta
.CONTENT_URI_SHARE
,
965 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
966 new String
[] { value
, mAccount
.name
}, null
);
967 } catch (RemoteException e
) {
969 "Couldn't determine file existance, assuming non existance: "
974 boolean retval
= c
.moveToFirst();
979 private boolean shareExists(long remoteId
) {
980 return shareExists(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, String
.valueOf(remoteId
));
983 private void cleanSharedFiles() {
984 ContentValues cv
= new ContentValues();
985 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
986 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
987 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
988 String
[] whereArgs
= new String
[]{mAccount
.name
};
990 if (getContentResolver() != null
) {
991 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
995 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
997 } catch (RemoteException e
) {
998 Log_OC
.e(TAG
, "Exception in cleanSharedFiles" + e
.getMessage());
1003 private void cleanSharedFilesInFolder(OCFile folder
) {
1004 ContentValues cv
= new ContentValues();
1005 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
1006 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
1007 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PARENT
+ "=?";
1008 String
[] whereArgs
= new String
[] { mAccount
.name
, String
.valueOf(folder
.getFileId()) };
1010 if (getContentResolver() != null
) {
1011 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1015 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
1017 } catch (RemoteException e
) {
1018 Log_OC
.e(TAG
, "Exception in cleanSharedFilesInFolder " + e
.getMessage());
1023 private void cleanShares() {
1024 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1025 String
[] whereArgs
= new String
[]{mAccount
.name
};
1027 if (getContentResolver() != null
) {
1028 getContentResolver().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1032 getContentProviderClient().delete(ProviderTableMeta
.CONTENT_URI_SHARE
, where
, whereArgs
);
1034 } catch (RemoteException e
) {
1035 Log_OC
.e(TAG
, "Exception in cleanShares" + e
.getMessage());
1040 public void saveShares(Collection
<OCShare
> shares
) {
1042 if (shares
!= null
) {
1043 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(shares
.size());
1045 // prepare operations to insert or update files to save in the given folder
1046 for (OCShare share
: shares
) {
1047 ContentValues cv
= new ContentValues();
1048 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1049 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1050 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1051 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1052 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1053 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1054 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1055 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1056 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1057 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1058 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1059 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1060 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1061 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1063 if (shareExists(share
.getIdRemoteShared())) {
1064 // updating an existing file
1065 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI_SHARE
).
1067 withSelection( ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ "=?",
1068 new String
[] { String
.valueOf(share
.getIdRemoteShared()) })
1072 // adding a new file
1073 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1077 // apply operations in batch
1078 if (operations
.size() > 0) {
1079 @SuppressWarnings("unused")
1080 ContentProviderResult
[] results
= null
;
1081 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1083 if (getContentResolver() != null
) {
1084 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1087 results
= getContentProviderClient().applyBatch(operations
);
1090 } catch (OperationApplicationException e
) {
1091 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1093 } catch (RemoteException e
) {
1094 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1101 public void updateSharedFiles(Collection
<OCFile
> sharedFiles
) {
1104 if (sharedFiles
!= null
) {
1105 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(sharedFiles
.size());
1107 // prepare operations to insert or update files to save in the given folder
1108 for (OCFile file
: sharedFiles
) {
1109 ContentValues cv
= new ContentValues();
1110 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
1111 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
1112 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
1113 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
1114 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
1115 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
1116 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
1117 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
1118 if (!file
.isFolder()) {
1119 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
1121 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
1122 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
1123 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
1124 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
1125 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
1126 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
1127 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
1129 boolean existsByPath
= fileExists(file
.getRemotePath());
1130 if (existsByPath
|| fileExists(file
.getFileId())) {
1131 // updating an existing file
1132 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
1134 withSelection( ProviderTableMeta
._ID
+ "=?",
1135 new String
[] { String
.valueOf(file
.getFileId()) })
1139 // adding a new file
1140 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
1144 // apply operations in batch
1145 if (operations
.size() > 0) {
1146 @SuppressWarnings("unused")
1147 ContentProviderResult
[] results
= null
;
1148 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1150 if (getContentResolver() != null
) {
1151 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1154 results
= getContentProviderClient().applyBatch(operations
);
1157 } catch (OperationApplicationException e
) {
1158 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1160 } catch (RemoteException e
) {
1161 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1168 public void removeShare(OCShare share
){
1169 Uri share_uri
= ProviderTableMeta
.CONTENT_URI_SHARE
;
1170 String where
= ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
1171 String
[] whereArgs
= new String
[]{mAccount
.name
, share
.getPath()};
1172 if (getContentProviderClient() != null
) {
1174 getContentProviderClient().delete(share_uri
, where
, whereArgs
);
1175 } catch (RemoteException e
) {
1176 e
.printStackTrace();
1179 getContentResolver().delete(share_uri
, where
, whereArgs
);
1183 public void saveSharesDB(ArrayList
<OCShare
> shares
) {
1186 ArrayList
<OCFile
> sharedFiles
= new ArrayList
<OCFile
>();
1188 for (OCShare share
: shares
) {
1190 String path
= share
.getPath();
1191 if (share
.isFolder()) {
1192 path
= path
+ FileUtils
.PATH_SEPARATOR
;
1195 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1196 OCFile file
= getFileByPath(path
);
1198 if (share
.getShareType().equals(ShareType
.PUBLIC_LINK
)) {
1199 file
.setShareByLink(true
);
1200 sharedFiles
.add(file
);
1205 updateSharedFiles(sharedFiles
);
1209 public void saveSharesInFolder(ArrayList
<OCShare
> shares
, OCFile folder
) {
1210 cleanSharedFilesInFolder(folder
);
1211 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>();
1212 operations
= prepareRemoveSharesInFolder(folder
, operations
);
1214 if (shares
!= null
) {
1215 // prepare operations to insert or update files to save in the given folder
1216 for (OCShare share
: shares
) {
1217 ContentValues cv
= new ContentValues();
1218 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
1219 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
1220 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
1221 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
1222 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
1223 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
1224 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
1225 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
1226 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
1227 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
1228 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isFolder() ?
1 : 0);
1229 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
1230 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
1231 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
1234 if (shareExists(share.getIdRemoteShared())) {
1235 // updating an existing share resource
1236 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1238 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1239 new String[] { String.valueOf(share.getIdRemoteShared()) })
1244 // adding a new share resource
1245 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI_SHARE
).withValues(cv
).build());
1250 // apply operations in batch
1251 if (operations
.size() > 0) {
1252 @SuppressWarnings("unused")
1253 ContentProviderResult
[] results
= null
;
1254 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
1256 if (getContentResolver() != null
) {
1257 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
1260 results
= getContentProviderClient().applyBatch(operations
);
1263 } catch (OperationApplicationException e
) {
1264 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1266 } catch (RemoteException e
) {
1267 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
1274 private ArrayList
<ContentProviderOperation
> prepareRemoveSharesInFolder(OCFile folder
, ArrayList
<ContentProviderOperation
> preparedOperations
) {
1275 if (folder
!= null
) {
1276 String where
= ProviderTableMeta
.OCSHARES_PATH
+ "=?" + " AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
1277 String
[] whereArgs
= new String
[]{ "", mAccount
.name
};
1279 Vector
<OCFile
> files
= getFolderContent(folder
);
1281 for (OCFile file
: files
) {
1282 whereArgs
[0] = file
.getRemotePath();
1283 preparedOperations
.add(ContentProviderOperation
.newDelete(ProviderTableMeta
.CONTENT_URI_SHARE
)
1284 .withSelection(where
, whereArgs
)
1288 return preparedOperations
;
1291 if (operations.size() > 0) {
1293 if (getContentResolver() != null) {
1294 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1297 getContentProviderClient().applyBatch(operations);
1300 } catch (OperationApplicationException e) {
1301 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1303 } catch (RemoteException e) {
1304 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1310 if (getContentResolver() != null) {
1312 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1317 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1321 } catch (RemoteException e) {
1322 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());