Remove Loadable.isLoadCanceled

This simplifies Loadable implementations, and also removes the
possibility of an incorrect Loadable implementation causing the
wrong Loader.Callback method being called (perviously, for the
correct method to be called, we relied on isLoadCanceled being
implemented correctly).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=198871133
This commit is contained in:
olly 2018-06-01 07:46:29 -07:00 committed by Oliver Woodman
parent 3670111310
commit 7621a71bc3
10 changed files with 7 additions and 59 deletions

View file

@ -832,11 +832,6 @@ import java.util.Arrays;
loadCanceled = true; loadCanceled = true;
} }
@Override
public boolean isLoadCanceled() {
return loadCanceled;
}
@Override @Override
public void load() throws IOException, InterruptedException { public void load() throws IOException, InterruptedException {
int result = Extractor.RESULT_CONTINUE; int result = Extractor.RESULT_CONTINUE;

View file

@ -348,11 +348,6 @@ import java.util.Arrays;
// Never happens. // Never happens.
} }
@Override
public boolean isLoadCanceled() {
return false;
}
@Override @Override
public void load() throws IOException, InterruptedException { public void load() throws IOException, InterruptedException {
// We always load from the beginning, so reset the sampleSize to 0. // We always load from the beginning, so reset the sampleSize to 0.

View file

@ -106,11 +106,6 @@ public class ContainerMediaChunk extends BaseMediaChunk {
loadCanceled = true; loadCanceled = true;
} }
@Override
public final boolean isLoadCanceled() {
return loadCanceled;
}
@SuppressWarnings("NonAtomicVolatileUpdate") @SuppressWarnings("NonAtomicVolatileUpdate")
@Override @Override
public final void load() throws IOException, InterruptedException { public final void load() throws IOException, InterruptedException {

View file

@ -75,11 +75,6 @@ public abstract class DataChunk extends Chunk {
loadCanceled = true; loadCanceled = true;
} }
@Override
public final boolean isLoadCanceled() {
return loadCanceled;
}
@Override @Override
public final void load() throws IOException, InterruptedException { public final void load() throws IOException, InterruptedException {
try { try {

View file

@ -69,11 +69,6 @@ public final class InitializationChunk extends Chunk {
loadCanceled = true; loadCanceled = true;
} }
@Override
public boolean isLoadCanceled() {
return loadCanceled;
}
@SuppressWarnings("NonAtomicVolatileUpdate") @SuppressWarnings("NonAtomicVolatileUpdate")
@Override @Override
public void load() throws IOException, InterruptedException { public void load() throws IOException, InterruptedException {

View file

@ -34,7 +34,6 @@ public final class SingleSampleMediaChunk extends BaseMediaChunk {
private final Format sampleFormat; private final Format sampleFormat;
private volatile int bytesLoaded; private volatile int bytesLoaded;
private volatile boolean loadCanceled;
private volatile boolean loadCompleted; private volatile boolean loadCompleted;
/** /**
@ -90,12 +89,7 @@ public final class SingleSampleMediaChunk extends BaseMediaChunk {
@Override @Override
public void cancelLoad() { public void cancelLoad() {
loadCanceled = true; // Do nothing.
}
@Override
public boolean isLoadCanceled() {
return loadCanceled;
} }
@SuppressWarnings("NonAtomicVolatileUpdate") @SuppressWarnings("NonAtomicVolatileUpdate")

View file

@ -57,11 +57,6 @@ public final class Loader implements LoaderErrorThrower {
*/ */
void cancelLoad(); void cancelLoad();
/**
* Returns whether the load has been canceled.
*/
boolean isLoadCanceled();
/** /**
* Performs the load, returning on completion or cancellation. * Performs the load, returning on completion or cancellation.
* *
@ -260,6 +255,7 @@ public final class Loader implements LoaderErrorThrower {
private int errorCount; private int errorCount;
private volatile Thread executorThread; private volatile Thread executorThread;
private volatile boolean canceled;
private volatile boolean released; private volatile boolean released;
public LoadTask(Looper looper, T loadable, Loader.Callback<T> callback, public LoadTask(Looper looper, T loadable, Loader.Callback<T> callback,
@ -296,6 +292,7 @@ public final class Loader implements LoaderErrorThrower {
sendEmptyMessage(MSG_CANCEL); sendEmptyMessage(MSG_CANCEL);
} }
} else { } else {
canceled = true;
loadable.cancelLoad(); loadable.cancelLoad();
if (executorThread != null) { if (executorThread != null) {
executorThread.interrupt(); executorThread.interrupt();
@ -317,7 +314,7 @@ public final class Loader implements LoaderErrorThrower {
public void run() { public void run() {
try { try {
executorThread = Thread.currentThread(); executorThread = Thread.currentThread();
if (!loadable.isLoadCanceled()) { if (!canceled) {
TraceUtil.beginSection("load:" + loadable.getClass().getSimpleName()); TraceUtil.beginSection("load:" + loadable.getClass().getSimpleName());
try { try {
loadable.load(); loadable.load();
@ -334,7 +331,7 @@ public final class Loader implements LoaderErrorThrower {
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
// The load was canceled. // The load was canceled.
Assertions.checkState(loadable.isLoadCanceled()); Assertions.checkState(canceled);
if (!released) { if (!released) {
sendEmptyMessage(MSG_END_OF_SOURCE); sendEmptyMessage(MSG_END_OF_SOURCE);
} }
@ -379,7 +376,7 @@ public final class Loader implements LoaderErrorThrower {
finish(); finish();
long nowMs = SystemClock.elapsedRealtime(); long nowMs = SystemClock.elapsedRealtime();
long durationMs = nowMs - startTimeMs; long durationMs = nowMs - startTimeMs;
if (loadable.isLoadCanceled()) { if (canceled) {
callback.onLoadCanceled(loadable, nowMs, durationMs, false); callback.onLoadCanceled(loadable, nowMs, durationMs, false);
return; return;
} }

View file

@ -78,7 +78,6 @@ public final class ParsingLoadable<T> implements Loadable {
private final Parser<? extends T> parser; private final Parser<? extends T> parser;
private volatile T result; private volatile T result;
private volatile boolean isCanceled;
private volatile long bytesLoaded; private volatile long bytesLoaded;
/** /**
@ -128,14 +127,7 @@ public final class ParsingLoadable<T> implements Loadable {
@Override @Override
public final void cancelLoad() { public final void cancelLoad() {
// We don't actually cancel anything, but we need to record the cancellation so that // Do nothing.
// isLoadCanceled can return the correct value.
isCanceled = true;
}
@Override
public final boolean isLoadCanceled() {
return isCanceled;
} }
@Override @Override

View file

@ -391,11 +391,6 @@ public final class AdaptiveTrackSelectionTest {
// Do nothing. // Do nothing.
} }
@Override
public boolean isLoadCanceled() {
return false;
}
@Override @Override
public void load() throws IOException, InterruptedException { public void load() throws IOException, InterruptedException {
// Do nothing. // Do nothing.

View file

@ -206,11 +206,6 @@ import java.util.concurrent.atomic.AtomicInteger;
loadCanceled = true; loadCanceled = true;
} }
@Override
public boolean isLoadCanceled() {
return loadCanceled;
}
@Override @Override
public void load() throws IOException, InterruptedException { public void load() throws IOException, InterruptedException {
maybeLoadInitData(); maybeLoadInitData();