Skip to content

Commit

Permalink
Unify the parsing of generic input streams (#2558)
Browse files Browse the repository at this point in the history
Fixes #2490
  • Loading branch information
gpeal authored Sep 29, 2024
1 parent 5eb81c0 commit 626f081
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,7 @@ private fun lottieTask(
is LottieCompositionSpec.ContentProvider -> {
val fis = context.contentResolver.openInputStream(spec.uri)
val actualCacheKey = if (cacheKey == DefaultCacheKey) spec.uri.toString() else cacheKey
when {
spec.uri.toString().endsWith("zip") ||
spec.uri.toString().endsWith("lottie") -> LottieCompositionFactory.fromZipStream(
ZipInputStream(fis),
actualCacheKey,
)

spec.uri.toString().endsWith("tgs") -> LottieCompositionFactory.fromJsonInputStream(
GZIPInputStream(fis),
actualCacheKey,
)

else -> LottieCompositionFactory.fromJsonInputStream(
fis,
actualCacheKey,
)
}
return LottieCompositionFactory.fromInputStream(context, fis, actualCacheKey)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,40 @@ public static LottieResult<LottieComposition> fromAssetSync(Context context, Str
return new LottieResult<>(cachedComposition);
}
try {
BufferedSource source = Okio.buffer(source(context.getAssets().open(fileName)));
return fromInputStreamSync(context, context.getAssets().open(fileName), cacheKey);
} catch (IOException e) {
return new LottieResult<>(e);
}
}

/**
* Use this when you have an input stream but aren't sure if it is a json, zip, or gzip file.
* This will read the file headers to see if it starts with the gzip or zip magic bytes.
* @param context is optional and only needed if your zip file contains ttf or otf fonts. If yours doesn't, you may pass null.
* Embedded fonts may be .ttf or .otf files, can be in subdirectories, but must have the same name as the
* font family (fFamily) in your animation file.
*/
public static LottieTask<LottieComposition> fromInputStream(@Nullable Context context, InputStream inputStream, @Nullable String cacheKey) {
// Prevent accidentally leaking an Activity.
final Context appContext = context == null ? null : context.getApplicationContext();
return cache(cacheKey, () -> fromInputStreamSync(appContext, inputStream, cacheKey), null);
}

/**
* Use this when you have an input stream but aren't sure if it is a json, zip, or gzip file.
* This will read the file headers to see if it starts with the gzip or zip magic bytes.
* @param context is optional and only needed if your zip file contains ttf or otf fonts. If yours doesn't, you may pass null.
* Embedded fonts may be .ttf or .otf files, can be in subdirectories, but must have the same name as the
* font family (fFamily) in your animation file.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromInputStreamSync(@Nullable Context context, InputStream inputStream, @Nullable String cacheKey) {
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieResult<>(cachedComposition);
}
try {
BufferedSource source = Okio.buffer(source(inputStream));
if (isZipCompressed(source)) {
return fromZipStreamSync(context, new ZipInputStream(source.inputStream()), cacheKey);
} else if (isGzipCompressed(source)) {
Expand Down Expand Up @@ -553,7 +586,8 @@ public static LottieResult<LottieComposition> fromZipStreamSync(@Nullable Contex
}

@WorkerThread
private static LottieResult<LottieComposition> fromZipStreamSyncInternal(@Nullable Context context, ZipInputStream inputStream, @Nullable String cacheKey) {
private static LottieResult<LottieComposition> fromZipStreamSyncInternal(@Nullable Context context, ZipInputStream inputStream,
@Nullable String cacheKey) {
LottieComposition composition = null;
Map<String, Bitmap> images = new HashMap<>();
Map<String, Typeface> fonts = new HashMap<>();
Expand Down Expand Up @@ -583,7 +617,8 @@ private static LottieResult<LottieComposition> fromZipStreamSyncInternal(@Nullab
String fontFamily = fileName.split("\\.")[0];

if (context == null) {
return new LottieResult<>(new IllegalStateException("Unable to extract font " + fontFamily + " please pass a non-null Context parameter"));
return new LottieResult<>(
new IllegalStateException("Unable to extract font " + fontFamily + " please pass a non-null Context parameter"));
}

File tempFile = new File(context.getCacheDir(), fileName);
Expand Down

0 comments on commit 626f081

Please sign in to comment.