Allow configuring a default global value for async updates
diff --git a/lottie/src/main/java/com/airbnb/lottie/L.java b/lottie/src/main/java/com/airbnb/lottie/L.java index ae9f92c..c22cb11 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 @@ 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 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 816fff7..c6274a3 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 @@ 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 b186058..497a5c1 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 @@ 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 @@ private boolean enableSystraceMarkers = false; private boolean enableNetworkCache = true; private boolean disablePathInterpolatorCache = true; + private AsyncUpdates enableAsyncUpdatesByDefault = 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 @@ return this; } + /** + * Sets the default value for async updates. + * @see LottieDrawable#setAsyncUpdates(AsyncUpdates) + */ + @NonNull + public Builder setDefaultAsyncUpdates(AsyncUpdates asyncUpdates) { + enableAsyncUpdatesByDefault = asyncUpdates; + return this; + } + @NonNull public LottieConfig build() { - return new LottieConfig(networkFetcher, cacheProvider, enableSystraceMarkers, enableNetworkCache, disablePathInterpolatorCache); + return new LottieConfig(networkFetcher, cacheProvider, enableSystraceMarkers, enableNetworkCache, disablePathInterpolatorCache, + enableAsyncUpdatesByDefault); } } }
diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java b/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java index 82233c0..abf094f 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 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 @@ * 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 @@ * whether automatic is defaulting to enabled or not. */ public boolean getAsyncUpdatesEnabled() { - return asyncUpdates == AsyncUpdates.ENABLED; + return getAsyncUpdates() == AsyncUpdates.ENABLED; } /**