Commit ad7bcab4 by olly Committed by Oliver Woodman

DownloadService: No-op cleanup

Issue: #6798
PiperOrigin-RevId: 289424582
parent 3c5c43c9
...@@ -34,6 +34,8 @@ import com.google.android.exoplayer2.util.NotificationUtil; ...@@ -34,6 +34,8 @@ import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.RequiresNonNull;
/** A {@link Service} for downloading media. */ /** A {@link Service} for downloading media. */
public abstract class DownloadService extends Service { public abstract class DownloadService extends Service {
...@@ -166,17 +168,18 @@ public abstract class DownloadService extends Service { ...@@ -166,17 +168,18 @@ public abstract class DownloadService extends Service {
private static final String TAG = "DownloadService"; private static final String TAG = "DownloadService";
// Keep DownloadManagerListeners for each DownloadService as long as there are downloads (and the // Keep a DownloadManagerHelper for each DownloadService as long as the process is running. The
// process is running). This allows DownloadService to restart when there's no scheduler. // helper is needed to restart the DownloadService when there's no scheduler. Even when there is a
// scheduler, the DownloadManagerHelper is typically able to restart the DownloadService faster.
private static final HashMap<Class<? extends DownloadService>, DownloadManagerHelper> private static final HashMap<Class<? extends DownloadService>, DownloadManagerHelper>
downloadManagerListeners = new HashMap<>(); downloadManagerHelpers = new HashMap<>();
@Nullable private final ForegroundNotificationUpdater foregroundNotificationUpdater; @Nullable private final ForegroundNotificationUpdater foregroundNotificationUpdater;
@Nullable private final String channelId; @Nullable private final String channelId;
@StringRes private final int channelNameResourceId; @StringRes private final int channelNameResourceId;
@StringRes private final int channelDescriptionResourceId; @StringRes private final int channelDescriptionResourceId;
@Nullable private DownloadManager downloadManager; @MonotonicNonNull private DownloadManager downloadManager;
private int lastStartId; private int lastStartId;
private boolean startedInForeground; private boolean startedInForeground;
private boolean taskRemoved; private boolean taskRemoved;
...@@ -575,16 +578,17 @@ public abstract class DownloadService extends Service { ...@@ -575,16 +578,17 @@ public abstract class DownloadService extends Service {
NotificationUtil.IMPORTANCE_LOW); NotificationUtil.IMPORTANCE_LOW);
} }
Class<? extends DownloadService> clazz = getClass(); Class<? extends DownloadService> clazz = getClass();
DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(clazz); DownloadManagerHelper downloadManagerHelper = downloadManagerHelpers.get(clazz);
if (downloadManagerHelper == null) { if (downloadManagerHelper == null) {
DownloadManager downloadManager = getDownloadManager(); downloadManager = getDownloadManager();
downloadManager.resumeDownloads(); downloadManager.resumeDownloads();
downloadManagerHelper = downloadManagerHelper =
new DownloadManagerHelper( new DownloadManagerHelper(
getApplicationContext(), downloadManager, getScheduler(), clazz); getApplicationContext(), downloadManager, getScheduler(), clazz);
downloadManagerListeners.put(clazz, downloadManagerHelper); downloadManagerHelpers.put(clazz, downloadManagerHelper);
} else {
downloadManager = downloadManagerHelper.downloadManager;
} }
downloadManager = downloadManagerHelper.downloadManager;
downloadManagerHelper.attachService(this); downloadManagerHelper.attachService(this);
} }
...@@ -671,9 +675,8 @@ public abstract class DownloadService extends Service { ...@@ -671,9 +675,8 @@ public abstract class DownloadService extends Service {
public void onDestroy() { public void onDestroy() {
isDestroyed = true; isDestroyed = true;
DownloadManagerHelper downloadManagerHelper = DownloadManagerHelper downloadManagerHelper =
Assertions.checkNotNull(downloadManagerListeners.get(getClass())); Assertions.checkNotNull(downloadManagerHelpers.get(getClass()));
boolean unschedule = !downloadManagerHelper.downloadManager.isWaitingForRequirements(); downloadManagerHelper.detachService(this);
downloadManagerHelper.detachService(this, unschedule);
if (foregroundNotificationUpdater != null) { if (foregroundNotificationUpdater != null) {
foregroundNotificationUpdater.stopPeriodicUpdates(); foregroundNotificationUpdater.stopPeriodicUpdates();
} }
...@@ -872,10 +875,9 @@ public abstract class DownloadService extends Service { ...@@ -872,10 +875,9 @@ public abstract class DownloadService extends Service {
this.scheduler = scheduler; this.scheduler = scheduler;
this.serviceClass = serviceClass; this.serviceClass = serviceClass;
downloadManager.addListener(this); downloadManager.addListener(this);
if (scheduler != null) { if (this.scheduler != null) {
Requirements requirements = downloadManager.getRequirements(); Requirements requirements = downloadManager.getRequirements();
setSchedulerEnabled( setSchedulerEnabled(/* enabled= */ !requirements.checkRequirements(context), requirements);
scheduler, /* enabled= */ !requirements.checkRequirements(context), requirements);
} }
} }
...@@ -884,10 +886,10 @@ public abstract class DownloadService extends Service { ...@@ -884,10 +886,10 @@ public abstract class DownloadService extends Service {
this.downloadService = downloadService; this.downloadService = downloadService;
} }
public void detachService(DownloadService downloadService, boolean unschedule) { public void detachService(DownloadService downloadService) {
Assertions.checkState(this.downloadService == downloadService); Assertions.checkState(this.downloadService == downloadService);
this.downloadService = null; this.downloadService = null;
if (scheduler != null && unschedule) { if (scheduler != null && !downloadManager.isWaitingForRequirements()) {
scheduler.cancel(); scheduler.cancel();
} }
} }
...@@ -929,12 +931,12 @@ public abstract class DownloadService extends Service { ...@@ -929,12 +931,12 @@ public abstract class DownloadService extends Service {
} }
} }
if (scheduler != null) { if (scheduler != null) {
setSchedulerEnabled(scheduler, /* enabled= */ !requirementsMet, requirements); setSchedulerEnabled(/* enabled= */ !requirementsMet, requirements);
} }
} }
private void setSchedulerEnabled( @RequiresNonNull("scheduler")
Scheduler scheduler, boolean enabled, Requirements requirements) { private void setSchedulerEnabled(boolean enabled, Requirements requirements) {
if (!enabled) { if (!enabled) {
scheduler.cancel(); scheduler.cancel();
} else { } else {
......
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