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