This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DotLottieCompositionFactory.java
525 lines (470 loc) · 21.7 KB
/
DotLottieCompositionFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.LottieImageAsset;
import com.airbnb.lottie.LottieListener;
import com.airbnb.lottie.LottieResult;
import com.airbnb.lottie.LottieTask;
import com.airbnb.lottie.model.LottieCompositionCache;
import com.airbnb.lottie.network.NetworkFetcher;
import com.airbnb.lottie.parser.LottieCompositionMoshiParser;
import com.airbnb.lottie.parser.moshi.JsonReader;
import com.airbnb.lottie.utils.Utils;
import org.json.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import androidx.annotation.WorkerThread;
import static com.airbnb.lottie.parser.moshi.JsonReader.*;
import static com.airbnb.lottie.utils.Utils.closeQuietly;
import static okio.Okio.buffer;
import static okio.Okio.source;
/**
* Helpers to create or cache a LottieComposition.
* <p>
* All factory methods take a cache key. The animation will be stored in an LRU cache for future use.
* In-progress tasks will also be held so they can be returned for subsequent requests for the same
* animation prior to the cache being populated.
*/
@SuppressWarnings({"WeakerAccess", "unused", "NullAway"})
@SuppressLint("RestrictedApi")
public class DotLottieCompositionFactory {
/**
* Keep a map of cache keys to in-progress tasks and return them for new requests.
* Without this, simultaneous requests to parse a composition will trigger multiple parallel
* parse tasks prior to the cache getting populated.
*/
private static final Map<String, LottieTask<LottieComposition>> taskCache = new HashMap<>();
private DotLottieCompositionFactory() {
}
/**
* Set the maximum number of compositions to keep cached in memory.
* This must be > 0.
*/
public static void setMaxCacheSize(int size) {
LottieCompositionCache.getInstance().resize(size);
}
/**
* Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk for
* future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you
* might need an animation in the future.
*
* To skip the cache, add null as a third parameter.
*/
public static LottieTask<LottieComposition> fromUrl(final Context context, final String url) {
return fromUrl(context, url, "url_" + url);
}
/**
* Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk for
* future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you
* might need an animation in the future.
*/
public static LottieTask<LottieComposition> fromUrl(final Context context, final String url, @Nullable String cacheKey) {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return NetworkFetcher.fetchSync(context, url);
}
});
}
/**
* Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk for
* future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you
* might need an animation in the future.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromUrlSync(Context context, String url) {
return NetworkFetcher.fetchSync(context, url);
}
/**
* Parse an animation from src/main/assets. It is recommended to use {@link #fromRawRes(Context, int)} instead.
* The asset file name will be used as a cache key so future usages won't have to parse the json again.
* However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
*
* To skip the cache, add null as a third parameter.
*
* @see #fromZipStream(ZipInputStream, String)
*/
public static LottieTask<LottieComposition> fromAsset(Context context, final String fileName) {
String cacheKey = "asset_" + fileName;
return fromAsset(context, fileName, cacheKey);
}
/**
* Parse an animation from src/main/assets. It is recommended to use {@link #fromRawRes(Context, int)} instead.
* The asset file name will be used as a cache key so future usages won't have to parse the json again.
* However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
*
* Pass null as the cache key to skip the cache.
*
* @see #fromZipStream(ZipInputStream, String)
*/
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, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return fromAssetSync(appContext, fileName, cacheKey);
}
});
}
/**
* Parse an animation from src/main/assets. It is recommended to use {@link #fromRawRes(Context, int)} instead.
* The asset file name will be used as a cache key so future usages won't have to parse the json again.
* However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
*
* To skip the cache, add null as a third parameter.
*
* @see #fromZipStreamSync(ZipInputStream, String)
*/
@WorkerThread
public static LottieResult<LottieComposition> fromAssetSync(Context context, String fileName) {
String cacheKey = "asset_" + fileName;
return fromAssetSync(context, fileName, cacheKey);
}
/**
* Parse an animation from src/main/assets. It is recommended to use {@link #fromRawRes(Context, int)} instead.
* The asset file name will be used as a cache key so future usages won't have to parse the json again.
* However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
*
* Pass null as the cache key to skip the cache.
*
* @see #fromZipStreamSync(ZipInputStream, String)
*/
@WorkerThread
public static LottieResult<LottieComposition> fromAssetSync(Context context, String fileName, @Nullable String cacheKey) {
try {
if (fileName.endsWith(".zip") || fileName.endsWith(".lottie")) {
return fromZipStreamSync(new ZipInputStream(context.getAssets().open(fileName)), cacheKey);
}
return fromJsonInputStreamSync(context.getAssets().open(fileName), cacheKey);
} catch (IOException e) {
return new LottieResult<>(e);
}
}
/**
* Parse an animation from raw/res. This is recommended over putting your animation in assets because
* it uses a hard reference to R.
* The resource id will be used as a cache key so future usages won't parse the json again.
* Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).
* The Activity won't be leaked.
*
* To skip the cache, add null as a third parameter.
*/
public static LottieTask<LottieComposition> fromRawRes(Context context, @RawRes final int rawRes) {
return fromRawRes(context, rawRes, rawResCacheKey(context, rawRes));
}
/**
* Parse an animation from raw/res. This is recommended over putting your animation in assets because
* it uses a hard reference to R.
* The resource id will be used as a cache key so future usages won't parse the json again.
* Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).
* The Activity won't be leaked.
*
* Pass null as the cache key to skip caching.
*/
public static LottieTask<LottieComposition> fromRawRes(Context context, @RawRes final int rawRes, @Nullable String cacheKey) {
// Prevent accidentally leaking an Activity.
final WeakReference<Context> contextRef = new WeakReference<>(context);
final Context appContext = context.getApplicationContext();
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
@Nullable Context originalContext = contextRef.get();
Context context = originalContext != null ? originalContext : appContext;
return fromRawResSync(context, rawRes);
}
});
}
/**
* Parse an animation from raw/res. This is recommended over putting your animation in assets because
* it uses a hard reference to R.
* The resource id will be used as a cache key so future usages won't parse the json again.
* Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).
* The Activity won't be leaked.
*
* To skip the cache, add null as a third parameter.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromRawResSync(Context context, @RawRes int rawRes) {
return fromRawResSync(context, rawRes, rawResCacheKey(context, rawRes));
}
/**
* Parse an animation from raw/res. This is recommended over putting your animation in assets because
* it uses a hard reference to R.
* The resource id will be used as a cache key so future usages won't parse the json again.
* Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).
* The Activity won't be leaked.
*
* Pass null as the cache key to skip caching.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromRawResSync(Context context, @RawRes int rawRes, @Nullable String cacheKey) {
try {
if(isZipCompressed(context, rawRes)) {
return fromZipStreamSync(new ZipInputStream(context.getResources().openRawResource(rawRes)), cacheKey);
}
return fromJsonInputStreamSync(context.getResources().openRawResource(rawRes), cacheKey);
} catch (Resources.NotFoundException e) {
return new LottieResult<>(e);
}
}
private static String rawResCacheKey(Context context, @RawRes int resId) {
return "rawRes" + (isNightMode(context) ? "_night_" : "_day_") + resId;
}
/**
* magic header for .zip files
*/
private static final int MAGIC[] = new int[] { 0x50, 0x4b, 0x03, 0x04 };
/**
* attempts to determine if the raw res is a .zip file or not
*/
private static Boolean isZipCompressed(Context context, @RawRes int rawRes) {
final InputStream inputStream = context.getResources().openRawResource(rawRes);
try {
for (int i = 0; i < MAGIC.length; ++i) {
if (inputStream.read() != MAGIC[i]) {
return false;
}
}
inputStream.close();
return true;
} catch (IOException e) {
return false;
}
}
/**
* It is important to include day/night in the cache key so that if it changes, the cache won't return an animation from the wrong bucket.
*/
private static boolean isNightMode(Context context) {
int nightModeMasked = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
return nightModeMasked == Configuration.UI_MODE_NIGHT_YES;
}
/**
* Auto-closes the stream.
*
* @see #fromJsonInputStreamSync(InputStream, String, boolean)
*/
public static LottieTask<LottieComposition> fromJsonInputStream(final InputStream stream, @Nullable final String cacheKey) {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return fromJsonInputStreamSync(stream, cacheKey);
}
});
}
/**
* Return a LottieComposition for the given InputStream to json.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromJsonInputStreamSync(InputStream stream, @Nullable String cacheKey) {
return fromJsonInputStreamSync(stream, cacheKey, true);
}
@WorkerThread
private static LottieResult<LottieComposition> fromJsonInputStreamSync(InputStream stream, @Nullable String cacheKey, boolean close) {
try {
return fromJsonReaderSync(of(buffer(source(stream))), cacheKey);
} finally {
if (close) {
closeQuietly(stream);
}
}
}
/**
* @see #fromJsonSync(JSONObject, String)
*/
@Deprecated
public static LottieTask<LottieComposition> fromJson(final JSONObject json, @Nullable final String cacheKey) {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
//noinspection deprecation
return fromJsonSync(json, cacheKey);
}
});
}
/**
* Prefer passing in the json string directly. This method just calls `toString()` on your JSONObject.
* If you are loading this animation from the network, just use the response body string instead of
* parsing it first for improved performance.
*/
@Deprecated
@WorkerThread
public static LottieResult<LottieComposition> fromJsonSync(JSONObject json, @Nullable String cacheKey) {
return fromJsonStringSync(json.toString(), cacheKey);
}
/**
* @see #fromJsonStringSync(String, String)
*/
public static LottieTask<LottieComposition> fromJsonString(final String json, @Nullable final String cacheKey) {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return fromJsonStringSync(json, cacheKey);
}
});
}
/**
* Return a LottieComposition for the specified raw json string.
* If loading from a file, it is preferable to use the InputStream or rawRes version.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromJsonStringSync(String json, @Nullable String cacheKey) {
ByteArrayInputStream stream = new ByteArrayInputStream(json.getBytes());
return fromJsonReaderSync(of(buffer(source(stream))), cacheKey);
}
public static LottieTask<LottieComposition> fromJsonReader(final JsonReader reader, @Nullable final String cacheKey) {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return fromJsonReaderSync(reader, cacheKey);
}
});
}
@WorkerThread
public static LottieResult<LottieComposition> fromJsonReaderSync(com.airbnb.lottie.parser.moshi.JsonReader reader, @Nullable String cacheKey) {
return fromJsonReaderSyncInternal(reader, cacheKey, true);
}
private static LottieResult<LottieComposition> fromJsonReaderSyncInternal(
com.airbnb.lottie.parser.moshi.JsonReader reader, @Nullable String cacheKey, boolean close) {
try {
LottieComposition composition = LottieCompositionMoshiParser.parse(reader);
if (cacheKey != null) {
LottieCompositionCache.getInstance().put(cacheKey, composition);
}
return new LottieResult<>(composition);
} catch (Exception e) {
return new LottieResult<>(e);
} finally {
if (close) {
closeQuietly(reader);
}
}
}
public static LottieTask<LottieComposition> fromZipStream(final ZipInputStream inputStream, @Nullable final String cacheKey) {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return fromZipStreamSync(inputStream, cacheKey);
}
});
}
/**
* Parses a zip input stream into a Lottie composition.
* Your zip file should just be a folder with your json file and images zipped together.
* It will automatically store and configure any images inside the animation if they exist.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromZipStreamSync(ZipInputStream inputStream, @Nullable String cacheKey) {
try {
return fromZipStreamSyncInternal(inputStream, cacheKey);
} finally {
closeQuietly(inputStream);
}
}
@WorkerThread
private static LottieResult<LottieComposition> fromZipStreamSyncInternal(ZipInputStream inputStream, @Nullable String cacheKey) {
LottieComposition composition = null;
Map<String, Bitmap> images = new HashMap<>();
try {
ZipEntry entry = inputStream.getNextEntry();
while (entry != null) {
final String entryName = entry.getName();
if (entryName.contains("__MACOSX")) {
inputStream.closeEntry();
} else if(entry.getName().equals("manifest.json")) {
//todo: parse the manifest?
} else if (entry.getName().contains(".json")) {
com.airbnb.lottie.parser.moshi.JsonReader reader = of(buffer(source(inputStream)));
composition = DotLottieCompositionFactory.fromJsonReaderSyncInternal(reader, null, false).getValue();
} else if (entryName.contains(".png") || entryName.contains(".webp")) {
String[] splitName = entryName.split("/");
String name = splitName[splitName.length - 1];
images.put(name, BitmapFactory.decodeStream(inputStream));
} else {
inputStream.closeEntry();
}
entry = inputStream.getNextEntry();
}
} catch (IOException e) {
return new LottieResult<>(e);
}
if (composition == null) {
return new LottieResult<>(new IllegalArgumentException("Unable to parse composition"));
}
for (Map.Entry<String, Bitmap> e : images.entrySet()) {
LottieImageAsset imageAsset = findImageAssetForFileName(composition, e.getKey());
if (imageAsset != null) {
imageAsset.setBitmap(Utils.resizeBitmapIfNeeded(e.getValue(), imageAsset.getWidth(), imageAsset.getHeight()));
}
}
// Ensure that all bitmaps have been set.
for (Map.Entry<String, LottieImageAsset> entry : composition.getImages().entrySet()) {
if (entry.getValue().getBitmap() == null) {
return new LottieResult<>(new IllegalStateException("There is no image for " + entry.getValue().getFileName()));
}
}
if (cacheKey != null) {
LottieCompositionCache.getInstance().put(cacheKey, composition);
}
return new LottieResult<>(composition);
}
@Nullable
private static LottieImageAsset findImageAssetForFileName(LottieComposition composition, String fileName) {
for (LottieImageAsset asset : composition.getImages().values()) {
if (asset.getFileName().equals(fileName)) {
return asset;
}
}
return null;
}
/**
* First, check to see if there are any in-progress tasks associated with the cache key and return it if there is.
* 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) {
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieTask<>(new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return new LottieResult<>(cachedComposition);
}
});
}
if (cacheKey != null && taskCache.containsKey(cacheKey)) {
return taskCache.get(cacheKey);
}
LottieTask<LottieComposition> task = new LottieTask<>(callable);
if (cacheKey != null) {
task.addListener(new LottieListener<LottieComposition>() {
@Override
public void onResult(LottieComposition result) {
taskCache.remove(cacheKey);
}
});
task.addFailureListener(new LottieListener<Throwable>() {
@Override
public void onResult(Throwable result) {
taskCache.remove(cacheKey);
}
});
taskCache.put(cacheKey, task);
}
return task;
}
}