blob: 82a69ee899226f61c7856db16326f699d36e8763 [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;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070038import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.telephony.PhoneNumberUtils;
Tyler Gunnc5428972018-03-28 14:15:34 -070040import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070041import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070042import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070044import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080045import android.text.Spannable;
46import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.text.TextUtils;
48import android.text.TextWatcher;
49import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080050import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070052import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070053import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070055import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070056import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.view.View;
58import android.view.WindowManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059
Lucas Dupinaf9e9912017-06-22 12:39:39 -070060import com.android.internal.colorextraction.ColorExtractor;
61import com.android.internal.colorextraction.ColorExtractor.GradientColors;
62import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070063import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070064import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070065import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067/**
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 /** The length of DTMF tones in milliseconds */
105 private static final int TONE_LENGTH_MS = 150;
106
107 /** The DTMF tone volume relative to other sounds in the stream */
108 private static final int TONE_RELATIVE_VOLUME = 80;
109
110 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
111 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
112
113 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
114
Lucas Dupineb9c5702017-05-10 16:57:09 -0700115 /** 90% opacity, different from other gradients **/
116 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
117
Tyler Gunnc5428972018-03-28 14:15:34 -0700118 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700119 private View mDialButton;
120 private View mDelete;
121
122 private ToneGenerator mToneGenerator;
123 private Object mToneGeneratorLock = new Object();
124
125 // determines if we want to playback local DTMF tones.
126 private boolean mDTMFToneEnabled;
127
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700128 private EmergencyActionGroup mEmergencyActionGroup;
129
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130 // close activity when screen turns off
131 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
132 @Override
133 public void onReceive(Context context, Intent intent) {
134 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700135 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136 }
137 }
138 };
139
140 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
141
Lucas Dupineb9c5702017-05-10 16:57:09 -0700142 // Background gradient
143 private ColorExtractor mColorExtractor;
144 private GradientDrawable mBackgroundGradient;
145 private boolean mSupportsDarkText;
146
Tyler Gunnc5428972018-03-28 14:15:34 -0700147 private boolean mIsWfcEmergencyCallingWarningEnabled;
148 private float mDefaultDigitsTextSize;
149
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 @Override
151 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
152 // Do nothing
153 }
154
155 @Override
156 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700157 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158 }
159
160 @Override
161 public void afterTextChanged(Editable input) {
162 // Check for special sequences, in particular the "**04" or "**05"
163 // sequences that allow you to enter PIN or PUK-related codes.
164 //
165 // But note we *don't* allow most other special sequences here,
166 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
167 // since those shouldn't be available if the device is locked.
168 //
169 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
170 // here, not the regular handleChars() method.
171 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
172 // A special sequence was entered, clear the digits
173 mDigits.getText().clear();
174 }
175
176 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800177 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 }
179
180 @Override
181 protected void onCreate(Bundle icicle) {
182 super.onCreate(icicle);
183
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 // Allow this activity to be displayed in front of the keyguard / lockscreen.
185 WindowManager.LayoutParams lp = getWindow().getAttributes();
186 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700187
188 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700189 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700190 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
191
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 getWindow().setAttributes(lp);
193
Lucas Dupineb9c5702017-05-10 16:57:09 -0700194 mColorExtractor = new ColorExtractor(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700195 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
196 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700197 updateTheme(lockScreenColors.supportsDarkText());
198
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 setContentView(R.layout.emergency_dialer);
200
Tyler Gunnc5428972018-03-28 14:15:34 -0700201 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 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);
Tyler Gunnc5428972018-03-28 14:15:34 -0700207 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 maybeAddNumberFormatting();
209
Lucas Dupineb9c5702017-05-10 16:57:09 -0700210 mBackgroundGradient = new GradientDrawable(this);
211 Point displaySize = new Point();
212 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
213 .getDefaultDisplay().getSize(displaySize);
214 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
215 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
216 getWindow().setBackgroundDrawable(mBackgroundGradient);
217
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218 // Check for the presence of the keypad
219 View view = findViewById(R.id.one);
220 if (view != null) {
221 setupKeypad();
222 }
223
224 mDelete = findViewById(R.id.deleteButton);
225 mDelete.setOnClickListener(this);
226 mDelete.setOnLongClickListener(this);
227
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700228 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229
230 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700231 // Read carrier config through the public API because PhoneGlobals is not available when we
232 // run as a secondary user.
233 CarrierConfigManager configMgr =
234 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
235 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800236 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700237
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700238 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 mDialButton.setOnClickListener(this);
240 } else {
241 mDialButton.setVisibility(View.GONE);
242 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700243 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
244 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
245 maybeShowWfcEmergencyCallingWarning();
246
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700247 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248
249 if (icicle != null) {
250 super.onRestoreInstanceState(icicle);
251 }
252
253 // Extract phone number from intent
254 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700255 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700256 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
257 if (number != null) {
258 mDigits.setText(number);
259 }
260 }
261
262 // if the mToneGenerator creation fails, just continue without it. It is
263 // a local audio signal, and is not as important as the dtmf tone itself.
264 synchronized (mToneGeneratorLock) {
265 if (mToneGenerator == null) {
266 try {
267 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
268 } catch (RuntimeException e) {
269 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
270 mToneGenerator = null;
271 }
272 }
273 }
274
275 final IntentFilter intentFilter = new IntentFilter();
276 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
277 registerReceiver(mBroadcastReceiver, intentFilter);
278
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700279 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 }
281
282 @Override
283 protected void onDestroy() {
284 super.onDestroy();
285 synchronized (mToneGeneratorLock) {
286 if (mToneGenerator != null) {
287 mToneGenerator.release();
288 mToneGenerator = null;
289 }
290 }
291 unregisterReceiver(mBroadcastReceiver);
292 }
293
294 @Override
295 protected void onRestoreInstanceState(Bundle icicle) {
296 mLastNumber = icicle.getString(LAST_NUMBER);
297 }
298
299 @Override
300 protected void onSaveInstanceState(Bundle outState) {
301 super.onSaveInstanceState(outState);
302 outState.putString(LAST_NUMBER, mLastNumber);
303 }
304
305 /**
306 * Explicitly turn off number formatting, since it gets in the way of the emergency
307 * number detector
308 */
309 protected void maybeAddNumberFormatting() {
310 // Do nothing.
311 }
312
313 @Override
314 protected void onPostCreate(Bundle savedInstanceState) {
315 super.onPostCreate(savedInstanceState);
316
317 // This can't be done in onCreate(), since the auto-restoring of the digits
318 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
319 // is called. This method will be called every time the activity is created, and
320 // will always happen after onRestoreSavedInstanceState().
321 mDigits.addTextChangedListener(this);
322 }
323
324 private void setupKeypad() {
325 // Setup the listeners for the buttons
326 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700327 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
328 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 }
330
331 View view = findViewById(R.id.zero);
332 view.setOnLongClickListener(this);
333 }
334
335 /**
336 * handle key events
337 */
338 @Override
339 public boolean onKeyDown(int keyCode, KeyEvent event) {
340 switch (keyCode) {
341 // Happen when there's a "Call" hard button.
342 case KeyEvent.KEYCODE_CALL: {
343 if (TextUtils.isEmpty(mDigits.getText().toString())) {
344 // if we are adding a call from the InCallScreen and the phone
345 // number entered is empty, we just close the dialer to expose
346 // the InCallScreen under it.
347 finish();
348 } else {
349 // otherwise, we place the call.
350 placeCall();
351 }
352 return true;
353 }
354 }
355 return super.onKeyDown(keyCode, event);
356 }
357
358 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700359 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
361 mDigits.onKeyDown(keyCode, event);
362 }
363
364 @Override
365 public boolean onKey(View view, int keyCode, KeyEvent event) {
366 switch (view.getId()) {
367 case R.id.digits:
368 // Happen when "Done" button of the IME is pressed. This can happen when this
369 // Activity is forced into landscape mode due to a desk dock.
370 if (keyCode == KeyEvent.KEYCODE_ENTER
371 && event.getAction() == KeyEvent.ACTION_UP) {
372 placeCall();
373 return true;
374 }
375 break;
376 }
377 return false;
378 }
379
380 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700381 public boolean dispatchTouchEvent(MotionEvent ev) {
382 mEmergencyActionGroup.onPreTouchEvent(ev);
383 boolean handled = super.dispatchTouchEvent(ev);
384 mEmergencyActionGroup.onPostTouchEvent(ev);
385 return handled;
386 }
387
388 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 public void onClick(View view) {
390 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700391 case R.id.deleteButton: {
392 keyPressed(KeyEvent.KEYCODE_DEL);
393 return;
394 }
395 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700396 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700397 placeCall();
398 return;
399 }
400 case R.id.digits: {
401 if (mDigits.length() != 0) {
402 mDigits.setCursorVisible(true);
403 }
404 return;
405 }
406 }
407 }
408
409 @Override
410 public void onPressed(View view, boolean pressed) {
411 if (!pressed) {
412 return;
413 }
414 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 case R.id.one: {
416 playTone(ToneGenerator.TONE_DTMF_1);
417 keyPressed(KeyEvent.KEYCODE_1);
418 return;
419 }
420 case R.id.two: {
421 playTone(ToneGenerator.TONE_DTMF_2);
422 keyPressed(KeyEvent.KEYCODE_2);
423 return;
424 }
425 case R.id.three: {
426 playTone(ToneGenerator.TONE_DTMF_3);
427 keyPressed(KeyEvent.KEYCODE_3);
428 return;
429 }
430 case R.id.four: {
431 playTone(ToneGenerator.TONE_DTMF_4);
432 keyPressed(KeyEvent.KEYCODE_4);
433 return;
434 }
435 case R.id.five: {
436 playTone(ToneGenerator.TONE_DTMF_5);
437 keyPressed(KeyEvent.KEYCODE_5);
438 return;
439 }
440 case R.id.six: {
441 playTone(ToneGenerator.TONE_DTMF_6);
442 keyPressed(KeyEvent.KEYCODE_6);
443 return;
444 }
445 case R.id.seven: {
446 playTone(ToneGenerator.TONE_DTMF_7);
447 keyPressed(KeyEvent.KEYCODE_7);
448 return;
449 }
450 case R.id.eight: {
451 playTone(ToneGenerator.TONE_DTMF_8);
452 keyPressed(KeyEvent.KEYCODE_8);
453 return;
454 }
455 case R.id.nine: {
456 playTone(ToneGenerator.TONE_DTMF_9);
457 keyPressed(KeyEvent.KEYCODE_9);
458 return;
459 }
460 case R.id.zero: {
461 playTone(ToneGenerator.TONE_DTMF_0);
462 keyPressed(KeyEvent.KEYCODE_0);
463 return;
464 }
465 case R.id.pound: {
466 playTone(ToneGenerator.TONE_DTMF_P);
467 keyPressed(KeyEvent.KEYCODE_POUND);
468 return;
469 }
470 case R.id.star: {
471 playTone(ToneGenerator.TONE_DTMF_S);
472 keyPressed(KeyEvent.KEYCODE_STAR);
473 return;
474 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700475 }
476 }
477
478 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 * called for long touch events
480 */
481 @Override
482 public boolean onLongClick(View view) {
483 int id = view.getId();
484 switch (id) {
485 case R.id.deleteButton: {
486 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700487 return true;
488 }
489 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700490 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700491 keyPressed(KeyEvent.KEYCODE_PLUS);
492 return true;
493 }
494 }
495 return false;
496 }
497
498 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700499 protected void onStart() {
500 super.onStart();
501
502 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700503 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
504 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700505 // Do not animate when view isn't visible yet, just set an initial state.
506 mBackgroundGradient.setColors(lockScreenColors, false);
507 updateTheme(lockScreenColors.supportsDarkText());
508 }
509
510 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700511 protected void onResume() {
512 super.onResume();
513
514 // retrieve the DTMF tone play back setting.
515 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
516 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
517
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700518 // if the mToneGenerator creation fails, just continue without it. It is
519 // a local audio signal, and is not as important as the dtmf tone itself.
520 synchronized (mToneGeneratorLock) {
521 if (mToneGenerator == null) {
522 try {
523 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
524 TONE_RELATIVE_VOLUME);
525 } catch (RuntimeException e) {
526 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
527 mToneGenerator = null;
528 }
529 }
530 }
531
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700532 updateDialAndDeleteButtonStateEnabledAttr();
533 }
534
535 @Override
536 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700537 super.onPause();
538
539 synchronized (mToneGeneratorLock) {
540 if (mToneGenerator != null) {
541 mToneGenerator.release();
542 mToneGenerator = null;
543 }
544 }
545 }
546
Lucas Dupineb9c5702017-05-10 16:57:09 -0700547 @Override
548 protected void onStop() {
549 super.onStop();
550
551 mColorExtractor.removeOnColorsChangedListener(this);
552 }
553
554 /**
555 * Sets theme based on gradient colors
556 * @param supportsDarkText true if gradient supports dark text
557 */
558 private void updateTheme(boolean supportsDarkText) {
559 if (mSupportsDarkText == supportsDarkText) {
560 return;
561 }
562 mSupportsDarkText = supportsDarkText;
563
564 // We can't change themes after inflation, in this case we'll have to recreate
565 // the whole activity.
566 if (mBackgroundGradient != null) {
567 recreate();
568 return;
569 }
570
571 int vis = getWindow().getDecorView().getSystemUiVisibility();
572 if (supportsDarkText) {
573 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
574 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
575 setTheme(R.style.EmergencyDialerThemeDark);
576 } else {
577 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
578 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
579 setTheme(R.style.EmergencyDialerTheme);
580 }
581 getWindow().getDecorView().setSystemUiVisibility(vis);
582 }
583
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700584 /**
585 * place the call, but check to make sure it is a viable number.
586 */
587 private void placeCall() {
588 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900589
590 // Convert into emergency number according to emergency conversion map.
591 // If conversion map is not defined (this is default), this method does
592 // nothing and just returns input number.
593 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
594
Yorke Lee36bb2542014-06-05 08:09:52 -0700595 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
597
598 // place the call if it is a valid number
599 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
600 // There is no number entered.
601 playTone(ToneGenerator.TONE_PROP_NACK);
602 return;
603 }
604 Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY);
Jay Shrauner137458b2014-09-05 14:27:25 -0700605 intent.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
607 startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608 } else {
609 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
610
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700611 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
612 }
Yorke Lee9b341512014-10-17 11:36:41 -0700613 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 }
615
616 /**
617 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
618 *
619 * The tone is played locally, using the audio stream for phone calls.
620 * Tones are played only if the "Audible touch tones" user preference
621 * is checked, and are NOT played if the device is in silent mode.
622 *
623 * @param tone a tone code from {@link ToneGenerator}
624 */
625 void playTone(int tone) {
626 // if local tone playback is disabled, just return.
627 if (!mDTMFToneEnabled) {
628 return;
629 }
630
631 // Also do nothing if the phone is in silent mode.
632 // We need to re-check the ringer mode for *every* playTone()
633 // call, rather than keeping a local flag that's updated in
634 // onResume(), since it's possible to toggle silent mode without
635 // leaving the current activity (via the ENDCALL-longpress menu.)
636 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
637 int ringerMode = audioManager.getRingerMode();
638 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
639 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
640 return;
641 }
642
643 synchronized (mToneGeneratorLock) {
644 if (mToneGenerator == null) {
645 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
646 return;
647 }
648
649 // Start the new tone (will stop any playing tone)
650 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
651 }
652 }
653
654 private CharSequence createErrorMessage(String number) {
655 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800656 String errorString = getString(R.string.dial_emergency_error, number);
657 int startingPosition = errorString.indexOf(number);
658 int endingPosition = startingPosition + number.length();
659 Spannable result = new SpannableString(errorString);
660 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
661 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 } else {
663 return getText(R.string.dial_emergency_empty_error).toString();
664 }
665 }
666
667 @Override
668 protected Dialog onCreateDialog(int id) {
669 AlertDialog dialog = null;
670 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
671 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700672 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
674 .setMessage(createErrorMessage(mLastNumber))
675 .setPositiveButton(R.string.ok, null)
676 .setCancelable(true).create();
677
678 // blur stuff behind the dialog
679 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Yorke Leec30f00c2014-07-31 16:09:05 -0700680 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700681 }
682 return dialog;
683 }
684
685 @Override
686 protected void onPrepareDialog(int id, Dialog dialog) {
687 super.onPrepareDialog(id, dialog);
688 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
689 AlertDialog alert = (AlertDialog) dialog;
690 alert.setMessage(createErrorMessage(mLastNumber));
691 }
692 }
693
Andrew Leed5631e82014-10-08 16:03:58 -0700694 @Override
695 public boolean onOptionsItemSelected(MenuItem item) {
696 final int itemId = item.getItemId();
697 if (itemId == android.R.id.home) {
698 onBackPressed();
699 return true;
700 }
701 return super.onOptionsItemSelected(item);
702 }
703
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704 /**
705 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
706 */
707 private void updateDialAndDeleteButtonStateEnabledAttr() {
708 final boolean notEmpty = mDigits.length() != 0;
709
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710 mDelete.setEnabled(notEmpty);
711 }
Yorke Lee91311662014-10-24 14:50:45 -0700712
713 /**
714 * Remove the digit just before the current position. Used by various long pressed callbacks
715 * to remove the digit that was populated as a result of the short click.
716 */
717 private void removePreviousDigitIfPossible() {
718 final int currentPosition = mDigits.getSelectionStart();
719 if (currentPosition > 0) {
720 mDigits.setSelection(currentPosition);
721 mDigits.getText().delete(currentPosition - 1, currentPosition);
722 }
723 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800724
725 /**
726 * Update the text-to-speech annotations in the edit field.
727 */
728 private void updateTtsSpans() {
729 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
730 mDigits.getText().removeSpan(o);
731 }
732 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
733 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700734
735 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700736 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700737 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700738 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
739 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700740 mBackgroundGradient.setColors(colors);
741 updateTheme(colors.supportsDarkText());
742 }
743 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700744
745 /**
746 * Where a carrier requires a warning that emergency calling is not available while on WFC,
747 * add hint text above the dial pad which warns the user of this case.
748 */
749 private void maybeShowWfcEmergencyCallingWarning() {
750 if (!mIsWfcEmergencyCallingWarningEnabled) {
751 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
752 return;
753 }
754
755 // Use an async task rather than calling into Telephony on UI thread.
756 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
757 @Override
758 protected Boolean doInBackground(Void... voids) {
759 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
760 boolean isWfcAvailable = tm.isWifiCallingAvailable();
761 ServiceState ss = tm.getServiceState();
762 boolean isCellAvailable =
763 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
764 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
765 + " isCellAvailable=" + isCellAvailable
766 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
767 return isWfcAvailable && !isCellAvailable;
768 }
769
770 @Override
771 protected void onPostExecute(Boolean result) {
772 if (result.booleanValue()) {
773 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
774 mDigits.setHint(R.string.dial_emergency_calling_not_available);
775 } else {
776 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
777 mDigits.setHint("");
778 }
779 maybeChangeHintSize();
780 }
781 };
782 showWfcWarningTask.execute((Void) null);
783 }
784
785 /**
786 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
787 * edit view and set the font size to a smaller size appropriate for the emergency calling
788 * warning.
789 */
790 private void maybeChangeHintSize() {
791 if (TextUtils.isEmpty(mDigits.getHint())
792 || !TextUtils.isEmpty(mDigits.getText().toString())) {
793 // No hint or there are dialed digits, so use default size.
794 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
795 // By default, the digits view auto-resizes to fit the text it contains, so
796 // enable that now.
797 mDigits.setResizeEnabled(true);
798 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
799 } else {
800 // Hint present and no dialed digits, set custom font size appropriate for the warning.
801 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
802 R.dimen.emergency_call_warning_size));
803 // Since we're populating this with a static text string, disable auto-resize.
804 mDigits.setResizeEnabled(false);
805 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
806 }
807 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808}