Bug 1384866 - Updated NotificationService for Oreo. r?sdaswani draft
authorVlad Baicu <vlad.baicu@softvision.ro>
Fri, 25 May 2018 16:02:09 +0300
changeset 799827 1d01a95a3938fdbd2b9a59cd5039b5cc354da7e3
parent 799826 99672012a07a0fd9fd87354aef444313e3eee81d
push id111173
push uservbaicu@mozilla.com
push dateFri, 25 May 2018 13:03:19 +0000
reviewerssdaswani
bugs1384866
milestone62.0a1
Bug 1384866 - Updated NotificationService for Oreo. r?sdaswani Using stopService instead of calling startService with a null notification in order to stop NotificationService. MozReview-Commit-ID: CxIhqPj65f8
mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java
mobile/android/base/java/org/mozilla/gecko/notifications/NotificationService.java
--- a/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java
+++ b/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java
@@ -1,32 +1,30 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.notifications;
 
+import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.app.Notification;
 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.support.v4.app.NotificationCompat;
 import android.support.v4.app.NotificationManagerCompat;
-import android.util.Log;
 
 import java.util.HashMap;
 
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.GeckoActivityMonitor;
-import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoService;
 import org.mozilla.gecko.NotificationListener;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.util.BitmapUtils;
 
 /**
  * Client for posting notifications.
@@ -294,23 +292,32 @@ public final class NotificationClient im
      */
     public boolean isOngoing(final Notification notification) {
         if (notification != null && (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0) {
             return true;
         }
         return false;
     }
 
+    @SuppressLint("NewApi")
     private void setForegroundNotificationLocked(final String name,
                                                  final Notification notification) {
         mForegroundNotification = name;
 
         final Intent intent = new Intent(mContext, NotificationService.class);
         intent.putExtra(NotificationService.EXTRA_NOTIFICATION, notification);
-        mContext.startService(intent);
+        if (AppConstants.Versions.preO) {
+            mContext.startService(intent);
+        } else {
+            mContext.startForegroundService(intent);
+        }
+    }
+
+    private void setForegroundNotificationClosed() {
+        mContext.stopService(new Intent(mContext, NotificationService.class));
     }
 
     private void updateForegroundNotificationLocked(final String oldName) {
         if (mForegroundNotification == null || !mForegroundNotification.equals(oldName)) {
             return;
         }
 
         // If we're removing the notification associated with the
@@ -323,11 +330,11 @@ public final class NotificationClient im
                 // uses a special ID, so we need to close its old instantiation and then
                 // re-add it with the new ID through the NotificationService.
                 onNotificationClose(name);
                 setForegroundNotificationLocked(name, notification);
                 return;
             }
         }
 
-        setForegroundNotificationLocked(null, null);
+        setForegroundNotificationClosed();
     }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationService.java
@@ -13,25 +13,18 @@ import android.os.IBinder;
 import org.mozilla.gecko.R;
 
 public final class NotificationService extends Service {
     public static final String EXTRA_NOTIFICATION = "notification";
 
     @Override // Service
     public int onStartCommand(final Intent intent, final int flags, final int startId) {
         final Notification notification = intent.getParcelableExtra(EXTRA_NOTIFICATION);
-        if (notification != null) {
-            // Start foreground notification.
-            startForeground(R.id.foregroundNotification, notification);
-            return START_NOT_STICKY;
-        }
-
-        // Stop foreground notification
-        stopForeground(true);
-        stopSelfResult(startId);
+        // Start foreground notification.
+        startForeground(R.id.foregroundNotification, notification);
         return START_NOT_STICKY;
     }
 
     @Override // Service
     public IBinder onBind(final Intent intent) {
         return null;
     }
 }