PreferenceManager.java 21 KB

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