From 33778e30ac156e22c9d748f905ddb26c65eadd78 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Mon, 22 Jun 2015 19:59:57 +0200 Subject: Don't cache shared preferences anymore. Some refactoring. --- .../core/preferences/UserPreferences.java | 547 ++++++--------------- .../danoeh/antennapod/core/storage/DBReader.java | 3 +- .../de/danoeh/antennapod/core/storage/DBTasks.java | 5 +- 3 files changed, 142 insertions(+), 413 deletions(-) (limited to 'core/src/main/java/de/danoeh/antennapod') diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index 6ef91a22a..520bba72b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -29,17 +29,16 @@ import de.danoeh.antennapod.core.receiver.FeedUpdateReceiver; /** * Provides access to preferences set by the user in the settings screen. A * private instance of this class must first be instantiated via - * createInstance() or otherwise every public method will throw an Exception + * init() or otherwise every public method will throw an Exception * when called. */ -public class UserPreferences implements - SharedPreferences.OnSharedPreferenceChangeListener { +public class UserPreferences { public static final String IMPORT_DIR = "import/"; private static final String TAG = "UserPreferences"; - // User Infercasce + // User Interface public static final String PREF_THEME = "prefTheme"; public static final String PREF_HIDDEN_DRAWER_ITEMS = "prefHiddenDrawerItems"; public static final String PREF_EXPANDED_NOTIFICATION = "prefExpandNotify"; @@ -55,7 +54,7 @@ public class UserPreferences implements public static final String PREF_FOLLOW_QUEUE = "prefFollowQueue"; public static final String PREF_AUTO_DELETE = "prefAutoDelete"; public static final String PREF_SMART_MARK_AS_PLAYED_SECS = "prefSmartMarkAsPlayedSecs"; - private static final String PREF_PLAYBACK_SPEED_ARRAY = "prefPlaybackSpeedArray"; + public static final String PREF_PLAYBACK_SPEED_ARRAY = "prefPlaybackSpeedArray"; public static final String PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS = "prefPauseForFocusLoss"; public static final String PREF_RESUME_AFTER_CALL = "prefResumeAfterCall"; @@ -82,188 +81,26 @@ public class UserPreferences implements private static final String PREF_REWIND_SECS = "prefRewindSecs"; public static final String PREF_QUEUE_LOCKED = "prefQueueLocked"; - // TODO: Make this value configurable - private static final float PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD_DEFAULT = 0.8f; - + // Constants private static int EPISODE_CACHE_SIZE_UNLIMITED = -1; - private static UserPreferences instance; - private final Context context; - - // User Interface - private int theme; - private List hiddenDrawerItems; - private int notifyPriority; - private boolean persistNotify; - private boolean showDownloadReport; - - // Queue - private boolean enqueueAtFront; - - // Playback - private boolean pauseOnHeadsetDisconnect; - private boolean unpauseOnHeadsetReconnect; - private boolean followQueue; - private boolean autoDelete; - private int smartMarkAsPlayedSecs; - private String[] playbackSpeedArray; - private boolean pauseForFocusLoss; - private boolean resumeAfterCall; - - // Network - private long updateInterval; - private boolean allowMobileUpdate; - private int parallelDownloads; - private int episodeCacheSize; - private boolean enableAutodownload; - private boolean enableAutodownloadOnBattery; - private boolean enableAutodownloadWifiFilter; - private String[] autodownloadSelectedNetworks; - - // Services - private boolean autoFlattr; - private float autoFlattrPlayedDurationThreshold; - - // Settings somewhere in the GUI - private String playbackSpeed; - private int fastForwardSecs; - private int rewindSecs; - private boolean queueLocked; - - - private UserPreferences(Context context) { - this.context = context; - loadPreferences(); - } + private static Context context; + private static SharedPreferences prefs; /** * Sets up the UserPreferences class. * * @throws IllegalArgumentException if context is null */ - public static void createInstance(Context context) { + public static void init(Context context) { Log.d(TAG, "Creating new instance of UserPreferences"); Validate.notNull(context); - instance = new UserPreferences(context); + UserPreferences.context = context; + UserPreferences.prefs = PreferenceManager.getDefaultSharedPreferences(context); createImportDirectory(); createNoMediaFile(); - PreferenceManager.getDefaultSharedPreferences(context) - .registerOnSharedPreferenceChangeListener(instance); - - } - - private void loadPreferences() { - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); - - // User Interface - theme = readThemeValue(sp.getString(PREF_THEME, "0")); - if (sp.getBoolean(PREF_EXPANDED_NOTIFICATION, false)) { - notifyPriority = NotificationCompat.PRIORITY_MAX; - } else { - notifyPriority = NotificationCompat.PRIORITY_DEFAULT; - } - hiddenDrawerItems = Arrays.asList(StringUtils.split(sp.getString(PREF_HIDDEN_DRAWER_ITEMS, ""), ',')); - persistNotify = sp.getBoolean(PREF_PERSISTENT_NOTIFICATION, false); - showDownloadReport = sp.getBoolean(PREF_SHOW_DOWNLOAD_REPORT, true); - - // Queue - enqueueAtFront = sp.getBoolean(PREF_QUEUE_ADD_TO_FRONT, false); - - // Playback - pauseOnHeadsetDisconnect = sp.getBoolean(PREF_PAUSE_ON_HEADSET_DISCONNECT, true); - unpauseOnHeadsetReconnect = sp.getBoolean(PREF_UNPAUSE_ON_HEADSET_RECONNECT, true); - followQueue = sp.getBoolean(PREF_FOLLOW_QUEUE, false); - autoDelete = sp.getBoolean(PREF_AUTO_DELETE, false); - smartMarkAsPlayedSecs = Integer.valueOf(sp.getString(PREF_SMART_MARK_AS_PLAYED_SECS, "30")); - playbackSpeedArray = readPlaybackSpeedArray(sp.getString( - PREF_PLAYBACK_SPEED_ARRAY, null)); - pauseForFocusLoss = sp.getBoolean(PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS, false); - - // Network - updateInterval = readUpdateInterval(sp.getString(PREF_UPDATE_INTERVAL, "0")); - allowMobileUpdate = sp.getBoolean(PREF_MOBILE_UPDATE, false); - parallelDownloads = Integer.valueOf(sp.getString(PREF_PARALLEL_DOWNLOADS, "6")); - EPISODE_CACHE_SIZE_UNLIMITED = context.getResources().getInteger( - R.integer.episode_cache_size_unlimited); - episodeCacheSize = readEpisodeCacheSizeInternal(sp.getString(PREF_EPISODE_CACHE_SIZE, "20")); - enableAutodownload = sp.getBoolean(PREF_ENABLE_AUTODL, false); - enableAutodownloadOnBattery = sp.getBoolean(PREF_ENABLE_AUTODL_ON_BATTERY, true); - enableAutodownloadWifiFilter = sp.getBoolean(PREF_ENABLE_AUTODL_WIFI_FILTER, false); - autodownloadSelectedNetworks = StringUtils.split( - sp.getString(PREF_AUTODL_SELECTED_NETWORKS, ""), ','); - - // Services - autoFlattr = sp.getBoolean(PREF_AUTO_FLATTR, false); - autoFlattrPlayedDurationThreshold = sp.getFloat(PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD, - PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD_DEFAULT); - - // MediaPlayer - playbackSpeed = sp.getString(PREF_PLAYBACK_SPEED, "1.0"); - fastForwardSecs = sp.getInt(PREF_FAST_FORWARD_SECS, 30); - rewindSecs = sp.getInt(PREF_REWIND_SECS, 30); - queueLocked = sp.getBoolean(PREF_QUEUE_LOCKED, false); - } - - private int readThemeValue(String valueFromPrefs) { - switch (Integer.parseInt(valueFromPrefs)) { - case 0: - return R.style.Theme_AntennaPod_Light; - case 1: - return R.style.Theme_AntennaPod_Dark; - default: - return R.style.Theme_AntennaPod_Light; - } - } - - private long readUpdateInterval(String valueFromPrefs) { - int hours = Integer.parseInt(valueFromPrefs); - return TimeUnit.HOURS.toMillis(hours); - } - - private int readEpisodeCacheSizeInternal(String valueFromPrefs) { - if (valueFromPrefs.equals(context - .getString(R.string.pref_episode_cache_unlimited))) { - return EPISODE_CACHE_SIZE_UNLIMITED; - } else { - return Integer.valueOf(valueFromPrefs); - } - } - - private String[] readPlaybackSpeedArray(String valueFromPrefs) { - String[] selectedSpeeds = null; - // If this preference hasn't been set yet, return the default options - if (valueFromPrefs == null) { - String[] allSpeeds = context.getResources().getStringArray( - R.array.playback_speed_values); - List speedList = new LinkedList(); - for (String speedStr : allSpeeds) { - float speed = Float.parseFloat(speedStr); - if (speed < 2.0001 && speed * 10 % 1 == 0) { - speedList.add(speedStr); - } - } - selectedSpeeds = speedList.toArray(new String[speedList.size()]); - } else { - try { - JSONArray jsonArray = new JSONArray(valueFromPrefs); - selectedSpeeds = new String[jsonArray.length()]; - for (int i = 0; i < jsonArray.length(); i++) { - selectedSpeeds[i] = jsonArray.getString(i); - } - } catch (JSONException e) { - Log.e(TAG, "Got JSON error when trying to get speeds from JSONArray"); - e.printStackTrace(); - } - } - return selectedSpeeds; - } - - private static void instanceAvailable() { - if (instance == null) { - throw new IllegalStateException("UserPreferences was used before being set up"); - } } /** @@ -272,8 +109,7 @@ public class UserPreferences implements * @return R.style.Theme_AntennaPod_Light or R.style.Theme_AntennaPod_Dark */ public static int getTheme() { - instanceAvailable(); - return instance.theme; + return readThemeValue(prefs.getString(PREF_THEME, "0")); } public static int getNoTitleTheme() { @@ -286,8 +122,8 @@ public class UserPreferences implements } public static List getHiddenDrawerItems() { - instanceAvailable(); - return new ArrayList(instance.hiddenDrawerItems); + String hiddenItems = prefs.getString(PREF_HIDDEN_DRAWER_ITEMS, ""); + return new ArrayList(Arrays.asList(StringUtils.split(hiddenItems, ','))); } /** @@ -296,8 +132,11 @@ public class UserPreferences implements * @return NotificationCompat.PRIORITY_MAX or NotificationCompat.PRIORITY_DEFAULT */ public static int getNotifyPriority() { - instanceAvailable(); - return instance.notifyPriority; + if (prefs.getBoolean(PREF_EXPANDED_NOTIFICATION, false)) { + return NotificationCompat.PRIORITY_MAX; + } else { + return NotificationCompat.PRIORITY_DEFAULT; + } } /** @@ -306,8 +145,7 @@ public class UserPreferences implements * @return {@code true} if notifications are persistent, {@code false} otherwise */ public static boolean isPersistNotify() { - instanceAvailable(); - return instance.persistNotify; + return prefs.getBoolean(PREF_PERSISTENT_NOTIFICATION, false); } /** @@ -316,8 +154,7 @@ public class UserPreferences implements * @return {@code true} if download reports are shown, {@code false} otherwise */ public static boolean showDownloadReport() { - instanceAvailable(); - return instance.showDownloadReport; + return prefs.getBoolean(PREF_SHOW_DOWNLOAD_REPORT, true); } /** @@ -326,73 +163,61 @@ public class UserPreferences implements * @return {@code true} if new queue elements are added to the front; {@code false} otherwise */ public static boolean enqueueAtFront() { - instanceAvailable(); - return instance.enqueueAtFront; + return prefs.getBoolean(PREF_QUEUE_ADD_TO_FRONT, false); } public static boolean isPauseOnHeadsetDisconnect() { - instanceAvailable(); - return instance.pauseOnHeadsetDisconnect; + return prefs.getBoolean(PREF_PAUSE_ON_HEADSET_DISCONNECT, true); } public static boolean isUnpauseOnHeadsetReconnect() { - instanceAvailable(); - return instance.unpauseOnHeadsetReconnect; + return prefs.getBoolean(PREF_UNPAUSE_ON_HEADSET_RECONNECT, true); } public static boolean isFollowQueue() { - instanceAvailable(); - return instance.followQueue; + return prefs.getBoolean(PREF_FOLLOW_QUEUE, false); } public static boolean isAutoDelete() { - instanceAvailable(); - return instance.autoDelete; + return prefs.getBoolean(PREF_AUTO_DELETE, false); } public static int getSmartMarkAsPlayedSecs() { - instanceAvailable(); - return instance.smartMarkAsPlayedSecs; + return Integer.valueOf(prefs.getString(PREF_SMART_MARK_AS_PLAYED_SECS, "30")); } public static boolean isAutoFlattr() { - instanceAvailable(); - return instance.autoFlattr; + return prefs.getBoolean(PREF_AUTO_FLATTR, false); } public static String getPlaybackSpeed() { - instanceAvailable(); - return instance.playbackSpeed; + return prefs.getString(PREF_PLAYBACK_SPEED, "1.0"); + } public static String[] getPlaybackSpeedArray() { - instanceAvailable(); - return instance.playbackSpeedArray; + return readPlaybackSpeedArray(prefs.getString(PREF_PLAYBACK_SPEED_ARRAY, null)); } public static boolean shouldPauseForFocusLoss() { - instanceAvailable(); - return instance.pauseForFocusLoss; + return prefs.getBoolean(PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS, false); } public static long getUpdateInterval() { - instanceAvailable(); - return instance.updateInterval; + return readUpdateInterval(prefs.getString(PREF_UPDATE_INTERVAL, "0")); } public static boolean isAllowMobileUpdate() { - instanceAvailable(); - return instance.allowMobileUpdate; + return prefs.getBoolean(PREF_MOBILE_UPDATE, false); } public static int getParallelDownloads() { - instanceAvailable(); - return instance.parallelDownloads; + return Integer.valueOf(prefs.getString(PREF_PARALLEL_DOWNLOADS, "4")); } public static int getEpisodeCacheSizeUnlimited() { - return EPISODE_CACHE_SIZE_UNLIMITED; + return context.getResources().getInteger(R.integer.episode_cache_size_unlimited); } /** @@ -401,33 +226,27 @@ public class UserPreferences implements * 'unlimited'. */ public static int getEpisodeCacheSize() { - instanceAvailable(); - return instance.episodeCacheSize; + return readEpisodeCacheSizeInternal(prefs.getString(PREF_EPISODE_CACHE_SIZE, "20")); } public static boolean isEnableAutodownload() { - instanceAvailable(); - return instance.enableAutodownload; + return prefs.getBoolean(PREF_ENABLE_AUTODL, false); } public static boolean isEnableAutodownloadOnBattery() { - instanceAvailable(); - return instance.enableAutodownloadOnBattery; + return prefs.getBoolean(PREF_ENABLE_AUTODL_ON_BATTERY, true); } public static boolean isEnableAutodownloadWifiFilter() { - instanceAvailable(); - return instance.enableAutodownloadWifiFilter; + return prefs.getBoolean(PREF_ENABLE_AUTODL_WIFI_FILTER, false); } public static int getFastFowardSecs() { - instanceAvailable(); - return instance.fastForwardSecs; + return prefs.getInt(PREF_FAST_FORWARD_SECS, 30); } public static int getRewindSecs() { - instanceAvailable(); - return instance.rewindSecs; + return prefs.getInt(PREF_REWIND_SECS, 30); } @@ -436,148 +255,40 @@ public class UserPreferences implements * duration. */ public static float getAutoFlattrPlayedDurationThreshold() { - instanceAvailable(); - return instance.autoFlattrPlayedDurationThreshold; + return prefs.getFloat(PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD, 0.8f); } public static String[] getAutodownloadSelectedNetworks() { - instanceAvailable(); - return instance.autodownloadSelectedNetworks; + String selectedNetWorks = prefs.getString(PREF_AUTODL_SELECTED_NETWORKS, ""); + return StringUtils.split(selectedNetWorks, ','); } public static boolean shouldResumeAfterCall() { - instanceAvailable(); - return instance.resumeAfterCall; + return prefs.getBoolean(PREF_RESUME_AFTER_CALL, true); } public static boolean isQueueLocked() { - instanceAvailable(); - return instance.queueLocked; - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences sp, String key) { - Log.d(TAG, "Registered change of user preferences. Key: " + key); - switch(key) { - // User Interface - case PREF_THEME: - theme = readThemeValue(sp.getString(PREF_THEME, "")); - break; - case PREF_HIDDEN_DRAWER_ITEMS: - hiddenDrawerItems = Arrays.asList(StringUtils.split(sp.getString(PREF_HIDDEN_DRAWER_ITEMS, ""), ',')); - break; - case PREF_EXPANDED_NOTIFICATION: - if (sp.getBoolean(PREF_EXPANDED_NOTIFICATION, false)) { - notifyPriority = NotificationCompat.PRIORITY_MAX; - } else { - notifyPriority = NotificationCompat.PRIORITY_DEFAULT; - } - break; - case PREF_PERSISTENT_NOTIFICATION: - persistNotify = sp.getBoolean(PREF_PERSISTENT_NOTIFICATION, false); - break; - case PREF_SHOW_DOWNLOAD_REPORT: - showDownloadReport = sp.getBoolean(PREF_SHOW_DOWNLOAD_REPORT, true); - break; - // Queue - case PREF_QUEUE_ADD_TO_FRONT: - enqueueAtFront = sp.getBoolean(PREF_QUEUE_ADD_TO_FRONT, false); - break; - // Playback - case PREF_PAUSE_ON_HEADSET_DISCONNECT: - pauseOnHeadsetDisconnect = sp.getBoolean(PREF_PAUSE_ON_HEADSET_DISCONNECT, true); - break; - case PREF_UNPAUSE_ON_HEADSET_RECONNECT: - unpauseOnHeadsetReconnect = sp.getBoolean(PREF_UNPAUSE_ON_HEADSET_RECONNECT, true); - break; - case PREF_FOLLOW_QUEUE: - followQueue = sp.getBoolean(PREF_FOLLOW_QUEUE, false); - break; - case PREF_AUTO_DELETE: - autoDelete = sp.getBoolean(PREF_AUTO_DELETE, false); - break; - case PREF_SMART_MARK_AS_PLAYED_SECS: - smartMarkAsPlayedSecs = Integer.valueOf(sp.getString(PREF_SMART_MARK_AS_PLAYED_SECS, "30")); - break; - case PREF_PLAYBACK_SPEED_ARRAY: - playbackSpeedArray = readPlaybackSpeedArray(sp.getString(PREF_PLAYBACK_SPEED_ARRAY, null)); - break; - case PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS: - pauseForFocusLoss = sp.getBoolean(PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS, false); - break; - case PREF_RESUME_AFTER_CALL: - resumeAfterCall = sp.getBoolean(PREF_RESUME_AFTER_CALL, true); - break; - // Network - case PREF_UPDATE_INTERVAL: - updateInterval = readUpdateInterval(sp.getString(PREF_UPDATE_INTERVAL, "0")); - ClientConfig.applicationCallbacks.setUpdateInterval(updateInterval); - break; - case PREF_MOBILE_UPDATE: - allowMobileUpdate = sp.getBoolean(PREF_MOBILE_UPDATE, false); - break; - case PREF_PARALLEL_DOWNLOADS: - parallelDownloads = Integer.valueOf(sp.getString(PREF_PARALLEL_DOWNLOADS, "6")); - break; - case PREF_EPISODE_CACHE_SIZE: - episodeCacheSize = readEpisodeCacheSizeInternal(sp.getString(PREF_EPISODE_CACHE_SIZE, "20")); - break; - case PREF_ENABLE_AUTODL: - enableAutodownload = sp.getBoolean(PREF_ENABLE_AUTODL, false); - break; - case PREF_ENABLE_AUTODL_ON_BATTERY: - enableAutodownloadOnBattery = sp.getBoolean(PREF_ENABLE_AUTODL_ON_BATTERY, true); - break; - case PREF_ENABLE_AUTODL_WIFI_FILTER: - enableAutodownloadWifiFilter = sp.getBoolean(PREF_ENABLE_AUTODL_WIFI_FILTER, false); - break; - case PREF_AUTODL_SELECTED_NETWORKS: - autodownloadSelectedNetworks = StringUtils.split( - sp.getString(PREF_AUTODL_SELECTED_NETWORKS, ""), ','); - break; - // Services - case PREF_AUTO_FLATTR: - autoFlattr = sp.getBoolean(PREF_AUTO_FLATTR, false); - break; - case PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD: - autoFlattrPlayedDurationThreshold = sp.getFloat(PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD, - PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD_DEFAULT); - break; - // Mediaplayer - case PREF_PLAYBACK_SPEED: - playbackSpeed = sp.getString(PREF_PLAYBACK_SPEED, "1.0"); - break; - case PREF_FAST_FORWARD_SECS: - fastForwardSecs = sp.getInt(PREF_FAST_FORWARD_SECS, 30); - break; - case PREF_REWIND_SECS: - rewindSecs = sp.getInt(PREF_REWIND_SECS, 30); - break; - case PREF_QUEUE_LOCKED: - queueLocked = sp.getBoolean(PREF_QUEUE_LOCKED, false); - break; - default: - Log.w(TAG, "Unhandled key: " + key); - } + return prefs.getBoolean(PREF_QUEUE_LOCKED, false); } public static void setPrefFastForwardSecs(int secs) { Log.d(TAG, "setPrefFastForwardSecs(" + secs +")"); - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(instance.context).edit(); + SharedPreferences.Editor editor = prefs.edit(); editor.putInt(PREF_FAST_FORWARD_SECS, secs); editor.commit(); } public static void setPrefRewindSecs(int secs) { Log.d(TAG, "setPrefRewindSecs(" + secs +")"); - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(instance.context).edit(); - editor.putInt(PREF_REWIND_SECS, secs); - editor.commit(); + prefs.edit() + .putInt(PREF_REWIND_SECS, secs) + .apply(); } public static void setPlaybackSpeed(String speed) { - PreferenceManager.getDefaultSharedPreferences(instance.context).edit() - .putString(PREF_PLAYBACK_SPEED, speed).apply(); + prefs.edit() + .putString(PREF_PLAYBACK_SPEED, speed) + .apply(); } public static void setPlaybackSpeedArray(String[] speeds) { @@ -585,72 +296,105 @@ public class UserPreferences implements for (String speed : speeds) { jsonArray.put(speed); } - PreferenceManager.getDefaultSharedPreferences(instance.context).edit() - .putString(PREF_PLAYBACK_SPEED_ARRAY, jsonArray.toString()) - .apply(); - } - - public static void setAutodownloadSelectedNetworks(Context context, - String[] value) { - SharedPreferences.Editor editor = PreferenceManager - .getDefaultSharedPreferences(context.getApplicationContext()) - .edit(); - editor.putString(PREF_AUTODL_SELECTED_NETWORKS, - StringUtils.join(value, ',')); - editor.commit(); + prefs.edit() + .putString(PREF_PLAYBACK_SPEED_ARRAY, jsonArray.toString()) + .apply(); + } + + public static void setAutodownloadSelectedNetworks(String[] value) { + prefs.edit() + .putString(PREF_AUTODL_SELECTED_NETWORKS, StringUtils.join(value, ',')) + .apply(); } /** * Sets the update interval value. Should only be used for testing purposes! */ - public static void setUpdateInterval(Context context, long newValue) { - instanceAvailable(); - SharedPreferences.Editor editor = PreferenceManager - .getDefaultSharedPreferences(context.getApplicationContext()) - .edit(); - editor.putString(PREF_UPDATE_INTERVAL, - String.valueOf(newValue)); - editor.commit(); - instance.updateInterval = newValue; + public static void setUpdateInterval(long hours) { + prefs.edit() + .putString(PREF_UPDATE_INTERVAL, String.valueOf(hours)) + .apply(); + restartUpdateAlarm(TimeUnit.HOURS.toMillis(hours), TimeUnit.HOURS.toMillis(hours)); } /** * Change the auto-flattr settings * - * @param context For accessing the shared preferences * @param enabled Whether automatic flattring should be enabled at all * @param autoFlattrThreshold The percentage of playback time after which an episode should be * flattrd. Must be a value between 0 and 1 (inclusive) * */ - public static void setAutoFlattrSettings(Context context, boolean enabled, float autoFlattrThreshold) { - instanceAvailable(); + public static void setAutoFlattrSettings( boolean enabled, float autoFlattrThreshold) { Validate.inclusiveBetween(0.0, 1.0, autoFlattrThreshold); - PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()) - .edit() - .putBoolean(PREF_AUTO_FLATTR, enabled) - .putFloat(PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD, autoFlattrThreshold) - .commit(); - instance.autoFlattr = enabled; - instance.autoFlattrPlayedDurationThreshold = autoFlattrThreshold; + prefs.edit() + .putBoolean(PREF_AUTO_FLATTR, enabled) + .putFloat(PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD, autoFlattrThreshold) + .apply(); } public static void setHiddenDrawerItems(Context context, List items) { - instanceAvailable(); - instance.hiddenDrawerItems = items; String str = StringUtils.join(items, ','); - PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()) - .edit() - .putString(PREF_HIDDEN_DRAWER_ITEMS, str) - .commit(); + prefs.edit() + .putString(PREF_HIDDEN_DRAWER_ITEMS, str) + .apply(); } public static void setQueueLocked(boolean locked) { - instanceAvailable(); - instance.queueLocked = locked; - PreferenceManager.getDefaultSharedPreferences(instance.context) - .edit() - .putBoolean(PREF_QUEUE_LOCKED, locked) - .commit(); + prefs.edit() + .putBoolean(PREF_QUEUE_LOCKED, locked) + .apply(); + } + + private static int readThemeValue(String valueFromPrefs) { + switch (Integer.parseInt(valueFromPrefs)) { + case 0: + return R.style.Theme_AntennaPod_Light; + case 1: + return R.style.Theme_AntennaPod_Dark; + default: + return R.style.Theme_AntennaPod_Light; + } + } + + private static long readUpdateInterval(String valueFromPrefs) { + int hours = Integer.parseInt(valueFromPrefs); + return TimeUnit.HOURS.toMillis(hours); + } + + private static int readEpisodeCacheSizeInternal(String valueFromPrefs) { + if (valueFromPrefs.equals(context.getString(R.string.pref_episode_cache_unlimited))) { + return EPISODE_CACHE_SIZE_UNLIMITED; + } else { + return Integer.valueOf(valueFromPrefs); + } + } + + private static String[] readPlaybackSpeedArray(String valueFromPrefs) { + String[] selectedSpeeds = null; + // If this preference hasn't been set yet, return the default options + if (valueFromPrefs == null) { + String[] allSpeeds = context.getResources().getStringArray(R.array.playback_speed_values); + List speedList = new LinkedList(); + for (String speedStr : allSpeeds) { + float speed = Float.parseFloat(speedStr); + if (speed < 2.0001 && speed * 10 % 1 == 0) { + speedList.add(speedStr); + } + } + selectedSpeeds = speedList.toArray(new String[speedList.size()]); + } else { + try { + JSONArray jsonArray = new JSONArray(valueFromPrefs); + selectedSpeeds = new String[jsonArray.length()]; + for (int i = 0; i < jsonArray.length(); i++) { + selectedSpeeds[i] = jsonArray.getString(i); + } + } catch (JSONException e) { + Log.e(TAG, "Got JSON error when trying to get speeds from JSONArray"); + e.printStackTrace(); + } + } + return selectedSpeeds; } @@ -664,9 +408,6 @@ public class UserPreferences implements * could not be created. */ public static File getDataFolder(Context context, String type) { - instanceAvailable(); - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(context.getApplicationContext()); String strDir = prefs.getString(PREF_DATA_FOLDER, null); if (strDir == null) { Log.d(TAG, "Using default data folder"); @@ -712,12 +453,9 @@ public class UserPreferences implements public static void setDataFolder(String dir) { Log.d(TAG, "Result from DirectoryChooser: " + dir); - instanceAvailable(); - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(instance.context); - SharedPreferences.Editor editor = prefs.edit(); - editor.putString(PREF_DATA_FOLDER, dir); - editor.commit(); + prefs.edit() + .putString(PREF_DATA_FOLDER, dir) + .apply(); createImportDirectory(); } @@ -725,8 +463,7 @@ public class UserPreferences implements * Create a .nomedia file to prevent scanning by the media scanner. */ private static void createNoMediaFile() { - File f = new File(instance.context.getExternalFilesDir(null), - ".nomedia"); + File f = new File(context.getExternalFilesDir(null), ".nomedia"); if (!f.exists()) { try { f.createNewFile(); @@ -743,8 +480,7 @@ public class UserPreferences implements * available */ private static void createImportDirectory() { - File importDir = getDataFolder(instance.context, - IMPORT_DIR); + File importDir = getDataFolder(context, IMPORT_DIR); if (importDir != null) { if (importDir.exists()) { Log.d(TAG, "Import directory already exists"); @@ -761,12 +497,10 @@ public class UserPreferences implements * Updates alarm registered with the AlarmManager service or deactivates it. */ public static void restartUpdateAlarm(long triggerAtMillis, long intervalMillis) { - instanceAvailable(); Log.d(TAG, "Restarting update alarm."); - AlarmManager alarmManager = (AlarmManager) instance.context - .getSystemService(Context.ALARM_SERVICE); - PendingIntent updateIntent = PendingIntent.getBroadcast( - instance.context, 0, new Intent(ClientConfig.applicationCallbacks.getApplicationInstance(), FeedUpdateReceiver.class), 0); + AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + PendingIntent updateIntent = PendingIntent.getBroadcast(context, 0, + new Intent(ClientConfig.applicationCallbacks.getApplicationInstance(), FeedUpdateReceiver.class), 0); alarmManager.cancel(updateIntent); if (intervalMillis != 0) { alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, @@ -777,12 +511,11 @@ public class UserPreferences implements } } - /** * Reads episode cache size as it is saved in the episode_cache_size_values array. */ public static int readEpisodeCacheSize(String valueFromPrefs) { - instanceAvailable(); - return instance.readEpisodeCacheSizeInternal(valueFromPrefs); + return readEpisodeCacheSizeInternal(valueFromPrefs); } + } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index 4a329c2f6..95c033dc5 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -124,8 +124,7 @@ public final class DBReader { * can be loaded separately with {@link #getFeedItemList(android.content.Context, de.danoeh.antennapod.core.feed.Feed)}. */ public static List getExpiredFeedsList(final Context context, final long expirationTime) { - if (BuildConfig.DEBUG) - Log.d(TAG, String.format("getExpiredFeedsList(%d)", expirationTime)); + Log.d(TAG, String.format("getExpiredFeedsList(%d)", expirationTime)); PodDBAdapter adapter = new PodDBAdapter(context); adapter.open(); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index defce5930..711cd773e 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -199,11 +199,8 @@ public final class DBTasks { */ public static List getExpiredFeeds(final Context context) { long millis = UserPreferences.getUpdateInterval(); - if (millis > 0) { - - List feedList = DBReader.getExpiredFeedsList(context, - millis); + List feedList = DBReader.getExpiredFeedsList(context, millis); if (feedList.size() > 0) { refreshFeeds(context, feedList); } -- cgit v1.2.3