blob: 424dddea5096a6c42a2be514e696d7f7ab5757c1 [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;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.content.res.Resources;
28import android.media.AudioManager;
29import android.media.ToneGenerator;
30import android.net.Uri;
31import android.os.Bundle;
32import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070033import android.telecom.PhoneAccount;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.telephony.PhoneNumberUtils;
35import android.text.Editable;
36import android.text.TextUtils;
37import android.text.TextWatcher;
38import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080039import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.util.Log;
41import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070042import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.view.View;
44import android.view.WindowManager;
45import android.view.accessibility.AccessibilityManager;
46import android.widget.EditText;
47
48import com.android.phone.common.HapticFeedback;
Yorke Lee23a70732014-08-14 17:12:01 -070049import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070050import com.android.phone.common.util.ViewUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051
52
53/**
54 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
55 *
56 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
57 * activity from apps/Contacts) that:
58 * 1. Allows ONLY emergency calls to be dialed
59 * 2. Disallows voicemail functionality
60 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
61 * activity to stay in front of the keyguard.
62 *
63 * TODO: Even though this is an ultra-simplified version of the normal
64 * dialer, there's still lots of code duplication between this class and
65 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
66 * moved into a shared base class that would live in the framework?
67 * Or could we figure out some way to move *this* class into apps/Contacts
68 * also?
69 */
70public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070071 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
72 DialpadKeyButton.OnPressedListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073 // Keys used with onSaveInstanceState().
74 private static final String LAST_NUMBER = "lastNumber";
75
76 // Intent action for this activity.
77 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
78
79 // List of dialer button IDs.
80 private static final int[] DIALER_KEYS = new int[] {
81 R.id.one, R.id.two, R.id.three,
82 R.id.four, R.id.five, R.id.six,
83 R.id.seven, R.id.eight, R.id.nine,
84 R.id.star, R.id.zero, R.id.pound };
85
86 // Debug constants.
87 private static final boolean DBG = false;
88 private static final String LOG_TAG = "EmergencyDialer";
89
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090 private StatusBarManager mStatusBarManager;
91 private AccessibilityManager mAccessibilityManager;
92
93 /** The length of DTMF tones in milliseconds */
94 private static final int TONE_LENGTH_MS = 150;
95
96 /** The DTMF tone volume relative to other sounds in the stream */
97 private static final int TONE_RELATIVE_VOLUME = 80;
98
99 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
100 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
101
102 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
103
Santos Cordonfc309812013-08-20 18:33:16 -0700104 // private static final int USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR = 15000; // millis
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 EditText mDigits;
107 private View mDialButton;
108 private View mDelete;
109
110 private ToneGenerator mToneGenerator;
111 private Object mToneGeneratorLock = new Object();
112
113 // determines if we want to playback local DTMF tones.
114 private boolean mDTMFToneEnabled;
115
116 // Haptic feedback (vibration) for dialer key presses.
117 private HapticFeedback mHaptic = new HapticFeedback();
118
119 // close activity when screen turns off
120 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
121 @Override
122 public void onReceive(Context context, Intent intent) {
123 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
124 finish();
125 }
126 }
127 };
128
129 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
130
131 @Override
132 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
133 // Do nothing
134 }
135
136 @Override
137 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
138 // Do nothing
139 }
140
141 @Override
142 public void afterTextChanged(Editable input) {
143 // Check for special sequences, in particular the "**04" or "**05"
144 // sequences that allow you to enter PIN or PUK-related codes.
145 //
146 // But note we *don't* allow most other special sequences here,
147 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
148 // since those shouldn't be available if the device is locked.
149 //
150 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
151 // here, not the regular handleChars() method.
152 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
153 // A special sequence was entered, clear the digits
154 mDigits.getText().clear();
155 }
156
157 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800158 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 }
160
161 @Override
162 protected void onCreate(Bundle icicle) {
163 super.onCreate(icicle);
164
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 mStatusBarManager = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
166 mAccessibilityManager = (AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE);
167
168 // Allow this activity to be displayed in front of the keyguard / lockscreen.
169 WindowManager.LayoutParams lp = getWindow().getAttributes();
170 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700171
172 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700173 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700174 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
175
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176 getWindow().setAttributes(lp);
177
178 setContentView(R.layout.emergency_dialer);
179
180 mDigits = (EditText) findViewById(R.id.digits);
181 mDigits.setKeyListener(DialerKeyListener.getInstance());
182 mDigits.setOnClickListener(this);
183 mDigits.setOnKeyListener(this);
184 mDigits.setLongClickable(false);
185 if (mAccessibilityManager.isEnabled()) {
186 // The text view must be selected to send accessibility events.
187 mDigits.setSelected(true);
188 }
189 maybeAddNumberFormatting();
190
191 // Check for the presence of the keypad
192 View view = findViewById(R.id.one);
193 if (view != null) {
194 setupKeypad();
195 }
196
197 mDelete = findViewById(R.id.deleteButton);
198 mDelete.setOnClickListener(this);
199 mDelete.setOnLongClickListener(this);
200
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700201 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202
203 // Check whether we should show the onscreen "Dial" button and co.
204 Resources res = getResources();
205 if (res.getBoolean(R.bool.config_show_onscreen_dial_button)) {
206 mDialButton.setOnClickListener(this);
207 } else {
208 mDialButton.setVisibility(View.GONE);
209 }
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700210 View floatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
211 ViewUtil.setupFloatingActionButton(floatingActionButtonContainer, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212
213 if (icicle != null) {
214 super.onRestoreInstanceState(icicle);
215 }
216
217 // Extract phone number from intent
218 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700219 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
221 if (number != null) {
222 mDigits.setText(number);
223 }
224 }
225
226 // if the mToneGenerator creation fails, just continue without it. It is
227 // a local audio signal, and is not as important as the dtmf tone itself.
228 synchronized (mToneGeneratorLock) {
229 if (mToneGenerator == null) {
230 try {
231 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
232 } catch (RuntimeException e) {
233 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
234 mToneGenerator = null;
235 }
236 }
237 }
238
239 final IntentFilter intentFilter = new IntentFilter();
240 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
241 registerReceiver(mBroadcastReceiver, intentFilter);
242
243 try {
244 mHaptic.init(this, res.getBoolean(R.bool.config_enable_dialer_key_vibration));
245 } catch (Resources.NotFoundException nfe) {
246 Log.e(LOG_TAG, "Vibrate control bool missing.", nfe);
247 }
248 }
249
250 @Override
251 protected void onDestroy() {
252 super.onDestroy();
253 synchronized (mToneGeneratorLock) {
254 if (mToneGenerator != null) {
255 mToneGenerator.release();
256 mToneGenerator = null;
257 }
258 }
259 unregisterReceiver(mBroadcastReceiver);
260 }
261
262 @Override
263 protected void onRestoreInstanceState(Bundle icicle) {
264 mLastNumber = icicle.getString(LAST_NUMBER);
265 }
266
267 @Override
268 protected void onSaveInstanceState(Bundle outState) {
269 super.onSaveInstanceState(outState);
270 outState.putString(LAST_NUMBER, mLastNumber);
271 }
272
273 /**
274 * Explicitly turn off number formatting, since it gets in the way of the emergency
275 * number detector
276 */
277 protected void maybeAddNumberFormatting() {
278 // Do nothing.
279 }
280
281 @Override
282 protected void onPostCreate(Bundle savedInstanceState) {
283 super.onPostCreate(savedInstanceState);
284
285 // This can't be done in onCreate(), since the auto-restoring of the digits
286 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
287 // is called. This method will be called every time the activity is created, and
288 // will always happen after onRestoreSavedInstanceState().
289 mDigits.addTextChangedListener(this);
290 }
291
292 private void setupKeypad() {
293 // Setup the listeners for the buttons
294 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700295 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
296 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 }
298
299 View view = findViewById(R.id.zero);
300 view.setOnLongClickListener(this);
301 }
302
303 /**
304 * handle key events
305 */
306 @Override
307 public boolean onKeyDown(int keyCode, KeyEvent event) {
308 switch (keyCode) {
309 // Happen when there's a "Call" hard button.
310 case KeyEvent.KEYCODE_CALL: {
311 if (TextUtils.isEmpty(mDigits.getText().toString())) {
312 // if we are adding a call from the InCallScreen and the phone
313 // number entered is empty, we just close the dialer to expose
314 // the InCallScreen under it.
315 finish();
316 } else {
317 // otherwise, we place the call.
318 placeCall();
319 }
320 return true;
321 }
322 }
323 return super.onKeyDown(keyCode, event);
324 }
325
326 private void keyPressed(int keyCode) {
327 mHaptic.vibrate();
328 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
329 mDigits.onKeyDown(keyCode, event);
330 }
331
332 @Override
333 public boolean onKey(View view, int keyCode, KeyEvent event) {
334 switch (view.getId()) {
335 case R.id.digits:
336 // Happen when "Done" button of the IME is pressed. This can happen when this
337 // Activity is forced into landscape mode due to a desk dock.
338 if (keyCode == KeyEvent.KEYCODE_ENTER
339 && event.getAction() == KeyEvent.ACTION_UP) {
340 placeCall();
341 return true;
342 }
343 break;
344 }
345 return false;
346 }
347
348 @Override
349 public void onClick(View view) {
350 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700351 case R.id.deleteButton: {
352 keyPressed(KeyEvent.KEYCODE_DEL);
353 return;
354 }
355 case R.id.floating_action_button: {
356 mHaptic.vibrate(); // Vibrate here too, just like we do for the regular keys
357 placeCall();
358 return;
359 }
360 case R.id.digits: {
361 if (mDigits.length() != 0) {
362 mDigits.setCursorVisible(true);
363 }
364 return;
365 }
366 }
367 }
368
369 @Override
370 public void onPressed(View view, boolean pressed) {
371 if (!pressed) {
372 return;
373 }
374 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 case R.id.one: {
376 playTone(ToneGenerator.TONE_DTMF_1);
377 keyPressed(KeyEvent.KEYCODE_1);
378 return;
379 }
380 case R.id.two: {
381 playTone(ToneGenerator.TONE_DTMF_2);
382 keyPressed(KeyEvent.KEYCODE_2);
383 return;
384 }
385 case R.id.three: {
386 playTone(ToneGenerator.TONE_DTMF_3);
387 keyPressed(KeyEvent.KEYCODE_3);
388 return;
389 }
390 case R.id.four: {
391 playTone(ToneGenerator.TONE_DTMF_4);
392 keyPressed(KeyEvent.KEYCODE_4);
393 return;
394 }
395 case R.id.five: {
396 playTone(ToneGenerator.TONE_DTMF_5);
397 keyPressed(KeyEvent.KEYCODE_5);
398 return;
399 }
400 case R.id.six: {
401 playTone(ToneGenerator.TONE_DTMF_6);
402 keyPressed(KeyEvent.KEYCODE_6);
403 return;
404 }
405 case R.id.seven: {
406 playTone(ToneGenerator.TONE_DTMF_7);
407 keyPressed(KeyEvent.KEYCODE_7);
408 return;
409 }
410 case R.id.eight: {
411 playTone(ToneGenerator.TONE_DTMF_8);
412 keyPressed(KeyEvent.KEYCODE_8);
413 return;
414 }
415 case R.id.nine: {
416 playTone(ToneGenerator.TONE_DTMF_9);
417 keyPressed(KeyEvent.KEYCODE_9);
418 return;
419 }
420 case R.id.zero: {
421 playTone(ToneGenerator.TONE_DTMF_0);
422 keyPressed(KeyEvent.KEYCODE_0);
423 return;
424 }
425 case R.id.pound: {
426 playTone(ToneGenerator.TONE_DTMF_P);
427 keyPressed(KeyEvent.KEYCODE_POUND);
428 return;
429 }
430 case R.id.star: {
431 playTone(ToneGenerator.TONE_DTMF_S);
432 keyPressed(KeyEvent.KEYCODE_STAR);
433 return;
434 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700435 }
436 }
437
438 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439 * called for long touch events
440 */
441 @Override
442 public boolean onLongClick(View view) {
443 int id = view.getId();
444 switch (id) {
445 case R.id.deleteButton: {
446 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700447 return true;
448 }
449 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700450 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700451 keyPressed(KeyEvent.KEYCODE_PLUS);
452 return true;
453 }
454 }
455 return false;
456 }
457
458 @Override
459 protected void onResume() {
460 super.onResume();
461
462 // retrieve the DTMF tone play back setting.
463 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
464 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
465
466 // Retrieve the haptic feedback setting.
467 mHaptic.checkSystemSetting();
468
469 // if the mToneGenerator creation fails, just continue without it. It is
470 // a local audio signal, and is not as important as the dtmf tone itself.
471 synchronized (mToneGeneratorLock) {
472 if (mToneGenerator == null) {
473 try {
474 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
475 TONE_RELATIVE_VOLUME);
476 } catch (RuntimeException e) {
477 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
478 mToneGenerator = null;
479 }
480 }
481 }
482
483 // Disable the status bar and set the poke lock timeout to medium.
484 // There is no need to do anything with the wake lock.
485 if (DBG) Log.d(LOG_TAG, "disabling status bar, set to long timeout");
486 mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
487
488 updateDialAndDeleteButtonStateEnabledAttr();
489 }
490
491 @Override
492 public void onPause() {
493 // Reenable the status bar and set the poke lock timeout to default.
494 // There is no need to do anything with the wake lock.
495 if (DBG) Log.d(LOG_TAG, "reenabling status bar and closing the dialer");
496 mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
497
498 super.onPause();
499
500 synchronized (mToneGeneratorLock) {
501 if (mToneGenerator != null) {
502 mToneGenerator.release();
503 mToneGenerator = null;
504 }
505 }
506 }
507
508 /**
509 * place the call, but check to make sure it is a viable number.
510 */
511 private void placeCall() {
512 mLastNumber = mDigits.getText().toString();
Yorke Lee36bb2542014-06-05 08:09:52 -0700513 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700514 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
515
516 // place the call if it is a valid number
517 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
518 // There is no number entered.
519 playTone(ToneGenerator.TONE_PROP_NACK);
520 return;
521 }
522 Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY);
Jay Shrauner137458b2014-09-05 14:27:25 -0700523 intent.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
525 startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700526 } else {
527 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
528
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700529 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
530 }
Yorke Lee9b341512014-10-17 11:36:41 -0700531 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700532 }
533
534 /**
535 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
536 *
537 * The tone is played locally, using the audio stream for phone calls.
538 * Tones are played only if the "Audible touch tones" user preference
539 * is checked, and are NOT played if the device is in silent mode.
540 *
541 * @param tone a tone code from {@link ToneGenerator}
542 */
543 void playTone(int tone) {
544 // if local tone playback is disabled, just return.
545 if (!mDTMFToneEnabled) {
546 return;
547 }
548
549 // Also do nothing if the phone is in silent mode.
550 // We need to re-check the ringer mode for *every* playTone()
551 // call, rather than keeping a local flag that's updated in
552 // onResume(), since it's possible to toggle silent mode without
553 // leaving the current activity (via the ENDCALL-longpress menu.)
554 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
555 int ringerMode = audioManager.getRingerMode();
556 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
557 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
558 return;
559 }
560
561 synchronized (mToneGeneratorLock) {
562 if (mToneGenerator == null) {
563 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
564 return;
565 }
566
567 // Start the new tone (will stop any playing tone)
568 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
569 }
570 }
571
572 private CharSequence createErrorMessage(String number) {
573 if (!TextUtils.isEmpty(number)) {
574 return getString(R.string.dial_emergency_error, mLastNumber);
575 } else {
576 return getText(R.string.dial_emergency_empty_error).toString();
577 }
578 }
579
580 @Override
581 protected Dialog onCreateDialog(int id) {
582 AlertDialog dialog = null;
583 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
584 // construct dialog
585 dialog = new AlertDialog.Builder(this)
586 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
587 .setMessage(createErrorMessage(mLastNumber))
588 .setPositiveButton(R.string.ok, null)
589 .setCancelable(true).create();
590
591 // blur stuff behind the dialog
592 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Yorke Leec30f00c2014-07-31 16:09:05 -0700593 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 }
595 return dialog;
596 }
597
598 @Override
599 protected void onPrepareDialog(int id, Dialog dialog) {
600 super.onPrepareDialog(id, dialog);
601 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
602 AlertDialog alert = (AlertDialog) dialog;
603 alert.setMessage(createErrorMessage(mLastNumber));
604 }
605 }
606
Andrew Leed5631e82014-10-08 16:03:58 -0700607 @Override
608 public boolean onOptionsItemSelected(MenuItem item) {
609 final int itemId = item.getItemId();
610 if (itemId == android.R.id.home) {
611 onBackPressed();
612 return true;
613 }
614 return super.onOptionsItemSelected(item);
615 }
616
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 /**
618 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
619 */
620 private void updateDialAndDeleteButtonStateEnabledAttr() {
621 final boolean notEmpty = mDigits.length() != 0;
622
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700623 mDelete.setEnabled(notEmpty);
624 }
Yorke Lee91311662014-10-24 14:50:45 -0700625
626 /**
627 * Remove the digit just before the current position. Used by various long pressed callbacks
628 * to remove the digit that was populated as a result of the short click.
629 */
630 private void removePreviousDigitIfPossible() {
631 final int currentPosition = mDigits.getSelectionStart();
632 if (currentPosition > 0) {
633 mDigits.setSelection(currentPosition);
634 mDigits.getText().delete(currentPosition - 1, currentPosition);
635 }
636 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800637
638 /**
639 * Update the text-to-speech annotations in the edit field.
640 */
641 private void updateTtsSpans() {
642 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
643 mDigits.getText().removeSpan(o);
644 }
645 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
646 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647}