diff options
author | mueller-ma <mueller-ma@users.noreply.github.com> | 2024-02-25 15:02:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-25 15:02:44 +0100 |
commit | 9cfbae183cd1b5e1bb1fed68125ab064d5be7c55 (patch) | |
tree | fe6dd49c414e8f8e7507a4a879ce8312f00a02fe | |
parent | 82c93bf7ee3d91395533068fdfe640dfa53113fe (diff) | |
download | AntennaPod-9cfbae183cd1b5e1bb1fed68125ab064d5be7c55.zip |
Toggle sleep timer from notification (#6913)
6 files changed, 60 insertions, 11 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java index 0f3320e98..2edf8cb68 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java @@ -6,12 +6,19 @@ import android.os.Build; import android.os.Bundle; import android.widget.Button; import android.widget.ListView; + import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; + import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; + +import org.greenrobot.eventbus.EventBus; + +import java.util.List; + import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.PreferenceActivity; import de.danoeh.antennapod.dialog.DrawerPreferencesDialog; @@ -20,9 +27,6 @@ import de.danoeh.antennapod.dialog.SubscriptionsFilterDialog; import de.danoeh.antennapod.event.PlayerStatusEvent; import de.danoeh.antennapod.event.UnreadItemsUpdateEvent; import de.danoeh.antennapod.storage.preferences.UserPreferences; -import org.greenrobot.eventbus.EventBus; - -import java.util.List; public class UserInterfacePreferencesFragment extends PreferenceFragmentCompat { private static final String PREF_SWIPE = "prefSwipe"; @@ -100,7 +104,12 @@ public class UserInterfacePreferencesFragment extends PreferenceFragmentCompat { final List<Integer> preferredButtons = UserPreferences.getFullNotificationButtons(); final String[] allButtonNames = context.getResources().getStringArray( R.array.full_notification_buttons_options); - final int[] buttonIDs = {2, 3, 4}; + final int[] buttonIDs = { + UserPreferences.NOTIFICATION_BUTTON_SKIP, + UserPreferences.NOTIFICATION_BUTTON_NEXT_CHAPTER, + UserPreferences.NOTIFICATION_BUTTON_PLAYBACK_SPEED, + UserPreferences.NOTIFICATION_BUTTON_SLEEP_TIMER, + }; final int exactItems = 2; final DialogInterface.OnClickListener completeListener = (dialog, which) -> UserPreferences.setFullNotificationButtons(preferredButtons); 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 02102db14..fd11dc4c5 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 @@ -129,6 +129,8 @@ public class PlaybackService extends MediaBrowserServiceCompat { private static final String CUSTOM_ACTION_REWIND = "action.de.danoeh.antennapod.core.service.rewind"; private static final String CUSTOM_ACTION_CHANGE_PLAYBACK_SPEED = "action.de.danoeh.antennapod.core.service.changePlaybackSpeed"; + private static final String CUSTOM_ACTION_TOGGLE_SLEEP_TIMER = + "action.de.danoeh.antennapod.core.service.toggleSleepTimer"; public static final String CUSTOM_ACTION_NEXT_CHAPTER = "action.de.danoeh.antennapod.core.service.next_chapter"; /** @@ -977,6 +979,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { @SuppressWarnings("unused") public void sleepTimerUpdate(SleepTimerUpdatedEvent event) { if (event.isOver()) { + updateMediaSession(mediaPlayer.getPlayerStatus()); mediaPlayer.pause(true, true); mediaPlayer.setVolume(1.0f, 1.0f); int newPosition = mediaPlayer.getPosition() - (int) SleepTimer.NOTIFICATION_THRESHOLD / 2; @@ -988,7 +991,10 @@ public class PlaybackService extends MediaBrowserServiceCompat { Log.d(TAG, "onSleepTimerAlmostExpired: " + multiplicator); mediaPlayer.setVolume(multiplicator, multiplicator); } else if (event.isCancelled()) { + updateMediaSession(mediaPlayer.getPlayerStatus()); mediaPlayer.setVolume(1.0f, 1.0f); + } else if (event.wasJustEnabled()) { + updateMediaSession(mediaPlayer.getPlayerStatus()); } } @@ -1271,6 +1277,16 @@ public class PlaybackService extends MediaBrowserServiceCompat { ); } + if (UserPreferences.showSleepTimerOnFullNotification()) { + @DrawableRes int icon = R.drawable.ic_notification_sleep; + if (sleepTimerActive()) { + icon = R.drawable.ic_notification_sleep_off; + } + sessionState.addCustomAction( + new PlaybackStateCompat.CustomAction.Builder(CUSTOM_ACTION_TOGGLE_SLEEP_TIMER, + getString(R.string.sleep_timer_label), icon).build()); + } + if (UserPreferences.showNextChapterOnFullNotification()) { if (getPlayable() != null && getPlayable().getChapters() != null) { sessionState.addCustomAction( @@ -1950,6 +1966,12 @@ public class PlaybackService extends MediaBrowserServiceCompat { } onSetPlaybackSpeed(newSpeed); } + } else if (CUSTOM_ACTION_TOGGLE_SLEEP_TIMER.equals(action)) { + if (sleepTimerActive()) { + disableSleepTimer(); + } else { + setSleepTimer(SleepTimerPreferences.timerMillis()); + } } } }; diff --git a/core/src/main/res/values/arrays.xml b/core/src/main/res/values/arrays.xml index 7eeab886a..2961339cd 100644 --- a/core/src/main/res/values/arrays.xml +++ b/core/src/main/res/values/arrays.xml @@ -250,6 +250,7 @@ <item>@string/skip_episode_label</item> <item>@string/next_chapter</item> <item>@string/playback_speed</item> + <item>@string/sleep_timer_label</item> </string-array> <string-array name="default_page_values"> diff --git a/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java b/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java index b454ee5a1..2e38d55f1 100644 --- a/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java +++ b/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java @@ -6,17 +6,13 @@ import android.os.Build; import android.text.TextUtils; import android.util.Log; import android.view.KeyEvent; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; import androidx.core.app.NotificationCompat; import androidx.preference.PreferenceManager; -import de.danoeh.antennapod.model.download.ProxyConfig; -import de.danoeh.antennapod.model.feed.FeedCounter; -import de.danoeh.antennapod.model.feed.FeedPreferences; -import de.danoeh.antennapod.model.feed.SortOrder; -import de.danoeh.antennapod.model.feed.SubscriptionsFilter; -import de.danoeh.antennapod.model.playback.MediaType; + import org.json.JSONArray; import org.json.JSONException; @@ -32,6 +28,13 @@ import java.util.List; import java.util.Locale; import java.util.Set; +import de.danoeh.antennapod.model.download.ProxyConfig; +import de.danoeh.antennapod.model.feed.FeedCounter; +import de.danoeh.antennapod.model.feed.FeedPreferences; +import de.danoeh.antennapod.model.feed.SortOrder; +import de.danoeh.antennapod.model.feed.SubscriptionsFilter; +import de.danoeh.antennapod.model.playback.MediaType; + /** * Provides access to preferences set by the user in the settings screen. A * private instance of this class must first be instantiated via @@ -130,9 +133,9 @@ public class UserPreferences { public static final int NOTIFICATION_BUTTON_REWIND = 0; public static final int NOTIFICATION_BUTTON_FAST_FORWARD = 1; public static final int NOTIFICATION_BUTTON_SKIP = 2; - public static final int NOTIFICATION_BUTTON_NEXT_CHAPTER = 3; public static final int NOTIFICATION_BUTTON_PLAYBACK_SPEED = 4; + public static final int NOTIFICATION_BUTTON_SLEEP_TIMER = 5; public static final int EPISODE_CACHE_SIZE_UNLIMITED = -1; public static final int FEED_ORDER_COUNTER = 0; public static final int FEED_ORDER_ALPHABETICAL = 1; @@ -235,6 +238,10 @@ public class UserPreferences { return showButtonOnFullNotification(NOTIFICATION_BUTTON_PLAYBACK_SPEED); } + public static boolean showSleepTimerOnFullNotification() { + return showButtonOnFullNotification(NOTIFICATION_BUTTON_SLEEP_TIMER); + } + public static int getFeedOrder() { String value = prefs.getString(PREF_DRAWER_FEED_ORDER, "" + FEED_ORDER_COUNTER); return Integer.parseInt(value); diff --git a/ui/png-icons/src/main/res/drawable/ic_notification_sleep.xml b/ui/png-icons/src/main/res/drawable/ic_notification_sleep.xml new file mode 100644 index 000000000..60cfbb616 --- /dev/null +++ b/ui/png-icons/src/main/res/drawable/ic_notification_sleep.xml @@ -0,0 +1,5 @@ +<vector android:height="24dp" + android:viewportHeight="24.0" android:viewportWidth="24.0" + android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> + <path android:fillColor="#ffffff" android:pathData="M23,12H17V10L20.39,6H17V4H23V6L19.62,10H23V12M15,16H9V14L12.39,10H9V8H15V10L11.62,14H15V16M7,20H1V18L4.39,14H1V12H7V14L3.62,18H7V20Z"/> +</vector> diff --git a/ui/png-icons/src/main/res/drawable/ic_notification_sleep_off.xml b/ui/png-icons/src/main/res/drawable/ic_notification_sleep_off.xml new file mode 100644 index 000000000..8cb32124b --- /dev/null +++ b/ui/png-icons/src/main/res/drawable/ic_notification_sleep_off.xml @@ -0,0 +1,5 @@ +<vector android:height="24dp" + android:viewportHeight="24.0" android:viewportWidth="24.0" + android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> + <path android:fillColor="#ffffff" android:pathData="M2,5.27L3.28,4L20,20.72L18.73,22L12.73,16H9V14L9.79,13.06L2,5.27M23,12H17V10L20.39,6H17V4H23V6L19.62,10H23V12M9.82,8H15V10L13.54,11.72L9.82,8M7,20H1V18L4.39,14H1V12H7V14L3.62,18H7V20Z"/> +</vector> |