summaryrefslogtreecommitdiff
path: root/com/android/systemui/recents/views/AnimationProps.java
blob: 716d1bcf78c22590902a6b450f5f8cc33a643d23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.recents.views;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.IntDef;
import android.util.SparseArray;
import android.util.SparseLongArray;
import android.view.View;
import android.view.animation.Interpolator;

import com.android.systemui.Interpolators;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

/**
 * The generic set of animation properties to animate a {@link View}. The animation can have
 * different interpolators, start delays and durations for each of the different properties.
 */
public class AnimationProps {

    public static final AnimationProps IMMEDIATE = new AnimationProps(0, Interpolators.LINEAR);

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({ALL, TRANSLATION_X, TRANSLATION_Y, TRANSLATION_Z, ALPHA, SCALE, BOUNDS})
    public @interface PropType {}

    public static final int ALL = 0;
    public static final int TRANSLATION_X = 1;
    public static final int TRANSLATION_Y = 2;
    public static final int TRANSLATION_Z = 3;
    public static final int ALPHA = 4;
    public static final int SCALE = 5;
    public static final int BOUNDS = 6;
    public static final int DIM_ALPHA = 7;
    public static final int FOCUS_STATE = 8;

    private SparseLongArray mPropStartDelay;
    private SparseLongArray mPropDuration;
    private SparseArray<Interpolator> mPropInterpolators;
    private Animator.AnimatorListener mListener;

    /**
     * The builder constructor.
     */
    public AnimationProps() {}

    /**
     * Creates an animation with a default {@param duration} and {@param interpolator} for all
     * properties in this animation.
     */
    public AnimationProps(int duration, Interpolator interpolator) {
        this(0, duration, interpolator, null);
    }

    /**
     * Creates an animation with a default {@param duration} and {@param interpolator} for all
     * properties in this animation.
     */
    public AnimationProps(int duration, Interpolator interpolator,
            Animator.AnimatorListener listener) {
        this(0, duration, interpolator, listener);
    }

    /**
     * Creates an animation with a default {@param startDelay}, {@param duration} and
     * {@param interpolator} for all properties in this animation.
     */
    public AnimationProps(int startDelay, int duration, Interpolator interpolator) {
        this(startDelay, duration, interpolator, null);
    }

    /**
     * Creates an animation with a default {@param startDelay}, {@param duration} and
     * {@param interpolator} for all properties in this animation.
     */
    public AnimationProps(int startDelay, int duration, Interpolator interpolator,
            Animator.AnimatorListener listener) {
        setStartDelay(ALL, startDelay);
        setDuration(ALL, duration);
        setInterpolator(ALL, interpolator);
        setListener(listener);
    }

    /**
     * Creates a new {@link AnimatorSet} that will animate the given animators.  Callers need to
     * manually apply the individual animation properties for each of the animators respectively.
     */
    public AnimatorSet createAnimator(List<Animator> animators) {
        AnimatorSet anim = new AnimatorSet();
        if (mListener != null) {
            anim.addListener(mListener);
        }
        anim.playTogether(animators);
        return anim;
    }

    /**
     * Applies the specific start delay, duration and interpolator to the given {@param animator}
     * for the specified {@param propertyType}.
     */
    public <T extends ValueAnimator> T apply(@PropType int propertyType, T animator) {
        animator.setStartDelay(getStartDelay(propertyType));
        animator.setDuration(getDuration(propertyType));
        animator.setInterpolator(getInterpolator(propertyType));
        return animator;
    }

    /**
     * Sets a start delay for a specific property.
     */
    public AnimationProps setStartDelay(@PropType int propertyType, int startDelay) {
        if (mPropStartDelay == null) {
            mPropStartDelay = new SparseLongArray();
        }
        mPropStartDelay.append(propertyType, startDelay);
        return this;
    }

    /**
     * Returns the start delay for a specific property.
     */
    public long getStartDelay(@PropType int propertyType) {
        if (mPropStartDelay != null) {
            long startDelay = mPropStartDelay.get(propertyType, -1);
            if (startDelay != -1) {
                return startDelay;
            }
            return mPropStartDelay.get(ALL, 0);
        }
        return 0;
    }

    /**
     * Sets a duration for a specific property.
     */
    public AnimationProps setDuration(@PropType int propertyType, int duration) {
        if (mPropDuration == null) {
            mPropDuration = new SparseLongArray();
        }
        mPropDuration.append(propertyType, duration);
        return this;
    }

    /**
     * Returns the duration for a specific property.
     */
    public long getDuration(@PropType int propertyType) {
        if (mPropDuration != null) {
            long duration = mPropDuration.get(propertyType, -1);
            if (duration != -1) {
                return duration;
            }
            return mPropDuration.get(ALL, 0);
        }
        return 0;
    }

    /**
     * Sets an interpolator for a specific property.
     */
    public AnimationProps setInterpolator(@PropType int propertyType, Interpolator interpolator) {
        if (mPropInterpolators == null) {
            mPropInterpolators = new SparseArray<>();
        }
        mPropInterpolators.append(propertyType, interpolator);
        return this;
    }

    /**
     * Returns the interpolator for a specific property, falling back to the general interpolator
     * if there is no specific property interpolator.
     */
    public Interpolator getInterpolator(@PropType int propertyType) {
        if (mPropInterpolators != null) {
            Interpolator interp = mPropInterpolators.get(propertyType);
            if (interp != null) {
                return interp;
            }
            return mPropInterpolators.get(ALL, Interpolators.LINEAR);
        }
        return Interpolators.LINEAR;
    }

    /**
     * Sets an animator listener for this animation.
     */
    public AnimationProps setListener(Animator.AnimatorListener listener) {
        mListener = listener;
        return this;
    }

    /**
     * Returns the animator listener for this animation.
     */
    public Animator.AnimatorListener getListener() {
        return mListener;
    }

    /**
     * Returns whether this animation has any duration.
     */
    public boolean isImmediate() {
        int count = mPropDuration.size();
        for (int i = 0; i < count; i++) {
            if (mPropDuration.valueAt(i) > 0) {
                return false;
            }
        }
        return true;
    }
}