From 6edf9c31bfff4e40bc299c39f5e12e16fc2888ea Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 16 Sep 2021 22:20:04 +0100 Subject: [PATCH] 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 --- .../android/exoplayer2/offline/DownloadService.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 200e2decda..0bccbb31b3 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 @@ -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)) {