blob: 59d8de78a0a5708b4705e9f209b928f12cbfc6d2 [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;
Yorke Lee116dd072015-08-31 11:38:39 -070044import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070046import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070047import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.view.View;
49import android.view.WindowManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.widget.EditText;
51
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
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700118 private EmergencyActionGroup mEmergencyActionGroup;
119
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 // close activity when screen turns off
121 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
122 @Override
123 public void onReceive(Context context, Intent intent) {
124 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700125 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 }
127 }
128 };
129
130 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
131
132 @Override
133 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
134 // Do nothing
135 }
136
137 @Override
138 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
139 // Do nothing
140 }
141
142 @Override
143 public void afterTextChanged(Editable input) {
144 // Check for special sequences, in particular the "**04" or "**05"
145 // sequences that allow you to enter PIN or PUK-related codes.
146 //
147 // But note we *don't* allow most other special sequences here,
148 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
149 // since those shouldn't be available if the device is locked.
150 //
151 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
152 // here, not the regular handleChars() method.
153 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
154 // A special sequence was entered, clear the digits
155 mDigits.getText().clear();
156 }
157
158 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800159 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160 }
161
162 @Override
163 protected void onCreate(Bundle icicle) {
164 super.onCreate(icicle);
165
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 mStatusBarManager = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167
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);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 maybeAddNumberFormatting();
186
187 // Check for the presence of the keypad
188 View view = findViewById(R.id.one);
189 if (view != null) {
190 setupKeypad();
191 }
192
193 mDelete = findViewById(R.id.deleteButton);
194 mDelete.setOnClickListener(this);
195 mDelete.setOnLongClickListener(this);
196
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700197 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198
199 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700200 // Read carrier config through the public API because PhoneGlobals is not available when we
201 // run as a secondary user.
202 CarrierConfigManager configMgr =
203 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
204 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800205 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700206 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207 mDialButton.setOnClickListener(this);
208 } else {
209 mDialButton.setVisibility(View.GONE);
210 }
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700211 ViewUtil.setupFloatingActionButton(mDialButton, 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
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700243 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 }
245
246 @Override
247 protected void onDestroy() {
248 super.onDestroy();
249 synchronized (mToneGeneratorLock) {
250 if (mToneGenerator != null) {
251 mToneGenerator.release();
252 mToneGenerator = null;
253 }
254 }
255 unregisterReceiver(mBroadcastReceiver);
256 }
257
258 @Override
259 protected void onRestoreInstanceState(Bundle icicle) {
260 mLastNumber = icicle.getString(LAST_NUMBER);
261 }
262
263 @Override
264 protected void onSaveInstanceState(Bundle outState) {
265 super.onSaveInstanceState(outState);
266 outState.putString(LAST_NUMBER, mLastNumber);
267 }
268
269 /**
270 * Explicitly turn off number formatting, since it gets in the way of the emergency
271 * number detector
272 */
273 protected void maybeAddNumberFormatting() {
274 // Do nothing.
275 }
276
277 @Override
278 protected void onPostCreate(Bundle savedInstanceState) {
279 super.onPostCreate(savedInstanceState);
280
281 // This can't be done in onCreate(), since the auto-restoring of the digits
282 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
283 // is called. This method will be called every time the activity is created, and
284 // will always happen after onRestoreSavedInstanceState().
285 mDigits.addTextChangedListener(this);
286 }
287
288 private void setupKeypad() {
289 // Setup the listeners for the buttons
290 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700291 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
292 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293 }
294
295 View view = findViewById(R.id.zero);
296 view.setOnLongClickListener(this);
297 }
298
299 /**
300 * handle key events
301 */
302 @Override
303 public boolean onKeyDown(int keyCode, KeyEvent event) {
304 switch (keyCode) {
305 // Happen when there's a "Call" hard button.
306 case KeyEvent.KEYCODE_CALL: {
307 if (TextUtils.isEmpty(mDigits.getText().toString())) {
308 // if we are adding a call from the InCallScreen and the phone
309 // number entered is empty, we just close the dialer to expose
310 // the InCallScreen under it.
311 finish();
312 } else {
313 // otherwise, we place the call.
314 placeCall();
315 }
316 return true;
317 }
318 }
319 return super.onKeyDown(keyCode, event);
320 }
321
322 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700323 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700324 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
325 mDigits.onKeyDown(keyCode, event);
326 }
327
328 @Override
329 public boolean onKey(View view, int keyCode, KeyEvent event) {
330 switch (view.getId()) {
331 case R.id.digits:
332 // Happen when "Done" button of the IME is pressed. This can happen when this
333 // Activity is forced into landscape mode due to a desk dock.
334 if (keyCode == KeyEvent.KEYCODE_ENTER
335 && event.getAction() == KeyEvent.ACTION_UP) {
336 placeCall();
337 return true;
338 }
339 break;
340 }
341 return false;
342 }
343
344 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700345 public boolean dispatchTouchEvent(MotionEvent ev) {
346 mEmergencyActionGroup.onPreTouchEvent(ev);
347 boolean handled = super.dispatchTouchEvent(ev);
348 mEmergencyActionGroup.onPostTouchEvent(ev);
349 return handled;
350 }
351
352 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 public void onClick(View view) {
354 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700355 case R.id.deleteButton: {
356 keyPressed(KeyEvent.KEYCODE_DEL);
357 return;
358 }
359 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700360 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700361 placeCall();
362 return;
363 }
364 case R.id.digits: {
365 if (mDigits.length() != 0) {
366 mDigits.setCursorVisible(true);
367 }
368 return;
369 }
370 }
371 }
372
373 @Override
374 public void onPressed(View view, boolean pressed) {
375 if (!pressed) {
376 return;
377 }
378 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700379 case R.id.one: {
380 playTone(ToneGenerator.TONE_DTMF_1);
381 keyPressed(KeyEvent.KEYCODE_1);
382 return;
383 }
384 case R.id.two: {
385 playTone(ToneGenerator.TONE_DTMF_2);
386 keyPressed(KeyEvent.KEYCODE_2);
387 return;
388 }
389 case R.id.three: {
390 playTone(ToneGenerator.TONE_DTMF_3);
391 keyPressed(KeyEvent.KEYCODE_3);
392 return;
393 }
394 case R.id.four: {
395 playTone(ToneGenerator.TONE_DTMF_4);
396 keyPressed(KeyEvent.KEYCODE_4);
397 return;
398 }
399 case R.id.five: {
400 playTone(ToneGenerator.TONE_DTMF_5);
401 keyPressed(KeyEvent.KEYCODE_5);
402 return;
403 }
404 case R.id.six: {
405 playTone(ToneGenerator.TONE_DTMF_6);
406 keyPressed(KeyEvent.KEYCODE_6);
407 return;
408 }
409 case R.id.seven: {
410 playTone(ToneGenerator.TONE_DTMF_7);
411 keyPressed(KeyEvent.KEYCODE_7);
412 return;
413 }
414 case R.id.eight: {
415 playTone(ToneGenerator.TONE_DTMF_8);
416 keyPressed(KeyEvent.KEYCODE_8);
417 return;
418 }
419 case R.id.nine: {
420 playTone(ToneGenerator.TONE_DTMF_9);
421 keyPressed(KeyEvent.KEYCODE_9);
422 return;
423 }
424 case R.id.zero: {
425 playTone(ToneGenerator.TONE_DTMF_0);
426 keyPressed(KeyEvent.KEYCODE_0);
427 return;
428 }
429 case R.id.pound: {
430 playTone(ToneGenerator.TONE_DTMF_P);
431 keyPressed(KeyEvent.KEYCODE_POUND);
432 return;
433 }
434 case R.id.star: {
435 playTone(ToneGenerator.TONE_DTMF_S);
436 keyPressed(KeyEvent.KEYCODE_STAR);
437 return;
438 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439 }
440 }
441
442 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700443 * called for long touch events
444 */
445 @Override
446 public boolean onLongClick(View view) {
447 int id = view.getId();
448 switch (id) {
449 case R.id.deleteButton: {
450 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700451 return true;
452 }
453 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700454 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700455 keyPressed(KeyEvent.KEYCODE_PLUS);
456 return true;
457 }
458 }
459 return false;
460 }
461
462 @Override
463 protected void onResume() {
464 super.onResume();
465
466 // retrieve the DTMF tone play back setting.
467 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
468 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
469
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700470 // if the mToneGenerator creation fails, just continue without it. It is
471 // a local audio signal, and is not as important as the dtmf tone itself.
472 synchronized (mToneGeneratorLock) {
473 if (mToneGenerator == null) {
474 try {
475 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
476 TONE_RELATIVE_VOLUME);
477 } catch (RuntimeException e) {
478 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
479 mToneGenerator = null;
480 }
481 }
482 }
483
484 // Disable the status bar and set the poke lock timeout to medium.
485 // There is no need to do anything with the wake lock.
486 if (DBG) Log.d(LOG_TAG, "disabling status bar, set to long timeout");
487 mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
488
489 updateDialAndDeleteButtonStateEnabledAttr();
490 }
491
492 @Override
493 public void onPause() {
494 // Reenable the status bar and set the poke lock timeout to default.
495 // There is no need to do anything with the wake lock.
496 if (DBG) Log.d(LOG_TAG, "reenabling status bar and closing the dialer");
497 mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
498
499 super.onPause();
500
501 synchronized (mToneGeneratorLock) {
502 if (mToneGenerator != null) {
503 mToneGenerator.release();
504 mToneGenerator = null;
505 }
506 }
507 }
508
509 /**
510 * place the call, but check to make sure it is a viable number.
511 */
512 private void placeCall() {
513 mLastNumber = mDigits.getText().toString();
Yorke Lee36bb2542014-06-05 08:09:52 -0700514 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
516
517 // place the call if it is a valid number
518 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
519 // There is no number entered.
520 playTone(ToneGenerator.TONE_PROP_NACK);
521 return;
522 }
523 Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY);
Jay Shrauner137458b2014-09-05 14:27:25 -0700524 intent.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700525 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
526 startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700527 } else {
528 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
529
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
531 }
Yorke Lee9b341512014-10-17 11:36:41 -0700532 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 }
534
535 /**
536 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
537 *
538 * The tone is played locally, using the audio stream for phone calls.
539 * Tones are played only if the "Audible touch tones" user preference
540 * is checked, and are NOT played if the device is in silent mode.
541 *
542 * @param tone a tone code from {@link ToneGenerator}
543 */
544 void playTone(int tone) {
545 // if local tone playback is disabled, just return.
546 if (!mDTMFToneEnabled) {
547 return;
548 }
549
550 // Also do nothing if the phone is in silent mode.
551 // We need to re-check the ringer mode for *every* playTone()
552 // call, rather than keeping a local flag that's updated in
553 // onResume(), since it's possible to toggle silent mode without
554 // leaving the current activity (via the ENDCALL-longpress menu.)
555 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
556 int ringerMode = audioManager.getRingerMode();
557 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
558 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
559 return;
560 }
561
562 synchronized (mToneGeneratorLock) {
563 if (mToneGenerator == null) {
564 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
565 return;
566 }
567
568 // Start the new tone (will stop any playing tone)
569 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
570 }
571 }
572
573 private CharSequence createErrorMessage(String number) {
574 if (!TextUtils.isEmpty(number)) {
575 return getString(R.string.dial_emergency_error, mLastNumber);
576 } else {
577 return getText(R.string.dial_emergency_empty_error).toString();
578 }
579 }
580
581 @Override
582 protected Dialog onCreateDialog(int id) {
583 AlertDialog dialog = null;
584 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
585 // construct dialog
586 dialog = new AlertDialog.Builder(this)
587 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
588 .setMessage(createErrorMessage(mLastNumber))
589 .setPositiveButton(R.string.ok, null)
590 .setCancelable(true).create();
591
592 // blur stuff behind the dialog
593 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Yorke Leec30f00c2014-07-31 16:09:05 -0700594 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700595 }
596 return dialog;
597 }
598
599 @Override
600 protected void onPrepareDialog(int id, Dialog dialog) {
601 super.onPrepareDialog(id, dialog);
602 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
603 AlertDialog alert = (AlertDialog) dialog;
604 alert.setMessage(createErrorMessage(mLastNumber));
605 }
606 }
607
Andrew Leed5631e82014-10-08 16:03:58 -0700608 @Override
609 public boolean onOptionsItemSelected(MenuItem item) {
610 final int itemId = item.getItemId();
611 if (itemId == android.R.id.home) {
612 onBackPressed();
613 return true;
614 }
615 return super.onOptionsItemSelected(item);
616 }
617
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 /**
619 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
620 */
621 private void updateDialAndDeleteButtonStateEnabledAttr() {
622 final boolean notEmpty = mDigits.length() != 0;
623
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 mDelete.setEnabled(notEmpty);
625 }
Yorke Lee91311662014-10-24 14:50:45 -0700626
627 /**
628 * Remove the digit just before the current position. Used by various long pressed callbacks
629 * to remove the digit that was populated as a result of the short click.
630 */
631 private void removePreviousDigitIfPossible() {
632 final int currentPosition = mDigits.getSelectionStart();
633 if (currentPosition > 0) {
634 mDigits.setSelection(currentPosition);
635 mDigits.getText().delete(currentPosition - 1, currentPosition);
636 }
637 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800638
639 /**
640 * Update the text-to-speech annotations in the edit field.
641 */
642 private void updateTtsSpans() {
643 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
644 mDigits.getText().removeSpan(o);
645 }
646 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
647 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700648}