blob: 31e1958e744b746bca0611a056274207079bcda9 [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.
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);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700196 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
197 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700198 updateTheme(lockScreenColors.supportsDarkText());
199
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 setContentView(R.layout.emergency_dialer);
201
Tyler Gunnc5428972018-03-28 14:15:34 -0700202 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 mDigits.setKeyListener(DialerKeyListener.getInstance());
204 mDigits.setOnClickListener(this);
205 mDigits.setOnKeyListener(this);
206 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700207 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700208 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 maybeAddNumberFormatting();
210
Lucas Dupineb9c5702017-05-10 16:57:09 -0700211 mBackgroundGradient = new GradientDrawable(this);
212 Point displaySize = new Point();
213 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
214 .getDefaultDisplay().getSize(displaySize);
215 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
216 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
217 getWindow().setBackgroundDrawable(mBackgroundGradient);
218
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700219 // Check for the presence of the keypad
220 View view = findViewById(R.id.one);
221 if (view != null) {
222 setupKeypad();
223 }
224
225 mDelete = findViewById(R.id.deleteButton);
226 mDelete.setOnClickListener(this);
227 mDelete.setOnLongClickListener(this);
228
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700229 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230
231 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700232 // Read carrier config through the public API because PhoneGlobals is not available when we
233 // run as a secondary user.
234 CarrierConfigManager configMgr =
235 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
236 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800237 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700238
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700239 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 mDialButton.setOnClickListener(this);
241 } else {
242 mDialButton.setVisibility(View.GONE);
243 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700244 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
245 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
246 maybeShowWfcEmergencyCallingWarning();
247
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700248 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249
250 if (icicle != null) {
251 super.onRestoreInstanceState(icicle);
252 }
253
254 // Extract phone number from intent
255 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700256 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
258 if (number != null) {
259 mDigits.setText(number);
260 }
261 }
262
263 // if the mToneGenerator creation fails, just continue without it. It is
264 // a local audio signal, and is not as important as the dtmf tone itself.
265 synchronized (mToneGeneratorLock) {
266 if (mToneGenerator == null) {
267 try {
268 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
269 } catch (RuntimeException e) {
270 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
271 mToneGenerator = null;
272 }
273 }
274 }
275
276 final IntentFilter intentFilter = new IntentFilter();
277 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
278 registerReceiver(mBroadcastReceiver, intentFilter);
279
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700280 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 }
282
283 @Override
284 protected void onDestroy() {
285 super.onDestroy();
286 synchronized (mToneGeneratorLock) {
287 if (mToneGenerator != null) {
288 mToneGenerator.release();
289 mToneGenerator = null;
290 }
291 }
292 unregisterReceiver(mBroadcastReceiver);
293 }
294
295 @Override
296 protected void onRestoreInstanceState(Bundle icicle) {
297 mLastNumber = icicle.getString(LAST_NUMBER);
298 }
299
300 @Override
301 protected void onSaveInstanceState(Bundle outState) {
302 super.onSaveInstanceState(outState);
303 outState.putString(LAST_NUMBER, mLastNumber);
304 }
305
306 /**
307 * Explicitly turn off number formatting, since it gets in the way of the emergency
308 * number detector
309 */
310 protected void maybeAddNumberFormatting() {
311 // Do nothing.
312 }
313
314 @Override
315 protected void onPostCreate(Bundle savedInstanceState) {
316 super.onPostCreate(savedInstanceState);
317
318 // This can't be done in onCreate(), since the auto-restoring of the digits
319 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
320 // is called. This method will be called every time the activity is created, and
321 // will always happen after onRestoreSavedInstanceState().
322 mDigits.addTextChangedListener(this);
323 }
324
325 private void setupKeypad() {
326 // Setup the listeners for the buttons
327 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700328 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
329 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 }
331
332 View view = findViewById(R.id.zero);
333 view.setOnLongClickListener(this);
334 }
335
336 /**
337 * handle key events
338 */
339 @Override
340 public boolean onKeyDown(int keyCode, KeyEvent event) {
341 switch (keyCode) {
342 // Happen when there's a "Call" hard button.
343 case KeyEvent.KEYCODE_CALL: {
344 if (TextUtils.isEmpty(mDigits.getText().toString())) {
345 // if we are adding a call from the InCallScreen and the phone
346 // number entered is empty, we just close the dialer to expose
347 // the InCallScreen under it.
348 finish();
349 } else {
350 // otherwise, we place the call.
351 placeCall();
352 }
353 return true;
354 }
355 }
356 return super.onKeyDown(keyCode, event);
357 }
358
359 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700360 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
362 mDigits.onKeyDown(keyCode, event);
363 }
364
365 @Override
366 public boolean onKey(View view, int keyCode, KeyEvent event) {
367 switch (view.getId()) {
368 case R.id.digits:
369 // Happen when "Done" button of the IME is pressed. This can happen when this
370 // Activity is forced into landscape mode due to a desk dock.
371 if (keyCode == KeyEvent.KEYCODE_ENTER
372 && event.getAction() == KeyEvent.ACTION_UP) {
373 placeCall();
374 return true;
375 }
376 break;
377 }
378 return false;
379 }
380
381 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700382 public boolean dispatchTouchEvent(MotionEvent ev) {
383 mEmergencyActionGroup.onPreTouchEvent(ev);
384 boolean handled = super.dispatchTouchEvent(ev);
385 mEmergencyActionGroup.onPostTouchEvent(ev);
386 return handled;
387 }
388
389 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700390 public void onClick(View view) {
391 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700392 case R.id.deleteButton: {
393 keyPressed(KeyEvent.KEYCODE_DEL);
394 return;
395 }
396 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700397 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700398 placeCall();
399 return;
400 }
401 case R.id.digits: {
402 if (mDigits.length() != 0) {
403 mDigits.setCursorVisible(true);
404 }
405 return;
406 }
407 }
408 }
409
410 @Override
411 public void onPressed(View view, boolean pressed) {
412 if (!pressed) {
413 return;
414 }
415 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 case R.id.one: {
417 playTone(ToneGenerator.TONE_DTMF_1);
418 keyPressed(KeyEvent.KEYCODE_1);
419 return;
420 }
421 case R.id.two: {
422 playTone(ToneGenerator.TONE_DTMF_2);
423 keyPressed(KeyEvent.KEYCODE_2);
424 return;
425 }
426 case R.id.three: {
427 playTone(ToneGenerator.TONE_DTMF_3);
428 keyPressed(KeyEvent.KEYCODE_3);
429 return;
430 }
431 case R.id.four: {
432 playTone(ToneGenerator.TONE_DTMF_4);
433 keyPressed(KeyEvent.KEYCODE_4);
434 return;
435 }
436 case R.id.five: {
437 playTone(ToneGenerator.TONE_DTMF_5);
438 keyPressed(KeyEvent.KEYCODE_5);
439 return;
440 }
441 case R.id.six: {
442 playTone(ToneGenerator.TONE_DTMF_6);
443 keyPressed(KeyEvent.KEYCODE_6);
444 return;
445 }
446 case R.id.seven: {
447 playTone(ToneGenerator.TONE_DTMF_7);
448 keyPressed(KeyEvent.KEYCODE_7);
449 return;
450 }
451 case R.id.eight: {
452 playTone(ToneGenerator.TONE_DTMF_8);
453 keyPressed(KeyEvent.KEYCODE_8);
454 return;
455 }
456 case R.id.nine: {
457 playTone(ToneGenerator.TONE_DTMF_9);
458 keyPressed(KeyEvent.KEYCODE_9);
459 return;
460 }
461 case R.id.zero: {
462 playTone(ToneGenerator.TONE_DTMF_0);
463 keyPressed(KeyEvent.KEYCODE_0);
464 return;
465 }
466 case R.id.pound: {
467 playTone(ToneGenerator.TONE_DTMF_P);
468 keyPressed(KeyEvent.KEYCODE_POUND);
469 return;
470 }
471 case R.id.star: {
472 playTone(ToneGenerator.TONE_DTMF_S);
473 keyPressed(KeyEvent.KEYCODE_STAR);
474 return;
475 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700476 }
477 }
478
479 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480 * called for long touch events
481 */
482 @Override
483 public boolean onLongClick(View view) {
484 int id = view.getId();
485 switch (id) {
486 case R.id.deleteButton: {
487 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700488 return true;
489 }
490 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700491 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700492 keyPressed(KeyEvent.KEYCODE_PLUS);
493 return true;
494 }
495 }
496 return false;
497 }
498
499 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700500 protected void onStart() {
501 super.onStart();
502
503 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700504 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
505 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700506 // Do not animate when view isn't visible yet, just set an initial state.
507 mBackgroundGradient.setColors(lockScreenColors, false);
508 updateTheme(lockScreenColors.supportsDarkText());
509 }
510
511 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 protected void onResume() {
513 super.onResume();
514
515 // retrieve the DTMF tone play back setting.
516 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
517 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
518
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519 // if the mToneGenerator creation fails, just continue without it. It is
520 // a local audio signal, and is not as important as the dtmf tone itself.
521 synchronized (mToneGeneratorLock) {
522 if (mToneGenerator == null) {
523 try {
524 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
525 TONE_RELATIVE_VOLUME);
526 } catch (RuntimeException e) {
527 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
528 mToneGenerator = null;
529 }
530 }
531 }
532
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 updateDialAndDeleteButtonStateEnabledAttr();
534 }
535
536 @Override
537 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700539 }
540
Lucas Dupineb9c5702017-05-10 16:57:09 -0700541 @Override
542 protected void onStop() {
543 super.onStop();
544
545 mColorExtractor.removeOnColorsChangedListener(this);
546 }
547
548 /**
549 * Sets theme based on gradient colors
550 * @param supportsDarkText true if gradient supports dark text
551 */
552 private void updateTheme(boolean supportsDarkText) {
553 if (mSupportsDarkText == supportsDarkText) {
554 return;
555 }
556 mSupportsDarkText = supportsDarkText;
557
558 // We can't change themes after inflation, in this case we'll have to recreate
559 // the whole activity.
560 if (mBackgroundGradient != null) {
561 recreate();
562 return;
563 }
564
565 int vis = getWindow().getDecorView().getSystemUiVisibility();
566 if (supportsDarkText) {
567 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
568 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
569 setTheme(R.style.EmergencyDialerThemeDark);
570 } else {
571 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
572 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
573 setTheme(R.style.EmergencyDialerTheme);
574 }
575 getWindow().getDecorView().setSystemUiVisibility(vis);
576 }
577
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578 /**
579 * place the call, but check to make sure it is a viable number.
580 */
581 private void placeCall() {
582 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900583
584 // Convert into emergency number according to emergency conversion map.
585 // If conversion map is not defined (this is default), this method does
586 // nothing and just returns input number.
587 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
588
Yorke Lee36bb2542014-06-05 08:09:52 -0700589 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
591
592 // place the call if it is a valid number
593 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
594 // There is no number entered.
595 playTone(ToneGenerator.TONE_PROP_NACK);
596 return;
597 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700598 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
599 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 } else {
601 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
602
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700603 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
604 }
Yorke Lee9b341512014-10-17 11:36:41 -0700605 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 }
607
608 /**
609 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
610 *
611 * The tone is played locally, using the audio stream for phone calls.
612 * Tones are played only if the "Audible touch tones" user preference
613 * is checked, and are NOT played if the device is in silent mode.
614 *
615 * @param tone a tone code from {@link ToneGenerator}
616 */
617 void playTone(int tone) {
618 // if local tone playback is disabled, just return.
619 if (!mDTMFToneEnabled) {
620 return;
621 }
622
623 // Also do nothing if the phone is in silent mode.
624 // We need to re-check the ringer mode for *every* playTone()
625 // call, rather than keeping a local flag that's updated in
626 // onResume(), since it's possible to toggle silent mode without
627 // leaving the current activity (via the ENDCALL-longpress menu.)
628 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
629 int ringerMode = audioManager.getRingerMode();
630 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
631 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
632 return;
633 }
634
635 synchronized (mToneGeneratorLock) {
636 if (mToneGenerator == null) {
637 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
638 return;
639 }
640
641 // Start the new tone (will stop any playing tone)
642 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
643 }
644 }
645
646 private CharSequence createErrorMessage(String number) {
647 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800648 String errorString = getString(R.string.dial_emergency_error, number);
649 int startingPosition = errorString.indexOf(number);
650 int endingPosition = startingPosition + number.length();
651 Spannable result = new SpannableString(errorString);
652 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
653 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 } else {
655 return getText(R.string.dial_emergency_empty_error).toString();
656 }
657 }
658
659 @Override
660 protected Dialog onCreateDialog(int id) {
661 AlertDialog dialog = null;
662 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
663 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700664 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700665 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
666 .setMessage(createErrorMessage(mLastNumber))
667 .setPositiveButton(R.string.ok, null)
668 .setCancelable(true).create();
669
670 // blur stuff behind the dialog
671 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700672 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 }
674 return dialog;
675 }
676
677 @Override
678 protected void onPrepareDialog(int id, Dialog dialog) {
679 super.onPrepareDialog(id, dialog);
680 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
681 AlertDialog alert = (AlertDialog) dialog;
682 alert.setMessage(createErrorMessage(mLastNumber));
683 }
684 }
685
Andrew Leed5631e82014-10-08 16:03:58 -0700686 @Override
687 public boolean onOptionsItemSelected(MenuItem item) {
688 final int itemId = item.getItemId();
689 if (itemId == android.R.id.home) {
690 onBackPressed();
691 return true;
692 }
693 return super.onOptionsItemSelected(item);
694 }
695
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700696 /**
697 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
698 */
699 private void updateDialAndDeleteButtonStateEnabledAttr() {
700 final boolean notEmpty = mDigits.length() != 0;
701
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700702 mDelete.setEnabled(notEmpty);
703 }
Yorke Lee91311662014-10-24 14:50:45 -0700704
705 /**
706 * Remove the digit just before the current position. Used by various long pressed callbacks
707 * to remove the digit that was populated as a result of the short click.
708 */
709 private void removePreviousDigitIfPossible() {
710 final int currentPosition = mDigits.getSelectionStart();
711 if (currentPosition > 0) {
712 mDigits.setSelection(currentPosition);
713 mDigits.getText().delete(currentPosition - 1, currentPosition);
714 }
715 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800716
717 /**
718 * Update the text-to-speech annotations in the edit field.
719 */
720 private void updateTtsSpans() {
721 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
722 mDigits.getText().removeSpan(o);
723 }
724 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
725 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700726
727 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700728 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700729 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700730 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
731 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700732 mBackgroundGradient.setColors(colors);
733 updateTheme(colors.supportsDarkText());
734 }
735 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700736
737 /**
738 * Where a carrier requires a warning that emergency calling is not available while on WFC,
739 * add hint text above the dial pad which warns the user of this case.
740 */
741 private void maybeShowWfcEmergencyCallingWarning() {
742 if (!mIsWfcEmergencyCallingWarningEnabled) {
743 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
744 return;
745 }
746
747 // Use an async task rather than calling into Telephony on UI thread.
748 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
749 @Override
750 protected Boolean doInBackground(Void... voids) {
751 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
752 boolean isWfcAvailable = tm.isWifiCallingAvailable();
753 ServiceState ss = tm.getServiceState();
754 boolean isCellAvailable =
755 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
756 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
757 + " isCellAvailable=" + isCellAvailable
758 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
759 return isWfcAvailable && !isCellAvailable;
760 }
761
762 @Override
763 protected void onPostExecute(Boolean result) {
764 if (result.booleanValue()) {
765 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
766 mDigits.setHint(R.string.dial_emergency_calling_not_available);
767 } else {
768 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
769 mDigits.setHint("");
770 }
771 maybeChangeHintSize();
772 }
773 };
774 showWfcWarningTask.execute((Void) null);
775 }
776
777 /**
778 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
779 * edit view and set the font size to a smaller size appropriate for the emergency calling
780 * warning.
781 */
782 private void maybeChangeHintSize() {
783 if (TextUtils.isEmpty(mDigits.getHint())
784 || !TextUtils.isEmpty(mDigits.getText().toString())) {
785 // No hint or there are dialed digits, so use default size.
786 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
787 // By default, the digits view auto-resizes to fit the text it contains, so
788 // enable that now.
789 mDigits.setResizeEnabled(true);
790 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
791 } else {
792 // Hint present and no dialed digits, set custom font size appropriate for the warning.
793 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
794 R.dimen.emergency_call_warning_size));
795 // Since we're populating this with a static text string, disable auto-resize.
796 mDigits.setResizeEnabled(false);
797 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
798 }
799 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800}