1 /*
2  * Copyright (C) 2017 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.launcher3.anim;
17 
18 import android.animation.Animator;
19 import android.animation.AnimatorSet;
20 import android.util.SparseArray;
21 import android.view.animation.Interpolator;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * Utility class for building animator set
28  */
29 public class AnimatorSetBuilder {
30 
31     public static final int ANIM_VERTICAL_PROGRESS = 0;
32     public static final int ANIM_WORKSPACE_SCALE = 1;
33     public static final int ANIM_WORKSPACE_TRANSLATE = 2;
34     public static final int ANIM_WORKSPACE_FADE = 3;
35     public static final int ANIM_HOTSEAT_SCALE = 4;
36     public static final int ANIM_HOTSEAT_TRANSLATE = 5;
37     public static final int ANIM_OVERVIEW_SCALE = 6;
38     public static final int ANIM_OVERVIEW_TRANSLATE_X = 7;
39     public static final int ANIM_OVERVIEW_TRANSLATE_Y = 8;
40     public static final int ANIM_OVERVIEW_FADE = 9;
41     public static final int ANIM_ALL_APPS_FADE = 10;
42     public static final int ANIM_OVERVIEW_SCRIM_FADE = 11;
43     public static final int ANIM_ALL_APPS_HEADER_FADE = 12; // e.g. predictions
44 
45     public static final int FLAG_DONT_ANIMATE_OVERVIEW = 1 << 0;
46 
47     protected final ArrayList<Animator> mAnims = new ArrayList<>();
48 
49     private final SparseArray<Interpolator> mInterpolators = new SparseArray<>();
50     private List<Runnable> mOnFinishRunnables = new ArrayList<>();
51     private int mFlags = 0;
52 
play(Animator anim)53     public void play(Animator anim) {
54         mAnims.add(anim);
55     }
56 
addOnFinishRunnable(Runnable onFinishRunnable)57     public void addOnFinishRunnable(Runnable onFinishRunnable) {
58         mOnFinishRunnables.add(onFinishRunnable);
59     }
60 
build()61     public AnimatorSet build() {
62         AnimatorSet anim = new AnimatorSet();
63         anim.playTogether(mAnims);
64         if (!mOnFinishRunnables.isEmpty()) {
65             anim.addListener(new AnimationSuccessListener() {
66                 @Override
67                 public void onAnimationSuccess(Animator animation) {
68                     for (Runnable onFinishRunnable : mOnFinishRunnables) {
69                         onFinishRunnable.run();
70                     }
71                     mOnFinishRunnables.clear();
72                 }
73             });
74         }
75         return anim;
76     }
77 
getInterpolator(int animId, Interpolator fallback)78     public Interpolator getInterpolator(int animId, Interpolator fallback) {
79         return mInterpolators.get(animId, fallback);
80     }
81 
setInterpolator(int animId, Interpolator interpolator)82     public void setInterpolator(int animId, Interpolator interpolator) {
83         mInterpolators.put(animId, interpolator);
84     }
85 
addFlag(int flag)86     public void addFlag(int flag) {
87         mFlags |= flag;
88     }
89 
hasFlag(int flag)90     public boolean hasFlag(int flag) {
91         return (mFlags & flag) != 0;
92     }
93 }
94