Bug 1465102 - Updated NotificationService for Oreo. r=JanH

Changed NotificationClient to use stopService instead of calling startService
with a null notification in order to stop NotificationService.
This way, we always have a guaranteed call to startForeground in our service,
abiding by Android Oreo regulations.

MozReview-Commit-ID: 4CzM4pvANJt

--HG--
extra : rebase_source : ce601c7817ed6dd52eed61e2a4b3a044f6a4b673
This commit is contained in:
Vlad Baicu 2018-05-30 17:54:50 +03:00
Родитель c65f234612
Коммит 2c34396d47
2 изменённых файлов: 18 добавлений и 16 удалений

Просмотреть файл

@ -5,23 +5,22 @@
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.annotation.NonNull;
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;
@ -299,13 +298,23 @@ public final class NotificationClient implements NotificationListener {
return false;
}
private void setForegroundNotificationLocked(final String name,
final Notification notification) {
@SuppressLint("NewApi")
private void setForegroundNotificationLocked(@NonNull final String name,
@NonNull 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 removeForegroundNotificationLocked() {
mForegroundNotification = null;
mContext.stopService(new Intent(mContext, NotificationService.class));
}
private void updateForegroundNotificationLocked(final String oldName) {
@ -328,6 +337,6 @@ public final class NotificationClient implements NotificationListener {
}
}
setForegroundNotificationLocked(null, null);
removeForegroundNotificationLocked();
}
}

Просмотреть файл

@ -18,15 +18,8 @@ public final class NotificationService extends Service {
@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;
}