From 9fb8125655060a343eb0a68d9a3fe69b01a28b9d Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 29 Dec 2023 14:04:18 +0100 Subject: [PATCH] Allow each notification slot to contain any possible action --- .../notification/NotificationConstants.java | 37 +++++++--------- .../player/notification/NotificationUtil.java | 43 ++++++++++++------- .../custom/NotificationActionsPreference.java | 5 ++- .../settings/custom/NotificationSlot.java | 6 +-- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java index 89bf0b22a..b9607f7ea 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java @@ -13,7 +13,7 @@ import org.schabi.newpipe.util.Localization; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; @@ -65,10 +65,16 @@ public final class NotificationConstants { public static final int CLOSE = 11; @Retention(RetentionPolicy.SOURCE) - @IntDef({NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT, - PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT, SHUFFLE, CLOSE}) + @IntDef({NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, + SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT, + SHUFFLE, CLOSE}) public @interface Action { } + @Action + public static final int[] ALL_ACTIONS = {NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, + SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT, + SHUFFLE, CLOSE}; + @DrawableRes public static final int[] ACTION_ICONS = { 0, @@ -95,16 +101,6 @@ public final class NotificationConstants { CLOSE, }; - @Action - public static final int[][] SLOT_ALLOWED_ACTIONS = { - new int[] {PREVIOUS, REWIND, SMART_REWIND_PREVIOUS}, - new int[] {REWIND, PLAY_PAUSE, PLAY_PAUSE_BUFFERING}, - new int[] {NEXT, FORWARD, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING}, - new int[] {NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, SMART_REWIND_PREVIOUS, - SMART_FORWARD_NEXT, REPEAT, SHUFFLE, CLOSE}, - new int[] {NOTHING, NEXT, FORWARD, SMART_FORWARD_NEXT, REPEAT, SHUFFLE, CLOSE}, - }; - public static final int[] SLOT_PREF_KEYS = { R.string.notification_slot_0_key, R.string.notification_slot_1_key, @@ -165,14 +161,11 @@ public final class NotificationConstants { /** * @param context the context to use * @param sharedPreferences the shared preferences to query values from - * @param slotCount remove indices >= than this value (set to {@code 5} to do nothing, or make - * it lower if there are slots with empty actions) * @return a sorted list of the indices of the slots to use as compact slots */ - public static List getCompactSlotsFromPreferences( + public static Collection getCompactSlotsFromPreferences( @NonNull final Context context, - final SharedPreferences sharedPreferences, - final int slotCount) { + final SharedPreferences sharedPreferences) { final SortedSet compactSlots = new TreeSet<>(); for (int i = 0; i < 3; i++) { final int compactSlot = sharedPreferences.getInt( @@ -180,14 +173,14 @@ public final class NotificationConstants { if (compactSlot == Integer.MAX_VALUE) { // settings not yet populated, return default values - return new ArrayList<>(SLOT_COMPACT_DEFAULTS); + return SLOT_COMPACT_DEFAULTS; } - // a negative value (-1) is set when the user does not want a particular compact slot - if (compactSlot >= 0 && compactSlot < slotCount) { + if (compactSlot >= 0) { + // compact slot is < 0 if there are less than 3 checked checkboxes compactSlots.add(compactSlot); } } - return new ArrayList<>(compactSlots); + return compactSlots; } } diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index 3fa7c2623..551e2b863 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -23,6 +23,8 @@ import org.schabi.newpipe.player.Player; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.util.NavigationHelper; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -101,21 +103,7 @@ public final class NotificationUtil { new NotificationCompat.Builder(player.getContext(), player.getContext().getString(R.string.notification_channel_id)); - initializeNotificationSlots(); - - // count the number of real slots, to make sure compact slots indices are not out of bound - int nonNothingSlotCount = 5; - if (notificationSlots[3] == NotificationConstants.NOTHING) { - --nonNothingSlotCount; - } - if (notificationSlots[4] == NotificationConstants.NOTHING) { - --nonNothingSlotCount; - } - - // build the compact slot indices array (need code to convert from Integer... because Java) - final List compactSlotList = NotificationConstants.getCompactSlotsFromPreferences( - player.getContext(), player.getPrefs(), nonNothingSlotCount); - final int[] compactSlots = compactSlotList.stream().mapToInt(Integer::intValue).toArray(); + final int[] compactSlots = initializeNotificationSlots(); final MediaStyle mediaStyle = new MediaStyle().setShowActionsInCompactView(compactSlots); player.UIs() @@ -209,12 +197,35 @@ public final class NotificationUtil { // ACTIONS ///////////////////////////////////////////////////// - private void initializeNotificationSlots() { + /** + * The compact slots array from settings contains indices from 0 to 4, each referring to one of + * the five actions configurable by the user. However, if the user sets an action to "Nothing", + * then all of the actions coming after will have a "settings index" different than the index + * of the corresponding action when sent to the system. + * + * @return the indices of compact slots referred to the list of non-nothing actions that will be + * sent to the system + */ + private int[] initializeNotificationSlots() { + final Collection settingsCompactSlots = NotificationConstants + .getCompactSlotsFromPreferences(player.getContext(), player.getPrefs()); + final List adjustedCompactSlots = new ArrayList<>(); + + int nonNothingIndex = 0; for (int i = 0; i < 5; ++i) { notificationSlots[i] = player.getPrefs().getInt( player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), NotificationConstants.SLOT_DEFAULTS[i]); + + if (notificationSlots[i] != NotificationConstants.NOTHING) { + if (settingsCompactSlots.contains(i)) { + adjustedCompactSlots.add(nonNothingIndex); + } + nonNothingIndex += 1; + } } + + return adjustedCompactSlots.stream().mapToInt(Integer::intValue).toArray(); } @SuppressLint("RestrictedApi") diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java index 43e9d6f0c..7dfddef20 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java @@ -20,6 +20,7 @@ import org.schabi.newpipe.App; import org.schabi.newpipe.R; import org.schabi.newpipe.player.notification.NotificationConstants; +import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; @@ -67,8 +68,8 @@ public class NotificationActionsPreference extends Preference { //////////////////////////////////////////////////////////////////////////// private void setupActions(@NonNull final View view) { - compactSlots = NotificationConstants.getCompactSlotsFromPreferences(getContext(), - getSharedPreferences(), 5); + compactSlots = new ArrayList<>(NotificationConstants.getCompactSlotsFromPreferences( + getContext(), getSharedPreferences())); notificationSlots = IntStream.range(0, 5) .mapToObj(i -> new NotificationSlot(getContext(), getSharedPreferences(), i, view, compactSlots.contains(i), this::onToggleCompactSlot)) diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java index 412215d0f..074532876 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java @@ -130,13 +130,13 @@ class NotificationSlot { .create(); final View.OnClickListener radioButtonsClickListener = v -> { - selectedAction = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][v.getId()]; + selectedAction = NotificationConstants.ALL_ACTIONS[v.getId()]; updateInfo(); alertDialog.dismiss(); }; - for (int id = 0; id < NotificationConstants.SLOT_ALLOWED_ACTIONS[i].length; ++id) { - final int action = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][id]; + for (int id = 0; id < NotificationConstants.ALL_ACTIONS.length; ++id) { + final int action = NotificationConstants.ALL_ACTIONS[id]; final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater) .getRoot();