1/*
2 * Copyright (C) 2015 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
17package com.android.systemui.assist;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.FrameLayout;
24
25import com.android.systemui.Interpolators;
26import com.android.systemui.R;
27
28public class AssistOrbContainer extends FrameLayout {
29
30    private static final long EXIT_START_DELAY = 150;
31
32    private View mScrim;
33    private View mNavbarScrim;
34    private AssistOrbView mOrb;
35
36    private boolean mAnimatingOut;
37
38    public AssistOrbContainer(Context context) {
39        this(context, null);
40    }
41
42    public AssistOrbContainer(Context context, @Nullable AttributeSet attrs) {
43        this(context, attrs, 0);
44    }
45
46    public AssistOrbContainer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
47        super(context, attrs, defStyleAttr);
48    }
49
50    @Override
51    protected void onFinishInflate() {
52        super.onFinishInflate();
53        mScrim = findViewById(R.id.assist_orb_scrim);
54        mNavbarScrim = findViewById(R.id.assist_orb_navbar_scrim);
55        mOrb = (AssistOrbView) findViewById(R.id.assist_orb);
56    }
57
58    public void show(final boolean show, boolean animate) {
59        if (show) {
60            if (getVisibility() != View.VISIBLE) {
61                setVisibility(View.VISIBLE);
62                if (animate) {
63                    startEnterAnimation();
64                } else {
65                    reset();
66                }
67            }
68        } else {
69            if (animate) {
70                startExitAnimation(new Runnable() {
71                    @Override
72                    public void run() {
73                        mAnimatingOut = false;
74                        setVisibility(View.GONE);
75                    }
76                });
77            } else {
78                setVisibility(View.GONE);
79            }
80        }
81    }
82
83    private void reset() {
84        mAnimatingOut = false;
85        mOrb.reset();
86        mScrim.setAlpha(1f);
87        mNavbarScrim.setAlpha(1f);
88    }
89
90    private void startEnterAnimation() {
91        if (mAnimatingOut) {
92            return;
93        }
94        mOrb.startEnterAnimation();
95        mScrim.setAlpha(0f);
96        mNavbarScrim.setAlpha(0f);
97        post(new Runnable() {
98            @Override
99            public void run() {
100                mScrim.animate()
101                        .alpha(1f)
102                        .setDuration(300)
103                        .setStartDelay(0)
104                        .setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
105                mNavbarScrim.animate()
106                        .alpha(1f)
107                        .setDuration(300)
108                        .setStartDelay(0)
109                        .setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
110            }
111        });
112    }
113
114    private void startExitAnimation(final Runnable endRunnable) {
115        if (mAnimatingOut) {
116            if (endRunnable != null) {
117                endRunnable.run();
118            }
119            return;
120        }
121        mAnimatingOut = true;
122        mOrb.startExitAnimation(EXIT_START_DELAY);
123        mScrim.animate()
124                .alpha(0f)
125                .setDuration(250)
126                .setStartDelay(EXIT_START_DELAY)
127                .setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
128        mNavbarScrim.animate()
129                .alpha(0f)
130                .setDuration(250)
131                .setStartDelay(EXIT_START_DELAY)
132                .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
133                .withEndAction(endRunnable);
134    }
135
136    /**
137     * Whether the panel is showing, or, if it's animating, whether it will be
138     * when the animation is done.
139     */
140    public boolean isShowing() {
141        return getVisibility() == View.VISIBLE && !mAnimatingOut;
142    }
143
144    public AssistOrbView getOrb() {
145        return mOrb;
146    }
147}
148