blob: 70ee5d689c01679838015a480aac58de4c7d63fc [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;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
Lucas Dupineb9c5702017-05-10 16:57:09 -070031import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.media.AudioManager;
33import android.media.ToneGenerator;
34import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070035import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070037import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070039import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070040import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070041import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.telephony.PhoneNumberUtils;
Tyler Gunnc5428972018-03-28 14:15:34 -070043import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070044import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070045import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070047import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080048import android.text.Spannable;
49import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.text.TextUtils;
51import android.text.TextWatcher;
52import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080053import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070055import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070056import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070058import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070059import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.view.View;
61import android.view.WindowManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062
Lucas Dupinaf9e9912017-06-22 12:39:39 -070063import com.android.internal.colorextraction.ColorExtractor;
64import com.android.internal.colorextraction.ColorExtractor.GradientColors;
65import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070066import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070067import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070068import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070/**
71 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
72 *
73 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
74 * activity from apps/Contacts) that:
75 * 1. Allows ONLY emergency calls to be dialed
76 * 2. Disallows voicemail functionality
77 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
78 * activity to stay in front of the keyguard.
79 *
80 * TODO: Even though this is an ultra-simplified version of the normal
81 * dialer, there's still lots of code duplication between this class and
82 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
83 * moved into a shared base class that would live in the framework?
84 * Or could we figure out some way to move *this* class into apps/Contacts
85 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080086 *
87 * TODO: Implement emergency dialer shortcut.
88 * emergency dialer shortcut offer a local emergency number list. Directly click a number to
89 * make an emergency phone call without entering numbers from dialpad.
90 * TODO item:
91 * 1.implement emergency shortcut list UI.
92 * 2.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093 */
94public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070095 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Lucas Dupineb9c5702017-05-10 16:57:09 -070096 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097 // Keys used with onSaveInstanceState().
98 private static final String LAST_NUMBER = "lastNumber";
99
100 // Intent action for this activity.
101 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
102
103 // List of dialer button IDs.
104 private static final int[] DIALER_KEYS = new int[] {
105 R.id.one, R.id.two, R.id.three,
106 R.id.four, R.id.five, R.id.six,
107 R.id.seven, R.id.eight, R.id.nine,
108 R.id.star, R.id.zero, R.id.pound };
109
110 // Debug constants.
111 private static final boolean DBG = false;
112 private static final String LOG_TAG = "EmergencyDialer";
113
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 /** The length of DTMF tones in milliseconds */
115 private static final int TONE_LENGTH_MS = 150;
116
117 /** The DTMF tone volume relative to other sounds in the stream */
118 private static final int TONE_RELATIVE_VOLUME = 80;
119
120 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
121 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
122
123 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
124
Lucas Dupineb9c5702017-05-10 16:57:09 -0700125 /** 90% opacity, different from other gradients **/
126 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
127
Tyler Gunnc5428972018-03-28 14:15:34 -0700128 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129 private View mDialButton;
130 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800131 private View mEmergencyShortcutView;
132 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133
134 private ToneGenerator mToneGenerator;
135 private Object mToneGeneratorLock = new Object();
136
137 // determines if we want to playback local DTMF tones.
138 private boolean mDTMFToneEnabled;
139
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700140 private EmergencyActionGroup mEmergencyActionGroup;
141
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 // close activity when screen turns off
143 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
144 @Override
145 public void onReceive(Context context, Intent intent) {
146 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700147 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 }
149 }
150 };
151
152 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
153
Lucas Dupineb9c5702017-05-10 16:57:09 -0700154 // Background gradient
155 private ColorExtractor mColorExtractor;
156 private GradientDrawable mBackgroundGradient;
157 private boolean mSupportsDarkText;
158
Tyler Gunnc5428972018-03-28 14:15:34 -0700159 private boolean mIsWfcEmergencyCallingWarningEnabled;
160 private float mDefaultDigitsTextSize;
161
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800162 private boolean mAreEmergencyDialerShortcutsEnabled;
163
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 @Override
165 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
166 // Do nothing
167 }
168
169 @Override
170 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700171 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 }
173
174 @Override
175 public void afterTextChanged(Editable input) {
176 // Check for special sequences, in particular the "**04" or "**05"
177 // sequences that allow you to enter PIN or PUK-related codes.
178 //
179 // But note we *don't* allow most other special sequences here,
180 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
181 // since those shouldn't be available if the device is locked.
182 //
183 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
184 // here, not the regular handleChars() method.
185 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
186 // A special sequence was entered, clear the digits
187 mDigits.getText().clear();
188 }
189
190 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800191 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 }
193
194 @Override
195 protected void onCreate(Bundle icicle) {
196 super.onCreate(icicle);
197
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800199 setShowWhenLocked(true);
200 // Allow turning screen on
201 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202
Lucas Dupineb9c5702017-05-10 16:57:09 -0700203 mColorExtractor = new ColorExtractor(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700204 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
205 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700206 updateTheme(lockScreenColors.supportsDarkText());
207
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 setContentView(R.layout.emergency_dialer);
209
Tyler Gunnc5428972018-03-28 14:15:34 -0700210 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 mDigits.setKeyListener(DialerKeyListener.getInstance());
212 mDigits.setOnClickListener(this);
213 mDigits.setOnKeyListener(this);
214 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700215 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700216 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 maybeAddNumberFormatting();
218
Lucas Dupineb9c5702017-05-10 16:57:09 -0700219 mBackgroundGradient = new GradientDrawable(this);
220 Point displaySize = new Point();
221 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
222 .getDefaultDisplay().getSize(displaySize);
223 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
224 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
225 getWindow().setBackgroundDrawable(mBackgroundGradient);
226
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 // Check for the presence of the keypad
228 View view = findViewById(R.id.one);
229 if (view != null) {
230 setupKeypad();
231 }
232
233 mDelete = findViewById(R.id.deleteButton);
234 mDelete.setOnClickListener(this);
235 mDelete.setOnLongClickListener(this);
236
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700237 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238
239 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700240 // Read carrier config through the public API because PhoneGlobals is not available when we
241 // run as a secondary user.
242 CarrierConfigManager configMgr =
243 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
244 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800245 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700246
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700247 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 mDialButton.setOnClickListener(this);
249 } else {
250 mDialButton.setVisibility(View.GONE);
251 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700252 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
253 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
254 maybeShowWfcEmergencyCallingWarning();
255
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700256 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257
258 if (icicle != null) {
259 super.onRestoreInstanceState(icicle);
260 }
261
262 // Extract phone number from intent
263 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700264 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
266 if (number != null) {
267 mDigits.setText(number);
268 }
269 }
270
271 // if the mToneGenerator creation fails, just continue without it. It is
272 // a local audio signal, and is not as important as the dtmf tone itself.
273 synchronized (mToneGeneratorLock) {
274 if (mToneGenerator == null) {
275 try {
276 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
277 } catch (RuntimeException e) {
278 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
279 mToneGenerator = null;
280 }
281 }
282 }
283
284 final IntentFilter intentFilter = new IntentFilter();
285 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
286 registerReceiver(mBroadcastReceiver, intentFilter);
287
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700288 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800289
290 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
291 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
292
293 if (mAreEmergencyDialerShortcutsEnabled) {
294 setupEmergencyShortcutsView();
295 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 }
297
298 @Override
299 protected void onDestroy() {
300 super.onDestroy();
301 synchronized (mToneGeneratorLock) {
302 if (mToneGenerator != null) {
303 mToneGenerator.release();
304 mToneGenerator = null;
305 }
306 }
307 unregisterReceiver(mBroadcastReceiver);
308 }
309
310 @Override
311 protected void onRestoreInstanceState(Bundle icicle) {
312 mLastNumber = icicle.getString(LAST_NUMBER);
313 }
314
315 @Override
316 protected void onSaveInstanceState(Bundle outState) {
317 super.onSaveInstanceState(outState);
318 outState.putString(LAST_NUMBER, mLastNumber);
319 }
320
321 /**
322 * Explicitly turn off number formatting, since it gets in the way of the emergency
323 * number detector
324 */
325 protected void maybeAddNumberFormatting() {
326 // Do nothing.
327 }
328
329 @Override
330 protected void onPostCreate(Bundle savedInstanceState) {
331 super.onPostCreate(savedInstanceState);
332
333 // This can't be done in onCreate(), since the auto-restoring of the digits
334 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
335 // is called. This method will be called every time the activity is created, and
336 // will always happen after onRestoreSavedInstanceState().
337 mDigits.addTextChangedListener(this);
338 }
339
340 private void setupKeypad() {
341 // Setup the listeners for the buttons
342 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700343 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
344 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 }
346
347 View view = findViewById(R.id.zero);
348 view.setOnLongClickListener(this);
349 }
350
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800351 @Override
352 public void onBackPressed() {
353 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
354 // back key will back to display FasterEmergencyDialer view.
355 // Otherwise, it would finish the activity.
356 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
357 && mDialpadView.getVisibility() == View.VISIBLE) {
358 switchView(mEmergencyShortcutView, mDialpadView, true);
359 return;
360 }
361 super.onBackPressed();
362 }
363
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700364 /**
365 * handle key events
366 */
367 @Override
368 public boolean onKeyDown(int keyCode, KeyEvent event) {
369 switch (keyCode) {
370 // Happen when there's a "Call" hard button.
371 case KeyEvent.KEYCODE_CALL: {
372 if (TextUtils.isEmpty(mDigits.getText().toString())) {
373 // if we are adding a call from the InCallScreen and the phone
374 // number entered is empty, we just close the dialer to expose
375 // the InCallScreen under it.
376 finish();
377 } else {
378 // otherwise, we place the call.
379 placeCall();
380 }
381 return true;
382 }
383 }
384 return super.onKeyDown(keyCode, event);
385 }
386
387 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700388 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
390 mDigits.onKeyDown(keyCode, event);
391 }
392
393 @Override
394 public boolean onKey(View view, int keyCode, KeyEvent event) {
395 switch (view.getId()) {
396 case R.id.digits:
397 // Happen when "Done" button of the IME is pressed. This can happen when this
398 // Activity is forced into landscape mode due to a desk dock.
399 if (keyCode == KeyEvent.KEYCODE_ENTER
400 && event.getAction() == KeyEvent.ACTION_UP) {
401 placeCall();
402 return true;
403 }
404 break;
405 }
406 return false;
407 }
408
409 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700410 public boolean dispatchTouchEvent(MotionEvent ev) {
411 mEmergencyActionGroup.onPreTouchEvent(ev);
412 boolean handled = super.dispatchTouchEvent(ev);
413 mEmergencyActionGroup.onPostTouchEvent(ev);
414 return handled;
415 }
416
417 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418 public void onClick(View view) {
419 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700420 case R.id.deleteButton: {
421 keyPressed(KeyEvent.KEYCODE_DEL);
422 return;
423 }
424 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700425 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700426 placeCall();
427 return;
428 }
429 case R.id.digits: {
430 if (mDigits.length() != 0) {
431 mDigits.setCursorVisible(true);
432 }
433 return;
434 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800435 case R.id.floating_action_button_dialpad: {
436 switchView(mDialpadView, mEmergencyShortcutView, true);
437 return;
438 }
439 case R.id.emergency_info_button: {
440 Intent intent = (Intent) view.getTag(R.id.tag_intent);
441 if (intent != null) {
442 startActivity(intent);
443 }
444 return;
445 }
Yorke Lee23a70732014-08-14 17:12:01 -0700446 }
447 }
448
449 @Override
450 public void onPressed(View view, boolean pressed) {
451 if (!pressed) {
452 return;
453 }
454 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700455 case R.id.one: {
456 playTone(ToneGenerator.TONE_DTMF_1);
457 keyPressed(KeyEvent.KEYCODE_1);
458 return;
459 }
460 case R.id.two: {
461 playTone(ToneGenerator.TONE_DTMF_2);
462 keyPressed(KeyEvent.KEYCODE_2);
463 return;
464 }
465 case R.id.three: {
466 playTone(ToneGenerator.TONE_DTMF_3);
467 keyPressed(KeyEvent.KEYCODE_3);
468 return;
469 }
470 case R.id.four: {
471 playTone(ToneGenerator.TONE_DTMF_4);
472 keyPressed(KeyEvent.KEYCODE_4);
473 return;
474 }
475 case R.id.five: {
476 playTone(ToneGenerator.TONE_DTMF_5);
477 keyPressed(KeyEvent.KEYCODE_5);
478 return;
479 }
480 case R.id.six: {
481 playTone(ToneGenerator.TONE_DTMF_6);
482 keyPressed(KeyEvent.KEYCODE_6);
483 return;
484 }
485 case R.id.seven: {
486 playTone(ToneGenerator.TONE_DTMF_7);
487 keyPressed(KeyEvent.KEYCODE_7);
488 return;
489 }
490 case R.id.eight: {
491 playTone(ToneGenerator.TONE_DTMF_8);
492 keyPressed(KeyEvent.KEYCODE_8);
493 return;
494 }
495 case R.id.nine: {
496 playTone(ToneGenerator.TONE_DTMF_9);
497 keyPressed(KeyEvent.KEYCODE_9);
498 return;
499 }
500 case R.id.zero: {
501 playTone(ToneGenerator.TONE_DTMF_0);
502 keyPressed(KeyEvent.KEYCODE_0);
503 return;
504 }
505 case R.id.pound: {
506 playTone(ToneGenerator.TONE_DTMF_P);
507 keyPressed(KeyEvent.KEYCODE_POUND);
508 return;
509 }
510 case R.id.star: {
511 playTone(ToneGenerator.TONE_DTMF_S);
512 keyPressed(KeyEvent.KEYCODE_STAR);
513 return;
514 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 }
516 }
517
518 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519 * called for long touch events
520 */
521 @Override
522 public boolean onLongClick(View view) {
523 int id = view.getId();
524 switch (id) {
525 case R.id.deleteButton: {
526 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700527 return true;
528 }
529 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700530 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 keyPressed(KeyEvent.KEYCODE_PLUS);
532 return true;
533 }
534 }
535 return false;
536 }
537
538 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700539 protected void onStart() {
540 super.onStart();
541
542 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700543 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
544 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700545 // Do not animate when view isn't visible yet, just set an initial state.
546 mBackgroundGradient.setColors(lockScreenColors, false);
547 updateTheme(lockScreenColors.supportsDarkText());
548 }
549
550 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700551 protected void onResume() {
552 super.onResume();
553
554 // retrieve the DTMF tone play back setting.
555 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
556 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
557
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700558 // if the mToneGenerator creation fails, just continue without it. It is
559 // a local audio signal, and is not as important as the dtmf tone itself.
560 synchronized (mToneGeneratorLock) {
561 if (mToneGenerator == null) {
562 try {
563 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
564 TONE_RELATIVE_VOLUME);
565 } catch (RuntimeException e) {
566 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
567 mToneGenerator = null;
568 }
569 }
570 }
571
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700572 updateDialAndDeleteButtonStateEnabledAttr();
573 }
574
575 @Override
576 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578 }
579
Lucas Dupineb9c5702017-05-10 16:57:09 -0700580 @Override
581 protected void onStop() {
582 super.onStop();
583
584 mColorExtractor.removeOnColorsChangedListener(this);
585 }
586
587 /**
588 * Sets theme based on gradient colors
589 * @param supportsDarkText true if gradient supports dark text
590 */
591 private void updateTheme(boolean supportsDarkText) {
592 if (mSupportsDarkText == supportsDarkText) {
593 return;
594 }
595 mSupportsDarkText = supportsDarkText;
596
597 // We can't change themes after inflation, in this case we'll have to recreate
598 // the whole activity.
599 if (mBackgroundGradient != null) {
600 recreate();
601 return;
602 }
603
604 int vis = getWindow().getDecorView().getSystemUiVisibility();
605 if (supportsDarkText) {
606 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
607 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
608 setTheme(R.style.EmergencyDialerThemeDark);
609 } else {
610 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
611 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
612 setTheme(R.style.EmergencyDialerTheme);
613 }
614 getWindow().getDecorView().setSystemUiVisibility(vis);
615 }
616
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 /**
618 * place the call, but check to make sure it is a viable number.
619 */
620 private void placeCall() {
621 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900622
623 // Convert into emergency number according to emergency conversion map.
624 // If conversion map is not defined (this is default), this method does
625 // nothing and just returns input number.
626 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
627
Yorke Lee36bb2542014-06-05 08:09:52 -0700628 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700629 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
630
631 // place the call if it is a valid number
632 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
633 // There is no number entered.
634 playTone(ToneGenerator.TONE_PROP_NACK);
635 return;
636 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700637 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
638 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700639 } else {
640 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
641
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
643 }
Yorke Lee9b341512014-10-17 11:36:41 -0700644 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700645 }
646
647 /**
648 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
649 *
650 * The tone is played locally, using the audio stream for phone calls.
651 * Tones are played only if the "Audible touch tones" user preference
652 * is checked, and are NOT played if the device is in silent mode.
653 *
654 * @param tone a tone code from {@link ToneGenerator}
655 */
656 void playTone(int tone) {
657 // if local tone playback is disabled, just return.
658 if (!mDTMFToneEnabled) {
659 return;
660 }
661
662 // Also do nothing if the phone is in silent mode.
663 // We need to re-check the ringer mode for *every* playTone()
664 // call, rather than keeping a local flag that's updated in
665 // onResume(), since it's possible to toggle silent mode without
666 // leaving the current activity (via the ENDCALL-longpress menu.)
667 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
668 int ringerMode = audioManager.getRingerMode();
669 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
670 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
671 return;
672 }
673
674 synchronized (mToneGeneratorLock) {
675 if (mToneGenerator == null) {
676 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
677 return;
678 }
679
680 // Start the new tone (will stop any playing tone)
681 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
682 }
683 }
684
685 private CharSequence createErrorMessage(String number) {
686 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800687 String errorString = getString(R.string.dial_emergency_error, number);
688 int startingPosition = errorString.indexOf(number);
689 int endingPosition = startingPosition + number.length();
690 Spannable result = new SpannableString(errorString);
691 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
692 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700693 } else {
694 return getText(R.string.dial_emergency_empty_error).toString();
695 }
696 }
697
698 @Override
699 protected Dialog onCreateDialog(int id) {
700 AlertDialog dialog = null;
701 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
702 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700703 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
705 .setMessage(createErrorMessage(mLastNumber))
706 .setPositiveButton(R.string.ok, null)
707 .setCancelable(true).create();
708
709 // blur stuff behind the dialog
710 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700711 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700712 }
713 return dialog;
714 }
715
716 @Override
717 protected void onPrepareDialog(int id, Dialog dialog) {
718 super.onPrepareDialog(id, dialog);
719 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
720 AlertDialog alert = (AlertDialog) dialog;
721 alert.setMessage(createErrorMessage(mLastNumber));
722 }
723 }
724
Andrew Leed5631e82014-10-08 16:03:58 -0700725 @Override
726 public boolean onOptionsItemSelected(MenuItem item) {
727 final int itemId = item.getItemId();
728 if (itemId == android.R.id.home) {
729 onBackPressed();
730 return true;
731 }
732 return super.onOptionsItemSelected(item);
733 }
734
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700735 /**
736 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
737 */
738 private void updateDialAndDeleteButtonStateEnabledAttr() {
739 final boolean notEmpty = mDigits.length() != 0;
740
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 mDelete.setEnabled(notEmpty);
742 }
Yorke Lee91311662014-10-24 14:50:45 -0700743
744 /**
745 * Remove the digit just before the current position. Used by various long pressed callbacks
746 * to remove the digit that was populated as a result of the short click.
747 */
748 private void removePreviousDigitIfPossible() {
749 final int currentPosition = mDigits.getSelectionStart();
750 if (currentPosition > 0) {
751 mDigits.setSelection(currentPosition);
752 mDigits.getText().delete(currentPosition - 1, currentPosition);
753 }
754 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800755
756 /**
757 * Update the text-to-speech annotations in the edit field.
758 */
759 private void updateTtsSpans() {
760 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
761 mDigits.getText().removeSpan(o);
762 }
763 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
764 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700765
766 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700767 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700768 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700769 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
770 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700771 mBackgroundGradient.setColors(colors);
772 updateTheme(colors.supportsDarkText());
773 }
774 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700775
776 /**
777 * Where a carrier requires a warning that emergency calling is not available while on WFC,
778 * add hint text above the dial pad which warns the user of this case.
779 */
780 private void maybeShowWfcEmergencyCallingWarning() {
781 if (!mIsWfcEmergencyCallingWarningEnabled) {
782 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
783 return;
784 }
785
786 // Use an async task rather than calling into Telephony on UI thread.
787 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
788 @Override
789 protected Boolean doInBackground(Void... voids) {
790 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
791 boolean isWfcAvailable = tm.isWifiCallingAvailable();
792 ServiceState ss = tm.getServiceState();
793 boolean isCellAvailable =
794 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
795 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
796 + " isCellAvailable=" + isCellAvailable
797 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
798 return isWfcAvailable && !isCellAvailable;
799 }
800
801 @Override
802 protected void onPostExecute(Boolean result) {
803 if (result.booleanValue()) {
804 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
805 mDigits.setHint(R.string.dial_emergency_calling_not_available);
806 } else {
807 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
808 mDigits.setHint("");
809 }
810 maybeChangeHintSize();
811 }
812 };
813 showWfcWarningTask.execute((Void) null);
814 }
815
816 /**
817 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
818 * edit view and set the font size to a smaller size appropriate for the emergency calling
819 * warning.
820 */
821 private void maybeChangeHintSize() {
822 if (TextUtils.isEmpty(mDigits.getHint())
823 || !TextUtils.isEmpty(mDigits.getText().toString())) {
824 // No hint or there are dialed digits, so use default size.
825 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
826 // By default, the digits view auto-resizes to fit the text it contains, so
827 // enable that now.
828 mDigits.setResizeEnabled(true);
829 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
830 } else {
831 // Hint present and no dialed digits, set custom font size appropriate for the warning.
832 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
833 R.dimen.emergency_call_warning_size));
834 // Since we're populating this with a static text string, disable auto-resize.
835 mDigits.setResizeEnabled(false);
836 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
837 }
838 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800839
840 private void setupEmergencyShortcutsView() {
841 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
842 mDialpadView = findViewById(R.id.emergency_dialer);
843
844 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
845 dialpadButton.setOnClickListener(this);
846
847 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
848 emergencyInfoButton.setOnClickListener(this);
849
850 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
851 mEmergencyActionGroup.setVisibility(View.GONE);
852
853 // Setup dialpad title.
854 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
855 emergencyDialpadTitle.setVisibility(View.VISIBLE);
856
857 switchView(mEmergencyShortcutView, mDialpadView, false);
858 }
859
860 /**
861 * Switch two view.
862 *
863 * @param displayView the view would be displayed.
864 * @param hideView the view would be hidden.
865 * @param hasAnimation is {@code true} when the view should be displayed with animation.
866 */
867 private void switchView(View displayView, View hideView, boolean hasAnimation) {
868 if (displayView == null || hideView == null) {
869 return;
870 }
871
872 if (displayView.getVisibility() == View.VISIBLE) {
873 return;
874 }
875
876 if (hasAnimation) {
877 crossfade(hideView, displayView);
878 } else {
879 hideView.setVisibility(View.GONE);
880 displayView.setVisibility(View.VISIBLE);
881 }
882 }
883
884 /**
885 * Fade out and fade in animation between two view transition.
886 */
887 private void crossfade(View fadeOutView, View fadeInView) {
888 if (fadeOutView == null || fadeInView == null) {
889 return;
890 }
891 final int shortAnimationDuration = getResources().getInteger(
892 android.R.integer.config_shortAnimTime);
893
894 fadeInView.setAlpha(0f);
895 fadeInView.setVisibility(View.VISIBLE);
896
897 fadeInView.animate()
898 .alpha(1f)
899 .setDuration(shortAnimationDuration)
900 .setListener(null);
901
902 fadeOutView.animate()
903 .alpha(0f)
904 .setDuration(shortAnimationDuration)
905 .setListener(new AnimatorListenerAdapter() {
906 @Override
907 public void onAnimationEnd(Animator animation) {
908 fadeOutView.setVisibility(View.GONE);
909 }
910 });
911 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700912}