Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add the LottieConfig.Builder.setEnableNetworkCache() method to completely disable internal network cache if needed #2158

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lottie/src/main/java/com/airbnb/lottie/L.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -47,6 +49,10 @@ public static void setTraceEnabled(boolean enabled) {
}
}

public static void setNetworkCacheEnabled(boolean enabled) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a javadoc for this? Also indicate that it will evict existing cache entries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But L class is restricted to the library scope, and there are no other javadocs there. I've added javadocs to the publicly accessible LottieConfig.Builder.setEnableNetworkCache(), should I just copy-paste them here as well?
Also disabling the cache should not evict existing cache entries I believe, they will still be there until the explicit LottieCompositionFactory.clearCache() call with the cache enabled.

networkCacheEnabled = enabled;
}

public static void beginSection(String section) {
if (!traceEnabled) {
return;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lottie/src/main/java/com/airbnb/lottie/Lottie.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}

/**
Expand Down
19 changes: 16 additions & 3 deletions lottie/src/main/java/com/airbnb/lottie/LottieConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -48,7 +48,7 @@ public LottieResult<LottieComposition> 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<FileExtension, InputStream> cacheResult = networkCache.fetch(url);
Expand Down Expand Up @@ -123,7 +123,7 @@ private LottieResult<LottieComposition> 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);
}

Expand All @@ -133,7 +133,7 @@ private LottieResult<LottieComposition> fromInputStream(Context context, @NonNul
@NonNull
private LottieResult<LottieComposition> 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);
Expand All @@ -143,7 +143,7 @@ private LottieResult<LottieComposition> fromZipStream(Context context, @NonNull
@NonNull
private LottieResult<LottieComposition> 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);
Expand Down