1 /*
2  * Copyright (C) 2021 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 
17 package com.android.managedprovisioning.common;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.graphics.ColorFilter;
22 import android.graphics.PorterDuff;
23 import android.graphics.PorterDuffColorFilter;
24 
25 import androidx.annotation.ColorRes;
26 import androidx.appcompat.app.AppCompatDelegate;
27 
28 import com.android.managedprovisioning.common.AnimationDynamicColorsHelper.AnimationWrapper;
29 
30 import com.airbnb.lottie.LottieAnimationView;
31 import com.airbnb.lottie.LottieProperty;
32 import com.airbnb.lottie.model.KeyPath;
33 import com.airbnb.lottie.value.LottieFrameInfo;
34 import com.airbnb.lottie.value.SimpleLottieValueCallback;
35 
36 /**
37  * An {@link AnimationWrapper} implementation for Lottie animations.
38  */
39 public class LottieAnimationWrapper implements AnimationWrapper {
40     private final LottieAnimationView mLottieAnimationView;
41 
LottieAnimationWrapper(LottieAnimationView lottieAnimationView)42     public LottieAnimationWrapper(LottieAnimationView lottieAnimationView) {
43         mLottieAnimationView = requireNonNull(lottieAnimationView);
44     }
45 
46     /**
47      * Loads the animation.
48      *
49      * <p>If the {@link LottieAnimationView} does not have a loaded {@link
50      * LottieComposition}, it asynchronously waits for it to load and then applies the colors.
51      */
52     @Override
loadAnimation(Runnable callback)53     public void loadAnimation(Runnable callback) {
54         if (mLottieAnimationView.getComposition() != null) {
55             callback.run();
56         } else {
57             mLottieAnimationView.addLottieOnCompositionLoadedListener(
58                     lottieComposition -> callback.run());
59         }
60     }
61 
62     @Override
setLayerColor(String layerName, @ColorRes int targetColorRes)63     public void setLayerColor(String layerName, @ColorRes int targetColorRes) {
64         KeyPath keyPath = new KeyPath("**", layerName, "**");
65         int targetColor = mLottieAnimationView.getContext().getColor(targetColorRes);
66         mLottieAnimationView.addValueCallback(keyPath, LottieProperty.COLOR_FILTER,
67                 new SimpleLottieValueCallback<ColorFilter>() {
68                     @Override
69                     public ColorFilter getValue(LottieFrameInfo<ColorFilter> frameInfo) {
70                         return new PorterDuffColorFilter(targetColor, PorterDuff.Mode.SRC_ATOP);
71                     }
72                 });
73     }
74 }
75