mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
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
This commit is contained in:
parent
e60de62588
commit
18d208ab93
2 changed files with 35 additions and 8 deletions
|
|
@ -90,6 +90,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}.
|
||||||
|
|
@ -221,12 +222,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:
|
||||||
|
|
@ -266,6 +272,12 @@ public abstract class DownloadService extends Service {
|
||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTaskRemoved(Intent rootIntent) {
|
||||||
|
logd("onTaskRemoved rootIntent: " + rootIntent);
|
||||||
|
taskRemoved = true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
logd("onDestroy");
|
logd("onDestroy");
|
||||||
|
|
@ -363,8 +375,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 {
|
||||||
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 {
|
||||||
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);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue