Skip to content

Commit 9fb8125

Browse files
committed
Allow each notification slot to contain any possible action
1 parent aab6580 commit 9fb8125

4 files changed

Lines changed: 48 additions & 43 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import java.lang.annotation.Retention;
1515
import java.lang.annotation.RetentionPolicy;
16-
import java.util.ArrayList;
16+
import java.util.Collection;
1717
import java.util.List;
1818
import java.util.SortedSet;
1919
import java.util.TreeSet;
@@ -65,10 +65,16 @@ private NotificationConstants() {
6565
public static final int CLOSE = 11;
6666

6767
@Retention(RetentionPolicy.SOURCE)
68-
@IntDef({NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT,
69-
PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT, SHUFFLE, CLOSE})
68+
@IntDef({NOTHING, PREVIOUS, NEXT, REWIND, FORWARD,
69+
SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT,
70+
SHUFFLE, CLOSE})
7071
public @interface Action { }
7172

73+
@Action
74+
public static final int[] ALL_ACTIONS = {NOTHING, PREVIOUS, NEXT, REWIND, FORWARD,
75+
SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT,
76+
SHUFFLE, CLOSE};
77+
7278
@DrawableRes
7379
public static final int[] ACTION_ICONS = {
7480
0,
@@ -95,16 +101,6 @@ private NotificationConstants() {
95101
CLOSE,
96102
};
97103

98-
@Action
99-
public static final int[][] SLOT_ALLOWED_ACTIONS = {
100-
new int[] {PREVIOUS, REWIND, SMART_REWIND_PREVIOUS},
101-
new int[] {REWIND, PLAY_PAUSE, PLAY_PAUSE_BUFFERING},
102-
new int[] {NEXT, FORWARD, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING},
103-
new int[] {NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, SMART_REWIND_PREVIOUS,
104-
SMART_FORWARD_NEXT, REPEAT, SHUFFLE, CLOSE},
105-
new int[] {NOTHING, NEXT, FORWARD, SMART_FORWARD_NEXT, REPEAT, SHUFFLE, CLOSE},
106-
};
107-
108104
public static final int[] SLOT_PREF_KEYS = {
109105
R.string.notification_slot_0_key,
110106
R.string.notification_slot_1_key,
@@ -165,29 +161,26 @@ public static String getActionName(@NonNull final Context context, @Action final
165161
/**
166162
* @param context the context to use
167163
* @param sharedPreferences the shared preferences to query values from
168-
* @param slotCount remove indices >= than this value (set to {@code 5} to do nothing, or make
169-
* it lower if there are slots with empty actions)
170164
* @return a sorted list of the indices of the slots to use as compact slots
171165
*/
172-
public static List<Integer> getCompactSlotsFromPreferences(
166+
public static Collection<Integer> getCompactSlotsFromPreferences(
173167
@NonNull final Context context,
174-
final SharedPreferences sharedPreferences,
175-
final int slotCount) {
168+
final SharedPreferences sharedPreferences) {
176169
final SortedSet<Integer> compactSlots = new TreeSet<>();
177170
for (int i = 0; i < 3; i++) {
178171
final int compactSlot = sharedPreferences.getInt(
179172
context.getString(SLOT_COMPACT_PREF_KEYS[i]), Integer.MAX_VALUE);
180173

181174
if (compactSlot == Integer.MAX_VALUE) {
182175
// settings not yet populated, return default values
183-
return new ArrayList<>(SLOT_COMPACT_DEFAULTS);
176+
return SLOT_COMPACT_DEFAULTS;
184177
}
185178

186-
// a negative value (-1) is set when the user does not want a particular compact slot
187-
if (compactSlot >= 0 && compactSlot < slotCount) {
179+
if (compactSlot >= 0) {
180+
// compact slot is < 0 if there are less than 3 checked checkboxes
188181
compactSlots.add(compactSlot);
189182
}
190183
}
191-
return new ArrayList<>(compactSlots);
184+
return compactSlots;
192185
}
193186
}

app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
2424
import org.schabi.newpipe.util.NavigationHelper;
2525

26+
import java.util.ArrayList;
27+
import java.util.Collection;
2628
import java.util.List;
2729
import java.util.Objects;
2830
import java.util.Optional;
@@ -101,21 +103,7 @@ private synchronized NotificationCompat.Builder createNotification() {
101103
new NotificationCompat.Builder(player.getContext(),
102104
player.getContext().getString(R.string.notification_channel_id));
103105

104-
initializeNotificationSlots();
105-
106-
// count the number of real slots, to make sure compact slots indices are not out of bound
107-
int nonNothingSlotCount = 5;
108-
if (notificationSlots[3] == NotificationConstants.NOTHING) {
109-
--nonNothingSlotCount;
110-
}
111-
if (notificationSlots[4] == NotificationConstants.NOTHING) {
112-
--nonNothingSlotCount;
113-
}
114-
115-
// build the compact slot indices array (need code to convert from Integer... because Java)
116-
final List<Integer> compactSlotList = NotificationConstants.getCompactSlotsFromPreferences(
117-
player.getContext(), player.getPrefs(), nonNothingSlotCount);
118-
final int[] compactSlots = compactSlotList.stream().mapToInt(Integer::intValue).toArray();
106+
final int[] compactSlots = initializeNotificationSlots();
119107

120108
final MediaStyle mediaStyle = new MediaStyle().setShowActionsInCompactView(compactSlots);
121109
player.UIs()
@@ -209,12 +197,35 @@ public void cancelNotificationAndStopForeground() {
209197
// ACTIONS
210198
/////////////////////////////////////////////////////
211199

212-
private void initializeNotificationSlots() {
200+
/**
201+
* The compact slots array from settings contains indices from 0 to 4, each referring to one of
202+
* the five actions configurable by the user. However, if the user sets an action to "Nothing",
203+
* then all of the actions coming after will have a "settings index" different than the index
204+
* of the corresponding action when sent to the system.
205+
*
206+
* @return the indices of compact slots referred to the list of non-nothing actions that will be
207+
* sent to the system
208+
*/
209+
private int[] initializeNotificationSlots() {
210+
final Collection<Integer> settingsCompactSlots = NotificationConstants
211+
.getCompactSlotsFromPreferences(player.getContext(), player.getPrefs());
212+
final List<Integer> adjustedCompactSlots = new ArrayList<>();
213+
214+
int nonNothingIndex = 0;
213215
for (int i = 0; i < 5; ++i) {
214216
notificationSlots[i] = player.getPrefs().getInt(
215217
player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]),
216218
NotificationConstants.SLOT_DEFAULTS[i]);
219+
220+
if (notificationSlots[i] != NotificationConstants.NOTHING) {
221+
if (settingsCompactSlots.contains(i)) {
222+
adjustedCompactSlots.add(nonNothingIndex);
223+
}
224+
nonNothingIndex += 1;
225+
}
217226
}
227+
228+
return adjustedCompactSlots.stream().mapToInt(Integer::intValue).toArray();
218229
}
219230

220231
@SuppressLint("RestrictedApi")

app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.schabi.newpipe.R;
2121
import org.schabi.newpipe.player.notification.NotificationConstants;
2222

23+
import java.util.ArrayList;
2324
import java.util.List;
2425
import java.util.stream.IntStream;
2526

@@ -67,8 +68,8 @@ public void onDetached() {
6768
////////////////////////////////////////////////////////////////////////////
6869

6970
private void setupActions(@NonNull final View view) {
70-
compactSlots = NotificationConstants.getCompactSlotsFromPreferences(getContext(),
71-
getSharedPreferences(), 5);
71+
compactSlots = new ArrayList<>(NotificationConstants.getCompactSlotsFromPreferences(
72+
getContext(), getSharedPreferences()));
7273
notificationSlots = IntStream.range(0, 5)
7374
.mapToObj(i -> new NotificationSlot(getContext(), getSharedPreferences(), i, view,
7475
compactSlots.contains(i), this::onToggleCompactSlot))

app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ void openActionChooserDialog() {
130130
.create();
131131

132132
final View.OnClickListener radioButtonsClickListener = v -> {
133-
selectedAction = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][v.getId()];
133+
selectedAction = NotificationConstants.ALL_ACTIONS[v.getId()];
134134
updateInfo();
135135
alertDialog.dismiss();
136136
};
137137

138-
for (int id = 0; id < NotificationConstants.SLOT_ALLOWED_ACTIONS[i].length; ++id) {
139-
final int action = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][id];
138+
for (int id = 0; id < NotificationConstants.ALL_ACTIONS.length; ++id) {
139+
final int action = NotificationConstants.ALL_ACTIONS[id];
140140
final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater)
141141
.getRoot();
142142

0 commit comments

Comments
 (0)