PreferenceManager.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. public static boolean instantPictureUploadEnabled(Context context) {
  45. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOADING, false);
  46. }
  47. public static boolean instantVideoUploadEnabled(Context context) {
  48. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOADING, false);
  49. }
  50. public static boolean instantPictureUploadPathUseSubfolders(Context context) {
  51. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS, false);
  52. }
  53. public static boolean instantPictureUploadViaWiFiOnly(Context context) {
  54. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_ON_WIFI, false);
  55. }
  56. public static boolean instantVideoUploadPathUseSubfolders(Context context) {
  57. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS, false);
  58. }
  59. public static boolean instantVideoUploadViaWiFiOnly(Context context) {
  60. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI, false);
  61. }
  62. public static boolean instantPictureUploadWhenChargingOnly(Context context) {
  63. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_charging", false);
  64. }
  65. public static boolean instantVideoUploadWhenChargingOnly(Context context) {
  66. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_charging", false);
  67. }
  68. public static boolean showHiddenFilesEnabled(Context context) {
  69. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("show_hidden_files_pref", false);
  70. }
  71. /**
  72. * Gets the selected file extension position the user selected to do the last upload of a url file shared from other
  73. * app.
  74. *
  75. * @param context Caller {@link Context}, used to access to shared preferences manager.
  76. * @return selectedPos the selected file extension position.
  77. */
  78. public static int getUploadUrlFileExtensionUrlSelectedPos(Context context) {
  79. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
  80. }
  81. /**
  82. * Saves the selected file extension position the user selected to do the last upload of a url file shared from
  83. * other app.
  84. *
  85. * @param context Caller {@link Context}, used to access to shared preferences manager.
  86. * @param selectedPos the selected file extension position.
  87. */
  88. public static void setUploadUrlFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  89. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos);
  90. }
  91. /**
  92. * Gets the selected map file extension position the user selected to do the last upload of a url file shared
  93. * from other app.
  94. *
  95. * @param context Caller {@link Context}, used to access to shared preferences manager.
  96. * @return selectedPos the selected file extension position.
  97. */
  98. public static int getUploadMapFileExtensionUrlSelectedPos(Context context) {
  99. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
  100. }
  101. /**
  102. * Saves the selected map file extension position the user selected to do the last upload of a url file shared from
  103. * other app.
  104. *
  105. * @param context Caller {@link Context}, used to access to shared preferences manager.
  106. * @param selectedPos the selected file extension position.
  107. */
  108. public static void setUploadMapFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  109. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos);
  110. }
  111. /**
  112. * Gets the path where the user selected to do the last upload of a file shared from other app.
  113. *
  114. * @param context Caller {@link Context}, used to access to shared preferences manager.
  115. * @return path Absolute path to a folder, as previously stored by {@link #setLastUploadPath(Context, String)},
  116. * or empty String if never saved before.
  117. */
  118. public static String getLastUploadPath(Context context) {
  119. return getDefaultSharedPreferences(context).getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
  120. }
  121. /**
  122. * Saves the path where the user selected to do the last upload of a file shared from other app.
  123. *
  124. * @param context Caller {@link Context}, used to access to shared preferences manager.
  125. * @param path Absolute path to a folder.
  126. */
  127. public static void setLastUploadPath(Context context, String path) {
  128. saveStringPreference(context, AUTO_PREF__LAST_UPLOAD_PATH, path);
  129. }
  130. /**
  131. * Gets the sort order which the user has set last.
  132. *
  133. * @param context Caller {@link Context}, used to access to shared preferences manager.
  134. * @return sort order the sort order, default is {@link FileStorageUtils#SORT_NAME} (sort by name)
  135. */
  136. public static int getSortOrder(Context context) {
  137. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__SORT_ORDER, FileStorageUtils.SORT_NAME);
  138. }
  139. /**
  140. * Save the sort order which the user has set last.
  141. *
  142. * @param context Caller {@link Context}, used to access to shared preferences manager.
  143. * @param order the sort order
  144. */
  145. public static void setSortOrder(Context context, int order) {
  146. saveIntPreference(context, AUTO_PREF__SORT_ORDER, order);
  147. }
  148. /**
  149. * Gets the ascending order flag which the user has set last.
  150. *
  151. * @param context Caller {@link Context}, used to access to shared preferences manager.
  152. * @return ascending order the ascending order, default is true
  153. */
  154. public static boolean getSortAscending(Context context) {
  155. return getDefaultSharedPreferences(context).getBoolean(AUTO_PREF__SORT_ASCENDING, true);
  156. }
  157. /**
  158. * Saves the ascending order flag which the user has set last.
  159. *
  160. * @param context Caller {@link Context}, used to access to shared preferences manager.
  161. * @param ascending flag if sorting is ascending or descending
  162. */
  163. public static void setSortAscending(Context context, boolean ascending) {
  164. saveBooleanPreference(context, AUTO_PREF__SORT_ASCENDING, ascending);
  165. }
  166. /**
  167. * Gets the legacy cleaning flag last set.
  168. *
  169. * @param context Caller {@link Context}, used to access to shared preferences manager.
  170. * @return ascending order the alegacy cleaning flag, default is false
  171. */
  172. public static boolean getLegacyClean(Context context) {
  173. return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
  174. }
  175. /**
  176. * Saves the legacy cleaning flag which the user has set last.
  177. *
  178. * @param context Caller {@link Context}, used to access to shared preferences manager.
  179. * @param legacyClean flag if it is a legacy cleaning
  180. */
  181. public static void setLegacyClean(Context context, boolean legacyClean) {
  182. saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean);
  183. }
  184. /**
  185. * Gets the uploader behavior which the user has set last.
  186. *
  187. * @param context Caller {@link Context}, used to access to shared preferences manager.
  188. * @return uploader behavior the uploader behavior
  189. */
  190. public static int getUploaderBehaviour(Context context) {
  191. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOADER_BEHAVIOR, 1);
  192. }
  193. /**
  194. * Saves the uploader behavior which the user has set last.
  195. *
  196. * @param context Caller {@link Context}, used to access to shared preferences manager.
  197. * @param uploaderBehaviour the uploader behavior
  198. */
  199. public static void setUploaderBehaviour(Context context, int uploaderBehaviour) {
  200. saveIntPreference(context, AUTO_PREF__UPLOADER_BEHAVIOR, uploaderBehaviour);
  201. }
  202. public static void saveBooleanPreference(Context context, String key, boolean value) {
  203. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  204. appPreferences.putBoolean(key, value);
  205. appPreferences.apply();
  206. }
  207. private static void saveStringPreference(Context context, String key, String value) {
  208. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  209. appPreferences.putString(key, value);
  210. appPreferences.apply();
  211. }
  212. private static void saveIntPreference(Context context, String key, int value) {
  213. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  214. appPreferences.putInt(key, value);
  215. appPreferences.apply();
  216. }
  217. public static SharedPreferences getDefaultSharedPreferences(Context context) {
  218. return android.preference.PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
  219. }
  220. }