diff --git a/lottie/src/main/java/com/airbnb/lottie/L.java b/lottie/src/main/java/com/airbnb/lottie/L.java index 180bfbaef0..c88fd6eb6a 100644 --- a/lottie/src/main/java/com/airbnb/lottie/L.java +++ b/lottie/src/main/java/com/airbnb/lottie/L.java @@ -3,6 +3,7 @@ import android.content.Context; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; import androidx.core.os.TraceCompat; @@ -22,6 +23,7 @@ public class L { private static final int MAX_DEPTH = 20; private static boolean traceEnabled = false; + private static boolean networkCacheEnabled = true; private static String[] sections; private static long[] startTimeNs; private static int traceDepth = 0; @@ -47,6 +49,10 @@ public static void setTraceEnabled(boolean enabled) { } } + public static void setNetworkCacheEnabled(boolean enabled) { + networkCacheEnabled = enabled; + } + public static void beginSection(String section) { if (!traceEnabled) { return; @@ -103,8 +109,11 @@ public static NetworkFetcher networkFetcher(@NonNull Context context) { return local; } - @NonNull + @Nullable public static NetworkCache networkCache(@NonNull final Context context) { + if (!networkCacheEnabled) { + return null; + } final Context appContext = context.getApplicationContext(); NetworkCache local = networkCache; if (local == null) { diff --git a/lottie/src/main/java/com/airbnb/lottie/Lottie.java b/lottie/src/main/java/com/airbnb/lottie/Lottie.java index af577c76c9..8faaeb7ca8 100644 --- a/lottie/src/main/java/com/airbnb/lottie/Lottie.java +++ b/lottie/src/main/java/com/airbnb/lottie/Lottie.java @@ -19,5 +19,6 @@ public static void initialize(@NonNull final LottieConfig lottieConfig) { L.setFetcher(lottieConfig.networkFetcher); L.setCacheProvider(lottieConfig.cacheProvider); L.setTraceEnabled(lottieConfig.enableSystraceMarkers); + L.setNetworkCacheEnabled(lottieConfig.enableNetworkCache); } -} \ No newline at end of file +} diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java b/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java index a5d9da3fa3..29ff32f901 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java @@ -18,6 +18,7 @@ import com.airbnb.lottie.model.Font; import com.airbnb.lottie.model.LottieCompositionCache; +import com.airbnb.lottie.network.NetworkCache; import com.airbnb.lottie.parser.LottieCompositionMoshiParser; import com.airbnb.lottie.parser.moshi.JsonReader; import com.airbnb.lottie.utils.Logger; @@ -27,7 +28,6 @@ import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -80,7 +80,10 @@ public static void setMaxCacheSize(int size) { public static void clearCache(Context context) { taskCache.clear(); LottieCompositionCache.getInstance().clear(); - L.networkCache(context).clear(); + final NetworkCache networkCache = L.networkCache(context); + if (networkCache != null) { + networkCache.clear(); + } } /** diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieConfig.java b/lottie/src/main/java/com/airbnb/lottie/LottieConfig.java index 6a6fa8da06..be426faf03 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieConfig.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieConfig.java @@ -18,12 +18,14 @@ public class LottieConfig { @Nullable final LottieNetworkFetcher networkFetcher; @Nullable final LottieNetworkCacheProvider cacheProvider; final boolean enableSystraceMarkers; + final boolean enableNetworkCache; private LottieConfig(@Nullable LottieNetworkFetcher networkFetcher, @Nullable LottieNetworkCacheProvider cacheProvider, - boolean enableSystraceMarkers) { + boolean enableSystraceMarkers, boolean enableNetworkCache) { this.networkFetcher = networkFetcher; this.cacheProvider = cacheProvider; this.enableSystraceMarkers = enableSystraceMarkers; + this.enableNetworkCache = enableNetworkCache; } public static final class Builder { @@ -33,6 +35,7 @@ public static final class Builder { @Nullable private LottieNetworkCacheProvider cacheProvider; private boolean enableSystraceMarkers = false; + private boolean enableNetworkCache = true; /** * Lottie has a default network fetching stack built on {@link java.net.HttpURLConnection}. However, if you would like to hook into your own @@ -98,9 +101,19 @@ public Builder setEnableSystraceMarkers(boolean enable) { return this; } + /** + * Disable this if you want to completely disable internal Lottie cache for retrieving network animations. + * Internal network cache is enabled by default. + */ + @NonNull + public Builder setEnableNetworkCache(boolean enable) { + enableNetworkCache = enable; + return this; + } + @NonNull public LottieConfig build() { - return new LottieConfig(networkFetcher, cacheProvider, enableSystraceMarkers); + return new LottieConfig(networkFetcher, cacheProvider, enableSystraceMarkers, enableNetworkCache); } } -} \ No newline at end of file +} diff --git a/lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java b/lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java index 09e7a120d0..893dd2febe 100644 --- a/lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java +++ b/lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java @@ -22,12 +22,12 @@ @RestrictTo(RestrictTo.Scope.LIBRARY) public class NetworkFetcher { - @NonNull + @Nullable private final NetworkCache networkCache; @NonNull private final LottieNetworkFetcher fetcher; - public NetworkFetcher(@NonNull NetworkCache networkCache, @NonNull LottieNetworkFetcher fetcher) { + public NetworkFetcher(@Nullable NetworkCache networkCache, @NonNull LottieNetworkFetcher fetcher) { this.networkCache = networkCache; this.fetcher = fetcher; } @@ -48,7 +48,7 @@ public LottieResult fetchSync(Context context, @NonNull Strin @Nullable @WorkerThread private LottieComposition fetchFromCache(Context context, @NonNull String url, @Nullable String cacheKey) { - if (cacheKey == null) { + if (cacheKey == null || networkCache == null) { return null; } Pair cacheResult = networkCache.fetch(url); @@ -123,7 +123,7 @@ private LottieResult fromInputStream(Context context, @NonNul result = fromJsonStream(url, inputStream, cacheKey); } - if (cacheKey != null && result.getValue() != null) { + if (cacheKey != null && result.getValue() != null && networkCache != null) { networkCache.renameTempFile(url, extension); } @@ -133,7 +133,7 @@ private LottieResult fromInputStream(Context context, @NonNul @NonNull private LottieResult fromZipStream(Context context, @NonNull String url, @NonNull InputStream inputStream, @Nullable String cacheKey) throws IOException { - if (cacheKey == null) { + if (cacheKey == null || networkCache == null) { return LottieCompositionFactory.fromZipStreamSync(context, new ZipInputStream(inputStream), null); } File file = networkCache.writeTempCacheFile(url, inputStream, FileExtension.ZIP); @@ -143,7 +143,7 @@ private LottieResult fromZipStream(Context context, @NonNull @NonNull private LottieResult fromJsonStream(@NonNull String url, @NonNull InputStream inputStream, @Nullable String cacheKey) throws IOException { - if (cacheKey == null) { + if (cacheKey == null || networkCache == null) { return LottieCompositionFactory.fromJsonInputStreamSync(inputStream, null); } File file = networkCache.writeTempCacheFile(url, inputStream, FileExtension.JSON);