PreferenceManager.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.owncloud.android.db;
  20. import android.accounts.Account;
  21. import android.content.Context;
  22. import android.content.SharedPreferences;
  23. import com.owncloud.android.authentication.AccountUtils;
  24. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  25. import com.owncloud.android.datamodel.FileDataStorageManager;
  26. import com.owncloud.android.datamodel.OCFile;
  27. import com.owncloud.android.ui.activity.ComponentsGetter;
  28. import com.owncloud.android.ui.activity.Preferences;
  29. import com.owncloud.android.utils.FileSortOrder;
  30. import static com.owncloud.android.ui.fragment.OCFileListFragment.FOLDER_LAYOUT_LIST;
  31. /**
  32. * Helper to simplify reading of Preferences all around the app
  33. */
  34. public abstract class PreferenceManager {
  35. /**
  36. * Constant to access value of last path selected by the user to upload a file shared from other app.
  37. * Value handled by the app without direct access in the UI.
  38. */
  39. private static final String AUTO_PREF__LAST_UPLOAD_PATH = "last_upload_path";
  40. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL = "prefs_upload_file_extension_map_url";
  41. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_URL = "prefs_upload_file_extension_url";
  42. private static final String AUTO_PREF__UPLOADER_BEHAVIOR = "prefs_uploader_behaviour";
  43. private static final String AUTO_PREF__GRID_COLUMNS = "grid_columns";
  44. private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
  45. private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
  46. private static final String PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS = "instant_upload_path_use_subfolders";
  47. private static final String PREF__INSTANT_UPLOAD_ON_WIFI = "instant_upload_on_wifi";
  48. private static final String PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI = "instant_video_upload_on_wifi";
  49. private static final String PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS
  50. = "instant_video_upload_path_use_subfolders";
  51. private static final String PREF__LEGACY_CLEAN = "legacyClean";
  52. public static final String PREF__KEYS_MIGRATION = "keysMigration";
  53. private static final String PREF__FIX_STORAGE_PATH = "storagePathFix";
  54. private static final String PREF__KEYS_REINIT = "keysReinit";
  55. private static final String PREF__AUTO_UPLOAD_UPDATE_PATH = "autoUploadPathUpdate";
  56. private static final String PREF__PUSH_TOKEN = "pushToken";
  57. private static final String PREF__AUTO_UPLOAD_SPLIT_OUT = "autoUploadEntriesSplitOut";
  58. private static final String PREF__AUTO_UPLOAD_INIT = "autoUploadInit";
  59. private static final String PREF__FOLDER_SORT_ORDER = "folder_sort_order";
  60. private static final String PREF__FOLDER_LAYOUT = "folder_layout";
  61. public static void setKeysReInit(Context context) {
  62. saveBooleanPreference(context, PREF__KEYS_REINIT, true);
  63. }
  64. public static boolean getKeysReInit(Context context) {
  65. return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_REINIT, false);
  66. }
  67. public static void setPushToken(Context context, String pushToken) {
  68. saveStringPreferenceNow(context, PREF__PUSH_TOKEN, pushToken);
  69. }
  70. public static String getPushToken(Context context) {
  71. return getDefaultSharedPreferences(context).getString(PREF__PUSH_TOKEN, "");
  72. }
  73. public static boolean instantPictureUploadEnabled(Context context) {
  74. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOADING, false);
  75. }
  76. public static boolean instantVideoUploadEnabled(Context context) {
  77. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOADING, false);
  78. }
  79. public static boolean instantPictureUploadPathUseSubfolders(Context context) {
  80. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS, false);
  81. }
  82. public static boolean instantPictureUploadViaWiFiOnly(Context context) {
  83. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_ON_WIFI, false);
  84. }
  85. public static boolean instantVideoUploadPathUseSubfolders(Context context) {
  86. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS, false);
  87. }
  88. public static boolean instantVideoUploadViaWiFiOnly(Context context) {
  89. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI, false);
  90. }
  91. public static boolean instantPictureUploadWhenChargingOnly(Context context) {
  92. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_charging", false);
  93. }
  94. public static boolean instantVideoUploadWhenChargingOnly(Context context) {
  95. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_charging",
  96. false);
  97. }
  98. public static boolean showHiddenFilesEnabled(Context context) {
  99. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("show_hidden_files_pref", false);
  100. }
  101. /**
  102. * Gets the selected file extension position the user selected to do the last upload of a url file shared from other
  103. * app.
  104. *
  105. * @param context Caller {@link Context}, used to access to shared preferences manager.
  106. * @return selectedPos the selected file extension position.
  107. */
  108. public static int getUploadUrlFileExtensionUrlSelectedPos(Context context) {
  109. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
  110. }
  111. /**
  112. * Saves the selected file extension position the user selected to do the last upload of a url file shared from
  113. * other app.
  114. *
  115. * @param context Caller {@link Context}, used to access to shared preferences manager.
  116. * @param selectedPos the selected file extension position.
  117. */
  118. public static void setUploadUrlFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  119. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos);
  120. }
  121. /**
  122. * Gets the selected map file extension position the user selected to do the last upload of a url file shared
  123. * from other app.
  124. *
  125. * @param context Caller {@link Context}, used to access to shared preferences manager.
  126. * @return selectedPos the selected file extension position.
  127. */
  128. public static int getUploadMapFileExtensionUrlSelectedPos(Context context) {
  129. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
  130. }
  131. /**
  132. * Saves the selected map file extension position the user selected to do the last upload of a url file shared from
  133. * other app.
  134. *
  135. * @param context Caller {@link Context}, used to access to shared preferences manager.
  136. * @param selectedPos the selected file extension position.
  137. */
  138. public static void setUploadMapFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  139. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos);
  140. }
  141. /**
  142. * Gets the path where the user selected to do the last upload of a file shared from other app.
  143. *
  144. * @param context Caller {@link Context}, used to access to shared preferences manager.
  145. * @return path Absolute path to a folder, as previously stored by {@link #setLastUploadPath(Context, String)},
  146. * or empty String if never saved before.
  147. */
  148. public static String getLastUploadPath(Context context) {
  149. return getDefaultSharedPreferences(context).getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
  150. }
  151. /**
  152. * Saves the path where the user selected to do the last upload of a file shared from other app.
  153. *
  154. * @param context Caller {@link Context}, used to access to shared preferences manager.
  155. * @param path Absolute path to a folder.
  156. */
  157. public static void setLastUploadPath(Context context, String path) {
  158. saveStringPreference(context, AUTO_PREF__LAST_UPLOAD_PATH, path);
  159. }
  160. /**
  161. * Gets the lock preference configured by the user.
  162. *
  163. * @param context Caller {@link Context}, used to access to shared preferences manager.
  164. * @return lock lock preference value.
  165. */
  166. public static String getLockPreference(Context context) {
  167. return getDefaultSharedPreferences(context).getString(Preferences.PREFERENCE_LOCK, "");
  168. }
  169. /**
  170. * Gets the lock via fingerprint preference configured by the user.
  171. *
  172. * @param context Caller {@link Context}, used to access to shared preferences manager.
  173. * @return useFingerprint is lock via fingerprint preference.
  174. */
  175. public static boolean isUseFingerprint(Context context) {
  176. return getDefaultSharedPreferences(context).getBoolean(Preferences.PREFERENCE_USE_FINGERPRINT, false);
  177. }
  178. /**
  179. * Get preferred folder display type.
  180. *
  181. * @param context Caller {@link Context}, used to access to preferences manager.
  182. * @param folder Folder
  183. * @return preference value, default is
  184. * {@link com.owncloud.android.ui.fragment.OCFileListFragment#FOLDER_LAYOUT_LIST}
  185. */
  186. public static String getFolderLayout(Context context, OCFile folder) {
  187. return getFolderPreference(context, PREF__FOLDER_LAYOUT, folder, FOLDER_LAYOUT_LIST);
  188. }
  189. /**
  190. * Set preferred folder display type.
  191. *
  192. * @param context Caller {@link Context}, used to access to shared preferences manager.
  193. * @param folder Folder
  194. * @param layout_name preference value
  195. */
  196. public static void setFolderLayout(Context context, OCFile folder, String layout_name) {
  197. setFolderPreference(context, PREF__FOLDER_LAYOUT, folder, layout_name);
  198. }
  199. /**
  200. * Get preferred folder sort order.
  201. *
  202. * @param context Caller {@link Context}, used to access to shared preferences manager.
  203. * @return sort order the sort order, default is {@link FileSortOrder#sort_a_to_z} (sort by name)
  204. */
  205. public static FileSortOrder getSortOrder(Context context, OCFile folder) {
  206. return FileSortOrder.sortOrders.get(getFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder,
  207. FileSortOrder.sort_a_to_z.mName));
  208. }
  209. /**
  210. * Set preferred folder sort order.
  211. *
  212. * @param context Caller {@link Context}, used to access to shared preferences manager.
  213. * @param sortOrder the sort order
  214. */
  215. public static void setSortOrder(Context context, OCFile folder, FileSortOrder sortOrder) {
  216. setFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder, sortOrder.mName);
  217. }
  218. /**
  219. * Get preference value for a folder.
  220. * If folder is not set itself, it finds an ancestor that is set.
  221. *
  222. * @param context Context object.
  223. * @param preferenceName Name of the preference to lookup.
  224. * @param folder Folder.
  225. * @param defaultValue Fallback value in case no ancestor is set.
  226. * @return Preference value
  227. */
  228. public static String getFolderPreference(Context context, String preferenceName, OCFile folder,
  229. String defaultValue) {
  230. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  231. if (account == null) {
  232. return defaultValue;
  233. }
  234. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  235. FileDataStorageManager storageManager = ((ComponentsGetter)context).getStorageManager();
  236. if (storageManager == null) {
  237. storageManager = new FileDataStorageManager(account, context.getContentResolver());
  238. }
  239. String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
  240. while (folder != null && value.isEmpty()) {
  241. folder = storageManager.getFileById(folder.getParentId());
  242. value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
  243. }
  244. return value.isEmpty() ? defaultValue : value;
  245. }
  246. /**
  247. * Set preference value for a folder.
  248. *
  249. * @param context Context object.
  250. * @param preferenceName Name of the preference to set.
  251. * @param folder Folder.
  252. * @param value Preference value to set.
  253. */
  254. public static void setFolderPreference(Context context, String preferenceName, OCFile folder, String value) {
  255. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  256. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  257. dataProvider.storeOrUpdateKeyValue(account.name, getKeyFromFolder(preferenceName, folder), value);
  258. }
  259. private static String getKeyFromFolder(String preferenceName, OCFile folder) {
  260. final String folderIdString = String.valueOf(folder != null ? folder.getFileId() :
  261. FileDataStorageManager.ROOT_PARENT_ID);
  262. return preferenceName + "_" + folderIdString;
  263. }
  264. public static boolean getAutoUploadInit(Context context) {
  265. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_INIT, false);
  266. }
  267. /**
  268. * Gets the legacy cleaning flag last set.
  269. *
  270. * @param context Caller {@link Context}, used to access to shared preferences manager.
  271. * @return ascending order the legacy cleaning flag, default is false
  272. */
  273. public static boolean getLegacyClean(Context context) {
  274. return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
  275. }
  276. public static boolean getKeysMigration(Context context) {
  277. return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_MIGRATION, false);
  278. }
  279. public static boolean getStoragePathFix(Context context) {
  280. return getDefaultSharedPreferences(context).getBoolean(PREF__FIX_STORAGE_PATH, false);
  281. }
  282. /**
  283. * Gets the auto upload paths flag last set.
  284. *
  285. * @param context Caller {@link Context}, used to access to shared preferences manager.
  286. * @return ascending order the legacy cleaning flag, default is false
  287. */
  288. public static boolean getAutoUploadPathsUpdate(Context context) {
  289. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_UPDATE_PATH, false);
  290. }
  291. /**
  292. * Gets the auto upload split out flag last set.
  293. *
  294. * @param context Caller {@link Context}, used to access to shared preferences manager.
  295. * @return ascending order the legacy cleaning flag, default is false
  296. */
  297. public static boolean getAutoUploadSplitEntries(Context context) {
  298. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_SPLIT_OUT, false);
  299. }
  300. /**
  301. * Saves the legacy cleaning flag which the user has set last.
  302. *
  303. * @param context Caller {@link Context}, used to access to shared preferences manager.
  304. * @param legacyClean flag if it is a legacy cleaning
  305. */
  306. public static void setLegacyClean(Context context, boolean legacyClean) {
  307. saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean);
  308. }
  309. public static void setKeysMigration(Context context, boolean keysMigration) {
  310. saveBooleanPreference(context, PREF__KEYS_MIGRATION, keysMigration);
  311. }
  312. public static void setStoragePathFix(Context context, boolean storagePathFix) {
  313. saveBooleanPreference(context, PREF__FIX_STORAGE_PATH, storagePathFix);
  314. }
  315. public static void setAutoUploadInit(Context context, boolean autoUploadInit) {
  316. saveBooleanPreference(context, PREF__AUTO_UPLOAD_INIT, autoUploadInit);
  317. }
  318. /**
  319. * Saves the legacy cleaning flag which the user has set last.
  320. *
  321. * @param context Caller {@link Context}, used to access to shared preferences manager.
  322. * @param pathUpdate flag if it is a auto upload path update
  323. */
  324. public static void setAutoUploadPathsUpdate(Context context, boolean pathUpdate) {
  325. saveBooleanPreference(context, PREF__AUTO_UPLOAD_UPDATE_PATH, pathUpdate);
  326. }
  327. /**
  328. * Saves the flag for split entries magic
  329. *
  330. * @param context Caller {@link Context}, used to access to shared preferences manager.
  331. * @param splitOut flag if it is a auto upload path update
  332. */
  333. public static void setAutoUploadSplitEntries(Context context, boolean splitOut) {
  334. saveBooleanPreference(context, PREF__AUTO_UPLOAD_SPLIT_OUT, splitOut);
  335. }
  336. /**
  337. * Gets the uploader behavior which the user has set last.
  338. *
  339. * @param context Caller {@link Context}, used to access to shared preferences manager.
  340. * @return uploader behavior the uploader behavior
  341. */
  342. public static int getUploaderBehaviour(Context context) {
  343. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOADER_BEHAVIOR, 1);
  344. }
  345. /**
  346. * Saves the uploader behavior which the user has set last.
  347. *
  348. * @param context Caller {@link Context}, used to access to shared preferences manager.
  349. * @param uploaderBehaviour the uploader behavior
  350. */
  351. public static void setUploaderBehaviour(Context context, int uploaderBehaviour) {
  352. saveIntPreference(context, AUTO_PREF__UPLOADER_BEHAVIOR, uploaderBehaviour);
  353. }
  354. /**
  355. * Gets the grid columns which the user has set last.
  356. *
  357. * @param context Caller {@link Context}, used to access to shared preferences manager.
  358. * @return grid columns grid columns
  359. */
  360. public static float getGridColumns(Context context) {
  361. return getDefaultSharedPreferences(context).getFloat(AUTO_PREF__GRID_COLUMNS, 4.0f);
  362. }
  363. /**
  364. * Saves the grid columns which the user has set last.
  365. *
  366. * @param context Caller {@link Context}, used to access to shared preferences manager.
  367. * @param gridColumns the uploader behavior
  368. */
  369. public static void setGridColumns(Context context, float gridColumns) {
  370. saveFloatPreference(context, AUTO_PREF__GRID_COLUMNS, gridColumns);
  371. }
  372. private static void saveBooleanPreference(Context context, String key, boolean value) {
  373. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  374. appPreferences.putBoolean(key, value).apply();
  375. }
  376. private static void saveStringPreference(Context context, String key, String value) {
  377. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  378. appPreferences.putString(key, value).apply();
  379. }
  380. private static void saveStringPreferenceNow(Context context, String key, String value) {
  381. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  382. appPreferences.putString(key, value);
  383. appPreferences.apply();
  384. }
  385. private static void saveIntPreference(Context context, String key, int value) {
  386. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  387. appPreferences.putInt(key, value).apply();
  388. }
  389. private static void saveFloatPreference(Context context, String key, float value) {
  390. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  391. appPreferences.putFloat(key, value).apply();
  392. }
  393. public static SharedPreferences getDefaultSharedPreferences(Context context) {
  394. return android.preference.PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
  395. }
  396. }