blob: 96adc5d024514c61591b904ef29d56fd036cee4c [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;
33import android.view.View;
34import android.view.ViewAnimationUtils;
35import android.view.ViewGroup;
36import android.view.animation.AnimationUtils;
37import android.view.animation.Interpolator;
38import android.widget.Button;
39import android.widget.FrameLayout;
40import android.widget.TextView;
41
42import java.util.List;
43
44public class EmergencyActionGroup extends FrameLayout implements View.OnClickListener {
45
46 private static final long HIDE_DELAY = 3000;
47 private static final int RIPPLE_DURATION = 600;
48 private static final long RIPPLE_PAUSE = 1000;
49
50 private final Interpolator mFastOutLinearInInterpolator;
51
52 private ViewGroup mSelectedContainer;
53 private TextView mSelectedLabel;
54 private View mRippleView;
55 private View mLaunchHint;
56
57 private View mLastRevealed;
58
59 public EmergencyActionGroup(Context context, @Nullable AttributeSet attrs) {
60 super(context, attrs);
61 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
62 android.R.interpolator.fast_out_linear_in);
63 }
64
65 @Override
66 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
67 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
68 }
69
70 @Override
71 protected void onFinishInflate() {
72 super.onFinishInflate();
73
74 setupAssistActions();
75
76 mSelectedContainer = (ViewGroup) findViewById(R.id.selected_container);
77 mSelectedContainer.setOnClickListener(this);
78 mSelectedLabel = (TextView) findViewById(R.id.selected_label);
79 mRippleView = findViewById(R.id.ripple_view);
80 mLaunchHint = findViewById(R.id.launch_hint);
81 }
82
83 private void setupAssistActions() {
84 int[] buttonIds = new int[] {R.id.action1, R.id.action2, R.id.action3};
85
86 List<ResolveInfo> infos = resolveAssistPackageAndQueryActivites();
87
88 for (int i = 0; i < 3; i++) {
89 Button button = (Button) findViewById(buttonIds[i]);
90 boolean visible = false;
91
92 button.setOnClickListener(this);
93
94 if (infos != null && infos.size() > i && infos.get(i) != null) {
95 ResolveInfo info = infos.get(i);
96 ComponentName name = getComponentName(info);
97
98 button.setTag(R.id.tag_intent,
99 new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
100 .setComponent(name));
101 button.setText(info.loadLabel(getContext().getPackageManager()));
102 visible = true;
103 }
104
105 button.setVisibility(visible ? View.VISIBLE : View.GONE);
106 }
107 }
108
109 private List<ResolveInfo> resolveAssistPackageAndQueryActivites() {
110 List<ResolveInfo> infos = queryAssistActivities();
111
112 if (infos == null || infos.isEmpty()) {
113 PackageManager packageManager = getContext().getPackageManager();
114 Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE);
115 infos = packageManager.queryIntentActivities(queryIntent, 0);
116
117 PackageInfo bestMatch = null;
118 for (int i = 0; i < infos.size(); i++) {
119 if (infos.get(i).activityInfo == null) continue;
120 String packageName = infos.get(i).activityInfo.packageName;
121 PackageInfo packageInfo;
122 try {
123 packageInfo = packageManager.getPackageInfo(packageName, 0);
124 } catch (PackageManager.NameNotFoundException e) {
125 continue;
126 }
127 // Get earliest installed app, but prioritize system apps.
128 if (bestMatch == null
129 || !isSystemApp(bestMatch) && isSystemApp(packageInfo)
130 || isSystemApp(bestMatch) == isSystemApp(packageInfo)
131 && bestMatch.firstInstallTime > packageInfo.firstInstallTime) {
132 bestMatch = packageInfo;
133 }
134 }
135
136 if (bestMatch != null) {
137 Settings.Secure.putString(getContext().getContentResolver(),
138 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION,
139 bestMatch.packageName);
140 return queryAssistActivities();
141 } else {
142 return null;
143 }
144 } else {
145 return infos;
146 }
147 }
148
149 private List<ResolveInfo> queryAssistActivities() {
150 String assistPackage = Settings.Secure.getString(
151 getContext().getContentResolver(),
152 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
153 List<ResolveInfo> infos = null;
154
155 if (!TextUtils.isEmpty(assistPackage)) {
156 Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
157 .setPackage(assistPackage);
158 infos = getContext().getPackageManager().queryIntentActivities(queryIntent, 0);
159 }
160 return infos;
161 }
162
163 private boolean isSystemApp(PackageInfo info) {
164 return info.applicationInfo != null
165 && (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
166 }
167
168 private ComponentName getComponentName(ResolveInfo resolveInfo) {
169 if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
170 return new ComponentName(resolveInfo.activityInfo.packageName,
171 resolveInfo.activityInfo.name);
172 }
173
174 @Override
175 public void onClick(View v) {
176 switch (v.getId()) {
177 case R.id.action1:
178 case R.id.action2:
179 case R.id.action3:
180 revealTheButton(v);
181 break;
182 case R.id.selected_container:
183 getContext().startActivity((Intent) v.getTag(R.id.tag_intent));
184 break;
185 }
186 }
187
188 private void revealTheButton(View v) {
189 mSelectedContainer.setVisibility(VISIBLE);
190 int centerX = v.getLeft() + v.getWidth() / 2;
191 int centerY = v.getTop() + v.getHeight() / 2;
192 Animator reveal = ViewAnimationUtils.createCircularReveal(
193 mSelectedContainer,
194 centerX,
195 centerY,
196 0,
197 Math.max(centerX, mSelectedContainer.getWidth() - centerX)
198 + Math.max(centerY, mSelectedContainer.getHeight() - centerY));
199 reveal.start();
200
201 animateHintText(mSelectedLabel, v, reveal);
202 animateHintText(mLaunchHint, v, reveal);
203
204 mSelectedLabel.setText(((Button) v).getText());
205 mSelectedContainer.setTag(R.id.tag_intent, v.getTag(R.id.tag_intent));
206 mLastRevealed = v;
207 mSelectedContainer.postDelayed(mHideRunnable, HIDE_DELAY);
208 mSelectedContainer.postDelayed(mRippleRunnable, RIPPLE_PAUSE / 2);
209 }
210
211 private void animateHintText(View selectedView, View v, Animator reveal) {
212 selectedView.setTranslationX(
213 (v.getLeft() + v.getWidth() / 2 - mSelectedContainer.getWidth() / 2) / 5);
214 selectedView.animate()
215 .setDuration(reveal.getDuration() / 3)
216 .setStartDelay(reveal.getDuration() / 5)
217 .translationX(0)
218 .setInterpolator(mFastOutLinearInInterpolator)
219 .start();
220 }
221
222 private void hideTheButton() {
223 View v = mLastRevealed;
224 int centerX = v.getLeft() + v.getWidth() / 2;
225 int centerY = v.getTop() + v.getHeight() / 2;
226 Animator reveal = ViewAnimationUtils.createCircularReveal(
227 mSelectedContainer,
228 centerX,
229 centerY,
230 Math.max(centerX, mSelectedContainer.getWidth() - centerX)
231 + Math.max(centerY, mSelectedContainer.getHeight() - centerY),
232 0);
233 reveal.addListener(new AnimatorListenerAdapter() {
234 @Override
235 public void onAnimationEnd(Animator animation) {
236 mSelectedContainer.setVisibility(INVISIBLE);
237 mSelectedContainer.removeCallbacks(mRippleRunnable);
238 }
239 });
240 reveal.start();
241 }
242
243 private void startRipple() {
244 final View ripple = mRippleView;
245 ripple.animate().cancel();
246 ripple.setVisibility(VISIBLE);
247 Animator reveal = ViewAnimationUtils.createCircularReveal(
248 ripple,
249 ripple.getLeft() + ripple.getWidth() / 2,
250 ripple.getTop() + ripple.getHeight() / 2,
251 0,
252 ripple.getWidth() / 2);
253 reveal.setDuration(RIPPLE_DURATION);
254 reveal.start();
255
256 ripple.setAlpha(0);
257 ripple.animate().alpha(1).setDuration(RIPPLE_DURATION / 2)
258 .withEndAction(new Runnable() {
259 @Override
260 public void run() {
261 ripple.animate().alpha(0).setDuration(RIPPLE_DURATION / 2)
262 .withEndAction(new Runnable() {
263 @Override
264 public void run() {
265 ripple.setVisibility(INVISIBLE);
266 postDelayed(mRippleRunnable, RIPPLE_PAUSE);
267 }
268 }).start();
269 }
270 }).start();
271 }
272
273 private final Runnable mHideRunnable = new Runnable() {
274 @Override
275 public void run() {
276 if (!isAttachedToWindow()) return;
277 hideTheButton();
278 }
279 };
280
281 private final Runnable mRippleRunnable = new Runnable() {
282 @Override
283 public void run() {
284 if (!isAttachedToWindow()) return;
285 startRipple();
286 }
287 };
288
289
290}