[Android] Refine notification channels management

Geoffrey Métais git at videolan.org
Thu Aug 24 09:50:33 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Aug 24 09:49:24 2017 +0200| [214cdaf8ed67f61a1a410d7aaaee96708627e57b] | committer: Geoffrey Métais

Refine notification channels management

> https://code.videolan.org/videolan/vlc-android/commit/214cdaf8ed67f61a1a410d7aaaee96708627e57b
---

 vlc-android/res/values/strings.xml                 |  4 ++++
 .../src/org/videolan/vlc/PlaybackService.java      |  2 --
 .../src/org/videolan/vlc/VLCApplication.java       |  2 +-
 .../vlc/gui/helpers/NotificationHelper.java        | 28 +++++++++++++++-------
 4 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index e25662872..739a2e50c 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -28,6 +28,10 @@
     </plurals>
 
     <string name="play">Play</string>
+    <string name="playback">Playback</string>
+    <string name="playback_controls">Playback controls</string>
+    <string name="medialibrary_scan">Media library scan</string>
+    <string name="Medialibrary_progress">Media library scanning progress updates</string>
     <string name="play_from_start">Play from start</string>
     <string name="play_as_audio">Play as audio</string>
     <string name="play_as_video">Play as video</string>
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 30d923d2e..7a37e5c20 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -908,8 +908,6 @@ 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());
-
-                        startService(new Intent(ctx, PlaybackService.class));
                         if (!AndroidUtil.isLolliPopOrLater || playing)
                             PlaybackService.this.startForeground(3, notification);
                         else {
diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index 0356eac24..123bab954 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -96,7 +96,7 @@ public class VLCApplication extends Application {
             @Override
             public void run() {
                 if (AndroidUtil.isOOrLater)
-                    NotificationHelper.createNotificationChannel();
+                    NotificationHelper.createNotificationChannels();
                 // Prepare cache folder constants
                 AudioUtil.prepareCacheFolder(instance);
 
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 1de8e997e..31846e756 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
@@ -58,7 +58,7 @@ public class NotificationHelper {
         final PendingIntent piPlay = PendingIntent.getBroadcast(ctx, 0, new Intent(PlaybackService.ACTION_REMOTE_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
         final PendingIntent piForward = PendingIntent.getBroadcast(ctx, 0, new Intent(PlaybackService.ACTION_REMOTE_FORWARD), PendingIntent.FLAG_UPDATE_CURRENT);
         if (AndroidUtil.isOOrLater) {
-            final Notification.Builder builder = new Notification.Builder(ctx, "vlc_channel");
+            final Notification.Builder builder = new Notification.Builder(ctx, "vlc_playback");
             builder.setSmallIcon(video ? R.drawable.ic_notif_video : R.drawable.ic_notif_audio)
                     .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                     .setContentTitle(title)
@@ -83,6 +83,7 @@ public class NotificationHelper {
                         .setShowActionsInCompactView(0,1,2)
                 );
             }
+            ctx.startForegroundService(new Intent(ctx, PlaybackService.class));
             return builder.build();
         } else {
             final NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
@@ -111,6 +112,7 @@ public class NotificationHelper {
                         .setCancelButtonIntent(piStop)
                 );
             }
+            ctx.startService(new Intent(ctx, PlaybackService.class));
             return builder.build();
         }
     }
@@ -121,7 +123,7 @@ public class NotificationHelper {
     public static Notification createScanNotification(Context ctx, String progressText, boolean updateActions, boolean paused) {
         if (AndroidUtil.isOOrLater) {
             if (scanBuilder == null) {
-                scanBuilder = new Notification.Builder(ctx, "vlc_channel")
+                scanBuilder = new Notification.Builder(ctx, "vlc_medialibrary")
                         .setContentIntent(PendingIntent.getActivity(ctx, 0, new Intent(ctx, StartActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
                         .setSmallIcon(R.drawable.ic_notif_scan)
                         .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
@@ -168,14 +170,22 @@ public class NotificationHelper {
 
     private static NotificationManager sNotificationManager;
     @RequiresApi(api = Build.VERSION_CODES.O)
-    public static void createNotificationChannel() {
+    public static void createNotificationChannels() {
         if (sNotificationManager == null)
             sNotificationManager = (NotificationManager) VLCApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
-        final CharSequence name = VLCApplication.getAppResources().getString(R.string.app_name);
-        final String description = VLCApplication.getAppResources().getString(R.string.app_name_full);
-        final int importance = NotificationManager.IMPORTANCE_DEFAULT;
-        NotificationChannel mChannel = new NotificationChannel("vlc_channel", name, importance);
-        mChannel.setDescription(description);
-        sNotificationManager.createNotificationChannel(mChannel);
+        // Playback channel
+        CharSequence name = VLCApplication.getAppResources().getString(R.string.playback);
+        String description = VLCApplication.getAppResources().getString(R.string.playback_controls);
+        NotificationChannel channel = new NotificationChannel("vlc_playback", name, NotificationManager.IMPORTANCE_DEFAULT);
+        channel.setDescription(description);
+        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
+        sNotificationManager.createNotificationChannel(channel);
+        // Scan channel
+        name = VLCApplication.getAppResources().getString(R.string.medialibrary_scan);
+        description = VLCApplication.getAppResources().getString(R.string.Medialibrary_progress);
+        channel = new NotificationChannel("vlc_medialibrary", name, NotificationManager.IMPORTANCE_DEFAULT);
+        channel.setDescription(description);
+        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
+        sNotificationManager.createNotificationChannel(channel);
     }
 }



More information about the Android mailing list