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