Commit 2f6273c9 by eguven Committed by Oliver Woodman

Fix DownloadService doesn't stop when the app is killed

Also fixed showing "remove notification" when download is completed.

Issue:#4469
Issue:#4488

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=203927268
parent 44c45fd1
...@@ -86,6 +86,7 @@ public abstract class DownloadService extends Service { ...@@ -86,6 +86,7 @@ public abstract class DownloadService extends Service {
private DownloadManagerListener downloadManagerListener; private DownloadManagerListener downloadManagerListener;
private int lastStartId; private int lastStartId;
private boolean startedInForeground; private boolean startedInForeground;
private boolean taskRemoved;
/** /**
* Creates a DownloadService with {@link #DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL}. * Creates a DownloadService with {@link #DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL}.
...@@ -219,12 +220,17 @@ public abstract class DownloadService extends Service { ...@@ -219,12 +220,17 @@ public abstract class DownloadService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
lastStartId = startId; lastStartId = startId;
taskRemoved = false;
String intentAction = null; String intentAction = null;
if (intent != null) { if (intent != null) {
intentAction = intent.getAction(); intentAction = intent.getAction();
startedInForeground |= startedInForeground |=
intent.getBooleanExtra(KEY_FOREGROUND, false) || ACTION_RESTART.equals(intentAction); intent.getBooleanExtra(KEY_FOREGROUND, false) || ACTION_RESTART.equals(intentAction);
} }
// intentAction is null if the service is restarted or no action is specified.
if (intentAction == null) {
intentAction = ACTION_INIT;
}
logd("onStartCommand action: " + intentAction + " startId: " + startId); logd("onStartCommand action: " + intentAction + " startId: " + startId);
switch (intentAction) { switch (intentAction) {
case ACTION_INIT: case ACTION_INIT:
...@@ -261,6 +267,12 @@ public abstract class DownloadService extends Service { ...@@ -261,6 +267,12 @@ public abstract class DownloadService extends Service {
} }
@Override @Override
public void onTaskRemoved(Intent rootIntent) {
logd("onTaskRemoved rootIntent: " + rootIntent);
taskRemoved = true;
}
@Override
public void onDestroy() { public void onDestroy() {
logd("onDestroy"); logd("onDestroy");
foregroundNotificationUpdater.stopPeriodicUpdates(); foregroundNotificationUpdater.stopPeriodicUpdates();
...@@ -353,8 +365,13 @@ public abstract class DownloadService extends Service { ...@@ -353,8 +365,13 @@ public abstract class DownloadService extends Service {
if (startedInForeground && Util.SDK_INT >= 26) { if (startedInForeground && Util.SDK_INT >= 26) {
foregroundNotificationUpdater.showNotificationIfNotAlready(); foregroundNotificationUpdater.showNotificationIfNotAlready();
} }
boolean stopSelfResult = stopSelfResult(lastStartId); if (Util.SDK_INT < 28 && taskRemoved) { // See [Internal: b/74248644].
logd("stopSelf(" + lastStartId + ") result: " + stopSelfResult); stopSelf();
logd("stopSelf()");
} else {
boolean stopSelfResult = stopSelfResult(lastStartId);
logd("stopSelf(" + lastStartId + ") result: " + stopSelfResult);
}
} }
private void logd(String message) { private void logd(String message) {
......
...@@ -55,10 +55,18 @@ public final class DownloadNotificationUtil { ...@@ -55,10 +55,18 @@ public final class DownloadNotificationUtil {
int downloadTaskCount = 0; int downloadTaskCount = 0;
boolean allDownloadPercentagesUnknown = true; boolean allDownloadPercentagesUnknown = true;
boolean haveDownloadedBytes = false; boolean haveDownloadedBytes = false;
boolean haveDownloadTasks = false;
boolean haveRemoveTasks = false;
for (TaskState taskState : taskStates) { for (TaskState taskState : taskStates) {
if (taskState.action.isRemoveAction || taskState.state != TaskState.STATE_STARTED) { if (taskState.state != TaskState.STATE_STARTED
&& taskState.state != TaskState.STATE_COMPLETED) {
continue; continue;
} }
if (taskState.action.isRemoveAction) {
haveRemoveTasks = true;
continue;
}
haveDownloadTasks = true;
if (taskState.downloadPercentage != C.PERCENTAGE_UNSET) { if (taskState.downloadPercentage != C.PERCENTAGE_UNSET) {
allDownloadPercentagesUnknown = false; allDownloadPercentagesUnknown = false;
totalPercentage += taskState.downloadPercentage; totalPercentage += taskState.downloadPercentage;
...@@ -67,18 +75,20 @@ public final class DownloadNotificationUtil { ...@@ -67,18 +75,20 @@ public final class DownloadNotificationUtil {
downloadTaskCount++; downloadTaskCount++;
} }
boolean haveDownloadTasks = downloadTaskCount > 0;
int titleStringId = int titleStringId =
haveDownloadTasks haveDownloadTasks
? R.string.exo_download_downloading ? R.string.exo_download_downloading
: (taskStates.length > 0 ? R.string.exo_download_removing : NULL_STRING_ID); : (haveRemoveTasks ? R.string.exo_download_removing : NULL_STRING_ID);
NotificationCompat.Builder notificationBuilder = NotificationCompat.Builder notificationBuilder =
newNotificationBuilder( newNotificationBuilder(
context, smallIcon, channelId, contentIntent, message, titleStringId); context, smallIcon, channelId, contentIntent, message, titleStringId);
int progress = haveDownloadTasks ? (int) (totalPercentage / downloadTaskCount) : 0; int progress = 0;
boolean indeterminate = boolean indeterminate = true;
!haveDownloadTasks || (allDownloadPercentagesUnknown && haveDownloadedBytes); if (haveDownloadTasks) {
progress = (int) (totalPercentage / downloadTaskCount);
indeterminate = allDownloadPercentagesUnknown && haveDownloadedBytes;
}
notificationBuilder.setProgress(/* max= */ 100, progress, indeterminate); notificationBuilder.setProgress(/* max= */ 100, progress, indeterminate);
notificationBuilder.setOngoing(true); notificationBuilder.setOngoing(true);
notificationBuilder.setShowWhen(false); notificationBuilder.setShowWhen(false);
......
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 sign in to comment