blob: 2c8a40b53ffcd0a1363115a9d8e6e87baf6d05cc [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 Gunn1acf54a2018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangd301d9a2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Lucas Dupineb9c5702017-05-10 16:57:09 -070026import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
Lucas Dupineb9c5702017-05-10 16:57:09 -070031import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.media.AudioManager;
33import android.media.ToneGenerator;
34import android.net.Uri;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070035import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070037import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070039import android.telecom.PhoneAccount;
Tyler Gunnca7dfef2018-03-27 18:50:05 +000040import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070041import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.telephony.PhoneNumberUtils;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070043import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070044import android.telephony.SubscriptionManager;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070045import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070047import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080048import android.text.Spannable;
49import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.text.TextUtils;
51import android.text.TextWatcher;
52import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080053import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.util.Log;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070055import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070056import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070058import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070059import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.view.View;
61import android.view.WindowManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062
Lucas Dupinaf9e9912017-06-22 12:39:39 -070063import com.android.internal.colorextraction.ColorExtractor;
64import com.android.internal.colorextraction.ColorExtractor.GradientColors;
65import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070066import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070067import com.android.phone.common.util.ViewUtil;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070068import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070/**
71 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
72 *
73 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
74 * activity from apps/Contacts) that:
75 * 1. Allows ONLY emergency calls to be dialed
76 * 2. Disallows voicemail functionality
77 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
78 * activity to stay in front of the keyguard.
79 *
80 * TODO: Even though this is an ultra-simplified version of the normal
81 * dialer, there's still lots of code duplication between this class and
82 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
83 * moved into a shared base class that would live in the framework?
84 * Or could we figure out some way to move *this* class into apps/Contacts
85 * also?
Chihhang Chuangd301d9a2018-06-05 15:29:06 +080086 *
87 * TODO: Implement emergency dialer shortcut.
88 * emergency dialer shortcut offer a local emergency number list. Directly click a number to
89 * make an emergency phone call without entering numbers from dialpad.
90 * TODO item:
91 * 1.implement emergency shortcut list UI.
92 * 2.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093 */
94public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070095 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Lucas Dupineb9c5702017-05-10 16:57:09 -070096 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097 // Keys used with onSaveInstanceState().
98 private static final String LAST_NUMBER = "lastNumber";
99
100 // Intent action for this activity.
101 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
102
103 // List of dialer button IDs.
104 private static final int[] DIALER_KEYS = new int[] {
105 R.id.one, R.id.two, R.id.three,
106 R.id.four, R.id.five, R.id.six,
107 R.id.seven, R.id.eight, R.id.nine,
108 R.id.star, R.id.zero, R.id.pound };
109
110 // Debug constants.
111 private static final boolean DBG = false;
112 private static final String LOG_TAG = "EmergencyDialer";
113
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 /** The length of DTMF tones in milliseconds */
115 private static final int TONE_LENGTH_MS = 150;
116
117 /** The DTMF tone volume relative to other sounds in the stream */
118 private static final int TONE_RELATIVE_VOLUME = 80;
119
120 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
121 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
122
123 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
124
Lucas Dupineb9c5702017-05-10 16:57:09 -0700125 /** 90% opacity, different from other gradients **/
126 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
127
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700128 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129 private View mDialButton;
130 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800131 private View mEmergencyShortcutView;
132 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133
134 private ToneGenerator mToneGenerator;
135 private Object mToneGeneratorLock = new Object();
136
137 // determines if we want to playback local DTMF tones.
138 private boolean mDTMFToneEnabled;
139
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700140 private EmergencyActionGroup mEmergencyActionGroup;
141
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 // close activity when screen turns off
143 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
144 @Override
145 public void onReceive(Context context, Intent intent) {
146 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700147 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 }
149 }
150 };
151
152 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
153
Lucas Dupineb9c5702017-05-10 16:57:09 -0700154 // Background gradient
155 private ColorExtractor mColorExtractor;
156 private GradientDrawable mBackgroundGradient;
157 private boolean mSupportsDarkText;
158
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700159 private boolean mIsWfcEmergencyCallingWarningEnabled;
160 private float mDefaultDigitsTextSize;
161
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800162 private boolean mAreEmergencyDialerShortcutsEnabled;
163
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 @Override
165 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
166 // Do nothing
167 }
168
169 @Override
170 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700171 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 }
173
174 @Override
175 public void afterTextChanged(Editable input) {
176 // Check for special sequences, in particular the "**04" or "**05"
177 // sequences that allow you to enter PIN or PUK-related codes.
178 //
179 // But note we *don't* allow most other special sequences here,
180 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
181 // since those shouldn't be available if the device is locked.
182 //
183 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
184 // here, not the regular handleChars() method.
185 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
186 // A special sequence was entered, clear the digits
187 mDigits.getText().clear();
188 }
189
190 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800191 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 }
193
194 @Override
195 protected void onCreate(Bundle icicle) {
196 super.onCreate(icicle);
197
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 // Allow this activity to be displayed in front of the keyguard / lockscreen.
199 WindowManager.LayoutParams lp = getWindow().getAttributes();
200 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700201
202 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700203 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700204 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
205
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206 getWindow().setAttributes(lp);
207
Lucas Dupineb9c5702017-05-10 16:57:09 -0700208 mColorExtractor = new ColorExtractor(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700209 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
210 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700211 updateTheme(lockScreenColors.supportsDarkText());
212
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 setContentView(R.layout.emergency_dialer);
214
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700215 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216 mDigits.setKeyListener(DialerKeyListener.getInstance());
217 mDigits.setOnClickListener(this);
218 mDigits.setOnKeyListener(this);
219 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700220 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700221 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 maybeAddNumberFormatting();
223
Lucas Dupineb9c5702017-05-10 16:57:09 -0700224 mBackgroundGradient = new GradientDrawable(this);
225 Point displaySize = new Point();
226 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
227 .getDefaultDisplay().getSize(displaySize);
228 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
229 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
230 getWindow().setBackgroundDrawable(mBackgroundGradient);
231
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232 // Check for the presence of the keypad
233 View view = findViewById(R.id.one);
234 if (view != null) {
235 setupKeypad();
236 }
237
238 mDelete = findViewById(R.id.deleteButton);
239 mDelete.setOnClickListener(this);
240 mDelete.setOnLongClickListener(this);
241
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700242 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243
244 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700245 // Read carrier config through the public API because PhoneGlobals is not available when we
246 // run as a secondary user.
247 CarrierConfigManager configMgr =
248 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
249 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800250 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700251
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700252 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 mDialButton.setOnClickListener(this);
254 } else {
255 mDialButton.setVisibility(View.GONE);
256 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700257 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
258 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
259 maybeShowWfcEmergencyCallingWarning();
260
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700261 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262
263 if (icicle != null) {
264 super.onRestoreInstanceState(icicle);
265 }
266
267 // Extract phone number from intent
268 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700269 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
271 if (number != null) {
272 mDigits.setText(number);
273 }
274 }
275
276 // if the mToneGenerator creation fails, just continue without it. It is
277 // a local audio signal, and is not as important as the dtmf tone itself.
278 synchronized (mToneGeneratorLock) {
279 if (mToneGenerator == null) {
280 try {
281 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
282 } catch (RuntimeException e) {
283 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
284 mToneGenerator = null;
285 }
286 }
287 }
288
289 final IntentFilter intentFilter = new IntentFilter();
290 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
291 registerReceiver(mBroadcastReceiver, intentFilter);
292
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700293 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800294
295 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
296 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
297
298 if (mAreEmergencyDialerShortcutsEnabled) {
299 setupEmergencyShortcutsView();
300 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 }
302
303 @Override
304 protected void onDestroy() {
305 super.onDestroy();
306 synchronized (mToneGeneratorLock) {
307 if (mToneGenerator != null) {
308 mToneGenerator.release();
309 mToneGenerator = null;
310 }
311 }
312 unregisterReceiver(mBroadcastReceiver);
313 }
314
315 @Override
316 protected void onRestoreInstanceState(Bundle icicle) {
317 mLastNumber = icicle.getString(LAST_NUMBER);
318 }
319
320 @Override
321 protected void onSaveInstanceState(Bundle outState) {
322 super.onSaveInstanceState(outState);
323 outState.putString(LAST_NUMBER, mLastNumber);
324 }
325
326 /**
327 * Explicitly turn off number formatting, since it gets in the way of the emergency
328 * number detector
329 */
330 protected void maybeAddNumberFormatting() {
331 // Do nothing.
332 }
333
334 @Override
335 protected void onPostCreate(Bundle savedInstanceState) {
336 super.onPostCreate(savedInstanceState);
337
338 // This can't be done in onCreate(), since the auto-restoring of the digits
339 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
340 // is called. This method will be called every time the activity is created, and
341 // will always happen after onRestoreSavedInstanceState().
342 mDigits.addTextChangedListener(this);
343 }
344
345 private void setupKeypad() {
346 // Setup the listeners for the buttons
347 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700348 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
349 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 }
351
352 View view = findViewById(R.id.zero);
353 view.setOnLongClickListener(this);
354 }
355
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800356 @Override
357 public void onBackPressed() {
358 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
359 // back key will back to display FasterEmergencyDialer view.
360 // Otherwise, it would finish the activity.
361 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
362 && mDialpadView.getVisibility() == View.VISIBLE) {
363 switchView(mEmergencyShortcutView, mDialpadView, true);
364 return;
365 }
366 super.onBackPressed();
367 }
368
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700369 /**
370 * handle key events
371 */
372 @Override
373 public boolean onKeyDown(int keyCode, KeyEvent event) {
374 switch (keyCode) {
375 // Happen when there's a "Call" hard button.
376 case KeyEvent.KEYCODE_CALL: {
377 if (TextUtils.isEmpty(mDigits.getText().toString())) {
378 // if we are adding a call from the InCallScreen and the phone
379 // number entered is empty, we just close the dialer to expose
380 // the InCallScreen under it.
381 finish();
382 } else {
383 // otherwise, we place the call.
384 placeCall();
385 }
386 return true;
387 }
388 }
389 return super.onKeyDown(keyCode, event);
390 }
391
392 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700393 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700394 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
395 mDigits.onKeyDown(keyCode, event);
396 }
397
398 @Override
399 public boolean onKey(View view, int keyCode, KeyEvent event) {
400 switch (view.getId()) {
401 case R.id.digits:
402 // Happen when "Done" button of the IME is pressed. This can happen when this
403 // Activity is forced into landscape mode due to a desk dock.
404 if (keyCode == KeyEvent.KEYCODE_ENTER
405 && event.getAction() == KeyEvent.ACTION_UP) {
406 placeCall();
407 return true;
408 }
409 break;
410 }
411 return false;
412 }
413
414 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700415 public boolean dispatchTouchEvent(MotionEvent ev) {
416 mEmergencyActionGroup.onPreTouchEvent(ev);
417 boolean handled = super.dispatchTouchEvent(ev);
418 mEmergencyActionGroup.onPostTouchEvent(ev);
419 return handled;
420 }
421
422 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700423 public void onClick(View view) {
424 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700425 case R.id.deleteButton: {
426 keyPressed(KeyEvent.KEYCODE_DEL);
427 return;
428 }
429 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700430 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700431 placeCall();
432 return;
433 }
434 case R.id.digits: {
435 if (mDigits.length() != 0) {
436 mDigits.setCursorVisible(true);
437 }
438 return;
439 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800440 case R.id.floating_action_button_dialpad: {
441 switchView(mDialpadView, mEmergencyShortcutView, true);
442 return;
443 }
444 case R.id.emergency_info_button: {
445 Intent intent = (Intent) view.getTag(R.id.tag_intent);
446 if (intent != null) {
447 startActivity(intent);
448 }
449 return;
450 }
Yorke Lee23a70732014-08-14 17:12:01 -0700451 }
452 }
453
454 @Override
455 public void onPressed(View view, boolean pressed) {
456 if (!pressed) {
457 return;
458 }
459 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700460 case R.id.one: {
461 playTone(ToneGenerator.TONE_DTMF_1);
462 keyPressed(KeyEvent.KEYCODE_1);
463 return;
464 }
465 case R.id.two: {
466 playTone(ToneGenerator.TONE_DTMF_2);
467 keyPressed(KeyEvent.KEYCODE_2);
468 return;
469 }
470 case R.id.three: {
471 playTone(ToneGenerator.TONE_DTMF_3);
472 keyPressed(KeyEvent.KEYCODE_3);
473 return;
474 }
475 case R.id.four: {
476 playTone(ToneGenerator.TONE_DTMF_4);
477 keyPressed(KeyEvent.KEYCODE_4);
478 return;
479 }
480 case R.id.five: {
481 playTone(ToneGenerator.TONE_DTMF_5);
482 keyPressed(KeyEvent.KEYCODE_5);
483 return;
484 }
485 case R.id.six: {
486 playTone(ToneGenerator.TONE_DTMF_6);
487 keyPressed(KeyEvent.KEYCODE_6);
488 return;
489 }
490 case R.id.seven: {
491 playTone(ToneGenerator.TONE_DTMF_7);
492 keyPressed(KeyEvent.KEYCODE_7);
493 return;
494 }
495 case R.id.eight: {
496 playTone(ToneGenerator.TONE_DTMF_8);
497 keyPressed(KeyEvent.KEYCODE_8);
498 return;
499 }
500 case R.id.nine: {
501 playTone(ToneGenerator.TONE_DTMF_9);
502 keyPressed(KeyEvent.KEYCODE_9);
503 return;
504 }
505 case R.id.zero: {
506 playTone(ToneGenerator.TONE_DTMF_0);
507 keyPressed(KeyEvent.KEYCODE_0);
508 return;
509 }
510 case R.id.pound: {
511 playTone(ToneGenerator.TONE_DTMF_P);
512 keyPressed(KeyEvent.KEYCODE_POUND);
513 return;
514 }
515 case R.id.star: {
516 playTone(ToneGenerator.TONE_DTMF_S);
517 keyPressed(KeyEvent.KEYCODE_STAR);
518 return;
519 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700520 }
521 }
522
523 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524 * called for long touch events
525 */
526 @Override
527 public boolean onLongClick(View view) {
528 int id = view.getId();
529 switch (id) {
530 case R.id.deleteButton: {
531 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700532 return true;
533 }
534 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700535 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700536 keyPressed(KeyEvent.KEYCODE_PLUS);
537 return true;
538 }
539 }
540 return false;
541 }
542
543 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700544 protected void onStart() {
545 super.onStart();
546
547 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700548 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
549 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700550 // Do not animate when view isn't visible yet, just set an initial state.
551 mBackgroundGradient.setColors(lockScreenColors, false);
552 updateTheme(lockScreenColors.supportsDarkText());
553 }
554
555 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 protected void onResume() {
557 super.onResume();
558
559 // retrieve the DTMF tone play back setting.
560 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
561 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
562
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 // if the mToneGenerator creation fails, just continue without it. It is
564 // a local audio signal, and is not as important as the dtmf tone itself.
565 synchronized (mToneGeneratorLock) {
566 if (mToneGenerator == null) {
567 try {
568 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
569 TONE_RELATIVE_VOLUME);
570 } catch (RuntimeException e) {
571 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
572 mToneGenerator = null;
573 }
574 }
575 }
576
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 updateDialAndDeleteButtonStateEnabledAttr();
578 }
579
580 @Override
581 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 }
584
Lucas Dupineb9c5702017-05-10 16:57:09 -0700585 @Override
586 protected void onStop() {
587 super.onStop();
588
589 mColorExtractor.removeOnColorsChangedListener(this);
590 }
591
592 /**
593 * Sets theme based on gradient colors
594 * @param supportsDarkText true if gradient supports dark text
595 */
596 private void updateTheme(boolean supportsDarkText) {
597 if (mSupportsDarkText == supportsDarkText) {
598 return;
599 }
600 mSupportsDarkText = supportsDarkText;
601
602 // We can't change themes after inflation, in this case we'll have to recreate
603 // the whole activity.
604 if (mBackgroundGradient != null) {
605 recreate();
606 return;
607 }
608
609 int vis = getWindow().getDecorView().getSystemUiVisibility();
610 if (supportsDarkText) {
611 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
612 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
613 setTheme(R.style.EmergencyDialerThemeDark);
614 } else {
615 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
616 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
617 setTheme(R.style.EmergencyDialerTheme);
618 }
619 getWindow().getDecorView().setSystemUiVisibility(vis);
620 }
621
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700622 /**
623 * place the call, but check to make sure it is a viable number.
624 */
625 private void placeCall() {
626 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900627
628 // Convert into emergency number according to emergency conversion map.
629 // If conversion map is not defined (this is default), this method does
630 // nothing and just returns input number.
631 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
632
Yorke Lee36bb2542014-06-05 08:09:52 -0700633 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
635
636 // place the call if it is a valid number
637 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
638 // There is no number entered.
639 playTone(ToneGenerator.TONE_PROP_NACK);
640 return;
641 }
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000642 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
643 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700644 } else {
645 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
646
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
648 }
Yorke Lee9b341512014-10-17 11:36:41 -0700649 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 }
651
652 /**
653 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
654 *
655 * The tone is played locally, using the audio stream for phone calls.
656 * Tones are played only if the "Audible touch tones" user preference
657 * is checked, and are NOT played if the device is in silent mode.
658 *
659 * @param tone a tone code from {@link ToneGenerator}
660 */
661 void playTone(int tone) {
662 // if local tone playback is disabled, just return.
663 if (!mDTMFToneEnabled) {
664 return;
665 }
666
667 // Also do nothing if the phone is in silent mode.
668 // We need to re-check the ringer mode for *every* playTone()
669 // call, rather than keeping a local flag that's updated in
670 // onResume(), since it's possible to toggle silent mode without
671 // leaving the current activity (via the ENDCALL-longpress menu.)
672 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
673 int ringerMode = audioManager.getRingerMode();
674 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
675 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
676 return;
677 }
678
679 synchronized (mToneGeneratorLock) {
680 if (mToneGenerator == null) {
681 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
682 return;
683 }
684
685 // Start the new tone (will stop any playing tone)
686 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
687 }
688 }
689
690 private CharSequence createErrorMessage(String number) {
691 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800692 String errorString = getString(R.string.dial_emergency_error, number);
693 int startingPosition = errorString.indexOf(number);
694 int endingPosition = startingPosition + number.length();
695 Spannable result = new SpannableString(errorString);
696 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
697 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700698 } else {
699 return getText(R.string.dial_emergency_empty_error).toString();
700 }
701 }
702
703 @Override
704 protected Dialog onCreateDialog(int id) {
705 AlertDialog dialog = null;
706 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
707 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700708 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700709 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
710 .setMessage(createErrorMessage(mLastNumber))
711 .setPositiveButton(R.string.ok, null)
712 .setCancelable(true).create();
713
714 // blur stuff behind the dialog
715 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700716 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 }
718 return dialog;
719 }
720
721 @Override
722 protected void onPrepareDialog(int id, Dialog dialog) {
723 super.onPrepareDialog(id, dialog);
724 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
725 AlertDialog alert = (AlertDialog) dialog;
726 alert.setMessage(createErrorMessage(mLastNumber));
727 }
728 }
729
Andrew Leed5631e82014-10-08 16:03:58 -0700730 @Override
731 public boolean onOptionsItemSelected(MenuItem item) {
732 final int itemId = item.getItemId();
733 if (itemId == android.R.id.home) {
734 onBackPressed();
735 return true;
736 }
737 return super.onOptionsItemSelected(item);
738 }
739
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 /**
741 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
742 */
743 private void updateDialAndDeleteButtonStateEnabledAttr() {
744 final boolean notEmpty = mDigits.length() != 0;
745
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700746 mDelete.setEnabled(notEmpty);
747 }
Yorke Lee91311662014-10-24 14:50:45 -0700748
749 /**
750 * Remove the digit just before the current position. Used by various long pressed callbacks
751 * to remove the digit that was populated as a result of the short click.
752 */
753 private void removePreviousDigitIfPossible() {
754 final int currentPosition = mDigits.getSelectionStart();
755 if (currentPosition > 0) {
756 mDigits.setSelection(currentPosition);
757 mDigits.getText().delete(currentPosition - 1, currentPosition);
758 }
759 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800760
761 /**
762 * Update the text-to-speech annotations in the edit field.
763 */
764 private void updateTtsSpans() {
765 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
766 mDigits.getText().removeSpan(o);
767 }
768 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
769 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700770
771 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700772 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700773 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700774 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
775 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700776 mBackgroundGradient.setColors(colors);
777 updateTheme(colors.supportsDarkText());
778 }
779 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700780
781 /**
782 * Where a carrier requires a warning that emergency calling is not available while on WFC,
783 * add hint text above the dial pad which warns the user of this case.
784 */
785 private void maybeShowWfcEmergencyCallingWarning() {
786 if (!mIsWfcEmergencyCallingWarningEnabled) {
787 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
788 return;
789 }
790
791 // Use an async task rather than calling into Telephony on UI thread.
792 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
793 @Override
794 protected Boolean doInBackground(Void... voids) {
795 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
796 boolean isWfcAvailable = tm.isWifiCallingAvailable();
797 ServiceState ss = tm.getServiceState();
798 boolean isCellAvailable =
799 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
800 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
801 + " isCellAvailable=" + isCellAvailable
802 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
803 return isWfcAvailable && !isCellAvailable;
804 }
805
806 @Override
807 protected void onPostExecute(Boolean result) {
808 if (result.booleanValue()) {
809 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
810 mDigits.setHint(R.string.dial_emergency_calling_not_available);
811 } else {
812 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
813 mDigits.setHint("");
814 }
815 maybeChangeHintSize();
816 }
817 };
818 showWfcWarningTask.execute((Void) null);
819 }
820
821 /**
822 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
823 * edit view and set the font size to a smaller size appropriate for the emergency calling
824 * warning.
825 */
826 private void maybeChangeHintSize() {
827 if (TextUtils.isEmpty(mDigits.getHint())
828 || !TextUtils.isEmpty(mDigits.getText().toString())) {
829 // No hint or there are dialed digits, so use default size.
830 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
831 // By default, the digits view auto-resizes to fit the text it contains, so
832 // enable that now.
833 mDigits.setResizeEnabled(true);
834 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
835 } else {
836 // Hint present and no dialed digits, set custom font size appropriate for the warning.
837 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
838 R.dimen.emergency_call_warning_size));
839 // Since we're populating this with a static text string, disable auto-resize.
840 mDigits.setResizeEnabled(false);
841 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
842 }
843 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800844
845 private void setupEmergencyShortcutsView() {
846 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
847 mDialpadView = findViewById(R.id.emergency_dialer);
848
849 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
850 dialpadButton.setOnClickListener(this);
851
852 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
853 emergencyInfoButton.setOnClickListener(this);
854
855 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
856 mEmergencyActionGroup.setVisibility(View.GONE);
857
858 // Setup dialpad title.
859 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
860 emergencyDialpadTitle.setVisibility(View.VISIBLE);
861
862 switchView(mEmergencyShortcutView, mDialpadView, false);
863 }
864
865 /**
866 * Switch two view.
867 *
868 * @param displayView the view would be displayed.
869 * @param hideView the view would be hidden.
870 * @param hasAnimation is {@code true} when the view should be displayed with animation.
871 */
872 private void switchView(View displayView, View hideView, boolean hasAnimation) {
873 if (displayView == null || hideView == null) {
874 return;
875 }
876
877 if (displayView.getVisibility() == View.VISIBLE) {
878 return;
879 }
880
881 if (hasAnimation) {
882 crossfade(hideView, displayView);
883 } else {
884 hideView.setVisibility(View.GONE);
885 displayView.setVisibility(View.VISIBLE);
886 }
887 }
888
889 /**
890 * Fade out and fade in animation between two view transition.
891 */
892 private void crossfade(View fadeOutView, View fadeInView) {
893 if (fadeOutView == null || fadeInView == null) {
894 return;
895 }
896 final int shortAnimationDuration = getResources().getInteger(
897 android.R.integer.config_shortAnimTime);
898
899 fadeInView.setAlpha(0f);
900 fadeInView.setVisibility(View.VISIBLE);
901
902 fadeInView.animate()
903 .alpha(1f)
904 .setDuration(shortAnimationDuration)
905 .setListener(null);
906
907 fadeOutView.animate()
908 .alpha(0f)
909 .setDuration(shortAnimationDuration)
910 .setListener(new AnimatorListenerAdapter() {
911 @Override
912 public void onAnimationEnd(Animator animation) {
913 fadeOutView.setVisibility(View.GONE);
914 }
915 });
916 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700917}