PreferenceManager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2016 ownCloud Inc.
  6. * <p/>
  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. * <p/>
  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. * <p/>
  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.content.Context;
  21. import android.content.SharedPreferences;
  22. import com.owncloud.android.utils.FileStorageUtils;
  23. /**
  24. * Helper to simplify reading of Preferences all around the app
  25. */
  26. public abstract class PreferenceManager {
  27. /**
  28. * Constant to access value of last path selected by the user to upload a file shared from other app.
  29. * Value handled by the app without direct access in the UI.
  30. */
  31. private static final String AUTO_PREF__LAST_UPLOAD_PATH = "last_upload_path";
  32. private static final String AUTO_PREF__SORT_ORDER = "sort_order";
  33. private static final String AUTO_PREF__SORT_ASCENDING = "sort_ascending";
  34. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL = "prefs_upload_file_extension_map_url";
  35. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_URL = "prefs_upload_file_extension_url";
  36. private static final String AUTO_PREF__UPLOADER_BEHAVIOR = "prefs_uploader_behaviour";
  37. private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
  38. private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
  39. private static final String PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS = "instant_upload_path_use_subfolders";
  40. private static final String PREF__INSTANT_UPLOAD_ON_WIFI = "instant_upload_on_wifi";
  41. private static final String PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI = "instant_video_upload_on_wifi";
  42. private static final String PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS = "instant_video_upload_path_use_subfolders";
  43. private static final String PREF__LEGACY_CLEAN = "legacyClean";
  44. private static final String PREF__AUTO_UPLOAD_UPDATE_PATH = "autoUploadPathUpdate";
  45. public static boolean instantPictureUploadEnabled(Context context) {
  46. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOADING, false);
  47. }
  48. public static boolean instantVideoUploadEnabled(Context context) {
  49. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOADING, false);
  50. }
  51. public static boolean instantPictureUploadPathUseSubfolders(Context context) {
  52. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS, false);
  53. }
  54. public static boolean instantPictureUploadViaWiFiOnly(Context context) {
  55. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_ON_WIFI, false);
  56. }
  57. public static boolean instantVideoUploadPathUseSubfolders(Context context) {
  58. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS, false);
  59. }
  60. public static boolean instantVideoUploadViaWiFiOnly(Context context) {
  61. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI, false);
  62. }
  63. public static boolean instantPictureUploadWhenChargingOnly(Context context) {
  64. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_charging", false);
  65. }
  66. public static boolean instantVideoUploadWhenChargingOnly(Context context) {
  67. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_charging", false);
  68. }
  69. public static boolean showHiddenFilesEnabled(Context context) {
  70. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("show_hidden_files_pref", false);
  71. }
  72. /**
  73. * Gets the selected file extension position the user selected to do the last upload of a url file shared from other
  74. * app.
  75. *
  76. * @param context Caller {@link Context}, used to access to shared preferences manager.
  77. * @return selectedPos the selected file extension position.
  78. */
  79. public static int getUploadUrlFileExtensionUrlSelectedPos(Context context) {
  80. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
  81. }
  82. /**
  83. * Saves the selected file extension position the user selected to do the last upload of a url file shared from
  84. * other app.
  85. *
  86. * @param context Caller {@link Context}, used to access to shared preferences manager.
  87. * @param selectedPos the selected file extension position.
  88. */
  89. public static void setUploadUrlFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  90. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos);
  91. }
  92. /**
  93. * Gets the selected map file extension position the user selected to do the last upload of a url file shared
  94. * from other app.
  95. *
  96. * @param context Caller {@link Context}, used to access to shared preferences manager.
  97. * @return selectedPos the selected file extension position.
  98. */
  99. public static int getUploadMapFileExtensionUrlSelectedPos(Context context) {
  100. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
  101. }
  102. /**
  103. * Saves the selected map file extension position the user selected to do the last upload of a url file shared from
  104. * other app.
  105. *
  106. * @param context Caller {@link Context}, used to access to shared preferences manager.
  107. * @param selectedPos the selected file extension position.
  108. */
  109. public static void setUploadMapFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  110. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos);
  111. }
  112. /**
  113. * Gets the path where the user selected to do the last upload of a file shared from other app.
  114. *
  115. * @param context Caller {@link Context}, used to access to shared preferences manager.
  116. * @return path Absolute path to a folder, as previously stored by {@link #setLastUploadPath(Context, String)},
  117. * or empty String if never saved before.
  118. */
  119. public static String getLastUploadPath(Context context) {
  120. return getDefaultSharedPreferences(context).getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
  121. }
  122. /**
  123. * Saves the path where the user selected to do the last upload of a file shared from other app.
  124. *
  125. * @param context Caller {@link Context}, used to access to shared preferences manager.
  126. * @param path Absolute path to a folder.
  127. */
  128. public static void setLastUploadPath(Context context, String path) {
  129. saveStringPreference(context, AUTO_PREF__LAST_UPLOAD_PATH, path);
  130. }
  131. /**
  132. * Gets the sort order which the user has set last.
  133. *
  134. * @param context Caller {@link Context}, used to access to shared preferences manager.
  135. * @return sort order the sort order, default is {@link FileStorageUtils#SORT_NAME} (sort by name)
  136. */
  137. public static int getSortOrder(Context context) {
  138. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__SORT_ORDER, FileStorageUtils.SORT_NAME);
  139. }
  140. /**
  141. * Save the sort order which the user has set last.
  142. *
  143. * @param context Caller {@link Context}, used to access to shared preferences manager.
  144. * @param order the sort order
  145. */
  146. public static void setSortOrder(Context context, int order) {
  147. saveIntPreference(context, AUTO_PREF__SORT_ORDER, order);
  148. }
  149. /**
  150. * Gets the ascending order flag which the user has set last.
  151. *
  152. * @param context Caller {@link Context}, used to access to shared preferences manager.
  153. * @return ascending order the ascending order, default is true
  154. */
  155. public static boolean getSortAscending(Context context) {
  156. return getDefaultSharedPreferences(context).getBoolean(AUTO_PREF__SORT_ASCENDING, true);
  157. }
  158. /**
  159. * Saves the ascending order flag which the user has set last.
  160. *
  161. * @param context Caller {@link Context}, used to access to shared preferences manager.
  162. * @param ascending flag if sorting is ascending or descending
  163. */
  164. public static void setSortAscending(Context context, boolean ascending) {
  165. saveBooleanPreference(context, AUTO_PREF__SORT_ASCENDING, ascending);
  166. }
  167. /**
  168. * Gets the legacy cleaning flag last set.
  169. *
  170. * @param context Caller {@link Context}, used to access to shared preferences manager.
  171. * @return ascending order the legacy cleaning flag, default is false
  172. */
  173. public static boolean getLegacyClean(Context context) {
  174. return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
  175. }
  176. /**
  177. * Gets the auto upload paths flag last set.
  178. *
  179. * @param context Caller {@link Context}, used to access to shared preferences manager.
  180. * @return ascending order the legacy cleaning flag, default is false
  181. */
  182. public static boolean getAutoUploadPathsUpdate(Context context) {
  183. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_UPDATE_PATH, false);
  184. }
  185. /**
  186. * Saves the legacy cleaning flag which the user has set last.
  187. *
  188. * @param context Caller {@link Context}, used to access to shared preferences manager.
  189. * @param legacyClean flag if it is a legacy cleaning
  190. */
  191. public static void setLegacyClean(Context context, boolean legacyClean) {
  192. saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean);
  193. }
  194. /**
  195. * Saves the legacy cleaning flag which the user has set last.
  196. *
  197. * @param context Caller {@link Context}, used to access to shared preferences manager.
  198. * @param pathUpdate flag if it is a auto upload path update
  199. */
  200. public static void setAutoUploadPathsUpdate(Context context, boolean pathUpdate) {
  201. saveBooleanPreference(context, PREF__AUTO_UPLOAD_UPDATE_PATH, pathUpdate);
  202. }
  203. /**
  204. * Gets the uploader behavior which the user has set last.
  205. *
  206. * @param context Caller {@link Context}, used to access to shared preferences manager.
  207. * @return uploader behavior the uploader behavior
  208. */
  209. public static int getUploaderBehaviour(Context context) {
  210. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOADER_BEHAVIOR, 1);
  211. }
  212. /**
  213. * Saves the uploader behavior which the user has set last.
  214. *
  215. * @param context Caller {@link Context}, used to access to shared preferences manager.
  216. * @param uploaderBehaviour the uploader behavior
  217. */
  218. public static void setUploaderBehaviour(Context context, int uploaderBehaviour) {
  219. saveIntPreference(context, AUTO_PREF__UPLOADER_BEHAVIOR, uploaderBehaviour);
  220. }
  221. public static void saveBooleanPreference(Context context, String key, boolean value) {
  222. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  223. appPreferences.putBoolean(key, value);
  224. appPreferences.apply();
  225. }
  226. private static void saveStringPreference(Context context, String key, String value) {
  227. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  228. appPreferences.putString(key, value);
  229. appPreferences.apply();
  230. }
  231. private static void saveIntPreference(Context context, String key, int value) {
  232. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  233. appPreferences.putInt(key, value);
  234. appPreferences.apply();
  235. }
  236. public static SharedPreferences getDefaultSharedPreferences(Context context) {
  237. return android.preference.PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
  238. }
  239. }