Bug 1465102 - Updated NotificationService for Oreo. r?sdaswani
Using stopService instead of calling startService with a null notification in order to stop NotificationService.
MozReview-Commit-ID: 9jq73uGQ2aV
--- a/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java
+++ b/mobile/android/base/java/org/mozilla/gecko/notifications/NotificationClient.java
@@ -1,15 +1,16 @@
/* -*- 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;
@@ -294,25 +295,35 @@ 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
// foreground, we need to pick another active notification to act
// as the foreground notification.
@@ -323,11 +334,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;
}
}