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; |
| 25 | import android.content.pm.ApplicationInfo; |
| 26 | import android.content.pm.PackageInfo; |
| 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.ResolveInfo; |
| 29 | import android.provider.Settings; |
| 30 | import android.telephony.TelephonyManager; |
| 31 | import android.text.TextUtils; |
| 32 | import android.util.AttributeSet; |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame^] | 33 | import android.view.MotionEvent; |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 34 | import android.view.View; |
| 35 | import android.view.ViewAnimationUtils; |
| 36 | import android.view.ViewGroup; |
| 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(); |
| 78 | |
| 79 | setupAssistActions(); |
| 80 | |
| 81 | mSelectedContainer = (ViewGroup) findViewById(R.id.selected_container); |
| 82 | mSelectedContainer.setOnClickListener(this); |
| 83 | mSelectedLabel = (TextView) findViewById(R.id.selected_label); |
| 84 | mRippleView = findViewById(R.id.ripple_view); |
| 85 | mLaunchHint = findViewById(R.id.launch_hint); |
| 86 | } |
| 87 | |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame^] | 88 | /** |
| 89 | * Called by the activity before a touch event is dispatched to the view hierarchy. |
| 90 | */ |
| 91 | public void onPreTouchEvent(MotionEvent event) { |
| 92 | mPendingTouchEvent = event; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean dispatchTouchEvent(MotionEvent event) { |
| 97 | boolean handled = super.dispatchTouchEvent(event); |
| 98 | if (mPendingTouchEvent == event && handled) { |
| 99 | mPendingTouchEvent = null; |
| 100 | } |
| 101 | return handled; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Called by the activity after a touch event is dispatched to the view hierarchy. |
| 106 | */ |
| 107 | public void onPostTouchEvent(MotionEvent event) { |
| 108 | // Hide the confirmation button if a touch event was delivered to the activity but not to |
| 109 | // this view. |
| 110 | if (mPendingTouchEvent != null) { |
| 111 | hideTheButton(); |
| 112 | } |
| 113 | mPendingTouchEvent = null; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 118 | private void setupAssistActions() { |
| 119 | int[] buttonIds = new int[] {R.id.action1, R.id.action2, R.id.action3}; |
| 120 | |
| 121 | List<ResolveInfo> infos = resolveAssistPackageAndQueryActivites(); |
| 122 | |
| 123 | for (int i = 0; i < 3; i++) { |
| 124 | Button button = (Button) findViewById(buttonIds[i]); |
| 125 | boolean visible = false; |
| 126 | |
| 127 | button.setOnClickListener(this); |
| 128 | |
| 129 | if (infos != null && infos.size() > i && infos.get(i) != null) { |
| 130 | ResolveInfo info = infos.get(i); |
| 131 | ComponentName name = getComponentName(info); |
| 132 | |
| 133 | button.setTag(R.id.tag_intent, |
| 134 | new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE) |
| 135 | .setComponent(name)); |
| 136 | button.setText(info.loadLabel(getContext().getPackageManager())); |
| 137 | visible = true; |
| 138 | } |
| 139 | |
| 140 | button.setVisibility(visible ? View.VISIBLE : View.GONE); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private List<ResolveInfo> resolveAssistPackageAndQueryActivites() { |
| 145 | List<ResolveInfo> infos = queryAssistActivities(); |
| 146 | |
| 147 | if (infos == null || infos.isEmpty()) { |
| 148 | PackageManager packageManager = getContext().getPackageManager(); |
| 149 | Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE); |
| 150 | infos = packageManager.queryIntentActivities(queryIntent, 0); |
| 151 | |
| 152 | PackageInfo bestMatch = null; |
| 153 | for (int i = 0; i < infos.size(); i++) { |
| 154 | if (infos.get(i).activityInfo == null) continue; |
| 155 | String packageName = infos.get(i).activityInfo.packageName; |
| 156 | PackageInfo packageInfo; |
| 157 | try { |
| 158 | packageInfo = packageManager.getPackageInfo(packageName, 0); |
| 159 | } catch (PackageManager.NameNotFoundException e) { |
| 160 | continue; |
| 161 | } |
| 162 | // Get earliest installed app, but prioritize system apps. |
| 163 | if (bestMatch == null |
| 164 | || !isSystemApp(bestMatch) && isSystemApp(packageInfo) |
| 165 | || isSystemApp(bestMatch) == isSystemApp(packageInfo) |
| 166 | && bestMatch.firstInstallTime > packageInfo.firstInstallTime) { |
| 167 | bestMatch = packageInfo; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (bestMatch != null) { |
| 172 | Settings.Secure.putString(getContext().getContentResolver(), |
| 173 | Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION, |
| 174 | bestMatch.packageName); |
| 175 | return queryAssistActivities(); |
| 176 | } else { |
| 177 | return null; |
| 178 | } |
| 179 | } else { |
| 180 | return infos; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private List<ResolveInfo> queryAssistActivities() { |
| 185 | String assistPackage = Settings.Secure.getString( |
| 186 | getContext().getContentResolver(), |
| 187 | Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION); |
| 188 | List<ResolveInfo> infos = null; |
| 189 | |
| 190 | if (!TextUtils.isEmpty(assistPackage)) { |
| 191 | Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE) |
| 192 | .setPackage(assistPackage); |
| 193 | infos = getContext().getPackageManager().queryIntentActivities(queryIntent, 0); |
| 194 | } |
| 195 | return infos; |
| 196 | } |
| 197 | |
| 198 | private boolean isSystemApp(PackageInfo info) { |
| 199 | return info.applicationInfo != null |
| 200 | && (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 201 | } |
| 202 | |
| 203 | private ComponentName getComponentName(ResolveInfo resolveInfo) { |
| 204 | if (resolveInfo == null || resolveInfo.activityInfo == null) return null; |
| 205 | return new ComponentName(resolveInfo.activityInfo.packageName, |
| 206 | resolveInfo.activityInfo.name); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void onClick(View v) { |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame^] | 211 | Intent intent = (Intent) v.getTag(R.id.tag_intent); |
| 212 | |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 213 | switch (v.getId()) { |
| 214 | case R.id.action1: |
| 215 | case R.id.action2: |
| 216 | case R.id.action3: |
| 217 | revealTheButton(v); |
| 218 | break; |
| 219 | case R.id.selected_container: |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame^] | 220 | if (!mHiding) { |
| 221 | getContext().startActivity(intent); |
| 222 | } |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private void revealTheButton(View v) { |
| 228 | mSelectedContainer.setVisibility(VISIBLE); |
| 229 | int centerX = v.getLeft() + v.getWidth() / 2; |
| 230 | int centerY = v.getTop() + v.getHeight() / 2; |
| 231 | Animator reveal = ViewAnimationUtils.createCircularReveal( |
| 232 | mSelectedContainer, |
| 233 | centerX, |
| 234 | centerY, |
| 235 | 0, |
| 236 | Math.max(centerX, mSelectedContainer.getWidth() - centerX) |
| 237 | + Math.max(centerY, mSelectedContainer.getHeight() - centerY)); |
| 238 | reveal.start(); |
| 239 | |
| 240 | animateHintText(mSelectedLabel, v, reveal); |
| 241 | animateHintText(mLaunchHint, v, reveal); |
| 242 | |
| 243 | mSelectedLabel.setText(((Button) v).getText()); |
| 244 | mSelectedContainer.setTag(R.id.tag_intent, v.getTag(R.id.tag_intent)); |
| 245 | mLastRevealed = v; |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame^] | 246 | postDelayed(mHideRunnable, HIDE_DELAY); |
| 247 | postDelayed(mRippleRunnable, RIPPLE_PAUSE / 2); |
| 248 | |
| 249 | // Transfer focus from the originally clicked button to the expanded button. |
| 250 | mSelectedContainer.requestFocus(); |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private void animateHintText(View selectedView, View v, Animator reveal) { |
| 254 | selectedView.setTranslationX( |
| 255 | (v.getLeft() + v.getWidth() / 2 - mSelectedContainer.getWidth() / 2) / 5); |
| 256 | selectedView.animate() |
| 257 | .setDuration(reveal.getDuration() / 3) |
| 258 | .setStartDelay(reveal.getDuration() / 5) |
| 259 | .translationX(0) |
| 260 | .setInterpolator(mFastOutLinearInInterpolator) |
| 261 | .start(); |
| 262 | } |
| 263 | |
| 264 | private void hideTheButton() { |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame^] | 265 | if (mHiding || mSelectedContainer.getVisibility() != VISIBLE) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | mHiding = true; |
| 270 | |
| 271 | removeCallbacks(mHideRunnable); |
| 272 | |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 273 | View v = mLastRevealed; |
| 274 | int centerX = v.getLeft() + v.getWidth() / 2; |
| 275 | int centerY = v.getTop() + v.getHeight() / 2; |
| 276 | Animator reveal = ViewAnimationUtils.createCircularReveal( |
| 277 | mSelectedContainer, |
| 278 | centerX, |
| 279 | centerY, |
| 280 | Math.max(centerX, mSelectedContainer.getWidth() - centerX) |
| 281 | + Math.max(centerY, mSelectedContainer.getHeight() - centerY), |
| 282 | 0); |
| 283 | reveal.addListener(new AnimatorListenerAdapter() { |
| 284 | @Override |
| 285 | public void onAnimationEnd(Animator animation) { |
| 286 | mSelectedContainer.setVisibility(INVISIBLE); |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame^] | 287 | removeCallbacks(mRippleRunnable); |
| 288 | mHiding = false; |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 289 | } |
| 290 | }); |
| 291 | reveal.start(); |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame^] | 292 | |
| 293 | // Transfer focus back to the originally clicked button. |
| 294 | if (mSelectedContainer.isFocused()) { |
| 295 | v.requestFocus(); |
| 296 | } |
Adrian Roos | 4e664b6 | 2015-03-25 15:09:46 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | private void startRipple() { |
| 300 | final View ripple = mRippleView; |
| 301 | ripple.animate().cancel(); |
| 302 | ripple.setVisibility(VISIBLE); |
| 303 | Animator reveal = ViewAnimationUtils.createCircularReveal( |
| 304 | ripple, |
| 305 | ripple.getLeft() + ripple.getWidth() / 2, |
| 306 | ripple.getTop() + ripple.getHeight() / 2, |
| 307 | 0, |
| 308 | ripple.getWidth() / 2); |
| 309 | reveal.setDuration(RIPPLE_DURATION); |
| 310 | reveal.start(); |
| 311 | |
| 312 | ripple.setAlpha(0); |
| 313 | ripple.animate().alpha(1).setDuration(RIPPLE_DURATION / 2) |
| 314 | .withEndAction(new Runnable() { |
| 315 | @Override |
| 316 | public void run() { |
| 317 | ripple.animate().alpha(0).setDuration(RIPPLE_DURATION / 2) |
| 318 | .withEndAction(new Runnable() { |
| 319 | @Override |
| 320 | public void run() { |
| 321 | ripple.setVisibility(INVISIBLE); |
| 322 | postDelayed(mRippleRunnable, RIPPLE_PAUSE); |
| 323 | } |
| 324 | }).start(); |
| 325 | } |
| 326 | }).start(); |
| 327 | } |
| 328 | |
| 329 | private final Runnable mHideRunnable = new Runnable() { |
| 330 | @Override |
| 331 | public void run() { |
| 332 | if (!isAttachedToWindow()) return; |
| 333 | hideTheButton(); |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | private final Runnable mRippleRunnable = new Runnable() { |
| 338 | @Override |
| 339 | public void run() { |
| 340 | if (!isAttachedToWindow()) return; |
| 341 | startRipple(); |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | |
| 346 | } |