[Android] Pass context for notifications init

Geoffrey Métais git at videolan.org
Fri Mar 30 16:51:08 CEST 2018


vlc-android | branch: 3.0.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Mar 12 09:53:36 2018 +0100| [cfc68a9ee6ad31b2aea424bc75f280cae9e2cf29] | committer: Geoffrey Métais

Pass context for notifications init

(cherry picked from commit 23819683f753a475ebd0338f4d793c3ca0f861e2)

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

 .../src/org/videolan/vlc/VLCApplication.java       |  2 +-
 .../vlc/gui/helpers/NotificationHelper.java        | 34 ++++++++++------------
 2 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index f1126ec6a..77a65a3e2 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -98,7 +98,7 @@ public class VLCApplication extends Application {
             @Override
             public void run() {
 
-                if (AndroidUtil.isOOrLater) NotificationHelper.createNotificationChannels();
+                if (AndroidUtil.isOOrLater) NotificationHelper.createNotificationChannels(VLCApplication.this);
                 // 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 dc8305c2b..e8faf0a52 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
@@ -37,7 +37,6 @@ import android.support.v7.app.NotificationCompat;
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.vlc.R;
 import org.videolan.vlc.StartActivity;
-import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Constants;
 import org.videolan.vlc.util.Util;
@@ -127,7 +126,7 @@ public class NotificationHelper {
                         .setContentIntent(PendingIntent.getActivity(ctx, 0, new Intent(ctx, StartActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
                         .setSmallIcon(R.drawable.ic_notif_scan)
                         .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
-                        .setContentTitle(VLCApplication.getAppResources().getString(R.string.ml_scanning))
+                        .setContentTitle(ctx.getString(R.string.ml_scanning))
                         .setAutoCancel(false)
                         .setCategory(NotificationCompat.CATEGORY_PROGRESS)
                         .setOngoing(true);
@@ -136,9 +135,9 @@ public class NotificationHelper {
 
             if (updateActions) {
                 notificationIntent.setAction(paused ? ACTION_RESUME_SCAN : ACTION_PAUSE_SCAN);
-                final PendingIntent pi = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
-                final Notification.Action playpause = paused ? new Notification.Action(R.drawable.ic_play, VLCApplication.getAppResources().getString(R.string.resume), pi)
-                        : new Notification.Action(R.drawable.ic_pause, VLCApplication.getAppResources().getString(R.string.pause), pi);
+                final PendingIntent pi = PendingIntent.getBroadcast(ctx.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+                final Notification.Action playpause = paused ? new Notification.Action(R.drawable.ic_play, ctx.getString(R.string.resume), pi)
+                        : new Notification.Action(R.drawable.ic_pause, ctx.getString(R.string.pause), pi);
                 scanBuilder.setActions(playpause);
             }
             return scanBuilder.build();
@@ -149,7 +148,7 @@ public class NotificationHelper {
                         .setContentIntent(PendingIntent.getActivity(ctx, 0, new Intent(ctx, StartActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
                         .setSmallIcon(R.drawable.ic_notif_scan)
                         .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
-                        .setContentTitle(VLCApplication.getAppResources().getString(R.string.ml_scanning))
+                        .setContentTitle(ctx.getString(R.string.ml_scanning))
                         .setAutoCancel(false)
                         .setCategory(NotificationCompat.CATEGORY_PROGRESS)
                         .setOngoing(true);
@@ -158,9 +157,9 @@ public class NotificationHelper {
 
             if (updateActions) {
                 notificationIntent.setAction(paused ? ACTION_RESUME_SCAN : ACTION_PAUSE_SCAN);
-                final PendingIntent pi = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
-                final NotificationCompat.Action playpause = paused ? new NotificationCompat.Action(R.drawable.ic_play, VLCApplication.getAppResources().getString(R.string.resume), pi)
-                        : new NotificationCompat.Action(R.drawable.ic_pause, VLCApplication.getAppResources().getString(R.string.pause), pi);
+                final PendingIntent pi = PendingIntent.getBroadcast(ctx.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+                final NotificationCompat.Action playpause = paused ? new NotificationCompat.Action(R.drawable.ic_play, ctx.getString(R.string.resume), pi)
+                        : new NotificationCompat.Action(R.drawable.ic_pause, ctx.getString(R.string.pause), pi);
                 scanCompatBuilder.mActions.clear();
                 scanCompatBuilder.addAction(playpause);
             }
@@ -169,27 +168,26 @@ public class NotificationHelper {
     }
 
     @RequiresApi(api = Build.VERSION_CODES.O)
-    public static void createNotificationChannels() {
-        final NotificationManager notificationManager = (NotificationManager) VLCApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
-        final Resources res = VLCApplication.getAppResources();
+    public static void createNotificationChannels(Context appCtx) {
+        final NotificationManager notificationManager = (NotificationManager) appCtx.getSystemService(Context.NOTIFICATION_SERVICE);
         // Playback channel
-        CharSequence name = res.getString(R.string.playback);
-        String description = res.getString(R.string.playback_controls);
+        CharSequence name = appCtx.getString(R.string.playback);
+        String description = appCtx.getString(R.string.playback_controls);
         NotificationChannel channel = new NotificationChannel("vlc_playback", name, NotificationManager.IMPORTANCE_LOW);
         channel.setDescription(description);
         channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
         notificationManager.createNotificationChannel(channel);
         // Scan channel
-        name = res.getString(R.string.medialibrary_scan);
-        description = res.getString(R.string.Medialibrary_progress);
+        name = appCtx.getString(R.string.medialibrary_scan);
+        description = appCtx.getString(R.string.Medialibrary_progress);
         channel = new NotificationChannel("vlc_medialibrary", name, NotificationManager.IMPORTANCE_LOW);
         channel.setDescription(description);
         channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
         notificationManager.createNotificationChannel(channel);
         // Recommendations channel
         if (AndroidDevices.isAndroidTv) {
-            name = res.getString(R.string.recommendations);
-            description = res.getString(R.string.recommendations_desc);
+            name = appCtx.getString(R.string.recommendations);
+            description = appCtx.getString(R.string.recommendations_desc);
             channel = new NotificationChannel("vlc_recommendations", name, NotificationManager.IMPORTANCE_LOW);
             channel.setDescription(description);
             channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);



More information about the Android mailing list