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