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