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
.Log_OC
;
29 import com
.owncloud
.android
.db
.ProviderMeta
;
30 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
31 import com
.owncloud
.android
.utils
.FileStorageUtils
;
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
.OperationApplicationException
;
41 import android
.database
.Cursor
;
42 import android
.net
.Uri
;
43 import android
.os
.RemoteException
;
45 public class FileDataStorageManager
{
47 public static final int ROOT_PARENT_ID
= 0;
49 private ContentResolver mContentResolver
;
50 private ContentProviderClient mContentProviderClient
;
51 private Account mAccount
;
53 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
56 public FileDataStorageManager(Account account
, ContentResolver cr
) {
57 mContentProviderClient
= null
;
58 mContentResolver
= cr
;
62 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
63 mContentProviderClient
= cp
;
64 mContentResolver
= null
;
69 public void setAccount(Account account
) {
73 public Account
getAccount() {
77 public void setContentResolver(ContentResolver cr
) {
78 mContentResolver
= cr
;
81 public ContentResolver
getContentResolver() {
82 return mContentResolver
;
85 public void setContentProviderClient(ContentProviderClient cp
) {
86 mContentProviderClient
= cp
;
89 public ContentProviderClient
getContentProviderClient() {
90 return mContentProviderClient
;
94 public OCFile
getFileByPath(String path
) {
95 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
97 if (c
.moveToFirst()) {
98 file
= createFileInstance(c
);
101 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
102 return createRootDir(); // root should always exist
108 public OCFile
getFileById(long id
) {
109 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
111 if (c
.moveToFirst()) {
112 file
= createFileInstance(c
);
118 public OCFile
getFileByLocalPath(String path
) {
119 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
121 if (c
.moveToFirst()) {
122 file
= createFileInstance(c
);
128 public boolean fileExists(long id
) {
129 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
132 public boolean fileExists(String path
) {
133 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
137 public Vector
<OCFile
> getFolderContent(OCFile f
) {
138 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
139 return getFolderContent(f
.getFileId());
142 return new Vector
<OCFile
>();
147 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
148 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
149 if (folder
!= null
) {
150 // TODO better implementation, filtering in the access to database (if possible) instead of here
151 Vector
<OCFile
> tmp
= getFolderContent(folder
);
152 OCFile current
= null
;
153 for (int i
=0; i
<tmp
.size(); i
++) {
154 current
= tmp
.get(i
);
155 if (current
.isImage()) {
164 public boolean saveFile(OCFile file
) {
165 boolean overriden
= false
;
166 ContentValues cv
= new ContentValues();
167 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
168 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
169 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
170 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
171 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
172 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
173 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
174 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
175 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
176 if (!file
.isFolder())
177 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
178 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
179 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
180 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
181 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
182 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
184 boolean sameRemotePath
= fileExists(file
.getRemotePath());
185 if (sameRemotePath
||
186 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
188 OCFile oldFile
= null
;
189 if (sameRemotePath
) {
190 oldFile
= getFileByPath(file
.getRemotePath());
191 file
.setFileId(oldFile
.getFileId());
193 oldFile
= getFileById(file
.getFileId());
197 if (getContentResolver() != null
) {
198 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
199 ProviderTableMeta
._ID
+ "=?",
200 new String
[] { String
.valueOf(file
.getFileId()) });
203 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
204 cv
, ProviderTableMeta
._ID
+ "=?",
205 new String
[] { String
.valueOf(file
.getFileId()) });
206 } catch (RemoteException e
) {
208 "Fail to insert insert file to database "
213 Uri result_uri
= null
;
214 if (getContentResolver() != null
) {
215 result_uri
= getContentResolver().insert(
216 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
219 result_uri
= getContentProviderClient().insert(
220 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
221 } catch (RemoteException e
) {
223 "Fail to insert insert file to database "
227 if (result_uri
!= null
) {
228 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
230 file
.setFileId(new_id
);
234 if (file
.isFolder()) {
235 updateFolderSize(file
.getFileId());
236 if (file
.needsUpdatingWhileSaving()) {
237 for (OCFile f
: getFolderContent(file
))
247 * Inserts or updates the list of files contained in a given folder.
251 * @param removeNotUpdated
253 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
255 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
257 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
260 // prepare operations to insert or update files to save in the given folder
261 for (OCFile file
: updatedFiles
) {
262 ContentValues cv
= new ContentValues();
263 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
264 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
265 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
266 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
267 folderSize
+= file
.getFileLength();
268 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
269 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
270 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
271 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
272 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
273 if (!file
.isFolder()) {
274 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
276 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
277 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
278 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
279 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
280 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
282 boolean existsByPath
= fileExists(file
.getRemotePath());
283 if (existsByPath
|| fileExists(file
.getFileId())) {
284 // updating an existing file
286 /* CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
288 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
291 OCFile oldFile = null;
294 oldFile = getFileByPath(file.getRemotePath());
295 file.setFileId(oldFile.getFileId());
297 oldFile = getFileById(file.getFileId());
300 if (file.isFolder()) {
301 // folders keep size information, since it's calculated
302 file.setFileLength(oldFile.getFileLength());
303 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, oldFile.getFileLength());
305 } else if (file.getStoragePath() == null && oldFile.getStoragePath() != null) {
306 // regular files keep access to local contents, although it's lost in the new OCFile
307 file.setStoragePath(oldFile.getStoragePath());
308 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, oldFile.getStoragePath());
312 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
314 withSelection( ProviderTableMeta
._ID
+ "=?",
315 new String
[] { String
.valueOf(file
.getFileId()) })
320 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
324 // prepare operations to remove files in the given folder
325 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
326 String
[] whereArgs
= null
;
327 for (OCFile file
: filesToRemove
) {
328 if (file
.getParentId() == folder
.getFileId()) {
329 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
330 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
331 if (file
.isFolder()) {
332 operations
.add(ContentProviderOperation
333 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
335 // TODO remove local folder
337 operations
.add(ContentProviderOperation
338 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
341 new File(file
.getStoragePath()).delete();
342 // TODO move the deletion of local contents after success of deletions
348 // update metadata of folder
349 ContentValues cv
= new ContentValues();
350 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
351 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
352 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
353 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, folderSize
);
354 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
355 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
356 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
357 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
358 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
359 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
360 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
361 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
362 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
363 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
365 withSelection( ProviderTableMeta
._ID
+ "=?",
366 new String
[] { String
.valueOf(folder
.getFileId()) })
369 // apply operations in batch
370 ContentProviderResult
[] results
= null
;
371 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
373 if (getContentResolver() != null
) {
374 results
= getContentResolver().applyBatch(ProviderMeta
.AUTHORITY_FILES
, operations
);
377 results
= getContentProviderClient().applyBatch(operations
);
380 } catch (OperationApplicationException e
) {
381 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
383 } catch (RemoteException e
) {
384 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
387 // update new id in file objects for insertions
388 if (results
!= null
) {
390 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
392 for (int i
=0; i
<results
.length
; i
++) {
393 if (filesIt
.hasNext()) {
394 file
= filesIt
.next();
398 if (results
[i
].uri
!= null
) {
399 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
400 //updatedFiles.get(i).setFileId(newId);
402 file
.setFileId(newId
);
415 public void updateFolderSize(long id
) {
416 if (getContentResolver() != null
) {
417 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_DIR
, null
,
418 ProviderTableMeta
._ID
+ "=?",
419 new String
[] { String
.valueOf(id
) });
422 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_DIR
, null
,
423 ProviderTableMeta
._ID
+ "=?",
424 new String
[] { String
.valueOf(id
) });
426 } catch (RemoteException e
) {
427 Log_OC
.e(TAG
, "Exception in update of folder size through compatibility patch " + e
.getMessage());
433 public void removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
435 if (file
.isFolder()) {
436 removeFolder(file
, removeDBData
, removeLocalCopy
);
440 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
441 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
442 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
443 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
444 if (getContentProviderClient() != null
) {
446 getContentProviderClient().delete(file_uri
, where
, whereArgs
);
447 } catch (RemoteException e
) {
451 getContentResolver().delete(file_uri
, where
, whereArgs
);
454 if (removeLocalCopy
&& file
.isDown()) {
455 boolean success
= new File(file
.getStoragePath()).delete();
456 if (!removeDBData
&& success
) {
457 // maybe unnecessary, but should be checked TODO remove if unnecessary
458 file
.setStoragePath(null
);
467 public void removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
468 if (folder
!= null
&& folder
.isFolder()) {
469 if (removeDBData
&& folder
.getFileId() != -1) {
470 removeFolderInDb(folder
);
472 if (removeLocalContent
) {
473 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
474 removeLocalFolder(localFolder
);
479 private void removeFolderInDb(OCFile folder
) {
480 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
481 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
482 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
483 if (getContentProviderClient() != null
) {
485 getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
486 } catch (RemoteException e
) {
490 getContentResolver().delete(folder_uri
, where
, whereArgs
);
494 private void removeLocalFolder(File folder
) {
495 if (folder
.exists()) {
496 File
[] files
= folder
.listFiles();
498 for (File file
: files
) {
499 if (file
.isDirectory()) {
500 removeLocalFolder(file
);
511 * Updates database for a folder that was moved to a different location.
513 * TODO explore better (faster) implementations
514 * TODO throw exceptions up !
516 public void moveFolder(OCFile folder
, String newPath
) {
517 // TODO check newPath
519 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
520 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
522 if (getContentProviderClient() != null
) {
524 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
526 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
527 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
528 } catch (RemoteException e
) {
529 Log_OC
.e(TAG
, e
.getMessage());
532 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
534 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
535 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
538 /// 2. prepare a batch of update operations to change all the descendants
539 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
540 int lengthOfOldPath
= folder
.getRemotePath().length();
541 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
542 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
543 if (c
.moveToFirst()) {
545 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
546 OCFile child
= createFileInstance(c
);
547 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
548 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
549 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
551 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
553 withSelection( ProviderTableMeta
._ID
+ "=?",
554 new String
[] { String
.valueOf(child
.getFileId()) })
556 } while (c
.moveToNext());
560 /// 3. apply updates in batch
562 if (getContentResolver() != null
) {
563 getContentResolver().applyBatch(ProviderMeta
.AUTHORITY_FILES
, operations
);
566 getContentProviderClient().applyBatch(operations
);
569 } catch (OperationApplicationException e
) {
570 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
572 } catch (RemoteException e
) {
573 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
580 private Vector
<OCFile
> getFolderContent(long parentId
) {
582 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
584 Uri req_uri
= Uri
.withAppendedPath(
585 ProviderTableMeta
.CONTENT_URI_DIR
,
586 String
.valueOf(parentId
));
589 if (getContentProviderClient() != null
) {
591 c
= getContentProviderClient().query(req_uri
, null
,
592 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
593 new String
[] { String
.valueOf(parentId
)}, null
);
594 } catch (RemoteException e
) {
595 Log_OC
.e(TAG
, e
.getMessage());
599 c
= getContentResolver().query(req_uri
, null
,
600 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
601 new String
[] { String
.valueOf(parentId
)}, null
);
604 if (c
.moveToFirst()) {
606 OCFile child
= createFileInstance(c
);
608 } while (c
.moveToNext());
613 Collections
.sort(ret
);
619 private OCFile
createRootDir() {
620 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
621 file
.setMimetype("DIR");
622 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
627 private boolean fileExists(String cmp_key
, String value
) {
629 if (getContentResolver() != null
) {
630 c
= getContentResolver()
631 .query(ProviderTableMeta
.CONTENT_URI
,
634 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
636 new String
[] { value
, mAccount
.name
}, null
);
639 c
= getContentProviderClient().query(
640 ProviderTableMeta
.CONTENT_URI
,
643 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
644 new String
[] { value
, mAccount
.name
}, null
);
645 } catch (RemoteException e
) {
647 "Couldn't determine file existance, assuming non existance: "
652 boolean retval
= c
.moveToFirst();
657 private Cursor
getCursorForValue(String key
, String value
) {
659 if (getContentResolver() != null
) {
660 c
= getContentResolver()
661 .query(ProviderTableMeta
.CONTENT_URI
,
664 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
666 new String
[] { value
, mAccount
.name
}, null
);
669 c
= getContentProviderClient().query(
670 ProviderTableMeta
.CONTENT_URI
,
672 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
673 + "=?", new String
[] { value
, mAccount
.name
},
675 } catch (RemoteException e
) {
676 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
683 private OCFile
createFileInstance(Cursor c
) {
686 file
= new OCFile(c
.getString(c
687 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
688 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
689 file
.setParentId(c
.getLong(c
690 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
691 file
.setMimetype(c
.getString(c
692 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
693 if (!file
.isFolder()) {
694 file
.setStoragePath(c
.getString(c
695 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
696 if (file
.getStoragePath() == null
) {
697 // 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
698 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
700 file
.setStoragePath(f
.getAbsolutePath());
701 file
.setLastSyncDateForData(f
.lastModified());
705 file
.setFileLength(c
.getLong(c
706 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
707 file
.setCreationTimestamp(c
.getLong(c
708 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
709 file
.setModificationTimestamp(c
.getLong(c
710 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
711 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
712 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
713 file
.setLastSyncDateForProperties(c
.getLong(c
714 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
715 file
.setLastSyncDateForData(c
.getLong(c
.
716 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
717 file
.setKeepInSync(c
.getInt(
718 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
719 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
726 * Update the size value of an OCFile in DB
728 private int updateSize(long id, long size) {
729 ContentValues cv = new ContentValues();
730 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, size);
732 if (getContentResolver() != null) {
733 result = getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
734 new String[] { String.valueOf(id) });
737 result = getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
738 new String[] { String.valueOf(id) });
739 } catch (RemoteException e) {
740 Log_OC.e(TAG,"Fail to update size column into database " + e.getMessage());
748 * Update the size of a subtree of folder from a file to the root
749 * @param parentId: parent of the file
751 private void updateSizesToTheRoot(long parentId) {
755 while (parentId != FileDataStorageManager.ROOT_PARENT_ID) {
757 // Update the size of the parent
758 updateFolderSize(parentId);
760 // search the next parent
761 file = getFileById(parentId);
762 parentId = file.getParentId();