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
.Collections
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
26 import java
.util
.Vector
;
28 import com
.owncloud
.android
.Log_OC
;
29 import com
.owncloud
.android
.MainApp
;
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
.ContentValues
;
39 import android
.content
.OperationApplicationException
;
40 import android
.database
.Cursor
;
41 import android
.net
.Uri
;
42 import android
.os
.RemoteException
;
44 public class FileDataStorageManager
implements DataStorageManager
{
46 private ContentResolver mContentResolver
;
47 private ContentProviderClient mContentProvider
;
48 private Account mAccount
;
50 private static String TAG
= "FileDataStorageManager";
52 public FileDataStorageManager(Account account
, ContentResolver cr
) {
53 mContentProvider
= null
;
54 mContentResolver
= cr
;
58 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
59 mContentProvider
= cp
;
60 mContentResolver
= null
;
65 public OCFile
getFileByPath(String path
) {
66 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
68 if (c
.moveToFirst()) {
69 file
= createFileInstance(c
);
72 if (file
== null
&& OCFile
.PATH_SEPARATOR
.equals(path
)) {
73 return createRootDir(); // root should always exist
79 private OCFile
createRootDir() {
80 OCFile file
= new OCFile(OCFile
.PATH_SEPARATOR
);
81 file
.setMimetype("DIR");
82 file
.setParentId(DataStorageManager
.ROOT_PARENT_ID
);
88 public OCFile
getFileById(long id
) {
89 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
91 if (c
.moveToFirst()) {
92 file
= createFileInstance(c
);
98 public OCFile
getFileByLocalPath(String path
) {
99 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
101 if (c
.moveToFirst()) {
102 file
= createFileInstance(c
);
109 public boolean fileExists(long id
) {
110 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
114 public boolean fileExists(String path
) {
115 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
119 public boolean saveFile(OCFile file
) {
120 boolean overriden
= false
;
121 ContentValues cv
= new ContentValues();
122 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
123 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
124 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
125 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
126 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
127 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
128 if (file
.getParentId() != 0)
129 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
130 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
131 if (!file
.isDirectory())
132 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
133 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
134 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
135 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
136 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
138 boolean sameRemotePath
= fileExists(file
.getRemotePath());
139 boolean changesSizeOfAncestors
= false
;
140 if (sameRemotePath
||
141 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
143 OCFile oldFile
= null
;
144 if (sameRemotePath
) {
145 oldFile
= getFileByPath(file
.getRemotePath());
146 file
.setFileId(oldFile
.getFileId());
148 oldFile
= getFileById(file
.getFileId());
150 changesSizeOfAncestors
= (oldFile
.getFileLength() != file
.getFileLength());
153 if (getContentResolver() != null
) {
154 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
155 ProviderTableMeta
._ID
+ "=?",
156 new String
[] { String
.valueOf(file
.getFileId()) });
159 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
160 cv
, ProviderTableMeta
._ID
+ "=?",
161 new String
[] { String
.valueOf(file
.getFileId()) });
162 } catch (RemoteException e
) {
164 "Fail to insert insert file to database "
169 changesSizeOfAncestors
= true
;
170 Uri result_uri
= null
;
171 if (getContentResolver() != null
) {
172 result_uri
= getContentResolver().insert(
173 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
176 result_uri
= getContentProvider().insert(
177 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
178 } catch (RemoteException e
) {
180 "Fail to insert insert file to database "
184 if (result_uri
!= null
) {
185 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
187 file
.setFileId(new_id
);
191 if (file
.isDirectory()) {
192 calculateFolderSize(file
.getFileId());
193 if (file
.needsUpdatingWhileSaving()) {
194 for (OCFile f
: getDirectoryContent(file
))
199 if (changesSizeOfAncestors
|| file
.isDirectory()) {
200 updateSizesToTheRoot(file
.getParentId());
208 public void saveFiles(List
<OCFile
> files
) {
210 Iterator
<OCFile
> filesIt
= files
.iterator();
211 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(files
.size());
214 // prepare operations to perform
215 while (filesIt
.hasNext()) {
216 file
= filesIt
.next();
217 ContentValues cv
= new ContentValues();
218 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
219 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
220 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
221 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
222 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
223 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
224 if (file
.getParentId() != 0)
225 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
226 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
227 if (!file
.isDirectory())
228 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
229 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
230 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
231 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
232 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
234 if (fileExists(file
.getRemotePath())) {
235 OCFile oldFile
= getFileByPath(file
.getRemotePath());
236 file
.setFileId(oldFile
.getFileId());
238 if (file
.isDirectory()) {
239 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, oldFile
.getFileLength());
240 file
.setFileLength(oldFile
.getFileLength());
243 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
245 withSelection( ProviderTableMeta
._ID
+ "=?",
246 new String
[] { String
.valueOf(file
.getFileId()) })
249 } else if (fileExists(file
.getFileId())) {
250 OCFile oldFile
= getFileById(file
.getFileId());
251 if (file
.getStoragePath() == null
&& oldFile
.getStoragePath() != null
)
252 file
.setStoragePath(oldFile
.getStoragePath());
254 if (!file
.isDirectory())
255 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
257 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, oldFile
.getFileLength());
258 file
.setFileLength(oldFile
.getFileLength());
261 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
263 withSelection( ProviderTableMeta
._ID
+ "=?",
264 new String
[] { String
.valueOf(file
.getFileId()) })
268 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
272 // apply operations in batch
273 ContentProviderResult
[] results
= null
;
275 if (getContentResolver() != null
) {
276 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
279 results
= getContentProvider().applyBatch(operations
);
282 } catch (OperationApplicationException e
) {
283 Log_OC
.e(TAG
, "Fail to update/insert list of files to database " + e
.getMessage());
285 } catch (RemoteException e
) {
286 Log_OC
.e(TAG
, "Fail to update/insert list of files to database " + e
.getMessage());
289 // update new id in file objects for insertions
290 if (results
!= null
) {
292 for (int i
=0; i
<results
.length
; i
++) {
293 if (results
[i
].uri
!= null
) {
294 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
295 files
.get(i
).setFileId(newId
);
296 //Log_OC.v(TAG, "Found and added id in insertion for " + files.get(i).getRemotePath());
301 for (OCFile aFile
: files
) {
302 if (aFile
.isDirectory() && aFile
.needsUpdatingWhileSaving())
303 saveFiles(getDirectoryContent(aFile
));
308 public void setAccount(Account account
) {
312 public Account
getAccount() {
316 public void setContentResolver(ContentResolver cr
) {
317 mContentResolver
= cr
;
320 public ContentResolver
getContentResolver() {
321 return mContentResolver
;
324 public void setContentProvider(ContentProviderClient cp
) {
325 mContentProvider
= cp
;
328 public ContentProviderClient
getContentProvider() {
329 return mContentProvider
;
333 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
334 if (f
!= null
&& f
.isDirectory() && f
.getFileId() != -1) {
335 return getDirectoryContent(f
.getFileId());
338 return new Vector
<OCFile
>();
342 private Vector
<OCFile
> getDirectoryContent(long parentId
) {
344 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
346 Uri req_uri
= Uri
.withAppendedPath(
347 ProviderTableMeta
.CONTENT_URI_DIR
,
348 String
.valueOf(parentId
));
351 if (getContentProvider() != null
) {
353 c
= getContentProvider().query(req_uri
, null
,
354 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
355 new String
[] { String
.valueOf(parentId
)}, null
);
356 } catch (RemoteException e
) {
357 Log_OC
.e(TAG
, e
.getMessage());
361 c
= getContentResolver().query(req_uri
, null
,
362 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
363 new String
[] { String
.valueOf(parentId
)}, null
);
366 if (c
.moveToFirst()) {
368 OCFile child
= createFileInstance(c
);
370 } while (c
.moveToNext());
375 Collections
.sort(ret
);
382 private boolean fileExists(String cmp_key
, String value
) {
384 if (getContentResolver() != null
) {
385 c
= getContentResolver()
386 .query(ProviderTableMeta
.CONTENT_URI
,
389 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
391 new String
[] { value
, mAccount
.name
}, null
);
394 c
= getContentProvider().query(
395 ProviderTableMeta
.CONTENT_URI
,
398 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
399 new String
[] { value
, mAccount
.name
}, null
);
400 } catch (RemoteException e
) {
402 "Couldn't determine file existance, assuming non existance: "
407 boolean retval
= c
.moveToFirst();
412 private Cursor
getCursorForValue(String key
, String value
) {
414 if (getContentResolver() != null
) {
415 c
= getContentResolver()
416 .query(ProviderTableMeta
.CONTENT_URI
,
419 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
421 new String
[] { value
, mAccount
.name
}, null
);
424 c
= getContentProvider().query(
425 ProviderTableMeta
.CONTENT_URI
,
427 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
428 + "=?", new String
[] { value
, mAccount
.name
},
430 } catch (RemoteException e
) {
431 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
438 private OCFile
createFileInstance(Cursor c
) {
441 file
= new OCFile(c
.getString(c
442 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
443 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
444 file
.setParentId(c
.getLong(c
445 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
446 file
.setMimetype(c
.getString(c
447 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
448 if (!file
.isDirectory()) {
449 file
.setStoragePath(c
.getString(c
450 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
451 if (file
.getStoragePath() == null
) {
452 // 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
453 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
455 file
.setStoragePath(f
.getAbsolutePath());
456 file
.setLastSyncDateForData(f
.lastModified());
460 file
.setFileLength(c
.getLong(c
461 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
462 file
.setCreationTimestamp(c
.getLong(c
463 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
464 file
.setModificationTimestamp(c
.getLong(c
465 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
466 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
467 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
468 file
.setLastSyncDateForProperties(c
.getLong(c
469 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
470 file
.setLastSyncDateForData(c
.getLong(c
.
471 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
472 file
.setKeepInSync(c
.getInt(
473 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
479 public void removeFile(OCFile file
, boolean removeLocalCopy
) {
480 Uri file_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, ""+file
.getFileId());
481 if (getContentProvider() != null
) {
483 getContentProvider().delete(file_uri
,
484 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
485 new String
[]{mAccount
.name
});
486 } catch (RemoteException e
) {
490 getContentResolver().delete(file_uri
,
491 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
492 new String
[]{mAccount
.name
});
494 if (file
.isDown() && removeLocalCopy
) {
495 new File(file
.getStoragePath()).delete();
497 if (file
.isDirectory() && removeLocalCopy
) {
498 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
499 if (f
.exists() && f
.isDirectory() && (f
.list() == null
|| f
.list().length
== 0)) {
504 if (file
.getFileLength() > 0) {
505 updateSizesToTheRoot(file
.getParentId());
510 public void removeDirectory(OCFile dir
, boolean removeDBData
, boolean removeLocalContent
) {
511 // TODO consider possible failures
512 if (dir
!= null
&& dir
.isDirectory() && dir
.getFileId() != -1) {
513 Vector
<OCFile
> children
= getDirectoryContent(dir
);
514 if (children
.size() > 0) {
516 for (int i
=0; i
<children
.size(); i
++) {
517 child
= children
.get(i
);
518 if (child
.isDirectory()) {
519 removeDirectory(child
, removeDBData
, removeLocalContent
);
522 removeFile(child
, removeLocalContent
);
523 } else if (removeLocalContent
) {
524 if (child
.isDown()) {
525 new File(child
.getStoragePath()).delete();
532 removeFile(dir
, true
);
535 if (dir
.getFileLength() > 0) {
536 updateSizesToTheRoot(dir
.getParentId());
543 * Updates database for a folder that was moved to a different location.
545 * TODO explore better (faster) implementations
546 * TODO throw exceptions up !
549 public void moveDirectory(OCFile dir
, String newPath
) {
550 // TODO check newPath
552 if (dir
!= null
&& dir
.isDirectory() && dir
.fileExists() && !dir
.getFileName().equals(OCFile
.PATH_SEPARATOR
)) {
553 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
555 if (getContentProvider() != null
) {
557 c
= getContentProvider().query(ProviderTableMeta
.CONTENT_URI
,
559 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
560 new String
[] { mAccount
.name
, dir
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
561 } catch (RemoteException e
) {
562 Log_OC
.e(TAG
, e
.getMessage());
565 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
567 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
568 new String
[] { mAccount
.name
, dir
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
571 /// 2. prepare a batch of update operations to change all the descendants
572 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
573 int lengthOfOldPath
= dir
.getRemotePath().length();
574 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
575 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
576 if (c
.moveToFirst()) {
578 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
579 OCFile child
= createFileInstance(c
);
580 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
581 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
582 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
584 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
586 withSelection( ProviderTableMeta
._ID
+ "=?",
587 new String
[] { String
.valueOf(child
.getFileId()) })
589 } while (c
.moveToNext());
593 /// 3. apply updates in batch
595 if (getContentResolver() != null
) {
596 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
599 getContentProvider().applyBatch(operations
);
602 } catch (OperationApplicationException e
) {
603 Log_OC
.e(TAG
, "Fail to update descendants of " + dir
.getFileId() + " in database", e
);
605 } catch (RemoteException e
) {
606 Log_OC
.e(TAG
, "Fail to update desendants of " + dir
.getFileId() + " in database", e
);
613 public Vector
<OCFile
> getDirectoryImages(OCFile directory
) {
614 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
615 if (directory
!= null
) {
616 // TODO better implementation, filtering in the access to database (if possible) instead of here
617 Vector
<OCFile
> tmp
= getDirectoryContent(directory
);
618 OCFile current
= null
;
619 for (int i
=0; i
<tmp
.size(); i
++) {
620 current
= tmp
.get(i
);
621 if (current
.isImage()) {
630 * Calculate and save the folderSize on DB
634 public void calculateFolderSize(long id
) {
637 Vector
<OCFile
> files
= getDirectoryContent(id
);
639 for (OCFile f
: files
)
641 folderSize
= folderSize
+ f
.getFileLength();
644 updateSize(id
, folderSize
);
648 * Update the size value of an OCFile in DB
650 private int updateSize(long id
, long size
) {
651 ContentValues cv
= new ContentValues();
652 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, size
);
654 if (getContentResolver() != null
) {
655 result
= getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, ProviderTableMeta
._ID
+ "=?",
656 new String
[] { String
.valueOf(id
) });
659 result
= getContentProvider().update(ProviderTableMeta
.CONTENT_URI
, cv
, ProviderTableMeta
._ID
+ "=?",
660 new String
[] { String
.valueOf(id
) });
661 } catch (RemoteException e
) {
662 Log_OC
.e(TAG
,"Fail to update size column into database " + e
.getMessage());
669 * Update the size of a subtree of folder from a file to the root
670 * @param parentId: parent of the file
672 private void updateSizesToTheRoot(long parentId
) {
676 while (parentId
!= 0) {
678 // Update the size of the parent
679 calculateFolderSize(parentId
);
681 // search the next parent
682 file
= getFileById(parentId
);
683 parentId
= file
.getParentId();