diff options
author | Martin Fietz <Martin.Fietz@gmail.com> | 2016-10-30 19:12:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-30 19:12:25 +0100 |
commit | cba6e728d2b6c6b791b37604e60557fb405e3b71 (patch) | |
tree | df01d31f66f7821436be8a392503f10d3b82785a /core | |
parent | 56318ae99ce9965dcadd411b6f123d52da766901 (diff) | |
parent | 0062cab1066d42d546d4a2e13012db9d2071db16 (diff) | |
download | AntennaPod-cba6e728d2b6c6b791b37604e60557fb405e3b71.zip |
Merge pull request #2152 from mfietz/2085-auto-enable-sleep-timer
Option to auto-enable sleep timer
Diffstat (limited to 'core')
6 files changed, 119 insertions, 1 deletions
diff --git a/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java b/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java index eb2e6fc9e..88ae6d6bc 100644 --- a/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java +++ b/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java @@ -3,6 +3,7 @@ package de.danoeh.antennapod.core; import android.content.Context; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; +import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.util.NetworkUtils; @@ -43,7 +44,7 @@ public class ClientConfig { UpdateManager.init(context); PlaybackPreferences.init(context); NetworkUtils.init(context); -// CastManager.init(context); + SleepTimerPreferences.init(context); initialized = true; } diff --git a/core/src/main/java/de/danoeh/antennapod/core/event/MessageEvent.java b/core/src/main/java/de/danoeh/antennapod/core/event/MessageEvent.java new file mode 100644 index 000000000..9fc488fbc --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/event/MessageEvent.java @@ -0,0 +1,21 @@ +package de.danoeh.antennapod.core.event; + +import android.support.annotation.Nullable; + +public class MessageEvent { + + public final String message; + + @Nullable + public final Runnable action; + + public MessageEvent(String message) { + this(message, null); + } + + public MessageEvent(String message, Runnable action) { + this.message = message; + this.action = action; + } + +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/SleepTimerPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/SleepTimerPreferences.java new file mode 100644 index 000000000..b7ed890f5 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/SleepTimerPreferences.java @@ -0,0 +1,80 @@ +package de.danoeh.antennapod.core.preferences; + +import android.content.Context; +import android.content.SharedPreferences; +import android.support.annotation.NonNull; +import android.util.Log; + +import java.util.concurrent.TimeUnit; + +public class SleepTimerPreferences { + + private static final String TAG = "SleepTimerPreferences"; + + private static final String PREF_NAME = "SleepTimerDialog"; + private static final String PREF_VALUE = "LastValue"; + private static final String PREF_TIME_UNIT = "LastTimeUnit"; + private static final String PREF_VIBRATE = "Vibrate"; + private static final String PREF_SHAKE_TO_RESET = "ShakeToReset"; + private static final String PREF_AUTO_ENABLE = "AutoEnable"; + + private static final TimeUnit[] UNITS = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS }; + + private static final String DEFAULT_VALUE = "15"; + private static final int DEFAULT_TIME_UNIT = 1; + + private static Context context; + private static SharedPreferences prefs; + + /** + * Sets up the UserPreferences class. + * + * @throws IllegalArgumentException if context is null + */ + public static void init(@NonNull Context context) { + Log.d(TAG, "Creating new instance of SleepTimerPreferences"); + SleepTimerPreferences.prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + } + + public static void setLastTimer(String value, int timeUnit) { + prefs.edit().putString(PREF_VALUE, value).putInt(PREF_TIME_UNIT, timeUnit).apply(); + } + + public static String lastTimerValue() { + return prefs.getString(PREF_VALUE, DEFAULT_VALUE); + } + + public static int lastTimerTimeUnit() { + return prefs.getInt(PREF_TIME_UNIT, DEFAULT_TIME_UNIT); + } + + public static long timerMillis() { + long value = Long.parseLong(lastTimerValue()); + return UNITS[lastTimerTimeUnit()].toMillis(value); + } + + public static void setVibrate(boolean vibrate) { + prefs.edit().putBoolean(PREF_VIBRATE, vibrate).apply(); + } + + public static boolean vibrate() { + return prefs.getBoolean(PREF_VIBRATE, true); + } + + public static void setShakeToReset(boolean shakeToReset) { + prefs.edit().putBoolean(PREF_SHAKE_TO_RESET, shakeToReset).apply(); + } + + public static boolean shakeToReset() { + return prefs.getBoolean(PREF_SHAKE_TO_RESET, true); + } + + public static void setAutoEnable(boolean autoEnable) { + prefs.edit().putBoolean(PREF_AUTO_ENABLE, autoEnable).apply(); + } + + public static boolean autoEnable() { + return prefs.getBoolean(PREF_AUTO_ENABLE, false); + } + +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index e67dc9d0a..33aec0ee0 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -47,12 +47,14 @@ import java.util.List; import de.danoeh.antennapod.core.ClientConfig; import de.danoeh.antennapod.core.R; +import de.danoeh.antennapod.core.event.MessageEvent; import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; +import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; import de.danoeh.antennapod.core.storage.DBReader; @@ -62,6 +64,7 @@ import de.danoeh.antennapod.core.util.IntList; import de.danoeh.antennapod.core.util.QueueAccess; import de.danoeh.antennapod.core.util.playback.ExternalMedia; import de.danoeh.antennapod.core.util.playback.Playable; +import de.greenrobot.event.EventBus; /** * Controls the MediaPlayer that plays a FeedMedia-file @@ -605,6 +608,11 @@ public class PlaybackService extends MediaBrowserServiceCompat { writePlayerStatusPlaybackPreferences(); setupNotification(newInfo); started = true; + // set sleep timer if auto-enabled + if(SleepTimerPreferences.autoEnable() && !sleepTimerActive()) { + setSleepTimer(SleepTimerPreferences.timerMillis(), SleepTimerPreferences.shakeToReset(), + SleepTimerPreferences.vibrate()); + } break; case ERROR: @@ -846,11 +854,14 @@ public class PlaybackService extends MediaBrowserServiceCompat { Log.d(TAG, "Setting sleep timer to " + Long.toString(waitingTime) + " milliseconds"); taskManager.setSleepTimer(waitingTime, shakeToReset, vibrate); sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0); + EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label), + () -> disableSleepTimer())); } public void disableSleepTimer() { taskManager.disableSleepTimer(); sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0); + EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_disabled_label))); } private void writePlaybackPreferencesNoMediaPlaying() { diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 8a92ab5b2..621fc547b 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -484,6 +484,9 @@ <item quantity="one">1 hour</item> <item quantity="other">%d hours</item> </plurals> + <string name="auto_enable_label">Auto-enable</string> + <string name="sleep_timer_enabled_label">Sleep timer enabled</string> + <string name="sleep_timer_disabled_label">Sleep timer disabled</string> <!-- gpodder.net --> <string name="gpodnet_taglist_header">CATEGORIES</string> diff --git a/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java b/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java index 3dfd6ea65..f12f1d806 100644 --- a/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java +++ b/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java @@ -4,6 +4,7 @@ import android.content.Context; import de.danoeh.antennapod.core.cast.CastManager; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; +import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.util.NetworkUtils; @@ -45,6 +46,7 @@ public class ClientConfig { PlaybackPreferences.init(context); NetworkUtils.init(context); CastManager.init(context); + SleepTimerPreferences.init(context); initialized = true; } |