From 2f6273c9fcc2fcafe49a6404ab658042e13dfe17 Mon Sep 17 00:00:00 2001 From: eguven Date: Tue, 10 Jul 2018 05:00:34 -0700 Subject: [PATCH] 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 --- .../exoplayer2/offline/DownloadService.java | 21 ++++++++++++++++-- .../ui/DownloadNotificationUtil.java | 22 ++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java index 6dae3f70b3..995e71a94a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java @@ -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}. @@ -219,12 +220,17 @@ public abstract class DownloadService extends Service { @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: @@ -260,6 +266,12 @@ public abstract class DownloadService extends Service { return START_STICKY; } + @Override + public void onTaskRemoved(Intent rootIntent) { + logd("onTaskRemoved rootIntent: " + rootIntent); + taskRemoved = true; + } + @Override public void onDestroy() { logd("onDestroy"); @@ -353,8 +365,13 @@ public abstract class DownloadService extends Service { 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) { diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationUtil.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationUtil.java index 0a841fa38f..97832abfc7 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationUtil.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationUtil.java @@ -55,10 +55,18 @@ public final class DownloadNotificationUtil { 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; @@ -67,18 +75,20 @@ public final class DownloadNotificationUtil { 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);