AppPreferencesImpl.java 23 KB

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