Remove DownloadState.stopFlags

PiperOrigin-RevId: 238442845
This commit is contained in:
eguven 2019-03-14 14:59:54 +00:00 committed by Oliver Woodman
parent f566c9486c
commit d088be7acc
6 changed files with 84 additions and 107 deletions

View File

@ -53,7 +53,6 @@ public final class DefaultDownloadIndex implements DownloadIndex {
private static final String COLUMN_DOWNLOADED_BYTES = "downloaded_bytes";
private static final String COLUMN_TOTAL_BYTES = "total_bytes";
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_MANUAL_STOP_REASON = "manual_stop_reason";
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_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_TYPE = 1;
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_TOTAL_BYTES = 7;
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 = 10;
private static final int COLUMN_INDEX_MANUAL_STOP_REASON = 11;
private static final int COLUMN_INDEX_START_TIME_MS = 12;
private static final int COLUMN_INDEX_UPDATE_TIME_MS = 13;
private static final int COLUMN_INDEX_STREAM_KEYS = 14;
private static final int COLUMN_INDEX_CUSTOM_METADATA = 15;
private static final int COLUMN_INDEX_NOT_MET_REQUIREMENTS = 9;
private static final int COLUMN_INDEX_MANUAL_STOP_REASON = 10;
private static final int COLUMN_INDEX_START_TIME_MS = 11;
private static final int COLUMN_INDEX_UPDATE_TIME_MS = 12;
private static final int COLUMN_INDEX_STREAM_KEYS = 13;
private static final int COLUMN_INDEX_CUSTOM_METADATA = 14;
private static final String WHERE_ID_EQUALS = COLUMN_ID + " = ?";
@ -91,7 +93,6 @@ public final class DefaultDownloadIndex implements DownloadIndex {
COLUMN_DOWNLOADED_BYTES,
COLUMN_TOTAL_BYTES,
COLUMN_FAILURE_REASON,
COLUMN_STOP_FLAGS,
COLUMN_NOT_MET_REQUIREMENTS,
COLUMN_MANUAL_STOP_REASON,
COLUMN_START_TIME_MS,
@ -210,7 +211,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
values.put(COLUMN_DOWNLOADED_BYTES, downloadState.downloadedBytes);
values.put(COLUMN_TOTAL_BYTES, downloadState.totalBytes);
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_MANUAL_STOP_REASON, downloadState.manualStopReason);
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_TOTAL_BYTES),
cursor.getInt(COLUMN_INDEX_FAILURE_REASON),
cursor.getInt(COLUMN_INDEX_STOP_FLAGS),
cursor.getInt(COLUMN_INDEX_NOT_MET_REQUIREMENTS),
cursor.getInt(COLUMN_INDEX_MANUAL_STOP_REASON),
cursor.getLong(COLUMN_INDEX_START_TIME_MS),

View File

@ -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_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_DOWNLOADING;
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_RESTARTING;
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.os.ConditionVariable;
@ -140,7 +140,6 @@ public final class DownloadManager {
private boolean initialized;
private boolean released;
@DownloadState.StopFlags private int stopFlags;
@Requirements.RequirementFlags private int notMetRequirements;
private int manualStopReason;
private RequirementsWatcher requirementsWatcher;
@ -186,7 +185,7 @@ public final class DownloadManager {
this.maxSimultaneousDownloads = maxSimultaneousDownloads;
this.minRetryCount = minRetryCount;
stopFlags = STOP_FLAG_MANUAL;
manualStopReason = MANUAL_STOP_REASON_UNDEFINED;
downloads = new ArrayList<>();
activeDownloads = new HashMap<>();
@ -203,7 +202,7 @@ public final class DownloadManager {
listeners = new CopyOnWriteArraySet<>();
actionQueue = new ArrayDeque<>();
setNotMetRequirements(watchRequirements(requirements));
notMetRequirements = watchRequirements(requirements);
loadActions();
logd("Created");
}
@ -246,48 +245,49 @@ public final class DownloadManager {
}
/**
* Clears {@link DownloadState#STOP_FLAG_MANUAL} flag of all downloads. Downloads are started if
* the requirements are met.
* Clears manual stop reason of all downloads. Downloads are started if the requirements are met.
*/
public void startDownloads() {
logd("manual stopped is cancelled");
manualStopReason = 0;
stopFlags &= ~STOP_FLAG_MANUAL;
logd("manual stop is cancelled");
manualStopReason = MANUAL_STOP_REASON_NONE;
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. */
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.
*
* @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) {
Assertions.checkArgument(manualStopReason != MANUAL_STOP_REASON_NONE);
logd("downloads are stopped manually");
this.manualStopReason = manualStopReason;
stopFlags |= STOP_FLAG_MANUAL;
for (int i = 0; i < downloads.size(); i++) {
downloads.get(i).setManualStopReason(this.manualStopReason);
}
}
/**
* Clears {@link DownloadState#STOP_FLAG_MANUAL} flag of the download with the {@code id}.
* Download is started if the requirements are met.
* Clears manual stop reason of the download with the {@code id}. Download is started if the
* requirements are met.
*
* @param id The unique content id of the download to be started.
*/
public void startDownload(String id) {
Download download = getDownload(id);
if (download != null) {
logd("download is started manually", download);
download.clearManualStopReason();
logd("manual stop is cancelled", download);
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.
*/
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.
*
* @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) {
Assertions.checkArgument(manualStopReason != MANUAL_STOP_REASON_NONE);
Download download = getDownload(id);
if (download != null) {
logd("download is stopped manually", download);
@ -401,7 +405,7 @@ public final class DownloadManager {
return;
}
}
Download download = new Download(this, action, stopFlags, notMetRequirements, manualStopReason);
Download download = new Download(this, action, notMetRequirements, manualStopReason);
downloads.add(download);
logd("Download is added", download);
}
@ -432,7 +436,7 @@ public final class DownloadManager {
}
private void onRequirementsStateChanged(@Requirements.RequirementFlags int notMetRequirements) {
setNotMetRequirements(notMetRequirements);
this.notMetRequirements = notMetRequirements;
logdFlags("Not met requirements are changed", notMetRequirements);
Requirements requirements = requirementsWatcher.getRequirements();
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
private Download getDownload(String id) {
for (int i = 0; i < downloads.size(); i++) {
@ -632,20 +627,17 @@ public final class DownloadManager {
private DownloadState downloadState;
@DownloadState.State private int state;
@MonotonicNonNull @DownloadState.FailureReason private int failureReason;
@DownloadState.StopFlags private int stopFlags;
@Requirements.RequirementFlags private int notMetRequirements;
private int manualStopReason;
private Download(
DownloadManager downloadManager,
DownloadAction action,
@DownloadState.StopFlags int stopFlags,
@Requirements.RequirementFlags int notMetRequirements,
int manualStopReason) {
this.downloadManager = downloadManager;
this.notMetRequirements = notMetRequirements;
this.manualStopReason = manualStopReason;
this.stopFlags = stopFlags;
downloadState = new DownloadState(action);
initialize(downloadState.state);
@ -686,7 +678,6 @@ public final class DownloadManager {
downloadedBytes,
totalBytes,
state != STATE_FAILED ? FAILURE_REASON_NONE : failureReason,
stopFlags,
notMetRequirements,
manualStopReason,
downloadState.startTimeMs,
@ -719,32 +710,24 @@ public final class DownloadManager {
public void setNotMetRequirements(@Requirements.RequirementFlags int notMetRequirements) {
this.notMetRequirements = notMetRequirements;
updateStopFlags(STOP_FLAG_REQUIREMENTS_NOT_MET, /* setFlags= */ notMetRequirements != 0);
updateStopState();
}
public void setManualStopReason(int manualStopReason) {
this.manualStopReason = manualStopReason;
updateStopFlags(STOP_FLAG_MANUAL, /* setFlags= */ true);
updateStopState();
}
public void clearManualStopReason() {
this.manualStopReason = 0;
updateStopFlags(STOP_FLAG_MANUAL, /* setFlags= */ false);
}
private void updateStopFlags(int flags, boolean setFlags) {
if (setFlags) {
stopFlags |= flags;
private void updateStopState() {
if (canStart()) {
if (state == STATE_STOPPED) {
startOrQueue();
}
} else {
stopFlags &= ~flags;
}
if (stopFlags != 0) {
if (state == STATE_DOWNLOADING || state == STATE_QUEUED) {
downloadManager.stopDownloadThread(this);
setState(STATE_STOPPED);
}
} else if (state == STATE_STOPPED) {
startOrQueue();
}
}
@ -754,16 +737,20 @@ public final class DownloadManager {
state = initialState;
if (state == STATE_REMOVING || state == STATE_RESTARTING) {
downloadManager.startDownloadThread(this, getAction());
} else if (stopFlags != 0) {
setState(STATE_STOPPED);
} else {
} else if (canStart()) {
startOrQueue();
} else {
setState(STATE_STOPPED);
}
if (state == initialState) {
downloadManager.onDownloadStateChange(this);
}
}
private boolean canStart() {
return manualStopReason == MANUAL_STOP_REASON_NONE && notMetRequirements == 0;
}
private void startOrQueue() {
Assertions.checkState(!(state == STATE_REMOVING || state == STATE_RESTARTING));
@StartThreadResults int result = downloadManager.startDownloadThread(this, getAction());

View File

@ -76,20 +76,10 @@ public final class DownloadState {
/** The download is failed because of unknown reason. */
public static final int FAILURE_REASON_UNKNOWN = 1;
/**
* Download stop flags. Possible flag values are {@link #STOP_FLAG_MANUAL} and {@link
* #STOP_FLAG_REQUIREMENTS_NOT_MET}.
*/
@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;
/** The download isn't manually stopped. */
public static final int MANUAL_STOP_REASON_NONE = 0;
/** The download is manually stopped but a reason isn't specified. */
public static final int MANUAL_STOP_REASON_UNDEFINED = Integer.MAX_VALUE;
/** Returns the state string for the given state value. */
public static String getStateString(@State int state) {
@ -156,11 +146,9 @@ public final class DownloadState {
* #FAILURE_REASON_NONE}.
*/
@FailureReason public final int failureReason;
/** Download stop flags. These flags stop downloading any content. */
@StopFlags public final int stopFlags;
/** Not met requirements to download. */
@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;
/**
@ -183,7 +171,6 @@ public final class DownloadState {
/* downloadedBytes= */ 0,
/* totalBytes= */ C.LENGTH_UNSET,
FAILURE_REASON_NONE,
/* stopFlags= */ 0,
/* notMetRequirements= */ 0,
/* manualStopReason= */ 0,
/* startTimeMs= */ currentTimeMs,
@ -202,7 +189,6 @@ public final class DownloadState {
long downloadedBytes,
long totalBytes,
@FailureReason int failureReason,
@StopFlags int stopFlags,
@RequirementFlags int notMetRequirements,
int manualStopReason,
long startTimeMs,
@ -210,10 +196,9 @@ public final class DownloadState {
StreamKey[] streamKeys,
byte[] customMetadata) {
Assertions.checkState((failureReason == FAILURE_REASON_NONE) == (state != STATE_FAILED));
Assertions.checkState(stopFlags == 0 || (state != STATE_DOWNLOADING && state != STATE_QUEUED));
Assertions.checkState(
((stopFlags & STOP_FLAG_REQUIREMENTS_NOT_MET) == 0) == (notMetRequirements == 0));
Assertions.checkState(((stopFlags & STOP_FLAG_MANUAL) != 0) || (manualStopReason == 0));
if (manualStopReason != 0 || notMetRequirements != 0) {
Assertions.checkState(state != STATE_DOWNLOADING && state != STATE_QUEUED);
}
this.id = id;
this.type = type;
this.uri = uri;
@ -223,7 +208,6 @@ public final class DownloadState {
this.downloadedBytes = downloadedBytes;
this.totalBytes = totalBytes;
this.failureReason = failureReason;
this.stopFlags = stopFlags;
this.notMetRequirements = notMetRequirements;
this.manualStopReason = manualStopReason;
this.startTimeMs = startTimeMs;
@ -247,12 +231,12 @@ public final class DownloadState {
type,
action.uri,
action.customCacheKey,
getNextState(state, stopFlags != 0, action.isRemoveAction),
getNextState(
state, manualStopReason != 0 || notMetRequirements != 0, action.isRemoveAction),
/* downloadPercentage= */ C.PERCENTAGE_UNSET,
downloadedBytes,
/* totalBytes= */ C.LENGTH_UNSET,
FAILURE_REASON_NONE,
stopFlags,
notMetRequirements,
manualStopReason,
startTimeMs,
@ -261,14 +245,14 @@ public final class DownloadState {
action.data);
}
private static int getNextState(int currentState, boolean stopFlagsSet, boolean remove) {
private static int getNextState(int currentState, boolean isStopped, boolean remove) {
int nextState;
if (remove) {
nextState = STATE_REMOVING;
} else {
if (currentState == STATE_REMOVING || currentState == STATE_RESTARTING) {
nextState = STATE_RESTARTING;
} else if (stopFlagsSet) {
} else if (isStopped) {
nextState = STATE_STOPPED;
} else {
nextState = STATE_QUEUED;

View File

@ -81,8 +81,6 @@ public class DefaultDownloadIndexTest {
.setDownloadedBytes(200)
.setTotalBytes(400)
.setFailureReason(DownloadState.FAILURE_REASON_UNKNOWN)
.setStopFlags(
DownloadState.STOP_FLAG_REQUIREMENTS_NOT_MET | DownloadState.STOP_FLAG_MANUAL)
.setNotMetRequirements(0x87654321)
.setManualStopReason(0x12345678)
.setStartTimeMs(10)

View File

@ -36,7 +36,6 @@ class DownloadStateBuilder {
private long downloadedBytes;
private long totalBytes;
private int failureReason;
private int stopFlags;
private int notMetRequirements;
private int manualStopReason;
private long startTimeMs;
@ -74,7 +73,6 @@ class DownloadStateBuilder {
this.downloadedBytes = (long) 0;
this.totalBytes = (long) C.LENGTH_UNSET;
this.failureReason = DownloadState.FAILURE_REASON_NONE;
this.stopFlags = 0;
this.startTimeMs = (long) 0;
this.updateTimeMs = (long) 0;
this.streamKeys = streamKeys;
@ -131,11 +129,6 @@ class DownloadStateBuilder {
return this;
}
public DownloadStateBuilder setStopFlags(int stopFlags) {
this.stopFlags = stopFlags;
return this;
}
public DownloadStateBuilder setNotMetRequirements(int notMetRequirements) {
this.notMetRequirements = notMetRequirements;
return this;
@ -177,7 +170,6 @@ class DownloadStateBuilder {
downloadedBytes,
totalBytes,
failureReason,
stopFlags,
notMetRequirements,
manualStopReason,
startTimeMs,

View File

@ -171,7 +171,7 @@ public class DownloadStateTest {
DownloadStateBuilder downloadStateBuilder =
new DownloadStateBuilder(downloadAction)
.setState(DownloadState.STATE_STOPPED)
.setStopFlags(DownloadState.STOP_FLAG_MANUAL);
.setManualStopReason(DownloadState.MANUAL_STOP_REASON_UNDEFINED);
DownloadState downloadState = downloadStateBuilder.build();
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
@ -185,7 +185,7 @@ public class DownloadStateTest {
DownloadStateBuilder downloadStateBuilder =
new DownloadStateBuilder(downloadAction)
.setState(DownloadState.STATE_STOPPED)
.setStopFlags(DownloadState.STOP_FLAG_MANUAL);
.setManualStopReason(DownloadState.MANUAL_STOP_REASON_UNDEFINED);
DownloadState downloadState = downloadStateBuilder.build();
DownloadState mergedDownloadState = downloadState.mergeAction(downloadAction);
@ -196,12 +196,28 @@ public class DownloadStateTest {
}
@Test
public void mergeAction_stopFlagSetButNotInStoppedState_stateBecomesStopped() {
public void mergeAction_manualStopReasonSetButNotInStoppedState_stateBecomesStopped() {
DownloadAction downloadAction = createDownloadAction();
DownloadStateBuilder downloadStateBuilder =
new DownloadStateBuilder(downloadAction)
.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 mergedDownloadState = downloadState.mergeAction(downloadAction);
@ -325,7 +341,7 @@ public class DownloadStateTest {
if (downloadState.failureReason != that.failureReason) {
return false;
}
if (downloadState.stopFlags != that.stopFlags) {
if (downloadState.manualStopReason != that.manualStopReason) {
return false;
}
if (downloadState.notMetRequirements != that.notMetRequirements) {