mirror of
https://github.com/androidx/media.git
synced 2025-05-04 06:00:37 +08:00
Remove DownloadState.stopFlags
PiperOrigin-RevId: 238442845
This commit is contained in:
parent
f566c9486c
commit
d088be7acc
@ -53,7 +53,6 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||||||
private static final String COLUMN_DOWNLOADED_BYTES = "downloaded_bytes";
|
private static final String COLUMN_DOWNLOADED_BYTES = "downloaded_bytes";
|
||||||
private static final String COLUMN_TOTAL_BYTES = "total_bytes";
|
private static final String COLUMN_TOTAL_BYTES = "total_bytes";
|
||||||
private static final String COLUMN_FAILURE_REASON = "failure_reason";
|
private static final String COLUMN_FAILURE_REASON = "failure_reason";
|
||||||
private static final String COLUMN_STOP_FLAGS = "stop_flags";
|
|
||||||
private static final String COLUMN_NOT_MET_REQUIREMENTS = "not_met_requirements";
|
private static final String COLUMN_NOT_MET_REQUIREMENTS = "not_met_requirements";
|
||||||
private static final String COLUMN_MANUAL_STOP_REASON = "manual_stop_reason";
|
private static final String COLUMN_MANUAL_STOP_REASON = "manual_stop_reason";
|
||||||
private static final String COLUMN_START_TIME_MS = "start_time_ms";
|
private static final String COLUMN_START_TIME_MS = "start_time_ms";
|
||||||
@ -61,6 +60,10 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||||||
private static final String COLUMN_STREAM_KEYS = "stream_keys";
|
private static final String COLUMN_STREAM_KEYS = "stream_keys";
|
||||||
private static final String COLUMN_CUSTOM_METADATA = "custom_metadata";
|
private static final String COLUMN_CUSTOM_METADATA = "custom_metadata";
|
||||||
|
|
||||||
|
@SuppressWarnings("DeprecatedIsStillUsed")
|
||||||
|
@Deprecated
|
||||||
|
private static final String COLUMN_STOP_FLAGS = "stop_flags";
|
||||||
|
|
||||||
private static final int COLUMN_INDEX_ID = 0;
|
private static final int COLUMN_INDEX_ID = 0;
|
||||||
private static final int COLUMN_INDEX_TYPE = 1;
|
private static final int COLUMN_INDEX_TYPE = 1;
|
||||||
private static final int COLUMN_INDEX_URI = 2;
|
private static final int COLUMN_INDEX_URI = 2;
|
||||||
@ -70,13 +73,12 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||||||
private static final int COLUMN_INDEX_DOWNLOADED_BYTES = 6;
|
private static final int COLUMN_INDEX_DOWNLOADED_BYTES = 6;
|
||||||
private static final int COLUMN_INDEX_TOTAL_BYTES = 7;
|
private static final int COLUMN_INDEX_TOTAL_BYTES = 7;
|
||||||
private static final int COLUMN_INDEX_FAILURE_REASON = 8;
|
private static final int COLUMN_INDEX_FAILURE_REASON = 8;
|
||||||
private static final int COLUMN_INDEX_STOP_FLAGS = 9;
|
private static final int COLUMN_INDEX_NOT_MET_REQUIREMENTS = 9;
|
||||||
private static final int COLUMN_INDEX_NOT_MET_REQUIREMENTS = 10;
|
private static final int COLUMN_INDEX_MANUAL_STOP_REASON = 10;
|
||||||
private static final int COLUMN_INDEX_MANUAL_STOP_REASON = 11;
|
private static final int COLUMN_INDEX_START_TIME_MS = 11;
|
||||||
private static final int COLUMN_INDEX_START_TIME_MS = 12;
|
private static final int COLUMN_INDEX_UPDATE_TIME_MS = 12;
|
||||||
private static final int COLUMN_INDEX_UPDATE_TIME_MS = 13;
|
private static final int COLUMN_INDEX_STREAM_KEYS = 13;
|
||||||
private static final int COLUMN_INDEX_STREAM_KEYS = 14;
|
private static final int COLUMN_INDEX_CUSTOM_METADATA = 14;
|
||||||
private static final int COLUMN_INDEX_CUSTOM_METADATA = 15;
|
|
||||||
|
|
||||||
private static final String WHERE_ID_EQUALS = COLUMN_ID + " = ?";
|
private static final String WHERE_ID_EQUALS = COLUMN_ID + " = ?";
|
||||||
|
|
||||||
@ -91,7 +93,6 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||||||
COLUMN_DOWNLOADED_BYTES,
|
COLUMN_DOWNLOADED_BYTES,
|
||||||
COLUMN_TOTAL_BYTES,
|
COLUMN_TOTAL_BYTES,
|
||||||
COLUMN_FAILURE_REASON,
|
COLUMN_FAILURE_REASON,
|
||||||
COLUMN_STOP_FLAGS,
|
|
||||||
COLUMN_NOT_MET_REQUIREMENTS,
|
COLUMN_NOT_MET_REQUIREMENTS,
|
||||||
COLUMN_MANUAL_STOP_REASON,
|
COLUMN_MANUAL_STOP_REASON,
|
||||||
COLUMN_START_TIME_MS,
|
COLUMN_START_TIME_MS,
|
||||||
@ -210,7 +211,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||||||
values.put(COLUMN_DOWNLOADED_BYTES, downloadState.downloadedBytes);
|
values.put(COLUMN_DOWNLOADED_BYTES, downloadState.downloadedBytes);
|
||||||
values.put(COLUMN_TOTAL_BYTES, downloadState.totalBytes);
|
values.put(COLUMN_TOTAL_BYTES, downloadState.totalBytes);
|
||||||
values.put(COLUMN_FAILURE_REASON, downloadState.failureReason);
|
values.put(COLUMN_FAILURE_REASON, downloadState.failureReason);
|
||||||
values.put(COLUMN_STOP_FLAGS, downloadState.stopFlags);
|
values.put(COLUMN_STOP_FLAGS, /*stopFlags*/ 0);
|
||||||
values.put(COLUMN_NOT_MET_REQUIREMENTS, downloadState.notMetRequirements);
|
values.put(COLUMN_NOT_MET_REQUIREMENTS, downloadState.notMetRequirements);
|
||||||
values.put(COLUMN_MANUAL_STOP_REASON, downloadState.manualStopReason);
|
values.put(COLUMN_MANUAL_STOP_REASON, downloadState.manualStopReason);
|
||||||
values.put(COLUMN_START_TIME_MS, downloadState.startTimeMs);
|
values.put(COLUMN_START_TIME_MS, downloadState.startTimeMs);
|
||||||
@ -288,7 +289,6 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||||||
cursor.getLong(COLUMN_INDEX_DOWNLOADED_BYTES),
|
cursor.getLong(COLUMN_INDEX_DOWNLOADED_BYTES),
|
||||||
cursor.getLong(COLUMN_INDEX_TOTAL_BYTES),
|
cursor.getLong(COLUMN_INDEX_TOTAL_BYTES),
|
||||||
cursor.getInt(COLUMN_INDEX_FAILURE_REASON),
|
cursor.getInt(COLUMN_INDEX_FAILURE_REASON),
|
||||||
cursor.getInt(COLUMN_INDEX_STOP_FLAGS),
|
|
||||||
cursor.getInt(COLUMN_INDEX_NOT_MET_REQUIREMENTS),
|
cursor.getInt(COLUMN_INDEX_NOT_MET_REQUIREMENTS),
|
||||||
cursor.getInt(COLUMN_INDEX_MANUAL_STOP_REASON),
|
cursor.getInt(COLUMN_INDEX_MANUAL_STOP_REASON),
|
||||||
cursor.getLong(COLUMN_INDEX_START_TIME_MS),
|
cursor.getLong(COLUMN_INDEX_START_TIME_MS),
|
||||||
|
@ -17,6 +17,8 @@ package com.google.android.exoplayer2.offline;
|
|||||||
|
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.FAILURE_REASON_NONE;
|
import static com.google.android.exoplayer2.offline.DownloadState.FAILURE_REASON_NONE;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.FAILURE_REASON_UNKNOWN;
|
import static com.google.android.exoplayer2.offline.DownloadState.FAILURE_REASON_UNKNOWN;
|
||||||
|
import static com.google.android.exoplayer2.offline.DownloadState.MANUAL_STOP_REASON_NONE;
|
||||||
|
import static com.google.android.exoplayer2.offline.DownloadState.MANUAL_STOP_REASON_UNDEFINED;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.STATE_COMPLETED;
|
import static com.google.android.exoplayer2.offline.DownloadState.STATE_COMPLETED;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.STATE_DOWNLOADING;
|
import static com.google.android.exoplayer2.offline.DownloadState.STATE_DOWNLOADING;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.STATE_FAILED;
|
import static com.google.android.exoplayer2.offline.DownloadState.STATE_FAILED;
|
||||||
@ -25,8 +27,6 @@ import static com.google.android.exoplayer2.offline.DownloadState.STATE_REMOVED;
|
|||||||
import static com.google.android.exoplayer2.offline.DownloadState.STATE_REMOVING;
|
import static com.google.android.exoplayer2.offline.DownloadState.STATE_REMOVING;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.STATE_RESTARTING;
|
import static com.google.android.exoplayer2.offline.DownloadState.STATE_RESTARTING;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.STATE_STOPPED;
|
import static com.google.android.exoplayer2.offline.DownloadState.STATE_STOPPED;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.STOP_FLAG_MANUAL;
|
|
||||||
import static com.google.android.exoplayer2.offline.DownloadState.STOP_FLAG_REQUIREMENTS_NOT_MET;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.ConditionVariable;
|
import android.os.ConditionVariable;
|
||||||
@ -140,7 +140,6 @@ public final class DownloadManager {
|
|||||||
|
|
||||||
private boolean initialized;
|
private boolean initialized;
|
||||||
private boolean released;
|
private boolean released;
|
||||||
@DownloadState.StopFlags private int stopFlags;
|
|
||||||
@Requirements.RequirementFlags private int notMetRequirements;
|
@Requirements.RequirementFlags private int notMetRequirements;
|
||||||
private int manualStopReason;
|
private int manualStopReason;
|
||||||
private RequirementsWatcher requirementsWatcher;
|
private RequirementsWatcher requirementsWatcher;
|
||||||
@ -186,7 +185,7 @@ public final class DownloadManager {
|
|||||||
this.maxSimultaneousDownloads = maxSimultaneousDownloads;
|
this.maxSimultaneousDownloads = maxSimultaneousDownloads;
|
||||||
this.minRetryCount = minRetryCount;
|
this.minRetryCount = minRetryCount;
|
||||||
|
|
||||||
stopFlags = STOP_FLAG_MANUAL;
|
manualStopReason = MANUAL_STOP_REASON_UNDEFINED;
|
||||||
downloads = new ArrayList<>();
|
downloads = new ArrayList<>();
|
||||||
activeDownloads = new HashMap<>();
|
activeDownloads = new HashMap<>();
|
||||||
|
|
||||||
@ -203,7 +202,7 @@ public final class DownloadManager {
|
|||||||
listeners = new CopyOnWriteArraySet<>();
|
listeners = new CopyOnWriteArraySet<>();
|
||||||
actionQueue = new ArrayDeque<>();
|
actionQueue = new ArrayDeque<>();
|
||||||
|
|
||||||
setNotMetRequirements(watchRequirements(requirements));
|
notMetRequirements = watchRequirements(requirements);
|
||||||
loadActions();
|
loadActions();
|
||||||
logd("Created");
|
logd("Created");
|
||||||
}
|
}
|
||||||
@ -246,48 +245,49 @@ public final class DownloadManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears {@link DownloadState#STOP_FLAG_MANUAL} flag of all downloads. Downloads are started if
|
* Clears manual stop reason of all downloads. Downloads are started if the requirements are met.
|
||||||
* the requirements are met.
|
|
||||||
*/
|
*/
|
||||||
public void startDownloads() {
|
public void startDownloads() {
|
||||||
logd("manual stopped is cancelled");
|
logd("manual stop is cancelled");
|
||||||
manualStopReason = 0;
|
manualStopReason = MANUAL_STOP_REASON_NONE;
|
||||||
stopFlags &= ~STOP_FLAG_MANUAL;
|
|
||||||
for (int i = 0; i < downloads.size(); i++) {
|
for (int i = 0; i < downloads.size(); i++) {
|
||||||
downloads.get(i).clearManualStopReason();
|
downloads.get(i).setManualStopReason(MANUAL_STOP_REASON_NONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Signals all downloads to stop. Call {@link #startDownloads()} to let them to be started. */
|
/** Signals all downloads to stop. Call {@link #startDownloads()} to let them to be started. */
|
||||||
public void stopDownloads() {
|
public void stopDownloads() {
|
||||||
stopDownloads(/* manualStopReason= */ 0);
|
stopDownloads(/* manualStopReason= */ MANUAL_STOP_REASON_UNDEFINED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signals all downloads to stop. Call {@link #startDownloads()} to let them to be started.
|
* Signals all downloads to stop. Call {@link #startDownloads()} to let them to be started.
|
||||||
*
|
*
|
||||||
* @param manualStopReason An application defined stop reason.
|
* @param manualStopReason An application defined stop reason. Value {@value
|
||||||
|
* DownloadState#MANUAL_STOP_REASON_NONE} is not allowed and value {@value
|
||||||
|
* DownloadState#MANUAL_STOP_REASON_UNDEFINED} is reserved for {@link
|
||||||
|
* DownloadState#MANUAL_STOP_REASON_UNDEFINED}.
|
||||||
*/
|
*/
|
||||||
public void stopDownloads(int manualStopReason) {
|
public void stopDownloads(int manualStopReason) {
|
||||||
|
Assertions.checkArgument(manualStopReason != MANUAL_STOP_REASON_NONE);
|
||||||
logd("downloads are stopped manually");
|
logd("downloads are stopped manually");
|
||||||
this.manualStopReason = manualStopReason;
|
this.manualStopReason = manualStopReason;
|
||||||
stopFlags |= STOP_FLAG_MANUAL;
|
|
||||||
for (int i = 0; i < downloads.size(); i++) {
|
for (int i = 0; i < downloads.size(); i++) {
|
||||||
downloads.get(i).setManualStopReason(this.manualStopReason);
|
downloads.get(i).setManualStopReason(this.manualStopReason);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears {@link DownloadState#STOP_FLAG_MANUAL} flag of the download with the {@code id}.
|
* Clears manual stop reason of the download with the {@code id}. Download is started if the
|
||||||
* Download is started if the requirements are met.
|
* requirements are met.
|
||||||
*
|
*
|
||||||
* @param id The unique content id of the download to be started.
|
* @param id The unique content id of the download to be started.
|
||||||
*/
|
*/
|
||||||
public void startDownload(String id) {
|
public void startDownload(String id) {
|
||||||
Download download = getDownload(id);
|
Download download = getDownload(id);
|
||||||
if (download != null) {
|
if (download != null) {
|
||||||
logd("download is started manually", download);
|
logd("manual stop is cancelled", download);
|
||||||
download.clearManualStopReason();
|
download.setManualStopReason(MANUAL_STOP_REASON_NONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,7 +298,7 @@ public final class DownloadManager {
|
|||||||
* @param id The unique content id of the download to be stopped.
|
* @param id The unique content id of the download to be stopped.
|
||||||
*/
|
*/
|
||||||
public void stopDownload(String id) {
|
public void stopDownload(String id) {
|
||||||
stopDownload(id, /* manualStopReason= */ 0);
|
stopDownload(id, /* manualStopReason= */ MANUAL_STOP_REASON_UNDEFINED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -306,9 +306,13 @@ public final class DownloadManager {
|
|||||||
* to be started.
|
* to be started.
|
||||||
*
|
*
|
||||||
* @param id The unique content id of the download to be stopped.
|
* @param id The unique content id of the download to be stopped.
|
||||||
* @param manualStopReason An application defined stop reason.
|
* @param manualStopReason An application defined stop reason. Value {@value
|
||||||
|
* DownloadState#MANUAL_STOP_REASON_NONE} is not allowed and value {@value
|
||||||
|
* DownloadState#MANUAL_STOP_REASON_UNDEFINED} is reserved for {@link
|
||||||
|
* DownloadState#MANUAL_STOP_REASON_UNDEFINED}.
|
||||||
*/
|
*/
|
||||||
public void stopDownload(String id, int manualStopReason) {
|
public void stopDownload(String id, int manualStopReason) {
|
||||||
|
Assertions.checkArgument(manualStopReason != MANUAL_STOP_REASON_NONE);
|
||||||
Download download = getDownload(id);
|
Download download = getDownload(id);
|
||||||
if (download != null) {
|
if (download != null) {
|
||||||
logd("download is stopped manually", download);
|
logd("download is stopped manually", download);
|
||||||
@ -401,7 +405,7 @@ public final class DownloadManager {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Download download = new Download(this, action, stopFlags, notMetRequirements, manualStopReason);
|
Download download = new Download(this, action, notMetRequirements, manualStopReason);
|
||||||
downloads.add(download);
|
downloads.add(download);
|
||||||
logd("Download is added", download);
|
logd("Download is added", download);
|
||||||
}
|
}
|
||||||
@ -432,7 +436,7 @@ public final class DownloadManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onRequirementsStateChanged(@Requirements.RequirementFlags int notMetRequirements) {
|
private void onRequirementsStateChanged(@Requirements.RequirementFlags int notMetRequirements) {
|
||||||
setNotMetRequirements(notMetRequirements);
|
this.notMetRequirements = notMetRequirements;
|
||||||
logdFlags("Not met requirements are changed", notMetRequirements);
|
logdFlags("Not met requirements are changed", notMetRequirements);
|
||||||
Requirements requirements = requirementsWatcher.getRequirements();
|
Requirements requirements = requirementsWatcher.getRequirements();
|
||||||
for (Listener listener : listeners) {
|
for (Listener listener : listeners) {
|
||||||
@ -443,15 +447,6 @@ public final class DownloadManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setNotMetRequirements(@Requirements.RequirementFlags int notMetRequirements) {
|
|
||||||
this.notMetRequirements = notMetRequirements;
|
|
||||||
if (notMetRequirements == 0) {
|
|
||||||
stopFlags &= ~STOP_FLAG_REQUIREMENTS_NOT_MET;
|
|
||||||
} else {
|
|
||||||
stopFlags |= STOP_FLAG_REQUIREMENTS_NOT_MET;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Download getDownload(String id) {
|
private Download getDownload(String id) {
|
||||||
for (int i = 0; i < downloads.size(); i++) {
|
for (int i = 0; i < downloads.size(); i++) {
|
||||||
@ -632,20 +627,17 @@ public final class DownloadManager {
|
|||||||
private DownloadState downloadState;
|
private DownloadState downloadState;
|
||||||
@DownloadState.State private int state;
|
@DownloadState.State private int state;
|
||||||
@MonotonicNonNull @DownloadState.FailureReason private int failureReason;
|
@MonotonicNonNull @DownloadState.FailureReason private int failureReason;
|
||||||
@DownloadState.StopFlags private int stopFlags;
|
|
||||||
@Requirements.RequirementFlags private int notMetRequirements;
|
@Requirements.RequirementFlags private int notMetRequirements;
|
||||||
private int manualStopReason;
|
private int manualStopReason;
|
||||||
|
|
||||||
private Download(
|
private Download(
|
||||||
DownloadManager downloadManager,
|
DownloadManager downloadManager,
|
||||||
DownloadAction action,
|
DownloadAction action,
|
||||||
@DownloadState.StopFlags int stopFlags,
|
|
||||||
@Requirements.RequirementFlags int notMetRequirements,
|
@Requirements.RequirementFlags int notMetRequirements,
|
||||||
int manualStopReason) {
|
int manualStopReason) {
|
||||||
this.downloadManager = downloadManager;
|
this.downloadManager = downloadManager;
|
||||||
this.notMetRequirements = notMetRequirements;
|
this.notMetRequirements = notMetRequirements;
|
||||||
this.manualStopReason = manualStopReason;
|
this.manualStopReason = manualStopReason;
|
||||||
this.stopFlags = stopFlags;
|
|
||||||
downloadState = new DownloadState(action);
|
downloadState = new DownloadState(action);
|
||||||
|
|
||||||
initialize(downloadState.state);
|
initialize(downloadState.state);
|
||||||
@ -686,7 +678,6 @@ public final class DownloadManager {
|
|||||||
downloadedBytes,
|
downloadedBytes,
|
||||||
totalBytes,
|
totalBytes,
|
||||||
state != STATE_FAILED ? FAILURE_REASON_NONE : failureReason,
|
state != STATE_FAILED ? FAILURE_REASON_NONE : failureReason,
|
||||||
stopFlags,
|
|
||||||
notMetRequirements,
|
notMetRequirements,
|
||||||
manualStopReason,
|
manualStopReason,
|
||||||
downloadState.startTimeMs,
|
downloadState.startTimeMs,
|
||||||
@ -719,32 +710,24 @@ public final class DownloadManager {
|
|||||||
|
|
||||||
public void setNotMetRequirements(@Requirements.RequirementFlags int notMetRequirements) {
|
public void setNotMetRequirements(@Requirements.RequirementFlags int notMetRequirements) {
|
||||||
this.notMetRequirements = notMetRequirements;
|
this.notMetRequirements = notMetRequirements;
|
||||||
updateStopFlags(STOP_FLAG_REQUIREMENTS_NOT_MET, /* setFlags= */ notMetRequirements != 0);
|
updateStopState();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setManualStopReason(int manualStopReason) {
|
public void setManualStopReason(int manualStopReason) {
|
||||||
this.manualStopReason = manualStopReason;
|
this.manualStopReason = manualStopReason;
|
||||||
updateStopFlags(STOP_FLAG_MANUAL, /* setFlags= */ true);
|
updateStopState();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearManualStopReason() {
|
private void updateStopState() {
|
||||||
this.manualStopReason = 0;
|
if (canStart()) {
|
||||||
updateStopFlags(STOP_FLAG_MANUAL, /* setFlags= */ false);
|
if (state == STATE_STOPPED) {
|
||||||
}
|
startOrQueue();
|
||||||
|
}
|
||||||
private void updateStopFlags(int flags, boolean setFlags) {
|
|
||||||
if (setFlags) {
|
|
||||||
stopFlags |= flags;
|
|
||||||
} else {
|
} else {
|
||||||
stopFlags &= ~flags;
|
|
||||||
}
|
|
||||||
if (stopFlags != 0) {
|
|
||||||
if (state == STATE_DOWNLOADING || state == STATE_QUEUED) {
|
if (state == STATE_DOWNLOADING || state == STATE_QUEUED) {
|
||||||
downloadManager.stopDownloadThread(this);
|
downloadManager.stopDownloadThread(this);
|
||||||
setState(STATE_STOPPED);
|
setState(STATE_STOPPED);
|
||||||
}
|
}
|
||||||
} else if (state == STATE_STOPPED) {
|
|
||||||
startOrQueue();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -754,16 +737,20 @@ public final class DownloadManager {
|
|||||||
state = initialState;
|
state = initialState;
|
||||||
if (state == STATE_REMOVING || state == STATE_RESTARTING) {
|
if (state == STATE_REMOVING || state == STATE_RESTARTING) {
|
||||||
downloadManager.startDownloadThread(this, getAction());
|
downloadManager.startDownloadThread(this, getAction());
|
||||||
} else if (stopFlags != 0) {
|
} else if (canStart()) {
|
||||||
setState(STATE_STOPPED);
|
|
||||||
} else {
|
|
||||||
startOrQueue();
|
startOrQueue();
|
||||||
|
} else {
|
||||||
|
setState(STATE_STOPPED);
|
||||||
}
|
}
|
||||||
if (state == initialState) {
|
if (state == initialState) {
|
||||||
downloadManager.onDownloadStateChange(this);
|
downloadManager.onDownloadStateChange(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean canStart() {
|
||||||
|
return manualStopReason == MANUAL_STOP_REASON_NONE && notMetRequirements == 0;
|
||||||
|
}
|
||||||
|
|
||||||
private void startOrQueue() {
|
private void startOrQueue() {
|
||||||
Assertions.checkState(!(state == STATE_REMOVING || state == STATE_RESTARTING));
|
Assertions.checkState(!(state == STATE_REMOVING || state == STATE_RESTARTING));
|
||||||
@StartThreadResults int result = downloadManager.startDownloadThread(this, getAction());
|
@StartThreadResults int result = downloadManager.startDownloadThread(this, getAction());
|
||||||
|
@ -76,20 +76,10 @@ public final class DownloadState {
|
|||||||
/** The download is failed because of unknown reason. */
|
/** The download is failed because of unknown reason. */
|
||||||
public static final int FAILURE_REASON_UNKNOWN = 1;
|
public static final int FAILURE_REASON_UNKNOWN = 1;
|
||||||
|
|
||||||
/**
|
/** The download isn't manually stopped. */
|
||||||
* Download stop flags. Possible flag values are {@link #STOP_FLAG_MANUAL} and {@link
|
public static final int MANUAL_STOP_REASON_NONE = 0;
|
||||||
* #STOP_FLAG_REQUIREMENTS_NOT_MET}.
|
/** The download is manually stopped but a reason isn't specified. */
|
||||||
*/
|
public static final int MANUAL_STOP_REASON_UNDEFINED = Integer.MAX_VALUE;
|
||||||
@Documented
|
|
||||||
@Retention(RetentionPolicy.SOURCE)
|
|
||||||
@IntDef(
|
|
||||||
flag = true,
|
|
||||||
value = {STOP_FLAG_MANUAL, STOP_FLAG_REQUIREMENTS_NOT_MET})
|
|
||||||
public @interface StopFlags {}
|
|
||||||
/** Download is stopped by the application. */
|
|
||||||
public static final int STOP_FLAG_MANUAL = 1;
|
|
||||||
/** Download is stopped as the requirements are not met. */
|
|
||||||
public static final int STOP_FLAG_REQUIREMENTS_NOT_MET = 1 << 1;
|
|
||||||
|
|
||||||
/** Returns the state string for the given state value. */
|
/** Returns the state string for the given state value. */
|
||||||
public static String getStateString(@State int state) {
|
public static String getStateString(@State int state) {
|
||||||
@ -156,11 +146,9 @@ public final class DownloadState {
|
|||||||
* #FAILURE_REASON_NONE}.
|
* #FAILURE_REASON_NONE}.
|
||||||
*/
|
*/
|
||||||
@FailureReason public final int failureReason;
|
@FailureReason public final int failureReason;
|
||||||
/** Download stop flags. These flags stop downloading any content. */
|
|
||||||
@StopFlags public final int stopFlags;
|
|
||||||
/** Not met requirements to download. */
|
/** Not met requirements to download. */
|
||||||
@Requirements.RequirementFlags public final int notMetRequirements;
|
@Requirements.RequirementFlags public final int notMetRequirements;
|
||||||
/** If {@link #STOP_FLAG_MANUAL} is set then this field holds the manual stop reason. */
|
/** The manual stop reason. */
|
||||||
public final int manualStopReason;
|
public final int manualStopReason;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,7 +171,6 @@ public final class DownloadState {
|
|||||||
/* downloadedBytes= */ 0,
|
/* downloadedBytes= */ 0,
|
||||||
/* totalBytes= */ C.LENGTH_UNSET,
|
/* totalBytes= */ C.LENGTH_UNSET,
|
||||||
FAILURE_REASON_NONE,
|
FAILURE_REASON_NONE,
|
||||||
/* stopFlags= */ 0,
|
|
||||||
/* notMetRequirements= */ 0,
|
/* notMetRequirements= */ 0,
|
||||||
/* manualStopReason= */ 0,
|
/* manualStopReason= */ 0,
|
||||||
/* startTimeMs= */ currentTimeMs,
|
/* startTimeMs= */ currentTimeMs,
|
||||||
@ -202,7 +189,6 @@ public final class DownloadState {
|
|||||||
long downloadedBytes,
|
long downloadedBytes,
|
||||||
long totalBytes,
|
long totalBytes,
|
||||||
@FailureReason int failureReason,
|
@FailureReason int failureReason,
|
||||||
@StopFlags int stopFlags,
|
|
||||||
@RequirementFlags int notMetRequirements,
|
@RequirementFlags int notMetRequirements,
|
||||||
int manualStopReason,
|
int manualStopReason,
|
||||||
long startTimeMs,
|
long startTimeMs,
|
||||||
@ -210,10 +196,9 @@ public final class DownloadState {
|
|||||||
StreamKey[] streamKeys,
|
StreamKey[] streamKeys,
|
||||||
byte[] customMetadata) {
|
byte[] customMetadata) {
|
||||||
Assertions.checkState((failureReason == FAILURE_REASON_NONE) == (state != STATE_FAILED));
|
Assertions.checkState((failureReason == FAILURE_REASON_NONE) == (state != STATE_FAILED));
|
||||||
Assertions.checkState(stopFlags == 0 || (state != STATE_DOWNLOADING && state != STATE_QUEUED));
|
if (manualStopReason != 0 || notMetRequirements != 0) {
|
||||||
Assertions.checkState(
|
Assertions.checkState(state != STATE_DOWNLOADING && state != STATE_QUEUED);
|
||||||
((stopFlags & STOP_FLAG_REQUIREMENTS_NOT_MET) == 0) == (notMetRequirements == 0));
|
}
|
||||||
Assertions.checkState(((stopFlags & STOP_FLAG_MANUAL) != 0) || (manualStopReason == 0));
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.uri = uri;
|
this.uri = uri;
|
||||||
@ -223,7 +208,6 @@ public final class DownloadState {
|
|||||||
this.downloadedBytes = downloadedBytes;
|
this.downloadedBytes = downloadedBytes;
|
||||||
this.totalBytes = totalBytes;
|
this.totalBytes = totalBytes;
|
||||||
this.failureReason = failureReason;
|
this.failureReason = failureReason;
|
||||||
this.stopFlags = stopFlags;
|
|
||||||
this.notMetRequirements = notMetRequirements;
|
this.notMetRequirements = notMetRequirements;
|
||||||
this.manualStopReason = manualStopReason;
|
this.manualStopReason = manualStopReason;
|
||||||
this.startTimeMs = startTimeMs;
|
this.startTimeMs = startTimeMs;
|
||||||
@ -247,12 +231,12 @@ public final class DownloadState {
|
|||||||
type,
|
type,
|
||||||
action.uri,
|
action.uri,
|
||||||
action.customCacheKey,
|
action.customCacheKey,
|
||||||
getNextState(state, stopFlags != 0, action.isRemoveAction),
|
getNextState(
|
||||||
|
state, manualStopReason != 0 || notMetRequirements != 0, action.isRemoveAction),
|
||||||
/* downloadPercentage= */ C.PERCENTAGE_UNSET,
|
/* downloadPercentage= */ C.PERCENTAGE_UNSET,
|
||||||
downloadedBytes,
|
downloadedBytes,
|
||||||
/* totalBytes= */ C.LENGTH_UNSET,
|
/* totalBytes= */ C.LENGTH_UNSET,
|
||||||
FAILURE_REASON_NONE,
|
FAILURE_REASON_NONE,
|
||||||
stopFlags,
|
|
||||||
notMetRequirements,
|
notMetRequirements,
|
||||||
manualStopReason,
|
manualStopReason,
|
||||||
startTimeMs,
|
startTimeMs,
|
||||||
@ -261,14 +245,14 @@ public final class DownloadState {
|
|||||||
action.data);
|
action.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getNextState(int currentState, boolean stopFlagsSet, boolean remove) {
|
private static int getNextState(int currentState, boolean isStopped, boolean remove) {
|
||||||
int nextState;
|
int nextState;
|
||||||
if (remove) {
|
if (remove) {
|
||||||
nextState = STATE_REMOVING;
|
nextState = STATE_REMOVING;
|
||||||
} else {
|
} else {
|
||||||
if (currentState == STATE_REMOVING || currentState == STATE_RESTARTING) {
|
if (currentState == STATE_REMOVING || currentState == STATE_RESTARTING) {
|
||||||
nextState = STATE_RESTARTING;
|
nextState = STATE_RESTARTING;
|
||||||
} else if (stopFlagsSet) {
|
} else if (isStopped) {
|
||||||
nextState = STATE_STOPPED;
|
nextState = STATE_STOPPED;
|
||||||
} else {
|
} else {
|
||||||
nextState = STATE_QUEUED;
|
nextState = STATE_QUEUED;
|
||||||
|
@ -81,8 +81,6 @@ public class DefaultDownloadIndexTest {
|
|||||||
.setDownloadedBytes(200)
|
.setDownloadedBytes(200)
|
||||||
.setTotalBytes(400)
|
.setTotalBytes(400)
|
||||||
.setFailureReason(DownloadState.FAILURE_REASON_UNKNOWN)
|
.setFailureReason(DownloadState.FAILURE_REASON_UNKNOWN)
|
||||||
.setStopFlags(
|
|
||||||
DownloadState.STOP_FLAG_REQUIREMENTS_NOT_MET | DownloadState.STOP_FLAG_MANUAL)
|
|
||||||
.setNotMetRequirements(0x87654321)
|
.setNotMetRequirements(0x87654321)
|
||||||
.setManualStopReason(0x12345678)
|
.setManualStopReason(0x12345678)
|
||||||
.setStartTimeMs(10)
|
.setStartTimeMs(10)
|
||||||
|
@ -36,7 +36,6 @@ class DownloadStateBuilder {
|
|||||||
private long downloadedBytes;
|
private long downloadedBytes;
|
||||||
private long totalBytes;
|
private long totalBytes;
|
||||||
private int failureReason;
|
private int failureReason;
|
||||||
private int stopFlags;
|
|
||||||
private int notMetRequirements;
|
private int notMetRequirements;
|
||||||
private int manualStopReason;
|
private int manualStopReason;
|
||||||
private long startTimeMs;
|
private long startTimeMs;
|
||||||
@ -74,7 +73,6 @@ class DownloadStateBuilder {
|
|||||||
this.downloadedBytes = (long) 0;
|
this.downloadedBytes = (long) 0;
|
||||||
this.totalBytes = (long) C.LENGTH_UNSET;
|
this.totalBytes = (long) C.LENGTH_UNSET;
|
||||||
this.failureReason = DownloadState.FAILURE_REASON_NONE;
|
this.failureReason = DownloadState.FAILURE_REASON_NONE;
|
||||||
this.stopFlags = 0;
|
|
||||||
this.startTimeMs = (long) 0;
|
this.startTimeMs = (long) 0;
|
||||||
this.updateTimeMs = (long) 0;
|
this.updateTimeMs = (long) 0;
|
||||||
this.streamKeys = streamKeys;
|
this.streamKeys = streamKeys;
|
||||||
@ -131,11 +129,6 @@ class DownloadStateBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DownloadStateBuilder setStopFlags(int stopFlags) {
|
|
||||||
this.stopFlags = stopFlags;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DownloadStateBuilder setNotMetRequirements(int notMetRequirements) {
|
public DownloadStateBuilder setNotMetRequirements(int notMetRequirements) {
|
||||||
this.notMetRequirements = notMetRequirements;
|
this.notMetRequirements = notMetRequirements;
|
||||||
return this;
|
return this;
|
||||||
@ -177,7 +170,6 @@ class DownloadStateBuilder {
|
|||||||
downloadedBytes,
|
downloadedBytes,
|
||||||
totalBytes,
|
totalBytes,
|
||||||
failureReason,
|
failureReason,
|
||||||
stopFlags,
|
|
||||||
notMetRequirements,
|
notMetRequirements,
|
||||||
manualStopReason,
|
manualStopReason,
|
||||||
startTimeMs,
|
startTimeMs,
|
||||||
|
@ -171,7 +171,7 @@ public class DownloadStateTest {
|
|||||||
DownloadStateBuilder downloadStateBuilder =
|
DownloadStateBuilder downloadStateBuilder =
|
||||||
new DownloadStateBuilder(downloadAction)
|
new DownloadStateBuilder(downloadAction)
|
||||||
.setState(DownloadState.STATE_STOPPED)
|
.setState(DownloadState.STATE_STOPPED)
|
||||||
.setStopFlags(DownloadState.STOP_FLAG_MANUAL);
|
.setManualStopReason(DownloadState.MANUAL_STOP_REASON_UNDEFINED);
|
||||||
DownloadState downloadState = downloadStateBuilder.build();
|
DownloadState downloadState = downloadStateBuilder.build();
|
||||||
|
|
||||||
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
|
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
|
||||||
@ -185,7 +185,7 @@ public class DownloadStateTest {
|
|||||||
DownloadStateBuilder downloadStateBuilder =
|
DownloadStateBuilder downloadStateBuilder =
|
||||||
new DownloadStateBuilder(downloadAction)
|
new DownloadStateBuilder(downloadAction)
|
||||||
.setState(DownloadState.STATE_STOPPED)
|
.setState(DownloadState.STATE_STOPPED)
|
||||||
.setStopFlags(DownloadState.STOP_FLAG_MANUAL);
|
.setManualStopReason(DownloadState.MANUAL_STOP_REASON_UNDEFINED);
|
||||||
DownloadState downloadState = downloadStateBuilder.build();
|
DownloadState downloadState = downloadStateBuilder.build();
|
||||||
|
|
||||||
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
|
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
|
||||||
@ -196,12 +196,28 @@ public class DownloadStateTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mergeAction_stopFlagSetButNotInStoppedState_stateBecomesStopped() {
|
public void mergeAction_manualStopReasonSetButNotInStoppedState_stateBecomesStopped() {
|
||||||
DownloadAction downloadAction = createDownloadAction();
|
DownloadAction downloadAction = createDownloadAction();
|
||||||
DownloadStateBuilder downloadStateBuilder =
|
DownloadStateBuilder downloadStateBuilder =
|
||||||
new DownloadStateBuilder(downloadAction)
|
new DownloadStateBuilder(downloadAction)
|
||||||
.setState(DownloadState.STATE_COMPLETED)
|
.setState(DownloadState.STATE_COMPLETED)
|
||||||
.setStopFlags(DownloadState.STOP_FLAG_MANUAL);
|
.setManualStopReason(DownloadState.MANUAL_STOP_REASON_UNDEFINED);
|
||||||
|
DownloadState downloadState = downloadStateBuilder.build();
|
||||||
|
|
||||||
|
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
|
||||||
|
|
||||||
|
DownloadState expectedDownloadState =
|
||||||
|
downloadStateBuilder.setState(DownloadState.STATE_STOPPED).build();
|
||||||
|
assertEqual(mergedDownloadState, expectedDownloadState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mergeAction_notMetRequirementsSetButNotInStoppedState_stateBecomesStopped() {
|
||||||
|
DownloadAction downloadAction = createDownloadAction();
|
||||||
|
DownloadStateBuilder downloadStateBuilder =
|
||||||
|
new DownloadStateBuilder(downloadAction)
|
||||||
|
.setState(DownloadState.STATE_COMPLETED)
|
||||||
|
.setNotMetRequirements(0x12345678);
|
||||||
DownloadState downloadState = downloadStateBuilder.build();
|
DownloadState downloadState = downloadStateBuilder.build();
|
||||||
|
|
||||||
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
|
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
|
||||||
@ -325,7 +341,7 @@ public class DownloadStateTest {
|
|||||||
if (downloadState.failureReason != that.failureReason) {
|
if (downloadState.failureReason != that.failureReason) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (downloadState.stopFlags != that.stopFlags) {
|
if (downloadState.manualStopReason != that.manualStopReason) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (downloadState.notMetRequirements != that.notMetRequirements) {
|
if (downloadState.notMetRequirements != that.notMetRequirements) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user