blob: 856969314cf7482ac5cfb5671f2da409ad26d605 [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 Basseri56814912015-06-26 10:43:21 -0700204 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
205 SubscriptionManager.getDefaultVoiceSubId());
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
243 try {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700244 mHaptic.init(
245 this,
246 carrierConfig.getBoolean(
247 CarrierConfigManager.KEY_ENABLE_DIALER_KEY_VIBRATION_BOOL));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 } catch (Resources.NotFoundException nfe) {
249 Log.e(LOG_TAG, "Vibrate control bool missing.", nfe);
250 }
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700251
252 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 }
254
255 @Override
256 protected void onDestroy() {
257 super.onDestroy();
258 synchronized (mToneGeneratorLock) {
259 if (mToneGenerator != null) {
260 mToneGenerator.release();
261 mToneGenerator = null;
262 }
263 }
264 unregisterReceiver(mBroadcastReceiver);
265 }
266
267 @Override
268 protected void onRestoreInstanceState(Bundle icicle) {
269 mLastNumber = icicle.getString(LAST_NUMBER);
270 }
271
272 @Override
273 protected void onSaveInstanceState(Bundle outState) {
274 super.onSaveInstanceState(outState);
275 outState.putString(LAST_NUMBER, mLastNumber);
276 }
277
278 /**
279 * Explicitly turn off number formatting, since it gets in the way of the emergency
280 * number detector
281 */
282 protected void maybeAddNumberFormatting() {
283 // Do nothing.
284 }
285
286 @Override
287 protected void onPostCreate(Bundle savedInstanceState) {
288 super.onPostCreate(savedInstanceState);
289
290 // This can't be done in onCreate(), since the auto-restoring of the digits
291 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
292 // is called. This method will be called every time the activity is created, and
293 // will always happen after onRestoreSavedInstanceState().
294 mDigits.addTextChangedListener(this);
295 }
296
297 private void setupKeypad() {
298 // Setup the listeners for the buttons
299 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700300 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
301 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 }
303
304 View view = findViewById(R.id.zero);
305 view.setOnLongClickListener(this);
306 }
307
308 /**
309 * handle key events
310 */
311 @Override
312 public boolean onKeyDown(int keyCode, KeyEvent event) {
313 switch (keyCode) {
314 // Happen when there's a "Call" hard button.
315 case KeyEvent.KEYCODE_CALL: {
316 if (TextUtils.isEmpty(mDigits.getText().toString())) {
317 // if we are adding a call from the InCallScreen and the phone
318 // number entered is empty, we just close the dialer to expose
319 // the InCallScreen under it.
320 finish();
321 } else {
322 // otherwise, we place the call.
323 placeCall();
324 }
325 return true;
326 }
327 }
328 return super.onKeyDown(keyCode, event);
329 }
330
331 private void keyPressed(int keyCode) {
332 mHaptic.vibrate();
333 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
334 mDigits.onKeyDown(keyCode, event);
335 }
336
337 @Override
338 public boolean onKey(View view, int keyCode, KeyEvent event) {
339 switch (view.getId()) {
340 case R.id.digits:
341 // Happen when "Done" button of the IME is pressed. This can happen when this
342 // Activity is forced into landscape mode due to a desk dock.
343 if (keyCode == KeyEvent.KEYCODE_ENTER
344 && event.getAction() == KeyEvent.ACTION_UP) {
345 placeCall();
346 return true;
347 }
348 break;
349 }
350 return false;
351 }
352
353 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700354 public boolean dispatchTouchEvent(MotionEvent ev) {
355 mEmergencyActionGroup.onPreTouchEvent(ev);
356 boolean handled = super.dispatchTouchEvent(ev);
357 mEmergencyActionGroup.onPostTouchEvent(ev);
358 return handled;
359 }
360
361 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362 public void onClick(View view) {
363 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700364 case R.id.deleteButton: {
365 keyPressed(KeyEvent.KEYCODE_DEL);
366 return;
367 }
368 case R.id.floating_action_button: {
369 mHaptic.vibrate(); // Vibrate here too, just like we do for the regular keys
370 placeCall();
371 return;
372 }
373 case R.id.digits: {
374 if (mDigits.length() != 0) {
375 mDigits.setCursorVisible(true);
376 }
377 return;
378 }
379 }
380 }
381
382 @Override
383 public void onPressed(View view, boolean pressed) {
384 if (!pressed) {
385 return;
386 }
387 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700388 case R.id.one: {
389 playTone(ToneGenerator.TONE_DTMF_1);
390 keyPressed(KeyEvent.KEYCODE_1);
391 return;
392 }
393 case R.id.two: {
394 playTone(ToneGenerator.TONE_DTMF_2);
395 keyPressed(KeyEvent.KEYCODE_2);
396 return;
397 }
398 case R.id.three: {
399 playTone(ToneGenerator.TONE_DTMF_3);
400 keyPressed(KeyEvent.KEYCODE_3);
401 return;
402 }
403 case R.id.four: {
404 playTone(ToneGenerator.TONE_DTMF_4);
405 keyPressed(KeyEvent.KEYCODE_4);
406 return;
407 }
408 case R.id.five: {
409 playTone(ToneGenerator.TONE_DTMF_5);
410 keyPressed(KeyEvent.KEYCODE_5);
411 return;
412 }
413 case R.id.six: {
414 playTone(ToneGenerator.TONE_DTMF_6);
415 keyPressed(KeyEvent.KEYCODE_6);
416 return;
417 }
418 case R.id.seven: {
419 playTone(ToneGenerator.TONE_DTMF_7);
420 keyPressed(KeyEvent.KEYCODE_7);
421 return;
422 }
423 case R.id.eight: {
424 playTone(ToneGenerator.TONE_DTMF_8);
425 keyPressed(KeyEvent.KEYCODE_8);
426 return;
427 }
428 case R.id.nine: {
429 playTone(ToneGenerator.TONE_DTMF_9);
430 keyPressed(KeyEvent.KEYCODE_9);
431 return;
432 }
433 case R.id.zero: {
434 playTone(ToneGenerator.TONE_DTMF_0);
435 keyPressed(KeyEvent.KEYCODE_0);
436 return;
437 }
438 case R.id.pound: {
439 playTone(ToneGenerator.TONE_DTMF_P);
440 keyPressed(KeyEvent.KEYCODE_POUND);
441 return;
442 }
443 case R.id.star: {
444 playTone(ToneGenerator.TONE_DTMF_S);
445 keyPressed(KeyEvent.KEYCODE_STAR);
446 return;
447 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700448 }
449 }
450
451 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 * called for long touch events
453 */
454 @Override
455 public boolean onLongClick(View view) {
456 int id = view.getId();
457 switch (id) {
458 case R.id.deleteButton: {
459 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700460 return true;
461 }
462 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700463 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 keyPressed(KeyEvent.KEYCODE_PLUS);
465 return true;
466 }
467 }
468 return false;
469 }
470
471 @Override
472 protected void onResume() {
473 super.onResume();
474
475 // retrieve the DTMF tone play back setting.
476 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
477 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
478
479 // Retrieve the haptic feedback setting.
480 mHaptic.checkSystemSetting();
481
482 // if the mToneGenerator creation fails, just continue without it. It is
483 // a local audio signal, and is not as important as the dtmf tone itself.
484 synchronized (mToneGeneratorLock) {
485 if (mToneGenerator == null) {
486 try {
487 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
488 TONE_RELATIVE_VOLUME);
489 } catch (RuntimeException e) {
490 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
491 mToneGenerator = null;
492 }
493 }
494 }
495
496 // Disable the status bar and set the poke lock timeout to medium.
497 // There is no need to do anything with the wake lock.
498 if (DBG) Log.d(LOG_TAG, "disabling status bar, set to long timeout");
499 mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
500
501 updateDialAndDeleteButtonStateEnabledAttr();
502 }
503
504 @Override
505 public void onPause() {
506 // Reenable the status bar and set the poke lock timeout to default.
507 // There is no need to do anything with the wake lock.
508 if (DBG) Log.d(LOG_TAG, "reenabling status bar and closing the dialer");
509 mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
510
511 super.onPause();
512
513 synchronized (mToneGeneratorLock) {
514 if (mToneGenerator != null) {
515 mToneGenerator.release();
516 mToneGenerator = null;
517 }
518 }
519 }
520
521 /**
522 * place the call, but check to make sure it is a viable number.
523 */
524 private void placeCall() {
525 mLastNumber = mDigits.getText().toString();
Yorke Lee36bb2542014-06-05 08:09:52 -0700526 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700527 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
528
529 // place the call if it is a valid number
530 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
531 // There is no number entered.
532 playTone(ToneGenerator.TONE_PROP_NACK);
533 return;
534 }
535 Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY);
Jay Shrauner137458b2014-09-05 14:27:25 -0700536 intent.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700537 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
538 startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700539 } else {
540 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
541
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700542 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
543 }
Yorke Lee9b341512014-10-17 11:36:41 -0700544 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 }
546
547 /**
548 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
549 *
550 * The tone is played locally, using the audio stream for phone calls.
551 * Tones are played only if the "Audible touch tones" user preference
552 * is checked, and are NOT played if the device is in silent mode.
553 *
554 * @param tone a tone code from {@link ToneGenerator}
555 */
556 void playTone(int tone) {
557 // if local tone playback is disabled, just return.
558 if (!mDTMFToneEnabled) {
559 return;
560 }
561
562 // Also do nothing if the phone is in silent mode.
563 // We need to re-check the ringer mode for *every* playTone()
564 // call, rather than keeping a local flag that's updated in
565 // onResume(), since it's possible to toggle silent mode without
566 // leaving the current activity (via the ENDCALL-longpress menu.)
567 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
568 int ringerMode = audioManager.getRingerMode();
569 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
570 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
571 return;
572 }
573
574 synchronized (mToneGeneratorLock) {
575 if (mToneGenerator == null) {
576 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
577 return;
578 }
579
580 // Start the new tone (will stop any playing tone)
581 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
582 }
583 }
584
585 private CharSequence createErrorMessage(String number) {
586 if (!TextUtils.isEmpty(number)) {
587 return getString(R.string.dial_emergency_error, mLastNumber);
588 } else {
589 return getText(R.string.dial_emergency_empty_error).toString();
590 }
591 }
592
593 @Override
594 protected Dialog onCreateDialog(int id) {
595 AlertDialog dialog = null;
596 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
597 // construct dialog
598 dialog = new AlertDialog.Builder(this)
599 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
600 .setMessage(createErrorMessage(mLastNumber))
601 .setPositiveButton(R.string.ok, null)
602 .setCancelable(true).create();
603
604 // blur stuff behind the dialog
605 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Yorke Leec30f00c2014-07-31 16:09:05 -0700606 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 }
608 return dialog;
609 }
610
611 @Override
612 protected void onPrepareDialog(int id, Dialog dialog) {
613 super.onPrepareDialog(id, dialog);
614 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
615 AlertDialog alert = (AlertDialog) dialog;
616 alert.setMessage(createErrorMessage(mLastNumber));
617 }
618 }
619
Andrew Leed5631e82014-10-08 16:03:58 -0700620 @Override
621 public boolean onOptionsItemSelected(MenuItem item) {
622 final int itemId = item.getItemId();
623 if (itemId == android.R.id.home) {
624 onBackPressed();
625 return true;
626 }
627 return super.onOptionsItemSelected(item);
628 }
629
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 /**
631 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
632 */
633 private void updateDialAndDeleteButtonStateEnabledAttr() {
634 final boolean notEmpty = mDigits.length() != 0;
635
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 mDelete.setEnabled(notEmpty);
637 }
Yorke Lee91311662014-10-24 14:50:45 -0700638
639 /**
640 * Remove the digit just before the current position. Used by various long pressed callbacks
641 * to remove the digit that was populated as a result of the short click.
642 */
643 private void removePreviousDigitIfPossible() {
644 final int currentPosition = mDigits.getSelectionStart();
645 if (currentPosition > 0) {
646 mDigits.setSelection(currentPosition);
647 mDigits.getText().delete(currentPosition - 1, currentPosition);
648 }
649 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800650
651 /**
652 * Update the text-to-speech annotations in the edit field.
653 */
654 private void updateTtsSpans() {
655 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
656 mDigits.getText().removeSpan(o);
657 }
658 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
659 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700660}