blob: 19c498364281dec446869e3840362e744686020c [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 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
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Lucas Dupineb9c5702017-05-10 16:57:09 -070026import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.BroadcastReceiver;
Billy Chi17ec2282018-06-15 19:00:15 +080028import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
Billy Chi17ec2282018-06-15 19:00:15 +080032import android.database.ContentObserver;
33import android.database.Cursor;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.media.AudioManager;
36import android.media.ToneGenerator;
37import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070038import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.Bundle;
Billy Chi17ec2282018-06-15 19:00:15 +080040import android.os.Handler;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070043import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070044import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070045import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080047import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070048import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070049import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070050import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070052import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080053import android.text.Spannable;
54import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.text.TextUtils;
56import android.text.TextWatcher;
57import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080058import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070060import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070061import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070063import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070064import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.view.View;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080066import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.view.WindowManager;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080068import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
Lucas Dupinaf9e9912017-06-22 12:39:39 -070070import com.android.internal.colorextraction.ColorExtractor;
71import com.android.internal.colorextraction.ColorExtractor.GradientColors;
72import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070073import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070074import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070075import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076
Chihhang Chuang92cfe512018-06-07 16:25:27 +080077import java.util.ArrayList;
78import java.util.List;
79
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080/**
81 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
82 *
83 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
84 * activity from apps/Contacts) that:
85 * 1. Allows ONLY emergency calls to be dialed
86 * 2. Disallows voicemail functionality
87 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
88 * activity to stay in front of the keyguard.
89 *
90 * TODO: Even though this is an ultra-simplified version of the normal
91 * dialer, there's still lots of code duplication between this class and
92 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
93 * moved into a shared base class that would live in the framework?
94 * Or could we figure out some way to move *this* class into apps/Contacts
95 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080096 *
97 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +080098 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
99 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800100 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800101 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 */
103public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700104 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800105 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
106 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 // Keys used with onSaveInstanceState().
108 private static final String LAST_NUMBER = "lastNumber";
109
110 // Intent action for this activity.
111 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
112
113 // List of dialer button IDs.
114 private static final int[] DIALER_KEYS = new int[] {
115 R.id.one, R.id.two, R.id.three,
116 R.id.four, R.id.five, R.id.six,
117 R.id.seven, R.id.eight, R.id.nine,
118 R.id.star, R.id.zero, R.id.pound };
119
120 // Debug constants.
121 private static final boolean DBG = false;
122 private static final String LOG_TAG = "EmergencyDialer";
123
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 /** The length of DTMF tones in milliseconds */
125 private static final int TONE_LENGTH_MS = 150;
126
127 /** The DTMF tone volume relative to other sounds in the stream */
128 private static final int TONE_RELATIVE_VOLUME = 80;
129
130 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
131 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
132
133 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
134
Lucas Dupineb9c5702017-05-10 16:57:09 -0700135 /** 90% opacity, different from other gradients **/
136 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
137
Tyler Gunnc5428972018-03-28 14:15:34 -0700138 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 private View mDialButton;
140 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800141 private View mEmergencyShortcutView;
142 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800144 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
145
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146 private ToneGenerator mToneGenerator;
147 private Object mToneGeneratorLock = new Object();
148
149 // determines if we want to playback local DTMF tones.
150 private boolean mDTMFToneEnabled;
151
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700152 private EmergencyActionGroup mEmergencyActionGroup;
153
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 // close activity when screen turns off
155 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
156 @Override
157 public void onReceive(Context context, Intent intent) {
158 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700159 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160 }
161 }
162 };
163
164 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
165
Lucas Dupineb9c5702017-05-10 16:57:09 -0700166 // Background gradient
167 private ColorExtractor mColorExtractor;
168 private GradientDrawable mBackgroundGradient;
169 private boolean mSupportsDarkText;
170
Tyler Gunnc5428972018-03-28 14:15:34 -0700171 private boolean mIsWfcEmergencyCallingWarningEnabled;
172 private float mDefaultDigitsTextSize;
173
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800174 private boolean mAreEmergencyDialerShortcutsEnabled;
175
Billy Chi17ec2282018-06-15 19:00:15 +0800176 /** Key of emergency information user name */
177 private static final String KEY_EMERGENCY_INFO_NAME = "name";
178
179 /** Authority of emergency information */
180 private static final String AUTHORITY = "com.android.emergency.info.name";
181
182 /** Content path of emergency information name */
183 private static final String CONTENT_PATH = "name";
184
185 /** Content URI of emergency information */
186 private static final Uri CONTENT_URI = new Uri.Builder()
187 .scheme(ContentResolver.SCHEME_CONTENT)
188 .authority(AUTHORITY)
189 .path(CONTENT_PATH)
190 .build();
191
192 /** ContentObserver for monitoring emergency info name changes */
193 private ContentObserver mEmergencyInfoNameObserver;
194
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 @Override
196 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
197 // Do nothing
198 }
199
200 @Override
201 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700202 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 }
204
205 @Override
206 public void afterTextChanged(Editable input) {
207 // Check for special sequences, in particular the "**04" or "**05"
208 // sequences that allow you to enter PIN or PUK-related codes.
209 //
210 // But note we *don't* allow most other special sequences here,
211 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
212 // since those shouldn't be available if the device is locked.
213 //
214 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
215 // here, not the regular handleChars() method.
216 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
217 // A special sequence was entered, clear the digits
218 mDigits.getText().clear();
219 }
220
221 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800222 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223 }
224
225 @Override
226 protected void onCreate(Bundle icicle) {
227 super.onCreate(icicle);
228
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800230 setShowWhenLocked(true);
231 // Allow turning screen on
232 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233
Lucas Dupineb9c5702017-05-10 16:57:09 -0700234 mColorExtractor = new ColorExtractor(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700235 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
236 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700237 updateTheme(lockScreenColors.supportsDarkText());
238
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 setContentView(R.layout.emergency_dialer);
240
Tyler Gunnc5428972018-03-28 14:15:34 -0700241 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700242 mDigits.setKeyListener(DialerKeyListener.getInstance());
243 mDigits.setOnClickListener(this);
244 mDigits.setOnKeyListener(this);
245 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700246 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700247 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 maybeAddNumberFormatting();
249
Lucas Dupineb9c5702017-05-10 16:57:09 -0700250 mBackgroundGradient = new GradientDrawable(this);
251 Point displaySize = new Point();
252 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
253 .getDefaultDisplay().getSize(displaySize);
254 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
255 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
256 getWindow().setBackgroundDrawable(mBackgroundGradient);
257
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 // Check for the presence of the keypad
259 View view = findViewById(R.id.one);
260 if (view != null) {
261 setupKeypad();
262 }
263
264 mDelete = findViewById(R.id.deleteButton);
265 mDelete.setOnClickListener(this);
266 mDelete.setOnLongClickListener(this);
267
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700268 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269
270 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700271 // Read carrier config through the public API because PhoneGlobals is not available when we
272 // run as a secondary user.
273 CarrierConfigManager configMgr =
274 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
275 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800276 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700277
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700278 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279 mDialButton.setOnClickListener(this);
280 } else {
281 mDialButton.setVisibility(View.GONE);
282 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700283 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
284 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
285 maybeShowWfcEmergencyCallingWarning();
286
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700287 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288
289 if (icicle != null) {
290 super.onRestoreInstanceState(icicle);
291 }
292
293 // Extract phone number from intent
294 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700295 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
297 if (number != null) {
298 mDigits.setText(number);
299 }
300 }
301
302 // if the mToneGenerator creation fails, just continue without it. It is
303 // a local audio signal, and is not as important as the dtmf tone itself.
304 synchronized (mToneGeneratorLock) {
305 if (mToneGenerator == null) {
306 try {
307 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
308 } catch (RuntimeException e) {
309 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
310 mToneGenerator = null;
311 }
312 }
313 }
314
315 final IntentFilter intentFilter = new IntentFilter();
316 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
317 registerReceiver(mBroadcastReceiver, intentFilter);
318
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700319 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800320
321 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
322 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
323
324 if (mAreEmergencyDialerShortcutsEnabled) {
325 setupEmergencyShortcutsView();
326 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700327 }
328
329 @Override
330 protected void onDestroy() {
331 super.onDestroy();
332 synchronized (mToneGeneratorLock) {
333 if (mToneGenerator != null) {
334 mToneGenerator.release();
335 mToneGenerator = null;
336 }
337 }
338 unregisterReceiver(mBroadcastReceiver);
Billy Chi17ec2282018-06-15 19:00:15 +0800339 if (mEmergencyInfoNameObserver != null) {
340 getContentResolver().unregisterContentObserver(mEmergencyInfoNameObserver);
341 mEmergencyInfoNameObserver = null;
342 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 }
344
345 @Override
346 protected void onRestoreInstanceState(Bundle icicle) {
347 mLastNumber = icicle.getString(LAST_NUMBER);
348 }
349
350 @Override
351 protected void onSaveInstanceState(Bundle outState) {
352 super.onSaveInstanceState(outState);
353 outState.putString(LAST_NUMBER, mLastNumber);
354 }
355
356 /**
357 * Explicitly turn off number formatting, since it gets in the way of the emergency
358 * number detector
359 */
360 protected void maybeAddNumberFormatting() {
361 // Do nothing.
362 }
363
364 @Override
365 protected void onPostCreate(Bundle savedInstanceState) {
366 super.onPostCreate(savedInstanceState);
367
368 // This can't be done in onCreate(), since the auto-restoring of the digits
369 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
370 // is called. This method will be called every time the activity is created, and
371 // will always happen after onRestoreSavedInstanceState().
372 mDigits.addTextChangedListener(this);
373 }
374
375 private void setupKeypad() {
376 // Setup the listeners for the buttons
377 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700378 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
379 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 }
381
382 View view = findViewById(R.id.zero);
383 view.setOnLongClickListener(this);
384 }
385
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800386 @Override
387 public void onBackPressed() {
388 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800389 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800390 // Otherwise, it would finish the activity.
391 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
392 && mDialpadView.getVisibility() == View.VISIBLE) {
393 switchView(mEmergencyShortcutView, mDialpadView, true);
394 return;
395 }
396 super.onBackPressed();
397 }
398
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700399 /**
400 * handle key events
401 */
402 @Override
403 public boolean onKeyDown(int keyCode, KeyEvent event) {
404 switch (keyCode) {
405 // Happen when there's a "Call" hard button.
406 case KeyEvent.KEYCODE_CALL: {
407 if (TextUtils.isEmpty(mDigits.getText().toString())) {
408 // if we are adding a call from the InCallScreen and the phone
409 // number entered is empty, we just close the dialer to expose
410 // the InCallScreen under it.
411 finish();
412 } else {
413 // otherwise, we place the call.
414 placeCall();
415 }
416 return true;
417 }
418 }
419 return super.onKeyDown(keyCode, event);
420 }
421
422 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700423 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700424 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
425 mDigits.onKeyDown(keyCode, event);
426 }
427
428 @Override
429 public boolean onKey(View view, int keyCode, KeyEvent event) {
430 switch (view.getId()) {
431 case R.id.digits:
432 // Happen when "Done" button of the IME is pressed. This can happen when this
433 // Activity is forced into landscape mode due to a desk dock.
434 if (keyCode == KeyEvent.KEYCODE_ENTER
435 && event.getAction() == KeyEvent.ACTION_UP) {
436 placeCall();
437 return true;
438 }
439 break;
440 }
441 return false;
442 }
443
444 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700445 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800446 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700447 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800448 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700449 return handled;
450 }
451
452 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800453 public void onConfirmClick(EmergencyShortcutButton button) {
454 if (button == null) return;
455
456 String phoneNumber = button.getPhoneNumber();
457
458 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800459 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800460 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
461 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
462 } else {
463 Log.d(LOG_TAG, "emergency number is empty");
464 }
465 }
466
467 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 public void onClick(View view) {
469 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700470 case R.id.deleteButton: {
471 keyPressed(KeyEvent.KEYCODE_DEL);
472 return;
473 }
474 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700475 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700476 placeCall();
477 return;
478 }
479 case R.id.digits: {
480 if (mDigits.length() != 0) {
481 mDigits.setCursorVisible(true);
482 }
483 return;
484 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800485 case R.id.floating_action_button_dialpad: {
486 switchView(mDialpadView, mEmergencyShortcutView, true);
487 return;
488 }
489 case R.id.emergency_info_button: {
490 Intent intent = (Intent) view.getTag(R.id.tag_intent);
491 if (intent != null) {
492 startActivity(intent);
493 }
494 return;
495 }
Yorke Lee23a70732014-08-14 17:12:01 -0700496 }
497 }
498
499 @Override
500 public void onPressed(View view, boolean pressed) {
501 if (!pressed) {
502 return;
503 }
504 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 case R.id.one: {
506 playTone(ToneGenerator.TONE_DTMF_1);
507 keyPressed(KeyEvent.KEYCODE_1);
508 return;
509 }
510 case R.id.two: {
511 playTone(ToneGenerator.TONE_DTMF_2);
512 keyPressed(KeyEvent.KEYCODE_2);
513 return;
514 }
515 case R.id.three: {
516 playTone(ToneGenerator.TONE_DTMF_3);
517 keyPressed(KeyEvent.KEYCODE_3);
518 return;
519 }
520 case R.id.four: {
521 playTone(ToneGenerator.TONE_DTMF_4);
522 keyPressed(KeyEvent.KEYCODE_4);
523 return;
524 }
525 case R.id.five: {
526 playTone(ToneGenerator.TONE_DTMF_5);
527 keyPressed(KeyEvent.KEYCODE_5);
528 return;
529 }
530 case R.id.six: {
531 playTone(ToneGenerator.TONE_DTMF_6);
532 keyPressed(KeyEvent.KEYCODE_6);
533 return;
534 }
535 case R.id.seven: {
536 playTone(ToneGenerator.TONE_DTMF_7);
537 keyPressed(KeyEvent.KEYCODE_7);
538 return;
539 }
540 case R.id.eight: {
541 playTone(ToneGenerator.TONE_DTMF_8);
542 keyPressed(KeyEvent.KEYCODE_8);
543 return;
544 }
545 case R.id.nine: {
546 playTone(ToneGenerator.TONE_DTMF_9);
547 keyPressed(KeyEvent.KEYCODE_9);
548 return;
549 }
550 case R.id.zero: {
551 playTone(ToneGenerator.TONE_DTMF_0);
552 keyPressed(KeyEvent.KEYCODE_0);
553 return;
554 }
555 case R.id.pound: {
556 playTone(ToneGenerator.TONE_DTMF_P);
557 keyPressed(KeyEvent.KEYCODE_POUND);
558 return;
559 }
560 case R.id.star: {
561 playTone(ToneGenerator.TONE_DTMF_S);
562 keyPressed(KeyEvent.KEYCODE_STAR);
563 return;
564 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 }
566 }
567
568 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700569 * called for long touch events
570 */
571 @Override
572 public boolean onLongClick(View view) {
573 int id = view.getId();
574 switch (id) {
575 case R.id.deleteButton: {
576 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 return true;
578 }
579 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700580 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700581 keyPressed(KeyEvent.KEYCODE_PLUS);
582 return true;
583 }
584 }
585 return false;
586 }
587
588 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700589 protected void onStart() {
590 super.onStart();
591
592 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700593 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
594 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700595 // Do not animate when view isn't visible yet, just set an initial state.
596 mBackgroundGradient.setColors(lockScreenColors, false);
597 updateTheme(lockScreenColors.supportsDarkText());
598 }
599
600 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700601 protected void onResume() {
602 super.onResume();
603
604 // retrieve the DTMF tone play back setting.
605 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
606 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
607
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608 // if the mToneGenerator creation fails, just continue without it. It is
609 // a local audio signal, and is not as important as the dtmf tone itself.
610 synchronized (mToneGeneratorLock) {
611 if (mToneGenerator == null) {
612 try {
613 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
614 TONE_RELATIVE_VOLUME);
615 } catch (RuntimeException e) {
616 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
617 mToneGenerator = null;
618 }
619 }
620 }
621
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700622 updateDialAndDeleteButtonStateEnabledAttr();
623 }
624
625 @Override
626 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 }
629
Lucas Dupineb9c5702017-05-10 16:57:09 -0700630 @Override
631 protected void onStop() {
632 super.onStop();
633
634 mColorExtractor.removeOnColorsChangedListener(this);
635 }
636
637 /**
638 * Sets theme based on gradient colors
639 * @param supportsDarkText true if gradient supports dark text
640 */
641 private void updateTheme(boolean supportsDarkText) {
642 if (mSupportsDarkText == supportsDarkText) {
643 return;
644 }
645 mSupportsDarkText = supportsDarkText;
646
647 // We can't change themes after inflation, in this case we'll have to recreate
648 // the whole activity.
649 if (mBackgroundGradient != null) {
650 recreate();
651 return;
652 }
653
654 int vis = getWindow().getDecorView().getSystemUiVisibility();
655 if (supportsDarkText) {
656 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
657 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
658 setTheme(R.style.EmergencyDialerThemeDark);
659 } else {
660 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
661 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
662 setTheme(R.style.EmergencyDialerTheme);
663 }
664 getWindow().getDecorView().setSystemUiVisibility(vis);
665 }
666
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 /**
668 * place the call, but check to make sure it is a viable number.
669 */
670 private void placeCall() {
671 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900672
673 // Convert into emergency number according to emergency conversion map.
674 // If conversion map is not defined (this is default), this method does
675 // nothing and just returns input number.
676 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
677
Yorke Lee36bb2542014-06-05 08:09:52 -0700678 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
680
681 // place the call if it is a valid number
682 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
683 // There is no number entered.
684 playTone(ToneGenerator.TONE_PROP_NACK);
685 return;
686 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700687 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
688 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700689 } else {
690 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
691
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700692 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
693 }
Yorke Lee9b341512014-10-17 11:36:41 -0700694 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700695 }
696
697 /**
698 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
699 *
700 * The tone is played locally, using the audio stream for phone calls.
701 * Tones are played only if the "Audible touch tones" user preference
702 * is checked, and are NOT played if the device is in silent mode.
703 *
704 * @param tone a tone code from {@link ToneGenerator}
705 */
706 void playTone(int tone) {
707 // if local tone playback is disabled, just return.
708 if (!mDTMFToneEnabled) {
709 return;
710 }
711
712 // Also do nothing if the phone is in silent mode.
713 // We need to re-check the ringer mode for *every* playTone()
714 // call, rather than keeping a local flag that's updated in
715 // onResume(), since it's possible to toggle silent mode without
716 // leaving the current activity (via the ENDCALL-longpress menu.)
717 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
718 int ringerMode = audioManager.getRingerMode();
719 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
720 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
721 return;
722 }
723
724 synchronized (mToneGeneratorLock) {
725 if (mToneGenerator == null) {
726 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
727 return;
728 }
729
730 // Start the new tone (will stop any playing tone)
731 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
732 }
733 }
734
735 private CharSequence createErrorMessage(String number) {
736 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800737 String errorString = getString(R.string.dial_emergency_error, number);
738 int startingPosition = errorString.indexOf(number);
739 int endingPosition = startingPosition + number.length();
740 Spannable result = new SpannableString(errorString);
741 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
742 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743 } else {
744 return getText(R.string.dial_emergency_empty_error).toString();
745 }
746 }
747
748 @Override
749 protected Dialog onCreateDialog(int id) {
750 AlertDialog dialog = null;
751 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
752 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700753 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
755 .setMessage(createErrorMessage(mLastNumber))
756 .setPositiveButton(R.string.ok, null)
757 .setCancelable(true).create();
758
759 // blur stuff behind the dialog
760 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700761 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 }
763 return dialog;
764 }
765
766 @Override
767 protected void onPrepareDialog(int id, Dialog dialog) {
768 super.onPrepareDialog(id, dialog);
769 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
770 AlertDialog alert = (AlertDialog) dialog;
771 alert.setMessage(createErrorMessage(mLastNumber));
772 }
773 }
774
Andrew Leed5631e82014-10-08 16:03:58 -0700775 @Override
776 public boolean onOptionsItemSelected(MenuItem item) {
777 final int itemId = item.getItemId();
778 if (itemId == android.R.id.home) {
779 onBackPressed();
780 return true;
781 }
782 return super.onOptionsItemSelected(item);
783 }
784
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 /**
786 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
787 */
788 private void updateDialAndDeleteButtonStateEnabledAttr() {
789 final boolean notEmpty = mDigits.length() != 0;
790
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 mDelete.setEnabled(notEmpty);
792 }
Yorke Lee91311662014-10-24 14:50:45 -0700793
794 /**
795 * Remove the digit just before the current position. Used by various long pressed callbacks
796 * to remove the digit that was populated as a result of the short click.
797 */
798 private void removePreviousDigitIfPossible() {
799 final int currentPosition = mDigits.getSelectionStart();
800 if (currentPosition > 0) {
801 mDigits.setSelection(currentPosition);
802 mDigits.getText().delete(currentPosition - 1, currentPosition);
803 }
804 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800805
806 /**
807 * Update the text-to-speech annotations in the edit field.
808 */
809 private void updateTtsSpans() {
810 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
811 mDigits.getText().removeSpan(o);
812 }
813 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
814 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700815
816 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700817 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700818 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700819 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
820 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700821 mBackgroundGradient.setColors(colors);
822 updateTheme(colors.supportsDarkText());
823 }
824 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700825
826 /**
827 * Where a carrier requires a warning that emergency calling is not available while on WFC,
828 * add hint text above the dial pad which warns the user of this case.
829 */
830 private void maybeShowWfcEmergencyCallingWarning() {
831 if (!mIsWfcEmergencyCallingWarningEnabled) {
832 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
833 return;
834 }
835
836 // Use an async task rather than calling into Telephony on UI thread.
837 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
838 @Override
839 protected Boolean doInBackground(Void... voids) {
840 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
841 boolean isWfcAvailable = tm.isWifiCallingAvailable();
842 ServiceState ss = tm.getServiceState();
843 boolean isCellAvailable =
844 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
845 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
846 + " isCellAvailable=" + isCellAvailable
847 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
848 return isWfcAvailable && !isCellAvailable;
849 }
850
851 @Override
852 protected void onPostExecute(Boolean result) {
853 if (result.booleanValue()) {
854 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
855 mDigits.setHint(R.string.dial_emergency_calling_not_available);
856 } else {
857 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
858 mDigits.setHint("");
859 }
860 maybeChangeHintSize();
861 }
862 };
863 showWfcWarningTask.execute((Void) null);
864 }
865
866 /**
867 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
868 * edit view and set the font size to a smaller size appropriate for the emergency calling
869 * warning.
870 */
871 private void maybeChangeHintSize() {
872 if (TextUtils.isEmpty(mDigits.getHint())
873 || !TextUtils.isEmpty(mDigits.getText().toString())) {
874 // No hint or there are dialed digits, so use default size.
875 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
876 // By default, the digits view auto-resizes to fit the text it contains, so
877 // enable that now.
878 mDigits.setResizeEnabled(true);
879 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
880 } else {
881 // Hint present and no dialed digits, set custom font size appropriate for the warning.
882 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
883 R.dimen.emergency_call_warning_size));
884 // Since we're populating this with a static text string, disable auto-resize.
885 mDigits.setResizeEnabled(false);
886 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
887 }
888 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800889
890 private void setupEmergencyShortcutsView() {
891 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
892 mDialpadView = findViewById(R.id.emergency_dialer);
893
894 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
895 dialpadButton.setOnClickListener(this);
896
897 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
898 emergencyInfoButton.setOnClickListener(this);
899
900 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
901 mEmergencyActionGroup.setVisibility(View.GONE);
902
903 // Setup dialpad title.
904 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
905 emergencyDialpadTitle.setVisibility(View.VISIBLE);
906
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800907 // TODO: Integrating emergency phone number table will get location information.
908 // Using null to present no location status.
909 setLocationInfo(null);
910
911 mEmergencyShortcutButtonList = new ArrayList<>();
912 setupEmergencyCallShortcutButton();
913
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800914 switchView(mEmergencyShortcutView, mDialpadView, false);
Billy Chi17ec2282018-06-15 19:00:15 +0800915
916 mEmergencyInfoNameObserver = new ContentObserver(new Handler()) {
917 @Override
918 public void onChange(boolean selfChange) {
919 super.onChange(selfChange);
920 getEmergencyInfoNameAsync();
921 }
922 };
923 // Register ContentProvider for monitoring emergency info name changes.
924 getContentResolver().registerContentObserver(CONTENT_URI, false,
925 mEmergencyInfoNameObserver);
926 // Query emergency info name.
927 getEmergencyInfoNameAsync();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800928 }
929
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800930 private void setLocationInfo(String country) {
931 final View locationInfo = findViewById(R.id.location_info);
932
933 if (TextUtils.isEmpty(country)) {
934 locationInfo.setVisibility(View.INVISIBLE);
935 } else {
936 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
937 location.setText(country);
938 locationInfo.setVisibility(View.VISIBLE);
939 }
940 }
941
942 // TODO: Integrate emergency phone number table.
943 // Using default layout(no location, phone number is 112, description is Emergency,
944 // and icon is cross shape) until integrating emergency phone number table.
945 private void setupEmergencyCallShortcutButton() {
946 final ViewGroup shortcutButtonContainer = findViewById(
947 R.id.emergency_shortcut_buttons_container);
948 shortcutButtonContainer.setClipToOutline(true);
949
950 final EmergencyShortcutButton button =
951 (EmergencyShortcutButton) getLayoutInflater().inflate(
952 R.layout.emergency_shortcut_button,
953 shortcutButtonContainer, false);
954
955 button.setPhoneNumber("112");
956 button.setPhoneDescription("Emergency");
957 button.setPhoneTypeIcon(R.drawable.ic_emergency_number_24);
958 button.setOnConfirmClickListener(this);
959
960 shortcutButtonContainer.addView(button);
961 mEmergencyShortcutButtonList.add(button);
962
963 //Set emergency number title for numerous buttons.
964 if (shortcutButtonContainer.getChildCount() > 1) {
965 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
966 emergencyNumberTitle.setText(getString(R.string.numerous_emergency_numbers_title));
967 }
968 }
969
970 /**
971 * Called by the activity before a touch event is dispatched to the view hierarchy.
972 */
973 private void onPreTouchEvent(MotionEvent event) {
974 mEmergencyActionGroup.onPreTouchEvent(event);
975
976 if (mEmergencyShortcutButtonList != null) {
977 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
978 button.onPreTouchEvent(event);
979 }
980 }
981 }
982
983 /**
984 * Called by the activity after a touch event is dispatched to the view hierarchy.
985 */
986 private void onPostTouchEvent(MotionEvent event) {
987 mEmergencyActionGroup.onPostTouchEvent(event);
988
989 if (mEmergencyShortcutButtonList != null) {
990 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
991 button.onPostTouchEvent(event);
992 }
993 }
994 }
995
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800996 /**
997 * Switch two view.
998 *
999 * @param displayView the view would be displayed.
1000 * @param hideView the view would be hidden.
1001 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1002 */
1003 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1004 if (displayView == null || hideView == null) {
1005 return;
1006 }
1007
1008 if (displayView.getVisibility() == View.VISIBLE) {
1009 return;
1010 }
1011
1012 if (hasAnimation) {
1013 crossfade(hideView, displayView);
1014 } else {
1015 hideView.setVisibility(View.GONE);
1016 displayView.setVisibility(View.VISIBLE);
1017 }
1018 }
1019
1020 /**
1021 * Fade out and fade in animation between two view transition.
1022 */
1023 private void crossfade(View fadeOutView, View fadeInView) {
1024 if (fadeOutView == null || fadeInView == null) {
1025 return;
1026 }
1027 final int shortAnimationDuration = getResources().getInteger(
1028 android.R.integer.config_shortAnimTime);
1029
1030 fadeInView.setAlpha(0f);
1031 fadeInView.setVisibility(View.VISIBLE);
1032
1033 fadeInView.animate()
1034 .alpha(1f)
1035 .setDuration(shortAnimationDuration)
1036 .setListener(null);
1037
1038 fadeOutView.animate()
1039 .alpha(0f)
1040 .setDuration(shortAnimationDuration)
1041 .setListener(new AnimatorListenerAdapter() {
1042 @Override
1043 public void onAnimationEnd(Animator animation) {
1044 fadeOutView.setVisibility(View.GONE);
1045 }
1046 });
1047 }
Billy Chi17ec2282018-06-15 19:00:15 +08001048
1049 /**
1050 * Get emergency info name from EmergencyInfo and then update EmergencyInfoGroup.
1051 */
1052 private void getEmergencyInfoNameAsync() {
1053 new AsyncTask<Void, Void, String>() {
1054 @Override
1055 protected String doInBackground(Void... params) {
1056 String name = "";
1057 try (Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,
1058 null)) {
1059 if (cursor != null && cursor.moveToFirst()) {
1060 int index = cursor.getColumnIndex(KEY_EMERGENCY_INFO_NAME);
1061 name = index > -1 ? cursor.getString(index) : "";
1062 }
1063 } catch (IllegalArgumentException ex) {
1064 Log.w(LOG_TAG, "getEmergencyInfoNameAsync failed", ex);
1065 }
1066 return name;
1067 }
1068
1069 @Override
1070 protected void onPostExecute(String result) {
1071 super.onPostExecute(result);
1072 if (!isFinishing() && !isDestroyed()) {
1073 // Update emergency info with emergency info name
1074 EmergencyInfoGroup group = findViewById(R.id.emergency_info_group);
1075 if (group != null) {
1076 group.updateEmergencyInfo(result);
1077 }
1078 }
1079 }
1080 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
1081 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082}