39ddb927e900f8097b97c45c1cc6762c34914c79
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / FileDataStorageManager.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.datamodel;
20
21 import java.io.File;
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;
27
28 import com.owncloud.android.DisplayUtils;
29 import com.owncloud.android.Log_OC;
30 import com.owncloud.android.db.ProviderMeta;
31 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
32 import com.owncloud.android.utils.FileStorageUtils;
33
34 import android.accounts.Account;
35 import android.content.ContentProviderClient;
36 import android.content.ContentProviderOperation;
37 import android.content.ContentProviderResult;
38 import android.content.ContentResolver;
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;
44
45 public class FileDataStorageManager {
46
47 public static final int ROOT_PARENT_ID = 0;
48
49 private ContentResolver mContentResolver;
50 private ContentProviderClient mContentProvider;
51 private Account mAccount;
52
53 private static String TAG = "FileDataStorageManager";
54
55 public FileDataStorageManager(Account account, ContentResolver cr) {
56 mContentProvider = null;
57 mContentResolver = cr;
58 mAccount = account;
59 }
60
61 public FileDataStorageManager(Account account, ContentProviderClient cp) {
62 mContentProvider = cp;
63 mContentResolver = null;
64 mAccount = account;
65 }
66
67
68 public OCFile getFileByPath(String path) {
69 Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
70 OCFile file = null;
71 if (c.moveToFirst()) {
72 file = createFileInstance(c);
73 }
74 c.close();
75 if (file == null && OCFile.PATH_SEPARATOR.equals(path)) {
76 return createRootDir(); // root should always exist
77 }
78 return file;
79 }
80
81
82 private OCFile createRootDir() {
83 OCFile file = new OCFile(OCFile.PATH_SEPARATOR);
84 file.setMimetype("DIR");
85 file.setParentId(FileDataStorageManager.ROOT_PARENT_ID);
86 saveFile(file);
87 return file;
88 }
89
90 public OCFile getFileById(long id) {
91 Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
92 OCFile file = null;
93 if (c.moveToFirst()) {
94 file = createFileInstance(c);
95 }
96 c.close();
97 return file;
98 }
99
100 public OCFile getFileByLocalPath(String path) {
101 Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
102 OCFile file = null;
103 if (c.moveToFirst()) {
104 file = createFileInstance(c);
105 }
106 c.close();
107 return file;
108 }
109
110 public boolean fileExists(long id) {
111 return fileExists(ProviderTableMeta._ID, String.valueOf(id));
112 }
113
114 public boolean fileExists(String path) {
115 return fileExists(ProviderTableMeta.FILE_PATH, path);
116 }
117
118 public boolean saveFile(OCFile file) {
119 boolean overriden = false;
120 ContentValues cv = new ContentValues();
121 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
122 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
123 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
124 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
125 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
126 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
127 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
128 cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
129 cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
130 if (!file.isDirectory())
131 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
132 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
133 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
134 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
135 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
136 cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
137
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
142
143 OCFile oldFile = null;
144 if (sameRemotePath) {
145 oldFile = getFileByPath(file.getRemotePath());
146 file.setFileId(oldFile.getFileId());
147 } else {
148 oldFile = getFileById(file.getFileId());
149 }
150 changesSizeOfAncestors = (oldFile.getFileLength() != file.getFileLength());
151
152 overriden = true;
153 if (getContentResolver() != null) {
154 getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
155 ProviderTableMeta._ID + "=?",
156 new String[] { String.valueOf(file.getFileId()) });
157 } else {
158 try {
159 getContentProvider().update(ProviderTableMeta.CONTENT_URI,
160 cv, ProviderTableMeta._ID + "=?",
161 new String[] { String.valueOf(file.getFileId()) });
162 } catch (RemoteException e) {
163 Log_OC.e(TAG,
164 "Fail to insert insert file to database "
165 + e.getMessage());
166 }
167 }
168 } else {
169 changesSizeOfAncestors = true;
170 Uri result_uri = null;
171 if (getContentResolver() != null) {
172 result_uri = getContentResolver().insert(
173 ProviderTableMeta.CONTENT_URI_FILE, cv);
174 } else {
175 try {
176 result_uri = getContentProvider().insert(
177 ProviderTableMeta.CONTENT_URI_FILE, cv);
178 } catch (RemoteException e) {
179 Log_OC.e(TAG,
180 "Fail to insert insert file to database "
181 + e.getMessage());
182 }
183 }
184 if (result_uri != null) {
185 long new_id = Long.parseLong(result_uri.getPathSegments()
186 .get(1));
187 file.setFileId(new_id);
188 }
189 }
190
191 if (file.isDirectory()) {
192 calculateFolderSize(file.getFileId());
193 if (file.needsUpdatingWhileSaving()) {
194 for (OCFile f : getDirectoryContent(file))
195 saveFile(f);
196 }
197 }
198
199 if (changesSizeOfAncestors || file.isDirectory()) {
200 updateSizesToTheRoot(file.getParentId());
201 }
202
203 return overriden;
204 }
205
206
207 public void saveFiles(List<OCFile> files) {
208
209 Iterator<OCFile> filesIt = files.iterator();
210 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(files.size());
211 OCFile file = null;
212
213 // prepare operations to perform
214 while (filesIt.hasNext()) {
215 file = filesIt.next();
216 ContentValues cv = new ContentValues();
217 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
218 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
219 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
220 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
221 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
222 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
223 if (file.getParentId() != FileDataStorageManager.ROOT_PARENT_ID)
224 cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
225 cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
226 if (!file.isDirectory())
227 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
228 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
229 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
230 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
231 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
232 cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
233
234 if (fileExists(file.getRemotePath())) {
235 OCFile oldFile = getFileByPath(file.getRemotePath());
236 file.setFileId(oldFile.getFileId());
237
238 if (file.isDirectory()) {
239 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, oldFile.getFileLength());
240 file.setFileLength(oldFile.getFileLength());
241 }
242
243 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
244 withValues(cv).
245 withSelection( ProviderTableMeta._ID + "=?",
246 new String[] { String.valueOf(file.getFileId()) })
247 .build());
248
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());
253
254 if (!file.isDirectory())
255 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
256 else {
257 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, oldFile.getFileLength());
258 file.setFileLength(oldFile.getFileLength());
259 }
260
261 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
262 withValues(cv).
263 withSelection( ProviderTableMeta._ID + "=?",
264 new String[] { String.valueOf(file.getFileId()) })
265 .build());
266
267 } else {
268 operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build());
269 }
270 }
271
272 // apply operations in batch
273 ContentProviderResult[] results = null;
274 try {
275 if (getContentResolver() != null) {
276 results = getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations);
277
278 } else {
279 results = getContentProvider().applyBatch(operations);
280 }
281
282 } catch (OperationApplicationException e) {
283 Log_OC.e(TAG, "Fail to update/insert list of files to database " + e.getMessage());
284
285 } catch (RemoteException e) {
286 Log_OC.e(TAG, "Fail to update/insert list of files to database " + e.getMessage());
287 }
288
289 // update new id in file objects for insertions
290 if (results != null) {
291 long newId;
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());
297 }
298 }
299 }
300
301 for (OCFile aFile : files) {
302 if (aFile.isDirectory() && aFile.needsUpdatingWhileSaving())
303 saveFiles(getDirectoryContent(aFile));
304 }
305
306 }
307
308 public void setAccount(Account account) {
309 mAccount = account;
310 }
311
312 public Account getAccount() {
313 return mAccount;
314 }
315
316 public void setContentResolver(ContentResolver cr) {
317 mContentResolver = cr;
318 }
319
320 public ContentResolver getContentResolver() {
321 return mContentResolver;
322 }
323
324 public void setContentProvider(ContentProviderClient cp) {
325 mContentProvider = cp;
326 }
327
328 public ContentProviderClient getContentProvider() {
329 return mContentProvider;
330 }
331
332 public Vector<OCFile> getDirectoryContent(OCFile f) {
333 if (f != null && f.isDirectory() && f.getFileId() != -1) {
334 return getDirectoryContent(f.getFileId());
335
336 } else {
337 return new Vector<OCFile>();
338 }
339 }
340
341 private Vector<OCFile> getDirectoryContent(long parentId) {
342
343 Vector<OCFile> ret = new Vector<OCFile>();
344
345 Uri req_uri = Uri.withAppendedPath(
346 ProviderTableMeta.CONTENT_URI_DIR,
347 String.valueOf(parentId));
348 Cursor c = null;
349
350 if (getContentProvider() != null) {
351 try {
352 c = getContentProvider().query(req_uri, null,
353 ProviderTableMeta.FILE_PARENT + "=?" ,
354 new String[] { String.valueOf(parentId)}, null);
355 } catch (RemoteException e) {
356 Log_OC.e(TAG, e.getMessage());
357 return ret;
358 }
359 } else {
360 c = getContentResolver().query(req_uri, null,
361 ProviderTableMeta.FILE_PARENT + "=?" ,
362 new String[] { String.valueOf(parentId)}, null);
363 }
364
365 if (c.moveToFirst()) {
366 do {
367 OCFile child = createFileInstance(c);
368 ret.add(child);
369 } while (c.moveToNext());
370 }
371
372 c.close();
373
374 Collections.sort(ret);
375
376 return ret;
377 }
378
379
380
381 private boolean fileExists(String cmp_key, String value) {
382 Cursor c;
383 if (getContentResolver() != null) {
384 c = getContentResolver()
385 .query(ProviderTableMeta.CONTENT_URI,
386 null,
387 cmp_key + "=? AND "
388 + ProviderTableMeta.FILE_ACCOUNT_OWNER
389 + "=?",
390 new String[] { value, mAccount.name }, null);
391 } else {
392 try {
393 c = getContentProvider().query(
394 ProviderTableMeta.CONTENT_URI,
395 null,
396 cmp_key + "=? AND "
397 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
398 new String[] { value, mAccount.name }, null);
399 } catch (RemoteException e) {
400 Log_OC.e(TAG,
401 "Couldn't determine file existance, assuming non existance: "
402 + e.getMessage());
403 return false;
404 }
405 }
406 boolean retval = c.moveToFirst();
407 c.close();
408 return retval;
409 }
410
411 private Cursor getCursorForValue(String key, String value) {
412 Cursor c = null;
413 if (getContentResolver() != null) {
414 c = getContentResolver()
415 .query(ProviderTableMeta.CONTENT_URI,
416 null,
417 key + "=? AND "
418 + ProviderTableMeta.FILE_ACCOUNT_OWNER
419 + "=?",
420 new String[] { value, mAccount.name }, null);
421 } else {
422 try {
423 c = getContentProvider().query(
424 ProviderTableMeta.CONTENT_URI,
425 null,
426 key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
427 + "=?", new String[] { value, mAccount.name },
428 null);
429 } catch (RemoteException e) {
430 Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
431 c = null;
432 }
433 }
434 return c;
435 }
436
437 private OCFile createFileInstance(Cursor c) {
438 OCFile file = null;
439 if (c != null) {
440 file = new OCFile(c.getString(c
441 .getColumnIndex(ProviderTableMeta.FILE_PATH)));
442 file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
443 file.setParentId(c.getLong(c
444 .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
445 file.setMimetype(c.getString(c
446 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
447 if (!file.isDirectory()) {
448 file.setStoragePath(c.getString(c
449 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
450 if (file.getStoragePath() == null) {
451 // 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
452 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
453 if (f.exists()) {
454 file.setStoragePath(f.getAbsolutePath());
455 file.setLastSyncDateForData(f.lastModified());
456 }
457 }
458 }
459 file.setFileLength(c.getLong(c
460 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
461 file.setCreationTimestamp(c.getLong(c
462 .getColumnIndex(ProviderTableMeta.FILE_CREATION)));
463 file.setModificationTimestamp(c.getLong(c
464 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
465 file.setModificationTimestampAtLastSyncForData(c.getLong(c
466 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
467 file.setLastSyncDateForProperties(c.getLong(c
468 .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
469 file.setLastSyncDateForData(c.getLong(c.
470 getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
471 file.setKeepInSync(c.getInt(
472 c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
473 file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG)));
474
475 }
476 return file;
477 }
478
479 public void removeFile(OCFile file, boolean removeLocalCopy) {
480 Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
481 if (getContentProvider() != null) {
482 try {
483 getContentProvider().delete(file_uri,
484 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
485 new String[]{mAccount.name});
486 } catch (RemoteException e) {
487 e.printStackTrace();
488 }
489 } else {
490 getContentResolver().delete(file_uri,
491 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
492 new String[]{mAccount.name});
493 }
494 if (file.isDown() && removeLocalCopy) {
495 new File(file.getStoragePath()).delete();
496 }
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)) {
500 f.delete();
501 }
502 }
503
504 if (file.getFileLength() > 0) {
505 updateSizesToTheRoot(file.getParentId());
506 }
507 }
508
509 public void removeDirectory(OCFile dir, boolean removeDBData, boolean removeLocalContent) {
510 // TODO consider possible failures
511 if (dir != null && dir.isDirectory() && dir.getFileId() != -1) {
512 Vector<OCFile> children = getDirectoryContent(dir);
513 if (children.size() > 0) {
514 OCFile child = null;
515 for (int i=0; i<children.size(); i++) {
516 child = children.get(i);
517 if (child.isDirectory()) {
518 removeDirectory(child, removeDBData, removeLocalContent);
519 } else {
520 if (removeDBData) {
521 removeFile(child, removeLocalContent);
522 } else if (removeLocalContent) {
523 if (child.isDown()) {
524 new File(child.getStoragePath()).delete();
525 }
526 }
527 }
528 }
529 }
530 if (removeDBData) {
531 removeFile(dir, true);
532 }
533
534 if (dir.getFileLength() > 0) {
535 updateSizesToTheRoot(dir.getParentId());
536 }
537 }
538 }
539
540
541 /**
542 * Updates database for a folder that was moved to a different location.
543 *
544 * TODO explore better (faster) implementations
545 * TODO throw exceptions up !
546 */
547 public void moveDirectory(OCFile dir, String newPath) {
548 // TODO check newPath
549
550 if (dir != null && dir.isDirectory() && dir.fileExists() && !dir.getFileName().equals(OCFile.PATH_SEPARATOR)) {
551 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
552 Cursor c = null;
553 if (getContentProvider() != null) {
554 try {
555 c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
556 null,
557 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ",
558 new String[] { mAccount.name, dir.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
559 } catch (RemoteException e) {
560 Log_OC.e(TAG, e.getMessage());
561 }
562 } else {
563 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
564 null,
565 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ",
566 new String[] { mAccount.name, dir.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
567 }
568
569 /// 2. prepare a batch of update operations to change all the descendants
570 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(c.getCount());
571 int lengthOfOldPath = dir.getRemotePath().length();
572 String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
573 int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
574 if (c.moveToFirst()) {
575 do {
576 ContentValues cv = new ContentValues(); // don't take the constructor out of the loop and clear the object
577 OCFile child = createFileInstance(c);
578 cv.put(ProviderTableMeta.FILE_PATH, newPath + child.getRemotePath().substring(lengthOfOldPath));
579 if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) {
580 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, defaultSavePath + newPath + child.getStoragePath().substring(lengthOfOldStoragePath));
581 }
582 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
583 withValues(cv).
584 withSelection( ProviderTableMeta._ID + "=?",
585 new String[] { String.valueOf(child.getFileId()) })
586 .build());
587 } while (c.moveToNext());
588 }
589 c.close();
590
591 /// 3. apply updates in batch
592 try {
593 if (getContentResolver() != null) {
594 getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations);
595
596 } else {
597 getContentProvider().applyBatch(operations);
598 }
599
600 } catch (OperationApplicationException e) {
601 Log_OC.e(TAG, "Fail to update descendants of " + dir.getFileId() + " in database", e);
602
603 } catch (RemoteException e) {
604 Log_OC.e(TAG, "Fail to update desendants of " + dir.getFileId() + " in database", e);
605 }
606
607 }
608 }
609
610 public Vector<OCFile> getDirectoryImages(OCFile directory) {
611 Vector<OCFile> ret = new Vector<OCFile>();
612 if (directory != null) {
613 // TODO better implementation, filtering in the access to database (if possible) instead of here
614 Vector<OCFile> tmp = getDirectoryContent(directory);
615 OCFile current = null;
616 for (int i=0; i<tmp.size(); i++) {
617 current = tmp.get(i);
618 if (current.isImage()) {
619 ret.add(current);
620 }
621 }
622 }
623 return ret;
624 }
625
626 /**
627 * Calculate and save the folderSize on DB
628 * @param id
629 */
630 public void calculateFolderSize(long id) {
631 long folderSize = 0;
632
633 Vector<OCFile> files = getDirectoryContent(id);
634
635 for (OCFile f: files)
636 {
637 folderSize = folderSize + f.getFileLength();
638 }
639
640 updateSize(id, folderSize);
641 }
642
643 /**
644 * Update the size value of an OCFile in DB
645 */
646 private int updateSize(long id, long size) {
647 ContentValues cv = new ContentValues();
648 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, size);
649 int result = -1;
650 if (getContentResolver() != null) {
651 result = getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
652 new String[] { String.valueOf(id) });
653 } else {
654 try {
655 result = getContentProvider().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
656 new String[] { String.valueOf(id) });
657 } catch (RemoteException e) {
658 Log_OC.e(TAG,"Fail to update size column into database " + e.getMessage());
659 }
660 }
661 return result;
662 }
663
664 /**
665 * Update the size of a subtree of folder from a file to the root
666 * @param parentId: parent of the file
667 */
668 private void updateSizesToTheRoot(long parentId) {
669
670 OCFile file;
671
672 while (parentId != FileDataStorageManager.ROOT_PARENT_ID) {
673
674 // Update the size of the parent
675 calculateFolderSize(parentId);
676
677 // search the next parent
678 file = getFileById(parentId);
679 parentId = file.getParentId();
680
681 }
682
683 }
684
685 }