diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java index 0e2c5874b1..b3f1d3da6a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java @@ -262,12 +262,23 @@ public final class DownloadManager { return task.id; } - /** Returns the current number of tasks. */ + /** Returns the number of tasks. */ public int getTaskCount() { Assertions.checkState(!released); return tasks.size(); } + /** Returns the number of download tasks. */ + public int getDownloadCount() { + int count = 0; + for (int i = 0; i < tasks.size(); i++) { + if (!tasks.get(i).action.isRemoveAction) { + count++; + } + } + return count; + } + /** Returns the state of a task, or null if no such task exists */ public @Nullable TaskState getTaskState(int taskId) { Assertions.checkState(!released); 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 908aae481a..21fd541b52 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 @@ -187,17 +187,6 @@ public abstract class DownloadService extends Service { downloadManager = getDownloadManager(); downloadManagerListener = new DownloadManagerListener(); downloadManager.addListener(downloadManagerListener); - - RequirementsHelper requirementsHelper; - synchronized (requirementsHelpers) { - Class clazz = getClass(); - requirementsHelper = requirementsHelpers.get(clazz); - if (requirementsHelper == null) { - requirementsHelper = new RequirementsHelper(this, getRequirements(), getScheduler(), clazz); - requirementsHelpers.put(clazz, requirementsHelper); - } - } - requirementsHelper.start(); } @Override @@ -237,6 +226,7 @@ public abstract class DownloadService extends Service { Log.e(TAG, "Ignoring unrecognized action: " + intentAction); break; } + maybeStartWatchingRequirements(); if (downloadManager.isIdle()) { stop(); } @@ -248,14 +238,7 @@ public abstract class DownloadService extends Service { logd("onDestroy"); foregroundNotificationUpdater.stopPeriodicUpdates(); downloadManager.removeListener(downloadManagerListener); - if (downloadManager.getTaskCount() == 0) { - synchronized (requirementsHelpers) { - RequirementsHelper requirementsHelper = requirementsHelpers.remove(getClass()); - if (requirementsHelper != null) { - requirementsHelper.stop(); - } - } - } + maybeStopWatchingRequirements(); } @Nullable @@ -312,6 +295,31 @@ public abstract class DownloadService extends Service { // Do nothing. } + private void maybeStartWatchingRequirements() { + if (downloadManager.getDownloadCount() == 0) { + return; + } + Class clazz = getClass(); + RequirementsHelper requirementsHelper = requirementsHelpers.get(clazz); + if (requirementsHelper == null) { + requirementsHelper = new RequirementsHelper(this, getRequirements(), getScheduler(), clazz); + requirementsHelpers.put(clazz, requirementsHelper); + requirementsHelper.start(); + logd("started watching requirements"); + } + } + + private void maybeStopWatchingRequirements() { + if (downloadManager.getDownloadCount() > 0) { + return; + } + RequirementsHelper requirementsHelper = requirementsHelpers.remove(getClass()); + if (requirementsHelper != null) { + requirementsHelper.stop(); + logd("stopped watching requirements"); + } + } + private void stop() { foregroundNotificationUpdater.stopPeriodicUpdates(); // Make sure startForeground is called before stopping. Workaround for [Internal: b/69424260]. @@ -331,7 +339,7 @@ public abstract class DownloadService extends Service { private final class DownloadManagerListener implements DownloadManager.Listener { @Override public void onInitialized(DownloadManager downloadManager) { - // Do nothing. + maybeStartWatchingRequirements(); } @Override