Merge "Refactor LottieAnimationColorUtils to enforce proper usage" into main

This commit is contained in:
Schneider Victor-tulias authored and Android (Google) Code Review committed 2023-11-28 19:49:36 +00:00
commit 812bc2a9c5
6 files changed
+32 -46

No files matched your search

@@ -251,5 +251,5 @@ private fun LottieAnimationView.supportLightTheme() {
return
}
LottieAnimationColorUtils.updateColors(this, DARK_TO_LIGHT_COLORS, context.theme)
LottieAnimationColorUtils.updateToColorResources(this, DARK_TO_LIGHT_COLORS, context.theme)
}
@@ -184,7 +184,7 @@ public class AllSetActivity extends Activity {
mAnimatedBackground.setAnimation(resources.openRawResource(R.raw.all_set_page_bg),
null);
LottieAnimationColorUtils.updateColors(
LottieAnimationColorUtils.updateToColorResources(
mAnimatedBackground,
Map.of(LOTTIE_PRIMARY_COLOR_TOKEN, R.color.all_set_bg_primary,
LOTTIE_TERTIARY_COLOR_TOKEN, R.color.all_set_bg_tertiary),
@@ -41,13 +41,13 @@ final class BackGestureTutorialController extends TutorialController {
super(fragment, tutorialType);
// Set the Lottie animation colors specifically for the Back gesture
if (ENABLE_NEW_GESTURE_NAV_TUTORIAL.get()) {
LottieAnimationColorUtils.updateColors(
LottieAnimationColorUtils.updateToArgbColors(
mAnimatedGestureDemonstration,
Map.of(".onSurfaceBack", fragment.mRootView.mColorOnSurfaceBack,
".surfaceBack", fragment.mRootView.mColorSurfaceBack,
".secondaryBack", fragment.mRootView.mColorSecondaryBack));
LottieAnimationColorUtils.updateColors(
LottieAnimationColorUtils.updateToArgbColors(
mCheckmarkAnimation,
Map.of(".checkmark",
Utilities.isDarkTheme(mContext)
@@ -38,13 +38,13 @@ final class HomeGestureTutorialController extends SwipeUpGestureTutorialControll
// Set the Lottie animation colors specifically for the Home gesture
if (ENABLE_NEW_GESTURE_NAV_TUTORIAL.get()) {
LottieAnimationColorUtils.updateColors(
LottieAnimationColorUtils.updateToArgbColors(
mAnimatedGestureDemonstration,
Map.of(".onSurfaceHome", fragment.mRootView.mColorOnSurfaceHome,
".surfaceHome", fragment.mRootView.mColorSurfaceHome,
".secondaryHome", fragment.mRootView.mColorSecondaryHome));
LottieAnimationColorUtils.updateColors(
LottieAnimationColorUtils.updateToArgbColors(
mCheckmarkAnimation,
Map.of(".checkmark",
Utilities.isDarkTheme(mContext)
@@ -53,13 +53,13 @@ final class OverviewGestureTutorialController extends SwipeUpGestureTutorialCont
// Set the Lottie animation colors specifically for the Overview gesture
if (ENABLE_NEW_GESTURE_NAV_TUTORIAL.get()) {
LottieAnimationColorUtils.updateColors(
LottieAnimationColorUtils.updateToArgbColors(
mAnimatedGestureDemonstration,
Map.of(".onSurfaceOverview", fragment.mRootView.mColorOnSurfaceOverview,
".surfaceOverview", fragment.mRootView.mColorSurfaceOverview,
".secondaryOverview", fragment.mRootView.mColorSecondaryOverview));
LottieAnimationColorUtils.updateColors(
LottieAnimationColorUtils.updateToArgbColors(
mCheckmarkAnimation,
Map.of(".checkmark",
Utilities.isDarkTheme(mContext)
@@ -17,13 +17,11 @@ package com.android.quickstep.util;
import static com.airbnb.lottie.LottieProperty.COLOR_FILTER;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.model.KeyPath;
@@ -40,51 +38,39 @@ public final class LottieAnimationColorUtils {
/**
* Updates the given Lottie animation's tokenized colors according to the given mapping.
* <p>
* Use this method signature only when {@code tokenToColorCodeMap} maps to packed ARBG color
* integers.
* <p>
* @param animationView {@link LottieAnimationView} whose animation's colors need to be updated
* @param tokenToColorCodeMap A mapping from the color tokens used in the Lottie file used in
* @param tokenToArgbColorMap A mapping from the color tokens used in the Lottie file used in
* {@code animationView} to packed ARBG color integers.
*/
public static void updateColors(
public static void updateToArgbColors(
@NonNull LottieAnimationView animationView,
@NonNull Map<String, Integer> tokenToColorCodeMap) {
updateColors(animationView, tokenToColorCodeMap, null);
}
/**
* Updates the given Lottie animation's tokenized colors according to the given mapping.
* <p>
* Use this method signature with a non-null theme only when {@code tokenToColorCodeMap} maps
* to color resource references.
* <p>
* @param animationView {@link LottieAnimationView} whose animation's colors need to be updated
* @param tokenToColorCodeMap A mapping from the color tokens used in the Lottie file used in
* {@code animationView} to packed ARBG color integers or color
* resource references.
* @param theme {@link Theme} to be used when resolving color resource references. {@code null}
* iff {@code tokenToColorCodeMap} maps to packed ARBG color integers.
*/
public static void updateColors(
@NonNull LottieAnimationView animationView,
@NonNull Map<String, Integer> tokenToColorCodeMap,
@Nullable Theme theme) {
Resources resources = animationView.getResources();
final Map<String, Integer> tokenToColorMap = theme == null
// tokenToColorCodeMap maps directly to ARBG values
? tokenToColorCodeMap
// tokenToColorCodeMap maps to color references, build a mapping to resolved colors
: tokenToColorCodeMap.keySet().stream().collect(Collectors.toMap(
Function.identity(),
token -> resources.getColor(tokenToColorCodeMap.get(token), theme)));
@NonNull Map<String, Integer> tokenToArgbColorMap) {
animationView.addLottieOnCompositionLoadedListener(
composition -> tokenToColorMap.forEach(
composition -> tokenToArgbColorMap.forEach(
(token, color) -> animationView.addValueCallback(
new KeyPath("**", token, "**"),
COLOR_FILTER,
frameInfo -> new PorterDuffColorFilter(
color, PorterDuff.Mode.SRC_ATOP))));
}
/**
* Updates the given Lottie animation's tokenized colors according to the given mapping.
* <p>
* @param animationView {@link LottieAnimationView} whose animation's colors need to be updated
* @param tokenToColorResourceMap A mapping from the color tokens used in the Lottie file used
* in {@code animationView} to color resource references.
* @param theme {@link Theme} to be used when resolving color resource references.
*/
public static void updateToColorResources(
@NonNull LottieAnimationView animationView,
@NonNull Map<String, Integer> tokenToColorResourceMap,
@NonNull Theme theme) {
updateToArgbColors(
animationView,
tokenToColorResourceMap.keySet().stream().collect(Collectors.toMap(
Function.identity(),
token -> animationView.getResources().getColor(
tokenToColorResourceMap.get(token), theme))));
}
}