1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.quickstep.util;
17 
18 import static com.airbnb.lottie.LottieProperty.COLOR_FILTER;
19 
20 import android.content.res.Resources.Theme;
21 import android.graphics.PorterDuff;
22 import android.graphics.PorterDuffColorFilter;
23 
24 import androidx.annotation.NonNull;
25 
26 import com.airbnb.lottie.LottieAnimationView;
27 import com.airbnb.lottie.model.KeyPath;
28 
29 import java.util.Map;
30 import java.util.function.Function;
31 import java.util.stream.Collectors;
32 
33 /** Utility class for programmatically updating Lottie animation tokenized colors. */
34 public final class LottieAnimationColorUtils {
35 
LottieAnimationColorUtils()36     private LottieAnimationColorUtils() {}
37 
38     /**
39      * Updates the given Lottie animation's tokenized colors according to the given mapping.
40      * <p>
41      * @param animationView {@link LottieAnimationView} whose animation's colors need to be updated
42      * @param tokenToArgbColorMap A mapping from the color tokens used in the Lottie file used in
43      *                            {@code animationView} to packed ARBG color integers.
44      */
updateToArgbColors( @onNull LottieAnimationView animationView, @NonNull Map<String, Integer> tokenToArgbColorMap)45     public static void updateToArgbColors(
46             @NonNull LottieAnimationView animationView,
47             @NonNull Map<String, Integer> tokenToArgbColorMap) {
48         animationView.addLottieOnCompositionLoadedListener(
49                 composition -> tokenToArgbColorMap.forEach(
50                         (token, color) -> animationView.addValueCallback(
51                                 new KeyPath("**", token, "**"),
52                                 COLOR_FILTER,
53                                 frameInfo -> new PorterDuffColorFilter(
54                                         color, PorterDuff.Mode.SRC_ATOP))));
55     }
56 
57     /**
58      * Updates the given Lottie animation's tokenized colors according to the given mapping.
59      * <p>
60      * @param animationView {@link LottieAnimationView} whose animation's colors need to be updated
61      * @param tokenToColorResourceMap A mapping from the color tokens used in the Lottie file used
62      *                                in {@code animationView} to color resource references.
63      * @param theme {@link Theme} to be used when resolving color resource references.
64      */
updateToColorResources( @onNull LottieAnimationView animationView, @NonNull Map<String, Integer> tokenToColorResourceMap, @NonNull Theme theme)65     public static void updateToColorResources(
66             @NonNull LottieAnimationView animationView,
67             @NonNull Map<String, Integer> tokenToColorResourceMap,
68             @NonNull Theme theme) {
69         updateToArgbColors(
70                 animationView,
71                 tokenToColorResourceMap.keySet().stream().collect(Collectors.toMap(
72                         Function.identity(),
73                         token -> animationView.getResources().getColor(
74                                 tokenToColorResourceMap.get(token), theme))));
75     }
76 }
77