blob: 780f76b5d40629977a746d1b2623940cc5d27ec5 [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;
Santos Cordone137eed2015-06-23 15:34:47 -070037import android.telephony.SubscriptionManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.text.Editable;
39import android.text.TextUtils;
40import android.text.TextWatcher;
41import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080042import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.util.Log;
44import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070045import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070046import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.view.View;
48import android.view.WindowManager;
49import android.view.accessibility.AccessibilityManager;
50import android.widget.EditText;
51
52import com.android.phone.common.HapticFeedback;
Yorke Lee23a70732014-08-14 17:12:01 -070053import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070054import com.android.phone.common.util.ViewUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055
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,
Yorke Lee23a70732014-08-14 17:12:01 -070075 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
76 DialpadKeyButton.OnPressedListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077 // 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
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 private StatusBarManager mStatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095
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
Santos Cordonfc309812013-08-20 18:33:16 -0700107 // private static final int USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR = 15000; // millis
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108
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
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700122 private EmergencyActionGroup mEmergencyActionGroup;
123
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 // 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())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700129 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130 }
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();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800163 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 }
165
166 @Override
167 protected void onCreate(Bundle icicle) {
168 super.onCreate(icicle);
169
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700170 mStatusBarManager = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171
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;
Santos Cordonfc309812013-08-20 18:33:16 -0700175
176 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700177 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700178 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
179
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 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);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 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.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700204 // Read carrier config through the public API because PhoneGlobals is not available when we
205 // run as a secondary user.
206 CarrierConfigManager configMgr =
207 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
208 PersistableBundle carrierConfig =
209 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubId());
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700210 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 mDialButton.setOnClickListener(this);
212 } else {
213 mDialButton.setVisibility(View.GONE);
214 }
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700215 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216
217 if (icicle != null) {
218 super.onRestoreInstanceState(icicle);
219 }
220
221 // Extract phone number from intent
222 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700223 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
225 if (number != null) {
226 mDigits.setText(number);
227 }
228 }
229
230 // if the mToneGenerator creation fails, just continue without it. It is
231 // a local audio signal, and is not as important as the dtmf tone itself.
232 synchronized (mToneGeneratorLock) {
233 if (mToneGenerator == null) {
234 try {
235 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
236 } catch (RuntimeException e) {
237 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
238 mToneGenerator = null;
239 }
240 }
241 }
242
243 final IntentFilter intentFilter = new IntentFilter();
244 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
245 registerReceiver(mBroadcastReceiver, intentFilter);
246
247 try {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700248 mHaptic.init(
249 this,
250 carrierConfig.getBoolean(
251 CarrierConfigManager.KEY_ENABLE_DIALER_KEY_VIBRATION_BOOL));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 } catch (Resources.NotFoundException nfe) {
253 Log.e(LOG_TAG, "Vibrate control bool missing.", nfe);
254 }
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700255
256 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 }
258
259 @Override
260 protected void onDestroy() {
261 super.onDestroy();
262 synchronized (mToneGeneratorLock) {
263 if (mToneGenerator != null) {
264 mToneGenerator.release();
265 mToneGenerator = null;
266 }
267 }
268 unregisterReceiver(mBroadcastReceiver);
269 }
270
271 @Override
272 protected void onRestoreInstanceState(Bundle icicle) {
273 mLastNumber = icicle.getString(LAST_NUMBER);
274 }
275
276 @Override
277 protected void onSaveInstanceState(Bundle outState) {
278 super.onSaveInstanceState(outState);
279 outState.putString(LAST_NUMBER, mLastNumber);
280 }
281
282 /**
283 * Explicitly turn off number formatting, since it gets in the way of the emergency
284 * number detector
285 */
286 protected void maybeAddNumberFormatting() {
287 // Do nothing.
288 }
289
290 @Override
291 protected void onPostCreate(Bundle savedInstanceState) {
292 super.onPostCreate(savedInstanceState);
293
294 // This can't be done in onCreate(), since the auto-restoring of the digits
295 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
296 // is called. This method will be called every time the activity is created, and
297 // will always happen after onRestoreSavedInstanceState().
298 mDigits.addTextChangedListener(this);
299 }
300
301 private void setupKeypad() {
302 // Setup the listeners for the buttons
303 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700304 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
305 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 }
307
308 View view = findViewById(R.id.zero);
309 view.setOnLongClickListener(this);
310 }
311
312 /**
313 * handle key events
314 */
315 @Override
316 public boolean onKeyDown(int keyCode, KeyEvent event) {
317 switch (keyCode) {
318 // Happen when there's a "Call" hard button.
319 case KeyEvent.KEYCODE_CALL: {
320 if (TextUtils.isEmpty(mDigits.getText().toString())) {
321 // if we are adding a call from the InCallScreen and the phone
322 // number entered is empty, we just close the dialer to expose
323 // the InCallScreen under it.
324 finish();
325 } else {
326 // otherwise, we place the call.
327 placeCall();
328 }
329 return true;
330 }
331 }
332 return super.onKeyDown(keyCode, event);
333 }
334
335 private void keyPressed(int keyCode) {
336 mHaptic.vibrate();
337 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
338 mDigits.onKeyDown(keyCode, event);
339 }
340
341 @Override
342 public boolean onKey(View view, int keyCode, KeyEvent event) {
343 switch (view.getId()) {
344 case R.id.digits:
345 // Happen when "Done" button of the IME is pressed. This can happen when this
346 // Activity is forced into landscape mode due to a desk dock.
347 if (keyCode == KeyEvent.KEYCODE_ENTER
348 && event.getAction() == KeyEvent.ACTION_UP) {
349 placeCall();
350 return true;
351 }
352 break;
353 }
354 return false;
355 }
356
357 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700358 public boolean dispatchTouchEvent(MotionEvent ev) {
359 mEmergencyActionGroup.onPreTouchEvent(ev);
360 boolean handled = super.dispatchTouchEvent(ev);
361 mEmergencyActionGroup.onPostTouchEvent(ev);
362 return handled;
363 }
364
365 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700366 public void onClick(View view) {
367 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700368 case R.id.deleteButton: {
369 keyPressed(KeyEvent.KEYCODE_DEL);
370 return;
371 }
372 case R.id.floating_action_button: {
373 mHaptic.vibrate(); // Vibrate here too, just like we do for the regular keys
374 placeCall();
375 return;
376 }
377 case R.id.digits: {
378 if (mDigits.length() != 0) {
379 mDigits.setCursorVisible(true);
380 }
381 return;
382 }
383 }
384 }
385
386 @Override
387 public void onPressed(View view, boolean pressed) {
388 if (!pressed) {
389 return;
390 }
391 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700392 case R.id.one: {
393 playTone(ToneGenerator.TONE_DTMF_1);
394 keyPressed(KeyEvent.KEYCODE_1);
395 return;
396 }
397 case R.id.two: {
398 playTone(ToneGenerator.TONE_DTMF_2);
399 keyPressed(KeyEvent.KEYCODE_2);
400 return;
401 }
402 case R.id.three: {
403 playTone(ToneGenerator.TONE_DTMF_3);
404 keyPressed(KeyEvent.KEYCODE_3);
405 return;
406 }
407 case R.id.four: {
408 playTone(ToneGenerator.TONE_DTMF_4);
409 keyPressed(KeyEvent.KEYCODE_4);
410 return;
411 }
412 case R.id.five: {
413 playTone(ToneGenerator.TONE_DTMF_5);
414 keyPressed(KeyEvent.KEYCODE_5);
415 return;
416 }
417 case R.id.six: {
418 playTone(ToneGenerator.TONE_DTMF_6);
419 keyPressed(KeyEvent.KEYCODE_6);
420 return;
421 }
422 case R.id.seven: {
423 playTone(ToneGenerator.TONE_DTMF_7);
424 keyPressed(KeyEvent.KEYCODE_7);
425 return;
426 }
427 case R.id.eight: {
428 playTone(ToneGenerator.TONE_DTMF_8);
429 keyPressed(KeyEvent.KEYCODE_8);
430 return;
431 }
432 case R.id.nine: {
433 playTone(ToneGenerator.TONE_DTMF_9);
434 keyPressed(KeyEvent.KEYCODE_9);
435 return;
436 }
437 case R.id.zero: {
438 playTone(ToneGenerator.TONE_DTMF_0);
439 keyPressed(KeyEvent.KEYCODE_0);
440 return;
441 }
442 case R.id.pound: {
443 playTone(ToneGenerator.TONE_DTMF_P);
444 keyPressed(KeyEvent.KEYCODE_POUND);
445 return;
446 }
447 case R.id.star: {
448 playTone(ToneGenerator.TONE_DTMF_S);
449 keyPressed(KeyEvent.KEYCODE_STAR);
450 return;
451 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 }
453 }
454
455 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700456 * called for long touch events
457 */
458 @Override
459 public boolean onLongClick(View view) {
460 int id = view.getId();
461 switch (id) {
462 case R.id.deleteButton: {
463 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 return true;
465 }
466 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700467 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 keyPressed(KeyEvent.KEYCODE_PLUS);
469 return true;
470 }
471 }
472 return false;
473 }
474
475 @Override
476 protected void onResume() {
477 super.onResume();
478
479 // retrieve the DTMF tone play back setting.
480 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
481 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
482
483 // Retrieve the haptic feedback setting.
484 mHaptic.checkSystemSetting();
485
486 // if the mToneGenerator creation fails, just continue without it. It is
487 // a local audio signal, and is not as important as the dtmf tone itself.
488 synchronized (mToneGeneratorLock) {
489 if (mToneGenerator == null) {
490 try {
491 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
492 TONE_RELATIVE_VOLUME);
493 } catch (RuntimeException e) {
494 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
495 mToneGenerator = null;
496 }
497 }
498 }
499
500 // Disable the status bar and set the poke lock timeout to medium.
501 // There is no need to do anything with the wake lock.
502 if (DBG) Log.d(LOG_TAG, "disabling status bar, set to long timeout");
503 mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
504
505 updateDialAndDeleteButtonStateEnabledAttr();
506 }
507
508 @Override
509 public void onPause() {
510 // Reenable the status bar and set the poke lock timeout to default.
511 // There is no need to do anything with the wake lock.
512 if (DBG) Log.d(LOG_TAG, "reenabling status bar and closing the dialer");
513 mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
514
515 super.onPause();
516
517 synchronized (mToneGeneratorLock) {
518 if (mToneGenerator != null) {
519 mToneGenerator.release();
520 mToneGenerator = null;
521 }
522 }
523 }
524
525 /**
526 * place the call, but check to make sure it is a viable number.
527 */
528 private void placeCall() {
529 mLastNumber = mDigits.getText().toString();
Yorke Lee36bb2542014-06-05 08:09:52 -0700530 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
532
533 // place the call if it is a valid number
534 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
535 // There is no number entered.
536 playTone(ToneGenerator.TONE_PROP_NACK);
537 return;
538 }
539 Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY);
Jay Shrauner137458b2014-09-05 14:27:25 -0700540 intent.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
542 startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 } else {
544 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
545
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
547 }
Yorke Lee9b341512014-10-17 11:36:41 -0700548 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 }
550
551 /**
552 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
553 *
554 * The tone is played locally, using the audio stream for phone calls.
555 * Tones are played only if the "Audible touch tones" user preference
556 * is checked, and are NOT played if the device is in silent mode.
557 *
558 * @param tone a tone code from {@link ToneGenerator}
559 */
560 void playTone(int tone) {
561 // if local tone playback is disabled, just return.
562 if (!mDTMFToneEnabled) {
563 return;
564 }
565
566 // Also do nothing if the phone is in silent mode.
567 // We need to re-check the ringer mode for *every* playTone()
568 // call, rather than keeping a local flag that's updated in
569 // onResume(), since it's possible to toggle silent mode without
570 // leaving the current activity (via the ENDCALL-longpress menu.)
571 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
572 int ringerMode = audioManager.getRingerMode();
573 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
574 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
575 return;
576 }
577
578 synchronized (mToneGeneratorLock) {
579 if (mToneGenerator == null) {
580 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
581 return;
582 }
583
584 // Start the new tone (will stop any playing tone)
585 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
586 }
587 }
588
589 private CharSequence createErrorMessage(String number) {
590 if (!TextUtils.isEmpty(number)) {
591 return getString(R.string.dial_emergency_error, mLastNumber);
592 } else {
593 return getText(R.string.dial_emergency_empty_error).toString();
594 }
595 }
596
597 @Override
598 protected Dialog onCreateDialog(int id) {
599 AlertDialog dialog = null;
600 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
601 // construct dialog
602 dialog = new AlertDialog.Builder(this)
603 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
604 .setMessage(createErrorMessage(mLastNumber))
605 .setPositiveButton(R.string.ok, null)
606 .setCancelable(true).create();
607
608 // blur stuff behind the dialog
609 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Yorke Leec30f00c2014-07-31 16:09:05 -0700610 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700611 }
612 return dialog;
613 }
614
615 @Override
616 protected void onPrepareDialog(int id, Dialog dialog) {
617 super.onPrepareDialog(id, dialog);
618 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
619 AlertDialog alert = (AlertDialog) dialog;
620 alert.setMessage(createErrorMessage(mLastNumber));
621 }
622 }
623
Andrew Leed5631e82014-10-08 16:03:58 -0700624 @Override
625 public boolean onOptionsItemSelected(MenuItem item) {
626 final int itemId = item.getItemId();
627 if (itemId == android.R.id.home) {
628 onBackPressed();
629 return true;
630 }
631 return super.onOptionsItemSelected(item);
632 }
633
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 /**
635 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
636 */
637 private void updateDialAndDeleteButtonStateEnabledAttr() {
638 final boolean notEmpty = mDigits.length() != 0;
639
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 mDelete.setEnabled(notEmpty);
641 }
Yorke Lee91311662014-10-24 14:50:45 -0700642
643 /**
644 * Remove the digit just before the current position. Used by various long pressed callbacks
645 * to remove the digit that was populated as a result of the short click.
646 */
647 private void removePreviousDigitIfPossible() {
648 final int currentPosition = mDigits.getSelectionStart();
649 if (currentPosition > 0) {
650 mDigits.setSelection(currentPosition);
651 mDigits.getText().delete(currentPosition - 1, currentPosition);
652 }
653 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800654
655 /**
656 * Update the text-to-speech annotations in the edit field.
657 */
658 private void updateTtsSpans() {
659 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
660 mDigits.getText().removeSpan(o);
661 }
662 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
663 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664}