• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.airbnb.lottie;
2 
3 import android.content.Context;
4 
5 import androidx.annotation.NonNull;
6 import androidx.annotation.Nullable;
7 import androidx.annotation.RestrictTo;
8 
9 import com.airbnb.lottie.configurations.reducemotion.ReducedMotionOption;
10 import com.airbnb.lottie.configurations.reducemotion.SystemReducedMotionOption;
11 import com.airbnb.lottie.network.DefaultLottieNetworkFetcher;
12 import com.airbnb.lottie.network.LottieNetworkCacheProvider;
13 import com.airbnb.lottie.network.LottieNetworkFetcher;
14 import com.airbnb.lottie.network.NetworkCache;
15 import com.airbnb.lottie.network.NetworkFetcher;
16 import com.airbnb.lottie.utils.LottieTrace;
17 
18 import java.io.File;
19 
20 @RestrictTo(RestrictTo.Scope.LIBRARY)
21 public class L {
22 
23   public static boolean DBG = false;
24   public static final String TAG = "LOTTIE";
25 
26   private static boolean traceEnabled = false;
27   private static boolean networkCacheEnabled = true;
28   private static boolean disablePathInterpolatorCache = true;
29   private static AsyncUpdates defaultAsyncUpdates = AsyncUpdates.AUTOMATIC;
30 
31   private static LottieNetworkFetcher fetcher;
32   private static LottieNetworkCacheProvider cacheProvider;
33 
34   private static volatile NetworkFetcher networkFetcher;
35   private static volatile NetworkCache networkCache;
36   private static ThreadLocal<LottieTrace> lottieTrace;
37   private static ReducedMotionOption reducedMotionOption = new SystemReducedMotionOption();
38 
L()39   private L() {
40   }
41 
setTraceEnabled(boolean enabled)42   public static void setTraceEnabled(boolean enabled) {
43     if (traceEnabled == enabled) {
44       return;
45     }
46     traceEnabled = enabled;
47     if (traceEnabled && lottieTrace == null) {
48       lottieTrace = new ThreadLocal<>();
49     }
50   }
51 
isTraceEnabled()52   public static boolean isTraceEnabled(){
53     return traceEnabled;
54   }
55 
setNetworkCacheEnabled(boolean enabled)56   public static void setNetworkCacheEnabled(boolean enabled) {
57     networkCacheEnabled = enabled;
58   }
59 
beginSection(String section)60   public static void beginSection(String section) {
61     if (!traceEnabled) {
62       return;
63     }
64     getTrace().beginSection(section);
65   }
66 
endSection(String section)67   public static float endSection(String section) {
68     if (!traceEnabled) {
69       return 0;
70     }
71     return getTrace().endSection(section);
72   }
73 
getTrace()74   private static LottieTrace getTrace() {
75     LottieTrace trace = lottieTrace.get();
76     if (trace == null) {
77       trace = new LottieTrace();
78       lottieTrace.set(trace);
79     }
80     return trace;
81   }
82 
setFetcher(LottieNetworkFetcher customFetcher)83   public static void setFetcher(LottieNetworkFetcher customFetcher) {
84     if ((fetcher == null && customFetcher == null) || (fetcher != null && fetcher.equals(customFetcher))) {
85       return;
86     }
87 
88     fetcher = customFetcher;
89     networkFetcher = null;
90   }
91 
setCacheProvider(LottieNetworkCacheProvider customProvider)92   public static void setCacheProvider(LottieNetworkCacheProvider customProvider) {
93     if ((cacheProvider == null && customProvider == null) || (cacheProvider != null && cacheProvider.equals(customProvider))) {
94       return;
95     }
96 
97     cacheProvider = customProvider;
98     networkCache = null;
99   }
100 
101   @NonNull
networkFetcher(@onNull Context context)102   public static NetworkFetcher networkFetcher(@NonNull Context context) {
103     NetworkFetcher local = networkFetcher;
104     if (local == null) {
105       synchronized (NetworkFetcher.class) {
106         local = networkFetcher;
107         if (local == null) {
108           networkFetcher = local = new NetworkFetcher(networkCache(context), fetcher != null ? fetcher : new DefaultLottieNetworkFetcher());
109         }
110       }
111     }
112     return local;
113   }
114 
115   @Nullable
networkCache(@onNull final Context context)116   public static NetworkCache networkCache(@NonNull final Context context) {
117     if (!networkCacheEnabled) {
118       return null;
119     }
120     final Context appContext = context.getApplicationContext();
121     NetworkCache local = networkCache;
122     if (local == null) {
123       synchronized (NetworkCache.class) {
124         local = networkCache;
125         if (local == null) {
126           networkCache = local = new NetworkCache(cacheProvider != null ? cacheProvider :
127               () -> new File(appContext.getCacheDir(), "lottie_network_cache"));
128         }
129       }
130     }
131     return local;
132   }
133 
setDisablePathInterpolatorCache(boolean disablePathInterpolatorCache)134   public static void setDisablePathInterpolatorCache(boolean disablePathInterpolatorCache) {
135     L.disablePathInterpolatorCache = disablePathInterpolatorCache;
136   }
137 
getDisablePathInterpolatorCache()138   public static boolean getDisablePathInterpolatorCache() {
139     return disablePathInterpolatorCache;
140   }
141 
setDefaultAsyncUpdates(AsyncUpdates asyncUpdates)142   public static void setDefaultAsyncUpdates(AsyncUpdates asyncUpdates) {
143     L.defaultAsyncUpdates = asyncUpdates;
144   }
145 
getDefaultAsyncUpdates()146   public static AsyncUpdates getDefaultAsyncUpdates() {
147     return L.defaultAsyncUpdates;
148   }
149 
setReducedMotionOption(ReducedMotionOption reducedMotionOption)150   public static void setReducedMotionOption(ReducedMotionOption reducedMotionOption){
151     L.reducedMotionOption = reducedMotionOption;
152   }
153 
getReducedMotionOption()154   public static ReducedMotionOption getReducedMotionOption(){return reducedMotionOption;}
155 }
156