DownloadService: Only call getScheduler once

The second getScheduler() call violates the documentation of
the class, which states that getScheduler() is not called if
foregroundNotificationId if FOREGROUND_NOTIFICATION_ID_NONE.
Presumably implementing subclasses would return null, in which
case this didn't do any harm, but we should make sure the
implementation behaves as documented regardless.

PiperOrigin-RevId: 397167603
This commit is contained in:
olly 2021-09-16 22:20:04 +01:00 committed by bachinger
parent a95ee0aeba
commit 6edf9c31bf

View File

@ -178,6 +178,7 @@ public abstract class DownloadService extends Service {
@StringRes private final int channelNameResourceId;
@StringRes private final int channelDescriptionResourceId;
private @MonotonicNonNull Scheduler scheduler;
private @MonotonicNonNull DownloadManager downloadManager;
private int lastStartId;
private boolean startedInForeground;
@ -582,6 +583,9 @@ public abstract class DownloadService extends Service {
if (downloadManagerHelper == null) {
boolean foregroundAllowed = foregroundNotificationUpdater != null;
@Nullable Scheduler scheduler = foregroundAllowed ? getScheduler() : null;
if (scheduler != null) {
this.scheduler = scheduler;
}
downloadManager = getDownloadManager();
downloadManager.resumeDownloads();
downloadManagerHelper =
@ -589,6 +593,9 @@ public abstract class DownloadService extends Service {
getApplicationContext(), downloadManager, foregroundAllowed, scheduler, clazz);
downloadManagerHelpers.put(clazz, downloadManagerHelper);
} else {
if (downloadManagerHelper.scheduler != null) {
scheduler = downloadManagerHelper.scheduler;
}
downloadManager = downloadManagerHelper.downloadManager;
}
downloadManagerHelper.attachService(this);
@ -658,7 +665,6 @@ public abstract class DownloadService extends Service {
if (requirements == null) {
Log.e(TAG, "Ignored SET_REQUIREMENTS: Missing " + KEY_REQUIREMENTS + " extra");
} else {
@Nullable Scheduler scheduler = getScheduler();
if (scheduler != null) {
Requirements supportedRequirements = scheduler.getSupportedRequirements(requirements);
if (!supportedRequirements.equals(requirements)) {