[tbb-commits] [tor-browser/tor-browser-60.3.0esr-8.5-1] Bug 28051 - Use Orbot's notification-builder wrapper class

gk at torproject.org gk at torproject.org
Thu Nov 29 09:24:28 UTC 2018


commit e6c7c8b6022a3d9e5c1dab2966ab45b467d84630
Author: Matthew Finkel <Matthew.Finkel at gmail.com>
Date:   Fri Nov 23 20:39:24 2018 +0000

    Bug 28051 - Use Orbot's notification-builder wrapper class
    
    This allows for notifications on all supported versions of Android.
---
 .../gecko/notifications/NotificationClient.java    | 39 ++++++++++++++++++++--
 .../gecko/notifications/NotificationHelper.java    |  5 +--
 2 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java b/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java
index d8392a801e13..b2312b8780d2 100644
--- a/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java
+++ b/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java
@@ -7,12 +7,15 @@ package org.mozilla.gecko.notifications;
 
 import android.app.Activity;
 import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.net.Uri;
+import android.os.Build;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.app.NotificationManagerCompat;
 import android.util.Log;
@@ -28,6 +31,8 @@ import org.mozilla.gecko.NotificationListener;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.util.BitmapUtils;
 
+import org.torproject.android.service.util.NotificationBuilderCompat;
+
 /**
  * Client for posting notifications.
  */
@@ -37,8 +42,11 @@ public final class NotificationClient implements NotificationListener {
     /* package */ static final String CLOSE_ACTION = AppConstants.ANDROID_PACKAGE_NAME + ".NOTIFICATION_CLOSE";
     /* package */ static final String PERSISTENT_INTENT_EXTRA = "persistentIntent";
 
+    private static final String NOTIFICATION_CHANNEL_ID = NotificationBuilderCompat.DEFAULT_CHANNEL_ID;
+
     private final Context mContext;
     private final NotificationManagerCompat mNotificationManager;
+    private NotificationManager mNativeNotificationManager;
 
     private final HashMap<String, Notification> mNotifications = new HashMap<>();
 
@@ -57,6 +65,8 @@ public final class NotificationClient implements NotificationListener {
     public NotificationClient(Context context) {
         mContext = context.getApplicationContext();
         mNotificationManager = NotificationManagerCompat.from(mContext);
+
+        createNotificationChannel();
     }
 
     @Override // NotificationListener
@@ -72,6 +82,31 @@ public final class NotificationClient implements NotificationListener {
         showNotification(name, cookie, title, text, host, imageUrl, data != null ? data : "");
     }
 
+    /* Only create the notification channel if we're running on Android O or later.
+     * The notification channel is required for notifications on new Android versions.
+     */
+    private void createNotificationChannel() {
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
+            return;
+        }
+
+        NotificationManager mNativeNotificationManager =
+            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+
+        CharSequence name = mContext.getString(R.string.app_name);
+        String description = mContext.getString(R.string.app_description);
+        int importance = NotificationManager.IMPORTANCE_LOW;
+
+        NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
+        mChannel.setDescription(description);
+        mChannel.enableLights(false);
+        mChannel.enableVibration(false);
+        mChannel.setShowBadge(false);
+        mChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
+
+        mNativeNotificationManager.createNotificationChannel(mChannel);
+    }
+
     private void showNotification(String name, String cookie, String title,
                                   String text, String host, String imageUrl,
                                   String persistentData) {
@@ -142,7 +177,7 @@ public final class NotificationClient implements NotificationListener {
     private void add(final String name, final String imageUrl, final String host,
                      final String alertTitle, final String alertText,
                      final PendingIntent contentIntent, final PendingIntent deleteIntent) {
-        final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
+        final NotificationBuilderCompat builder = new NotificationBuilderCompat(mContext)
                 .setContentTitle(alertTitle)
                 .setContentText(alertText)
                 .setSmallIcon(R.drawable.ic_status_logo)
@@ -150,7 +185,7 @@ public final class NotificationClient implements NotificationListener {
                 .setDeleteIntent(deleteIntent)
                 .setAutoCancel(true)
                 .setDefaults(Notification.DEFAULT_SOUND)
-                .setStyle(new NotificationCompat.BigTextStyle()
+                .setStyle(new Notification.BigTextStyle()
                         .bigText(alertText)
                         .setSummaryText(host));
 
diff --git a/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationHelper.java b/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationHelper.java
index 34ea99882d9a..35366609da49 100644
--- a/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationHelper.java
+++ b/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationHelper.java
@@ -32,10 +32,11 @@ import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.graphics.Bitmap;
 import android.net.Uri;
-import android.support.v4.app.NotificationCompat;
 import android.support.v4.util.SimpleArrayMap;
 import android.util.Log;
 
+import org.torproject.android.service.util.NotificationBuilderCompat;
+
 public final class NotificationHelper implements BundleEventListener {
     public static final String HELPER_BROADCAST_ACTION = AppConstants.ANDROID_PACKAGE_NAME + ".helperBroadcastAction";
 
@@ -233,7 +234,7 @@ public final class NotificationHelper implements BundleEventListener {
     private void showNotification(final GeckoBundle message) {
         ThreadUtils.assertOnUiThread();
 
-        final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
+        final NotificationBuilderCompat builder = new NotificationBuilderCompat(mContext);
 
         // These attributes are required
         final String id = message.getString(ID_ATTR);



More information about the tbb-commits mailing list