blob: ea9eae1a9dddfbf9ef47cb5b3d26d922770a1bc4 [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
Chihhang Chuangf264cfb2018-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 Gunnc5428972018-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 Gunnfa77e202018-03-23 07:47:00 -070040import 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 Gunnc5428972018-03-28 14:15:34 -070043import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070044import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-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 Gunnc5428972018-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;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080061import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import android.view.WindowManager;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080063import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064
Lucas Dupinaf9e9912017-06-22 12:39:39 -070065import com.android.internal.colorextraction.ColorExtractor;
66import com.android.internal.colorextraction.ColorExtractor.GradientColors;
67import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070068import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070069import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070070import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071
Chihhang Chuang92cfe512018-06-07 16:25:27 +080072import java.util.ArrayList;
73import java.util.List;
74
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075/**
76 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
77 *
78 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
79 * activity from apps/Contacts) that:
80 * 1. Allows ONLY emergency calls to be dialed
81 * 2. Disallows voicemail functionality
82 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
83 * activity to stay in front of the keyguard.
84 *
85 * TODO: Even though this is an ultra-simplified version of the normal
86 * dialer, there's still lots of code duplication between this class and
87 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
88 * moved into a shared base class that would live in the framework?
89 * Or could we figure out some way to move *this* class into apps/Contacts
90 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080091 *
92 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +080093 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
94 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080095 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +080096 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097 */
98public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070099 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800100 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
101 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 // Keys used with onSaveInstanceState().
103 private static final String LAST_NUMBER = "lastNumber";
104
105 // Intent action for this activity.
106 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
107
108 // List of dialer button IDs.
109 private static final int[] DIALER_KEYS = new int[] {
110 R.id.one, R.id.two, R.id.three,
111 R.id.four, R.id.five, R.id.six,
112 R.id.seven, R.id.eight, R.id.nine,
113 R.id.star, R.id.zero, R.id.pound };
114
115 // Debug constants.
116 private static final boolean DBG = false;
117 private static final String LOG_TAG = "EmergencyDialer";
118
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700119 /** The length of DTMF tones in milliseconds */
120 private static final int TONE_LENGTH_MS = 150;
121
122 /** The DTMF tone volume relative to other sounds in the stream */
123 private static final int TONE_RELATIVE_VOLUME = 80;
124
125 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
126 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
127
128 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
129
Lucas Dupineb9c5702017-05-10 16:57:09 -0700130 /** 90% opacity, different from other gradients **/
131 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
132
Tyler Gunnc5428972018-03-28 14:15:34 -0700133 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700134 private View mDialButton;
135 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800136 private View mEmergencyShortcutView;
137 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800139 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
140
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141 private ToneGenerator mToneGenerator;
142 private Object mToneGeneratorLock = new Object();
143
144 // determines if we want to playback local DTMF tones.
145 private boolean mDTMFToneEnabled;
146
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700147 private EmergencyActionGroup mEmergencyActionGroup;
148
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 // close activity when screen turns off
150 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
151 @Override
152 public void onReceive(Context context, Intent intent) {
153 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700154 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 }
156 }
157 };
158
159 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
160
Lucas Dupineb9c5702017-05-10 16:57:09 -0700161 // Background gradient
162 private ColorExtractor mColorExtractor;
163 private GradientDrawable mBackgroundGradient;
164 private boolean mSupportsDarkText;
165
Tyler Gunnc5428972018-03-28 14:15:34 -0700166 private boolean mIsWfcEmergencyCallingWarningEnabled;
167 private float mDefaultDigitsTextSize;
168
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800169 private boolean mAreEmergencyDialerShortcutsEnabled;
170
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 @Override
172 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
173 // Do nothing
174 }
175
176 @Override
177 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700178 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179 }
180
181 @Override
182 public void afterTextChanged(Editable input) {
183 // Check for special sequences, in particular the "**04" or "**05"
184 // sequences that allow you to enter PIN or PUK-related codes.
185 //
186 // But note we *don't* allow most other special sequences here,
187 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
188 // since those shouldn't be available if the device is locked.
189 //
190 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
191 // here, not the regular handleChars() method.
192 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
193 // A special sequence was entered, clear the digits
194 mDigits.getText().clear();
195 }
196
197 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800198 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 }
200
201 @Override
202 protected void onCreate(Bundle icicle) {
203 super.onCreate(icicle);
204
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800206 setShowWhenLocked(true);
207 // Allow turning screen on
208 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209
Lucas Dupineb9c5702017-05-10 16:57:09 -0700210 mColorExtractor = new ColorExtractor(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700211 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
212 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700213 updateTheme(lockScreenColors.supportsDarkText());
214
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 setContentView(R.layout.emergency_dialer);
216
Tyler Gunnc5428972018-03-28 14:15:34 -0700217 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218 mDigits.setKeyListener(DialerKeyListener.getInstance());
219 mDigits.setOnClickListener(this);
220 mDigits.setOnKeyListener(this);
221 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700222 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700223 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 maybeAddNumberFormatting();
225
Lucas Dupineb9c5702017-05-10 16:57:09 -0700226 mBackgroundGradient = new GradientDrawable(this);
227 Point displaySize = new Point();
228 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
229 .getDefaultDisplay().getSize(displaySize);
230 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
231 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
232 getWindow().setBackgroundDrawable(mBackgroundGradient);
233
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700234 // Check for the presence of the keypad
235 View view = findViewById(R.id.one);
236 if (view != null) {
237 setupKeypad();
238 }
239
240 mDelete = findViewById(R.id.deleteButton);
241 mDelete.setOnClickListener(this);
242 mDelete.setOnLongClickListener(this);
243
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700244 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245
246 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700247 // Read carrier config through the public API because PhoneGlobals is not available when we
248 // run as a secondary user.
249 CarrierConfigManager configMgr =
250 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
251 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800252 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700253
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700254 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 mDialButton.setOnClickListener(this);
256 } else {
257 mDialButton.setVisibility(View.GONE);
258 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700259 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
260 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
261 maybeShowWfcEmergencyCallingWarning();
262
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700263 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264
265 if (icicle != null) {
266 super.onRestoreInstanceState(icicle);
267 }
268
269 // Extract phone number from intent
270 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700271 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
273 if (number != null) {
274 mDigits.setText(number);
275 }
276 }
277
278 // if the mToneGenerator creation fails, just continue without it. It is
279 // a local audio signal, and is not as important as the dtmf tone itself.
280 synchronized (mToneGeneratorLock) {
281 if (mToneGenerator == null) {
282 try {
283 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
284 } catch (RuntimeException e) {
285 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
286 mToneGenerator = null;
287 }
288 }
289 }
290
291 final IntentFilter intentFilter = new IntentFilter();
292 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
293 registerReceiver(mBroadcastReceiver, intentFilter);
294
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700295 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800296
297 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
298 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
299
300 if (mAreEmergencyDialerShortcutsEnabled) {
301 setupEmergencyShortcutsView();
302 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 }
304
305 @Override
306 protected void onDestroy() {
307 super.onDestroy();
308 synchronized (mToneGeneratorLock) {
309 if (mToneGenerator != null) {
310 mToneGenerator.release();
311 mToneGenerator = null;
312 }
313 }
314 unregisterReceiver(mBroadcastReceiver);
315 }
316
317 @Override
318 protected void onRestoreInstanceState(Bundle icicle) {
319 mLastNumber = icicle.getString(LAST_NUMBER);
320 }
321
322 @Override
323 protected void onSaveInstanceState(Bundle outState) {
324 super.onSaveInstanceState(outState);
325 outState.putString(LAST_NUMBER, mLastNumber);
326 }
327
328 /**
329 * Explicitly turn off number formatting, since it gets in the way of the emergency
330 * number detector
331 */
332 protected void maybeAddNumberFormatting() {
333 // Do nothing.
334 }
335
336 @Override
337 protected void onPostCreate(Bundle savedInstanceState) {
338 super.onPostCreate(savedInstanceState);
339
340 // This can't be done in onCreate(), since the auto-restoring of the digits
341 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
342 // is called. This method will be called every time the activity is created, and
343 // will always happen after onRestoreSavedInstanceState().
344 mDigits.addTextChangedListener(this);
345 }
346
347 private void setupKeypad() {
348 // Setup the listeners for the buttons
349 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700350 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
351 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 }
353
354 View view = findViewById(R.id.zero);
355 view.setOnLongClickListener(this);
356 }
357
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800358 @Override
359 public void onBackPressed() {
360 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
361 // back key will back to display FasterEmergencyDialer view.
362 // Otherwise, it would finish the activity.
363 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
364 && mDialpadView.getVisibility() == View.VISIBLE) {
365 switchView(mEmergencyShortcutView, mDialpadView, true);
366 return;
367 }
368 super.onBackPressed();
369 }
370
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371 /**
372 * handle key events
373 */
374 @Override
375 public boolean onKeyDown(int keyCode, KeyEvent event) {
376 switch (keyCode) {
377 // Happen when there's a "Call" hard button.
378 case KeyEvent.KEYCODE_CALL: {
379 if (TextUtils.isEmpty(mDigits.getText().toString())) {
380 // if we are adding a call from the InCallScreen and the phone
381 // number entered is empty, we just close the dialer to expose
382 // the InCallScreen under it.
383 finish();
384 } else {
385 // otherwise, we place the call.
386 placeCall();
387 }
388 return true;
389 }
390 }
391 return super.onKeyDown(keyCode, event);
392 }
393
394 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700395 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
397 mDigits.onKeyDown(keyCode, event);
398 }
399
400 @Override
401 public boolean onKey(View view, int keyCode, KeyEvent event) {
402 switch (view.getId()) {
403 case R.id.digits:
404 // Happen when "Done" button of the IME is pressed. This can happen when this
405 // Activity is forced into landscape mode due to a desk dock.
406 if (keyCode == KeyEvent.KEYCODE_ENTER
407 && event.getAction() == KeyEvent.ACTION_UP) {
408 placeCall();
409 return true;
410 }
411 break;
412 }
413 return false;
414 }
415
416 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700417 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800418 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700419 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800420 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700421 return handled;
422 }
423
424 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800425 public void onConfirmClick(EmergencyShortcutButton button) {
426 if (button == null) return;
427
428 String phoneNumber = button.getPhoneNumber();
429
430 if (!TextUtils.isEmpty(phoneNumber)) {
431 Log.d(LOG_TAG, "dial emergency number: " + phoneNumber);
432 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
433 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
434 } else {
435 Log.d(LOG_TAG, "emergency number is empty");
436 }
437 }
438
439 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700440 public void onClick(View view) {
441 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700442 case R.id.deleteButton: {
443 keyPressed(KeyEvent.KEYCODE_DEL);
444 return;
445 }
446 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700447 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700448 placeCall();
449 return;
450 }
451 case R.id.digits: {
452 if (mDigits.length() != 0) {
453 mDigits.setCursorVisible(true);
454 }
455 return;
456 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800457 case R.id.floating_action_button_dialpad: {
458 switchView(mDialpadView, mEmergencyShortcutView, true);
459 return;
460 }
461 case R.id.emergency_info_button: {
462 Intent intent = (Intent) view.getTag(R.id.tag_intent);
463 if (intent != null) {
464 startActivity(intent);
465 }
466 return;
467 }
Yorke Lee23a70732014-08-14 17:12:01 -0700468 }
469 }
470
471 @Override
472 public void onPressed(View view, boolean pressed) {
473 if (!pressed) {
474 return;
475 }
476 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700477 case R.id.one: {
478 playTone(ToneGenerator.TONE_DTMF_1);
479 keyPressed(KeyEvent.KEYCODE_1);
480 return;
481 }
482 case R.id.two: {
483 playTone(ToneGenerator.TONE_DTMF_2);
484 keyPressed(KeyEvent.KEYCODE_2);
485 return;
486 }
487 case R.id.three: {
488 playTone(ToneGenerator.TONE_DTMF_3);
489 keyPressed(KeyEvent.KEYCODE_3);
490 return;
491 }
492 case R.id.four: {
493 playTone(ToneGenerator.TONE_DTMF_4);
494 keyPressed(KeyEvent.KEYCODE_4);
495 return;
496 }
497 case R.id.five: {
498 playTone(ToneGenerator.TONE_DTMF_5);
499 keyPressed(KeyEvent.KEYCODE_5);
500 return;
501 }
502 case R.id.six: {
503 playTone(ToneGenerator.TONE_DTMF_6);
504 keyPressed(KeyEvent.KEYCODE_6);
505 return;
506 }
507 case R.id.seven: {
508 playTone(ToneGenerator.TONE_DTMF_7);
509 keyPressed(KeyEvent.KEYCODE_7);
510 return;
511 }
512 case R.id.eight: {
513 playTone(ToneGenerator.TONE_DTMF_8);
514 keyPressed(KeyEvent.KEYCODE_8);
515 return;
516 }
517 case R.id.nine: {
518 playTone(ToneGenerator.TONE_DTMF_9);
519 keyPressed(KeyEvent.KEYCODE_9);
520 return;
521 }
522 case R.id.zero: {
523 playTone(ToneGenerator.TONE_DTMF_0);
524 keyPressed(KeyEvent.KEYCODE_0);
525 return;
526 }
527 case R.id.pound: {
528 playTone(ToneGenerator.TONE_DTMF_P);
529 keyPressed(KeyEvent.KEYCODE_POUND);
530 return;
531 }
532 case R.id.star: {
533 playTone(ToneGenerator.TONE_DTMF_S);
534 keyPressed(KeyEvent.KEYCODE_STAR);
535 return;
536 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700537 }
538 }
539
540 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 * called for long touch events
542 */
543 @Override
544 public boolean onLongClick(View view) {
545 int id = view.getId();
546 switch (id) {
547 case R.id.deleteButton: {
548 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 return true;
550 }
551 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700552 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700553 keyPressed(KeyEvent.KEYCODE_PLUS);
554 return true;
555 }
556 }
557 return false;
558 }
559
560 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700561 protected void onStart() {
562 super.onStart();
563
564 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700565 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
566 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700567 // Do not animate when view isn't visible yet, just set an initial state.
568 mBackgroundGradient.setColors(lockScreenColors, false);
569 updateTheme(lockScreenColors.supportsDarkText());
570 }
571
572 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 protected void onResume() {
574 super.onResume();
575
576 // retrieve the DTMF tone play back setting.
577 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
578 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
579
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 // if the mToneGenerator creation fails, just continue without it. It is
581 // a local audio signal, and is not as important as the dtmf tone itself.
582 synchronized (mToneGeneratorLock) {
583 if (mToneGenerator == null) {
584 try {
585 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
586 TONE_RELATIVE_VOLUME);
587 } catch (RuntimeException e) {
588 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
589 mToneGenerator = null;
590 }
591 }
592 }
593
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 updateDialAndDeleteButtonStateEnabledAttr();
595 }
596
597 @Override
598 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 }
601
Lucas Dupineb9c5702017-05-10 16:57:09 -0700602 @Override
603 protected void onStop() {
604 super.onStop();
605
606 mColorExtractor.removeOnColorsChangedListener(this);
607 }
608
609 /**
610 * Sets theme based on gradient colors
611 * @param supportsDarkText true if gradient supports dark text
612 */
613 private void updateTheme(boolean supportsDarkText) {
614 if (mSupportsDarkText == supportsDarkText) {
615 return;
616 }
617 mSupportsDarkText = supportsDarkText;
618
619 // We can't change themes after inflation, in this case we'll have to recreate
620 // the whole activity.
621 if (mBackgroundGradient != null) {
622 recreate();
623 return;
624 }
625
626 int vis = getWindow().getDecorView().getSystemUiVisibility();
627 if (supportsDarkText) {
628 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
629 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
630 setTheme(R.style.EmergencyDialerThemeDark);
631 } else {
632 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
633 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
634 setTheme(R.style.EmergencyDialerTheme);
635 }
636 getWindow().getDecorView().setSystemUiVisibility(vis);
637 }
638
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700639 /**
640 * place the call, but check to make sure it is a viable number.
641 */
642 private void placeCall() {
643 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900644
645 // Convert into emergency number according to emergency conversion map.
646 // If conversion map is not defined (this is default), this method does
647 // nothing and just returns input number.
648 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
649
Yorke Lee36bb2542014-06-05 08:09:52 -0700650 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700651 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
652
653 // place the call if it is a valid number
654 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
655 // There is no number entered.
656 playTone(ToneGenerator.TONE_PROP_NACK);
657 return;
658 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700659 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
660 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700661 } else {
662 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
663
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
665 }
Yorke Lee9b341512014-10-17 11:36:41 -0700666 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 }
668
669 /**
670 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
671 *
672 * The tone is played locally, using the audio stream for phone calls.
673 * Tones are played only if the "Audible touch tones" user preference
674 * is checked, and are NOT played if the device is in silent mode.
675 *
676 * @param tone a tone code from {@link ToneGenerator}
677 */
678 void playTone(int tone) {
679 // if local tone playback is disabled, just return.
680 if (!mDTMFToneEnabled) {
681 return;
682 }
683
684 // Also do nothing if the phone is in silent mode.
685 // We need to re-check the ringer mode for *every* playTone()
686 // call, rather than keeping a local flag that's updated in
687 // onResume(), since it's possible to toggle silent mode without
688 // leaving the current activity (via the ENDCALL-longpress menu.)
689 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
690 int ringerMode = audioManager.getRingerMode();
691 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
692 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
693 return;
694 }
695
696 synchronized (mToneGeneratorLock) {
697 if (mToneGenerator == null) {
698 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
699 return;
700 }
701
702 // Start the new tone (will stop any playing tone)
703 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
704 }
705 }
706
707 private CharSequence createErrorMessage(String number) {
708 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800709 String errorString = getString(R.string.dial_emergency_error, number);
710 int startingPosition = errorString.indexOf(number);
711 int endingPosition = startingPosition + number.length();
712 Spannable result = new SpannableString(errorString);
713 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
714 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 } else {
716 return getText(R.string.dial_emergency_empty_error).toString();
717 }
718 }
719
720 @Override
721 protected Dialog onCreateDialog(int id) {
722 AlertDialog dialog = null;
723 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
724 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700725 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
727 .setMessage(createErrorMessage(mLastNumber))
728 .setPositiveButton(R.string.ok, null)
729 .setCancelable(true).create();
730
731 // blur stuff behind the dialog
732 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700733 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 }
735 return dialog;
736 }
737
738 @Override
739 protected void onPrepareDialog(int id, Dialog dialog) {
740 super.onPrepareDialog(id, dialog);
741 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
742 AlertDialog alert = (AlertDialog) dialog;
743 alert.setMessage(createErrorMessage(mLastNumber));
744 }
745 }
746
Andrew Leed5631e82014-10-08 16:03:58 -0700747 @Override
748 public boolean onOptionsItemSelected(MenuItem item) {
749 final int itemId = item.getItemId();
750 if (itemId == android.R.id.home) {
751 onBackPressed();
752 return true;
753 }
754 return super.onOptionsItemSelected(item);
755 }
756
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700757 /**
758 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
759 */
760 private void updateDialAndDeleteButtonStateEnabledAttr() {
761 final boolean notEmpty = mDigits.length() != 0;
762
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700763 mDelete.setEnabled(notEmpty);
764 }
Yorke Lee91311662014-10-24 14:50:45 -0700765
766 /**
767 * Remove the digit just before the current position. Used by various long pressed callbacks
768 * to remove the digit that was populated as a result of the short click.
769 */
770 private void removePreviousDigitIfPossible() {
771 final int currentPosition = mDigits.getSelectionStart();
772 if (currentPosition > 0) {
773 mDigits.setSelection(currentPosition);
774 mDigits.getText().delete(currentPosition - 1, currentPosition);
775 }
776 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800777
778 /**
779 * Update the text-to-speech annotations in the edit field.
780 */
781 private void updateTtsSpans() {
782 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
783 mDigits.getText().removeSpan(o);
784 }
785 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
786 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700787
788 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700789 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700790 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700791 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
792 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700793 mBackgroundGradient.setColors(colors);
794 updateTheme(colors.supportsDarkText());
795 }
796 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700797
798 /**
799 * Where a carrier requires a warning that emergency calling is not available while on WFC,
800 * add hint text above the dial pad which warns the user of this case.
801 */
802 private void maybeShowWfcEmergencyCallingWarning() {
803 if (!mIsWfcEmergencyCallingWarningEnabled) {
804 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
805 return;
806 }
807
808 // Use an async task rather than calling into Telephony on UI thread.
809 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
810 @Override
811 protected Boolean doInBackground(Void... voids) {
812 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
813 boolean isWfcAvailable = tm.isWifiCallingAvailable();
814 ServiceState ss = tm.getServiceState();
815 boolean isCellAvailable =
816 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
817 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
818 + " isCellAvailable=" + isCellAvailable
819 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
820 return isWfcAvailable && !isCellAvailable;
821 }
822
823 @Override
824 protected void onPostExecute(Boolean result) {
825 if (result.booleanValue()) {
826 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
827 mDigits.setHint(R.string.dial_emergency_calling_not_available);
828 } else {
829 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
830 mDigits.setHint("");
831 }
832 maybeChangeHintSize();
833 }
834 };
835 showWfcWarningTask.execute((Void) null);
836 }
837
838 /**
839 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
840 * edit view and set the font size to a smaller size appropriate for the emergency calling
841 * warning.
842 */
843 private void maybeChangeHintSize() {
844 if (TextUtils.isEmpty(mDigits.getHint())
845 || !TextUtils.isEmpty(mDigits.getText().toString())) {
846 // No hint or there are dialed digits, so use default size.
847 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
848 // By default, the digits view auto-resizes to fit the text it contains, so
849 // enable that now.
850 mDigits.setResizeEnabled(true);
851 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
852 } else {
853 // Hint present and no dialed digits, set custom font size appropriate for the warning.
854 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
855 R.dimen.emergency_call_warning_size));
856 // Since we're populating this with a static text string, disable auto-resize.
857 mDigits.setResizeEnabled(false);
858 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
859 }
860 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800861
862 private void setupEmergencyShortcutsView() {
863 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
864 mDialpadView = findViewById(R.id.emergency_dialer);
865
866 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
867 dialpadButton.setOnClickListener(this);
868
869 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
870 emergencyInfoButton.setOnClickListener(this);
871
872 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
873 mEmergencyActionGroup.setVisibility(View.GONE);
874
875 // Setup dialpad title.
876 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
877 emergencyDialpadTitle.setVisibility(View.VISIBLE);
878
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800879 // TODO: Integrating emergency phone number table will get location information.
880 // Using null to present no location status.
881 setLocationInfo(null);
882
883 mEmergencyShortcutButtonList = new ArrayList<>();
884 setupEmergencyCallShortcutButton();
885
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800886 switchView(mEmergencyShortcutView, mDialpadView, false);
887 }
888
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800889 private void setLocationInfo(String country) {
890 final View locationInfo = findViewById(R.id.location_info);
891
892 if (TextUtils.isEmpty(country)) {
893 locationInfo.setVisibility(View.INVISIBLE);
894 } else {
895 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
896 location.setText(country);
897 locationInfo.setVisibility(View.VISIBLE);
898 }
899 }
900
901 // TODO: Integrate emergency phone number table.
902 // Using default layout(no location, phone number is 112, description is Emergency,
903 // and icon is cross shape) until integrating emergency phone number table.
904 private void setupEmergencyCallShortcutButton() {
905 final ViewGroup shortcutButtonContainer = findViewById(
906 R.id.emergency_shortcut_buttons_container);
907 shortcutButtonContainer.setClipToOutline(true);
908
909 final EmergencyShortcutButton button =
910 (EmergencyShortcutButton) getLayoutInflater().inflate(
911 R.layout.emergency_shortcut_button,
912 shortcutButtonContainer, false);
913
914 button.setPhoneNumber("112");
915 button.setPhoneDescription("Emergency");
916 button.setPhoneTypeIcon(R.drawable.ic_emergency_number_24);
917 button.setOnConfirmClickListener(this);
918
919 shortcutButtonContainer.addView(button);
920 mEmergencyShortcutButtonList.add(button);
921
922 //Set emergency number title for numerous buttons.
923 if (shortcutButtonContainer.getChildCount() > 1) {
924 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
925 emergencyNumberTitle.setText(getString(R.string.numerous_emergency_numbers_title));
926 }
927 }
928
929 /**
930 * Called by the activity before a touch event is dispatched to the view hierarchy.
931 */
932 private void onPreTouchEvent(MotionEvent event) {
933 mEmergencyActionGroup.onPreTouchEvent(event);
934
935 if (mEmergencyShortcutButtonList != null) {
936 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
937 button.onPreTouchEvent(event);
938 }
939 }
940 }
941
942 /**
943 * Called by the activity after a touch event is dispatched to the view hierarchy.
944 */
945 private void onPostTouchEvent(MotionEvent event) {
946 mEmergencyActionGroup.onPostTouchEvent(event);
947
948 if (mEmergencyShortcutButtonList != null) {
949 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
950 button.onPostTouchEvent(event);
951 }
952 }
953 }
954
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800955 /**
956 * Switch two view.
957 *
958 * @param displayView the view would be displayed.
959 * @param hideView the view would be hidden.
960 * @param hasAnimation is {@code true} when the view should be displayed with animation.
961 */
962 private void switchView(View displayView, View hideView, boolean hasAnimation) {
963 if (displayView == null || hideView == null) {
964 return;
965 }
966
967 if (displayView.getVisibility() == View.VISIBLE) {
968 return;
969 }
970
971 if (hasAnimation) {
972 crossfade(hideView, displayView);
973 } else {
974 hideView.setVisibility(View.GONE);
975 displayView.setVisibility(View.VISIBLE);
976 }
977 }
978
979 /**
980 * Fade out and fade in animation between two view transition.
981 */
982 private void crossfade(View fadeOutView, View fadeInView) {
983 if (fadeOutView == null || fadeInView == null) {
984 return;
985 }
986 final int shortAnimationDuration = getResources().getInteger(
987 android.R.integer.config_shortAnimTime);
988
989 fadeInView.setAlpha(0f);
990 fadeInView.setVisibility(View.VISIBLE);
991
992 fadeInView.animate()
993 .alpha(1f)
994 .setDuration(shortAnimationDuration)
995 .setListener(null);
996
997 fadeOutView.animate()
998 .alpha(0f)
999 .setDuration(shortAnimationDuration)
1000 .setListener(new AnimatorListenerAdapter() {
1001 @Override
1002 public void onAnimationEnd(Animator animation) {
1003 fadeOutView.setVisibility(View.GONE);
1004 }
1005 });
1006 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007}