PreferenceManager.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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.utils.FileSortOrder;
  29. import static com.owncloud.android.ui.fragment.OCFileListFragment.FOLDER_LAYOUT_LIST;
  30. /**
  31. * Helper to simplify reading of Preferences all around the app
  32. */
  33. public abstract class PreferenceManager {
  34. /**
  35. * Constant to access value of last path selected by the user to upload a file shared from other app.
  36. * Value handled by the app without direct access in the UI.
  37. */
  38. private static final String AUTO_PREF__LAST_UPLOAD_PATH = "last_upload_path";
  39. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL = "prefs_upload_file_extension_map_url";
  40. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_URL = "prefs_upload_file_extension_url";
  41. private static final String AUTO_PREF__UPLOADER_BEHAVIOR = "prefs_uploader_behaviour";
  42. private static final String AUTO_PREF__GRID_COLUMNS = "grid_columns";
  43. private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
  44. private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
  45. private static final String PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS = "instant_upload_path_use_subfolders";
  46. private static final String PREF__INSTANT_UPLOAD_ON_WIFI = "instant_upload_on_wifi";
  47. private static final String PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI = "instant_video_upload_on_wifi";
  48. private static final String PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS
  49. = "instant_video_upload_path_use_subfolders";
  50. private static final String PREF__LEGACY_CLEAN = "legacyClean";
  51. public static final String PREF__KEYS_MIGRATION = "keysMigration";
  52. private static final String PREF__FIX_STORAGE_PATH = "storagePathFix";
  53. private static final String PREF__KEYS_REINIT = "keysReinit";
  54. private static final String PREF__AUTO_UPLOAD_UPDATE_PATH = "autoUploadPathUpdate";
  55. private static final String PREF__PUSH_TOKEN = "pushToken";
  56. private static final String PREF__AUTO_UPLOAD_SPLIT_OUT = "autoUploadEntriesSplitOut";
  57. private static final String PREF__AUTO_UPLOAD_INIT = "autoUploadInit";
  58. private static final String PREF__FOLDER_SORT_ORDER = "folder_sort_order";
  59. private static final String PREF__FOLDER_LAYOUT = "folder_layout";
  60. private static final String KEY_FAB_EVER_CLICKED = "FAB_EVER_CLICKED";
  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. public static long getFABClicked(Context context) {
  102. return getDefaultSharedPreferences(context).getLong(KEY_FAB_EVER_CLICKED, 0);
  103. }
  104. public static void setFABClicked(Context context) {
  105. getDefaultSharedPreferences(context).edit().putLong(KEY_FAB_EVER_CLICKED, 1).apply();
  106. }
  107. /**
  108. * Gets the selected file extension position the user selected to do the last upload of a url file shared from other
  109. * app.
  110. *
  111. * @param context Caller {@link Context}, used to access to shared preferences manager.
  112. * @return selectedPos the selected file extension position.
  113. */
  114. public static int getUploadUrlFileExtensionUrlSelectedPos(Context context) {
  115. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
  116. }
  117. /**
  118. * Saves the selected file extension position the user selected to do the last upload of a url file shared from
  119. * other app.
  120. *
  121. * @param context Caller {@link Context}, used to access to shared preferences manager.
  122. * @param selectedPos the selected file extension position.
  123. */
  124. public static void setUploadUrlFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  125. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos);
  126. }
  127. /**
  128. * Gets the selected map file extension position the user selected to do the last upload of a url file shared
  129. * from other app.
  130. *
  131. * @param context Caller {@link Context}, used to access to shared preferences manager.
  132. * @return selectedPos the selected file extension position.
  133. */
  134. public static int getUploadMapFileExtensionUrlSelectedPos(Context context) {
  135. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
  136. }
  137. /**
  138. * Saves the selected map file extension position the user selected to do the last upload of a url file shared from
  139. * other app.
  140. *
  141. * @param context Caller {@link Context}, used to access to shared preferences manager.
  142. * @param selectedPos the selected file extension position.
  143. */
  144. public static void setUploadMapFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  145. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos);
  146. }
  147. /**
  148. * Gets the path where the user selected to do the last upload of a file shared from other app.
  149. *
  150. * @param context Caller {@link Context}, used to access to shared preferences manager.
  151. * @return path Absolute path to a folder, as previously stored by {@link #setLastUploadPath(Context, String)},
  152. * or empty String if never saved before.
  153. */
  154. public static String getLastUploadPath(Context context) {
  155. return getDefaultSharedPreferences(context).getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
  156. }
  157. /**
  158. * Saves the path where the user selected to do the last upload of a file shared from other app.
  159. *
  160. * @param context Caller {@link Context}, used to access to shared preferences manager.
  161. * @param path Absolute path to a folder.
  162. */
  163. public static void setLastUploadPath(Context context, String path) {
  164. saveStringPreference(context, AUTO_PREF__LAST_UPLOAD_PATH, path);
  165. }
  166. /**
  167. * Get preferred folder display type.
  168. *
  169. * @param context Caller {@link Context}, used to access to preferences manager.
  170. * @param folder Folder
  171. * @return preference value, default is
  172. * {@link com.owncloud.android.ui.fragment.OCFileListFragment#FOLDER_LAYOUT_LIST}
  173. */
  174. public static String getFolderLayout(Context context, OCFile folder) {
  175. return getFolderPreference(context, PREF__FOLDER_LAYOUT, folder, FOLDER_LAYOUT_LIST);
  176. }
  177. /**
  178. * Set preferred folder display type.
  179. *
  180. * @param context Caller {@link Context}, used to access to shared preferences manager.
  181. * @param folder Folder
  182. * @param layout_name preference value
  183. */
  184. public static void setFolderLayout(Context context, OCFile folder, String layout_name) {
  185. setFolderPreference(context, PREF__FOLDER_LAYOUT, folder, layout_name);
  186. }
  187. /**
  188. * Get preferred folder sort order.
  189. *
  190. * @param context Caller {@link Context}, used to access to shared preferences manager.
  191. * @return sort order the sort order, default is {@link FileSortOrder#sort_a_to_z} (sort by name)
  192. */
  193. public static FileSortOrder getSortOrder(Context context, OCFile folder) {
  194. return FileSortOrder.sortOrders.get(getFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder,
  195. FileSortOrder.sort_a_to_z.mName));
  196. }
  197. /**
  198. * Set preferred folder sort order.
  199. *
  200. * @param context Caller {@link Context}, used to access to shared preferences manager.
  201. * @param sortOrder the sort order
  202. */
  203. public static void setSortOrder(Context context, OCFile folder, FileSortOrder sortOrder) {
  204. setFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder, sortOrder.mName);
  205. }
  206. /**
  207. * Get preference value for a folder.
  208. * If folder is not set itself, it finds an ancestor that is set.
  209. *
  210. * @param context Context object.
  211. * @param preferenceName Name of the preference to lookup.
  212. * @param folder Folder.
  213. * @param defaultValue Fallback value in case no ancestor is set.
  214. * @return Preference value
  215. */
  216. public static String getFolderPreference(Context context, String preferenceName, OCFile folder,
  217. String defaultValue) {
  218. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  219. if (account == null) {
  220. return defaultValue;
  221. }
  222. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  223. FileDataStorageManager storageManager = ((ComponentsGetter)context).getStorageManager();
  224. if (storageManager == null) {
  225. storageManager = new FileDataStorageManager(account, context.getContentResolver());
  226. }
  227. String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
  228. while (folder != null && value.isEmpty()) {
  229. folder = storageManager.getFileById(folder.getParentId());
  230. value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
  231. }
  232. return value.isEmpty() ? defaultValue : value;
  233. }
  234. /**
  235. * Set preference value for a folder.
  236. *
  237. * @param context Context object.
  238. * @param preferenceName Name of the preference to set.
  239. * @param folder Folder.
  240. * @param value Preference value to set.
  241. */
  242. public static void setFolderPreference(Context context, String preferenceName, OCFile folder, String value) {
  243. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  244. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  245. dataProvider.storeOrUpdateKeyValue(account.name, getKeyFromFolder(preferenceName, folder), value);
  246. }
  247. private static String getKeyFromFolder(String preferenceName, OCFile folder) {
  248. final String folderIdString = String.valueOf(folder != null ? folder.getFileId() :
  249. FileDataStorageManager.ROOT_PARENT_ID);
  250. return preferenceName + "_" + folderIdString;
  251. }
  252. public static boolean getAutoUploadInit(Context context) {
  253. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_INIT, false);
  254. }
  255. /**
  256. * Gets the legacy cleaning flag last set.
  257. *
  258. * @param context Caller {@link Context}, used to access to shared preferences manager.
  259. * @return ascending order the legacy cleaning flag, default is false
  260. */
  261. public static boolean getLegacyClean(Context context) {
  262. return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
  263. }
  264. public static boolean getKeysMigration(Context context) {
  265. return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_MIGRATION, false);
  266. }
  267. public static boolean getStoragePathFix(Context context) {
  268. return getDefaultSharedPreferences(context).getBoolean(PREF__FIX_STORAGE_PATH, false);
  269. }
  270. /**
  271. * Gets the auto upload paths flag last set.
  272. *
  273. * @param context Caller {@link Context}, used to access to shared preferences manager.
  274. * @return ascending order the legacy cleaning flag, default is false
  275. */
  276. public static boolean getAutoUploadPathsUpdate(Context context) {
  277. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_UPDATE_PATH, false);
  278. }
  279. /**
  280. * Gets the auto upload split out flag last set.
  281. *
  282. * @param context Caller {@link Context}, used to access to shared preferences manager.
  283. * @return ascending order the legacy cleaning flag, default is false
  284. */
  285. public static boolean getAutoUploadSplitEntries(Context context) {
  286. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_SPLIT_OUT, false);
  287. }
  288. /**
  289. * Saves the legacy cleaning flag which the user has set last.
  290. *
  291. * @param context Caller {@link Context}, used to access to shared preferences manager.
  292. * @param legacyClean flag if it is a legacy cleaning
  293. */
  294. public static void setLegacyClean(Context context, boolean legacyClean) {
  295. saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean);
  296. }
  297. public static void setKeysMigration(Context context, boolean keysMigration) {
  298. saveBooleanPreference(context, PREF__KEYS_MIGRATION, keysMigration);
  299. }
  300. public static void setStoragePathFix(Context context, boolean storagePathFix) {
  301. saveBooleanPreference(context, PREF__FIX_STORAGE_PATH, storagePathFix);
  302. }
  303. public static void setAutoUploadInit(Context context, boolean autoUploadInit) {
  304. saveBooleanPreference(context, PREF__AUTO_UPLOAD_INIT, autoUploadInit);
  305. }
  306. /**
  307. * Saves the legacy cleaning flag which the user has set last.
  308. *
  309. * @param context Caller {@link Context}, used to access to shared preferences manager.
  310. * @param pathUpdate flag if it is a auto upload path update
  311. */
  312. public static void setAutoUploadPathsUpdate(Context context, boolean pathUpdate) {
  313. saveBooleanPreference(context, PREF__AUTO_UPLOAD_UPDATE_PATH, pathUpdate);
  314. }
  315. /**
  316. * Saves the flag for split entries magic
  317. *
  318. * @param context Caller {@link Context}, used to access to shared preferences manager.
  319. * @param splitOut flag if it is a auto upload path update
  320. */
  321. public static void setAutoUploadSplitEntries(Context context, boolean splitOut) {
  322. saveBooleanPreference(context, PREF__AUTO_UPLOAD_SPLIT_OUT, splitOut);
  323. }
  324. /**
  325. * Gets the uploader behavior which the user has set last.
  326. *
  327. * @param context Caller {@link Context}, used to access to shared preferences manager.
  328. * @return uploader behavior the uploader behavior
  329. */
  330. public static int getUploaderBehaviour(Context context) {
  331. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOADER_BEHAVIOR, 1);
  332. }
  333. /**
  334. * Saves the uploader behavior which the user has set last.
  335. *
  336. * @param context Caller {@link Context}, used to access to shared preferences manager.
  337. * @param uploaderBehaviour the uploader behavior
  338. */
  339. public static void setUploaderBehaviour(Context context, int uploaderBehaviour) {
  340. saveIntPreference(context, AUTO_PREF__UPLOADER_BEHAVIOR, uploaderBehaviour);
  341. }
  342. /**
  343. * Gets the grid columns which the user has set last.
  344. *
  345. * @param context Caller {@link Context}, used to access to shared preferences manager.
  346. * @return grid columns grid columns
  347. */
  348. public static float getGridColumns(Context context) {
  349. return getDefaultSharedPreferences(context).getFloat(AUTO_PREF__GRID_COLUMNS, 4.0f);
  350. }
  351. /**
  352. * Saves the grid columns which the user has set last.
  353. *
  354. * @param context Caller {@link Context}, used to access to shared preferences manager.
  355. * @param gridColumns the uploader behavior
  356. */
  357. public static void setGridColumns(Context context, float gridColumns) {
  358. saveFloatPreference(context, AUTO_PREF__GRID_COLUMNS, gridColumns);
  359. }
  360. private static void saveBooleanPreference(Context context, String key, boolean value) {
  361. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  362. appPreferences.putBoolean(key, value).apply();
  363. }
  364. private static void saveStringPreference(Context context, String key, String value) {
  365. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  366. appPreferences.putString(key, value).apply();
  367. }
  368. private static void saveStringPreferenceNow(Context context, String key, String value) {
  369. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  370. appPreferences.putString(key, value);
  371. appPreferences.apply();
  372. }
  373. private static void saveIntPreference(Context context, String key, int value) {
  374. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  375. appPreferences.putInt(key, value).apply();
  376. }
  377. private static void saveFloatPreference(Context context, String key, float value) {
  378. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  379. appPreferences.putFloat(key, value).apply();
  380. }
  381. public static SharedPreferences getDefaultSharedPreferences(Context context) {
  382. return android.preference.PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
  383. }
  384. }