Merge branch 'master' into develop
[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 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 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
239 withValues(cv).
240 withSelection( ProviderTableMeta._ID + "=?",
241 new String[] { String.valueOf(file.getFileId()) })
242 .build());
243
244 } else if (fileExists(file.getFileId())) {
245 OCFile oldFile = getFileById(file.getFileId());
246 if (file.getStoragePath() == null && oldFile.getStoragePath() != null)
247 file.setStoragePath(oldFile.getStoragePath());
248 if (!file.isDirectory());
249 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
250
251 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
252 withValues(cv).
253 withSelection( ProviderTableMeta._ID + "=?",
254 new String[] { String.valueOf(file.getFileId()) })
255 .build());
256
257 } else {
258 operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build());
259 }
260 }
261
262 // apply operations in batch
263 ContentProviderResult[] results = null;
264 try {
265 if (getContentResolver() != null) {
266 results = getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations);
267
268 } else {
269 results = getContentProvider().applyBatch(operations);
270 }
271
272 } catch (OperationApplicationException e) {
273 Log_OC.e(TAG, "Fail to update/insert list of files to database " + e.getMessage());
274
275 } catch (RemoteException e) {
276 Log_OC.e(TAG, "Fail to update/insert list of files to database " + e.getMessage());
277 }
278
279 // update new id in file objects for insertions
280 if (results != null) {
281 long newId;
282 for (int i=0; i<results.length; i++) {
283 if (results[i].uri != null) {
284 newId = Long.parseLong(results[i].uri.getPathSegments().get(1));
285 files.get(i).setFileId(newId);
286 //Log_OC.v(TAG, "Found and added id in insertion for " + files.get(i).getRemotePath());
287 }
288 }
289 }
290
291 for (OCFile aFile : files) {
292 if (aFile.isDirectory() && aFile.needsUpdatingWhileSaving())
293 saveFiles(getDirectoryContent(aFile));
294 }
295
296 }
297
298 public void setAccount(Account account) {
299 mAccount = account;
300 }
301
302 public Account getAccount() {
303 return mAccount;
304 }
305
306 public void setContentResolver(ContentResolver cr) {
307 mContentResolver = cr;
308 }
309
310 public ContentResolver getContentResolver() {
311 return mContentResolver;
312 }
313
314 public void setContentProvider(ContentProviderClient cp) {
315 mContentProvider = cp;
316 }
317
318 public ContentProviderClient getContentProvider() {
319 return mContentProvider;
320 }
321
322 @Override
323 public Vector<OCFile> getDirectoryContent(OCFile f) {
324 if (f != null && f.isDirectory() && f.getFileId() != -1) {
325 return getDirectoryContent(f.getFileId());
326
327 } else {
328 return new Vector<OCFile>();
329 }
330 }
331
332 private Vector<OCFile> getDirectoryContent(long parentId) {
333
334 Vector<OCFile> ret = new Vector<OCFile>();
335
336 Uri req_uri = Uri.withAppendedPath(
337 ProviderTableMeta.CONTENT_URI_DIR,
338 String.valueOf(parentId));
339 Cursor c = null;
340
341 if (getContentProvider() != null) {
342 try {
343 c = getContentProvider().query(req_uri, null,
344 ProviderTableMeta.FILE_PARENT + "=?" ,
345 new String[] { String.valueOf(parentId)}, null);
346 } catch (RemoteException e) {
347 Log_OC.e(TAG, e.getMessage());
348 return ret;
349 }
350 } else {
351 c = getContentResolver().query(req_uri, null,
352 ProviderTableMeta.FILE_PARENT + "=?" ,
353 new String[] { String.valueOf(parentId)}, null);
354 }
355
356 if (c.moveToFirst()) {
357 do {
358 OCFile child = createFileInstance(c);
359 ret.add(child);
360 } while (c.moveToNext());
361 }
362
363 c.close();
364
365 Collections.sort(ret);
366
367 return ret;
368 }
369
370
371
372 private boolean fileExists(String cmp_key, String value) {
373 Cursor c;
374 if (getContentResolver() != null) {
375 c = getContentResolver()
376 .query(ProviderTableMeta.CONTENT_URI,
377 null,
378 cmp_key + "=? AND "
379 + ProviderTableMeta.FILE_ACCOUNT_OWNER
380 + "=?",
381 new String[] { value, mAccount.name }, null);
382 } else {
383 try {
384 c = getContentProvider().query(
385 ProviderTableMeta.CONTENT_URI,
386 null,
387 cmp_key + "=? AND "
388 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
389 new String[] { value, mAccount.name }, null);
390 } catch (RemoteException e) {
391 Log_OC.e(TAG,
392 "Couldn't determine file existance, assuming non existance: "
393 + e.getMessage());
394 return false;
395 }
396 }
397 boolean retval = c.moveToFirst();
398 c.close();
399 return retval;
400 }
401
402 private Cursor getCursorForValue(String key, String value) {
403 Cursor c = null;
404 if (getContentResolver() != null) {
405 c = getContentResolver()
406 .query(ProviderTableMeta.CONTENT_URI,
407 null,
408 key + "=? AND "
409 + ProviderTableMeta.FILE_ACCOUNT_OWNER
410 + "=?",
411 new String[] { value, mAccount.name }, null);
412 } else {
413 try {
414 c = getContentProvider().query(
415 ProviderTableMeta.CONTENT_URI,
416 null,
417 key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
418 + "=?", new String[] { value, mAccount.name },
419 null);
420 } catch (RemoteException e) {
421 Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
422 c = null;
423 }
424 }
425 return c;
426 }
427
428 private OCFile createFileInstance(Cursor c) {
429 OCFile file = null;
430 if (c != null) {
431 file = new OCFile(c.getString(c
432 .getColumnIndex(ProviderTableMeta.FILE_PATH)));
433 file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
434 file.setParentId(c.getLong(c
435 .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
436 file.setMimetype(c.getString(c
437 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
438 if (!file.isDirectory()) {
439 file.setStoragePath(c.getString(c
440 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
441 if (file.getStoragePath() == null) {
442 // 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
443 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
444 if (f.exists()) {
445 file.setStoragePath(f.getAbsolutePath());
446 file.setLastSyncDateForData(f.lastModified());
447 }
448 }
449 }
450 file.setFileLength(c.getLong(c
451 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
452 file.setCreationTimestamp(c.getLong(c
453 .getColumnIndex(ProviderTableMeta.FILE_CREATION)));
454 file.setModificationTimestamp(c.getLong(c
455 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
456 file.setModificationTimestampAtLastSyncForData(c.getLong(c
457 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
458 file.setLastSyncDateForProperties(c.getLong(c
459 .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
460 file.setLastSyncDateForData(c.getLong(c.
461 getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
462 file.setKeepInSync(c.getInt(
463 c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
464 }
465 return file;
466 }
467
468 @Override
469 public void removeFile(OCFile file, boolean removeLocalCopy) {
470 Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
471 if (getContentProvider() != null) {
472 try {
473 getContentProvider().delete(file_uri,
474 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
475 new String[]{mAccount.name});
476 } catch (RemoteException e) {
477 e.printStackTrace();
478 }
479 } else {
480 getContentResolver().delete(file_uri,
481 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
482 new String[]{mAccount.name});
483 }
484 if (file.isDown() && removeLocalCopy) {
485 new File(file.getStoragePath()).delete();
486 }
487 if (file.isDirectory() && removeLocalCopy) {
488 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
489 if (f.exists() && f.isDirectory() && (f.list() == null || f.list().length == 0)) {
490 f.delete();
491 }
492 }
493
494 if (file.getFileLength() > 0) {
495 updateSizesToTheRoot(file.getParentId());
496 }
497 }
498
499 @Override
500 public void removeDirectory(OCFile dir, boolean removeDBData, boolean removeLocalContent) {
501 // TODO consider possible failures
502 if (dir != null && dir.isDirectory() && dir.getFileId() != -1) {
503 Vector<OCFile> children = getDirectoryContent(dir);
504 if (children.size() > 0) {
505 OCFile child = null;
506 for (int i=0; i<children.size(); i++) {
507 child = children.get(i);
508 if (child.isDirectory()) {
509 removeDirectory(child, removeDBData, removeLocalContent);
510 } else {
511 if (removeDBData) {
512 removeFile(child, removeLocalContent);
513 } else if (removeLocalContent) {
514 if (child.isDown()) {
515 new File(child.getStoragePath()).delete();
516 }
517 }
518 }
519 }
520 }
521 if (removeDBData) {
522 removeFile(dir, true);
523 }
524
525 if (dir.getFileLength() > 0) {
526 updateSizesToTheRoot(dir.getParentId());
527 }
528 }
529 }
530
531
532 /**
533 * Updates database for a folder that was moved to a different location.
534 *
535 * TODO explore better (faster) implementations
536 * TODO throw exceptions up !
537 */
538 @Override
539 public void moveDirectory(OCFile dir, String newPath) {
540 // TODO check newPath
541
542 if (dir != null && dir.isDirectory() && dir.fileExists() && !dir.getFileName().equals(OCFile.PATH_SEPARATOR)) {
543 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
544 Cursor c = null;
545 if (getContentProvider() != null) {
546 try {
547 c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
548 null,
549 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ?",
550 new String[] { mAccount.name, dir.getRemotePath() + "%" }, null);
551 } catch (RemoteException e) {
552 Log_OC.e(TAG, e.getMessage());
553 }
554 } else {
555 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
556 null,
557 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ?",
558 new String[] { mAccount.name, dir.getRemotePath() + "%" }, null);
559 }
560
561 /// 2. prepare a batch of update operations to change all the descendants
562 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(c.getCount());
563 int lengthOfOldPath = dir.getRemotePath().length();
564 String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
565 int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
566 if (c.moveToFirst()) {
567 do {
568 ContentValues cv = new ContentValues(); // don't take the constructor out of the loop and clear the object
569 OCFile child = createFileInstance(c);
570 cv.put(ProviderTableMeta.FILE_PATH, newPath + child.getRemotePath().substring(lengthOfOldPath));
571 if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) {
572 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, defaultSavePath + newPath + child.getStoragePath().substring(lengthOfOldStoragePath));
573 }
574 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
575 withValues(cv).
576 withSelection( ProviderTableMeta._ID + "=?",
577 new String[] { String.valueOf(child.getFileId()) })
578 .build());
579 } while (c.moveToNext());
580 }
581 c.close();
582
583 /// 3. apply updates in batch
584 try {
585 if (getContentResolver() != null) {
586 getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations);
587
588 } else {
589 getContentProvider().applyBatch(operations);
590 }
591
592 } catch (OperationApplicationException e) {
593 Log_OC.e(TAG, "Fail to update descendants of " + dir.getFileId() + " in database", e);
594
595 } catch (RemoteException e) {
596 Log_OC.e(TAG, "Fail to update desendants of " + dir.getFileId() + " in database", e);
597 }
598
599 }
600 }
601
602 @Override
603 public Vector<OCFile> getDirectoryImages(OCFile directory) {
604 Vector<OCFile> ret = new Vector<OCFile>();
605 if (directory != null) {
606 // TODO better implementation, filtering in the access to database (if possible) instead of here
607 Vector<OCFile> tmp = getDirectoryContent(directory);
608 OCFile current = null;
609 for (int i=0; i<tmp.size(); i++) {
610 current = tmp.get(i);
611 if (current.isImage()) {
612 ret.add(current);
613 }
614 }
615 }
616 return ret;
617 }
618
619 /**
620 * Calculate and save the folderSize on DB
621 * @param id
622 */
623 @Override
624 public void calculateFolderSize(long id) {
625 long folderSize = 0;
626
627 Vector<OCFile> files = getDirectoryContent(id);
628
629 for (OCFile f: files)
630 {
631 folderSize = folderSize + f.getFileLength();
632 }
633
634 updateSize(id, folderSize);
635 }
636
637 /**
638 * Update the size value of an OCFile in DB
639 */
640 private int updateSize(long id, long size) {
641 ContentValues cv = new ContentValues();
642 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, size);
643 int result = -1;
644 if (getContentResolver() != null) {
645 result = getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
646 new String[] { String.valueOf(id) });
647 } else {
648 try {
649 result = getContentProvider().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
650 new String[] { String.valueOf(id) });
651 } catch (RemoteException e) {
652 Log_OC.e(TAG,"Fail to update size column into database " + e.getMessage());
653 }
654 }
655 return result;
656 }
657
658 /**
659 * Update the size of a subtree of folder from a file to the root
660 * @param parentId: parent of the file
661 */
662 private void updateSizesToTheRoot(long parentId) {
663
664 OCFile file;
665
666 while (parentId != 0) {
667
668 Log_OC.d(TAG, "parent = " + parentId);
669 // Update the size of the parent
670 calculateFolderSize(parentId);
671
672 // search the next parent
673 file = getFileById(parentId);
674 parentId = file.getParentId();
675
676 }
677
678 }
679
680 }