blob: 53ec1eb306014782f0b25ac907a5161a645f8cbb [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
Amit Mahajan28a499c2019-11-20 15:13:42 -0800160 if (v.getId() == R.id.action1 || v.getId() == R.id.action2 || v.getId() == R.id.action3) {
161 AccessibilityManager accessibilityMgr =
162 (AccessibilityManager) mContext.getSystemService(
163 Context.ACCESSIBILITY_SERVICE);
164 if (accessibilityMgr.isTouchExplorationEnabled()) {
165 getContext().startActivity(intent);
166 } else {
167 revealTheButton(v);
168 }
169 } else if (v.getId() == R.id.selected_container) {
170 if (!mHiding) {
171 getContext().startActivity(intent);
172 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700173 }
174 }
175
176 private void revealTheButton(View v) {
Hall Liu21a37792017-07-26 15:49:27 -0700177 CharSequence buttonText = ((Button) v).getText();
178 mSelectedLabel.setText(buttonText);
179 mSelectedLabel.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);
dollyleede4f7a92018-05-03 11:22:27 +0800180
181 // In order to trigger OnLayoutChangeListener for reset default minimum font size.
182 mSelectedLabel.requestLayout();
183 mLaunchHint.requestLayout();
184
Adrian Roos4e664b62015-03-25 15:09:46 -0700185 mSelectedContainer.setVisibility(VISIBLE);
186 int centerX = v.getLeft() + v.getWidth() / 2;
187 int centerY = v.getTop() + v.getHeight() / 2;
188 Animator reveal = ViewAnimationUtils.createCircularReveal(
189 mSelectedContainer,
190 centerX,
191 centerY,
192 0,
193 Math.max(centerX, mSelectedContainer.getWidth() - centerX)
194 + Math.max(centerY, mSelectedContainer.getHeight() - centerY));
195 reveal.start();
196
197 animateHintText(mSelectedLabel, v, reveal);
198 animateHintText(mLaunchHint, v, reveal);
199
Adrian Roos4e664b62015-03-25 15:09:46 -0700200 mSelectedContainer.setTag(R.id.tag_intent, v.getTag(R.id.tag_intent));
201 mLastRevealed = v;
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700202 postDelayed(mHideRunnable, HIDE_DELAY);
203 postDelayed(mRippleRunnable, RIPPLE_PAUSE / 2);
204
205 // Transfer focus from the originally clicked button to the expanded button.
206 mSelectedContainer.requestFocus();
Adrian Roos4e664b62015-03-25 15:09:46 -0700207 }
208
dollyleede4f7a92018-05-03 11:22:27 +0800209
210 private final OnLayoutChangeListener mLayoutChangeListener = new OnLayoutChangeListener() {
211 @Override
212 public void onLayoutChange(View v, int left, int top, int right, int bottom,
213 int oldLeft,
214 int oldTop, int oldRight, int oldBottom) {
215 decreaseAutoSizeMinTextSize(v);
216 }
217 };
218
219 /**
220 * Prevent some localization string will be truncated if there is low resolution screen
221 * or font size and display size of setting is largest.
222 */
223 private void decreaseAutoSizeMinTextSize(View selectedView) {
224 if (selectedView != null) {
225 if (selectedView instanceof TextView) {
226 TextView textView = (TextView) selectedView;
227 textView.setEllipsize(TextUtils.TruncateAt.END);
228
229 // The textView layout will be null due to it's property is hiding when
230 // initialization.
231 Layout layout = textView.getLayout();
232 if (layout != null) {
233 if (layout.getEllipsisCount(textView.getMaxLines() - 1) > 0) {
234 textView.setAutoSizeTextTypeUniformWithConfiguration(
235 8,
236 textView.getAutoSizeMaxTextSize(),
237 textView.getAutoSizeStepGranularity(),
238 TypedValue.COMPLEX_UNIT_SP);
239 textView.setGravity(Gravity.CENTER);
240 }
241 }
242 }
243 }
244 }
245
Adrian Roos4e664b62015-03-25 15:09:46 -0700246 private void animateHintText(View selectedView, View v, Animator reveal) {
247 selectedView.setTranslationX(
248 (v.getLeft() + v.getWidth() / 2 - mSelectedContainer.getWidth() / 2) / 5);
249 selectedView.animate()
250 .setDuration(reveal.getDuration() / 3)
251 .setStartDelay(reveal.getDuration() / 5)
252 .translationX(0)
253 .setInterpolator(mFastOutLinearInInterpolator)
254 .start();
255 }
256
257 private void hideTheButton() {
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700258 if (mHiding || mSelectedContainer.getVisibility() != VISIBLE) {
259 return;
260 }
261
262 mHiding = true;
263
264 removeCallbacks(mHideRunnable);
265
Adrian Roos4e664b62015-03-25 15:09:46 -0700266 View v = mLastRevealed;
267 int centerX = v.getLeft() + v.getWidth() / 2;
268 int centerY = v.getTop() + v.getHeight() / 2;
269 Animator reveal = ViewAnimationUtils.createCircularReveal(
270 mSelectedContainer,
271 centerX,
272 centerY,
273 Math.max(centerX, mSelectedContainer.getWidth() - centerX)
274 + Math.max(centerY, mSelectedContainer.getHeight() - centerY),
275 0);
276 reveal.addListener(new AnimatorListenerAdapter() {
277 @Override
278 public void onAnimationEnd(Animator animation) {
279 mSelectedContainer.setVisibility(INVISIBLE);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700280 removeCallbacks(mRippleRunnable);
281 mHiding = false;
Adrian Roos4e664b62015-03-25 15:09:46 -0700282 }
283 });
284 reveal.start();
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700285
286 // Transfer focus back to the originally clicked button.
287 if (mSelectedContainer.isFocused()) {
288 v.requestFocus();
289 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700290 }
291
292 private void startRipple() {
293 final View ripple = mRippleView;
294 ripple.animate().cancel();
295 ripple.setVisibility(VISIBLE);
296 Animator reveal = ViewAnimationUtils.createCircularReveal(
297 ripple,
298 ripple.getLeft() + ripple.getWidth() / 2,
299 ripple.getTop() + ripple.getHeight() / 2,
300 0,
301 ripple.getWidth() / 2);
302 reveal.setDuration(RIPPLE_DURATION);
303 reveal.start();
304
305 ripple.setAlpha(0);
306 ripple.animate().alpha(1).setDuration(RIPPLE_DURATION / 2)
307 .withEndAction(new Runnable() {
308 @Override
309 public void run() {
310 ripple.animate().alpha(0).setDuration(RIPPLE_DURATION / 2)
311 .withEndAction(new Runnable() {
312 @Override
313 public void run() {
314 ripple.setVisibility(INVISIBLE);
315 postDelayed(mRippleRunnable, RIPPLE_PAUSE);
316 }
317 }).start();
318 }
319 }).start();
320 }
321
322 private final Runnable mHideRunnable = new Runnable() {
323 @Override
324 public void run() {
325 if (!isAttachedToWindow()) return;
326 hideTheButton();
327 }
328 };
329
330 private final Runnable mRippleRunnable = new Runnable() {
331 @Override
332 public void run() {
333 if (!isAttachedToWindow()) return;
334 startRipple();
335 }
336 };
Adrian Roos4e664b62015-03-25 15:09:46 -0700337}