PreferenceManager.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 final 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_FROM_LOCAL_LAST_PATH = "upload_from_local_last_path";
  41. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL = "prefs_upload_file_extension_map_url";
  42. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_URL = "prefs_upload_file_extension_url";
  43. private static final String AUTO_PREF__UPLOADER_BEHAVIOR = "prefs_uploader_behaviour";
  44. private static final String AUTO_PREF__GRID_COLUMNS = "grid_columns";
  45. public static final String AUTO_PREF__LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
  46. private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
  47. private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
  48. private static final String PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS = "instant_upload_path_use_subfolders";
  49. private static final String PREF__INSTANT_UPLOAD_ON_WIFI = "instant_upload_on_wifi";
  50. private static final String PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI = "instant_video_upload_on_wifi";
  51. private static final String PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS
  52. = "instant_video_upload_path_use_subfolders";
  53. private static final String PREF__LEGACY_CLEAN = "legacyClean";
  54. public static final String PREF__KEYS_MIGRATION = "keysMigration";
  55. private static final String PREF__FIX_STORAGE_PATH = "storagePathFix";
  56. private static final String PREF__KEYS_REINIT = "keysReinit";
  57. private static final String PREF__AUTO_UPLOAD_UPDATE_PATH = "autoUploadPathUpdate";
  58. private static final String PREF__PUSH_TOKEN = "pushToken";
  59. private static final String PREF__AUTO_UPLOAD_SPLIT_OUT = "autoUploadEntriesSplitOut";
  60. private static final String PREF__AUTO_UPLOAD_INIT = "autoUploadInit";
  61. private static final String PREF__FOLDER_SORT_ORDER = "folder_sort_order";
  62. private static final String PREF__FOLDER_LAYOUT = "folder_layout";
  63. private PreferenceManager() {
  64. }
  65. public static void setKeysReInit(Context context) {
  66. saveBooleanPreference(context, PREF__KEYS_REINIT, true);
  67. }
  68. public static boolean getKeysReInit(Context context) {
  69. return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_REINIT, false);
  70. }
  71. public static void setPushToken(Context context, String pushToken) {
  72. saveStringPreferenceNow(context, PREF__PUSH_TOKEN, pushToken);
  73. }
  74. public static String getPushToken(Context context) {
  75. return getDefaultSharedPreferences(context).getString(PREF__PUSH_TOKEN, "");
  76. }
  77. public static boolean instantPictureUploadEnabled(Context context) {
  78. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOADING, false);
  79. }
  80. public static boolean instantVideoUploadEnabled(Context context) {
  81. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOADING, false);
  82. }
  83. public static boolean instantPictureUploadPathUseSubfolders(Context context) {
  84. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS, false);
  85. }
  86. public static boolean instantPictureUploadViaWiFiOnly(Context context) {
  87. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_ON_WIFI, false);
  88. }
  89. public static boolean instantVideoUploadPathUseSubfolders(Context context) {
  90. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS, false);
  91. }
  92. public static boolean instantVideoUploadViaWiFiOnly(Context context) {
  93. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI, false);
  94. }
  95. public static boolean instantPictureUploadWhenChargingOnly(Context context) {
  96. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_charging", false);
  97. }
  98. public static boolean instantVideoUploadWhenChargingOnly(Context context) {
  99. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_charging",
  100. false);
  101. }
  102. public static boolean showHiddenFilesEnabled(Context context) {
  103. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("show_hidden_files_pref", false);
  104. }
  105. /**
  106. * Gets the selected file extension position the user selected to do the last upload of a url file shared from other
  107. * app.
  108. *
  109. * @param context Caller {@link Context}, used to access to shared preferences manager.
  110. * @return selectedPos the selected file extension position.
  111. */
  112. public static int getUploadUrlFileExtensionUrlSelectedPos(Context context) {
  113. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
  114. }
  115. /**
  116. * Saves the selected file extension position the user selected to do the last upload of a url file shared from
  117. * other app.
  118. *
  119. * @param context Caller {@link Context}, used to access to shared preferences manager.
  120. * @param selectedPos the selected file extension position.
  121. */
  122. public static void setUploadUrlFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  123. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos);
  124. }
  125. /**
  126. * Gets the selected map file extension position the user selected to do the last upload of a url file shared
  127. * from other app.
  128. *
  129. * @param context Caller {@link Context}, used to access to shared preferences manager.
  130. * @return selectedPos the selected file extension position.
  131. */
  132. public static int getUploadMapFileExtensionUrlSelectedPos(Context context) {
  133. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
  134. }
  135. /**
  136. * Saves the selected map file extension position the user selected to do the last upload of a url file shared from
  137. * other app.
  138. *
  139. * @param context Caller {@link Context}, used to access to shared preferences manager.
  140. * @param selectedPos the selected file extension position.
  141. */
  142. public static void setUploadMapFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  143. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos);
  144. }
  145. /**
  146. * Gets the path where the user selected to do the last upload of a file shared from other app.
  147. *
  148. * @param context Caller {@link Context}, used to access to shared preferences manager.
  149. * @return path Absolute path to a folder, as previously stored by {@link #setLastUploadPath(Context, String)},
  150. * or empty String if never saved before.
  151. */
  152. public static String getLastUploadPath(Context context) {
  153. return getDefaultSharedPreferences(context).getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
  154. }
  155. /**
  156. * Saves the path where the user selected to do the last upload of a file shared from other app.
  157. *
  158. * @param context Caller {@link Context}, used to access to shared preferences manager.
  159. * @param path Absolute path to a folder.
  160. */
  161. public static void setLastUploadPath(Context context, String path) {
  162. saveStringPreference(context, AUTO_PREF__LAST_UPLOAD_PATH, path);
  163. }
  164. /**
  165. * Gets the last local path where the user selected to do an upload from.
  166. *
  167. * @param context Caller {@link Context}, used to access to shared preferences manager.
  168. * @return path Absolute path to a folder, as previously stored by
  169. * {@link #setUploadFromLocalLastPath(Context, String)}, or empty String if never saved before.
  170. */
  171. public static String getUploadFromLocalLastPath(Context context) {
  172. return getDefaultSharedPreferences(context).getString(AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, "");
  173. }
  174. /**
  175. * Saves the path where the user selected to do the last local upload of a file from.
  176. *
  177. * @param context Caller {@link Context}, used to access to shared preferences manager.
  178. * @param path Absolute path to a folder.
  179. */
  180. public static void setUploadFromLocalLastPath(Context context, String path) {
  181. saveStringPreference(context, AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, path);
  182. }
  183. /**
  184. * Gets the lock preference configured by the user.
  185. *
  186. * @param context Caller {@link Context}, used to access to shared preferences manager.
  187. * @return lock lock preference value.
  188. */
  189. public static String getLockPreference(Context context) {
  190. return getDefaultSharedPreferences(context).getString(Preferences.PREFERENCE_LOCK, "");
  191. }
  192. /**
  193. * Gets the lock via fingerprint preference configured by the user.
  194. *
  195. * @param context Caller {@link Context}, used to access to shared preferences manager.
  196. * @return useFingerprint is lock via fingerprint preference.
  197. */
  198. public static boolean isUseFingerprint(Context context) {
  199. return getDefaultSharedPreferences(context).getBoolean(Preferences.PREFERENCE_USE_FINGERPRINT, false);
  200. }
  201. /**
  202. * Get preferred folder display type.
  203. *
  204. * @param context Caller {@link Context}, used to access to preferences manager.
  205. * @param folder Folder
  206. * @return preference value, default is
  207. * {@link com.owncloud.android.ui.fragment.OCFileListFragment#FOLDER_LAYOUT_LIST}
  208. */
  209. public static String getFolderLayout(Context context, OCFile folder) {
  210. return getFolderPreference(context, PREF__FOLDER_LAYOUT, folder, FOLDER_LAYOUT_LIST);
  211. }
  212. /**
  213. * Set preferred folder display type.
  214. *
  215. * @param context Caller {@link Context}, used to access to shared preferences manager.
  216. * @param folder Folder
  217. * @param layout_name preference value
  218. */
  219. public static void setFolderLayout(Context context, OCFile folder, String layout_name) {
  220. setFolderPreference(context, PREF__FOLDER_LAYOUT, folder, layout_name);
  221. }
  222. /**
  223. * Get preferred folder sort order.
  224. *
  225. * @param context Caller {@link Context}, used to access to shared preferences manager.
  226. * @return sort order the sort order, default is {@link FileSortOrder#sort_a_to_z} (sort by name)
  227. */
  228. public static FileSortOrder getSortOrder(Context context, OCFile folder) {
  229. return FileSortOrder.sortOrders.get(getFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder,
  230. FileSortOrder.sort_a_to_z.mName));
  231. }
  232. /**
  233. * Set preferred folder sort order.
  234. *
  235. * @param context Caller {@link Context}, used to access to shared preferences manager.
  236. * @param sortOrder the sort order
  237. */
  238. public static void setSortOrder(Context context, OCFile folder, FileSortOrder sortOrder) {
  239. setFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder, sortOrder.mName);
  240. }
  241. /**
  242. * Get preference value for a folder.
  243. * If folder is not set itself, it finds an ancestor that is set.
  244. *
  245. * @param context Context object.
  246. * @param preferenceName Name of the preference to lookup.
  247. * @param folder Folder.
  248. * @param defaultValue Fallback value in case no ancestor is set.
  249. * @return Preference value
  250. */
  251. public static String getFolderPreference(Context context, String preferenceName, OCFile folder,
  252. String defaultValue) {
  253. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  254. if (account == null) {
  255. return defaultValue;
  256. }
  257. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  258. FileDataStorageManager storageManager = ((ComponentsGetter)context).getStorageManager();
  259. if (storageManager == null) {
  260. storageManager = new FileDataStorageManager(account, context.getContentResolver());
  261. }
  262. String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
  263. while (folder != null && value.isEmpty()) {
  264. folder = storageManager.getFileById(folder.getParentId());
  265. value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
  266. }
  267. return value.isEmpty() ? defaultValue : value;
  268. }
  269. /**
  270. * Set preference value for a folder.
  271. *
  272. * @param context Context object.
  273. * @param preferenceName Name of the preference to set.
  274. * @param folder Folder.
  275. * @param value Preference value to set.
  276. */
  277. public static void setFolderPreference(Context context, String preferenceName, OCFile folder, String value) {
  278. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  279. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  280. dataProvider.storeOrUpdateKeyValue(account.name, getKeyFromFolder(preferenceName, folder), value);
  281. }
  282. private static String getKeyFromFolder(String preferenceName, OCFile folder) {
  283. final String folderIdString = String.valueOf(folder != null ? folder.getFileId() :
  284. FileDataStorageManager.ROOT_PARENT_ID);
  285. return preferenceName + "_" + folderIdString;
  286. }
  287. public static boolean getAutoUploadInit(Context context) {
  288. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_INIT, false);
  289. }
  290. /**
  291. * Gets the legacy cleaning flag last set.
  292. *
  293. * @param context Caller {@link Context}, used to access to shared preferences manager.
  294. * @return ascending order the legacy cleaning flag, default is false
  295. */
  296. public static boolean getLegacyClean(Context context) {
  297. return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
  298. }
  299. public static boolean getKeysMigration(Context context) {
  300. return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_MIGRATION, false);
  301. }
  302. public static boolean getStoragePathFix(Context context) {
  303. return getDefaultSharedPreferences(context).getBoolean(PREF__FIX_STORAGE_PATH, false);
  304. }
  305. /**
  306. * Gets the auto upload paths flag last set.
  307. *
  308. * @param context Caller {@link Context}, used to access to shared preferences manager.
  309. * @return ascending order the legacy cleaning flag, default is false
  310. */
  311. public static boolean getAutoUploadPathsUpdate(Context context) {
  312. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_UPDATE_PATH, false);
  313. }
  314. /**
  315. * Gets the auto upload split out flag last set.
  316. *
  317. * @param context Caller {@link Context}, used to access to shared preferences manager.
  318. * @return ascending order the legacy cleaning flag, default is false
  319. */
  320. public static boolean getAutoUploadSplitEntries(Context context) {
  321. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_SPLIT_OUT, false);
  322. }
  323. /**
  324. * Saves the legacy cleaning flag which the user has set last.
  325. *
  326. * @param context Caller {@link Context}, used to access to shared preferences manager.
  327. * @param legacyClean flag if it is a legacy cleaning
  328. */
  329. public static void setLegacyClean(Context context, boolean legacyClean) {
  330. saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean);
  331. }
  332. public static void setKeysMigration(Context context, boolean keysMigration) {
  333. saveBooleanPreference(context, PREF__KEYS_MIGRATION, keysMigration);
  334. }
  335. public static void setStoragePathFix(Context context, boolean storagePathFix) {
  336. saveBooleanPreference(context, PREF__FIX_STORAGE_PATH, storagePathFix);
  337. }
  338. public static void setAutoUploadInit(Context context, boolean autoUploadInit) {
  339. saveBooleanPreference(context, PREF__AUTO_UPLOAD_INIT, autoUploadInit);
  340. }
  341. /**
  342. * Saves the legacy cleaning flag which the user has set last.
  343. *
  344. * @param context Caller {@link Context}, used to access to shared preferences manager.
  345. * @param pathUpdate flag if it is a auto upload path update
  346. */
  347. public static void setAutoUploadPathsUpdate(Context context, boolean pathUpdate) {
  348. saveBooleanPreference(context, PREF__AUTO_UPLOAD_UPDATE_PATH, pathUpdate);
  349. }
  350. /**
  351. * Saves the flag for split entries magic
  352. *
  353. * @param context Caller {@link Context}, used to access to shared preferences manager.
  354. * @param splitOut flag if it is a auto upload path update
  355. */
  356. public static void setAutoUploadSplitEntries(Context context, boolean splitOut) {
  357. saveBooleanPreference(context, PREF__AUTO_UPLOAD_SPLIT_OUT, splitOut);
  358. }
  359. /**
  360. * Gets the uploader behavior which the user has set last.
  361. *
  362. * @param context Caller {@link Context}, used to access to shared preferences manager.
  363. * @return uploader behavior the uploader behavior
  364. */
  365. public static int getUploaderBehaviour(Context context) {
  366. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOADER_BEHAVIOR, 1);
  367. }
  368. /**
  369. * Saves the uploader behavior which the user has set last.
  370. *
  371. * @param context Caller {@link Context}, used to access to shared preferences manager.
  372. * @param uploaderBehaviour the uploader behavior
  373. */
  374. public static void setUploaderBehaviour(Context context, int uploaderBehaviour) {
  375. saveIntPreference(context, AUTO_PREF__UPLOADER_BEHAVIOR, uploaderBehaviour);
  376. }
  377. /**
  378. * Gets the grid columns which the user has set last.
  379. *
  380. * @param context Caller {@link Context}, used to access to shared preferences manager.
  381. * @return grid columns grid columns
  382. */
  383. public static float getGridColumns(Context context) {
  384. return getDefaultSharedPreferences(context).getFloat(AUTO_PREF__GRID_COLUMNS, 4.0f);
  385. }
  386. /**
  387. * Saves the grid columns which the user has set last.
  388. *
  389. * @param context Caller {@link Context}, used to access to shared preferences manager.
  390. * @param gridColumns the uploader behavior
  391. */
  392. public static void setGridColumns(Context context, float gridColumns) {
  393. saveFloatPreference(context, AUTO_PREF__GRID_COLUMNS, gridColumns);
  394. }
  395. /**
  396. * Gets the last seen version code right before updating.
  397. *
  398. * @param context Caller {@link Context}, used to access to shared preferences manager.
  399. * @return grid columns grid columns
  400. */
  401. public static int getLastSeenVersionCode(Context context) {
  402. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, 0);
  403. }
  404. /**
  405. * Saves the version code as the last seen version code.
  406. *
  407. * @param context Caller {@link Context}, used to access to shared preferences manager.
  408. * @param versionCode the app's version code
  409. */
  410. public static void setLastSeenVersionCode(Context context, int versionCode) {
  411. saveIntPreference(context, AUTO_PREF__LAST_SEEN_VERSION_CODE, versionCode);
  412. }
  413. private static void saveBooleanPreference(Context context, String key, boolean value) {
  414. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  415. appPreferences.putBoolean(key, value).apply();
  416. }
  417. private static void saveStringPreference(Context context, String key, String value) {
  418. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  419. appPreferences.putString(key, value).apply();
  420. }
  421. private static void saveStringPreferenceNow(Context context, String key, String value) {
  422. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  423. appPreferences.putString(key, value);
  424. appPreferences.apply();
  425. }
  426. private static void saveIntPreference(Context context, String key, int value) {
  427. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  428. appPreferences.putInt(key, value).apply();
  429. }
  430. private static void saveFloatPreference(Context context, String key, float value) {
  431. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  432. appPreferences.putFloat(key, value).apply();
  433. }
  434. public static SharedPreferences getDefaultSharedPreferences(Context context) {
  435. return android.preference.PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
  436. }
  437. }