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