Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 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.phone; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.annotation.Nullable; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 25 | import android.content.pm.ResolveInfo; |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 26 | import android.telephony.TelephonyManager; |
dollylee | de4f7a9 | 2018-05-03 11:22:27 +0800 | [diff] [blame] | 27 | import android.text.Layout; |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
| 29 | import android.util.AttributeSet; |
dollylee | de4f7a9 | 2018-05-03 11:22:27 +0800 | [diff] [blame] | 30 | import android.util.TypedValue; |
| 31 | import android.view.Gravity; |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 32 | import android.view.MotionEvent; |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 33 | import android.view.View; |
| 34 | import android.view.ViewAnimationUtils; |
| 35 | import android.view.ViewGroup; |
Adrian Roos | ee55c73 | 2015-04-13 14:53:53 -0700 | [diff] [blame] | 36 | import android.view.accessibility.AccessibilityManager; |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 37 | import android.view.animation.AnimationUtils; |
| 38 | import android.view.animation.Interpolator; |
| 39 | import android.widget.Button; |
| 40 | import android.widget.FrameLayout; |
| 41 | import android.widget.TextView; |
| 42 | |
| 43 | import java.util.List; |
| 44 | |
| 45 | public 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 Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 60 | private MotionEvent mPendingTouchEvent; |
| 61 | |
| 62 | private boolean mHiding; |
| 63 | |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 64 | 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 Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 78 | |
| 79 | mSelectedContainer = (ViewGroup) findViewById(R.id.selected_container); |
| 80 | mSelectedContainer.setOnClickListener(this); |
| 81 | mSelectedLabel = (TextView) findViewById(R.id.selected_label); |
dollylee | de4f7a9 | 2018-05-03 11:22:27 +0800 | [diff] [blame] | 82 | mSelectedLabel.addOnLayoutChangeListener(mLayoutChangeListener); |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 83 | mRippleView = findViewById(R.id.ripple_view); |
| 84 | mLaunchHint = findViewById(R.id.launch_hint); |
dollylee | de4f7a9 | 2018-05-03 11:22:27 +0800 | [diff] [blame] | 85 | mLaunchHint.addOnLayoutChangeListener(mLayoutChangeListener); |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 86 | } |
| 87 | |
mariagpuyol | d0056c0 | 2016-03-29 12:12:40 -0700 | [diff] [blame] | 88 | @Override |
| 89 | protected void onWindowVisibilityChanged(int visibility) { |
| 90 | super.onWindowVisibilityChanged(visibility); |
| 91 | if (visibility == View.VISIBLE) { |
| 92 | setupAssistActions(); |
| 93 | } |
| 94 | } |
| 95 | |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 96 | /** |
| 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 Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 124 | private void setupAssistActions() { |
| 125 | int[] buttonIds = new int[] {R.id.action1, R.id.action2, R.id.action3}; |
| 126 | |
Adrian Roos | 9946f8b | 2015-05-27 14:37:58 -0700 | [diff] [blame] | 127 | List<ResolveInfo> infos; |
| 128 | |
| 129 | if (TelephonyManager.EMERGENCY_ASSISTANCE_ENABLED) { |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 130 | infos = EmergencyAssistanceHelper.resolveAssistPackageAndQueryActivities(getContext()); |
Adrian Roos | 9946f8b | 2015-05-27 14:37:58 -0700 | [diff] [blame] | 131 | } else { |
| 132 | infos = null; |
| 133 | } |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 134 | |
| 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 Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 143 | ComponentName name = EmergencyAssistanceHelper.getComponentName(info); |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 144 | |
| 145 | button.setTag(R.id.tag_intent, |
yuanjiahsu | cc83a3b | 2019-05-23 15:29:30 +0800 | [diff] [blame^] | 146 | new Intent(EmergencyAssistanceHelper.getIntentAction()) |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 147 | .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 Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public void onClick(View v) { |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 158 | Intent intent = (Intent) v.getTag(R.id.tag_intent); |
| 159 | |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 160 | switch (v.getId()) { |
| 161 | case R.id.action1: |
| 162 | case R.id.action2: |
| 163 | case R.id.action3: |
Adrian Roos | ee55c73 | 2015-04-13 14:53:53 -0700 | [diff] [blame] | 164 | if (AccessibilityManager.getInstance(mContext).isTouchExplorationEnabled()) { |
| 165 | getContext().startActivity(intent); |
| 166 | } else { |
| 167 | revealTheButton(v); |
| 168 | } |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 169 | break; |
| 170 | case R.id.selected_container: |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 171 | if (!mHiding) { |
| 172 | getContext().startActivity(intent); |
| 173 | } |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private void revealTheButton(View v) { |
Hall Liu | 21a3779 | 2017-07-26 15:49:27 -0700 | [diff] [blame] | 179 | CharSequence buttonText = ((Button) v).getText(); |
| 180 | mSelectedLabel.setText(buttonText); |
| 181 | mSelectedLabel.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM); |
dollylee | de4f7a9 | 2018-05-03 11:22:27 +0800 | [diff] [blame] | 182 | |
| 183 | // In order to trigger OnLayoutChangeListener for reset default minimum font size. |
| 184 | mSelectedLabel.requestLayout(); |
| 185 | mLaunchHint.requestLayout(); |
| 186 | |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 187 | 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 Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 202 | mSelectedContainer.setTag(R.id.tag_intent, v.getTag(R.id.tag_intent)); |
| 203 | mLastRevealed = v; |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 204 | 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 Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 209 | } |
| 210 | |
dollylee | de4f7a9 | 2018-05-03 11:22:27 +0800 | [diff] [blame] | 211 | |
| 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 Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 248 | 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 Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 260 | if (mHiding || mSelectedContainer.getVisibility() != VISIBLE) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | mHiding = true; |
| 265 | |
| 266 | removeCallbacks(mHideRunnable); |
| 267 | |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 268 | 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 Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 282 | removeCallbacks(mRippleRunnable); |
| 283 | mHiding = false; |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 284 | } |
| 285 | }); |
| 286 | reveal.start(); |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 287 | |
| 288 | // Transfer focus back to the originally clicked button. |
| 289 | if (mSelectedContainer.isFocused()) { |
| 290 | v.requestFocus(); |
| 291 | } |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 292 | } |
| 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 Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 339 | } |