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