diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2015-01-08 13:13:34 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2015-01-08 13:13:34 +0100 |
commit | d58c2b6a2ea416ba8e8ea50605ed75aae6b128fa (patch) | |
tree | d75607779357126247cb0a615b30898e31f32626 /core/src/main | |
parent | 389e1accdd1b753372a03d80b9ad9da3cd54aec4 (diff) | |
parent | fe5d1debc295efaf526bbf381c9105a38dad65e4 (diff) | |
download | AntennaPod-d58c2b6a2ea416ba8e8ea50605ed75aae6b128fa.zip |
Merge branch 'download-when-charging-issue-104' of git://github.com/TomHennen/AntennaPod into TomHennen-download-when-charging-issue-104
Diffstat (limited to 'core/src/main')
5 files changed, 61 insertions, 6 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..96baeeb96 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. @@ -461,10 +463,20 @@ public final class DBTasks { return autodownloadExec.submit(new Runnable() { @Override public void run() { - if (BuildConfig.DEBUG) + + // true if we should auto download based on network status + boolean networkShouldAutoDl = NetworkUtils.autodownloadNetworkAvailable(context) + && UserPreferences.isEnableAutodownload(); + + // true if we should auto download based on power status + boolean powerShouldAutoDl = PowerUtils.deviceCharging(context) + || UserPreferences.isEnableAutodownloadOnBattery(); + + // we should only auto download if both network AND power are happy + if (networkShouldAutoDl && powerShouldAutoDl) { + Log.d(TAG, "Performing auto-dl of undownloaded episodes"); - if (NetworkUtils.autodownloadNetworkAvailable(context) - && UserPreferences.isEnableAutodownload()) { + 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/storage/DownloadRequester.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java index a94bf43d7..d0cdad649 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java @@ -255,8 +255,7 @@ public class DownloadRequester { * Cancels all running downloads */ public synchronized void cancelAllDownloads(Context context) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Cancelling all running downloads"); + Log.d(TAG, "Cancelling all running downloads"); context.sendBroadcast(new Intent( DownloadService.ACTION_CANCEL_ALL_DOWNLOADS)); } 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> |