Toggle sleep timer from notification (#6913)

This commit is contained in:
mueller-ma 2024-02-25 15:02:44 +01:00 committed by GitHub
parent 82c93bf7ee
commit 9cfbae183c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 60 additions and 11 deletions

View File

@ -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);

View File

@ -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());
}
}
}
};

View File

@ -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">

View File

@ -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);

View File

@ -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>

View File

@ -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>