AppPreferencesImpl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author Chris Narkiewicz Chris Narkiewicz
  6. * Copyright (C) 2016 ownCloud Inc.
  7. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.nextcloud.client.preferences;
  22. import android.annotation.SuppressLint;
  23. import android.content.Context;
  24. import android.content.SharedPreferences;
  25. import com.nextcloud.client.account.CurrentAccountProvider;
  26. import com.nextcloud.client.account.User;
  27. import com.nextcloud.client.account.UserAccountManagerImpl;
  28. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  29. import com.owncloud.android.datamodel.FileDataStorageManager;
  30. import com.owncloud.android.datamodel.OCFile;
  31. import com.owncloud.android.ui.activity.PassCodeActivity;
  32. import com.owncloud.android.ui.activity.SettingsActivity;
  33. import com.owncloud.android.utils.FileSortOrder;
  34. import java.util.Set;
  35. import java.util.concurrent.CopyOnWriteArraySet;
  36. import androidx.annotation.Nullable;
  37. import static com.owncloud.android.ui.fragment.OCFileListFragment.FOLDER_LAYOUT_LIST;
  38. /**
  39. * Implementation of application-wide preferences using {@link SharedPreferences}.
  40. *
  41. * Users should not use this class directly. Please use {@link AppPreferences} interface
  42. * instead.
  43. */
  44. public final class AppPreferencesImpl implements AppPreferences {
  45. /**
  46. * Constant to access value of last path selected by the user to upload a file shared from other app.
  47. * Value handled by the app without direct access in the UI.
  48. */
  49. public static final String AUTO_PREF__LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
  50. public static final String STORAGE_PATH = "storage_path";
  51. public static final String STORAGE_PATH_VALID = "storage_path_valid";
  52. public static final String PREF__DARK_THEME = "dark_theme_mode";
  53. public static final float DEFAULT_GRID_COLUMN = 3f;
  54. private static final String AUTO_PREF__LAST_UPLOAD_PATH = "last_upload_path";
  55. private static final String AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH = "upload_from_local_last_path";
  56. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL = "prefs_upload_file_extension_map_url";
  57. private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_URL = "prefs_upload_file_extension_url";
  58. private static final String AUTO_PREF__UPLOADER_BEHAVIOR = "prefs_uploader_behaviour";
  59. private static final String AUTO_PREF__GRID_COLUMNS = "grid_columns";
  60. private static final String AUTO_PREF__SHOW_DETAILED_TIMESTAMP = "detailed_timestamp";
  61. private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
  62. private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
  63. private static final String PREF__SHOW_HIDDEN_FILES = "show_hidden_files_pref";
  64. private static final String PREF__LEGACY_CLEAN = "legacyClean";
  65. private static final String PREF__KEYS_MIGRATION = "keysMigration";
  66. private static final String PREF__FIX_STORAGE_PATH = "storagePathFix";
  67. private static final String PREF__KEYS_REINIT = "keysReinit";
  68. private static final String PREF__AUTO_UPLOAD_UPDATE_PATH = "autoUploadPathUpdate";
  69. private static final String PREF__PUSH_TOKEN = "pushToken";
  70. private static final String PREF__AUTO_UPLOAD_SPLIT_OUT = "autoUploadEntriesSplitOut";
  71. private static final String PREF__AUTO_UPLOAD_INIT = "autoUploadInit";
  72. private static final String PREF__FOLDER_SORT_ORDER = "folder_sort_order";
  73. private static final String PREF__FOLDER_LAYOUT = "folder_layout";
  74. private static final String PREF__LOCK_TIMESTAMP = "lock_timestamp";
  75. private static final String PREF__SHOW_MEDIA_SCAN_NOTIFICATIONS = "show_media_scan_notifications";
  76. private static final String PREF__LOCK = SettingsActivity.PREFERENCE_LOCK;
  77. private static final String PREF__SELECTED_ACCOUNT_NAME = "select_oc_account";
  78. private static final String PREF__MIGRATED_USER_ID = "migrated_user_id";
  79. private static final String PREF__PHOTO_SEARCH_TIMESTAMP = "photo_search_timestamp";
  80. private static final String PREF__POWER_CHECK_DISABLED = "power_check_disabled";
  81. private final Context context;
  82. private final SharedPreferences preferences;
  83. private final CurrentAccountProvider currentAccountProvider;
  84. private final ListenerRegistry listeners;
  85. /**
  86. * Adapter delegating raw {@link SharedPreferences.OnSharedPreferenceChangeListener} calls
  87. * with key-value pairs to respective {@link com.nextcloud.client.preferences.AppPreferences.Listener} method.
  88. */
  89. static class ListenerRegistry implements SharedPreferences.OnSharedPreferenceChangeListener {
  90. private final AppPreferences preferences;
  91. private final Set<Listener> listeners;
  92. ListenerRegistry(AppPreferences preferences) {
  93. this.preferences = preferences;
  94. this.listeners = new CopyOnWriteArraySet<>();
  95. }
  96. void add(@Nullable final Listener listener) {
  97. if (listener != null) {
  98. listeners.add(listener);
  99. }
  100. }
  101. void remove(@Nullable final Listener listener) {
  102. if (listener != null) {
  103. listeners.remove(listener);
  104. }
  105. }
  106. @Override
  107. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  108. if (PREF__DARK_THEME.equals(key)) {
  109. DarkMode mode = preferences.getDarkThemeMode();
  110. for(Listener l : listeners) {
  111. l.onDarkThemeModeChanged(mode);
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * This is a temporary workaround to access app preferences in places that cannot use
  118. * dependency injection yet. Use injected component via {@link AppPreferences} interface.
  119. *
  120. * WARNING: this creates new instance! it does not return app-wide singleton
  121. *
  122. * @param context Context used to create shared preferences
  123. * @return New instance of app preferences component
  124. */
  125. @Deprecated
  126. public static AppPreferences fromContext(Context context) {
  127. final CurrentAccountProvider currentAccountProvider = UserAccountManagerImpl.fromContext(context);
  128. final SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
  129. return new AppPreferencesImpl(context, prefs, currentAccountProvider);
  130. }
  131. AppPreferencesImpl(Context appContext, SharedPreferences preferences, CurrentAccountProvider currentAccountProvider) {
  132. this.context = appContext;
  133. this.preferences = preferences;
  134. this.currentAccountProvider = currentAccountProvider;
  135. this.listeners = new ListenerRegistry(this);
  136. this.preferences.registerOnSharedPreferenceChangeListener(listeners);
  137. }
  138. @Override
  139. public void addListener(@Nullable AppPreferences.Listener listener) {
  140. this.listeners.add(listener);
  141. }
  142. @Override
  143. public void removeListener(@Nullable AppPreferences.Listener listener) {
  144. this.listeners.remove(listener);
  145. }
  146. @Override
  147. public void setKeysReInitEnabled() {
  148. preferences.edit().putBoolean(PREF__KEYS_REINIT, true).apply();
  149. }
  150. @Override
  151. public boolean isKeysReInitEnabled() {
  152. return preferences.getBoolean(PREF__KEYS_REINIT, false);
  153. }
  154. @Override
  155. public void setPushToken(String pushToken) {
  156. preferences.edit().putString(PREF__PUSH_TOKEN, pushToken).apply();
  157. }
  158. @Override
  159. public String getPushToken() {
  160. return preferences.getString(PREF__PUSH_TOKEN, "");
  161. }
  162. @Override
  163. public boolean instantPictureUploadEnabled() {
  164. return preferences.getBoolean(PREF__INSTANT_UPLOADING, false);
  165. }
  166. @Override
  167. public boolean instantVideoUploadEnabled() {
  168. return preferences.getBoolean(PREF__INSTANT_VIDEO_UPLOADING, false);
  169. }
  170. @Override
  171. public boolean isShowHiddenFilesEnabled() {
  172. return preferences.getBoolean(PREF__SHOW_HIDDEN_FILES, false);
  173. }
  174. @Override
  175. public void setShowHiddenFilesEnabled(boolean enabled) {
  176. preferences.edit().putBoolean(PREF__SHOW_HIDDEN_FILES, enabled).apply();
  177. }
  178. @Override
  179. public int getUploadUrlFileExtensionUrlSelectedPos() {
  180. return preferences.getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
  181. }
  182. @Override
  183. public void setUploadUrlFileExtensionUrlSelectedPos(int selectedPos) {
  184. preferences.edit().putInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos).apply();
  185. }
  186. @Override
  187. public int getUploadMapFileExtensionUrlSelectedPos() {
  188. return preferences.getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
  189. }
  190. @Override
  191. public void setUploadMapFileExtensionUrlSelectedPos(int selectedPos) {
  192. preferences.edit().putInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos).apply();
  193. }
  194. @Override
  195. public String getUploadFromLocalLastPath() {
  196. return preferences.getString(AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, "");
  197. }
  198. @Override
  199. public void setUploadFromLocalLastPath(String path) {
  200. preferences.edit().putString(AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, path).apply();
  201. }
  202. @Override
  203. public String getLastUploadPath() {
  204. return preferences.getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
  205. }
  206. @Override
  207. public void setLastUploadPath(String path) {
  208. preferences.edit().putString(AUTO_PREF__LAST_UPLOAD_PATH, path).apply();
  209. }
  210. @Override
  211. public String getLockPreference() {
  212. return preferences.getString(PREF__LOCK, SettingsActivity.LOCK_NONE);
  213. }
  214. @Override
  215. public void setLockPreference(String lockPreference) {
  216. preferences.edit().putString(PREF__LOCK, lockPreference).apply();
  217. }
  218. @Override
  219. public void setPassCode(String d1, String d2, String d3, String d4) {
  220. preferences
  221. .edit()
  222. .putString(PassCodeActivity.PREFERENCE_PASSCODE_D1, d1)
  223. .putString(PassCodeActivity.PREFERENCE_PASSCODE_D2, d2)
  224. .putString(PassCodeActivity.PREFERENCE_PASSCODE_D3, d3)
  225. .putString(PassCodeActivity.PREFERENCE_PASSCODE_D4, d4)
  226. .apply();
  227. }
  228. @Override
  229. public String[] getPassCode() {
  230. return new String[] {
  231. preferences.getString(PassCodeActivity.PREFERENCE_PASSCODE_D1, null),
  232. preferences.getString(PassCodeActivity.PREFERENCE_PASSCODE_D2, null),
  233. preferences.getString(PassCodeActivity.PREFERENCE_PASSCODE_D3, null),
  234. preferences.getString(PassCodeActivity.PREFERENCE_PASSCODE_D4, null),
  235. };
  236. }
  237. @Override
  238. public boolean isFingerprintUnlockEnabled() {
  239. return preferences.getBoolean(SettingsActivity.PREFERENCE_USE_FINGERPRINT, false);
  240. }
  241. @Override
  242. public String getFolderLayout(OCFile folder) {
  243. return getFolderPreference(context,
  244. currentAccountProvider.getUser(),
  245. PREF__FOLDER_LAYOUT,
  246. folder,
  247. FOLDER_LAYOUT_LIST);
  248. }
  249. @Override
  250. public void setFolderLayout(@Nullable OCFile folder, String layoutName) {
  251. setFolderPreference(context,
  252. currentAccountProvider.getUser(),
  253. PREF__FOLDER_LAYOUT,
  254. folder,
  255. layoutName);
  256. }
  257. @Override
  258. public FileSortOrder getSortOrderByFolder(OCFile folder) {
  259. return FileSortOrder.sortOrders.get(getFolderPreference(context,
  260. currentAccountProvider.getUser(),
  261. PREF__FOLDER_SORT_ORDER,
  262. folder,
  263. FileSortOrder.sort_a_to_z.name));
  264. }
  265. @Override
  266. public void setSortOrder(@Nullable OCFile folder, FileSortOrder sortOrder) {
  267. setFolderPreference(context,
  268. currentAccountProvider.getUser(),
  269. PREF__FOLDER_SORT_ORDER,
  270. folder,
  271. sortOrder.name);
  272. }
  273. @Override
  274. public FileSortOrder getSortOrderByType(FileSortOrder.Type type) {
  275. return getSortOrderByType(type, FileSortOrder.sort_a_to_z);
  276. }
  277. @Override
  278. public FileSortOrder getSortOrderByType(FileSortOrder.Type type, FileSortOrder defaultOrder) {
  279. User user = currentAccountProvider.getUser();
  280. if (user.isAnonymous()) {
  281. return defaultOrder;
  282. }
  283. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  284. String value = dataProvider.getValue(user.getAccountName(), PREF__FOLDER_SORT_ORDER + "_" + type);
  285. return value.isEmpty() ? defaultOrder : FileSortOrder.sortOrders.get(value);
  286. }
  287. @Override
  288. public void setSortOrder(FileSortOrder.Type type, FileSortOrder sortOrder) {
  289. User user = currentAccountProvider.getUser();
  290. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  291. dataProvider.storeOrUpdateKeyValue(user.getAccountName(), PREF__FOLDER_SORT_ORDER + "_" + type, sortOrder.name);
  292. }
  293. @Override
  294. public boolean isLegacyClean() {
  295. return preferences.getBoolean(PREF__LEGACY_CLEAN, false);
  296. }
  297. @Override
  298. public void setLegacyClean(boolean isLegacyClean) {
  299. preferences.edit().putBoolean(PREF__LEGACY_CLEAN, isLegacyClean).apply();
  300. }
  301. @Override
  302. public boolean isKeysMigrationEnabled() {
  303. return preferences.getBoolean(PREF__KEYS_MIGRATION, false);
  304. }
  305. @Override
  306. public void setKeysMigrationEnabled(boolean keysMigration) {
  307. preferences.edit().putBoolean(PREF__KEYS_MIGRATION, keysMigration).apply();
  308. }
  309. @Override
  310. public boolean isStoragePathFixEnabled() {
  311. return preferences.getBoolean(PREF__FIX_STORAGE_PATH, false);
  312. }
  313. @Override
  314. public void setStoragePathFixEnabled(boolean storagePathFixEnabled) {
  315. preferences.edit().putBoolean(PREF__FIX_STORAGE_PATH, storagePathFixEnabled).apply();
  316. }
  317. @Override
  318. public boolean isAutoUploadPathsUpdateEnabled() {
  319. return preferences.getBoolean(PREF__AUTO_UPLOAD_UPDATE_PATH, false);
  320. }
  321. @Override
  322. public void setAutoUploadPathsUpdateEnabled(boolean pathUpdate) {
  323. preferences.edit().putBoolean(PREF__AUTO_UPLOAD_UPDATE_PATH, pathUpdate).apply();
  324. }
  325. @Override
  326. public boolean isAutoUploadSplitEntriesEnabled() {
  327. return preferences.getBoolean(PREF__AUTO_UPLOAD_SPLIT_OUT, false);
  328. }
  329. @Override
  330. public void setAutoUploadSplitEntriesEnabled(boolean splitOut) {
  331. preferences.edit().putBoolean(PREF__AUTO_UPLOAD_SPLIT_OUT, splitOut).apply();
  332. }
  333. @Override
  334. public boolean isAutoUploadInitialized() {
  335. return preferences.getBoolean(PREF__AUTO_UPLOAD_INIT, false);
  336. }
  337. @Override
  338. public void setAutoUploadInit(boolean autoUploadInit) {
  339. preferences.edit().putBoolean(PREF__AUTO_UPLOAD_INIT, autoUploadInit).apply();
  340. }
  341. @Override
  342. public int getUploaderBehaviour() {
  343. return preferences.getInt(AUTO_PREF__UPLOADER_BEHAVIOR, 1);
  344. }
  345. @Override
  346. public void setDarkThemeMode(DarkMode mode) {
  347. preferences.edit().putString(PREF__DARK_THEME, mode.name()).apply();
  348. }
  349. @Override
  350. public DarkMode getDarkThemeMode() {
  351. try {
  352. return DarkMode.valueOf(preferences.getString(PREF__DARK_THEME, DarkMode.SYSTEM.name()));
  353. } catch (ClassCastException e) {
  354. preferences.edit().putString(PREF__DARK_THEME, DarkMode.SYSTEM.name()).apply();
  355. return DarkMode.SYSTEM;
  356. }
  357. }
  358. @Override
  359. public void setUploaderBehaviour(int uploaderBehaviour) {
  360. preferences.edit().putInt(AUTO_PREF__UPLOADER_BEHAVIOR, uploaderBehaviour).apply();
  361. }
  362. /**
  363. * Gets the grid columns which the user has set last.
  364. *
  365. * @return grid columns grid columns
  366. */
  367. @Override
  368. public float getGridColumns() {
  369. float columns = preferences.getFloat(AUTO_PREF__GRID_COLUMNS, DEFAULT_GRID_COLUMN);
  370. if (columns < 0) {
  371. return DEFAULT_GRID_COLUMN;
  372. } else {
  373. return columns;
  374. }
  375. }
  376. /**
  377. * Saves the grid columns which the user has set last.
  378. *
  379. * @param gridColumns the uploader behavior
  380. */
  381. @Override
  382. public void setGridColumns(float gridColumns) {
  383. preferences.edit().putFloat(AUTO_PREF__GRID_COLUMNS, gridColumns).apply();
  384. }
  385. @Override
  386. public int getLastSeenVersionCode() {
  387. return preferences.getInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, 0);
  388. }
  389. @Override
  390. public void setLastSeenVersionCode(int versionCode) {
  391. preferences.edit().putInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, versionCode).apply();
  392. }
  393. @Override
  394. public long getLockTimestamp() {
  395. return preferences.getLong(PREF__LOCK_TIMESTAMP, 0);
  396. }
  397. @Override
  398. public void setLockTimestamp(long timestamp) {
  399. preferences.edit().putLong(PREF__LOCK_TIMESTAMP, timestamp).apply();
  400. }
  401. @Override
  402. public boolean isShowDetailedTimestampEnabled() {
  403. return preferences.getBoolean(AUTO_PREF__SHOW_DETAILED_TIMESTAMP, false);
  404. }
  405. @Override
  406. public void setShowDetailedTimestampEnabled(boolean showDetailedTimestamp) {
  407. preferences.edit().putBoolean(AUTO_PREF__SHOW_DETAILED_TIMESTAMP, showDetailedTimestamp).apply();
  408. }
  409. @Override
  410. public boolean isShowMediaScanNotifications() {
  411. return preferences.getBoolean(PREF__SHOW_MEDIA_SCAN_NOTIFICATIONS, true);
  412. }
  413. @Override
  414. public void setShowMediaScanNotifications(boolean value) {
  415. preferences.edit().putBoolean(PREF__SHOW_MEDIA_SCAN_NOTIFICATIONS, value).apply();
  416. }
  417. @Override
  418. public void removeLegacyPreferences() {
  419. preferences.edit()
  420. .remove("instant_uploading")
  421. .remove("instant_video_uploading")
  422. .remove("instant_upload_path")
  423. .remove("instant_upload_path_use_subfolders")
  424. .remove("instant_upload_on_wifi")
  425. .remove("instant_upload_on_charging")
  426. .remove("instant_video_upload_path")
  427. .remove("instant_video_upload_path_use_subfolders")
  428. .remove("instant_video_upload_on_wifi")
  429. .remove("instant_video_uploading")
  430. .remove("instant_video_upload_on_charging")
  431. .remove("prefs_instant_behaviour")
  432. .apply();
  433. }
  434. @Override
  435. public void clear() {
  436. preferences.edit().clear().apply();
  437. }
  438. @Override
  439. public String getStoragePath(String defaultPath) {
  440. return preferences.getString(STORAGE_PATH, defaultPath);
  441. }
  442. @SuppressLint("ApplySharedPref")
  443. @Override
  444. public void setStoragePath(String path) {
  445. preferences.edit().putString(STORAGE_PATH, path).commit(); // commit synchronously
  446. }
  447. @SuppressLint("ApplySharedPref")
  448. @Override
  449. public void setStoragePathValid() {
  450. preferences.edit().putBoolean(STORAGE_PATH_VALID, true).commit();
  451. }
  452. @Override
  453. public boolean isStoragePathValid() {
  454. return preferences.getBoolean(STORAGE_PATH_VALID, false);
  455. }
  456. /**
  457. * Removes keys migration key from shared preferences.
  458. */
  459. @SuppressLint("ApplySharedPref")
  460. @Override
  461. public void removeKeysMigrationPreference() {
  462. preferences.edit().remove(AppPreferencesImpl.PREF__KEYS_MIGRATION).commit(); // commit synchronously
  463. }
  464. @Override
  465. public String getCurrentAccountName() {
  466. return preferences.getString(PREF__SELECTED_ACCOUNT_NAME, null);
  467. }
  468. @Override
  469. public void setCurrentAccountName(String accountName) {
  470. preferences.edit().putString(PREF__SELECTED_ACCOUNT_NAME, accountName).apply();
  471. }
  472. @Override
  473. public boolean isUserIdMigrated() {
  474. return preferences.getBoolean(PREF__MIGRATED_USER_ID, false);
  475. }
  476. @Override
  477. public void setMigratedUserId(boolean value) {
  478. preferences.edit().putBoolean(PREF__MIGRATED_USER_ID, value).apply();
  479. }
  480. @Override
  481. public void setPhotoSearchTimestamp(long timestamp) {
  482. preferences.edit().putLong(PREF__PHOTO_SEARCH_TIMESTAMP, timestamp).apply();
  483. }
  484. @Override
  485. public long getPhotoSearchTimestamp() {
  486. return preferences.getLong(PREF__PHOTO_SEARCH_TIMESTAMP, 0);
  487. }
  488. /**
  489. * Get preference value for a folder.
  490. * If folder is not set itself, it finds an ancestor that is set.
  491. *
  492. * @param context Context object.
  493. * @param preferenceName Name of the preference to lookup.
  494. * @param folder Folder.
  495. * @param defaultValue Fallback value in case no ancestor is set.
  496. * @return Preference value
  497. */
  498. private static String getFolderPreference(final Context context,
  499. final User user,
  500. final String preferenceName,
  501. final OCFile folder,
  502. final String defaultValue) {
  503. if (user.isAnonymous()) {
  504. return defaultValue;
  505. }
  506. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  507. FileDataStorageManager storageManager = new FileDataStorageManager(user.toPlatformAccount(), context.getContentResolver());
  508. String value = dataProvider.getValue(user.getAccountName(), getKeyFromFolder(preferenceName, folder));
  509. OCFile prefFolder = folder;
  510. while (prefFolder != null && value.isEmpty()) {
  511. prefFolder = storageManager.getFileById(prefFolder.getParentId());
  512. value = dataProvider.getValue(user.getAccountName(), getKeyFromFolder(preferenceName, prefFolder));
  513. }
  514. return value.isEmpty() ? defaultValue : value;
  515. }
  516. /**
  517. * Set preference value for a folder.
  518. *
  519. * @param context Context object.
  520. * @param preferenceName Name of the preference to set.
  521. * @param folder Folder.
  522. * @param value Preference value to set.
  523. */
  524. private static void setFolderPreference(final Context context,
  525. final User user,
  526. final String preferenceName,
  527. @Nullable final OCFile folder,
  528. final String value) {
  529. ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
  530. dataProvider.storeOrUpdateKeyValue(user.getAccountName(), getKeyFromFolder(preferenceName, folder), value);
  531. }
  532. private static String getKeyFromFolder(String preferenceName, @Nullable OCFile folder) {
  533. final String folderIdString = String.valueOf(folder != null ? folder.getFileId() :
  534. FileDataStorageManager.ROOT_PARENT_ID);
  535. return preferenceName + "_" + folderIdString;
  536. }
  537. @Override
  538. public boolean isPowerCheckDisabled() {
  539. return preferences.getBoolean(PREF__POWER_CHECK_DISABLED, false);
  540. }
  541. @Override
  542. public void setPowerCheckDisabled(boolean value) {
  543. preferences.edit().putBoolean(PREF__POWER_CHECK_DISABLED, value).apply();
  544. }
  545. }