Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel loading tasks when a new animation is set #14

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static LottieAnimationView forScreenshotTest(Context context) {
@Override
public void onCompositionLoaded(LottieComposition composition) {
setComposition(composition);
compositionLoader = null;
}
};

Expand All @@ -70,6 +71,7 @@ public void onCompositionLoaded(LottieComposition composition) {
private boolean setProgressWhenCompositionSet;
private boolean playAnimationWhenCompositionSet;

@Nullable private LottieComposition.Cancellable compositionLoader;
/** Can be null because it is created async */
@Nullable private LottieComposition composition;
private boolean hasInvalidatedThisFrame;
Expand Down Expand Up @@ -201,7 +203,8 @@ public void setAnimation(final String animationName) {
playAnimationWhenCompositionSet = false;

this.animationName = animationName;
LottieComposition.fromFile(getContext(), animationName, loadedListener);
cancelLoaderTask();
compositionLoader = LottieComposition.fromFile(getContext(), animationName, loadedListener);
}

/**
Expand All @@ -213,7 +216,15 @@ public void setAnimation(final JSONObject json) {
setProgressWhenCompositionSet = false;
playAnimationWhenCompositionSet = false;

LottieComposition.fromJson(getContext(), json, loadedListener);
cancelLoaderTask();
compositionLoader = LottieComposition.fromJson(getContext(), json, loadedListener);
}

private void cancelLoaderTask() {
if (compositionLoader != null) {
compositionLoader.cancel();
compositionLoader = null;
}
}

public void setComposition(@NonNull LottieComposition composition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public interface OnCompositionLoadedListener {
void onCompositionLoaded(LottieComposition composition);
}

public interface Cancellable {
void cancel();
}

/**
* The largest bitmap drawing cache can be is 8,294,400 bytes. There are 4 bytes per pixel leaving ~2.3M pixels available.
* Reduce the number a little bit for safety.
Expand All @@ -30,14 +34,16 @@ public interface OnCompositionLoadedListener {
*/
private static final int MAX_PIXELS = 1000;

public static void fromFile(Context context, String fileName, OnCompositionLoadedListener loadedListener) {
public static Cancellable fromFile(Context context, String fileName, OnCompositionLoadedListener loadedListener) {
InputStream file;
try {
file = context.getAssets().open(fileName);
} catch (IOException e) {
throw new IllegalStateException("Unable to find file " + fileName, e);
}
new FileCompositionLoader(loadedListener).execute(file);
FileCompositionLoader loader = new FileCompositionLoader(loadedListener);
loader.execute(file);
return loader;
}

public static LottieComposition fromFileSync(Context context, String fileName) {
Expand All @@ -50,8 +56,10 @@ public static LottieComposition fromFileSync(Context context, String fileName) {
return fromInputStream(file);
}

public static void fromJson(Context context, JSONObject json, OnCompositionLoadedListener loadedListener) {
new JsonCompositionLoader(loadedListener).execute(json);
public static Cancellable fromJson(Context context, JSONObject json, OnCompositionLoadedListener loadedListener) {
JsonCompositionLoader loader = new JsonCompositionLoader(loadedListener);
loader.execute(json);
return loader;
}

private static LottieComposition fromInputStream(InputStream file) {
Expand Down Expand Up @@ -175,7 +183,7 @@ public boolean hasMattes() {
return hasMattes;
}

private static final class FileCompositionLoader extends AsyncTask<InputStream, Void, LottieComposition> {
private static final class FileCompositionLoader extends CompositionLoader<InputStream> {

private final OnCompositionLoadedListener loadedListener;

Expand All @@ -194,7 +202,7 @@ protected void onPostExecute(LottieComposition composition) {
}
}

private static final class JsonCompositionLoader extends AsyncTask<JSONObject, Void, LottieComposition> {
private static final class JsonCompositionLoader extends CompositionLoader<JSONObject> {

private final OnCompositionLoadedListener loadedListener;

Expand All @@ -212,4 +220,14 @@ protected void onPostExecute(LottieComposition composition) {
loadedListener.onCompositionLoaded(composition);
}
}

private abstract static class CompositionLoader<Params>
extends AsyncTask<Params, Void, LottieComposition>
implements Cancellable {

@Override
public void cancel() {
cancel(true);
}
}
}