summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/assist/AssistOrbContainer.java
blob: f78436a8c51abe52e5a4bb00e95df2c02c37289f (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
/*
 * 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.assist;

import android.annotation.Nullable;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.android.systemui.Interpolators;
import com.android.systemui.R;

public class AssistOrbContainer extends FrameLayout {

    private static final long EXIT_START_DELAY = 150;

    private View mScrim;
    private View mNavbarScrim;
    private AssistOrbView mOrb;

    private boolean mAnimatingOut;

    public AssistOrbContainer(Context context) {
        this(context, null);
    }

    public AssistOrbContainer(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AssistOrbContainer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mScrim = findViewById(R.id.assist_orb_scrim);
        mNavbarScrim = findViewById(R.id.assist_orb_navbar_scrim);
        mOrb = (AssistOrbView) findViewById(R.id.assist_orb);
    }

    public void show(final boolean show, boolean animate) {
        if (show) {
            if (getVisibility() != View.VISIBLE) {
                setVisibility(View.VISIBLE);
                if (animate) {
                    startEnterAnimation();
                } else {
                    reset();
                }
            }
        } else {
            if (animate) {
                startExitAnimation(new Runnable() {
                    @Override
                    public void run() {
                        mAnimatingOut = false;
                        setVisibility(View.GONE);
                    }
                });
            } else {
                setVisibility(View.GONE);
            }
        }
    }

    private void reset() {
        mAnimatingOut = false;
        mOrb.reset();
        mScrim.setAlpha(1f);
        mNavbarScrim.setAlpha(1f);
    }

    private void startEnterAnimation() {
        if (mAnimatingOut) {
            return;
        }
        mOrb.startEnterAnimation();
        mScrim.setAlpha(0f);
        mNavbarScrim.setAlpha(0f);
        post(new Runnable() {
            @Override
            public void run() {
                mScrim.animate()
                        .alpha(1f)
                        .setDuration(300)
                        .setStartDelay(0)
                        .setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
                mNavbarScrim.animate()
                        .alpha(1f)
                        .setDuration(300)
                        .setStartDelay(0)
                        .setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
            }
        });
    }

    private void startExitAnimation(final Runnable endRunnable) {
        if (mAnimatingOut) {
            if (endRunnable != null) {
                endRunnable.run();
            }
            return;
        }
        mAnimatingOut = true;
        mOrb.startExitAnimation(EXIT_START_DELAY);
        mScrim.animate()
                .alpha(0f)
                .setDuration(250)
                .setStartDelay(EXIT_START_DELAY)
                .setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
        mNavbarScrim.animate()
                .alpha(0f)
                .setDuration(250)
                .setStartDelay(EXIT_START_DELAY)
                .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
                .withEndAction(endRunnable);
    }

    /**
     * Whether the panel is showing, or, if it's animating, whether it will be
     * when the animation is done.
     */
    public boolean isShowing() {
        return getVisibility() == View.VISIBLE && !mAnimatingOut;
    }

    public AssistOrbView getOrb() {
        return mOrb;
    }
}