Skip to content

Commit 2c4c283

Browse files
committed
Extract NotificationActionData from NotificationUtil
1 parent 9fb8125 commit 2c4c283

2 files changed

Lines changed: 184 additions & 120 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package org.schabi.newpipe.player.notification;
2+
3+
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL;
4+
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE;
5+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_CLOSE;
6+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_FORWARD;
7+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_REWIND;
8+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_NEXT;
9+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PAUSE;
10+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PREVIOUS;
11+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT;
12+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE;
13+
14+
import android.content.Context;
15+
16+
import androidx.annotation.DrawableRes;
17+
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
19+
20+
import org.schabi.newpipe.R;
21+
import org.schabi.newpipe.player.Player;
22+
23+
public final class NotificationActionData {
24+
@Nullable
25+
private final String action;
26+
@NonNull
27+
private final String name;
28+
@DrawableRes
29+
private final int icon;
30+
31+
public NotificationActionData(@Nullable final String action, @NonNull final String name,
32+
@DrawableRes final int icon) {
33+
this.action = action;
34+
this.name = name;
35+
this.icon = icon;
36+
}
37+
38+
@Nullable
39+
public String action() {
40+
return action;
41+
}
42+
43+
@NonNull
44+
public String name() {
45+
return name;
46+
}
47+
48+
@DrawableRes
49+
public int icon() {
50+
return icon;
51+
}
52+
53+
54+
@Nullable
55+
public static NotificationActionData fromNotificationActionEnum(
56+
@NonNull final Player player,
57+
@NotificationConstants.Action final int selectedAction
58+
) {
59+
60+
final int baseActionIcon = NotificationConstants.ACTION_ICONS[selectedAction];
61+
final Context ctx = player.getContext();
62+
63+
switch (selectedAction) {
64+
case NotificationConstants.PREVIOUS:
65+
return new NotificationActionData(ACTION_PLAY_PREVIOUS,
66+
ctx.getString(R.string.exo_controls_previous_description), baseActionIcon);
67+
68+
case NotificationConstants.NEXT:
69+
return new NotificationActionData(ACTION_PLAY_NEXT,
70+
ctx.getString(R.string.exo_controls_next_description), baseActionIcon);
71+
72+
case NotificationConstants.REWIND:
73+
return new NotificationActionData(ACTION_FAST_REWIND,
74+
ctx.getString(R.string.exo_controls_rewind_description), baseActionIcon);
75+
76+
case NotificationConstants.FORWARD:
77+
return new NotificationActionData(ACTION_FAST_FORWARD,
78+
ctx.getString(R.string.exo_controls_fastforward_description),
79+
baseActionIcon);
80+
81+
case NotificationConstants.SMART_REWIND_PREVIOUS:
82+
if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) {
83+
return new NotificationActionData(ACTION_PLAY_PREVIOUS,
84+
ctx.getString(R.string.exo_controls_previous_description),
85+
R.drawable.exo_notification_previous);
86+
} else {
87+
return new NotificationActionData(ACTION_FAST_REWIND,
88+
ctx.getString(R.string.exo_controls_rewind_description),
89+
R.drawable.exo_controls_rewind);
90+
}
91+
92+
case NotificationConstants.SMART_FORWARD_NEXT:
93+
if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) {
94+
return new NotificationActionData(ACTION_PLAY_NEXT,
95+
ctx.getString(R.string.exo_controls_next_description),
96+
R.drawable.exo_notification_next);
97+
} else {
98+
return new NotificationActionData(ACTION_FAST_FORWARD,
99+
ctx.getString(R.string.exo_controls_fastforward_description),
100+
R.drawable.exo_controls_fastforward);
101+
}
102+
103+
case NotificationConstants.PLAY_PAUSE_BUFFERING:
104+
if (player.getCurrentState() == Player.STATE_PREFLIGHT
105+
|| player.getCurrentState() == Player.STATE_BLOCKED
106+
|| player.getCurrentState() == Player.STATE_BUFFERING) {
107+
// null intent action -> show hourglass icon that does nothing when clicked
108+
return new NotificationActionData(null,
109+
ctx.getString(R.string.notification_action_buffering),
110+
R.drawable.ic_hourglass_top);
111+
}
112+
113+
// fallthrough
114+
case NotificationConstants.PLAY_PAUSE:
115+
if (player.getCurrentState() == Player.STATE_COMPLETED) {
116+
return new NotificationActionData(ACTION_PLAY_PAUSE,
117+
ctx.getString(R.string.exo_controls_pause_description),
118+
R.drawable.ic_replay);
119+
} else if (player.isPlaying()
120+
|| player.getCurrentState() == Player.STATE_PREFLIGHT
121+
|| player.getCurrentState() == Player.STATE_BLOCKED
122+
|| player.getCurrentState() == Player.STATE_BUFFERING) {
123+
return new NotificationActionData(ACTION_PLAY_PAUSE,
124+
ctx.getString(R.string.exo_controls_pause_description),
125+
R.drawable.exo_notification_pause);
126+
} else {
127+
return new NotificationActionData(ACTION_PLAY_PAUSE,
128+
ctx.getString(R.string.exo_controls_play_description),
129+
R.drawable.exo_notification_play);
130+
}
131+
132+
case NotificationConstants.REPEAT:
133+
if (player.getRepeatMode() == REPEAT_MODE_ALL) {
134+
return new NotificationActionData(ACTION_REPEAT,
135+
ctx.getString(R.string.exo_controls_repeat_all_description),
136+
R.drawable.exo_media_action_repeat_all);
137+
} else if (player.getRepeatMode() == REPEAT_MODE_ONE) {
138+
return new NotificationActionData(ACTION_REPEAT,
139+
ctx.getString(R.string.exo_controls_repeat_one_description),
140+
R.drawable.exo_media_action_repeat_one);
141+
} else /* player.getRepeatMode() == REPEAT_MODE_OFF */ {
142+
return new NotificationActionData(ACTION_REPEAT,
143+
ctx.getString(R.string.exo_controls_repeat_off_description),
144+
R.drawable.exo_media_action_repeat_off);
145+
}
146+
147+
case NotificationConstants.SHUFFLE:
148+
if (player.getPlayQueue() != null && player.getPlayQueue().isShuffled()) {
149+
return new NotificationActionData(ACTION_SHUFFLE,
150+
ctx.getString(R.string.exo_controls_shuffle_on_description),
151+
R.drawable.exo_controls_shuffle_on);
152+
} else {
153+
return new NotificationActionData(ACTION_SHUFFLE,
154+
ctx.getString(R.string.exo_controls_shuffle_off_description),
155+
R.drawable.exo_controls_shuffle_off);
156+
}
157+
158+
case NotificationConstants.CLOSE:
159+
return new NotificationActionData(ACTION_CLOSE, ctx.getString(R.string.close),
160+
R.drawable.ic_close);
161+
162+
case NotificationConstants.NOTHING:
163+
default:
164+
// do nothing
165+
return null;
166+
}
167+
}
168+
}

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

Lines changed: 16 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package org.schabi.newpipe.player.notification;
22

3+
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
4+
import static androidx.media.app.NotificationCompat.MediaStyle;
5+
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_CLOSE;
6+
37
import android.annotation.SuppressLint;
8+
import android.app.PendingIntent;
49
import android.content.Intent;
510
import android.content.pm.ServiceInfo;
611
import android.graphics.Bitmap;
712
import android.os.Build;
813
import android.util.Log;
914

10-
import androidx.annotation.DrawableRes;
1115
import androidx.annotation.NonNull;
1216
import androidx.annotation.Nullable;
13-
import androidx.annotation.StringRes;
1417
import androidx.core.app.NotificationCompat;
1518
import androidx.core.app.NotificationManagerCompat;
1619
import androidx.core.app.PendingIntentCompat;
@@ -29,19 +32,6 @@
2932
import java.util.Objects;
3033
import java.util.Optional;
3134

32-
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
33-
import static androidx.media.app.NotificationCompat.MediaStyle;
34-
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL;
35-
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE;
36-
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_CLOSE;
37-
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_FORWARD;
38-
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_REWIND;
39-
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_NEXT;
40-
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PAUSE;
41-
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PREVIOUS;
42-
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT;
43-
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE;
44-
4535
/**
4636
* This is a utility class for player notifications.
4737
*/
@@ -238,115 +228,21 @@ private void updateActions(final NotificationCompat.Builder builder) {
238228

239229
private void addAction(final NotificationCompat.Builder builder,
240230
@NotificationConstants.Action final int slot) {
241-
final NotificationCompat.Action action = getAction(slot);
242-
if (action != null) {
243-
builder.addAction(action);
231+
@Nullable final NotificationActionData data =
232+
NotificationActionData.fromNotificationActionEnum(player, slot);
233+
if (data == null) {
234+
return;
244235
}
245-
}
246236

247-
@Nullable
248-
private NotificationCompat.Action getAction(
249-
@NotificationConstants.Action final int selectedAction) {
250-
final int baseActionIcon = NotificationConstants.ACTION_ICONS[selectedAction];
251-
switch (selectedAction) {
252-
case NotificationConstants.PREVIOUS:
253-
return getAction(baseActionIcon,
254-
R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS);
255-
256-
case NotificationConstants.NEXT:
257-
return getAction(baseActionIcon,
258-
R.string.exo_controls_next_description, ACTION_PLAY_NEXT);
259-
260-
case NotificationConstants.REWIND:
261-
return getAction(baseActionIcon,
262-
R.string.exo_controls_rewind_description, ACTION_FAST_REWIND);
263-
264-
case NotificationConstants.FORWARD:
265-
return getAction(baseActionIcon,
266-
R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD);
267-
268-
case NotificationConstants.SMART_REWIND_PREVIOUS:
269-
if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) {
270-
return getAction(R.drawable.exo_notification_previous,
271-
R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS);
272-
} else {
273-
return getAction(R.drawable.exo_controls_rewind,
274-
R.string.exo_controls_rewind_description, ACTION_FAST_REWIND);
275-
}
276-
277-
case NotificationConstants.SMART_FORWARD_NEXT:
278-
if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) {
279-
return getAction(R.drawable.exo_notification_next,
280-
R.string.exo_controls_next_description, ACTION_PLAY_NEXT);
281-
} else {
282-
return getAction(R.drawable.exo_controls_fastforward,
283-
R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD);
284-
}
285-
286-
case NotificationConstants.PLAY_PAUSE_BUFFERING:
287-
if (player.getCurrentState() == Player.STATE_PREFLIGHT
288-
|| player.getCurrentState() == Player.STATE_BLOCKED
289-
|| player.getCurrentState() == Player.STATE_BUFFERING) {
290-
// null intent -> show hourglass icon that does nothing when clicked
291-
return new NotificationCompat.Action(R.drawable.ic_hourglass_top,
292-
player.getContext().getString(R.string.notification_action_buffering),
293-
null);
294-
}
295-
296-
// fallthrough
297-
case NotificationConstants.PLAY_PAUSE:
298-
if (player.getCurrentState() == Player.STATE_COMPLETED) {
299-
return getAction(R.drawable.ic_replay,
300-
R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE);
301-
} else if (player.isPlaying()
302-
|| player.getCurrentState() == Player.STATE_PREFLIGHT
303-
|| player.getCurrentState() == Player.STATE_BLOCKED
304-
|| player.getCurrentState() == Player.STATE_BUFFERING) {
305-
return getAction(R.drawable.exo_notification_pause,
306-
R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE);
307-
} else {
308-
return getAction(R.drawable.exo_notification_play,
309-
R.string.exo_controls_play_description, ACTION_PLAY_PAUSE);
310-
}
311-
312-
case NotificationConstants.REPEAT:
313-
if (player.getRepeatMode() == REPEAT_MODE_ALL) {
314-
return getAction(R.drawable.exo_media_action_repeat_all,
315-
R.string.exo_controls_repeat_all_description, ACTION_REPEAT);
316-
} else if (player.getRepeatMode() == REPEAT_MODE_ONE) {
317-
return getAction(R.drawable.exo_media_action_repeat_one,
318-
R.string.exo_controls_repeat_one_description, ACTION_REPEAT);
319-
} else /* player.getRepeatMode() == REPEAT_MODE_OFF */ {
320-
return getAction(R.drawable.exo_media_action_repeat_off,
321-
R.string.exo_controls_repeat_off_description, ACTION_REPEAT);
322-
}
323-
324-
case NotificationConstants.SHUFFLE:
325-
if (player.getPlayQueue() != null && player.getPlayQueue().isShuffled()) {
326-
return getAction(R.drawable.exo_controls_shuffle_on,
327-
R.string.exo_controls_shuffle_on_description, ACTION_SHUFFLE);
328-
} else {
329-
return getAction(R.drawable.exo_controls_shuffle_off,
330-
R.string.exo_controls_shuffle_off_description, ACTION_SHUFFLE);
331-
}
332-
333-
case NotificationConstants.CLOSE:
334-
return getAction(R.drawable.ic_close,
335-
R.string.close, ACTION_CLOSE);
336-
337-
case NotificationConstants.NOTHING:
338-
default:
339-
// do nothing
340-
return null;
237+
final PendingIntent intent;
238+
if (data.action() == null) {
239+
intent = null;
240+
} else {
241+
intent = PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID,
242+
new Intent(data.action()), FLAG_UPDATE_CURRENT, false);
341243
}
342-
}
343244

344-
private NotificationCompat.Action getAction(@DrawableRes final int drawable,
345-
@StringRes final int title,
346-
final String intentAction) {
347-
return new NotificationCompat.Action(drawable, player.getContext().getString(title),
348-
PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID,
349-
new Intent(intentAction), FLAG_UPDATE_CURRENT, false));
245+
builder.addAction(new NotificationCompat.Action(data.icon(), data.name(), intent));
350246
}
351247

352248
private Intent getIntentForNotification() {

0 commit comments

Comments
 (0)