|
1 | 1 | package org.schabi.newpipe.player.notification; |
2 | 2 |
|
| 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 | + |
3 | 7 | import android.annotation.SuppressLint; |
| 8 | +import android.app.PendingIntent; |
4 | 9 | import android.content.Intent; |
5 | 10 | import android.content.pm.ServiceInfo; |
6 | 11 | import android.graphics.Bitmap; |
7 | 12 | import android.os.Build; |
8 | 13 | import android.util.Log; |
9 | 14 |
|
10 | | -import androidx.annotation.DrawableRes; |
11 | 15 | import androidx.annotation.NonNull; |
12 | 16 | import androidx.annotation.Nullable; |
13 | | -import androidx.annotation.StringRes; |
14 | 17 | import androidx.core.app.NotificationCompat; |
15 | 18 | import androidx.core.app.NotificationManagerCompat; |
16 | 19 | import androidx.core.app.PendingIntentCompat; |
|
29 | 32 | import java.util.Objects; |
30 | 33 | import java.util.Optional; |
31 | 34 |
|
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 | | - |
45 | 35 | /** |
46 | 36 | * This is a utility class for player notifications. |
47 | 37 | */ |
@@ -238,115 +228,21 @@ private void updateActions(final NotificationCompat.Builder builder) { |
238 | 228 |
|
239 | 229 | private void addAction(final NotificationCompat.Builder builder, |
240 | 230 | @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; |
244 | 235 | } |
245 | | - } |
246 | 236 |
|
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); |
341 | 243 | } |
342 | | - } |
343 | 244 |
|
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)); |
350 | 246 | } |
351 | 247 |
|
352 | 248 | private Intent getIntentForNotification() { |
|
0 commit comments