From d3b94f3d4cc441d561c00e2b7a56fdab6664fb83 Mon Sep 17 00:00:00 2001 From: tzugen Date: Wed, 21 Apr 2021 16:41:08 +0200 Subject: [PATCH] Add Star/Unstar and close Action Also: Add album to subtitle :) --- .../service/MediaPlayerControllerImpl.java | 12 +++ .../service/MediaPlayerLifecycleSupport.java | 3 + .../service/MediaPlayerService.java | 75 ++++++++++++++----- .../res/drawable/ic_baseline_close_24.xml | 9 +++ 4 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 ultrasonic/src/main/res/drawable/ic_baseline_close_24.xml diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerControllerImpl.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerControllerImpl.java index 516406d7..662b0da1 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerControllerImpl.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerControllerImpl.java @@ -589,6 +589,18 @@ public class MediaPlayerControllerImpl implements MediaPlayerController if (mediaPlayerService != null) mediaPlayerService.updateNotification(localMediaPlayer.playerState, localMediaPlayer.currentPlaying); } + public void toggleSongStarred() { + if (localMediaPlayer.currentPlaying == null) + return; + + final Entry song = localMediaPlayer.currentPlaying.getSong(); + + // Trigger an update + localMediaPlayer.setCurrentPlaying(localMediaPlayer.currentPlaying); + + song.setStarred(!song.getStarred()); + } + public void setSongRating(final int rating) { if (!KoinJavaComponent.get(FeatureStorage.class).isFeatureEnabled(Feature.FIVE_STAR_RATING)) diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.java index 2e4d6984..3c4c0750 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.java @@ -254,6 +254,9 @@ public class MediaPlayerLifecycleSupport case KeyEvent.KEYCODE_5: mediaPlayerController.setSongRating(5); break; + case KeyEvent.KEYCODE_STAR: + mediaPlayerController.toggleSongStarred(); + break; default: break; } diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerService.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerService.java index 460947b6..a0046956 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerService.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/MediaPlayerService.java @@ -672,6 +672,7 @@ public class MediaPlayerService extends Service private Notification buildForegroundNotification(PlayerState playerState, DownloadFile currentPlaying) { // Init Context context = getApplicationContext(); + MusicDirectory.Entry song = (currentPlaying != null) ? currentPlaying.getSong() : null; // We should use a single notification builder, otherwise the notification may not be updated if (notificationBuilder == null) { @@ -682,26 +683,15 @@ public class MediaPlayerService extends Service notificationBuilder.setAutoCancel(false); notificationBuilder.setOngoing(true); notificationBuilder.setOnlyAlertOnce(true); - notificationBuilder.setWhen(0); + notificationBuilder.setWhen(System.currentTimeMillis()); + notificationBuilder.setShowWhen(false); notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); notificationBuilder.setPriority(NotificationCompat.PRIORITY_LOW); - notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); - notificationBuilder.setColor(NotificationCompat.COLOR_DEFAULT); // Add content intent (when user taps on notification) notificationBuilder.setContentIntent(getPendingIntentForContent()); } - // Set song title, artist and cover if possible - if (currentPlaying != null) { - MusicDirectory.Entry song = currentPlaying.getSong(); - int iconSize = (int) (256 * context.getResources().getDisplayMetrics().density); - Bitmap bitmap = FileUtil.getAlbumArtBitmap(context, song, iconSize, true); - notificationBuilder.setContentTitle(song.getTitle()); - notificationBuilder.setContentText(song.getArtist()); - notificationBuilder.setLargeIcon(bitmap); - } - // Use the Media Style, to enable native Android support for playback notification androidx.media.app.NotificationCompat.MediaStyle style = new androidx.media.app.NotificationCompat.MediaStyle(); style.setMediaSession(mediaSessionToken); @@ -710,19 +700,37 @@ public class MediaPlayerService extends Service notificationBuilder.clearActions(); // Add actions - int[] compactActions = addActions(context, notificationBuilder, playerState); + int[] compactActions = addActions(context, notificationBuilder, playerState, song); + + // Configure shortcut actions style.setShowActionsInCompactView(compactActions); notificationBuilder.setStyle(style); + // Set song title, artist and cover if possible + if (song != null) { + int iconSize = (int) (256 * context.getResources().getDisplayMetrics().density); + Bitmap bitmap = FileUtil.getAlbumArtBitmap(context, song, iconSize, true); + notificationBuilder.setContentTitle(song.getTitle()); + notificationBuilder.setContentText(song.getArtist()); + notificationBuilder.setLargeIcon(bitmap); + notificationBuilder.setSubText(song.getAlbum()); + } + return notificationBuilder.build(); } - private int[] addActions(Context context, NotificationCompat.Builder notificationBuilder, PlayerState playerState) { + private int[] addActions(Context context, NotificationCompat.Builder notificationBuilder, PlayerState playerState, MusicDirectory.Entry song) { ArrayList compactActionList = new ArrayList<>(); int numActions = 0; // we start and 0 and then increment by 1 for each call to generateAction + // Star + if (song != null) { + notificationBuilder.addAction(generateStarUnstarAction(context, numActions, song.getStarred())); + } + numActions++; + // Next notificationBuilder.addAction(generateAction(context, numActions)); compactActionList.add(numActions); @@ -736,6 +744,10 @@ public class MediaPlayerService extends Service // Previous notificationBuilder.addAction(generateAction(context, numActions)); compactActionList.add(numActions); + numActions++; + + // Close + notificationBuilder.addAction(generateAction(context, numActions)); int[] actionArray = new int[compactActionList.size()]; @@ -755,19 +767,24 @@ public class MediaPlayerService extends Service // If you change the order here, also update the requestCode in updatePlayPauseAction()! switch (requestCode) { - case 0: + case 1: keycode = KeyEvent.KEYCODE_MEDIA_PREVIOUS; label = getString(R.string.common_play_previous); icon = R.drawable.media_backward_medium_dark; break; - case 1: + case 2: // Is handled in generatePlayPauseAction() return null; - case 2: + case 3: keycode = KeyEvent.KEYCODE_MEDIA_NEXT; label = getString(R.string.common_play_next); icon = R.drawable.media_forward_medium_dark; break; + case 4: + keycode = KeyEvent.KEYCODE_MEDIA_STOP; + label = getString(R.string.buttons_stop); + icon = R.drawable.ic_baseline_close_24; + break; default: return null; } @@ -797,6 +814,28 @@ public class MediaPlayerService extends Service } + private NotificationCompat.Action generateStarUnstarAction(Context context, int requestCode, Boolean isStarred) { + + int keyCode; + String label; + int icon; + keyCode = KeyEvent.KEYCODE_STAR; + + if (isStarred) { + label = getString(R.string.download_menu_star); + icon = R.drawable.ic_star_full_dark; + + } else { + label = getString(R.string.download_menu_star); + icon = R.drawable.ic_star_hollow_dark; + } + + PendingIntent pendingIntent = getPendingIntentForMediaAction(context, keyCode, requestCode); + + return new NotificationCompat.Action.Builder(icon, label, pendingIntent).build(); + } + + private PendingIntent getPendingIntentForContent() { Intent notificationIntent = new Intent(this, NavigationActivity.class) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); diff --git a/ultrasonic/src/main/res/drawable/ic_baseline_close_24.xml b/ultrasonic/src/main/res/drawable/ic_baseline_close_24.xml new file mode 100644 index 00000000..361e6899 --- /dev/null +++ b/ultrasonic/src/main/res/drawable/ic_baseline_close_24.xml @@ -0,0 +1,9 @@ + + +