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

Fixed the stream was not closed when the lottie animation hit the cache #2253

Merged
merged 1 commit into from
May 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ public void setAnimationFromJson(String jsonString, @Nullable String cacheKey) {
* <p>
* This is particularly useful for animations loaded from the network. You can fetch the
* bodymovin json from the network and pass it directly here.
* <p>
* Auto-closes the stream.
*/
public void setAnimation(InputStream stream, @Nullable String cacheKey) {
setCompositionTask(LottieCompositionFactory.fromJsonInputStream(stream, cacheKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static LottieTask<LottieComposition> fromUrl(final Context context, final
LottieCompositionCache.getInstance().put(cacheKey, result.getValue());
}
return result;
});
}, null);
}

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ public static LottieTask<LottieComposition> fromAsset(Context context, final Str
public static LottieTask<LottieComposition> fromAsset(Context context, final String fileName, @Nullable final String cacheKey) {
// Prevent accidentally leaking an Activity.
final Context appContext = context.getApplicationContext();
return cache(cacheKey, () -> fromAssetSync(appContext, fileName, cacheKey));
return cache(cacheKey, () -> fromAssetSync(appContext, fileName, cacheKey), null);
}

/**
Expand Down Expand Up @@ -254,7 +254,7 @@ public static LottieTask<LottieComposition> fromRawRes(Context context, @RawRes
@Nullable Context originalContext = contextRef.get();
Context context1 = originalContext != null ? originalContext : appContext;
return fromRawResSync(context1, rawRes, cacheKey);
});
}, null);
}

/**
Expand Down Expand Up @@ -311,7 +311,7 @@ private static boolean isNightMode(Context context) {
* @see #fromJsonInputStreamSync(InputStream, String, boolean)
*/
public static LottieTask<LottieComposition> fromJsonInputStream(final InputStream stream, @Nullable final String cacheKey) {
return cache(cacheKey, () -> fromJsonInputStreamSync(stream, cacheKey));
return cache(cacheKey, () -> fromJsonInputStreamSync(stream, cacheKey), () -> closeQuietly(stream));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromJsonInputStreamSync closes the stream here. Is that not sufficient for your use case?

Copy link
Contributor Author

@sunlocker sunlocker Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing.

Yes, fromJsonInputStreamSync doesn't close my stream because I set stream and cacheKey via setAnimation(InputStream stream, @Nullable String cacheKey) and hit the cache, but fromJsonInputStreamSync doesn't execute when there is cache. return from here

I also can't close my stream manually because the Lottie framework internally closes my stream via fromJsonInputStreamSync when there is no cache.

}

/**
Expand Down Expand Up @@ -343,7 +343,7 @@ public static LottieTask<LottieComposition> fromJson(final JSONObject json, @Nul
return cache(cacheKey, () -> {
//noinspection deprecation
return fromJsonSync(json, cacheKey);
});
}, null);
}

/**
Expand All @@ -361,7 +361,7 @@ public static LottieResult<LottieComposition> fromJsonSync(JSONObject json, @Nul
* @see #fromJsonStringSync(String, String)
*/
public static LottieTask<LottieComposition> fromJsonString(final String json, @Nullable final String cacheKey) {
return cache(cacheKey, () -> fromJsonStringSync(json, cacheKey));
return cache(cacheKey, () -> fromJsonStringSync(json, cacheKey), null);
}

/**
Expand All @@ -377,7 +377,7 @@ public static LottieResult<LottieComposition> fromJsonStringSync(String json, @N
}

public static LottieTask<LottieComposition> fromJsonReader(final JsonReader reader, @Nullable final String cacheKey) {
return cache(cacheKey, () -> fromJsonReaderSync(reader, cacheKey));
return cache(cacheKey, () -> fromJsonReaderSync(reader, cacheKey), () -> Utils.closeQuietly(reader));
}


Expand Down Expand Up @@ -417,7 +417,7 @@ public static LottieTask<LottieComposition> fromZipStream(final ZipInputStream i
* @see #fromZipStreamSync(Context, ZipInputStream, String)
*/
public static LottieTask<LottieComposition> fromZipStream(Context context, final ZipInputStream inputStream, @Nullable final String cacheKey) {
return cache(cacheKey, () -> fromZipStreamSync(context, inputStream, cacheKey));
return cache(cacheKey, () -> fromZipStreamSync(context, inputStream, cacheKey), () -> closeQuietly(inputStream));
}

/**
Expand Down Expand Up @@ -604,17 +604,24 @@ private static LottieImageAsset findImageAssetForFileName(LottieComposition comp
* If not, create a new task for the callable.
* Then, add the new task to the task cache and set up listeners so it gets cleared when done.
*/
private static LottieTask<LottieComposition> cache(
@Nullable final String cacheKey, Callable<LottieResult<LottieComposition>> callable) {
private static LottieTask<LottieComposition> cache(@Nullable final String cacheKey, Callable<LottieResult<LottieComposition>> callable,
@Nullable Runnable onCached) {
LottieTask<LottieComposition> task = null;
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieTask<>(() -> new LottieResult<>(cachedComposition));
task = new LottieTask<>(() -> new LottieResult<>(cachedComposition));
}
if (cacheKey != null && taskCache.containsKey(cacheKey)) {
return taskCache.get(cacheKey);
task = taskCache.get(cacheKey);
}
if (task != null) {
if (onCached != null) {
onCached.run();
}
return task;
}

LottieTask<LottieComposition> task = new LottieTask<>(callable);
task = new LottieTask<>(callable);
if (cacheKey != null) {
AtomicBoolean resultAlreadyCalled = new AtomicBoolean(false);
task.addListener(result -> {
Expand Down