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