blob: 157cf1d72b87f41486c0982824e69449eccbb096 [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
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.Activity;
22import android.app.AlertDialog;
23import android.app.Dialog;
Lucas Dupineb9c5702017-05-10 16:57:09 -070024import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.content.BroadcastReceiver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
Lucas Dupineb9c5702017-05-10 16:57:09 -070029import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.media.AudioManager;
31import android.media.ToneGenerator;
32import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070033import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070035import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070037import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070038import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070039import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.telephony.PhoneNumberUtils;
Tyler Gunnc5428972018-03-28 14:15:34 -070041import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070042import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070043import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070045import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080046import android.text.Spannable;
47import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.text.TextUtils;
49import android.text.TextWatcher;
50import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080051import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070053import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070054import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070056import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070057import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.view.View;
59import android.view.WindowManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060
Lucas Dupinaf9e9912017-06-22 12:39:39 -070061import com.android.internal.colorextraction.ColorExtractor;
62import com.android.internal.colorextraction.ColorExtractor.GradientColors;
63import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070064import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070065import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070066import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068/**
69 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
70 *
71 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
72 * activity from apps/Contacts) that:
73 * 1. Allows ONLY emergency calls to be dialed
74 * 2. Disallows voicemail functionality
75 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
76 * activity to stay in front of the keyguard.
77 *
78 * TODO: Even though this is an ultra-simplified version of the normal
79 * dialer, there's still lots of code duplication between this class and
80 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
81 * moved into a shared base class that would live in the framework?
82 * Or could we figure out some way to move *this* class into apps/Contacts
83 * also?
84 */
85public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070086 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Lucas Dupineb9c5702017-05-10 16:57:09 -070087 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088 // Keys used with onSaveInstanceState().
89 private static final String LAST_NUMBER = "lastNumber";
90
91 // Intent action for this activity.
92 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
93
94 // List of dialer button IDs.
95 private static final int[] DIALER_KEYS = new int[] {
96 R.id.one, R.id.two, R.id.three,
97 R.id.four, R.id.five, R.id.six,
98 R.id.seven, R.id.eight, R.id.nine,
99 R.id.star, R.id.zero, R.id.pound };
100
101 // Debug constants.
102 private static final boolean DBG = false;
103 private static final String LOG_TAG = "EmergencyDialer";
104
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105 /** The length of DTMF tones in milliseconds */
106 private static final int TONE_LENGTH_MS = 150;
107
108 /** The DTMF tone volume relative to other sounds in the stream */
109 private static final int TONE_RELATIVE_VOLUME = 80;
110
111 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
112 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
113
114 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
115
Lucas Dupineb9c5702017-05-10 16:57:09 -0700116 /** 90% opacity, different from other gradients **/
117 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
118
Tyler Gunnc5428972018-03-28 14:15:34 -0700119 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 private View mDialButton;
121 private View mDelete;
122
123 private ToneGenerator mToneGenerator;
124 private Object mToneGeneratorLock = new Object();
125
126 // determines if we want to playback local DTMF tones.
127 private boolean mDTMFToneEnabled;
128
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700129 private EmergencyActionGroup mEmergencyActionGroup;
130
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131 // close activity when screen turns off
132 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
133 @Override
134 public void onReceive(Context context, Intent intent) {
135 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700136 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137 }
138 }
139 };
140
141 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
142
Lucas Dupineb9c5702017-05-10 16:57:09 -0700143 // Background gradient
144 private ColorExtractor mColorExtractor;
145 private GradientDrawable mBackgroundGradient;
146 private boolean mSupportsDarkText;
147
Tyler Gunnc5428972018-03-28 14:15:34 -0700148 private boolean mIsWfcEmergencyCallingWarningEnabled;
149 private float mDefaultDigitsTextSize;
150
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 @Override
152 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
153 // Do nothing
154 }
155
156 @Override
157 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700158 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 }
160
161 @Override
162 public void afterTextChanged(Editable input) {
163 // Check for special sequences, in particular the "**04" or "**05"
164 // sequences that allow you to enter PIN or PUK-related codes.
165 //
166 // But note we *don't* allow most other special sequences here,
167 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
168 // since those shouldn't be available if the device is locked.
169 //
170 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
171 // here, not the regular handleChars() method.
172 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
173 // A special sequence was entered, clear the digits
174 mDigits.getText().clear();
175 }
176
177 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800178 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179 }
180
181 @Override
182 protected void onCreate(Bundle icicle) {
183 super.onCreate(icicle);
184
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800186 setShowWhenLocked(true);
187 // Allow turning screen on
188 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189
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
Tyler Gunnc5428972018-03-28 14:15:34 -0700197 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 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);
Tyler Gunnc5428972018-03-28 14:15:34 -0700203 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
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());
Tyler Gunnc5428972018-03-28 14:15:34 -0700233
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700234 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235 mDialButton.setOnClickListener(this);
236 } else {
237 mDialButton.setVisibility(View.GONE);
238 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700239 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
240 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
241 maybeShowWfcEmergencyCallingWarning();
242
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700243 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244
245 if (icicle != null) {
246 super.onRestoreInstanceState(icicle);
247 }
248
249 // Extract phone number from intent
250 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700251 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
253 if (number != null) {
254 mDigits.setText(number);
255 }
256 }
257
258 // if the mToneGenerator creation fails, just continue without it. It is
259 // a local audio signal, and is not as important as the dtmf tone itself.
260 synchronized (mToneGeneratorLock) {
261 if (mToneGenerator == null) {
262 try {
263 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
264 } catch (RuntimeException e) {
265 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
266 mToneGenerator = null;
267 }
268 }
269 }
270
271 final IntentFilter intentFilter = new IntentFilter();
272 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
273 registerReceiver(mBroadcastReceiver, intentFilter);
274
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700275 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 }
277
278 @Override
279 protected void onDestroy() {
280 super.onDestroy();
281 synchronized (mToneGeneratorLock) {
282 if (mToneGenerator != null) {
283 mToneGenerator.release();
284 mToneGenerator = null;
285 }
286 }
287 unregisterReceiver(mBroadcastReceiver);
288 }
289
290 @Override
291 protected void onRestoreInstanceState(Bundle icicle) {
292 mLastNumber = icicle.getString(LAST_NUMBER);
293 }
294
295 @Override
296 protected void onSaveInstanceState(Bundle outState) {
297 super.onSaveInstanceState(outState);
298 outState.putString(LAST_NUMBER, mLastNumber);
299 }
300
301 /**
302 * Explicitly turn off number formatting, since it gets in the way of the emergency
303 * number detector
304 */
305 protected void maybeAddNumberFormatting() {
306 // Do nothing.
307 }
308
309 @Override
310 protected void onPostCreate(Bundle savedInstanceState) {
311 super.onPostCreate(savedInstanceState);
312
313 // This can't be done in onCreate(), since the auto-restoring of the digits
314 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
315 // is called. This method will be called every time the activity is created, and
316 // will always happen after onRestoreSavedInstanceState().
317 mDigits.addTextChangedListener(this);
318 }
319
320 private void setupKeypad() {
321 // Setup the listeners for the buttons
322 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700323 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
324 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 }
326
327 View view = findViewById(R.id.zero);
328 view.setOnLongClickListener(this);
329 }
330
331 /**
332 * handle key events
333 */
334 @Override
335 public boolean onKeyDown(int keyCode, KeyEvent event) {
336 switch (keyCode) {
337 // Happen when there's a "Call" hard button.
338 case KeyEvent.KEYCODE_CALL: {
339 if (TextUtils.isEmpty(mDigits.getText().toString())) {
340 // if we are adding a call from the InCallScreen and the phone
341 // number entered is empty, we just close the dialer to expose
342 // the InCallScreen under it.
343 finish();
344 } else {
345 // otherwise, we place the call.
346 placeCall();
347 }
348 return true;
349 }
350 }
351 return super.onKeyDown(keyCode, event);
352 }
353
354 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700355 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
357 mDigits.onKeyDown(keyCode, event);
358 }
359
360 @Override
361 public boolean onKey(View view, int keyCode, KeyEvent event) {
362 switch (view.getId()) {
363 case R.id.digits:
364 // Happen when "Done" button of the IME is pressed. This can happen when this
365 // Activity is forced into landscape mode due to a desk dock.
366 if (keyCode == KeyEvent.KEYCODE_ENTER
367 && event.getAction() == KeyEvent.ACTION_UP) {
368 placeCall();
369 return true;
370 }
371 break;
372 }
373 return false;
374 }
375
376 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700377 public boolean dispatchTouchEvent(MotionEvent ev) {
378 mEmergencyActionGroup.onPreTouchEvent(ev);
379 boolean handled = super.dispatchTouchEvent(ev);
380 mEmergencyActionGroup.onPostTouchEvent(ev);
381 return handled;
382 }
383
384 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700385 public void onClick(View view) {
386 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700387 case R.id.deleteButton: {
388 keyPressed(KeyEvent.KEYCODE_DEL);
389 return;
390 }
391 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700392 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700393 placeCall();
394 return;
395 }
396 case R.id.digits: {
397 if (mDigits.length() != 0) {
398 mDigits.setCursorVisible(true);
399 }
400 return;
401 }
402 }
403 }
404
405 @Override
406 public void onPressed(View view, boolean pressed) {
407 if (!pressed) {
408 return;
409 }
410 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411 case R.id.one: {
412 playTone(ToneGenerator.TONE_DTMF_1);
413 keyPressed(KeyEvent.KEYCODE_1);
414 return;
415 }
416 case R.id.two: {
417 playTone(ToneGenerator.TONE_DTMF_2);
418 keyPressed(KeyEvent.KEYCODE_2);
419 return;
420 }
421 case R.id.three: {
422 playTone(ToneGenerator.TONE_DTMF_3);
423 keyPressed(KeyEvent.KEYCODE_3);
424 return;
425 }
426 case R.id.four: {
427 playTone(ToneGenerator.TONE_DTMF_4);
428 keyPressed(KeyEvent.KEYCODE_4);
429 return;
430 }
431 case R.id.five: {
432 playTone(ToneGenerator.TONE_DTMF_5);
433 keyPressed(KeyEvent.KEYCODE_5);
434 return;
435 }
436 case R.id.six: {
437 playTone(ToneGenerator.TONE_DTMF_6);
438 keyPressed(KeyEvent.KEYCODE_6);
439 return;
440 }
441 case R.id.seven: {
442 playTone(ToneGenerator.TONE_DTMF_7);
443 keyPressed(KeyEvent.KEYCODE_7);
444 return;
445 }
446 case R.id.eight: {
447 playTone(ToneGenerator.TONE_DTMF_8);
448 keyPressed(KeyEvent.KEYCODE_8);
449 return;
450 }
451 case R.id.nine: {
452 playTone(ToneGenerator.TONE_DTMF_9);
453 keyPressed(KeyEvent.KEYCODE_9);
454 return;
455 }
456 case R.id.zero: {
457 playTone(ToneGenerator.TONE_DTMF_0);
458 keyPressed(KeyEvent.KEYCODE_0);
459 return;
460 }
461 case R.id.pound: {
462 playTone(ToneGenerator.TONE_DTMF_P);
463 keyPressed(KeyEvent.KEYCODE_POUND);
464 return;
465 }
466 case R.id.star: {
467 playTone(ToneGenerator.TONE_DTMF_S);
468 keyPressed(KeyEvent.KEYCODE_STAR);
469 return;
470 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700471 }
472 }
473
474 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700475 * called for long touch events
476 */
477 @Override
478 public boolean onLongClick(View view) {
479 int id = view.getId();
480 switch (id) {
481 case R.id.deleteButton: {
482 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700483 return true;
484 }
485 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700486 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700487 keyPressed(KeyEvent.KEYCODE_PLUS);
488 return true;
489 }
490 }
491 return false;
492 }
493
494 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700495 protected void onStart() {
496 super.onStart();
497
498 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700499 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
500 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700501 // Do not animate when view isn't visible yet, just set an initial state.
502 mBackgroundGradient.setColors(lockScreenColors, false);
503 updateTheme(lockScreenColors.supportsDarkText());
504 }
505
506 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700507 protected void onResume() {
508 super.onResume();
509
510 // retrieve the DTMF tone play back setting.
511 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
512 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
513
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700514 // if the mToneGenerator creation fails, just continue without it. It is
515 // a local audio signal, and is not as important as the dtmf tone itself.
516 synchronized (mToneGeneratorLock) {
517 if (mToneGenerator == null) {
518 try {
519 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
520 TONE_RELATIVE_VOLUME);
521 } catch (RuntimeException e) {
522 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
523 mToneGenerator = null;
524 }
525 }
526 }
527
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700528 updateDialAndDeleteButtonStateEnabledAttr();
529 }
530
531 @Override
532 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700534 }
535
Lucas Dupineb9c5702017-05-10 16:57:09 -0700536 @Override
537 protected void onStop() {
538 super.onStop();
539
540 mColorExtractor.removeOnColorsChangedListener(this);
541 }
542
543 /**
544 * Sets theme based on gradient colors
545 * @param supportsDarkText true if gradient supports dark text
546 */
547 private void updateTheme(boolean supportsDarkText) {
548 if (mSupportsDarkText == supportsDarkText) {
549 return;
550 }
551 mSupportsDarkText = supportsDarkText;
552
553 // We can't change themes after inflation, in this case we'll have to recreate
554 // the whole activity.
555 if (mBackgroundGradient != null) {
556 recreate();
557 return;
558 }
559
560 int vis = getWindow().getDecorView().getSystemUiVisibility();
561 if (supportsDarkText) {
562 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
563 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
564 setTheme(R.style.EmergencyDialerThemeDark);
565 } else {
566 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
567 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
568 setTheme(R.style.EmergencyDialerTheme);
569 }
570 getWindow().getDecorView().setSystemUiVisibility(vis);
571 }
572
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 /**
574 * place the call, but check to make sure it is a viable number.
575 */
576 private void placeCall() {
577 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900578
579 // Convert into emergency number according to emergency conversion map.
580 // If conversion map is not defined (this is default), this method does
581 // nothing and just returns input number.
582 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
583
Yorke Lee36bb2542014-06-05 08:09:52 -0700584 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700585 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
586
587 // place the call if it is a valid number
588 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
589 // There is no number entered.
590 playTone(ToneGenerator.TONE_PROP_NACK);
591 return;
592 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700593 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
594 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700595 } else {
596 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
597
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700598 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
599 }
Yorke Lee9b341512014-10-17 11:36:41 -0700600 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700601 }
602
603 /**
604 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
605 *
606 * The tone is played locally, using the audio stream for phone calls.
607 * Tones are played only if the "Audible touch tones" user preference
608 * is checked, and are NOT played if the device is in silent mode.
609 *
610 * @param tone a tone code from {@link ToneGenerator}
611 */
612 void playTone(int tone) {
613 // if local tone playback is disabled, just return.
614 if (!mDTMFToneEnabled) {
615 return;
616 }
617
618 // Also do nothing if the phone is in silent mode.
619 // We need to re-check the ringer mode for *every* playTone()
620 // call, rather than keeping a local flag that's updated in
621 // onResume(), since it's possible to toggle silent mode without
622 // leaving the current activity (via the ENDCALL-longpress menu.)
623 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
624 int ringerMode = audioManager.getRingerMode();
625 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
626 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
627 return;
628 }
629
630 synchronized (mToneGeneratorLock) {
631 if (mToneGenerator == null) {
632 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
633 return;
634 }
635
636 // Start the new tone (will stop any playing tone)
637 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
638 }
639 }
640
641 private CharSequence createErrorMessage(String number) {
642 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800643 String errorString = getString(R.string.dial_emergency_error, number);
644 int startingPosition = errorString.indexOf(number);
645 int endingPosition = startingPosition + number.length();
646 Spannable result = new SpannableString(errorString);
647 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
648 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700649 } else {
650 return getText(R.string.dial_emergency_empty_error).toString();
651 }
652 }
653
654 @Override
655 protected Dialog onCreateDialog(int id) {
656 AlertDialog dialog = null;
657 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
658 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700659 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700660 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
661 .setMessage(createErrorMessage(mLastNumber))
662 .setPositiveButton(R.string.ok, null)
663 .setCancelable(true).create();
664
665 // blur stuff behind the dialog
666 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700667 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 }
669 return dialog;
670 }
671
672 @Override
673 protected void onPrepareDialog(int id, Dialog dialog) {
674 super.onPrepareDialog(id, dialog);
675 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
676 AlertDialog alert = (AlertDialog) dialog;
677 alert.setMessage(createErrorMessage(mLastNumber));
678 }
679 }
680
Andrew Leed5631e82014-10-08 16:03:58 -0700681 @Override
682 public boolean onOptionsItemSelected(MenuItem item) {
683 final int itemId = item.getItemId();
684 if (itemId == android.R.id.home) {
685 onBackPressed();
686 return true;
687 }
688 return super.onOptionsItemSelected(item);
689 }
690
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 /**
692 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
693 */
694 private void updateDialAndDeleteButtonStateEnabledAttr() {
695 final boolean notEmpty = mDigits.length() != 0;
696
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700697 mDelete.setEnabled(notEmpty);
698 }
Yorke Lee91311662014-10-24 14:50:45 -0700699
700 /**
701 * Remove the digit just before the current position. Used by various long pressed callbacks
702 * to remove the digit that was populated as a result of the short click.
703 */
704 private void removePreviousDigitIfPossible() {
705 final int currentPosition = mDigits.getSelectionStart();
706 if (currentPosition > 0) {
707 mDigits.setSelection(currentPosition);
708 mDigits.getText().delete(currentPosition - 1, currentPosition);
709 }
710 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800711
712 /**
713 * Update the text-to-speech annotations in the edit field.
714 */
715 private void updateTtsSpans() {
716 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
717 mDigits.getText().removeSpan(o);
718 }
719 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
720 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700721
722 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700723 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700724 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700725 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
726 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700727 mBackgroundGradient.setColors(colors);
728 updateTheme(colors.supportsDarkText());
729 }
730 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700731
732 /**
733 * Where a carrier requires a warning that emergency calling is not available while on WFC,
734 * add hint text above the dial pad which warns the user of this case.
735 */
736 private void maybeShowWfcEmergencyCallingWarning() {
737 if (!mIsWfcEmergencyCallingWarningEnabled) {
738 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
739 return;
740 }
741
742 // Use an async task rather than calling into Telephony on UI thread.
743 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
744 @Override
745 protected Boolean doInBackground(Void... voids) {
746 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
747 boolean isWfcAvailable = tm.isWifiCallingAvailable();
748 ServiceState ss = tm.getServiceState();
749 boolean isCellAvailable =
750 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
751 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
752 + " isCellAvailable=" + isCellAvailable
753 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
754 return isWfcAvailable && !isCellAvailable;
755 }
756
757 @Override
758 protected void onPostExecute(Boolean result) {
759 if (result.booleanValue()) {
760 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
761 mDigits.setHint(R.string.dial_emergency_calling_not_available);
762 } else {
763 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
764 mDigits.setHint("");
765 }
766 maybeChangeHintSize();
767 }
768 };
769 showWfcWarningTask.execute((Void) null);
770 }
771
772 /**
773 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
774 * edit view and set the font size to a smaller size appropriate for the emergency calling
775 * warning.
776 */
777 private void maybeChangeHintSize() {
778 if (TextUtils.isEmpty(mDigits.getHint())
779 || !TextUtils.isEmpty(mDigits.getText().toString())) {
780 // No hint or there are dialed digits, so use default size.
781 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
782 // By default, the digits view auto-resizes to fit the text it contains, so
783 // enable that now.
784 mDigits.setResizeEnabled(true);
785 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
786 } else {
787 // Hint present and no dialed digits, set custom font size appropriate for the warning.
788 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
789 R.dimen.emergency_call_warning_size));
790 // Since we're populating this with a static text string, disable auto-resize.
791 mDigits.setResizeEnabled(false);
792 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
793 }
794 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795}