summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogMotion.java
blob: fdf18406de94c733177785f1e0875f1fba467a14 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (C) 2015 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.volume;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnShowListener;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.PathInterpolator;

public class VolumeDialogMotion {
    private static final String TAG = Util.logTag(VolumeDialogMotion.class);

    private static final float ANIMATION_SCALE = 1.0f;
    private static final int PRE_DISMISS_DELAY = 50;
    private static final int POST_SHOW_DELAY = 200;

    private final Dialog mDialog;
    private final View mDialogView;
    private final ViewGroup mContents;  // volume rows + zen footer
    private final View mChevron;
    private final Handler mHandler = new Handler();
    private final Callback mCallback;

    private boolean mAnimating;  // show or dismiss animation is running
    private boolean mShowing;  // show animation is running
    private boolean mDismissing;  // dismiss animation is running
    private ValueAnimator mChevronPositionAnimator;
    private ValueAnimator mContentsPositionAnimator;

    public VolumeDialogMotion(Dialog dialog, View dialogView, ViewGroup contents, View chevron,
            Callback callback) {
        mDialog = dialog;
        mDialogView = dialogView;
        mContents = contents;
        mChevron = chevron;
        mCallback = callback;
        mDialog.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (D.BUG) Log.d(TAG, "mDialog.onDismiss");
            }
        });
        mDialog.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if (D.BUG) Log.d(TAG, "mDialog.onShow");
                final int h = mDialogView.getHeight();
                mDialogView.setTranslationY(-h);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startShowAnimation();
                    }
                }, POST_SHOW_DELAY);
            }
        });
    }

    public boolean isAnimating() {
        return mAnimating;
    }

    private void setShowing(boolean showing) {
        if (showing == mShowing) return;
        mShowing = showing;
        if (D.BUG) Log.d(TAG, "mShowing = " + mShowing);
        updateAnimating();
    }

    private void setDismissing(boolean dismissing) {
        if (dismissing == mDismissing) return;
        mDismissing = dismissing;
        if (D.BUG) Log.d(TAG, "mDismissing = " + mDismissing);
        updateAnimating();
    }

    private void updateAnimating() {
        final boolean animating = mShowing || mDismissing;
        if (animating == mAnimating) return;
        mAnimating = animating;
        if (D.BUG) Log.d(TAG, "mAnimating = " + mAnimating);
        if (mCallback != null) {
            mCallback.onAnimatingChanged(mAnimating);
        }
    }

    public void startShow() {
        if (D.BUG) Log.d(TAG, "startShow");
        if (mShowing) return;
        setShowing(true);
        if (mDismissing) {
            mDialogView.animate().cancel();
            setDismissing(false);
            startShowAnimation();
            return;
        }
        if (D.BUG) Log.d(TAG, "mDialog.show()");
        mDialog.show();
    }

    private int chevronDistance() {
        return mChevron.getHeight() / 6;
    }

    private void startShowAnimation() {
        if (D.BUG) Log.d(TAG, "startShowAnimation");
        mDialogView.animate()
                .translationY(0)
                .setDuration(scaledDuration(300))
                .setInterpolator(new LogDecelerateInterpolator())
                .setListener(null)
                .setUpdateListener(new AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        if (mChevronPositionAnimator == null) return;
                        // reposition chevron
                        final float v = (Float) mChevronPositionAnimator.getAnimatedValue();
                        final int posY = (Integer) mChevron.getTag();
                        mChevron.setTranslationY(posY + v + -mDialogView.getTranslationY());
                    }})
                .start();

        mContentsPositionAnimator = ValueAnimator.ofFloat(-chevronDistance(), 0)
                .setDuration(scaledDuration(400));
        mContentsPositionAnimator.addListener(new AnimatorListenerAdapter() {
            private boolean mCancelled;

            @Override
            public void onAnimationEnd(Animator animation) {
                if (mCancelled) return;
                if (D.BUG) Log.d(TAG, "show.onAnimationEnd");
                setShowing(false);
            }
            @Override
            public void onAnimationCancel(Animator animation) {
                if (D.BUG) Log.d(TAG, "show.onAnimationCancel");
                mCancelled = true;
            }
        });
        mContentsPositionAnimator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float v = (Float) animation.getAnimatedValue();
                mContents.setTranslationY(v + -mDialogView.getTranslationY());
            }
        });
        mContentsPositionAnimator.setInterpolator(new LogDecelerateInterpolator());
        mContentsPositionAnimator.start();

        mContents.setAlpha(0);
        mContents.animate()
                .alpha(1)
                .setDuration(scaledDuration(150))
                .setInterpolator(new PathInterpolator(0f, 0f, .2f, 1f))
                .start();

        mChevronPositionAnimator = ValueAnimator.ofFloat(-chevronDistance(), 0)
                .setDuration(scaledDuration(250));
        mChevronPositionAnimator.setInterpolator(new PathInterpolator(.4f, 0f, .2f, 1f));
        mChevronPositionAnimator.start();

        mChevron.setAlpha(0);
        mChevron.animate()
                .alpha(1)
                .setStartDelay(scaledDuration(50))
                .setDuration(scaledDuration(150))
                .setInterpolator(new PathInterpolator(.4f, 0f, 1f, 1f))
                .start();
    }

    public void startDismiss(final Runnable onComplete) {
        if (D.BUG) Log.d(TAG, "startDismiss");
        if (mDismissing) return;
        setDismissing(true);
        if (mShowing) {
            mDialogView.animate().cancel();
            mContentsPositionAnimator.cancel();
            mContents.animate().cancel();
            mChevronPositionAnimator.cancel();
            mChevron.animate().cancel();
            setShowing(false);
        }
        mDialogView.animate()
                .translationY(-mDialogView.getHeight())
                .setDuration(scaledDuration(250))
                .setInterpolator(new LogAccelerateInterpolator())
                .setUpdateListener(new AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        mContents.setTranslationY(-mDialogView.getTranslationY());
                        int posY = (Integer) mChevron.getTag();
                        mChevron.setTranslationY(posY + -mDialogView.getTranslationY());
                    }
                })
                .setListener(new AnimatorListenerAdapter() {
                    private boolean mCancelled;
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if (mCancelled) return;
                        if (D.BUG) Log.d(TAG, "dismiss.onAnimationEnd");
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (D.BUG) Log.d(TAG, "mDialog.dismiss()");
                                mDialog.dismiss();
                                onComplete.run();
                                setDismissing(false);
                            }
                        }, PRE_DISMISS_DELAY);

                    }
                    @Override
                    public void onAnimationCancel(Animator animation) {
                        if (D.BUG) Log.d(TAG, "dismiss.onAnimationCancel");
                        mCancelled = true;
                    }
                }).start();
    }

    private static int scaledDuration(int base) {
        return (int) (base * ANIMATION_SCALE);
    }

    private static final class LogDecelerateInterpolator implements TimeInterpolator {
        private final float mBase;
        private final float mDrift;
        private final float mTimeScale;
        private final float mOutputScale;

        private LogDecelerateInterpolator() {
            this(400f, 1.4f, 0);
        }

        private LogDecelerateInterpolator(float base, float timeScale, float drift) {
            mBase = base;
            mDrift = drift;
            mTimeScale = 1f / timeScale;

            mOutputScale = 1f / computeLog(1f);
        }

        private float computeLog(float t) {
            return 1f - (float) Math.pow(mBase, -t * mTimeScale) + (mDrift * t);
        }

        @Override
        public float getInterpolation(float t) {
            return computeLog(t) * mOutputScale;
        }
    }

    private static final class LogAccelerateInterpolator implements TimeInterpolator {
        private final int mBase;
        private final int mDrift;
        private final float mLogScale;

        private LogAccelerateInterpolator() {
            this(100, 0);
        }

        private LogAccelerateInterpolator(int base, int drift) {
            mBase = base;
            mDrift = drift;
            mLogScale = 1f / computeLog(1, mBase, mDrift);
        }

        private static float computeLog(float t, int base, int drift) {
            return (float) -Math.pow(base, -t) + 1 + (drift * t);
        }

        @Override
        public float getInterpolation(float t) {
            return 1 - computeLog(1 - t, mBase, mDrift) * mLogScale;
        }
    }

    public interface Callback {
        void onAnimatingChanged(boolean animating);
    }
}