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 {
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})
@Retention(RetentionPolicy.SOURCE)
public @interface StopForegroundNotification {
......@@ -231,6 +235,7 @@ public class DownloadForegroundService extends Service {
@VisibleForTesting
void startForegroundInternal(int notificationId, Notification notification) {
Log.w(TAG, "startForegroundInternal id: " + notificationId);
mStartForeground = true;
ForegroundServiceUtils.getInstance().startForeground(
this, notificationId, notification, 0 /* foregroundServiceType */);
}
......@@ -238,6 +243,10 @@ public class DownloadForegroundService extends Service {
@VisibleForTesting
void stopForegroundInternal(int flags) {
Log.w(TAG, "stopForegroundInternal flags: " + flags);
if (!mStartForeground) {
Log.e(TAG, "stopForeground is called without startForeground.");
return;
}
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