PreferenceManager.java 10 KB

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