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