blob: 039dd82c0ffdac37d17c77f440d79f517e9876c0 [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;
25import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageInfo;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
29import android.provider.Settings;
30import android.telephony.TelephonyManager;
31import android.text.TextUtils;
32import android.util.AttributeSet;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070033import android.view.MotionEvent;
Adrian Roos4e664b62015-03-25 15:09:46 -070034import android.view.View;
35import android.view.ViewAnimationUtils;
36import android.view.ViewGroup;
Adrian Roosee55c732015-04-13 14:53:53 -070037import android.view.accessibility.AccessibilityManager;
Adrian Roos4e664b62015-03-25 15:09:46 -070038import android.view.animation.AnimationUtils;
39import android.view.animation.Interpolator;
40import android.widget.Button;
41import android.widget.FrameLayout;
42import android.widget.TextView;
43
44import java.util.List;
45
46public class EmergencyActionGroup extends FrameLayout implements View.OnClickListener {
47
48 private static final long HIDE_DELAY = 3000;
49 private static final int RIPPLE_DURATION = 600;
50 private static final long RIPPLE_PAUSE = 1000;
51
52 private final Interpolator mFastOutLinearInInterpolator;
53
54 private ViewGroup mSelectedContainer;
55 private TextView mSelectedLabel;
56 private View mRippleView;
57 private View mLaunchHint;
58
59 private View mLastRevealed;
60
Adrian Roos1c4b47f2015-04-13 14:53:32 -070061 private MotionEvent mPendingTouchEvent;
62
63 private boolean mHiding;
64
Adrian Roos4e664b62015-03-25 15:09:46 -070065 public EmergencyActionGroup(Context context, @Nullable AttributeSet attrs) {
66 super(context, attrs);
67 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
68 android.R.interpolator.fast_out_linear_in);
69 }
70
71 @Override
72 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
73 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
74 }
75
76 @Override
77 protected void onFinishInflate() {
78 super.onFinishInflate();
Adrian Roos4e664b62015-03-25 15:09:46 -070079 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 Roos1c4b47f2015-04-13 14:53:32 -070088 /**
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 Roos4e664b62015-03-25 15:09:46 -0700118 private void setupAssistActions() {
119 int[] buttonIds = new int[] {R.id.action1, R.id.action2, R.id.action3};
120
Adrian Roos9946f8b2015-05-27 14:37:58 -0700121 List<ResolveInfo> infos;
122
123 if (TelephonyManager.EMERGENCY_ASSISTANCE_ENABLED) {
124 infos = resolveAssistPackageAndQueryActivites();
125 } else {
126 infos = null;
127 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700128
129 for (int i = 0; i < 3; i++) {
130 Button button = (Button) findViewById(buttonIds[i]);
131 boolean visible = false;
132
133 button.setOnClickListener(this);
134
135 if (infos != null && infos.size() > i && infos.get(i) != null) {
136 ResolveInfo info = infos.get(i);
137 ComponentName name = getComponentName(info);
138
139 button.setTag(R.id.tag_intent,
140 new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
141 .setComponent(name));
142 button.setText(info.loadLabel(getContext().getPackageManager()));
143 visible = true;
144 }
145
146 button.setVisibility(visible ? View.VISIBLE : View.GONE);
147 }
148 }
149
150 private List<ResolveInfo> resolveAssistPackageAndQueryActivites() {
151 List<ResolveInfo> infos = queryAssistActivities();
152
153 if (infos == null || infos.isEmpty()) {
154 PackageManager packageManager = getContext().getPackageManager();
155 Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE);
156 infos = packageManager.queryIntentActivities(queryIntent, 0);
157
158 PackageInfo bestMatch = null;
159 for (int i = 0; i < infos.size(); i++) {
160 if (infos.get(i).activityInfo == null) continue;
161 String packageName = infos.get(i).activityInfo.packageName;
162 PackageInfo packageInfo;
163 try {
164 packageInfo = packageManager.getPackageInfo(packageName, 0);
165 } catch (PackageManager.NameNotFoundException e) {
166 continue;
167 }
Akshay Kannan5d927b42016-02-09 13:51:17 -0800168 // Get earliest installed system app.
169 if (isSystemApp(packageInfo) && (bestMatch == null ||
170 bestMatch.firstInstallTime > packageInfo.firstInstallTime)) {
Adrian Roos4e664b62015-03-25 15:09:46 -0700171 bestMatch = packageInfo;
172 }
173 }
174
175 if (bestMatch != null) {
176 Settings.Secure.putString(getContext().getContentResolver(),
177 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION,
178 bestMatch.packageName);
179 return queryAssistActivities();
180 } else {
181 return null;
182 }
183 } else {
184 return infos;
185 }
186 }
187
188 private List<ResolveInfo> queryAssistActivities() {
189 String assistPackage = Settings.Secure.getString(
190 getContext().getContentResolver(),
191 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
192 List<ResolveInfo> infos = null;
193
194 if (!TextUtils.isEmpty(assistPackage)) {
195 Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
196 .setPackage(assistPackage);
197 infos = getContext().getPackageManager().queryIntentActivities(queryIntent, 0);
198 }
199 return infos;
200 }
201
202 private boolean isSystemApp(PackageInfo info) {
203 return info.applicationInfo != null
204 && (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
205 }
206
207 private ComponentName getComponentName(ResolveInfo resolveInfo) {
208 if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
209 return new ComponentName(resolveInfo.activityInfo.packageName,
210 resolveInfo.activityInfo.name);
211 }
212
213 @Override
214 public void onClick(View v) {
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700215 Intent intent = (Intent) v.getTag(R.id.tag_intent);
216
Adrian Roos4e664b62015-03-25 15:09:46 -0700217 switch (v.getId()) {
218 case R.id.action1:
219 case R.id.action2:
220 case R.id.action3:
Adrian Roosee55c732015-04-13 14:53:53 -0700221 if (AccessibilityManager.getInstance(mContext).isTouchExplorationEnabled()) {
222 getContext().startActivity(intent);
223 } else {
224 revealTheButton(v);
225 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700226 break;
227 case R.id.selected_container:
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700228 if (!mHiding) {
229 getContext().startActivity(intent);
230 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700231 break;
232 }
233 }
234
235 private void revealTheButton(View v) {
236 mSelectedContainer.setVisibility(VISIBLE);
237 int centerX = v.getLeft() + v.getWidth() / 2;
238 int centerY = v.getTop() + v.getHeight() / 2;
239 Animator reveal = ViewAnimationUtils.createCircularReveal(
240 mSelectedContainer,
241 centerX,
242 centerY,
243 0,
244 Math.max(centerX, mSelectedContainer.getWidth() - centerX)
245 + Math.max(centerY, mSelectedContainer.getHeight() - centerY));
246 reveal.start();
247
248 animateHintText(mSelectedLabel, v, reveal);
249 animateHintText(mLaunchHint, v, reveal);
250
251 mSelectedLabel.setText(((Button) v).getText());
252 mSelectedContainer.setTag(R.id.tag_intent, v.getTag(R.id.tag_intent));
253 mLastRevealed = v;
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700254 postDelayed(mHideRunnable, HIDE_DELAY);
255 postDelayed(mRippleRunnable, RIPPLE_PAUSE / 2);
256
257 // Transfer focus from the originally clicked button to the expanded button.
258 mSelectedContainer.requestFocus();
Adrian Roos4e664b62015-03-25 15:09:46 -0700259 }
260
261 private void animateHintText(View selectedView, View v, Animator reveal) {
262 selectedView.setTranslationX(
263 (v.getLeft() + v.getWidth() / 2 - mSelectedContainer.getWidth() / 2) / 5);
264 selectedView.animate()
265 .setDuration(reveal.getDuration() / 3)
266 .setStartDelay(reveal.getDuration() / 5)
267 .translationX(0)
268 .setInterpolator(mFastOutLinearInInterpolator)
269 .start();
270 }
271
272 private void hideTheButton() {
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700273 if (mHiding || mSelectedContainer.getVisibility() != VISIBLE) {
274 return;
275 }
276
277 mHiding = true;
278
279 removeCallbacks(mHideRunnable);
280
Adrian Roos4e664b62015-03-25 15:09:46 -0700281 View v = mLastRevealed;
282 int centerX = v.getLeft() + v.getWidth() / 2;
283 int centerY = v.getTop() + v.getHeight() / 2;
284 Animator reveal = ViewAnimationUtils.createCircularReveal(
285 mSelectedContainer,
286 centerX,
287 centerY,
288 Math.max(centerX, mSelectedContainer.getWidth() - centerX)
289 + Math.max(centerY, mSelectedContainer.getHeight() - centerY),
290 0);
291 reveal.addListener(new AnimatorListenerAdapter() {
292 @Override
293 public void onAnimationEnd(Animator animation) {
294 mSelectedContainer.setVisibility(INVISIBLE);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700295 removeCallbacks(mRippleRunnable);
296 mHiding = false;
Adrian Roos4e664b62015-03-25 15:09:46 -0700297 }
298 });
299 reveal.start();
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700300
301 // Transfer focus back to the originally clicked button.
302 if (mSelectedContainer.isFocused()) {
303 v.requestFocus();
304 }
Adrian Roos4e664b62015-03-25 15:09:46 -0700305 }
306
307 private void startRipple() {
308 final View ripple = mRippleView;
309 ripple.animate().cancel();
310 ripple.setVisibility(VISIBLE);
311 Animator reveal = ViewAnimationUtils.createCircularReveal(
312 ripple,
313 ripple.getLeft() + ripple.getWidth() / 2,
314 ripple.getTop() + ripple.getHeight() / 2,
315 0,
316 ripple.getWidth() / 2);
317 reveal.setDuration(RIPPLE_DURATION);
318 reveal.start();
319
320 ripple.setAlpha(0);
321 ripple.animate().alpha(1).setDuration(RIPPLE_DURATION / 2)
322 .withEndAction(new Runnable() {
323 @Override
324 public void run() {
325 ripple.animate().alpha(0).setDuration(RIPPLE_DURATION / 2)
326 .withEndAction(new Runnable() {
327 @Override
328 public void run() {
329 ripple.setVisibility(INVISIBLE);
330 postDelayed(mRippleRunnable, RIPPLE_PAUSE);
331 }
332 }).start();
333 }
334 }).start();
335 }
336
337 private final Runnable mHideRunnable = new Runnable() {
338 @Override
339 public void run() {
340 if (!isAttachedToWindow()) return;
341 hideTheButton();
342 }
343 };
344
345 private final Runnable mRippleRunnable = new Runnable() {
346 @Override
347 public void run() {
348 if (!isAttachedToWindow()) return;
349 startRipple();
350 }
351 };
352
353
354}