From c76b9f00cff28ba9a7c5f319db4aa23b75a8d9a7 Mon Sep 17 00:00:00 2001 From: Gabriel Peal Date: Sat, 2 Sep 2023 18:54:57 -0500 Subject: [PATCH] Allow configuring a default global value for async updates (#2356) --- lottie/src/main/java/com/airbnb/lottie/L.java | 10 +++++++++- .../main/java/com/airbnb/lottie/Lottie.java | 1 + .../java/com/airbnb/lottie/LottieConfig.java | 19 +++++++++++++++++-- .../com/airbnb/lottie/LottieDrawable.java | 12 +++++++++--- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lottie/src/main/java/com/airbnb/lottie/L.java b/lottie/src/main/java/com/airbnb/lottie/L.java index ae9f92c57c..c22cb114ad 100644 --- a/lottie/src/main/java/com/airbnb/lottie/L.java +++ b/lottie/src/main/java/com/airbnb/lottie/L.java @@ -5,7 +5,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; -import androidx.core.os.TraceCompat; import com.airbnb.lottie.network.DefaultLottieNetworkFetcher; import com.airbnb.lottie.network.LottieNetworkCacheProvider; @@ -25,6 +24,7 @@ public class L { private static boolean traceEnabled = false; private static boolean networkCacheEnabled = true; private static boolean disablePathInterpolatorCache = true; + private static AsyncUpdates defaultAsyncUpdates = AsyncUpdates.AUTOMATIC; private static LottieNetworkFetcher fetcher; private static LottieNetworkCacheProvider cacheProvider; @@ -131,4 +131,12 @@ public static void setDisablePathInterpolatorCache(boolean disablePathInterpolat public static boolean getDisablePathInterpolatorCache() { return disablePathInterpolatorCache; } + + public static void setDefaultAsyncUpdates(AsyncUpdates asyncUpdates) { + L.defaultAsyncUpdates = asyncUpdates; + } + + public static AsyncUpdates getDefaultAsyncUpdates() { + return L.defaultAsyncUpdates; + } } diff --git a/lottie/src/main/java/com/airbnb/lottie/Lottie.java b/lottie/src/main/java/com/airbnb/lottie/Lottie.java index 816fff71bf..c6274a39cb 100644 --- a/lottie/src/main/java/com/airbnb/lottie/Lottie.java +++ b/lottie/src/main/java/com/airbnb/lottie/Lottie.java @@ -21,5 +21,6 @@ public static void initialize(@NonNull final LottieConfig lottieConfig) { L.setTraceEnabled(lottieConfig.enableSystraceMarkers); L.setNetworkCacheEnabled(lottieConfig.enableNetworkCache); L.setDisablePathInterpolatorCache(lottieConfig.disablePathInterpolatorCache); + L.setDefaultAsyncUpdates(lottieConfig.defaultAsyncUpdates); } } diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieConfig.java b/lottie/src/main/java/com/airbnb/lottie/LottieConfig.java index b186058635..09f5e57bcb 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieConfig.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieConfig.java @@ -20,14 +20,17 @@ public class LottieConfig { final boolean enableSystraceMarkers; final boolean enableNetworkCache; final boolean disablePathInterpolatorCache; + final AsyncUpdates defaultAsyncUpdates; private LottieConfig(@Nullable LottieNetworkFetcher networkFetcher, @Nullable LottieNetworkCacheProvider cacheProvider, - boolean enableSystraceMarkers, boolean enableNetworkCache, boolean disablePathInterpolatorCache) { + boolean enableSystraceMarkers, boolean enableNetworkCache, boolean disablePathInterpolatorCache, + AsyncUpdates defaultAsyncUpdates) { this.networkFetcher = networkFetcher; this.cacheProvider = cacheProvider; this.enableSystraceMarkers = enableSystraceMarkers; this.enableNetworkCache = enableNetworkCache; this.disablePathInterpolatorCache = disablePathInterpolatorCache; + this.defaultAsyncUpdates = defaultAsyncUpdates; } public static final class Builder { @@ -39,6 +42,7 @@ public static final class Builder { private boolean enableSystraceMarkers = false; private boolean enableNetworkCache = true; private boolean disablePathInterpolatorCache = true; + private AsyncUpdates defaultAsyncUpdates = AsyncUpdates.AUTOMATIC; /** * Lottie has a default network fetching stack built on {@link java.net.HttpURLConnection}. However, if you would like to hook into your own @@ -127,9 +131,20 @@ public Builder setDisablePathInterpolatorCache(boolean disable) { return this; } + /** + * Sets the default value for async updates. + * @see LottieDrawable#setAsyncUpdates(AsyncUpdates) + */ + @NonNull + public Builder setDefaultAsyncUpdates(AsyncUpdates asyncUpdates) { + defaultAsyncUpdates = asyncUpdates; + return this; + } + @NonNull public LottieConfig build() { - return new LottieConfig(networkFetcher, cacheProvider, enableSystraceMarkers, enableNetworkCache, disablePathInterpolatorCache); + return new LottieConfig(networkFetcher, cacheProvider, enableSystraceMarkers, enableNetworkCache, disablePathInterpolatorCache, + defaultAsyncUpdates); } } } diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java b/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java index 82233c0afd..abf094f56b 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java @@ -31,6 +31,7 @@ import androidx.annotation.RestrictTo; import com.airbnb.lottie.animation.LPaint; +import com.airbnb.lottie.animation.keyframe.PathKeyframe; import com.airbnb.lottie.manager.FontAssetManager; import com.airbnb.lottie.manager.ImageAssetManager; import com.airbnb.lottie.model.Font; @@ -145,7 +146,8 @@ private enum OnVisibleAction { private Matrix softwareRenderingOriginalCanvasMatrix; private Matrix softwareRenderingOriginalCanvasMatrixInverse; - private AsyncUpdates asyncUpdates = AsyncUpdates.AUTOMATIC; + /** Use the getter so that it can fall back to {@link L#getDefaultAsyncUpdates()}. */ + @Nullable private AsyncUpdates asyncUpdates; private final ValueAnimator.AnimatorUpdateListener progressUpdateListener = animation -> { if (getAsyncUpdatesEnabled()) { // Render a new frame. @@ -411,7 +413,11 @@ public void setRenderMode(RenderMode renderMode) { * Returns the current value of {@link AsyncUpdates}. Refer to the docs for {@link AsyncUpdates} for more info. */ public AsyncUpdates getAsyncUpdates() { - return asyncUpdates; + AsyncUpdates asyncUpdates = this.asyncUpdates; + if (asyncUpdates != null) { + return asyncUpdates; + } + return L.getDefaultAsyncUpdates(); } /** @@ -421,7 +427,7 @@ public AsyncUpdates getAsyncUpdates() { * whether automatic is defaulting to enabled or not. */ public boolean getAsyncUpdatesEnabled() { - return asyncUpdates == AsyncUpdates.ENABLED; + return getAsyncUpdates() == AsyncUpdates.ENABLED; } /**