DownloadManager improvements

- Do requirements TODO
- Add useful helper method to retrieve not met requirements
- Fix WritableDownloadIndex Javadoc

PiperOrigin-RevId: 245922903
This commit is contained in:
olly 2019-04-30 12:25:04 +01:00 committed by Oliver Woodman
parent 6b34ade908
commit 4a5b8e17de
2 changed files with 31 additions and 20 deletions

View File

@ -173,6 +173,7 @@ public final class DownloadManager {
private boolean downloadsPaused; private boolean downloadsPaused;
private int maxParallelDownloads; private int maxParallelDownloads;
private int minRetryCount; private int minRetryCount;
private int notMetRequirements;
private RequirementsWatcher requirementsWatcher; private RequirementsWatcher requirementsWatcher;
/** /**
@ -212,7 +213,7 @@ public final class DownloadManager {
requirementsListener = this::onRequirementsStateChanged; requirementsListener = this::onRequirementsStateChanged;
requirementsWatcher = requirementsWatcher =
new RequirementsWatcher(context, requirementsListener, DEFAULT_REQUIREMENTS); new RequirementsWatcher(context, requirementsListener, DEFAULT_REQUIREMENTS);
int notMetRequirements = requirementsWatcher.start(); notMetRequirements = requirementsWatcher.start();
mainHandler = new Handler(Util.getLooper(), this::handleMainMessage); mainHandler = new Handler(Util.getLooper(), this::handleMainMessage);
HandlerThread internalThread = new HandlerThread("DownloadManager file i/o"); HandlerThread internalThread = new HandlerThread("DownloadManager file i/o");
@ -274,11 +275,21 @@ public final class DownloadManager {
listeners.remove(listener); listeners.remove(listener);
} }
/** Returns the requirements needed to be met to start downloads. */ /** Returns the requirements needed to be met to progress. */
public Requirements getRequirements() { public Requirements getRequirements() {
return requirementsWatcher.getRequirements(); return requirementsWatcher.getRequirements();
} }
/**
* Returns the requirements needed for downloads to progress that are not currently met.
*
* @return The not met {@link Requirements.RequirementFlags}, or 0 if all requirements are met.
*/
@Requirements.RequirementFlags
public int getNotMetRequirements() {
return getRequirements().getNotMetRequirements(context);
}
/** /**
* Sets the requirements that need to be met for downloads to progress. * Sets the requirements that need to be met for downloads to progress.
* *
@ -413,7 +424,7 @@ public final class DownloadManager {
* @param request The download request. * @param request The download request.
*/ */
public void addDownload(DownloadRequest request) { public void addDownload(DownloadRequest request) {
addDownload(request, Download.STOP_REASON_NONE); addDownload(request, STOP_REASON_NONE);
} }
/** /**
@ -478,6 +489,10 @@ public final class DownloadManager {
for (Listener listener : listeners) { for (Listener listener : listeners) {
listener.onRequirementsStateChanged(this, requirements, notMetRequirements); listener.onRequirementsStateChanged(this, requirements, notMetRequirements);
} }
if (this.notMetRequirements == notMetRequirements) {
return;
}
this.notMetRequirements = notMetRequirements;
pendingMessages++; pendingMessages++;
internalHandler internalHandler
.obtainMessage(MSG_SET_NOT_MET_REQUIREMENTS, notMetRequirements, /* unused */ 0) .obtainMessage(MSG_SET_NOT_MET_REQUIREMENTS, notMetRequirements, /* unused */ 0)
@ -747,10 +762,6 @@ public final class DownloadManager {
} }
private void setNotMetRequirements(@Requirements.RequirementFlags int notMetRequirements) { private void setNotMetRequirements(@Requirements.RequirementFlags int notMetRequirements) {
// TODO: Move this deduplication check to the main thread.
if (this.notMetRequirements == notMetRequirements) {
return;
}
this.notMetRequirements = notMetRequirements; this.notMetRequirements = notMetRequirements;
logdFlags("Not met requirements are changed", notMetRequirements); logdFlags("Not met requirements are changed", notMetRequirements);
for (int i = 0; i < downloadInternals.size(); i++) { for (int i = 0; i < downloadInternals.size(); i++) {

View File

@ -17,43 +17,43 @@ package com.google.android.exoplayer2.offline;
import java.io.IOException; import java.io.IOException;
/** An writable index of {@link Download Downloads}. */ /** A writable index of {@link Download Downloads}. */
public interface WritableDownloadIndex extends DownloadIndex { public interface WritableDownloadIndex extends DownloadIndex {
/** /**
* Adds or replaces a {@link Download}. * Adds or replaces a {@link Download}.
* *
* @param download The {@link Download} to be added. * @param download The {@link Download} to be added.
* @throws throws IOException If an error occurs setting the state. * @throws IOException If an error occurs setting the state.
*/ */
void putDownload(Download download) throws IOException; void putDownload(Download download) throws IOException;
/** /**
* Removes the {@link Download} with the given {@code id}. * Removes the download with the given ID. Does nothing if a download with the given ID does not
* exist.
* *
* @param id ID of a {@link Download}. * @param id The ID of the download to remove.
* @throws throws IOException If an error occurs removing the state. * @throws IOException If an error occurs removing the state.
*/ */
void removeDownload(String id) throws IOException; void removeDownload(String id) throws IOException;
/** /**
* Sets the stop reason of the downloads in a terminal state ({@link Download#STATE_COMPLETED}, * Sets the stop reason of the downloads in a terminal state ({@link Download#STATE_COMPLETED},
* {@link Download#STATE_FAILED}). * {@link Download#STATE_FAILED}).
* *
* @param stopReason The stop reason. * @param stopReason The stop reason.
* @throws throws IOException If an error occurs updating the state. * @throws IOException If an error occurs updating the state.
*/ */
void setStopReason(int stopReason) throws IOException; void setStopReason(int stopReason) throws IOException;
/** /**
* Sets the stop reason of the download with the given {@code id} in a terminal state ({@link * Sets the stop reason of the download with the given ID in a terminal state ({@link
* Download#STATE_COMPLETED}, {@link Download#STATE_FAILED}). * Download#STATE_COMPLETED}, {@link Download#STATE_FAILED}). Does nothing if a download with the
* given ID does not exist, or if it's not in a terminal state.
* *
* <p>If there's no {@link Download} with the given {@code id} or it isn't in a terminal state, * @param id The ID of the download to update.
* then nothing happens.
*
* @param id ID of a {@link Download}.
* @param stopReason The stop reason. * @param stopReason The stop reason.
* @throws throws IOException If an error occurs updating the state. * @throws IOException If an error occurs updating the state.
*/ */
void setStopReason(String id, int stopReason) throws IOException; void setStopReason(String id, int stopReason) throws IOException;
} }