PreferenceManager.java 18 KB

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