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