FileDataStorageManager.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 eu.alefzero.owncloud.datamodel;
  19. import java.util.Vector;
  20. import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
  21. import android.accounts.Account;
  22. import android.content.ContentProviderClient;
  23. import android.content.ContentResolver;
  24. import android.content.ContentValues;
  25. import android.database.Cursor;
  26. import android.net.Uri;
  27. import android.os.RemoteException;
  28. import android.util.Log;
  29. public class FileDataStorageManager implements DataStorageManager {
  30. private ContentResolver mContentResolver;
  31. private ContentProviderClient mContentProvider;
  32. private Account mAccount;
  33. private static String TAG = "FileDataStorageManager";
  34. public FileDataStorageManager(Account account, ContentResolver cr) {
  35. mContentProvider = null;
  36. mContentResolver = cr;
  37. mAccount = account;
  38. }
  39. public FileDataStorageManager(Account account, ContentProviderClient cp) {
  40. mContentProvider = cp;
  41. mContentResolver = null;
  42. mAccount = account;
  43. }
  44. @Override
  45. public OCFile getFileByPath(String path) {
  46. Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
  47. if (c.moveToFirst())
  48. return createFileInstance(c);
  49. return null;
  50. }
  51. @Override
  52. public OCFile getFileById(long id) {
  53. Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  54. if (c.moveToFirst())
  55. return createFileInstance(c);
  56. return null;
  57. }
  58. @Override
  59. public boolean fileExists(long id) {
  60. return fileExists(ProviderTableMeta._ID, String.valueOf(id));
  61. }
  62. @Override
  63. public boolean fileExists(String path) {
  64. return fileExists(ProviderTableMeta.FILE_PATH, path);
  65. }
  66. @Override
  67. public boolean saveFile(OCFile file) {
  68. boolean overriden = false;
  69. ContentValues cv = new ContentValues();
  70. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  71. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  72. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  73. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  74. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  75. if (file.getParentId() != 0)
  76. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  77. cv.put(ProviderTableMeta.FILE_PATH, file.getPath());
  78. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  79. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  80. if (fileExists(file.getPath())) {
  81. overriden = true;
  82. if (getContentResolver() != null) {
  83. getContentResolver().update(ProviderTableMeta.CONTENT_URI,
  84. cv,
  85. ProviderTableMeta._ID + "=?",
  86. new String[] {String.valueOf(file.getFileId())});
  87. } else {
  88. try {
  89. getContentProvider().update(ProviderTableMeta.CONTENT_URI,
  90. cv,
  91. ProviderTableMeta._ID + "=?",
  92. new String[] {String.valueOf(file.getFileId())});
  93. } catch (RemoteException e) {
  94. Log.e(TAG, "Fail to insert insert file to database " + e.getMessage());
  95. }
  96. }
  97. } else {
  98. Uri result_uri = null;
  99. if (getContentResolver() != null) {
  100. result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  101. } else {
  102. try {
  103. result_uri = getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  104. } catch (RemoteException e) {
  105. Log.e(TAG, "Fail to insert insert file to database " + e.getMessage());
  106. }
  107. }
  108. if (result_uri != null) {
  109. long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
  110. file.setFileId(new_id);
  111. }
  112. }
  113. if (file.isDirectory() && file.needsUpdatingWhileSaving())
  114. for (OCFile f : getDirectoryContent(file))
  115. saveFile(f);
  116. return overriden;
  117. }
  118. public void setAccount(Account account) {
  119. mAccount = account;
  120. }
  121. public Account getAccount() {
  122. return mAccount;
  123. }
  124. public void setContentResolver(ContentResolver cr) {
  125. mContentResolver = cr;
  126. }
  127. public ContentResolver getContentResolver() {
  128. return mContentResolver;
  129. }
  130. public void setContentProvider(ContentProviderClient cp) {
  131. mContentProvider = cp;
  132. }
  133. public ContentProviderClient getContentProvider() {
  134. return mContentProvider;
  135. }
  136. public Vector<OCFile> getDirectoryContent(OCFile f) {
  137. if (f.isDirectory() && f.getFileId() != -1) {
  138. Vector<OCFile> ret = new Vector<OCFile>();
  139. Uri req_uri = Uri.withAppendedPath(
  140. ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(f.getFileId()));
  141. Cursor c = null;
  142. if (getContentProvider() != null) {
  143. try {
  144. c = getContentProvider().query(req_uri, null, null, null, null);
  145. } catch (RemoteException e) {
  146. Log.e(TAG, e.getMessage());
  147. return ret;
  148. }
  149. } else {
  150. c = getContentResolver().query(req_uri, null, null, null, null);
  151. }
  152. if (c.moveToFirst()) {
  153. do {
  154. OCFile child = createFileInstance(c);
  155. ret.add(child);
  156. } while (c.moveToNext());
  157. }
  158. c.close();
  159. return ret;
  160. }
  161. return null;
  162. }
  163. private boolean fileExists(String cmp_key, String value) {
  164. Cursor c;
  165. if (getContentResolver() != null) {
  166. c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
  167. null,
  168. cmp_key + "=?",
  169. new String[] {value},
  170. null);
  171. } else {
  172. try {
  173. c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
  174. null,
  175. cmp_key + "=?",
  176. new String[] {value},
  177. null);
  178. } catch (RemoteException e) {
  179. Log.e(TAG, "Couldn't determine file existance, assuming non existance: " + e.getMessage());
  180. return false;
  181. }
  182. }
  183. return c.moveToFirst();
  184. }
  185. private Cursor getCursorForValue(String key, String value) {
  186. Cursor c = null;
  187. if (getContentResolver() != null) {
  188. c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
  189. null,
  190. key + "=?",
  191. new String[] {value},
  192. null);
  193. } else {
  194. try {
  195. c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
  196. null,
  197. key + "=?",
  198. new String[]{value},
  199. null);
  200. } catch (RemoteException e) {
  201. Log.e(TAG, "Could not get file details: " + e.getMessage());
  202. c = null;
  203. }
  204. }
  205. return c;
  206. }
  207. private OCFile createFileInstance(Cursor c) {
  208. OCFile file = null;
  209. if (c != null) {
  210. file = new OCFile(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  211. file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  212. file.setParentId(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_PARENT)));
  213. file.setStoragePath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
  214. file.setMimetype(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
  215. file.setFileLength(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
  216. file.setCreationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION)));
  217. file.setModificationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
  218. }
  219. return file;
  220. }
  221. }