Commit 7da3d4aa authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Download notification: Never call stopForeground without startForeground.

This CL adds a boolean check for stopForeground that if startForeground
is not called, then ignore stopForeground call.

Bug: 1121096
Change-Id: I71295038925947472b0df7be2630a7cffbb79f97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2386421Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803529}
parent db0cba00
...@@ -36,6 +36,10 @@ public class DownloadForegroundService extends Service { ...@@ -36,6 +36,10 @@ public class DownloadForegroundService extends Service {
private NotificationManager mNotificationManager; private NotificationManager mNotificationManager;
// Whether Service.startForeground is called. Notices Android will crash if
// Service.startForeground is not called when calling Service.stopForeground.
private boolean mStartForeground;
@IntDef({StopForegroundNotification.KILL, StopForegroundNotification.DETACH}) @IntDef({StopForegroundNotification.KILL, StopForegroundNotification.DETACH})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface StopForegroundNotification { public @interface StopForegroundNotification {
...@@ -231,6 +235,7 @@ public class DownloadForegroundService extends Service { ...@@ -231,6 +235,7 @@ public class DownloadForegroundService extends Service {
@VisibleForTesting @VisibleForTesting
void startForegroundInternal(int notificationId, Notification notification) { void startForegroundInternal(int notificationId, Notification notification) {
Log.w(TAG, "startForegroundInternal id: " + notificationId); Log.w(TAG, "startForegroundInternal id: " + notificationId);
mStartForeground = true;
ForegroundServiceUtils.getInstance().startForeground( ForegroundServiceUtils.getInstance().startForeground(
this, notificationId, notification, 0 /* foregroundServiceType */); this, notificationId, notification, 0 /* foregroundServiceType */);
} }
...@@ -238,6 +243,10 @@ public class DownloadForegroundService extends Service { ...@@ -238,6 +243,10 @@ public class DownloadForegroundService extends Service {
@VisibleForTesting @VisibleForTesting
void stopForegroundInternal(int flags) { void stopForegroundInternal(int flags) {
Log.w(TAG, "stopForegroundInternal flags: " + flags); Log.w(TAG, "stopForegroundInternal flags: " + flags);
if (!mStartForeground) {
Log.e(TAG, "stopForeground is called without startForeground.");
return;
}
ForegroundServiceUtils.getInstance().stopForeground(this, flags); ForegroundServiceUtils.getInstance().stopForeground(this, flags);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment