mirror of
https://github.com/androidx/media.git
synced 2025-05-05 14:40:50 +08:00
Avoid starting RequirementsWatcher if there is no download task
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=198860680
This commit is contained in:
parent
928cbfa7bc
commit
3670111310
@ -262,12 +262,23 @@ public final class DownloadManager {
|
|||||||
return task.id;
|
return task.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the current number of tasks. */
|
/** Returns the number of tasks. */
|
||||||
public int getTaskCount() {
|
public int getTaskCount() {
|
||||||
Assertions.checkState(!released);
|
Assertions.checkState(!released);
|
||||||
return tasks.size();
|
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 */
|
/** Returns the state of a task, or null if no such task exists */
|
||||||
public @Nullable TaskState getTaskState(int taskId) {
|
public @Nullable TaskState getTaskState(int taskId) {
|
||||||
Assertions.checkState(!released);
|
Assertions.checkState(!released);
|
||||||
|
@ -187,17 +187,6 @@ public abstract class DownloadService extends Service {
|
|||||||
downloadManager = getDownloadManager();
|
downloadManager = getDownloadManager();
|
||||||
downloadManagerListener = new DownloadManagerListener();
|
downloadManagerListener = new DownloadManagerListener();
|
||||||
downloadManager.addListener(downloadManagerListener);
|
downloadManager.addListener(downloadManagerListener);
|
||||||
|
|
||||||
RequirementsHelper requirementsHelper;
|
|
||||||
synchronized (requirementsHelpers) {
|
|
||||||
Class<? extends DownloadService> clazz = getClass();
|
|
||||||
requirementsHelper = requirementsHelpers.get(clazz);
|
|
||||||
if (requirementsHelper == null) {
|
|
||||||
requirementsHelper = new RequirementsHelper(this, getRequirements(), getScheduler(), clazz);
|
|
||||||
requirementsHelpers.put(clazz, requirementsHelper);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
requirementsHelper.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -237,6 +226,7 @@ public abstract class DownloadService extends Service {
|
|||||||
Log.e(TAG, "Ignoring unrecognized action: " + intentAction);
|
Log.e(TAG, "Ignoring unrecognized action: " + intentAction);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
maybeStartWatchingRequirements();
|
||||||
if (downloadManager.isIdle()) {
|
if (downloadManager.isIdle()) {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
@ -248,14 +238,7 @@ public abstract class DownloadService extends Service {
|
|||||||
logd("onDestroy");
|
logd("onDestroy");
|
||||||
foregroundNotificationUpdater.stopPeriodicUpdates();
|
foregroundNotificationUpdater.stopPeriodicUpdates();
|
||||||
downloadManager.removeListener(downloadManagerListener);
|
downloadManager.removeListener(downloadManagerListener);
|
||||||
if (downloadManager.getTaskCount() == 0) {
|
maybeStopWatchingRequirements();
|
||||||
synchronized (requirementsHelpers) {
|
|
||||||
RequirementsHelper requirementsHelper = requirementsHelpers.remove(getClass());
|
|
||||||
if (requirementsHelper != null) {
|
|
||||||
requirementsHelper.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -312,6 +295,31 @@ public abstract class DownloadService extends Service {
|
|||||||
// Do nothing.
|
// Do nothing.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void maybeStartWatchingRequirements() {
|
||||||
|
if (downloadManager.getDownloadCount() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Class<? extends DownloadService> 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() {
|
private void stop() {
|
||||||
foregroundNotificationUpdater.stopPeriodicUpdates();
|
foregroundNotificationUpdater.stopPeriodicUpdates();
|
||||||
// Make sure startForeground is called before stopping. Workaround for [Internal: b/69424260].
|
// 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 {
|
private final class DownloadManagerListener implements DownloadManager.Listener {
|
||||||
@Override
|
@Override
|
||||||
public void onInitialized(DownloadManager downloadManager) {
|
public void onInitialized(DownloadManager downloadManager) {
|
||||||
// Do nothing.
|
maybeStartWatchingRequirements();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user