blob: fd64ff2fbdc30f9d219a9ed3b4c12163075b784f [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;
Lucas Dupineb9c5702017-05-10 16:57:09 -070023import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
Lucas Dupineb9c5702017-05-10 16:57:09 -070028import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.media.AudioManager;
30import android.media.ToneGenerator;
31import android.net.Uri;
32import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070035import android.telecom.PhoneAccount;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070036import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.telephony.PhoneNumberUtils;
Santos Cordone137eed2015-06-23 15:34:47 -070038import android.telephony.SubscriptionManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070040import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080041import android.text.Spannable;
42import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.text.TextUtils;
44import android.text.TextWatcher;
45import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080046import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.util.Log;
Yorke Lee116dd072015-08-31 11:38:39 -070048import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070050import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070051import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.view.View;
53import android.view.WindowManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.widget.EditText;
55
Lucas Dupinaf9e9912017-06-22 12:39:39 -070056import com.android.internal.colorextraction.ColorExtractor;
57import com.android.internal.colorextraction.ColorExtractor.GradientColors;
58import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070059import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070060import com.android.phone.common.util.ViewUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062/**
63 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
64 *
65 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
66 * activity from apps/Contacts) that:
67 * 1. Allows ONLY emergency calls to be dialed
68 * 2. Disallows voicemail functionality
69 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
70 * activity to stay in front of the keyguard.
71 *
72 * TODO: Even though this is an ultra-simplified version of the normal
73 * dialer, there's still lots of code duplication between this class and
74 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
75 * moved into a shared base class that would live in the framework?
76 * Or could we figure out some way to move *this* class into apps/Contacts
77 * also?
78 */
79public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070080 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Lucas Dupineb9c5702017-05-10 16:57:09 -070081 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082 // Keys used with onSaveInstanceState().
83 private static final String LAST_NUMBER = "lastNumber";
84
85 // Intent action for this activity.
86 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
87
88 // List of dialer button IDs.
89 private static final int[] DIALER_KEYS = new int[] {
90 R.id.one, R.id.two, R.id.three,
91 R.id.four, R.id.five, R.id.six,
92 R.id.seven, R.id.eight, R.id.nine,
93 R.id.star, R.id.zero, R.id.pound };
94
95 // Debug constants.
96 private static final boolean DBG = false;
97 private static final String LOG_TAG = "EmergencyDialer";
98
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099 private StatusBarManager mStatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100
101 /** The length of DTMF tones in milliseconds */
102 private static final int TONE_LENGTH_MS = 150;
103
104 /** The DTMF tone volume relative to other sounds in the stream */
105 private static final int TONE_RELATIVE_VOLUME = 80;
106
107 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
108 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
109
110 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
111
Lucas Dupineb9c5702017-05-10 16:57:09 -0700112 /** 90% opacity, different from other gradients **/
113 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
114
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 EditText mDigits;
116 private View mDialButton;
117 private View mDelete;
118
119 private ToneGenerator mToneGenerator;
120 private Object mToneGeneratorLock = new Object();
121
122 // determines if we want to playback local DTMF tones.
123 private boolean mDTMFToneEnabled;
124
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700125 private EmergencyActionGroup mEmergencyActionGroup;
126
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 // close activity when screen turns off
128 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
129 @Override
130 public void onReceive(Context context, Intent intent) {
131 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700132 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133 }
134 }
135 };
136
137 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
138
Lucas Dupineb9c5702017-05-10 16:57:09 -0700139 // Background gradient
140 private ColorExtractor mColorExtractor;
141 private GradientDrawable mBackgroundGradient;
142 private boolean mSupportsDarkText;
143
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144 @Override
145 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
146 // Do nothing
147 }
148
149 @Override
150 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
151 // Do nothing
152 }
153
154 @Override
155 public void afterTextChanged(Editable input) {
156 // Check for special sequences, in particular the "**04" or "**05"
157 // sequences that allow you to enter PIN or PUK-related codes.
158 //
159 // But note we *don't* allow most other special sequences here,
160 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
161 // since those shouldn't be available if the device is locked.
162 //
163 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
164 // here, not the regular handleChars() method.
165 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
166 // A special sequence was entered, clear the digits
167 mDigits.getText().clear();
168 }
169
170 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800171 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 }
173
174 @Override
175 protected void onCreate(Bundle icicle) {
176 super.onCreate(icicle);
177
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 mStatusBarManager = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179
180 // Allow this activity to be displayed in front of the keyguard / lockscreen.
181 WindowManager.LayoutParams lp = getWindow().getAttributes();
182 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700183
184 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700185 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700186 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 getWindow().setAttributes(lp);
189
Lucas Dupineb9c5702017-05-10 16:57:09 -0700190 mColorExtractor = new ColorExtractor(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700191 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
192 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700193 updateTheme(lockScreenColors.supportsDarkText());
194
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 setContentView(R.layout.emergency_dialer);
196
197 mDigits = (EditText) findViewById(R.id.digits);
198 mDigits.setKeyListener(DialerKeyListener.getInstance());
199 mDigits.setOnClickListener(this);
200 mDigits.setOnKeyListener(this);
201 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700202 mDigits.setInputType(InputType.TYPE_NULL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 maybeAddNumberFormatting();
204
Lucas Dupineb9c5702017-05-10 16:57:09 -0700205 mBackgroundGradient = new GradientDrawable(this);
206 Point displaySize = new Point();
207 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
208 .getDefaultDisplay().getSize(displaySize);
209 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
210 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
211 getWindow().setBackgroundDrawable(mBackgroundGradient);
212
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 // Check for the presence of the keypad
214 View view = findViewById(R.id.one);
215 if (view != null) {
216 setupKeypad();
217 }
218
219 mDelete = findViewById(R.id.deleteButton);
220 mDelete.setOnClickListener(this);
221 mDelete.setOnLongClickListener(this);
222
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700223 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224
225 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700226 // Read carrier config through the public API because PhoneGlobals is not available when we
227 // run as a secondary user.
228 CarrierConfigManager configMgr =
229 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
230 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800231 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700232 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 mDialButton.setOnClickListener(this);
234 } else {
235 mDialButton.setVisibility(View.GONE);
236 }
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700237 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238
239 if (icicle != null) {
240 super.onRestoreInstanceState(icicle);
241 }
242
243 // Extract phone number from intent
244 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700245 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
247 if (number != null) {
248 mDigits.setText(number);
249 }
250 }
251
252 // if the mToneGenerator creation fails, just continue without it. It is
253 // a local audio signal, and is not as important as the dtmf tone itself.
254 synchronized (mToneGeneratorLock) {
255 if (mToneGenerator == null) {
256 try {
257 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
258 } catch (RuntimeException e) {
259 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
260 mToneGenerator = null;
261 }
262 }
263 }
264
265 final IntentFilter intentFilter = new IntentFilter();
266 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
267 registerReceiver(mBroadcastReceiver, intentFilter);
268
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700269 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270 }
271
272 @Override
273 protected void onDestroy() {
274 super.onDestroy();
275 synchronized (mToneGeneratorLock) {
276 if (mToneGenerator != null) {
277 mToneGenerator.release();
278 mToneGenerator = null;
279 }
280 }
281 unregisterReceiver(mBroadcastReceiver);
282 }
283
284 @Override
285 protected void onRestoreInstanceState(Bundle icicle) {
286 mLastNumber = icicle.getString(LAST_NUMBER);
287 }
288
289 @Override
290 protected void onSaveInstanceState(Bundle outState) {
291 super.onSaveInstanceState(outState);
292 outState.putString(LAST_NUMBER, mLastNumber);
293 }
294
295 /**
296 * Explicitly turn off number formatting, since it gets in the way of the emergency
297 * number detector
298 */
299 protected void maybeAddNumberFormatting() {
300 // Do nothing.
301 }
302
303 @Override
304 protected void onPostCreate(Bundle savedInstanceState) {
305 super.onPostCreate(savedInstanceState);
306
307 // This can't be done in onCreate(), since the auto-restoring of the digits
308 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
309 // is called. This method will be called every time the activity is created, and
310 // will always happen after onRestoreSavedInstanceState().
311 mDigits.addTextChangedListener(this);
312 }
313
314 private void setupKeypad() {
315 // Setup the listeners for the buttons
316 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700317 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
318 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 }
320
321 View view = findViewById(R.id.zero);
322 view.setOnLongClickListener(this);
323 }
324
325 /**
326 * handle key events
327 */
328 @Override
329 public boolean onKeyDown(int keyCode, KeyEvent event) {
330 switch (keyCode) {
331 // Happen when there's a "Call" hard button.
332 case KeyEvent.KEYCODE_CALL: {
333 if (TextUtils.isEmpty(mDigits.getText().toString())) {
334 // if we are adding a call from the InCallScreen and the phone
335 // number entered is empty, we just close the dialer to expose
336 // the InCallScreen under it.
337 finish();
338 } else {
339 // otherwise, we place the call.
340 placeCall();
341 }
342 return true;
343 }
344 }
345 return super.onKeyDown(keyCode, event);
346 }
347
348 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700349 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
351 mDigits.onKeyDown(keyCode, event);
352 }
353
354 @Override
355 public boolean onKey(View view, int keyCode, KeyEvent event) {
356 switch (view.getId()) {
357 case R.id.digits:
358 // Happen when "Done" button of the IME is pressed. This can happen when this
359 // Activity is forced into landscape mode due to a desk dock.
360 if (keyCode == KeyEvent.KEYCODE_ENTER
361 && event.getAction() == KeyEvent.ACTION_UP) {
362 placeCall();
363 return true;
364 }
365 break;
366 }
367 return false;
368 }
369
370 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700371 public boolean dispatchTouchEvent(MotionEvent ev) {
372 mEmergencyActionGroup.onPreTouchEvent(ev);
373 boolean handled = super.dispatchTouchEvent(ev);
374 mEmergencyActionGroup.onPostTouchEvent(ev);
375 return handled;
376 }
377
378 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700379 public void onClick(View view) {
380 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700381 case R.id.deleteButton: {
382 keyPressed(KeyEvent.KEYCODE_DEL);
383 return;
384 }
385 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700386 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700387 placeCall();
388 return;
389 }
390 case R.id.digits: {
391 if (mDigits.length() != 0) {
392 mDigits.setCursorVisible(true);
393 }
394 return;
395 }
396 }
397 }
398
399 @Override
400 public void onPressed(View view, boolean pressed) {
401 if (!pressed) {
402 return;
403 }
404 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405 case R.id.one: {
406 playTone(ToneGenerator.TONE_DTMF_1);
407 keyPressed(KeyEvent.KEYCODE_1);
408 return;
409 }
410 case R.id.two: {
411 playTone(ToneGenerator.TONE_DTMF_2);
412 keyPressed(KeyEvent.KEYCODE_2);
413 return;
414 }
415 case R.id.three: {
416 playTone(ToneGenerator.TONE_DTMF_3);
417 keyPressed(KeyEvent.KEYCODE_3);
418 return;
419 }
420 case R.id.four: {
421 playTone(ToneGenerator.TONE_DTMF_4);
422 keyPressed(KeyEvent.KEYCODE_4);
423 return;
424 }
425 case R.id.five: {
426 playTone(ToneGenerator.TONE_DTMF_5);
427 keyPressed(KeyEvent.KEYCODE_5);
428 return;
429 }
430 case R.id.six: {
431 playTone(ToneGenerator.TONE_DTMF_6);
432 keyPressed(KeyEvent.KEYCODE_6);
433 return;
434 }
435 case R.id.seven: {
436 playTone(ToneGenerator.TONE_DTMF_7);
437 keyPressed(KeyEvent.KEYCODE_7);
438 return;
439 }
440 case R.id.eight: {
441 playTone(ToneGenerator.TONE_DTMF_8);
442 keyPressed(KeyEvent.KEYCODE_8);
443 return;
444 }
445 case R.id.nine: {
446 playTone(ToneGenerator.TONE_DTMF_9);
447 keyPressed(KeyEvent.KEYCODE_9);
448 return;
449 }
450 case R.id.zero: {
451 playTone(ToneGenerator.TONE_DTMF_0);
452 keyPressed(KeyEvent.KEYCODE_0);
453 return;
454 }
455 case R.id.pound: {
456 playTone(ToneGenerator.TONE_DTMF_P);
457 keyPressed(KeyEvent.KEYCODE_POUND);
458 return;
459 }
460 case R.id.star: {
461 playTone(ToneGenerator.TONE_DTMF_S);
462 keyPressed(KeyEvent.KEYCODE_STAR);
463 return;
464 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700465 }
466 }
467
468 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 * called for long touch events
470 */
471 @Override
472 public boolean onLongClick(View view) {
473 int id = view.getId();
474 switch (id) {
475 case R.id.deleteButton: {
476 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700477 return true;
478 }
479 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700480 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700481 keyPressed(KeyEvent.KEYCODE_PLUS);
482 return true;
483 }
484 }
485 return false;
486 }
487
488 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700489 protected void onStart() {
490 super.onStart();
491
492 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700493 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
494 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700495 // Do not animate when view isn't visible yet, just set an initial state.
496 mBackgroundGradient.setColors(lockScreenColors, false);
497 updateTheme(lockScreenColors.supportsDarkText());
498 }
499
500 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501 protected void onResume() {
502 super.onResume();
503
504 // retrieve the DTMF tone play back setting.
505 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
506 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
507
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508 // if the mToneGenerator creation fails, just continue without it. It is
509 // a local audio signal, and is not as important as the dtmf tone itself.
510 synchronized (mToneGeneratorLock) {
511 if (mToneGenerator == null) {
512 try {
513 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
514 TONE_RELATIVE_VOLUME);
515 } catch (RuntimeException e) {
516 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
517 mToneGenerator = null;
518 }
519 }
520 }
521
522 // Disable the status bar and set the poke lock timeout to medium.
523 // There is no need to do anything with the wake lock.
524 if (DBG) Log.d(LOG_TAG, "disabling status bar, set to long timeout");
525 mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
526
527 updateDialAndDeleteButtonStateEnabledAttr();
528 }
529
530 @Override
531 public void onPause() {
532 // Reenable the status bar and set the poke lock timeout to default.
533 // There is no need to do anything with the wake lock.
534 if (DBG) Log.d(LOG_TAG, "reenabling status bar and closing the dialer");
535 mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
536
537 super.onPause();
538
539 synchronized (mToneGeneratorLock) {
540 if (mToneGenerator != null) {
541 mToneGenerator.release();
542 mToneGenerator = null;
543 }
544 }
545 }
546
Lucas Dupineb9c5702017-05-10 16:57:09 -0700547 @Override
548 protected void onStop() {
549 super.onStop();
550
551 mColorExtractor.removeOnColorsChangedListener(this);
552 }
553
554 /**
555 * Sets theme based on gradient colors
556 * @param supportsDarkText true if gradient supports dark text
557 */
558 private void updateTheme(boolean supportsDarkText) {
559 if (mSupportsDarkText == supportsDarkText) {
560 return;
561 }
562 mSupportsDarkText = supportsDarkText;
563
564 // We can't change themes after inflation, in this case we'll have to recreate
565 // the whole activity.
566 if (mBackgroundGradient != null) {
567 recreate();
568 return;
569 }
570
571 int vis = getWindow().getDecorView().getSystemUiVisibility();
572 if (supportsDarkText) {
573 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
574 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
575 setTheme(R.style.EmergencyDialerThemeDark);
576 } else {
577 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
578 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
579 setTheme(R.style.EmergencyDialerTheme);
580 }
581 getWindow().getDecorView().setSystemUiVisibility(vis);
582 }
583
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700584 /**
585 * place the call, but check to make sure it is a viable number.
586 */
587 private void placeCall() {
588 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900589
590 // Convert into emergency number according to emergency conversion map.
591 // If conversion map is not defined (this is default), this method does
592 // nothing and just returns input number.
593 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
594
Yorke Lee36bb2542014-06-05 08:09:52 -0700595 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
597
598 // place the call if it is a valid number
599 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
600 // There is no number entered.
601 playTone(ToneGenerator.TONE_PROP_NACK);
602 return;
603 }
604 Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY);
Jay Shrauner137458b2014-09-05 14:27:25 -0700605 intent.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
607 startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608 } else {
609 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
610
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700611 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
612 }
Yorke Lee9b341512014-10-17 11:36:41 -0700613 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 }
615
616 /**
617 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
618 *
619 * The tone is played locally, using the audio stream for phone calls.
620 * Tones are played only if the "Audible touch tones" user preference
621 * is checked, and are NOT played if the device is in silent mode.
622 *
623 * @param tone a tone code from {@link ToneGenerator}
624 */
625 void playTone(int tone) {
626 // if local tone playback is disabled, just return.
627 if (!mDTMFToneEnabled) {
628 return;
629 }
630
631 // Also do nothing if the phone is in silent mode.
632 // We need to re-check the ringer mode for *every* playTone()
633 // call, rather than keeping a local flag that's updated in
634 // onResume(), since it's possible to toggle silent mode without
635 // leaving the current activity (via the ENDCALL-longpress menu.)
636 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
637 int ringerMode = audioManager.getRingerMode();
638 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
639 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
640 return;
641 }
642
643 synchronized (mToneGeneratorLock) {
644 if (mToneGenerator == null) {
645 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
646 return;
647 }
648
649 // Start the new tone (will stop any playing tone)
650 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
651 }
652 }
653
654 private CharSequence createErrorMessage(String number) {
655 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800656 String errorString = getString(R.string.dial_emergency_error, number);
657 int startingPosition = errorString.indexOf(number);
658 int endingPosition = startingPosition + number.length();
659 Spannable result = new SpannableString(errorString);
660 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
661 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 } else {
663 return getText(R.string.dial_emergency_empty_error).toString();
664 }
665 }
666
667 @Override
668 protected Dialog onCreateDialog(int id) {
669 AlertDialog dialog = null;
670 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
671 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700672 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
674 .setMessage(createErrorMessage(mLastNumber))
675 .setPositiveButton(R.string.ok, null)
676 .setCancelable(true).create();
677
678 // blur stuff behind the dialog
679 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Yorke Leec30f00c2014-07-31 16:09:05 -0700680 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700681 }
682 return dialog;
683 }
684
685 @Override
686 protected void onPrepareDialog(int id, Dialog dialog) {
687 super.onPrepareDialog(id, dialog);
688 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
689 AlertDialog alert = (AlertDialog) dialog;
690 alert.setMessage(createErrorMessage(mLastNumber));
691 }
692 }
693
Andrew Leed5631e82014-10-08 16:03:58 -0700694 @Override
695 public boolean onOptionsItemSelected(MenuItem item) {
696 final int itemId = item.getItemId();
697 if (itemId == android.R.id.home) {
698 onBackPressed();
699 return true;
700 }
701 return super.onOptionsItemSelected(item);
702 }
703
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704 /**
705 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
706 */
707 private void updateDialAndDeleteButtonStateEnabledAttr() {
708 final boolean notEmpty = mDigits.length() != 0;
709
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710 mDelete.setEnabled(notEmpty);
711 }
Yorke Lee91311662014-10-24 14:50:45 -0700712
713 /**
714 * Remove the digit just before the current position. Used by various long pressed callbacks
715 * to remove the digit that was populated as a result of the short click.
716 */
717 private void removePreviousDigitIfPossible() {
718 final int currentPosition = mDigits.getSelectionStart();
719 if (currentPosition > 0) {
720 mDigits.setSelection(currentPosition);
721 mDigits.getText().delete(currentPosition - 1, currentPosition);
722 }
723 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800724
725 /**
726 * Update the text-to-speech annotations in the edit field.
727 */
728 private void updateTtsSpans() {
729 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
730 mDigits.getText().removeSpan(o);
731 }
732 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
733 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700734
735 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700736 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700737 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700738 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
739 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700740 mBackgroundGradient.setColors(colors);
741 updateTheme(colors.supportsDarkText());
742 }
743 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744}