summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/recent/Choreographer.java
blob: b876075565aa1b69c40c17be8ba9e6d514e2d3f9 (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
/*
 * Copyright (C) 2011 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.recent;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.util.Log;
import android.util.Slog;
import android.view.View;

/* package */ class Choreographer implements Animator.AnimatorListener {
    // should group this into a multi-property animation
    private static final int OPEN_DURATION = 136;
    private static final int CLOSE_DURATION = 250;
    private static final String TAG = RecentsPanelView.TAG;
    private static final boolean DEBUG = RecentsPanelView.DEBUG;

    boolean mVisible;
    int mPanelHeight;
    View mRootView;
    View mScrimView;
    View mContentView;
    AnimatorSet mContentAnim;

    // the panel will start to appear this many px from the end
    final int HYPERSPACE_OFFRAMP = 200;

    public Choreographer(View root, View scrim, View content) {
        mRootView = root;
        mScrimView = scrim;
        mContentView = content;
    }

    void createAnimation(boolean appearing) {
        float start, end;

        if (RecentsPanelView.DEBUG) Log.e(TAG, "createAnimation()", new Exception());

        // 0: on-screen
        // height: off-screen
        float y = mContentView.getTranslationY();
        if (appearing) {
            // we want to go from near-the-top to the top, unless we're half-open in the right
            // general vicinity
            start = (y < HYPERSPACE_OFFRAMP) ? y : HYPERSPACE_OFFRAMP;
            end = 0;
        } else {
            start = y;
            end = y + HYPERSPACE_OFFRAMP;
        }

        Animator posAnim = ObjectAnimator.ofFloat(mContentView, "translationY",
                start, end);
        posAnim.setInterpolator(appearing
                ? new android.view.animation.DecelerateInterpolator(2.5f)
                : new android.view.animation.AccelerateInterpolator(2.5f));

        Animator glowAnim = ObjectAnimator.ofFloat(mContentView, "alpha",
                mContentView.getAlpha(), appearing ? 1.0f : 0.0f);
        glowAnim.setInterpolator(appearing
                ? new android.view.animation.AccelerateInterpolator(1.0f)
                : new android.view.animation.DecelerateInterpolator(1.0f));

        Animator bgAnim = ObjectAnimator.ofInt(mScrimView.getBackground(),
                "alpha", appearing ? 0 : 255, appearing ? 255 : 0);

        mContentAnim = new AnimatorSet();
        mContentAnim
                .play(bgAnim)
                .with(glowAnim)
                .with(posAnim);
        mContentAnim.setDuration(appearing ? OPEN_DURATION : CLOSE_DURATION);
        mContentAnim.addListener(this);
    }

    void startAnimation(boolean appearing) {
        if (DEBUG) Slog.d(TAG, "startAnimation(appearing=" + appearing + ")");

        createAnimation(appearing);

        mContentView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        mContentAnim.start();

        mVisible = appearing;
    }

    void jumpTo(boolean appearing) {
        mContentView.setTranslationY(appearing ? 0 : mPanelHeight);
    }

    public void setPanelHeight(int h) {
        if (DEBUG) Slog.d(TAG, "panelHeight=" + h);
        mPanelHeight = h;
    }

    public void onAnimationCancel(Animator animation) {
        if (DEBUG) Slog.d(TAG, "onAnimationCancel");
        // force this to zero so we close the window
        mVisible = false;
    }

    public void onAnimationEnd(Animator animation) {
        if (DEBUG) Slog.d(TAG, "onAnimationEnd");
        if (!mVisible) {
            mRootView.setVisibility(View.GONE);
        }
        mContentView.setLayerType(View.LAYER_TYPE_NONE, null);
        mContentAnim = null;
    }

    public void onAnimationRepeat(Animator animation) {
    }

    public void onAnimationStart(Animator animation) {
    }
}