Remove targetState in DownloadManager.Download

PiperOrigin-RevId: 225849846
This commit is contained in:
eguven 2018-12-17 18:34:31 +00:00 committed by Oliver Woodman
parent 3d6707e2c4
commit 3dc72a690c

View File

@ -553,12 +553,6 @@ public final class DownloadManager {
private static final class Download { private static final class Download {
/** Target states for the download thread. */
@Documented
@Retention(RetentionPolicy.SOURCE)
@IntDef({STATE_QUEUED, STATE_COMPLETED})
public @interface TargetState {}
private final String id; private final String id;
private final DownloadManager downloadManager; private final DownloadManager downloadManager;
private final DownloaderFactory downloaderFactory; private final DownloaderFactory downloaderFactory;
@ -568,11 +562,6 @@ public final class DownloadManager {
private DownloadAction action; private DownloadAction action;
/** The current state of the download. */ /** The current state of the download. */
@DownloadState.State private int state; @DownloadState.State private int state;
/**
* When started, this is the target state that the download will transition to when the download
* thread stops.
*/
@TargetState private volatile int targetState;
@MonotonicNonNull private Downloader downloader; @MonotonicNonNull private Downloader downloader;
@MonotonicNonNull private DownloadThread downloadThread; @MonotonicNonNull private DownloadThread downloadThread;
@ -590,7 +579,6 @@ public final class DownloadManager {
this.minRetryCount = minRetryCount; this.minRetryCount = minRetryCount;
this.startTimeMs = System.currentTimeMillis(); this.startTimeMs = System.currentTimeMillis();
state = STATE_QUEUED; state = STATE_QUEUED;
targetState = STATE_COMPLETED;
actionQueue = new ArrayDeque<>(); actionQueue = new ArrayDeque<>();
actionQueue.add(action); actionQueue.add(action);
} }
@ -603,9 +591,7 @@ public final class DownloadManager {
return; return;
} }
if (state == STATE_STARTED) { if (state == STATE_STARTED) {
if (targetState == STATE_COMPLETED) { stopDownloadThread();
stopDownloadThread();
}
} else { } else {
Assertions.checkState(state == STATE_QUEUED); Assertions.checkState(state == STATE_QUEUED);
action = updatedAction; action = updatedAction;
@ -648,9 +634,7 @@ public final class DownloadManager {
+ ' ' + ' '
+ (action.isRemoveAction ? "remove" : "download") + (action.isRemoveAction ? "remove" : "download")
+ ' ' + ' '
+ DownloadState.getStateString(state) + DownloadState.getStateString(state);
+ ' '
+ DownloadState.getStateString(targetState);
} }
public boolean canStart() { public boolean canStart() {
@ -661,7 +645,6 @@ public final class DownloadManager {
if (state == STATE_QUEUED) { if (state == STATE_QUEUED) {
state = STATE_STARTED; state = STATE_STARTED;
action = actionQueue.peek(); action = actionQueue.peek();
targetState = STATE_COMPLETED;
downloader = downloaderFactory.createDownloader(action); downloader = downloaderFactory.createDownloader(action);
downloadThread = downloadThread =
new DownloadThread( new DownloadThread(
@ -679,14 +662,12 @@ public final class DownloadManager {
// Internal methods running on the main thread. // Internal methods running on the main thread.
private void stopDownloadThread() { private void stopDownloadThread() {
this.targetState = DownloadState.STATE_QUEUED;
Assertions.checkNotNull(downloadThread).cancel(); Assertions.checkNotNull(downloadThread).cancel();
} }
private void onDownloadThreadStopped(@Nullable Throwable finalError) { private void onDownloadThreadStopped(@Nullable Throwable finalError) {
state = targetState;
failureReason = FAILURE_REASON_NONE; failureReason = FAILURE_REASON_NONE;
if (targetState == STATE_COMPLETED) { if (!downloadThread.isCanceled) {
if (finalError != null) { if (finalError != null) {
state = STATE_FAILED; state = STATE_FAILED;
failureReason = FAILURE_REASON_UNKNOWN; failureReason = FAILURE_REASON_UNKNOWN;
@ -696,8 +677,12 @@ public final class DownloadManager {
// Don't continue running. Wait to be restarted by maybeStartDownloads(). // Don't continue running. Wait to be restarted by maybeStartDownloads().
state = STATE_QUEUED; state = STATE_QUEUED;
action = actionQueue.peek(); action = actionQueue.peek();
} else {
state = STATE_COMPLETED;
} }
} }
} else {
state = STATE_QUEUED;
} }
downloadManager.onDownloadStateChange(this); downloadManager.onDownloadStateChange(this);
} }