PreferenceManager.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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.ui.activity.Preferences;
  29. import com.owncloud.android.utils.FileSortOrder;
  30. import static com.owncloud.android.ui.fragment.OCFileListFragment.FOLDER_LAYOUT_LIST;
  31. /**
  32. * Helper to simplify reading of Preferences all around the app
  33. */
  34. public final class PreferenceManager {
  35. /**
  36. * Constant to access value of last path selected by the user to upload a file shared from other app.
  37. * Value handled by the app without direct access in the UI.
  38. */
  39. private static final String AUTO_PREF__LAST_UPLOAD_PATH = "last_upload_path";
  40. private static final String AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH = "upload_from_local_last_path";
  41. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL = "prefs_upload_file_extension_map_url";
  42. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_URL = "prefs_upload_file_extension_url";
  43. private static final String AUTO_PREF__UPLOADER_BEHAVIOR = "prefs_uploader_behaviour";
  44. private static final String AUTO_PREF__GRID_COLUMNS = "grid_columns";
  45. public static final String AUTO_PREF__LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
  46. private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
  47. private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
  48. private static final String PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS = "instant_upload_path_use_subfolders";
  49. private static final String PREF__INSTANT_UPLOAD_ON_WIFI = "instant_upload_on_wifi";
  50. private static final String PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI = "instant_video_upload_on_wifi";
  51. private static final String PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS
  52. = "instant_video_upload_path_use_subfolders";
  53. private static final String PREF__LEGACY_CLEAN = "legacyClean";
  54. public static final String PREF__KEYS_MIGRATION = "keysMigration";
  55. private static final String PREF__FIX_STORAGE_PATH = "storagePathFix";
  56. private static final String PREF__KEYS_REINIT = "keysReinit";
  57. private static final String PREF__AUTO_UPLOAD_UPDATE_PATH = "autoUploadPathUpdate";
  58. private static final String PREF__PUSH_TOKEN = "pushToken";
  59. private static final String PREF__AUTO_UPLOAD_SPLIT_OUT = "autoUploadEntriesSplitOut";
  60. private static final String PREF__AUTO_UPLOAD_INIT = "autoUploadInit";
  61. private static final String PREF__FOLDER_SORT_ORDER = "folder_sort_order";
  62. private static final String PREF__FOLDER_LAYOUT = "folder_layout";
  63. public static final String PREF__LOCK_TIMESTAMP = "lock_timestamp";
  64. private PreferenceManager() {
  65. }
  66. public static void setKeysReInit(Context context) {
  67. saveBooleanPreference(context, PREF__KEYS_REINIT, true);
  68. }
  69. public static boolean getKeysReInit(Context context) {
  70. return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_REINIT, false);
  71. }
  72. public static void setPushToken(Context context, String pushToken) {
  73. saveStringPreferenceNow(context, PREF__PUSH_TOKEN, pushToken);
  74. }
  75. public static String getPushToken(Context context) {
  76. return getDefaultSharedPreferences(context).getString(PREF__PUSH_TOKEN, "");
  77. }
  78. public static boolean instantPictureUploadEnabled(Context context) {
  79. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOADING, false);
  80. }
  81. public static boolean instantVideoUploadEnabled(Context context) {
  82. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOADING, false);
  83. }
  84. public static boolean instantPictureUploadPathUseSubfolders(Context context) {
  85. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS, false);
  86. }
  87. public static boolean instantPictureUploadViaWiFiOnly(Context context) {
  88. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_ON_WIFI, false);
  89. }
  90. public static boolean instantVideoUploadPathUseSubfolders(Context context) {
  91. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS, false);
  92. }
  93. public static boolean instantVideoUploadViaWiFiOnly(Context context) {
  94. return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI, false);
  95. }
  96. public static boolean instantPictureUploadWhenChargingOnly(Context context) {
  97. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_charging", false);
  98. }
  99. public static boolean instantVideoUploadWhenChargingOnly(Context context) {
  100. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_charging",
  101. false);
  102. }
  103. public static boolean showHiddenFilesEnabled(Context context) {
  104. return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("show_hidden_files_pref", false);
  105. }
  106. /**
  107. * Gets the selected file extension position the user selected to do the last upload of a url file shared from other
  108. * app.
  109. *
  110. * @param context Caller {@link Context}, used to access to shared preferences manager.
  111. * @return selectedPos the selected file extension position.
  112. */
  113. public static int getUploadUrlFileExtensionUrlSelectedPos(Context context) {
  114. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
  115. }
  116. /**
  117. * Saves the selected file extension position the user selected to do the last upload of a url file shared from
  118. * other app.
  119. *
  120. * @param context Caller {@link Context}, used to access to shared preferences manager.
  121. * @param selectedPos the selected file extension position.
  122. */
  123. public static void setUploadUrlFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  124. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos);
  125. }
  126. /**
  127. * Gets the selected map file extension position the user selected to do the last upload of a url file shared
  128. * from other app.
  129. *
  130. * @param context Caller {@link Context}, used to access to shared preferences manager.
  131. * @return selectedPos the selected file extension position.
  132. */
  133. public static int getUploadMapFileExtensionUrlSelectedPos(Context context) {
  134. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
  135. }
  136. /**
  137. * Saves the selected map file extension position the user selected to do the last upload of a url file shared from
  138. * other app.
  139. *
  140. * @param context Caller {@link Context}, used to access to shared preferences manager.
  141. * @param selectedPos the selected file extension position.
  142. */
  143. public static void setUploadMapFileExtensionUrlSelectedPos(Context context, int selectedPos) {
  144. saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos);
  145. }
  146. /**
  147. * Gets the path where the user selected to do the last upload of a file shared from other app.
  148. *
  149. * @param context Caller {@link Context}, used to access to shared preferences manager.
  150. * @return path Absolute path to a folder, as previously stored by {@link #setLastUploadPath(Context, String)},
  151. * or empty String if never saved before.
  152. */
  153. public static String getLastUploadPath(Context context) {
  154. return getDefaultSharedPreferences(context).getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
  155. }
  156. /**
  157. * Saves the path where the user selected to do the last upload of a file shared from other app.
  158. *
  159. * @param context Caller {@link Context}, used to access to shared preferences manager.
  160. * @param path Absolute path to a folder.
  161. */
  162. public static void setLastUploadPath(Context context, String path) {
  163. saveStringPreference(context, AUTO_PREF__LAST_UPLOAD_PATH, path);
  164. }
  165. /**
  166. * Gets the last local path where the user selected to do an upload from.
  167. *
  168. * @param context Caller {@link Context}, used to access to shared preferences manager.
  169. * @return path Absolute path to a folder, as previously stored by
  170. * {@link #setUploadFromLocalLastPath(Context, String)}, or empty String if never saved before.
  171. */
  172. public static String getUploadFromLocalLastPath(Context context) {
  173. return getDefaultSharedPreferences(context).getString(AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, "");
  174. }
  175. /**
  176. * Saves the path where the user selected to do the last local upload of a file from.
  177. *
  178. * @param context Caller {@link Context}, used to access to shared preferences manager.
  179. * @param path Absolute path to a folder.
  180. */
  181. public static void setUploadFromLocalLastPath(Context context, String path) {
  182. saveStringPreference(context, AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, path);
  183. }
  184. /**
  185. * Gets the lock preference configured by the user.
  186. *
  187. * @param context Caller {@link Context}, used to access to shared preferences manager.
  188. * @return lock lock preference value.
  189. */
  190. public static String getLockPreference(Context context) {
  191. return getDefaultSharedPreferences(context).getString(Preferences.PREFERENCE_LOCK, "");
  192. }
  193. /**
  194. * Gets the lock via fingerprint preference configured by the user.
  195. *
  196. * @param context Caller {@link Context}, used to access to shared preferences manager.
  197. * @return useFingerprint is lock via fingerprint preference.
  198. */
  199. public static boolean isUseFingerprint(Context context) {
  200. return getDefaultSharedPreferences(context).getBoolean(Preferences.PREFERENCE_USE_FINGERPRINT, false);
  201. }
  202. /**
  203. * Get preferred folder display type.
  204. *
  205. * @param context Caller {@link Context}, used to access to preferences manager.
  206. * @param folder Folder
  207. * @return preference value, default is
  208. * {@link com.owncloud.android.ui.fragment.OCFileListFragment#FOLDER_LAYOUT_LIST}
  209. */
  210. public static String getFolderLayout(Context context, OCFile folder) {
  211. return getFolderPreference(context, PREF__FOLDER_LAYOUT, folder, FOLDER_LAYOUT_LIST);
  212. }
  213. /**
  214. * Set preferred folder display type.
  215. *
  216. * @param context Caller {@link Context}, used to access to shared preferences manager.
  217. * @param folder Folder
  218. * @param layout_name preference value
  219. */
  220. public static void setFolderLayout(Context context, OCFile folder, String layout_name) {
  221. setFolderPreference(context, PREF__FOLDER_LAYOUT, folder, layout_name);
  222. }
  223. /**
  224. * Get preferred folder sort order.
  225. *
  226. * @param context Caller {@link Context}, used to access to shared preferences manager.
  227. * @return sort order the sort order, default is {@link FileSortOrder#sort_a_to_z} (sort by name)
  228. */
  229. public static FileSortOrder getSortOrderByFolder(Context context, OCFile folder) {
  230. return FileSortOrder.sortOrders.get(getFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder,
  231. FileSortOrder.sort_a_to_z.name));
  232. }
  233. /**
  234. * Set preferred folder sort order.
  235. *
  236. * @param context Caller {@link Context}, used to access to shared preferences manager.
  237. * @param sortOrder the sort order
  238. */
  239. public static void setSortOrder(Context context, OCFile folder, FileSortOrder sortOrder) {
  240. setFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder, sortOrder.name);
  241. }
  242. public static FileSortOrder getSortOrderByType(Context context, FileSortOrder.Type type) {
  243. return getSortOrderByType(context, type, FileSortOrder.sort_a_to_z);
  244. }
  245. /**
  246. * Get preferred folder sort order.
  247. *
  248. * @param context Caller {@link Context}, used to access to shared preferences manager.
  249. * @return sort order the sort order, default is {@link FileSortOrder#sort_a_to_z} (sort by name)
  250. */
  251. public static FileSortOrder getSortOrderByType(Context context, FileSortOrder.Type type,
  252. FileSortOrder defaultOrder) {
  253. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  254. if (account == null) {
  255. return defaultOrder;
  256. }
  257. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  258. String value = dataProvider.getValue(account.name, PREF__FOLDER_SORT_ORDER + "_" + type);
  259. return value.isEmpty() ? defaultOrder : FileSortOrder.sortOrders.get(value);
  260. }
  261. /**
  262. * Set preferred folder sort order.
  263. *
  264. * @param context Caller {@link Context}, used to access to shared preferences manager.
  265. * @param sortOrder the sort order
  266. */
  267. public static void setSortOrder(Context context, FileSortOrder.Type type, FileSortOrder sortOrder) {
  268. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  269. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  270. dataProvider.storeOrUpdateKeyValue(account.name, PREF__FOLDER_SORT_ORDER + "_" + type, sortOrder.name);
  271. }
  272. /**
  273. * Get preference value for a folder.
  274. * If folder is not set itself, it finds an ancestor that is set.
  275. *
  276. * @param context Context object.
  277. * @param preferenceName Name of the preference to lookup.
  278. * @param folder Folder.
  279. * @param defaultValue Fallback value in case no ancestor is set.
  280. * @return Preference value
  281. */
  282. public static String getFolderPreference(Context context, String preferenceName, OCFile folder,
  283. String defaultValue) {
  284. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  285. if (account == null) {
  286. return defaultValue;
  287. }
  288. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  289. FileDataStorageManager storageManager = ((ComponentsGetter)context).getStorageManager();
  290. if (storageManager == null) {
  291. storageManager = new FileDataStorageManager(account, context.getContentResolver());
  292. }
  293. String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
  294. while (folder != null && value.isEmpty()) {
  295. folder = storageManager.getFileById(folder.getParentId());
  296. value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
  297. }
  298. return value.isEmpty() ? defaultValue : value;
  299. }
  300. /**
  301. * Get preference value for a view.
  302. *
  303. * @param context Context object.
  304. * @param preferenceName Name of the preference to lookup.
  305. * @param type view which view
  306. * @param defaultValue Fallback value in case nothing is set.
  307. * @return Preference value
  308. */
  309. public static String getTypePreference(Context context, String preferenceName, String type, String defaultValue) {
  310. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  311. if (account == null) {
  312. return defaultValue;
  313. }
  314. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  315. String value = dataProvider.getValue(account.name, preferenceName + "_" + type);
  316. return value.isEmpty() ? defaultValue : value;
  317. }
  318. /**
  319. * Set preference value for a folder.
  320. *
  321. * @param context Context object.
  322. * @param preferenceName Name of the preference to set.
  323. * @param folder Folder.
  324. * @param value Preference value to set.
  325. */
  326. public static void setFolderPreference(Context context, String preferenceName, OCFile folder, String value) {
  327. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  328. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  329. dataProvider.storeOrUpdateKeyValue(account.name, getKeyFromFolder(preferenceName, folder), value);
  330. }
  331. private static String getKeyFromFolder(String preferenceName, OCFile folder) {
  332. final String folderIdString = String.valueOf(folder != null ? folder.getFileId() :
  333. FileDataStorageManager.ROOT_PARENT_ID);
  334. return preferenceName + "_" + folderIdString;
  335. }
  336. public static boolean getAutoUploadInit(Context context) {
  337. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_INIT, false);
  338. }
  339. /**
  340. * Gets the legacy cleaning flag last set.
  341. *
  342. * @param context Caller {@link Context}, used to access to shared preferences manager.
  343. * @return ascending order the legacy cleaning flag, default is false
  344. */
  345. public static boolean getLegacyClean(Context context) {
  346. return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
  347. }
  348. public static boolean getKeysMigration(Context context) {
  349. return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_MIGRATION, false);
  350. }
  351. public static boolean getStoragePathFix(Context context) {
  352. return getDefaultSharedPreferences(context).getBoolean(PREF__FIX_STORAGE_PATH, false);
  353. }
  354. /**
  355. * Gets the auto upload paths flag last set.
  356. *
  357. * @param context Caller {@link Context}, used to access to shared preferences manager.
  358. * @return ascending order the legacy cleaning flag, default is false
  359. */
  360. public static boolean getAutoUploadPathsUpdate(Context context) {
  361. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_UPDATE_PATH, false);
  362. }
  363. /**
  364. * Gets the auto upload split out flag last set.
  365. *
  366. * @param context Caller {@link Context}, used to access to shared preferences manager.
  367. * @return ascending order the legacy cleaning flag, default is false
  368. */
  369. public static boolean getAutoUploadSplitEntries(Context context) {
  370. return getDefaultSharedPreferences(context).getBoolean(PREF__AUTO_UPLOAD_SPLIT_OUT, false);
  371. }
  372. /**
  373. * Saves the legacy cleaning flag which the user has set last.
  374. *
  375. * @param context Caller {@link Context}, used to access to shared preferences manager.
  376. * @param legacyClean flag if it is a legacy cleaning
  377. */
  378. public static void setLegacyClean(Context context, boolean legacyClean) {
  379. saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean);
  380. }
  381. public static void setKeysMigration(Context context, boolean keysMigration) {
  382. saveBooleanPreference(context, PREF__KEYS_MIGRATION, keysMigration);
  383. }
  384. public static void setStoragePathFix(Context context, boolean storagePathFix) {
  385. saveBooleanPreference(context, PREF__FIX_STORAGE_PATH, storagePathFix);
  386. }
  387. public static void setAutoUploadInit(Context context, boolean autoUploadInit) {
  388. saveBooleanPreference(context, PREF__AUTO_UPLOAD_INIT, autoUploadInit);
  389. }
  390. /**
  391. * Saves the legacy cleaning flag which the user has set last.
  392. *
  393. * @param context Caller {@link Context}, used to access to shared preferences manager.
  394. * @param pathUpdate flag if it is a auto upload path update
  395. */
  396. public static void setAutoUploadPathsUpdate(Context context, boolean pathUpdate) {
  397. saveBooleanPreference(context, PREF__AUTO_UPLOAD_UPDATE_PATH, pathUpdate);
  398. }
  399. /**
  400. * Saves the flag for split entries magic
  401. *
  402. * @param context Caller {@link Context}, used to access to shared preferences manager.
  403. * @param splitOut flag if it is a auto upload path update
  404. */
  405. public static void setAutoUploadSplitEntries(Context context, boolean splitOut) {
  406. saveBooleanPreference(context, PREF__AUTO_UPLOAD_SPLIT_OUT, splitOut);
  407. }
  408. /**
  409. * Gets the uploader behavior which the user has set last.
  410. *
  411. * @param context Caller {@link Context}, used to access to shared preferences manager.
  412. * @return uploader behavior the uploader behavior
  413. */
  414. public static int getUploaderBehaviour(Context context) {
  415. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOADER_BEHAVIOR, 1);
  416. }
  417. /**
  418. * Saves the uploader behavior which the user has set last.
  419. *
  420. * @param context Caller {@link Context}, used to access to shared preferences manager.
  421. * @param uploaderBehaviour the uploader behavior
  422. */
  423. public static void setUploaderBehaviour(Context context, int uploaderBehaviour) {
  424. saveIntPreference(context, AUTO_PREF__UPLOADER_BEHAVIOR, uploaderBehaviour);
  425. }
  426. /**
  427. * Gets the grid columns which the user has set last.
  428. *
  429. * @param context Caller {@link Context}, used to access to shared preferences manager.
  430. * @return grid columns grid columns
  431. */
  432. public static float getGridColumns(Context context) {
  433. return getDefaultSharedPreferences(context).getFloat(AUTO_PREF__GRID_COLUMNS, 4.0f);
  434. }
  435. /**
  436. * Saves the grid columns which the user has set last.
  437. *
  438. * @param context Caller {@link Context}, used to access to shared preferences manager.
  439. * @param gridColumns the uploader behavior
  440. */
  441. public static void setGridColumns(Context context, float gridColumns) {
  442. saveFloatPreference(context, AUTO_PREF__GRID_COLUMNS, gridColumns);
  443. }
  444. /**
  445. * Gets the last seen version code right before updating.
  446. *
  447. * @param context Caller {@link Context}, used to access to shared preferences manager.
  448. * @return grid columns grid columns
  449. */
  450. public static int getLastSeenVersionCode(Context context) {
  451. return getDefaultSharedPreferences(context).getInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, 0);
  452. }
  453. /**
  454. * Saves the version code as the last seen version code.
  455. *
  456. * @param context Caller {@link Context}, used to access to shared preferences manager.
  457. * @param versionCode the app's version code
  458. */
  459. public static void setLastSeenVersionCode(Context context, int versionCode) {
  460. saveIntPreference(context, AUTO_PREF__LAST_SEEN_VERSION_CODE, versionCode);
  461. }
  462. public static long getLockTimestamp(Context context) {
  463. return getDefaultSharedPreferences(context).getLong(PREF__LOCK_TIMESTAMP, 0);
  464. }
  465. public static void setLockTimestamp(Context context, long timestamp) {
  466. saveLongPreference(context, PREF__LOCK_TIMESTAMP, timestamp);
  467. }
  468. private static void saveBooleanPreference(Context context, String key, boolean value) {
  469. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  470. appPreferences.putBoolean(key, value).apply();
  471. }
  472. private static void saveStringPreference(Context context, String key, String value) {
  473. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  474. appPreferences.putString(key, value).apply();
  475. }
  476. private static void saveStringPreferenceNow(Context context, String key, String value) {
  477. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  478. appPreferences.putString(key, value);
  479. appPreferences.apply();
  480. }
  481. private static void saveIntPreference(Context context, String key, int value) {
  482. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  483. appPreferences.putInt(key, value).apply();
  484. }
  485. private static void saveFloatPreference(Context context, String key, float value) {
  486. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  487. appPreferences.putFloat(key, value).apply();
  488. }
  489. private static void saveLongPreference(Context context, String key, long value) {
  490. SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
  491. appPreferences.putLong(key, value).apply();
  492. }
  493. public static SharedPreferences getDefaultSharedPreferences(Context context) {
  494. return android.preference.PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
  495. }
  496. }