FileDataStorageManager.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  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.db.ProviderMeta;
  26. import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
  27. import com.owncloud.android.files.services.FileDownloader;
  28. import android.accounts.Account;
  29. import android.content.ContentProviderClient;
  30. import android.content.ContentProviderOperation;
  31. import android.content.ContentProviderResult;
  32. import android.content.ContentResolver;
  33. import android.content.ContentValues;
  34. import android.content.OperationApplicationException;
  35. import android.database.Cursor;
  36. import android.net.Uri;
  37. import android.os.RemoteException;
  38. import android.util.Log;
  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. return file;
  63. }
  64. @Override
  65. public OCFile getFileById(long id) {
  66. Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  67. OCFile file = null;
  68. if (c.moveToFirst()) {
  69. file = createFileInstance(c);
  70. }
  71. c.close();
  72. return file;
  73. }
  74. public OCFile getFileByLocalPath(String path) {
  75. Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
  76. OCFile file = null;
  77. if (c.moveToFirst()) {
  78. file = createFileInstance(c);
  79. }
  80. c.close();
  81. return file;
  82. }
  83. @Override
  84. public boolean fileExists(long id) {
  85. return fileExists(ProviderTableMeta._ID, String.valueOf(id));
  86. }
  87. @Override
  88. public boolean fileExists(String path) {
  89. return fileExists(ProviderTableMeta.FILE_PATH, path);
  90. }
  91. @Override
  92. public boolean saveFile(OCFile file) {
  93. boolean overriden = false;
  94. ContentValues cv = new ContentValues();
  95. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  96. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  97. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  98. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  99. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  100. if (file.getParentId() != 0)
  101. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  102. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  103. if (!file.isDirectory())
  104. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  105. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  106. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDate());
  107. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
  108. if (fileExists(file.getRemotePath())) {
  109. OCFile oldFile = getFileByPath(file.getRemotePath());
  110. if (file.getStoragePath() == null && oldFile.getStoragePath() != null)
  111. file.setStoragePath(oldFile.getStoragePath());
  112. if (!file.isDirectory());
  113. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  114. file.setFileId(oldFile.getFileId());
  115. overriden = true;
  116. if (getContentResolver() != null) {
  117. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
  118. ProviderTableMeta._ID + "=?",
  119. new String[] { String.valueOf(file.getFileId()) });
  120. } else {
  121. try {
  122. getContentProvider().update(ProviderTableMeta.CONTENT_URI,
  123. cv, ProviderTableMeta._ID + "=?",
  124. new String[] { String.valueOf(file.getFileId()) });
  125. } catch (RemoteException e) {
  126. Log.e(TAG,
  127. "Fail to insert insert file to database "
  128. + e.getMessage());
  129. }
  130. }
  131. } else {
  132. Uri result_uri = null;
  133. if (getContentResolver() != null) {
  134. result_uri = getContentResolver().insert(
  135. ProviderTableMeta.CONTENT_URI_FILE, cv);
  136. } else {
  137. try {
  138. result_uri = getContentProvider().insert(
  139. ProviderTableMeta.CONTENT_URI_FILE, cv);
  140. } catch (RemoteException e) {
  141. Log.e(TAG,
  142. "Fail to insert insert file to database "
  143. + e.getMessage());
  144. }
  145. }
  146. if (result_uri != null) {
  147. long new_id = Long.parseLong(result_uri.getPathSegments()
  148. .get(1));
  149. file.setFileId(new_id);
  150. }
  151. }
  152. if (file.isDirectory() && file.needsUpdatingWhileSaving())
  153. for (OCFile f : getDirectoryContent(file))
  154. saveFile(f);
  155. return overriden;
  156. }
  157. @Override
  158. public void saveFiles(List<OCFile> files) {
  159. Iterator<OCFile> filesIt = files.iterator();
  160. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(files.size());
  161. OCFile file = null;
  162. // prepare operations to perform
  163. while (filesIt.hasNext()) {
  164. file = filesIt.next();
  165. ContentValues cv = new ContentValues();
  166. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  167. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  168. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  169. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  170. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  171. if (file.getParentId() != 0)
  172. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  173. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  174. if (!file.isDirectory())
  175. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  176. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  177. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDate());
  178. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
  179. if (fileExists(file.getRemotePath())) {
  180. OCFile tmpfile = getFileByPath(file.getRemotePath());
  181. file.setStoragePath(tmpfile.getStoragePath());
  182. if (!file.isDirectory());
  183. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  184. file.setFileId(tmpfile.getFileId());
  185. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  186. withValues(cv).
  187. withSelection( ProviderTableMeta._ID + "=?",
  188. new String[] { String.valueOf(file.getFileId()) })
  189. .build());
  190. } else {
  191. operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build());
  192. }
  193. }
  194. // apply operations in batch
  195. ContentProviderResult[] results = null;
  196. try {
  197. if (getContentResolver() != null) {
  198. results = getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations);
  199. } else {
  200. results = getContentProvider().applyBatch(operations);
  201. }
  202. } catch (OperationApplicationException e) {
  203. Log.e(TAG, "Fail to update/insert list of files to database " + e.getMessage());
  204. } catch (RemoteException e) {
  205. Log.e(TAG, "Fail to update/insert list of files to database " + e.getMessage());
  206. }
  207. // update new id in file objects for insertions
  208. if (results != null) {
  209. long newId;
  210. for (int i=0; i<results.length; i++) {
  211. if (results[i].uri != null) {
  212. newId = Long.parseLong(results[i].uri.getPathSegments().get(1));
  213. files.get(i).setFileId(newId);
  214. //Log.v(TAG, "Found and added id in insertion for " + files.get(i).getRemotePath());
  215. }
  216. }
  217. }
  218. for (OCFile aFile : files) {
  219. if (aFile.isDirectory() && aFile.needsUpdatingWhileSaving())
  220. saveFiles(getDirectoryContent(aFile));
  221. }
  222. }
  223. public void setAccount(Account account) {
  224. mAccount = account;
  225. }
  226. public Account getAccount() {
  227. return mAccount;
  228. }
  229. public void setContentResolver(ContentResolver cr) {
  230. mContentResolver = cr;
  231. }
  232. public ContentResolver getContentResolver() {
  233. return mContentResolver;
  234. }
  235. public void setContentProvider(ContentProviderClient cp) {
  236. mContentProvider = cp;
  237. }
  238. public ContentProviderClient getContentProvider() {
  239. return mContentProvider;
  240. }
  241. @Override
  242. public Vector<OCFile> getDirectoryContent(OCFile f) {
  243. if (f != null && f.isDirectory() && f.getFileId() != -1) {
  244. Vector<OCFile> ret = new Vector<OCFile>();
  245. Uri req_uri = Uri.withAppendedPath(
  246. ProviderTableMeta.CONTENT_URI_DIR,
  247. String.valueOf(f.getFileId()));
  248. Cursor c = null;
  249. if (getContentProvider() != null) {
  250. try {
  251. c = getContentProvider().query(req_uri, null,
  252. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  253. new String[] { mAccount.name }, null);
  254. } catch (RemoteException e) {
  255. Log.e(TAG, e.getMessage());
  256. return ret;
  257. }
  258. } else {
  259. c = getContentResolver().query(req_uri, null,
  260. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  261. new String[] { mAccount.name }, null);
  262. }
  263. if (c.moveToFirst()) {
  264. do {
  265. OCFile child = createFileInstance(c);
  266. ret.add(child);
  267. } while (c.moveToNext());
  268. }
  269. c.close();
  270. Collections.sort(ret);
  271. return ret;
  272. }
  273. return null;
  274. }
  275. private boolean fileExists(String cmp_key, String value) {
  276. Cursor c;
  277. if (getContentResolver() != null) {
  278. c = getContentResolver()
  279. .query(ProviderTableMeta.CONTENT_URI,
  280. null,
  281. cmp_key + "=? AND "
  282. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  283. + "=?",
  284. new String[] { value, mAccount.name }, null);
  285. } else {
  286. try {
  287. c = getContentProvider().query(
  288. ProviderTableMeta.CONTENT_URI,
  289. null,
  290. cmp_key + "=? AND "
  291. + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  292. new String[] { value, mAccount.name }, null);
  293. } catch (RemoteException e) {
  294. Log.e(TAG,
  295. "Couldn't determine file existance, assuming non existance: "
  296. + e.getMessage());
  297. return false;
  298. }
  299. }
  300. boolean retval = c.moveToFirst();
  301. c.close();
  302. return retval;
  303. }
  304. private Cursor getCursorForValue(String key, String value) {
  305. Cursor c = null;
  306. if (getContentResolver() != null) {
  307. c = getContentResolver()
  308. .query(ProviderTableMeta.CONTENT_URI,
  309. null,
  310. key + "=? AND "
  311. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  312. + "=?",
  313. new String[] { value, mAccount.name }, null);
  314. } else {
  315. try {
  316. c = getContentProvider().query(
  317. ProviderTableMeta.CONTENT_URI,
  318. null,
  319. key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
  320. + "=?", new String[] { value, mAccount.name },
  321. null);
  322. } catch (RemoteException e) {
  323. Log.e(TAG, "Could not get file details: " + e.getMessage());
  324. c = null;
  325. }
  326. }
  327. return c;
  328. }
  329. private OCFile createFileInstance(Cursor c) {
  330. OCFile file = null;
  331. if (c != null) {
  332. file = new OCFile(c.getString(c
  333. .getColumnIndex(ProviderTableMeta.FILE_PATH)));
  334. file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  335. file.setParentId(c.getLong(c
  336. .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
  337. file.setMimetype(c.getString(c
  338. .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
  339. if (!file.isDirectory()) {
  340. file.setStoragePath(c.getString(c
  341. .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
  342. if (file.getStoragePath() == null) {
  343. // try to find existing file and bind it with current account
  344. File f = new File(FileDownloader.getSavePath(mAccount.name) + file.getRemotePath());
  345. if (f.exists())
  346. file.setStoragePath(f.getAbsolutePath());
  347. }
  348. }
  349. file.setFileLength(c.getLong(c
  350. .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
  351. file.setCreationTimestamp(c.getLong(c
  352. .getColumnIndex(ProviderTableMeta.FILE_CREATION)));
  353. file.setModificationTimestamp(c.getLong(c
  354. .getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
  355. file.setLastSyncDate(c.getLong(c
  356. .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
  357. file.setKeepInSync(c.getInt(
  358. c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
  359. }
  360. return file;
  361. }
  362. @Override
  363. public void removeFile(OCFile file, boolean removeLocalCopy) {
  364. Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
  365. if (getContentProvider() != null) {
  366. try {
  367. getContentProvider().delete(file_uri,
  368. ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
  369. new String[]{mAccount.name});
  370. } catch (RemoteException e) {
  371. e.printStackTrace();
  372. }
  373. } else {
  374. getContentResolver().delete(file_uri,
  375. ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
  376. new String[]{mAccount.name});
  377. }
  378. if (file.isDown() && removeLocalCopy) {
  379. new File(file.getStoragePath()).delete();
  380. }
  381. }
  382. }