Skip to content

Commit

Permalink
Fix DownloadService doesn't stop when the app is killed
Browse files Browse the repository at this point in the history
Also fixed showing "remove notification" when download is completed.

Issue:#4469
Issue:#4488

-------------
Created by MOE: https:/google/moe
MOE_MIGRATED_REVID=203927268
  • Loading branch information
erdemguven authored and ojw28 committed Jul 23, 2018
1 parent 44c45fd commit 2f6273c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public abstract class DownloadService extends Service {
private DownloadManagerListener downloadManagerListener;
private int lastStartId;
private boolean startedInForeground;
private boolean taskRemoved;

/**
* Creates a DownloadService with {@link #DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL}.
Expand Down Expand Up @@ -219,12 +220,17 @@ public void onCreate() {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
lastStartId = startId;
taskRemoved = false;
String intentAction = null;
if (intent != null) {
intentAction = intent.getAction();
startedInForeground |=
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);
switch (intentAction) {
case ACTION_INIT:
Expand Down Expand Up @@ -260,6 +266,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
logd("onTaskRemoved rootIntent: " + rootIntent);
taskRemoved = true;
}

@Override
public void onDestroy() {
logd("onDestroy");
Expand Down Expand Up @@ -353,8 +365,13 @@ private void stop() {
if (startedInForeground && Util.SDK_INT >= 26) {
foregroundNotificationUpdater.showNotificationIfNotAlready();
}
boolean stopSelfResult = stopSelfResult(lastStartId);
logd("stopSelf(" + lastStartId + ") result: " + stopSelfResult);
if (Util.SDK_INT < 28 && taskRemoved) { // See [Internal: b/74248644].
stopSelf();
logd("stopSelf()");
} else {
boolean stopSelfResult = stopSelfResult(lastStartId);
logd("stopSelf(" + lastStartId + ") result: " + stopSelfResult);
}
}

private void logd(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ public static Notification buildProgressNotification(
int downloadTaskCount = 0;
boolean allDownloadPercentagesUnknown = true;
boolean haveDownloadedBytes = false;
boolean haveDownloadTasks = false;
boolean haveRemoveTasks = false;
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;
}
if (taskState.action.isRemoveAction) {
haveRemoveTasks = true;
continue;
}
haveDownloadTasks = true;
if (taskState.downloadPercentage != C.PERCENTAGE_UNSET) {
allDownloadPercentagesUnknown = false;
totalPercentage += taskState.downloadPercentage;
Expand All @@ -67,18 +75,20 @@ public static Notification buildProgressNotification(
downloadTaskCount++;
}

boolean haveDownloadTasks = downloadTaskCount > 0;
int titleStringId =
haveDownloadTasks
? 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 =
newNotificationBuilder(
context, smallIcon, channelId, contentIntent, message, titleStringId);

int progress = haveDownloadTasks ? (int) (totalPercentage / downloadTaskCount) : 0;
boolean indeterminate =
!haveDownloadTasks || (allDownloadPercentagesUnknown && haveDownloadedBytes);
int progress = 0;
boolean indeterminate = true;
if (haveDownloadTasks) {
progress = (int) (totalPercentage / downloadTaskCount);
indeterminate = allDownloadPercentagesUnknown && haveDownloadedBytes;
}
notificationBuilder.setProgress(/* max= */ 100, progress, indeterminate);
notificationBuilder.setOngoing(true);
notificationBuilder.setShowWhen(false);
Expand Down

0 comments on commit 2f6273c

Please sign in to comment.