[Android] Close button in notification to stop unpausable media.

Shivansh Saini git at videolan.org
Wed Mar 13 11:12:06 CET 2019


vlc-android | branch: 3.1.x | Shivansh Saini <shivanshs9 at gmail.com> | Sun Mar 10 21:48:49 2019 +0530| [8664912f4ecddd2019f99424dd0bd66f7a2ab8d9] | committer: Geoffrey Métais

Close button in notification to stop unpausable media.

Fixes #181

> https://code.videolan.org/videolan/vlc-android/commit/8664912f4ecddd2019f99424dd0bd66f7a2ab8d9
---

 .../src/org/videolan/vlc/PlaybackService.kt        |  5 ++--
 .../vlc/gui/helpers/NotificationHelper.java        | 30 ++++++++++++----------
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 155d47d3f..f31c08a01 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -569,7 +569,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
         val notification = if (this::notification.isInitialized && !stopped) notification
         else NotificationHelper.createPlaybackNotification(ctx,false,
                 ctx.resources.getString(R.string.loading), "", "",null,
-                false, mediaSession.sessionToken, sessionPendingIntent)
+                false, true, mediaSession.sessionToken, sessionPendingIntent)
         startForeground(3, notification)
         isForeground = true
         if (isVideoPlaying || AndroidDevices.showTvUi(this) || stopped) hideNotification(true)
@@ -661,6 +661,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
         if (mw != null) {
             val coverOnLockscreen = settings.getBoolean("lockscreen_cover", true)
             val playing = isPlaying
+            val pausable = isPausable && isSeekable && length > 0 // TODO: length > 0 is temporarily hack till libVlc is fixed.
             val sessionToken = mediaSession.sessionToken
             val ctx = this
             val metaData = mediaSession.controller.metadata
@@ -680,7 +681,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
 
                     notification = NotificationHelper.createPlaybackNotification(ctx,
                             mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO), title, artist, album,
-                            cover, playing, sessionToken, sessionPendingIntent)
+                            cover, playing, pausable, sessionToken, sessionPendingIntent)
                     if (isPlayingPopup) return at launch
                     if (!AndroidUtil.isLolliPopOrLater || playing || audioFocusHelper.lossTransient) {
                         if (!isForeground) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
index e56d0bedb..44e51cab3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
@@ -50,7 +50,7 @@ public class NotificationHelper {
     public static final String VLC_DEBUG_CHANNEL = "vlc_debug";
 
     public static Notification createPlaybackNotification(Context ctx, boolean video, String title, String artist,
-                                                          String album, Bitmap cover, boolean playing,
+                                                          String album, Bitmap cover, boolean playing, boolean pausable,
                                                           MediaSessionCompat.Token sessionToken,
                                                           PendingIntent spi) {
 
@@ -70,17 +70,20 @@ public class NotificationHelper {
                 .setDeleteIntent(piStop)
                 .setContentIntent(spi)
                 .addAction(new NotificationCompat.Action(
-                R.drawable.ic_widget_previous_w, ctx.getString(R.string.previous),
-                MediaButtonReceiver.buildMediaButtonPendingIntent(ctx,
-                        PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)));
-        if (playing) builder.addAction(new NotificationCompat.Action(
-                R.drawable.ic_widget_pause_w, ctx.getString(R.string.pause),
-                MediaButtonReceiver.buildMediaButtonPendingIntent(ctx,
-                        PlaybackStateCompat.ACTION_PLAY_PAUSE)));
-        else builder.addAction(new NotificationCompat.Action(
-                R.drawable.ic_widget_play_w, ctx.getString(R.string.play),
-                MediaButtonReceiver.buildMediaButtonPendingIntent(ctx,
-                        PlaybackStateCompat.ACTION_PLAY_PAUSE)));
+                        R.drawable.ic_widget_previous_w, ctx.getString(R.string.previous),
+                        MediaButtonReceiver.buildMediaButtonPendingIntent(ctx,
+                                PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)));
+        if (pausable) {
+            if (playing) builder.addAction(new NotificationCompat.Action(
+                    R.drawable.ic_widget_pause_w, ctx.getString(R.string.pause),
+                    MediaButtonReceiver.buildMediaButtonPendingIntent(ctx,
+                            PlaybackStateCompat.ACTION_PLAY_PAUSE)));
+            else builder.addAction(new NotificationCompat.Action(
+                    R.drawable.ic_widget_play_w, ctx.getString(R.string.play),
+                    MediaButtonReceiver.buildMediaButtonPendingIntent(ctx,
+                            PlaybackStateCompat.ACTION_PLAY_PAUSE)));
+        } else builder.addAction(new NotificationCompat.Action(
+                R.drawable.ic_widget_close_w, ctx.getString(R.string.stop), piStop));
         builder.addAction(new NotificationCompat.Action(
                 R.drawable.ic_widget_next_w, ctx.getString(R.string.next),
                 MediaButtonReceiver.buildMediaButtonPendingIntent(ctx,
@@ -89,7 +92,7 @@ public class NotificationHelper {
         if (AndroidDevices.showMediaStyle) {
             builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                     .setMediaSession(sessionToken)
-                    .setShowActionsInCompactView(0,1,2)
+                    .setShowActionsInCompactView(0, 1, 2)
                     .setShowCancelButton(true)
                     .setCancelButtonIntent(piStop)
             );
@@ -99,6 +102,7 @@ public class NotificationHelper {
 
     private static NotificationCompat.Builder scanCompatBuilder;
     private static final Intent notificationIntent = new Intent();
+
     public static Notification createScanNotification(Context ctx, String progressText, boolean updateActions, boolean paused) {
         if (scanCompatBuilder == null) {
             scanCompatBuilder = new NotificationCompat.Builder(ctx, "vlc_medialibrary")



More information about the Android mailing list