FileDataStorageManager.java 25 KB

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