diff options
author | Tom Hennen <tom.hennen@gmail.com> | 2015-01-05 21:23:21 -0500 |
---|---|---|
committer | Tom Hennen <tom.hennen@gmail.com> | 2015-01-05 21:23:21 -0500 |
commit | 1b9c96bc7664adeb12ffd0267c76a4667b25311f (patch) | |
tree | 6a105f8ab1a7468f61f7d35d57de45fb3043a4bb /core | |
parent | 9ece3a7db4949455cbaf0127e3eea3c4c739f9b1 (diff) | |
download | AntennaPod-1b9c96bc7664adeb12ffd0267c76a4667b25311f.zip |
Add ability to only do autodl when charging.
Defaults to allow autodl on battery to preserve existing behavior.
Might be a good idea to add a receiver for the intent similar to
ConnectivityActionReceiver as this will allow us to start/stop
downloads as needed.
Diffstat (limited to 'core')
4 files changed, 51 insertions, 3 deletions
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 186b390a8..a3b9f6049 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 @@ -51,6 +51,7 @@ public class UserPreferences implements public static final String PREF_DATA_FOLDER = "prefDataFolder"; public static final String PREF_ENABLE_AUTODL = "prefEnableAutoDl"; public static final String PREF_ENABLE_AUTODL_WIFI_FILTER = "prefEnableAutoDownloadWifiFilter"; + public static final String PREF_ENABLE_AUTODL_ON_BATTERY = "prefEnableAutoDownloadOnBattery"; private static final String PREF_AUTODL_SELECTED_NETWORKS = "prefAutodownloadSelectedNetworks"; public static final String PREF_EPISODE_CACHE_SIZE = "prefEpisodeCacheSize"; private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed"; @@ -82,6 +83,7 @@ public class UserPreferences implements private int theme; private boolean enableAutodownload; private boolean enableAutodownloadWifiFilter; + private boolean enableAutodownloadOnBattery; private String[] autodownloadSelectedNetworks; private int episodeCacheSize; private String playbackSpeed; @@ -144,6 +146,7 @@ public class UserPreferences implements 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); playbackSpeed = sp.getString(PREF_PLAYBACK_SPEED, "1.0"); playbackSpeedArray = readPlaybackSpeedArray(sp.getString( PREF_PLAYBACK_SPEED_ARRAY, null)); @@ -344,6 +347,11 @@ public class UserPreferences implements return instance.enableAutodownload; } + public static boolean isEnableAutodownloadOnBattery() { + instanceAvailable(); + return instance.enableAutodownloadOnBattery; + } + public static boolean shouldPauseForFocusLoss() { instanceAvailable(); return instance.pauseForFocusLoss; @@ -395,6 +403,8 @@ public class UserPreferences implements PREF_EPISODE_CACHE_SIZE, "20")); } else if (key.equals(PREF_ENABLE_AUTODL)) { enableAutodownload = sp.getBoolean(PREF_ENABLE_AUTODL, false); + } else if (key.equals(PREF_ENABLE_AUTODL_ON_BATTERY)) { + enableAutodownloadOnBattery = sp.getBoolean(PREF_ENABLE_AUTODL_ON_BATTERY, true); } else if (key.equals(PREF_PLAYBACK_SPEED)) { playbackSpeed = sp.getString(PREF_PLAYBACK_SPEED, "1.0"); } else if (key.equals(PREF_PLAYBACK_SPEED_ARRAY)) { 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 b1aff5594..b02e621ae 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 @@ -36,6 +36,7 @@ import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.util.DownloadError; import de.danoeh.antennapod.core.util.NetworkUtils; +import de.danoeh.antennapod.core.util.PowerUtils; import de.danoeh.antennapod.core.util.QueueAccess; import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator; import de.danoeh.antennapod.core.util.exception.MediaFileNotFoundException; @@ -449,7 +450,8 @@ public final class DBTasks { /** * Looks for undownloaded episodes in the queue or list of unread items and request a download if * 1. Network is available - * 2. There is free space in the episode cache + * 2. The device is charging or the user allows auto download on battery + * 3. There is free space in the episode cache * This method is executed on an internal single thread executor. * * @param context Used for accessing the DB. @@ -463,8 +465,10 @@ public final class DBTasks { public void run() { if (BuildConfig.DEBUG) Log.d(TAG, "Performing auto-dl of undownloaded episodes"); - if (NetworkUtils.autodownloadNetworkAvailable(context) - && UserPreferences.isEnableAutodownload()) { + if ((NetworkUtils.autodownloadNetworkAvailable(context) + && UserPreferences.isEnableAutodownload()) + && (PowerUtils.deviceCharging(context) + || UserPreferences.isEnableAutodownloadOnBattery())) { final List<FeedItem> queue = DBReader.getQueue(context); final List<FeedItem> unreadItems = DBReader .getUnreadItemsList(context); diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/PowerUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/PowerUtils.java new file mode 100644 index 000000000..39deea36a --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/util/PowerUtils.java @@ -0,0 +1,32 @@ +package de.danoeh.antennapod.core.util; + +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.BatteryManager; + +/** + * Created by Tom on 1/5/15. + */ +public class PowerUtils { + + private static final String TAG = "PowerUtils"; + + private PowerUtils() { + + } + + /** + * @return true if the device is charging + */ + public static boolean deviceCharging(Context context) { + // from http://developer.android.com/training/monitoring-device-state/battery-monitoring.html + IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + Intent batteryStatus = context.registerReceiver(null, iFilter); + + int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); + return (status == BatteryManager.BATTERY_STATUS_CHARGING || + status == BatteryManager.BATTERY_STATUS_FULL); + + } +} diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 75376cef8..dcad6c201 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -235,6 +235,8 @@ <string name="pref_automatic_download_sum">Configure the automatic download of episodes.</string> <string name="pref_autodl_wifi_filter_title">Enable Wi-Fi filter</string> <string name="pref_autodl_wifi_filter_sum">Allow automatic download only for selected Wi-Fi networks.</string> + <string name="pref_automatic_download_on_battery_title">Automatic download on battery</string> + <string name="pref_automatic_download_on_battery_sum">Allow automatic download while on battery</string> <string name="pref_episode_cache_title">Episode cache</string> <string name="pref_theme_title_light">Light</string> <string name="pref_theme_title_dark">Dark</string> |