[Android] Set playback service foreground when in popup mode

Geoffrey Métais git at videolan.org
Mon Oct 2 15:34:42 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Oct  2 15:31:46 2017 +0200| [4b25ae5d64c8087247ab941f2cdbed215a98ec86] | committer: Geoffrey Métais

Set playback service foreground when in popup mode

> https://code.videolan.org/videolan/vlc-android/commit/4b25ae5d64c8087247ab941f2cdbed215a98ec86
---

 vlc-android/src/org/videolan/vlc/PlaybackService.java    |  9 +++++++--
 .../src/org/videolan/vlc/gui/video/PopupManager.java     | 16 ++++++----------
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 0218cf72e..5ab64847e 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -895,7 +895,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
     private void showNotification() {
         if (VLCApplication.showTvUi())
             return;
-        if (mMediaPlayer.getVLCVout().areViewsAttached()) {
+        if (isPlayingPopup() || mMediaPlayer.getVLCVout().areViewsAttached()) {
             hideNotification();
             return;
         }
@@ -908,6 +908,8 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
             mExecutorService.execute(new Runnable() {
                 @Override
                 public void run() {
+                    if (isPlayingPopup())
+                        return;
                     try {
                         Bitmap cover;
                         String title, artist, album;
@@ -930,6 +932,8 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
                         Notification notification = NotificationHelper.createPlaybackNotification(PlaybackService.this,
                                 mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO), title, artist, album,
                                 cover, playing, sessionToken, getSessionPendingIntent());
+                        if (isPlayingPopup())
+                            return;
                         if (!AndroidUtil.isLolliPopOrLater || playing)
                             PlaybackService.this.startForeground(3, notification);
                         else {
@@ -968,7 +972,8 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
         mExecutorService.execute(new Runnable() {
             @Override
             public void run() {
-                PlaybackService.this.stopForeground(true);
+                if (!isPlayingPopup())
+                    PlaybackService.this.stopForeground(true);
                 NotificationManagerCompat.from(PlaybackService.this).cancel(3);
             }
         });
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/PopupManager.java b/vlc-android/src/org/videolan/vlc/gui/video/PopupManager.java
index 878af0d14..4a85d6214 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/PopupManager.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/PopupManager.java
@@ -31,7 +31,6 @@ import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
 import android.support.v4.app.NotificationCompat;
-import android.support.v4.app.NotificationManagerCompat;
 import android.support.v4.view.GestureDetectorCompat;
 import android.support.v7.preference.PreferenceManager;
 import android.util.Log;
@@ -283,9 +282,9 @@ public class PopupManager implements PlaybackService.Callback, GestureDetector.O
     }
 
     private void showNotification() {
-        PendingIntent piStop = PendingIntent.getBroadcast(mService, 0,
+        final PendingIntent piStop = PendingIntent.getBroadcast(mService, 0,
                 new Intent(PlaybackService.ACTION_REMOTE_STOP), PendingIntent.FLAG_UPDATE_CURRENT);
-        NotificationCompat.Builder builder = new NotificationCompat.Builder(mService)
+        final NotificationCompat.Builder builder = new NotificationCompat.Builder(mService)
                 .setSmallIcon(R.drawable.ic_notif_video)
                 .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                 .setContentTitle(mService.getTitle())
@@ -296,23 +295,20 @@ public class PopupManager implements PlaybackService.Callback, GestureDetector.O
 
         //Switch
         final Intent notificationIntent = new Intent(PlaybackService.ACTION_REMOTE_SWITCH_VIDEO);
-        PendingIntent piExpand = PendingIntent.getBroadcast(mService, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+        final PendingIntent piExpand = PendingIntent.getBroadcast(mService, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
         //PLay Pause
-        PendingIntent piPlay = PendingIntent.getBroadcast(mService, 0, new Intent(PlaybackService.ACTION_REMOTE_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
+        final PendingIntent piPlay = PendingIntent.getBroadcast(mService, 0, new Intent(PlaybackService.ACTION_REMOTE_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
 
         if (mService.isPlaying())
             builder.addAction(R.drawable.ic_popup_pause, mService.getString(R.string.pause), piPlay);
         else
             builder.addAction(R.drawable.ic_popup_play, mService.getString(R.string.play), piPlay);
         builder.addAction(R.drawable.ic_popup_expand_w, mService.getString(R.string.popup_expand), piExpand);
-
-        try {
-            NotificationManagerCompat.from(mService).notify(42, builder.build());
-        } catch (IllegalArgumentException ignored) {}
+        mService.startForeground(42, builder.build());
     }
 
     private void hideNotification() {
-        NotificationManagerCompat.from(mService).cancel(42);
+        mService.stopForeground(true);
     }
 
     @Override



More information about the Android mailing list