blob: a98c201cf88fd6722f86c5742959934328c8ec4d [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
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.StatusBarManager;
Lucas Dupineb9c5702017-05-10 16:57:09 -070023import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
Lucas Dupineb9c5702017-05-10 16:57:09 -070028import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.media.AudioManager;
30import android.media.ToneGenerator;
31import android.net.Uri;
32import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070035import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070036import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070037import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.telephony.PhoneNumberUtils;
Santos Cordone137eed2015-06-23 15:34:47 -070039import android.telephony.SubscriptionManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070041import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080042import android.text.Spannable;
43import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.text.TextUtils;
45import android.text.TextWatcher;
46import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080047import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.util.Log;
Yorke Lee116dd072015-08-31 11:38:39 -070049import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070051import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070052import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.view.View;
54import android.view.WindowManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.widget.EditText;
56
Lucas Dupinaf9e9912017-06-22 12:39:39 -070057import com.android.internal.colorextraction.ColorExtractor;
58import com.android.internal.colorextraction.ColorExtractor.GradientColors;
59import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070060import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070061import com.android.phone.common.util.ViewUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063/**
64 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
65 *
66 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
67 * activity from apps/Contacts) that:
68 * 1. Allows ONLY emergency calls to be dialed
69 * 2. Disallows voicemail functionality
70 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
71 * activity to stay in front of the keyguard.
72 *
73 * TODO: Even though this is an ultra-simplified version of the normal
74 * dialer, there's still lots of code duplication between this class and
75 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
76 * moved into a shared base class that would live in the framework?
77 * Or could we figure out some way to move *this* class into apps/Contacts
78 * also?
79 */
80public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070081 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Lucas Dupineb9c5702017-05-10 16:57:09 -070082 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083 // Keys used with onSaveInstanceState().
84 private static final String LAST_NUMBER = "lastNumber";
85
86 // Intent action for this activity.
87 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
88
89 // List of dialer button IDs.
90 private static final int[] DIALER_KEYS = new int[] {
91 R.id.one, R.id.two, R.id.three,
92 R.id.four, R.id.five, R.id.six,
93 R.id.seven, R.id.eight, R.id.nine,
94 R.id.star, R.id.zero, R.id.pound };
95
96 // Debug constants.
97 private static final boolean DBG = false;
98 private static final String LOG_TAG = "EmergencyDialer";
99
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100 /** The length of DTMF tones in milliseconds */
101 private static final int TONE_LENGTH_MS = 150;
102
103 /** The DTMF tone volume relative to other sounds in the stream */
104 private static final int TONE_RELATIVE_VOLUME = 80;
105
106 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
107 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
108
109 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
110
Lucas Dupineb9c5702017-05-10 16:57:09 -0700111 /** 90% opacity, different from other gradients **/
112 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
113
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 EditText mDigits;
115 private View mDialButton;
116 private View mDelete;
117
118 private ToneGenerator mToneGenerator;
119 private Object mToneGeneratorLock = new Object();
120
121 // determines if we want to playback local DTMF tones.
122 private boolean mDTMFToneEnabled;
123
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700124 private EmergencyActionGroup mEmergencyActionGroup;
125
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 // close activity when screen turns off
127 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
128 @Override
129 public void onReceive(Context context, Intent intent) {
130 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700131 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700132 }
133 }
134 };
135
136 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
137
Lucas Dupineb9c5702017-05-10 16:57:09 -0700138 // Background gradient
139 private ColorExtractor mColorExtractor;
140 private GradientDrawable mBackgroundGradient;
141 private boolean mSupportsDarkText;
142
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143 @Override
144 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
145 // Do nothing
146 }
147
148 @Override
149 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
150 // Do nothing
151 }
152
153 @Override
154 public void afterTextChanged(Editable input) {
155 // Check for special sequences, in particular the "**04" or "**05"
156 // sequences that allow you to enter PIN or PUK-related codes.
157 //
158 // But note we *don't* allow most other special sequences here,
159 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
160 // since those shouldn't be available if the device is locked.
161 //
162 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
163 // here, not the regular handleChars() method.
164 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
165 // A special sequence was entered, clear the digits
166 mDigits.getText().clear();
167 }
168
169 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800170 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 }
172
173 @Override
174 protected void onCreate(Bundle icicle) {
175 super.onCreate(icicle);
176
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177 // Allow this activity to be displayed in front of the keyguard / lockscreen.
178 WindowManager.LayoutParams lp = getWindow().getAttributes();
179 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700180
181 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700182 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700183 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
184
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 getWindow().setAttributes(lp);
186
Lucas Dupineb9c5702017-05-10 16:57:09 -0700187 mColorExtractor = new ColorExtractor(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700188 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
189 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700190 updateTheme(lockScreenColors.supportsDarkText());
191
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 setContentView(R.layout.emergency_dialer);
193
194 mDigits = (EditText) findViewById(R.id.digits);
195 mDigits.setKeyListener(DialerKeyListener.getInstance());
196 mDigits.setOnClickListener(this);
197 mDigits.setOnKeyListener(this);
198 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700199 mDigits.setInputType(InputType.TYPE_NULL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 maybeAddNumberFormatting();
201
Lucas Dupineb9c5702017-05-10 16:57:09 -0700202 mBackgroundGradient = new GradientDrawable(this);
203 Point displaySize = new Point();
204 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
205 .getDefaultDisplay().getSize(displaySize);
206 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
207 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
208 getWindow().setBackgroundDrawable(mBackgroundGradient);
209
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 // Check for the presence of the keypad
211 View view = findViewById(R.id.one);
212 if (view != null) {
213 setupKeypad();
214 }
215
216 mDelete = findViewById(R.id.deleteButton);
217 mDelete.setOnClickListener(this);
218 mDelete.setOnLongClickListener(this);
219
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700220 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221
222 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700223 // Read carrier config through the public API because PhoneGlobals is not available when we
224 // run as a secondary user.
225 CarrierConfigManager configMgr =
226 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
227 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800228 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700229 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 mDialButton.setOnClickListener(this);
231 } else {
232 mDialButton.setVisibility(View.GONE);
233 }
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700234 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235
236 if (icicle != null) {
237 super.onRestoreInstanceState(icicle);
238 }
239
240 // Extract phone number from intent
241 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700242 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
244 if (number != null) {
245 mDigits.setText(number);
246 }
247 }
248
249 // if the mToneGenerator creation fails, just continue without it. It is
250 // a local audio signal, and is not as important as the dtmf tone itself.
251 synchronized (mToneGeneratorLock) {
252 if (mToneGenerator == null) {
253 try {
254 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
255 } catch (RuntimeException e) {
256 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
257 mToneGenerator = null;
258 }
259 }
260 }
261
262 final IntentFilter intentFilter = new IntentFilter();
263 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
264 registerReceiver(mBroadcastReceiver, intentFilter);
265
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700266 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 }
268
269 @Override
270 protected void onDestroy() {
271 super.onDestroy();
272 synchronized (mToneGeneratorLock) {
273 if (mToneGenerator != null) {
274 mToneGenerator.release();
275 mToneGenerator = null;
276 }
277 }
278 unregisterReceiver(mBroadcastReceiver);
279 }
280
281 @Override
282 protected void onRestoreInstanceState(Bundle icicle) {
283 mLastNumber = icicle.getString(LAST_NUMBER);
284 }
285
286 @Override
287 protected void onSaveInstanceState(Bundle outState) {
288 super.onSaveInstanceState(outState);
289 outState.putString(LAST_NUMBER, mLastNumber);
290 }
291
292 /**
293 * Explicitly turn off number formatting, since it gets in the way of the emergency
294 * number detector
295 */
296 protected void maybeAddNumberFormatting() {
297 // Do nothing.
298 }
299
300 @Override
301 protected void onPostCreate(Bundle savedInstanceState) {
302 super.onPostCreate(savedInstanceState);
303
304 // This can't be done in onCreate(), since the auto-restoring of the digits
305 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
306 // is called. This method will be called every time the activity is created, and
307 // will always happen after onRestoreSavedInstanceState().
308 mDigits.addTextChangedListener(this);
309 }
310
311 private void setupKeypad() {
312 // Setup the listeners for the buttons
313 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700314 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
315 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 }
317
318 View view = findViewById(R.id.zero);
319 view.setOnLongClickListener(this);
320 }
321
322 /**
323 * handle key events
324 */
325 @Override
326 public boolean onKeyDown(int keyCode, KeyEvent event) {
327 switch (keyCode) {
328 // Happen when there's a "Call" hard button.
329 case KeyEvent.KEYCODE_CALL: {
330 if (TextUtils.isEmpty(mDigits.getText().toString())) {
331 // if we are adding a call from the InCallScreen and the phone
332 // number entered is empty, we just close the dialer to expose
333 // the InCallScreen under it.
334 finish();
335 } else {
336 // otherwise, we place the call.
337 placeCall();
338 }
339 return true;
340 }
341 }
342 return super.onKeyDown(keyCode, event);
343 }
344
345 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700346 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
348 mDigits.onKeyDown(keyCode, event);
349 }
350
351 @Override
352 public boolean onKey(View view, int keyCode, KeyEvent event) {
353 switch (view.getId()) {
354 case R.id.digits:
355 // Happen when "Done" button of the IME is pressed. This can happen when this
356 // Activity is forced into landscape mode due to a desk dock.
357 if (keyCode == KeyEvent.KEYCODE_ENTER
358 && event.getAction() == KeyEvent.ACTION_UP) {
359 placeCall();
360 return true;
361 }
362 break;
363 }
364 return false;
365 }
366
367 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700368 public boolean dispatchTouchEvent(MotionEvent ev) {
369 mEmergencyActionGroup.onPreTouchEvent(ev);
370 boolean handled = super.dispatchTouchEvent(ev);
371 mEmergencyActionGroup.onPostTouchEvent(ev);
372 return handled;
373 }
374
375 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 public void onClick(View view) {
377 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700378 case R.id.deleteButton: {
379 keyPressed(KeyEvent.KEYCODE_DEL);
380 return;
381 }
382 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700383 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700384 placeCall();
385 return;
386 }
387 case R.id.digits: {
388 if (mDigits.length() != 0) {
389 mDigits.setCursorVisible(true);
390 }
391 return;
392 }
393 }
394 }
395
396 @Override
397 public void onPressed(View view, boolean pressed) {
398 if (!pressed) {
399 return;
400 }
401 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402 case R.id.one: {
403 playTone(ToneGenerator.TONE_DTMF_1);
404 keyPressed(KeyEvent.KEYCODE_1);
405 return;
406 }
407 case R.id.two: {
408 playTone(ToneGenerator.TONE_DTMF_2);
409 keyPressed(KeyEvent.KEYCODE_2);
410 return;
411 }
412 case R.id.three: {
413 playTone(ToneGenerator.TONE_DTMF_3);
414 keyPressed(KeyEvent.KEYCODE_3);
415 return;
416 }
417 case R.id.four: {
418 playTone(ToneGenerator.TONE_DTMF_4);
419 keyPressed(KeyEvent.KEYCODE_4);
420 return;
421 }
422 case R.id.five: {
423 playTone(ToneGenerator.TONE_DTMF_5);
424 keyPressed(KeyEvent.KEYCODE_5);
425 return;
426 }
427 case R.id.six: {
428 playTone(ToneGenerator.TONE_DTMF_6);
429 keyPressed(KeyEvent.KEYCODE_6);
430 return;
431 }
432 case R.id.seven: {
433 playTone(ToneGenerator.TONE_DTMF_7);
434 keyPressed(KeyEvent.KEYCODE_7);
435 return;
436 }
437 case R.id.eight: {
438 playTone(ToneGenerator.TONE_DTMF_8);
439 keyPressed(KeyEvent.KEYCODE_8);
440 return;
441 }
442 case R.id.nine: {
443 playTone(ToneGenerator.TONE_DTMF_9);
444 keyPressed(KeyEvent.KEYCODE_9);
445 return;
446 }
447 case R.id.zero: {
448 playTone(ToneGenerator.TONE_DTMF_0);
449 keyPressed(KeyEvent.KEYCODE_0);
450 return;
451 }
452 case R.id.pound: {
453 playTone(ToneGenerator.TONE_DTMF_P);
454 keyPressed(KeyEvent.KEYCODE_POUND);
455 return;
456 }
457 case R.id.star: {
458 playTone(ToneGenerator.TONE_DTMF_S);
459 keyPressed(KeyEvent.KEYCODE_STAR);
460 return;
461 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700462 }
463 }
464
465 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700466 * called for long touch events
467 */
468 @Override
469 public boolean onLongClick(View view) {
470 int id = view.getId();
471 switch (id) {
472 case R.id.deleteButton: {
473 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700474 return true;
475 }
476 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700477 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700478 keyPressed(KeyEvent.KEYCODE_PLUS);
479 return true;
480 }
481 }
482 return false;
483 }
484
485 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700486 protected void onStart() {
487 super.onStart();
488
489 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700490 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
491 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700492 // Do not animate when view isn't visible yet, just set an initial state.
493 mBackgroundGradient.setColors(lockScreenColors, false);
494 updateTheme(lockScreenColors.supportsDarkText());
495 }
496
497 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700498 protected void onResume() {
499 super.onResume();
500
501 // retrieve the DTMF tone play back setting.
502 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
503 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
504
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 // if the mToneGenerator creation fails, just continue without it. It is
506 // a local audio signal, and is not as important as the dtmf tone itself.
507 synchronized (mToneGeneratorLock) {
508 if (mToneGenerator == null) {
509 try {
510 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
511 TONE_RELATIVE_VOLUME);
512 } catch (RuntimeException e) {
513 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
514 mToneGenerator = null;
515 }
516 }
517 }
518
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519 updateDialAndDeleteButtonStateEnabledAttr();
520 }
521
522 @Override
523 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524 super.onPause();
525
526 synchronized (mToneGeneratorLock) {
527 if (mToneGenerator != null) {
528 mToneGenerator.release();
529 mToneGenerator = null;
530 }
531 }
532 }
533
Lucas Dupineb9c5702017-05-10 16:57:09 -0700534 @Override
535 protected void onStop() {
536 super.onStop();
537
538 mColorExtractor.removeOnColorsChangedListener(this);
539 }
540
541 /**
542 * Sets theme based on gradient colors
543 * @param supportsDarkText true if gradient supports dark text
544 */
545 private void updateTheme(boolean supportsDarkText) {
546 if (mSupportsDarkText == supportsDarkText) {
547 return;
548 }
549 mSupportsDarkText = supportsDarkText;
550
551 // We can't change themes after inflation, in this case we'll have to recreate
552 // the whole activity.
553 if (mBackgroundGradient != null) {
554 recreate();
555 return;
556 }
557
558 int vis = getWindow().getDecorView().getSystemUiVisibility();
559 if (supportsDarkText) {
560 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
561 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
562 setTheme(R.style.EmergencyDialerThemeDark);
563 } else {
564 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
565 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
566 setTheme(R.style.EmergencyDialerTheme);
567 }
568 getWindow().getDecorView().setSystemUiVisibility(vis);
569 }
570
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700571 /**
572 * place the call, but check to make sure it is a viable number.
573 */
574 private void placeCall() {
575 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900576
577 // Convert into emergency number according to emergency conversion map.
578 // If conversion map is not defined (this is default), this method does
579 // nothing and just returns input number.
580 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
581
Yorke Lee36bb2542014-06-05 08:09:52 -0700582 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
584
585 // place the call if it is a valid number
586 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
587 // There is no number entered.
588 playTone(ToneGenerator.TONE_PROP_NACK);
589 return;
590 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700591 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
592 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 } else {
594 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
595
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
597 }
Yorke Lee9b341512014-10-17 11:36:41 -0700598 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 }
600
601 /**
602 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
603 *
604 * The tone is played locally, using the audio stream for phone calls.
605 * Tones are played only if the "Audible touch tones" user preference
606 * is checked, and are NOT played if the device is in silent mode.
607 *
608 * @param tone a tone code from {@link ToneGenerator}
609 */
610 void playTone(int tone) {
611 // if local tone playback is disabled, just return.
612 if (!mDTMFToneEnabled) {
613 return;
614 }
615
616 // Also do nothing if the phone is in silent mode.
617 // We need to re-check the ringer mode for *every* playTone()
618 // call, rather than keeping a local flag that's updated in
619 // onResume(), since it's possible to toggle silent mode without
620 // leaving the current activity (via the ENDCALL-longpress menu.)
621 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
622 int ringerMode = audioManager.getRingerMode();
623 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
624 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
625 return;
626 }
627
628 synchronized (mToneGeneratorLock) {
629 if (mToneGenerator == null) {
630 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
631 return;
632 }
633
634 // Start the new tone (will stop any playing tone)
635 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
636 }
637 }
638
639 private CharSequence createErrorMessage(String number) {
640 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800641 String errorString = getString(R.string.dial_emergency_error, number);
642 int startingPosition = errorString.indexOf(number);
643 int endingPosition = startingPosition + number.length();
644 Spannable result = new SpannableString(errorString);
645 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
646 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 } else {
648 return getText(R.string.dial_emergency_empty_error).toString();
649 }
650 }
651
652 @Override
653 protected Dialog onCreateDialog(int id) {
654 AlertDialog dialog = null;
655 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
656 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700657 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700658 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
659 .setMessage(createErrorMessage(mLastNumber))
660 .setPositiveButton(R.string.ok, null)
661 .setCancelable(true).create();
662
663 // blur stuff behind the dialog
664 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Yorke Leec30f00c2014-07-31 16:09:05 -0700665 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700666 }
667 return dialog;
668 }
669
670 @Override
671 protected void onPrepareDialog(int id, Dialog dialog) {
672 super.onPrepareDialog(id, dialog);
673 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
674 AlertDialog alert = (AlertDialog) dialog;
675 alert.setMessage(createErrorMessage(mLastNumber));
676 }
677 }
678
Andrew Leed5631e82014-10-08 16:03:58 -0700679 @Override
680 public boolean onOptionsItemSelected(MenuItem item) {
681 final int itemId = item.getItemId();
682 if (itemId == android.R.id.home) {
683 onBackPressed();
684 return true;
685 }
686 return super.onOptionsItemSelected(item);
687 }
688
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700689 /**
690 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
691 */
692 private void updateDialAndDeleteButtonStateEnabledAttr() {
693 final boolean notEmpty = mDigits.length() != 0;
694
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700695 mDelete.setEnabled(notEmpty);
696 }
Yorke Lee91311662014-10-24 14:50:45 -0700697
698 /**
699 * Remove the digit just before the current position. Used by various long pressed callbacks
700 * to remove the digit that was populated as a result of the short click.
701 */
702 private void removePreviousDigitIfPossible() {
703 final int currentPosition = mDigits.getSelectionStart();
704 if (currentPosition > 0) {
705 mDigits.setSelection(currentPosition);
706 mDigits.getText().delete(currentPosition - 1, currentPosition);
707 }
708 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800709
710 /**
711 * Update the text-to-speech annotations in the edit field.
712 */
713 private void updateTtsSpans() {
714 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
715 mDigits.getText().removeSpan(o);
716 }
717 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
718 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700719
720 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700721 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700722 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700723 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
724 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700725 mBackgroundGradient.setColors(colors);
726 updateTheme(colors.supportsDarkText());
727 }
728 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729}