SyncedFolderProvider.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * Copyright (C) 2016 Andy Scherzinger
  5. * Copyright (C) 2016 Nextcloud.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.datamodel;
  21. import android.accounts.Account;
  22. import android.content.ContentResolver;
  23. import android.content.ContentValues;
  24. import android.content.Context;
  25. import android.database.Cursor;
  26. import android.net.Uri;
  27. import android.support.annotation.NonNull;
  28. import com.owncloud.android.MainApp;
  29. import com.owncloud.android.db.PreferenceManager;
  30. import com.owncloud.android.db.ProviderMeta;
  31. import com.owncloud.android.lib.common.utils.Log_OC;
  32. import java.io.File;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import java.util.Observable;
  36. /**
  37. * Database provider for handling the persistence aspects of {@link SyncedFolder}s.
  38. */
  39. public class SyncedFolderProvider extends Observable {
  40. static private final String TAG = SyncedFolderProvider.class.getSimpleName();
  41. private ContentResolver mContentResolver;
  42. /**
  43. * constructor.
  44. *
  45. * @param contentResolver the ContentResolver to work with.
  46. */
  47. public SyncedFolderProvider(ContentResolver contentResolver) {
  48. if (contentResolver == null) {
  49. throw new IllegalArgumentException("Cannot create an instance with a NULL contentResolver");
  50. }
  51. mContentResolver = contentResolver;
  52. }
  53. /**
  54. * Stores an media folder sync object in database.
  55. *
  56. * @param syncedFolder synced folder to store
  57. * @return synced folder id, -1 if the insert process fails.
  58. */
  59. public long storeFolderSync(SyncedFolder syncedFolder) {
  60. Log_OC.v(TAG, "Inserting " + syncedFolder.getLocalPath() + " with enabled=" + syncedFolder.isEnabled());
  61. ContentValues cv = createContentValuesFromSyncedFolder(syncedFolder);
  62. Uri result = mContentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS, cv);
  63. if (result != null) {
  64. notifyFolderSyncObservers(syncedFolder);
  65. return Long.parseLong(result.getPathSegments().get(1));
  66. } else {
  67. Log_OC.e(TAG, "Failed to insert item " + syncedFolder.getLocalPath() + " into folder sync db.");
  68. return -1;
  69. }
  70. }
  71. /**
  72. * get all synced folder entries.
  73. *
  74. * @return all synced folder entries, empty if none have been found
  75. */
  76. public List<SyncedFolder> getSyncedFolders() {
  77. Cursor cursor = mContentResolver.query(
  78. ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
  79. null,
  80. "1=1",
  81. null,
  82. null
  83. );
  84. if (cursor != null) {
  85. List<SyncedFolder> list = new ArrayList<>(cursor.getCount());
  86. if (cursor.moveToFirst()) {
  87. do {
  88. SyncedFolder syncedFolder = createSyncedFolderFromCursor(cursor);
  89. if (syncedFolder == null) {
  90. Log_OC.e(TAG, "SyncedFolder could not be created from cursor");
  91. } else {
  92. list.add(cursor.getPosition(), syncedFolder);
  93. }
  94. } while (cursor.moveToNext());
  95. }
  96. cursor.close();
  97. return list;
  98. } else {
  99. Log_OC.e(TAG, "DB error creating read all cursor for synced folders.");
  100. }
  101. return new ArrayList<>(0);
  102. }
  103. /**
  104. * Update upload status of file uniquely referenced by id.
  105. *
  106. * @param id folder sync id.
  107. * @param enabled new status.
  108. * @return the number of rows updated.
  109. */
  110. public int updateFolderSyncEnabled(long id, Boolean enabled) {
  111. Log_OC.v(TAG, "Storing sync folder id" + id + " with enabled=" + enabled);
  112. int result = 0;
  113. Cursor cursor = mContentResolver.query(
  114. ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
  115. null,
  116. ProviderMeta.ProviderTableMeta._ID + "=?",
  117. new String[]{String.valueOf(id)},
  118. null
  119. );
  120. if (cursor != null && cursor.getCount() == 1) {
  121. while (cursor.moveToNext()) {
  122. // read sync folder object and update
  123. SyncedFolder syncedFolder = createSyncedFolderFromCursor(cursor);
  124. syncedFolder.setEnabled(enabled);
  125. // update sync folder object in db
  126. result = updateSyncFolder(syncedFolder);
  127. cursor.close();
  128. }
  129. } else {
  130. if (cursor == null) {
  131. Log_OC.e(TAG, "Sync folder db cursor for ID=" + id + " in NULL.");
  132. } else {
  133. Log_OC.e(TAG, cursor.getCount() + " items for id=" + id + " available in sync folder database. " +
  134. "Expected 1. Failed to update sync folder db.");
  135. }
  136. }
  137. return result;
  138. }
  139. /**
  140. * find a synced folder by local path.
  141. *
  142. * @param localPath the local path of the local folder
  143. * @return the synced folder if found, else null
  144. */
  145. public SyncedFolder findByLocalPath(String localPath) {
  146. SyncedFolder result = null;
  147. Cursor cursor = mContentResolver.query(
  148. ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
  149. null,
  150. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH + "== \"" + localPath + "\"",
  151. null,
  152. null
  153. );
  154. if (cursor != null && cursor.getCount() == 1) {
  155. result = createSyncedFolderFromCursor(cursor);
  156. cursor.close();
  157. } else {
  158. if (cursor == null) {
  159. Log_OC.e(TAG, "Sync folder db cursor for local path=" + localPath + " in NULL.");
  160. } else {
  161. Log_OC.e(TAG, cursor.getCount() + " items for local path=" + localPath
  162. + " available in sync folder db. Expected 1. Failed to update sync folder db.");
  163. }
  164. }
  165. return result;
  166. }
  167. /**
  168. * Delete all synced folders for an account
  169. *
  170. * @param account whose synced folders should be deleted
  171. */
  172. public int deleteSyncFoldersForAccount(Account account) {
  173. int result = mContentResolver.delete(
  174. ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
  175. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_ACCOUNT + " = ?",
  176. new String[]{String.valueOf(account.name)}
  177. );
  178. return result;
  179. }
  180. /**
  181. * Delete a synced folder from the db
  182. *
  183. * @param id for the synced folder.
  184. */
  185. private int deleteSyncFolderWithId(long id) {
  186. int result = mContentResolver.delete(
  187. ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
  188. ProviderMeta.ProviderTableMeta._ID + " = ?",
  189. new String[]{String.valueOf(id)}
  190. );
  191. return result;
  192. }
  193. /**
  194. * Try to figure out if a path exists for synced folder, and if not, go one folder back
  195. * Otherwise, delete the entry
  196. *
  197. * @param context the context.
  198. */
  199. public void updateAutoUploadPaths(Context context) {
  200. List<SyncedFolder> syncedFolders = getSyncedFolders();
  201. for (int i = 0; i < syncedFolders.size(); i++) {
  202. SyncedFolder syncedFolder = syncedFolders.get(i);
  203. if (!new File(syncedFolder.getLocalPath()).exists()) {
  204. String localPath = syncedFolder.getLocalPath();
  205. if (localPath.endsWith("/")) {
  206. localPath = localPath.substring(0, localPath.lastIndexOf("/"));
  207. }
  208. localPath = localPath.substring(0, localPath.lastIndexOf("/"));
  209. if (new File(localPath).exists()) {
  210. syncedFolders.get(i).setLocalPath(localPath);
  211. updateSyncFolder(syncedFolder);
  212. } else {
  213. deleteSyncFolderWithId(syncedFolder.getId());
  214. }
  215. }
  216. }
  217. if (context != null) {
  218. PreferenceManager.setAutoUploadPathsUpdate(context, true);
  219. }
  220. }
  221. /**
  222. * delete any records of synchronized folders that are not within the given list of ids.
  223. *
  224. * @param context the context.
  225. * @param ids the list of ids to be excluded from deletion.
  226. * @return number of deleted records.
  227. */
  228. public int deleteSyncedFoldersNotInList(Context context, ArrayList<Long> ids) {
  229. int result = mContentResolver.delete(
  230. ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
  231. ProviderMeta.ProviderTableMeta._ID + " NOT IN (?)",
  232. new String[]{String.valueOf(ids)}
  233. );
  234. if (result > 0 && context != null) {
  235. PreferenceManager.setLegacyClean(context, true);
  236. }
  237. return result;
  238. }
  239. /**
  240. * update given synced folder.
  241. *
  242. * @param syncedFolder the synced folder to be updated.
  243. * @return the number of rows updated.
  244. */
  245. public int updateSyncFolder(SyncedFolder syncedFolder) {
  246. Log_OC.v(TAG, "Updating " + syncedFolder.getLocalPath() + " with enabled=" + syncedFolder.isEnabled());
  247. ContentValues cv = createContentValuesFromSyncedFolder(syncedFolder);
  248. int result = mContentResolver.update(
  249. ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
  250. cv,
  251. ProviderMeta.ProviderTableMeta._ID + "=?",
  252. new String[]{String.valueOf(syncedFolder.getId())}
  253. );
  254. if (result > 0) {
  255. notifyFolderSyncObservers(syncedFolder);
  256. }
  257. return result;
  258. }
  259. /**
  260. * maps a cursor into a SyncedFolder object.
  261. *
  262. * @param cursor the db cursor
  263. * @return the mapped SyncedFolder, null if cursor is null
  264. */
  265. private SyncedFolder createSyncedFolderFromCursor(Cursor cursor) {
  266. SyncedFolder syncedFolder = null;
  267. if (cursor != null) {
  268. long id = cursor.getLong(cursor.getColumnIndex(ProviderMeta.ProviderTableMeta._ID));
  269. String localPath = cursor.getString(cursor.getColumnIndex(
  270. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH));
  271. String remotePath = cursor.getString(cursor.getColumnIndex(
  272. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_REMOTE_PATH));
  273. Boolean wifiOnly = cursor.getInt(cursor.getColumnIndex(
  274. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_WIFI_ONLY)) == 1;
  275. Boolean chargingOnly = cursor.getInt(cursor.getColumnIndex(
  276. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_CHARGING_ONLY)) == 1;
  277. Boolean subfolderByDate = cursor.getInt(cursor.getColumnIndex(
  278. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_SUBFOLDER_BY_DATE)) == 1;
  279. String accountName = cursor.getString(cursor.getColumnIndex(
  280. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_ACCOUNT));
  281. Integer uploadAction = cursor.getInt(cursor.getColumnIndex(
  282. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_UPLOAD_ACTION));
  283. Boolean enabled = cursor.getInt(cursor.getColumnIndex(
  284. ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_ENABLED)) == 1;
  285. syncedFolder = new SyncedFolder(id, localPath, remotePath, wifiOnly, chargingOnly, subfolderByDate,
  286. accountName, uploadAction, enabled);
  287. }
  288. return syncedFolder;
  289. }
  290. /**
  291. * create ContentValues object based on given SyncedFolder.
  292. *
  293. * @param syncedFolder the synced folder
  294. * @return the corresponding ContentValues object
  295. */
  296. @NonNull
  297. private ContentValues createContentValuesFromSyncedFolder(SyncedFolder syncedFolder) {
  298. ContentValues cv = new ContentValues();
  299. cv.put(ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH, syncedFolder.getLocalPath());
  300. cv.put(ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_REMOTE_PATH, syncedFolder.getRemotePath());
  301. cv.put(ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_WIFI_ONLY, syncedFolder.getWifiOnly());
  302. cv.put(ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_CHARGING_ONLY, syncedFolder.getChargingOnly());
  303. cv.put(ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_ENABLED, syncedFolder.isEnabled());
  304. cv.put(ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_SUBFOLDER_BY_DATE, syncedFolder.getSubfolderByDate());
  305. cv.put(ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_ACCOUNT, syncedFolder.getAccount());
  306. cv.put(ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_UPLOAD_ACTION, syncedFolder.getUploadAction());
  307. return cv;
  308. }
  309. /**
  310. * Inform all observers about data change.
  311. *
  312. * @param syncedFolder changed, synchronized folder
  313. */
  314. private void notifyFolderSyncObservers(SyncedFolder syncedFolder) {
  315. if (syncedFolder != null) {
  316. MainApp.getSyncedFolderObserverService().restartObserver(syncedFolder);
  317. Log_OC.d(TAG, "notifying folder sync data observers for changed/added: " + syncedFolder.getLocalPath());
  318. }
  319. }
  320. }