1 package com.airbnb.lottie; 2 3 import android.content.Context; 4 5 import androidx.annotation.NonNull; 6 import androidx.annotation.RestrictTo; 7 import androidx.core.os.TraceCompat; 8 9 import com.airbnb.lottie.network.DefaultLottieNetworkFetcher; 10 import com.airbnb.lottie.network.LottieNetworkCacheProvider; 11 import com.airbnb.lottie.network.LottieNetworkFetcher; 12 import com.airbnb.lottie.network.NetworkCache; 13 import com.airbnb.lottie.network.NetworkFetcher; 14 15 import java.io.File; 16 17 @RestrictTo(RestrictTo.Scope.LIBRARY) 18 public class L { 19 20 public static boolean DBG = false; 21 public static final String TAG = "LOTTIE"; 22 23 private static final int MAX_DEPTH = 20; 24 private static boolean traceEnabled = false; 25 private static String[] sections; 26 private static long[] startTimeNs; 27 private static int traceDepth = 0; 28 private static int depthPastMaxDepth = 0; 29 30 private static LottieNetworkFetcher fetcher; 31 private static LottieNetworkCacheProvider cacheProvider; 32 33 private static volatile NetworkFetcher networkFetcher; 34 private static volatile NetworkCache networkCache; 35 L()36 private L() { 37 } 38 setTraceEnabled(boolean enabled)39 public static void setTraceEnabled(boolean enabled) { 40 if (traceEnabled == enabled) { 41 return; 42 } 43 traceEnabled = enabled; 44 if (traceEnabled) { 45 sections = new String[MAX_DEPTH]; 46 startTimeNs = new long[MAX_DEPTH]; 47 } 48 } 49 beginSection(String section)50 public static void beginSection(String section) { 51 if (!traceEnabled) { 52 return; 53 } 54 if (traceDepth == MAX_DEPTH) { 55 depthPastMaxDepth++; 56 return; 57 } 58 sections[traceDepth] = section; 59 startTimeNs[traceDepth] = System.nanoTime(); 60 TraceCompat.beginSection(section); 61 traceDepth++; 62 } 63 endSection(String section)64 public static float endSection(String section) { 65 if (depthPastMaxDepth > 0) { 66 depthPastMaxDepth--; 67 return 0; 68 } 69 if (!traceEnabled) { 70 return 0; 71 } 72 traceDepth--; 73 if (traceDepth == -1) { 74 throw new IllegalStateException("Can't end trace section. There are none."); 75 } 76 if (!section.equals(sections[traceDepth])) { 77 throw new IllegalStateException("Unbalanced trace call " + section + 78 ". Expected " + sections[traceDepth] + "."); 79 } 80 TraceCompat.endSection(); 81 return (System.nanoTime() - startTimeNs[traceDepth]) / 1000000f; 82 } 83 setFetcher(LottieNetworkFetcher customFetcher)84 public static void setFetcher(LottieNetworkFetcher customFetcher) { 85 fetcher = customFetcher; 86 } 87 setCacheProvider(LottieNetworkCacheProvider customProvider)88 public static void setCacheProvider(LottieNetworkCacheProvider customProvider) { 89 cacheProvider = customProvider; 90 } 91 92 @NonNull networkFetcher(@onNull Context context)93 public static NetworkFetcher networkFetcher(@NonNull Context context) { 94 NetworkFetcher local = networkFetcher; 95 if (local == null) { 96 synchronized (NetworkFetcher.class) { 97 local = networkFetcher; 98 if (local == null) { 99 networkFetcher = local = new NetworkFetcher(networkCache(context), fetcher != null ? fetcher : new DefaultLottieNetworkFetcher()); 100 } 101 } 102 } 103 return local; 104 } 105 106 @NonNull networkCache(@onNull final Context context)107 public static NetworkCache networkCache(@NonNull final Context context) { 108 final Context appContext = context.getApplicationContext(); 109 NetworkCache local = networkCache; 110 if (local == null) { 111 synchronized (NetworkCache.class) { 112 local = networkCache; 113 if (local == null) { 114 networkCache = local = new NetworkCache(cacheProvider != null ? cacheProvider : new LottieNetworkCacheProvider() { 115 @Override @NonNull public File getCacheDir() { 116 return new File(appContext.getCacheDir(), "lottie_network_cache"); 117 } 118 }); 119 } 120 } 121 } 122 return local; 123 } 124 } 125