Merge branch 'setup_colors' into setup_app_name
[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 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;
43
44 public class FileDataStorageManager implements DataStorageManager {
45
46 private ContentResolver mContentResolver;
47 private ContentProviderClient mContentProvider;
48 private Account mAccount;
49
50 private static String TAG = "FileDataStorageManager";
51
52 public FileDataStorageManager(Account account, ContentResolver cr) {
53 mContentProvider = null;
54 mContentResolver = cr;
55 mAccount = account;
56 }
57
58 public FileDataStorageManager(Account account, ContentProviderClient cp) {
59 mContentProvider = cp;
60 mContentResolver = null;
61 mAccount = account;
62 }
63
64 @Override
65 public OCFile getFileByPath(String path) {
66 Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
67 OCFile file = null;
68 if (c.moveToFirst()) {
69 file = createFileInstance(c);
70 }
71 c.close();
72 if (file == null && OCFile.PATH_SEPARATOR.equals(path)) {
73 return createRootDir(); // root should always exist
74 }
75 return file;
76 }
77
78
79 private OCFile createRootDir() {
80 OCFile file = new OCFile(OCFile.PATH_SEPARATOR);
81 file.setMimetype("DIR");
82 file.setParentId(DataStorageManager.ROOT_PARENT_ID);
83 saveFile(file);
84 return file;
85 }
86
87 @Override
88 public OCFile getFileById(long id) {
89 Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
90 OCFile file = null;
91 if (c.moveToFirst()) {
92 file = createFileInstance(c);
93 }
94 c.close();
95 return file;
96 }
97
98 public OCFile getFileByLocalPath(String path) {
99 Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
100 OCFile file = null;
101 if (c.moveToFirst()) {
102 file = createFileInstance(c);
103 }
104 c.close();
105 return file;
106 }
107
108 @Override
109 public boolean fileExists(long id) {
110 return fileExists(ProviderTableMeta._ID, String.valueOf(id));
111 }
112
113 @Override
114 public boolean fileExists(String path) {
115 return fileExists(ProviderTableMeta.FILE_PATH, path);
116 }
117
118 @Override
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);
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 @Override
208 public void saveFiles(List<OCFile> files) {
209
210 Iterator<OCFile> filesIt = files.iterator();
211 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(files.size());
212 OCFile file = null;
213
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);
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(MainApp.getAuthority(), 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 @Override
333 public Vector<OCFile> getDirectoryContent(OCFile f) {
334 if (f != null && f.isDirectory() && f.getFileId() != -1) {
335 return getDirectoryContent(f.getFileId());
336
337 } else {
338 return new Vector<OCFile>();
339 }
340 }
341
342 private Vector<OCFile> getDirectoryContent(long parentId) {
343
344 Vector<OCFile> ret = new Vector<OCFile>();
345
346 Uri req_uri = Uri.withAppendedPath(
347 ProviderTableMeta.CONTENT_URI_DIR,
348 String.valueOf(parentId));
349 Cursor c = null;
350
351 if (getContentProvider() != null) {
352 try {
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());
358 return ret;
359 }
360 } else {
361 c = getContentResolver().query(req_uri, null,
362 ProviderTableMeta.FILE_PARENT + "=?" ,
363 new String[] { String.valueOf(parentId)}, null);
364 }
365
366 if (c.moveToFirst()) {
367 do {
368 OCFile child = createFileInstance(c);
369 ret.add(child);
370 } while (c.moveToNext());
371 }
372
373 c.close();
374
375 Collections.sort(ret);
376
377 return ret;
378 }
379
380
381
382 private boolean fileExists(String cmp_key, String value) {
383 Cursor c;
384 if (getContentResolver() != null) {
385 c = getContentResolver()
386 .query(ProviderTableMeta.CONTENT_URI,
387 null,
388 cmp_key + "=? AND "
389 + ProviderTableMeta.FILE_ACCOUNT_OWNER
390 + "=?",
391 new String[] { value, mAccount.name }, null);
392 } else {
393 try {
394 c = getContentProvider().query(
395 ProviderTableMeta.CONTENT_URI,
396 null,
397 cmp_key + "=? AND "
398 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
399 new String[] { value, mAccount.name }, null);
400 } catch (RemoteException e) {
401 Log_OC.e(TAG,
402 "Couldn't determine file existance, assuming non existance: "
403 + e.getMessage());
404 return false;
405 }
406 }
407 boolean retval = c.moveToFirst();
408 c.close();
409 return retval;
410 }
411
412 private Cursor getCursorForValue(String key, String value) {
413 Cursor c = null;
414 if (getContentResolver() != null) {
415 c = getContentResolver()
416 .query(ProviderTableMeta.CONTENT_URI,
417 null,
418 key + "=? AND "
419 + ProviderTableMeta.FILE_ACCOUNT_OWNER
420 + "=?",
421 new String[] { value, mAccount.name }, null);
422 } else {
423 try {
424 c = getContentProvider().query(
425 ProviderTableMeta.CONTENT_URI,
426 null,
427 key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
428 + "=?", new String[] { value, mAccount.name },
429 null);
430 } catch (RemoteException e) {
431 Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
432 c = null;
433 }
434 }
435 return c;
436 }
437
438 private OCFile createFileInstance(Cursor c) {
439 OCFile file = null;
440 if (c != null) {
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));
454 if (f.exists()) {
455 file.setStoragePath(f.getAbsolutePath());
456 file.setLastSyncDateForData(f.lastModified());
457 }
458 }
459 }
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);
474 }
475 return file;
476 }
477
478 @Override
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 @Override
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) {
515 OCFile child = null;
516 for (int i=0; i<children.size(); i++) {
517 child = children.get(i);
518 if (child.isDirectory()) {
519 removeDirectory(child, removeDBData, removeLocalContent);
520 } else {
521 if (removeDBData) {
522 removeFile(child, removeLocalContent);
523 } else if (removeLocalContent) {
524 if (child.isDown()) {
525 new File(child.getStoragePath()).delete();
526 }
527 }
528 }
529 }
530 }
531 if (removeDBData) {
532 removeFile(dir, true);
533 }
534
535 if (dir.getFileLength() > 0) {
536 updateSizesToTheRoot(dir.getParentId());
537 }
538 }
539 }
540
541
542 /**
543 * Updates database for a folder that was moved to a different location.
544 *
545 * TODO explore better (faster) implementations
546 * TODO throw exceptions up !
547 */
548 @Override
549 public void moveDirectory(OCFile dir, String newPath) {
550 // TODO check newPath
551
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')
554 Cursor c = null;
555 if (getContentProvider() != null) {
556 try {
557 c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
558 null,
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());
563 }
564 } else {
565 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
566 null,
567 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ",
568 new String[] { mAccount.name, dir.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
569 }
570
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()) {
577 do {
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));
583 }
584 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
585 withValues(cv).
586 withSelection( ProviderTableMeta._ID + "=?",
587 new String[] { String.valueOf(child.getFileId()) })
588 .build());
589 } while (c.moveToNext());
590 }
591 c.close();
592
593 /// 3. apply updates in batch
594 try {
595 if (getContentResolver() != null) {
596 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
597
598 } else {
599 getContentProvider().applyBatch(operations);
600 }
601
602 } catch (OperationApplicationException e) {
603 Log_OC.e(TAG, "Fail to update descendants of " + dir.getFileId() + " in database", e);
604
605 } catch (RemoteException e) {
606 Log_OC.e(TAG, "Fail to update desendants of " + dir.getFileId() + " in database", e);
607 }
608
609 }
610 }
611
612 @Override
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()) {
622 ret.add(current);
623 }
624 }
625 }
626 return ret;
627 }
628
629 /**
630 * Calculate and save the folderSize on DB
631 * @param id
632 */
633 @Override
634 public void calculateFolderSize(long id) {
635 long folderSize = 0;
636
637 Vector<OCFile> files = getDirectoryContent(id);
638
639 for (OCFile f: files)
640 {
641 folderSize = folderSize + f.getFileLength();
642 }
643
644 updateSize(id, folderSize);
645 }
646
647 /**
648 * Update the size value of an OCFile in DB
649 */
650 private int updateSize(long id, long size) {
651 ContentValues cv = new ContentValues();
652 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, size);
653 int result = -1;
654 if (getContentResolver() != null) {
655 result = getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
656 new String[] { String.valueOf(id) });
657 } else {
658 try {
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());
663 }
664 }
665 return result;
666 }
667
668 /**
669 * Update the size of a subtree of folder from a file to the root
670 * @param parentId: parent of the file
671 */
672 private void updateSizesToTheRoot(long parentId) {
673
674 OCFile file;
675
676 while (parentId != 0) {
677
678 // Update the size of the parent
679 calculateFolderSize(parentId);
680
681 // search the next parent
682 file = getFileById(parentId);
683 parentId = file.getParentId();
684
685 }
686
687 }
688
689 }