blob: b8bf7a8d78d06151737da756f829cc78fae28819 [file] [log] [blame]
Adrian Roos4e664b62015-03-25 15:09:46 -07001/*
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.phone;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.annotation.Nullable;
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
Adrian Roos4e664b62015-03-25 15:09:46 -070025import android.content.pm.ResolveInfo;
Adrian Roos4e664b62015-03-25 15:09:46 -070026import android.telephony.TelephonyManager;
dollyleede4f7a92018-05-03 11:22:27 +080027import android.text.Layout;
Adrian Roos4e664b62015-03-25 15:09:46 -070028import android.text.TextUtils;
29import android.util.AttributeSet;
dollyleede4f7a92018-05-03 11:22:27 +080030import android.util.TypedValue;
31import android.view.Gravity;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070032import android.view.MotionEvent;
Adrian Roos4e664b62015-03-25 15:09:46 -070033import android.view.View;
34import android.view.ViewAnimationUtils;
35import android.view.ViewGroup;
Adrian Roosee55c732015-04-13 14:53:53 -070036import android.view.accessibility.AccessibilityManager;
Adrian Roos4e664b62015-03-25 15:09:46 -070037import android.view.animation.AnimationUtils;
38import android.view.animation.Interpolator;
39import android.widget.Button;
40import android.widget.FrameLayout;
41import android.widget.TextView;
42
43import java.util.List;
44
45public class EmergencyActionGroup extends FrameLayout implements View.OnClickListener {
46
47 private static final long HIDE_DELAY = 3000;
48 private static final int RIPPLE_DURATION = 600;
49 private static final long RIPPLE_PAUSE = 1000;
50
51 private final Interpolator mFastOutLinearInInterpolator;
52
53 private ViewGroup mSelectedContainer;
54 private TextView mSelectedLabel;
55 private View mRippleView;
56 private View mLaunchHint;
57
58 private View mLastRevealed;
59
Adrian Roos1c4b47f2015-04-13 14:53:32 -070060 private MotionEvent mPendingTouchEvent;
61
62 private boolean mHiding;
63
Adrian Roos4e664b62015-03-25 15:09:46 -070064 public EmergencyActionGroup(Context context, @Nullable AttributeSet attrs) {
65 super(context, attrs);
66 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
67 android.R.interpolator.fast_out_linear_in);
68 }
69
70 @Override
71 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
72 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
73 }
74
75 @Override
76 protected void onFinishInflate() {
77 super.onFinishInflate();
Adrian Roos4e664b62015-03-25 15:09:46 -070078
79 mSelectedContainer = (ViewGroup) findViewById(R.id.selected_container);
80 mSelectedContainer.setOnClickListener(this);
81 mSelectedLabel = (TextView) findViewById(R.id.selected_label);
dollyleede4f7a92018-05-03 11:22:27 +080082 mSelectedLabel.addOnLayoutChangeListener(mLayoutChangeListener);
Adrian Roos4e664b62015-03-25 15:09:46 -070083 mRippleView = findViewById(R.id.ripple_view);
84 mLaunchHint = findViewById(R.id.launch_hint);
dollyleede4f7a92018-05-03 11:22:27 +080085 mLaunchHint.addOnLayoutChangeListener(mLayoutChangeListener);
Adrian Roos4e664b62015-03-25 15:09:46 -070086 }
87
mariagpuyold0056c02016-03-29 12:12:40 -070088 @Override
89 protected void onWindowVisibilityChanged(int visibility) {
90 super.onWindowVisibilityChanged(visibility);
91 if (visibility == View.VISIBLE) {
92 setupAssistActions();
93 }
94 }
95
Adrian Roos1c4b47f2015-04-13 14:53:32 -070096 /**
97 * Called by the activity before a touch event is dispatched to the view hierarchy.
98 */
99 public void onPreTouchEvent(MotionEvent event) {
100 mPendingTouchEvent = event;
101 }
102
103 @Override
104 public boolean dispatchTouchEvent(MotionEvent event) {
105 boolean handled = super.dispatchTouchEvent(event);
106 if (mPendingTouchEvent == event && handled) {
107 mPendingTouchEvent = null;
108 }
109 return handled;
110 }
111
112 /**
113 * Called by the activity after a touch event is dispatched to the view hierarchy.
114 */
115 public void onPostTouchEvent(MotionEvent event) {
116 // Hide the confirmation button if a touch event was delivered to the activity but not to
117 // this view.
118 if (mPendingTouchEvent != null) {
119 hideTheButton();
120 }
121 mPendingTouchEvent = null;
122 }
123
Adrian Roos4e664b62015-03-25 15:09:46 -0700124 private void setupAssistActions() {
125 int[] buttonIds = new int[] {R.id.action1, R.id.action2, R.id.action3};
126
Adrian Roos9946f8b2015-05-27 14:37:58 -0700127 List<ResolveInfo> infos;
128
129 if (TelephonyManager.EMERGENCY_ASSISTANCE_ENABLED) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800130 infos = EmergencyAssistanceHelper.resolveAssistPackageAndQueryActivities(getContext());
Adrian Roos9946f8b2015-05-27 14:37:58 -0700131 } else {
132 infos = null;
133 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700134
135 for (int i = 0; i < 3; i++) {
136 Button button = (Button) findViewById(buttonIds[i]);
137 boolean visible = false;
138
139 button.setOnClickListener(this);
140
141 if (infos != null && infos.size() > i && infos.get(i) != null) {
142 ResolveInfo info = infos.get(i);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800143 ComponentName name = EmergencyAssistanceHelper.getComponentName(info);
Adrian Roos4e664b62015-03-25 15:09:46 -0700144
145 button.setTag(R.id.tag_intent,
yuanjiahsucc83a3b2019-05-23 15:29:30 +0800146 new Intent(EmergencyAssistanceHelper.getIntentAction())
Adrian Roos4e664b62015-03-25 15:09:46 -0700147 .setComponent(name));
148 button.setText(info.loadLabel(getContext().getPackageManager()));
149 visible = true;
150 }
151
152 button.setVisibility(visible ? View.VISIBLE : View.GONE);
153 }
154 }
155
Adrian Roos4e664b62015-03-25 15:09:46 -0700156 @Override
157 public void onClick(View v) {
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700158 Intent intent = (Intent) v.getTag(R.id.tag_intent);
159
Adrian Roos4e664b62015-03-25 15:09:46 -0700160 switch (v.getId()) {
161 case R.id.action1:
162 case R.id.action2:
163 case R.id.action3:
Adrian Roosee55c732015-04-13 14:53:53 -0700164 if (AccessibilityManager.getInstance(mContext).isTouchExplorationEnabled()) {
165 getContext().startActivity(intent);
166 } else {
167 revealTheButton(v);
168 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700169 break;
170 case R.id.selected_container:
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700171 if (!mHiding) {
172 getContext().startActivity(intent);
173 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700174 break;
175 }
176 }
177
178 private void revealTheButton(View v) {
Hall Liu21a37792017-07-26 15:49:27 -0700179 CharSequence buttonText = ((Button) v).getText();
180 mSelectedLabel.setText(buttonText);
181 mSelectedLabel.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);
dollyleede4f7a92018-05-03 11:22:27 +0800182
183 // In order to trigger OnLayoutChangeListener for reset default minimum font size.
184 mSelectedLabel.requestLayout();
185 mLaunchHint.requestLayout();
186
Adrian Roos4e664b62015-03-25 15:09:46 -0700187 mSelectedContainer.setVisibility(VISIBLE);
188 int centerX = v.getLeft() + v.getWidth() / 2;
189 int centerY = v.getTop() + v.getHeight() / 2;
190 Animator reveal = ViewAnimationUtils.createCircularReveal(
191 mSelectedContainer,
192 centerX,
193 centerY,
194 0,
195 Math.max(centerX, mSelectedContainer.getWidth() - centerX)
196 + Math.max(centerY, mSelectedContainer.getHeight() - centerY));
197 reveal.start();
198
199 animateHintText(mSelectedLabel, v, reveal);
200 animateHintText(mLaunchHint, v, reveal);
201
Adrian Roos4e664b62015-03-25 15:09:46 -0700202 mSelectedContainer.setTag(R.id.tag_intent, v.getTag(R.id.tag_intent));
203 mLastRevealed = v;
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700204 postDelayed(mHideRunnable, HIDE_DELAY);
205 postDelayed(mRippleRunnable, RIPPLE_PAUSE / 2);
206
207 // Transfer focus from the originally clicked button to the expanded button.
208 mSelectedContainer.requestFocus();
Adrian Roos4e664b62015-03-25 15:09:46 -0700209 }
210
dollyleede4f7a92018-05-03 11:22:27 +0800211
212 private final OnLayoutChangeListener mLayoutChangeListener = new OnLayoutChangeListener() {
213 @Override
214 public void onLayoutChange(View v, int left, int top, int right, int bottom,
215 int oldLeft,
216 int oldTop, int oldRight, int oldBottom) {
217 decreaseAutoSizeMinTextSize(v);
218 }
219 };
220
221 /**
222 * Prevent some localization string will be truncated if there is low resolution screen
223 * or font size and display size of setting is largest.
224 */
225 private void decreaseAutoSizeMinTextSize(View selectedView) {
226 if (selectedView != null) {
227 if (selectedView instanceof TextView) {
228 TextView textView = (TextView) selectedView;
229 textView.setEllipsize(TextUtils.TruncateAt.END);
230
231 // The textView layout will be null due to it's property is hiding when
232 // initialization.
233 Layout layout = textView.getLayout();
234 if (layout != null) {
235 if (layout.getEllipsisCount(textView.getMaxLines() - 1) > 0) {
236 textView.setAutoSizeTextTypeUniformWithConfiguration(
237 8,
238 textView.getAutoSizeMaxTextSize(),
239 textView.getAutoSizeStepGranularity(),
240 TypedValue.COMPLEX_UNIT_SP);
241 textView.setGravity(Gravity.CENTER);
242 }
243 }
244 }
245 }
246 }
247
Adrian Roos4e664b62015-03-25 15:09:46 -0700248 private void animateHintText(View selectedView, View v, Animator reveal) {
249 selectedView.setTranslationX(
250 (v.getLeft() + v.getWidth() / 2 - mSelectedContainer.getWidth() / 2) / 5);
251 selectedView.animate()
252 .setDuration(reveal.getDuration() / 3)
253 .setStartDelay(reveal.getDuration() / 5)
254 .translationX(0)
255 .setInterpolator(mFastOutLinearInInterpolator)
256 .start();
257 }
258
259 private void hideTheButton() {
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700260 if (mHiding || mSelectedContainer.getVisibility() != VISIBLE) {
261 return;
262 }
263
264 mHiding = true;
265
266 removeCallbacks(mHideRunnable);
267
Adrian Roos4e664b62015-03-25 15:09:46 -0700268 View v = mLastRevealed;
269 int centerX = v.getLeft() + v.getWidth() / 2;
270 int centerY = v.getTop() + v.getHeight() / 2;
271 Animator reveal = ViewAnimationUtils.createCircularReveal(
272 mSelectedContainer,
273 centerX,
274 centerY,
275 Math.max(centerX, mSelectedContainer.getWidth() - centerX)
276 + Math.max(centerY, mSelectedContainer.getHeight() - centerY),
277 0);
278 reveal.addListener(new AnimatorListenerAdapter() {
279 @Override
280 public void onAnimationEnd(Animator animation) {
281 mSelectedContainer.setVisibility(INVISIBLE);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700282 removeCallbacks(mRippleRunnable);
283 mHiding = false;
Adrian Roos4e664b62015-03-25 15:09:46 -0700284 }
285 });
286 reveal.start();
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700287
288 // Transfer focus back to the originally clicked button.
289 if (mSelectedContainer.isFocused()) {
290 v.requestFocus();
291 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700292 }
293
294 private void startRipple() {
295 final View ripple = mRippleView;
296 ripple.animate().cancel();
297 ripple.setVisibility(VISIBLE);
298 Animator reveal = ViewAnimationUtils.createCircularReveal(
299 ripple,
300 ripple.getLeft() + ripple.getWidth() / 2,
301 ripple.getTop() + ripple.getHeight() / 2,
302 0,
303 ripple.getWidth() / 2);
304 reveal.setDuration(RIPPLE_DURATION);
305 reveal.start();
306
307 ripple.setAlpha(0);
308 ripple.animate().alpha(1).setDuration(RIPPLE_DURATION / 2)
309 .withEndAction(new Runnable() {
310 @Override
311 public void run() {
312 ripple.animate().alpha(0).setDuration(RIPPLE_DURATION / 2)
313 .withEndAction(new Runnable() {
314 @Override
315 public void run() {
316 ripple.setVisibility(INVISIBLE);
317 postDelayed(mRippleRunnable, RIPPLE_PAUSE);
318 }
319 }).start();
320 }
321 }).start();
322 }
323
324 private final Runnable mHideRunnable = new Runnable() {
325 @Override
326 public void run() {
327 if (!isAttachedToWindow()) return;
328 hideTheButton();
329 }
330 };
331
332 private final Runnable mRippleRunnable = new Runnable() {
333 @Override
334 public void run() {
335 if (!isAttachedToWindow()) return;
336 startRipple();
337 }
338 };
Adrian Roos4e664b62015-03-25 15:09:46 -0700339}