PreferencesHelper.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package eu.kanade.mangafeed.data.preference;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.os.Environment;
  5. import android.preference.PreferenceManager;
  6. import com.f2prateek.rx.preferences.Preference;
  7. import com.f2prateek.rx.preferences.RxSharedPreferences;
  8. import java.io.File;
  9. import eu.kanade.mangafeed.R;
  10. import eu.kanade.mangafeed.data.mangasync.base.BaseMangaSync;
  11. import eu.kanade.mangafeed.data.source.base.Source;
  12. import rx.Observable;
  13. public class PreferencesHelper {
  14. private Context context;
  15. private SharedPreferences prefs;
  16. private RxSharedPreferences rxPrefs;
  17. private static final String SOURCE_ACCOUNT_USERNAME = "pref_source_username_";
  18. private static final String SOURCE_ACCOUNT_PASSWORD = "pref_source_password_";
  19. private static final String MANGASYNC_ACCOUNT_USERNAME = "pref_mangasync_username_";
  20. private static final String MANGASYNC_ACCOUNT_PASSWORD = "pref_mangasync_password_";
  21. private File defaultDownloadsDir;
  22. public PreferencesHelper(Context context) {
  23. this.context = context;
  24. PreferenceManager.setDefaultValues(context, R.xml.pref_reader, false);
  25. prefs = PreferenceManager.getDefaultSharedPreferences(context);
  26. rxPrefs = RxSharedPreferences.create(prefs);
  27. defaultDownloadsDir = new File(Environment.getExternalStorageDirectory() +
  28. File.separator + context.getString(R.string.app_name), "downloads");
  29. // Create default directory
  30. if (getDownloadsDirectory().equals(defaultDownloadsDir.getAbsolutePath()))
  31. defaultDownloadsDir.mkdirs();
  32. }
  33. private String getKey(int keyResource) {
  34. return context.getString(keyResource);
  35. }
  36. public void clear() {
  37. prefs.edit().clear().apply();
  38. }
  39. public int cacheSize() {
  40. return prefs.getInt(getKey(R.string.pref_chapter_cache_size_key), 75);
  41. }
  42. public Preference<Boolean> lockOrientation() {
  43. return rxPrefs.getBoolean(getKey(R.string.pref_lock_orientation_key), true);
  44. }
  45. public Preference<Boolean> enableTransitions() {
  46. return rxPrefs.getBoolean(getKey(R.string.pref_enable_transitions_key), true);
  47. }
  48. public Preference<Boolean> showPageNumber() {
  49. return rxPrefs.getBoolean(getKey(R.string.pref_show_page_number_key), true);
  50. }
  51. public Preference<Boolean> hideStatusBar() {
  52. return rxPrefs.getBoolean(getKey(R.string.pref_hide_status_bar_key), true);
  53. }
  54. public Preference<Boolean> keepScreenOn() {
  55. return rxPrefs.getBoolean(getKey(R.string.pref_keep_screen_on_key), true);
  56. }
  57. public Preference<Boolean> customBrightness() {
  58. return rxPrefs.getBoolean(getKey(R.string.pref_custom_brightness_key), false);
  59. }
  60. public Preference<Float> customBrightnessValue() {
  61. return rxPrefs.getFloat(getKey(R.string.pref_custom_brightness_value_key), 0F);
  62. }
  63. public int getDefaultViewer() {
  64. return Integer.parseInt(prefs.getString(getKey(R.string.pref_default_viewer_key), "1"));
  65. }
  66. public String getSourceUsername(Source source) {
  67. return prefs.getString(SOURCE_ACCOUNT_USERNAME + source.getSourceId(), "");
  68. }
  69. public String getSourcePassword(Source source) {
  70. return prefs.getString(SOURCE_ACCOUNT_PASSWORD + source.getSourceId(), "");
  71. }
  72. public void setSourceCredentials(Source source, String username, String password) {
  73. prefs.edit()
  74. .putString(SOURCE_ACCOUNT_USERNAME + source.getSourceId(), username)
  75. .putString(SOURCE_ACCOUNT_PASSWORD + source.getSourceId(), password)
  76. .apply();
  77. }
  78. public String getMangaSyncUsername(BaseMangaSync sync) {
  79. return prefs.getString(MANGASYNC_ACCOUNT_USERNAME + sync.getId(), "");
  80. }
  81. public String getMangaSyncPassword(BaseMangaSync sync) {
  82. return prefs.getString(MANGASYNC_ACCOUNT_PASSWORD + sync.getId(), "");
  83. }
  84. public void setMangaSyncCredentials(BaseMangaSync sync, String username, String password) {
  85. prefs.edit()
  86. .putString(MANGASYNC_ACCOUNT_USERNAME + sync.getId(), username)
  87. .putString(MANGASYNC_ACCOUNT_PASSWORD + sync.getId(), password)
  88. .apply();
  89. }
  90. public String getDownloadsDirectory() {
  91. return prefs.getString(getKey(R.string.pref_download_directory_key),
  92. defaultDownloadsDir.getAbsolutePath());
  93. }
  94. public void setDownloadsDirectory(String path) {
  95. prefs.edit().putString(getKey(R.string.pref_download_directory_key), path).apply();
  96. }
  97. public int getDownloadThreads() {
  98. return prefs.getInt(getKey(R.string.pref_download_slots_key), 1);
  99. }
  100. public Observable<Integer> getDownloadTheadsObservable() {
  101. return rxPrefs.getInteger(getKey(R.string.pref_download_slots_key), 1).asObservable();
  102. }
  103. }