1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.db
.ProviderMeta
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.utils
.FileStorageUtils
;
32 import android
.accounts
.Account
;
33 import android
.content
.ContentProviderClient
;
34 import android
.content
.ContentProviderOperation
;
35 import android
.content
.ContentProviderResult
;
36 import android
.content
.ContentResolver
;
37 import android
.content
.ContentValues
;
38 import android
.content
.OperationApplicationException
;
39 import android
.database
.Cursor
;
40 import android
.net
.Uri
;
41 import android
.os
.RemoteException
;
42 import android
.util
.Log
;
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
);
76 public OCFile
getFileById(long id
) {
77 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
79 if (c
.moveToFirst()) {
80 file
= createFileInstance(c
);
86 public OCFile
getFileByLocalPath(String path
) {
87 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
89 if (c
.moveToFirst()) {
90 file
= createFileInstance(c
);
97 public boolean fileExists(long id
) {
98 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
102 public boolean fileExists(String path
) {
103 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
107 public boolean saveFile(OCFile file
) {
108 boolean overriden
= false
;
109 ContentValues cv
= new ContentValues();
110 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
111 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
112 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
113 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
114 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
115 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
116 if (file
.getParentId() != 0)
117 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
118 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
119 if (!file
.isDirectory())
120 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
121 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
122 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
123 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
124 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
126 boolean sameRemotePath
= fileExists(file
.getRemotePath());
127 if (sameRemotePath
||
128 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
130 if (sameRemotePath
) {
131 OCFile oldFile
= getFileByPath(file
.getRemotePath());
132 file
.setFileId(oldFile
.getFileId());
136 if (getContentResolver() != null
) {
137 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
138 ProviderTableMeta
._ID
+ "=?",
139 new String
[] { String
.valueOf(file
.getFileId()) });
142 getContentProvider().update(ProviderTableMeta
.CONTENT_URI
,
143 cv
, ProviderTableMeta
._ID
+ "=?",
144 new String
[] { String
.valueOf(file
.getFileId()) });
145 } catch (RemoteException e
) {
147 "Fail to insert insert file to database "
152 Uri result_uri
= null
;
153 if (getContentResolver() != null
) {
154 result_uri
= getContentResolver().insert(
155 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
158 result_uri
= getContentProvider().insert(
159 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
160 } catch (RemoteException e
) {
162 "Fail to insert insert file to database "
166 if (result_uri
!= null
) {
167 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
169 file
.setFileId(new_id
);
173 if (file
.isDirectory() && file
.needsUpdatingWhileSaving())
174 for (OCFile f
: getDirectoryContent(file
))
182 public void saveFiles(List
<OCFile
> files
) {
184 Iterator
<OCFile
> filesIt
= files
.iterator();
185 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(files
.size());
188 // prepare operations to perform
189 while (filesIt
.hasNext()) {
190 file
= filesIt
.next();
191 ContentValues cv
= new ContentValues();
192 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
193 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
194 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
195 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
196 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
197 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
198 if (file
.getParentId() != 0)
199 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
200 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
201 if (!file
.isDirectory())
202 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
203 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
204 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
205 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
206 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
208 if (fileExists(file
.getRemotePath())) {
209 OCFile oldFile
= getFileByPath(file
.getRemotePath());
210 file
.setFileId(oldFile
.getFileId());
211 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
213 withSelection( ProviderTableMeta
._ID
+ "=?",
214 new String
[] { String
.valueOf(file
.getFileId()) })
217 } else if (fileExists(file
.getFileId())) {
218 OCFile oldFile
= getFileById(file
.getFileId());
219 if (file
.getStoragePath() == null
&& oldFile
.getStoragePath() != null
)
220 file
.setStoragePath(oldFile
.getStoragePath());
221 if (!file
.isDirectory());
222 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
224 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
226 withSelection( ProviderTableMeta
._ID
+ "=?",
227 new String
[] { String
.valueOf(file
.getFileId()) })
231 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
235 // apply operations in batch
236 ContentProviderResult
[] results
= null
;
238 if (getContentResolver() != null
) {
239 results
= getContentResolver().applyBatch(ProviderMeta
.AUTHORITY_FILES
, operations
);
242 results
= getContentProvider().applyBatch(operations
);
245 } catch (OperationApplicationException e
) {
246 Log
.e(TAG
, "Fail to update/insert list of files to database " + e
.getMessage());
248 } catch (RemoteException e
) {
249 Log
.e(TAG
, "Fail to update/insert list of files to database " + e
.getMessage());
252 // update new id in file objects for insertions
253 if (results
!= null
) {
255 for (int i
=0; i
<results
.length
; i
++) {
256 if (results
[i
].uri
!= null
) {
257 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
258 files
.get(i
).setFileId(newId
);
259 //Log.v(TAG, "Found and added id in insertion for " + files.get(i).getRemotePath());
264 for (OCFile aFile
: files
) {
265 if (aFile
.isDirectory() && aFile
.needsUpdatingWhileSaving())
266 saveFiles(getDirectoryContent(aFile
));
271 public void setAccount(Account account
) {
275 public Account
getAccount() {
279 public void setContentResolver(ContentResolver cr
) {
280 mContentResolver
= cr
;
283 public ContentResolver
getContentResolver() {
284 return mContentResolver
;
287 public void setContentProvider(ContentProviderClient cp
) {
288 mContentProvider
= cp
;
291 public ContentProviderClient
getContentProvider() {
292 return mContentProvider
;
296 public Vector
<OCFile
> getDirectoryContent(OCFile f
) {
297 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
298 if (f
!= null
&& f
.isDirectory() && f
.getFileId() != -1) {
300 Uri req_uri
= Uri
.withAppendedPath(
301 ProviderTableMeta
.CONTENT_URI_DIR
,
302 String
.valueOf(f
.getFileId()));
305 if (getContentProvider() != null
) {
307 c
= getContentProvider().query(req_uri
, null
,
308 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
309 new String
[] { mAccount
.name
}, null
);
310 } catch (RemoteException e
) {
311 Log
.e(TAG
, e
.getMessage());
315 c
= getContentResolver().query(req_uri
, null
,
316 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
317 new String
[] { mAccount
.name
}, null
);
320 if (c
.moveToFirst()) {
322 OCFile child
= createFileInstance(c
);
324 } while (c
.moveToNext());
329 Collections
.sort(ret
);
335 private boolean fileExists(String cmp_key
, String value
) {
337 if (getContentResolver() != null
) {
338 c
= getContentResolver()
339 .query(ProviderTableMeta
.CONTENT_URI
,
342 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
344 new String
[] { value
, mAccount
.name
}, null
);
347 c
= getContentProvider().query(
348 ProviderTableMeta
.CONTENT_URI
,
351 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
352 new String
[] { value
, mAccount
.name
}, null
);
353 } catch (RemoteException e
) {
355 "Couldn't determine file existance, assuming non existance: "
360 boolean retval
= c
.moveToFirst();
365 private Cursor
getCursorForValue(String key
, String value
) {
367 if (getContentResolver() != null
) {
368 c
= getContentResolver()
369 .query(ProviderTableMeta
.CONTENT_URI
,
372 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
374 new String
[] { value
, mAccount
.name
}, null
);
377 c
= getContentProvider().query(
378 ProviderTableMeta
.CONTENT_URI
,
380 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
381 + "=?", new String
[] { value
, mAccount
.name
},
383 } catch (RemoteException e
) {
384 Log
.e(TAG
, "Could not get file details: " + e
.getMessage());
391 private OCFile
createFileInstance(Cursor c
) {
394 file
= new OCFile(c
.getString(c
395 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
396 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
397 file
.setParentId(c
.getLong(c
398 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
399 file
.setMimetype(c
.getString(c
400 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
401 if (!file
.isDirectory()) {
402 file
.setStoragePath(c
.getString(c
403 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
404 if (file
.getStoragePath() == null
) {
405 // 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
406 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
408 file
.setStoragePath(f
.getAbsolutePath());
409 file
.setLastSyncDateForData(f
.lastModified());
413 file
.setFileLength(c
.getLong(c
414 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
415 file
.setCreationTimestamp(c
.getLong(c
416 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
417 file
.setModificationTimestamp(c
.getLong(c
418 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
419 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
420 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
421 file
.setLastSyncDateForProperties(c
.getLong(c
422 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
423 file
.setLastSyncDateForData(c
.getLong(c
.
424 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
425 file
.setKeepInSync(c
.getInt(
426 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
432 public void removeFile(OCFile file
, boolean removeLocalCopy
) {
433 Uri file_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, ""+file
.getFileId());
434 if (getContentProvider() != null
) {
436 getContentProvider().delete(file_uri
,
437 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
438 new String
[]{mAccount
.name
});
439 } catch (RemoteException e
) {
443 getContentResolver().delete(file_uri
,
444 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
445 new String
[]{mAccount
.name
});
447 if (file
.isDown() && removeLocalCopy
) {
448 new File(file
.getStoragePath()).delete();
450 if (file
.isDirectory() && removeLocalCopy
) {
451 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
452 if (f
.exists() && f
.isDirectory() && (f
.list() == null
|| f
.list().length
== 0)) {
459 public void removeDirectory(OCFile dir
, boolean removeDBData
, boolean removeLocalContent
) {
460 // TODO consider possible failures
461 if (dir
!= null
&& dir
.isDirectory() && dir
.getFileId() != -1) {
462 Vector
<OCFile
> children
= getDirectoryContent(dir
);
463 if (children
.size() > 0) {
465 for (int i
=0; i
<children
.size(); i
++) {
466 child
= children
.get(i
);
467 if (child
.isDirectory()) {
468 removeDirectory(child
, removeDBData
, removeLocalContent
);
471 removeFile(child
, removeLocalContent
);
472 } else if (removeLocalContent
) {
473 if (child
.isDown()) {
474 new File(child
.getStoragePath()).delete();
480 removeFile(dir
, true
);
488 * Updates database for a folder that was moved to a different location.
490 * TODO explore better (faster) implementations
491 * TODO throw exceptions up !
494 public void moveDirectory(OCFile dir
, String newPath
) {
495 // TODO check newPath
497 if (dir
!= null
&& dir
.isDirectory() && dir
.fileExists() && !dir
.getFileName().equals(OCFile
.PATH_SEPARATOR
)) {
498 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
500 if (getContentProvider() != null
) {
502 c
= getContentProvider().query(ProviderTableMeta
.CONTENT_URI
,
504 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ?",
505 new String
[] { mAccount
.name
, dir
.getRemotePath() + "%" }, null
);
506 } catch (RemoteException e
) {
507 Log
.e(TAG
, e
.getMessage());
510 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
512 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ?",
513 new String
[] { mAccount
.name
, dir
.getRemotePath() + "%" }, null
);
516 /// 2. prepare a batch of update operations to change all the descendants
517 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
518 int lengthOfOldPath
= dir
.getRemotePath().length();
519 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
520 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
521 if (c
.moveToFirst()) {
523 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
524 OCFile child
= createFileInstance(c
);
525 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
526 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
527 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
529 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
531 withSelection( ProviderTableMeta
._ID
+ "=?",
532 new String
[] { String
.valueOf(child
.getFileId()) })
534 } while (c
.moveToNext());
538 /// 3. apply updates in batch
540 if (getContentResolver() != null
) {
541 getContentResolver().applyBatch(ProviderMeta
.AUTHORITY_FILES
, operations
);
544 getContentProvider().applyBatch(operations
);
547 } catch (OperationApplicationException e
) {
548 Log
.e(TAG
, "Fail to update descendants of " + dir
.getFileId() + " in database", e
);
550 } catch (RemoteException e
) {
551 Log
.e(TAG
, "Fail to update desendants of " + dir
.getFileId() + " in database", e
);