blob: 6b3dc9ef74f148dbaae20c414857ff6f25caa1f0 [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;
Grace Jia835d8e62020-04-01 15:47:39 -070023import android.annotation.ColorInt;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.app.Activity;
25import android.app.AlertDialog;
26import android.app.Dialog;
Michelef6a5ea22019-10-15 16:08:54 -070027import android.app.WallpaperColors;
Lucas Dupineb9c5702017-05-10 16:57:09 -070028import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.content.BroadcastReceiver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080033import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080034import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070035import android.graphics.Point;
Michelef6a5ea22019-10-15 16:08:54 -070036import android.graphics.drawable.ColorDrawable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.media.AudioManager;
38import android.media.ToneGenerator;
39import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070040import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070042import android.os.PersistableBundle;
Jack Yu58d04bd2024-09-10 17:32:39 -070043import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070045import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070046import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070047import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.telephony.PhoneNumberUtils;
Tyler Gunnc5428972018-03-28 14:15:34 -070049import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070050import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070053import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080054import android.text.Spannable;
55import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.text.TextUtils;
57import android.text.TextWatcher;
58import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080059import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070061import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070062import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070064import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070065import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080067import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080068import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080070import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080071import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072
Yorke Lee23a70732014-08-14 17:12:01 -070073import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070074import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070075import com.android.phone.common.widget.ResizingTextEditText;
Grace Jia835d8e62020-04-01 15:47:39 -070076import com.android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Chihhang Chuang92cfe512018-06-07 16:25:27 +080078import java.util.ArrayList;
79import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080080import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080081
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082/**
83 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
84 *
85 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
86 * activity from apps/Contacts) that:
Leo Hsu83ab2332018-12-18 15:20:45 +080087 * 1. Allows ONLY emergency calls to be dialed
88 * 2. Disallows voicemail functionality
89 * 3. Allows this activity to stay in front of the keyguard.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090 *
91 * TODO: Even though this is an ultra-simplified version of the normal
92 * dialer, there's still lots of code duplication between this class and
93 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
94 * moved into a shared base class that would live in the framework?
95 * Or could we figure out some way to move *this* class into apps/Contacts
96 * also?
97 */
98public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070099 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Michelef6a5ea22019-10-15 16:08:54 -0700100 DialpadKeyButton.OnPressedListener,
101 WallpaperManager.OnColorsChangedListener,
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800102 EmergencyShortcutButton.OnConfirmClickListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800103 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-07-04 16:53:01 +0800104
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105 // Keys used with onSaveInstanceState().
106 private static final String LAST_NUMBER = "lastNumber";
107
108 // Intent action for this activity.
109 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
110
Shaotang Li8662a912018-07-04 16:53:01 +0800111 /**
112 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
113 * the emergency dialer.
114 */
115 public static final String EXTRA_ENTRY_TYPE =
116 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
117
Leo Hsue3512b62019-02-14 15:53:00 +0800118 // Constants indicating the entry type that user opened emergency dialer.
119 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
120 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
121 public static final int ENTRY_TYPE_UNKNOWN = 0;
122 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
123 public static final int ENTRY_TYPE_POWER_MENU = 2;
124
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125 // List of dialer button IDs.
Leo Hsu83ab2332018-12-18 15:20:45 +0800126 private static final int[] DIALER_KEYS = new int[]{
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 R.id.one, R.id.two, R.id.three,
128 R.id.four, R.id.five, R.id.six,
129 R.id.seven, R.id.eight, R.id.nine,
Leo Hsu83ab2332018-12-18 15:20:45 +0800130 R.id.star, R.id.zero, R.id.pound};
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131
132 // Debug constants.
133 private static final boolean DBG = false;
134 private static final String LOG_TAG = "EmergencyDialer";
135
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136 /** The length of DTMF tones in milliseconds */
137 private static final int TONE_LENGTH_MS = 150;
138
139 /** The DTMF tone volume relative to other sounds in the stream */
140 private static final int TONE_RELATIVE_VOLUME = 80;
141
142 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
143 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
144
145 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
146
Lucas Dupineb9c5702017-05-10 16:57:09 -0700147 /** 90% opacity, different from other gradients **/
148 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
149
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800150 /** 85% opacity for black background **/
151 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
152
CY Cheng9a69c182018-06-15 21:20:10 +0800153 /** Size limit of emergency shortcut buttons container. **/
154 private static final int SHORTCUT_SIZE_LIMIT = 3;
155
Grace Jia835d8e62020-04-01 15:47:39 -0700156 private static final float COLOR_DELTA = 1.0f / 16.0f;
157
158 /** Dial button color, from packages/apps/PhoneCommon/res/drawable-mdpi/fab_green.png **/
159 @ColorInt private static final int DIALER_GREEN = 0xff00c853;
160
Tyler Gunnc5428972018-03-28 14:15:34 -0700161 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 private View mDialButton;
163 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800164 private View mEmergencyShortcutView;
165 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800167 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800168 private EccShortcutAdapter mShortcutAdapter;
169 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800170
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 private ToneGenerator mToneGenerator;
172 private Object mToneGeneratorLock = new Object();
173
174 // determines if we want to playback local DTMF tones.
175 private boolean mDTMFToneEnabled;
176
Fan Zhang39d81f52022-03-21 22:47:21 +0000177 private EmergencyInfoGroup mEmergencyInfoInDialpad;
178 private EmergencyInfoGroup mEmergencyInfoInShortcut;
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800179
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 // close activity when screen turns off
181 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
182 @Override
183 public void onReceive(Context context, Intent intent) {
184 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700185 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 }
187 }
188 };
189
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800190 /**
191 * Customize accessibility methods in View.
192 */
193 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
194
195 /**
196 * Stop AccessiblityService from reading the title of a hidden View.
197 *
198 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
199 * in the animation end. The view with an accessibility pane title would call the
200 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
201 * accessibility service to read the pane title of fade out view instead of pane title of
202 * fade in view. So it need to filter out the event called by vanished pane.
203 */
204 @Override
205 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
206 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
207 && host.getVisibility() == View.GONE) {
208 return;
209 }
210 super.onPopulateAccessibilityEvent(host, event);
211 }
212 };
213
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
215
Lucas Dupineb9c5702017-05-10 16:57:09 -0700216 // Background gradient
Michelef6a5ea22019-10-15 16:08:54 -0700217 private ColorDrawable mBackgroundDrawable;
Lucas Dupineb9c5702017-05-10 16:57:09 -0700218 private boolean mSupportsDarkText;
219
Tyler Gunnc5428972018-03-28 14:15:34 -0700220 private boolean mIsWfcEmergencyCallingWarningEnabled;
221 private float mDefaultDigitsTextSize;
222
Leo Hsue3512b62019-02-14 15:53:00 +0800223 private int mEntryType;
Leo Hsubc7553a2019-03-05 15:47:23 +0800224 private ShortcutViewUtils.Config mShortcutViewConfig;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800225
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 @Override
227 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
228 // Do nothing
229 }
230
231 @Override
232 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700233 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700234 }
235
236 @Override
237 public void afterTextChanged(Editable input) {
238 // Check for special sequences, in particular the "**04" or "**05"
239 // sequences that allow you to enter PIN or PUK-related codes.
240 //
241 // But note we *don't* allow most other special sequences here,
242 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
243 // since those shouldn't be available if the device is locked.
244 //
245 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
246 // here, not the regular handleChars() method.
247 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
248 // A special sequence was entered, clear the digits
249 mDigits.getText().clear();
250 }
251
252 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800253 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 }
255
256 @Override
257 protected void onCreate(Bundle icicle) {
258 super.onCreate(icicle);
259
Tyler Gunn662cb392025-01-17 23:34:46 +0000260 getWindow().addSystemFlags(
261 android.view.WindowManager.LayoutParams
262 .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
263
Leo Hsue3512b62019-02-14 15:53:00 +0800264 mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
265 Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
266
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800267 // Allow turning screen on
268 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269
Chuck Liaocdeebb42018-10-30 12:07:18 +0800270 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
Tomasz Wasilczyk8919d702024-04-01 10:58:20 -0700271 PersistableBundle carrierConfig = configMgr == null ? null :
Chuck Liaocdeebb42018-10-30 12:07:18 +0800272 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
273
Leo Hsubc7553a2019-03-05 15:47:23 +0800274 mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
CY Cheng9a69c182018-06-15 21:20:10 +0800275 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsubc7553a2019-03-05 15:47:23 +0800276 + mShortcutViewConfig.isEnabled());
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800277
Leo Hsubc7553a2019-03-05 15:47:23 +0800278 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800279 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800280 updateTheme(false);
281 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700282 WallpaperColors wallpaperColors =
283 getWallpaperManager().getWallpaperColors(WallpaperManager.FLAG_LOCK);
284 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800285 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700286
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 setContentView(R.layout.emergency_dialer);
288
Tyler Gunnc5428972018-03-28 14:15:34 -0700289 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 mDigits.setKeyListener(DialerKeyListener.getInstance());
291 mDigits.setOnClickListener(this);
292 mDigits.setOnKeyListener(this);
293 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700294 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700295 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 maybeAddNumberFormatting();
297
Michelef6a5ea22019-10-15 16:08:54 -0700298 mBackgroundDrawable = new ColorDrawable();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700299 Point displaySize = new Point();
300 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
301 .getDefaultDisplay().getSize(displaySize);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700302 mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled()
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800303 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700304 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700305
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 // Check for the presence of the keypad
307 View view = findViewById(R.id.one);
308 if (view != null) {
309 setupKeypad();
310 }
311
312 mDelete = findViewById(R.id.deleteButton);
313 mDelete.setOnClickListener(this);
314 mDelete.setOnLongClickListener(this);
315
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700316 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317
318 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700319 // Read carrier config through the public API because PhoneGlobals is not available when we
320 // run as a secondary user.
Tomasz Wasilczyk8919d702024-04-01 10:58:20 -0700321 if (carrierConfig != null
322 && carrierConfig.getBoolean(
323 CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700324 mDialButton.setOnClickListener(this);
325 } else {
326 mDialButton.setVisibility(View.GONE);
327 }
Tomasz Wasilczyk8919d702024-04-01 10:58:20 -0700328 mIsWfcEmergencyCallingWarningEnabled = carrierConfig != null && carrierConfig.getInt(
Tyler Gunnc5428972018-03-28 14:15:34 -0700329 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
330 maybeShowWfcEmergencyCallingWarning();
331
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700332 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333
334 if (icicle != null) {
335 super.onRestoreInstanceState(icicle);
336 }
337
338 // Extract phone number from intent
339 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700340 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
342 if (number != null) {
343 mDigits.setText(number);
344 }
345 }
346
347 // if the mToneGenerator creation fails, just continue without it. It is
348 // a local audio signal, and is not as important as the dtmf tone itself.
349 synchronized (mToneGeneratorLock) {
350 if (mToneGenerator == null) {
351 try {
352 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
353 } catch (RuntimeException e) {
354 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
355 mToneGenerator = null;
356 }
357 }
358 }
359
360 final IntentFilter intentFilter = new IntentFilter();
361 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
362 registerReceiver(mBroadcastReceiver, intentFilter);
363
Fan Zhang39d81f52022-03-21 22:47:21 +0000364 mEmergencyInfoInDialpad = findViewById(R.id.emergency_dialer)
365 .findViewById(R.id.emergency_info_button);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800366
Fan Zhang39d81f52022-03-21 22:47:21 +0000367 mEmergencyInfoInShortcut = findViewById(R.id.emergency_dialer_shortcuts)
368 .findViewById(R.id.emergency_info_button);
369
370 setupEmergencyDialpadViews();
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800371
Leo Hsubc7553a2019-03-05 15:47:23 +0800372 if (mShortcutViewConfig.isEnabled()) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800373 setupEmergencyShortcutsView();
374 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 }
376
377 @Override
378 protected void onDestroy() {
379 super.onDestroy();
380 synchronized (mToneGeneratorLock) {
381 if (mToneGenerator != null) {
382 mToneGenerator.release();
383 mToneGenerator = null;
384 }
385 }
386 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800387 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
388 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
389 mShortcutDataSetObserver = null;
390 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391 }
392
393 @Override
394 protected void onRestoreInstanceState(Bundle icicle) {
395 mLastNumber = icicle.getString(LAST_NUMBER);
396 }
397
398 @Override
399 protected void onSaveInstanceState(Bundle outState) {
400 super.onSaveInstanceState(outState);
401 outState.putString(LAST_NUMBER, mLastNumber);
402 }
403
404 /**
405 * Explicitly turn off number formatting, since it gets in the way of the emergency
406 * number detector
407 */
408 protected void maybeAddNumberFormatting() {
409 // Do nothing.
410 }
411
412 @Override
413 protected void onPostCreate(Bundle savedInstanceState) {
414 super.onPostCreate(savedInstanceState);
415
416 // This can't be done in onCreate(), since the auto-restoring of the digits
417 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
418 // is called. This method will be called every time the activity is created, and
419 // will always happen after onRestoreSavedInstanceState().
420 mDigits.addTextChangedListener(this);
421 }
422
423 private void setupKeypad() {
424 // Setup the listeners for the buttons
425 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700426 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
427 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700428 }
429
430 View view = findViewById(R.id.zero);
431 view.setOnLongClickListener(this);
432 }
433
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800434 @Override
435 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800436 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
437 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
Leo Hsubc7553a2019-03-05 15:47:23 +0800438 if (mShortcutViewConfig.isEnabled() && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800439 && mDialpadView.getVisibility() == View.VISIBLE) {
440 switchView(mEmergencyShortcutView, mDialpadView, true);
441 return;
442 }
443 super.onBackPressed();
444 }
445
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700446 /**
447 * handle key events
448 */
449 @Override
450 public boolean onKeyDown(int keyCode, KeyEvent event) {
451 switch (keyCode) {
452 // Happen when there's a "Call" hard button.
453 case KeyEvent.KEYCODE_CALL: {
454 if (TextUtils.isEmpty(mDigits.getText().toString())) {
455 // if we are adding a call from the InCallScreen and the phone
456 // number entered is empty, we just close the dialer to expose
457 // the InCallScreen under it.
458 finish();
459 } else {
460 // otherwise, we place the call.
461 placeCall();
462 }
463 return true;
464 }
465 }
466 return super.onKeyDown(keyCode, event);
467 }
468
469 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700470 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700471 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
472 mDigits.onKeyDown(keyCode, event);
473 }
474
475 @Override
476 public boolean onKey(View view, int keyCode, KeyEvent event) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800477 if (view.getId()
478 == R.id.digits) { // Happen when "Done" button of the IME is pressed. This can
479 // happen when this
480 // Activity is forced into landscape mode due to a desk dock.
481 if (keyCode == KeyEvent.KEYCODE_ENTER
482 && event.getAction() == KeyEvent.ACTION_UP) {
483 placeCall();
484 return true;
485 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486 }
487 return false;
488 }
489
490 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700491 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800492 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700493 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800494 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700495 return handled;
496 }
497
498 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800499 public void onConfirmClick(EmergencyShortcutButton button) {
500 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800501 String phoneNumber = button.getPhoneNumber();
502
503 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800504 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800505
Hall Liu6f35de92020-01-23 14:29:26 -0800506 placeCall(phoneNumber, TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800507 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800508 } else {
509 Log.d(LOG_TAG, "emergency number is empty");
510 }
511 }
512
513 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800514 public void onConfirmClick(EmergencyInfoGroup button) {
515 if (button == null) return;
516
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800517 Intent intent = (Intent) button.getTag(R.id.tag_intent);
518 if (intent != null) {
Jack Yu58d04bd2024-09-10 17:32:39 -0700519 startActivityAsUser(intent, UserHandle.CURRENT);
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800520 }
521 }
522
523 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524 public void onClick(View view) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800525 if (view.getId() == R.id.deleteButton) {
526 keyPressed(KeyEvent.KEYCODE_DEL);
527 return;
528 } else if (view.getId() == R.id.floating_action_button) {
529 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
530 placeCall();
531 return;
532 } else if (view.getId() == R.id.digits) {
533 if (mDigits.length() != 0) {
534 mDigits.setCursorVisible(true);
Yorke Lee23a70732014-08-14 17:12:01 -0700535 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800536 return;
537 } else if (view.getId() == R.id.floating_action_button_dialpad) {
538 mDigits.getText().clear();
539 switchView(mDialpadView, mEmergencyShortcutView, true);
540 return;
Yorke Lee23a70732014-08-14 17:12:01 -0700541 }
542 }
543
544 @Override
545 public void onPressed(View view, boolean pressed) {
546 if (!pressed) {
547 return;
548 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800549 if (view.getId() == R.id.one) {
550 playTone(ToneGenerator.TONE_DTMF_1);
551 keyPressed(KeyEvent.KEYCODE_1);
552 return;
553 } else if (view.getId() == R.id.two) {
554 playTone(ToneGenerator.TONE_DTMF_2);
555 keyPressed(KeyEvent.KEYCODE_2);
556 return;
557 } else if (view.getId() == R.id.three) {
558 playTone(ToneGenerator.TONE_DTMF_3);
559 keyPressed(KeyEvent.KEYCODE_3);
560 return;
561 } else if (view.getId() == R.id.four) {
562 playTone(ToneGenerator.TONE_DTMF_4);
563 keyPressed(KeyEvent.KEYCODE_4);
564 return;
565 } else if (view.getId() == R.id.five) {
566 playTone(ToneGenerator.TONE_DTMF_5);
567 keyPressed(KeyEvent.KEYCODE_5);
568 return;
569 } else if (view.getId() == R.id.six) {
570 playTone(ToneGenerator.TONE_DTMF_6);
571 keyPressed(KeyEvent.KEYCODE_6);
572 return;
573 } else if (view.getId() == R.id.seven) {
574 playTone(ToneGenerator.TONE_DTMF_7);
575 keyPressed(KeyEvent.KEYCODE_7);
576 return;
577 } else if (view.getId() == R.id.eight) {
578 playTone(ToneGenerator.TONE_DTMF_8);
579 keyPressed(KeyEvent.KEYCODE_8);
580 return;
581 } else if (view.getId() == R.id.nine) {
582 playTone(ToneGenerator.TONE_DTMF_9);
583 keyPressed(KeyEvent.KEYCODE_9);
584 return;
585 } else if (view.getId() == R.id.zero) {
586 playTone(ToneGenerator.TONE_DTMF_0);
587 keyPressed(KeyEvent.KEYCODE_0);
588 return;
589 } else if (view.getId() == R.id.pound) {
590 playTone(ToneGenerator.TONE_DTMF_P);
591 keyPressed(KeyEvent.KEYCODE_POUND);
592 return;
593 } else if (view.getId() == R.id.star) {
594 playTone(ToneGenerator.TONE_DTMF_S);
595 keyPressed(KeyEvent.KEYCODE_STAR);
596 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700597 }
598 }
599
600 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700601 * called for long touch events
602 */
603 @Override
604 public boolean onLongClick(View view) {
605 int id = view.getId();
Amit Mahajan28a499c2019-11-20 15:13:42 -0800606 if (id == R.id.deleteButton) {
607 mDigits.getText().clear();
608 return true;
609 } else if (id == R.id.zero) {
610 removePreviousDigitIfPossible();
611 keyPressed(KeyEvent.KEYCODE_PLUS);
612 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700613 }
614 return false;
615 }
616
617 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700618 protected void onStart() {
619 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800620
Leo Hsubc7553a2019-03-05 15:47:23 +0800621 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800622 // Shortcut view doesn't support dark text theme.
Michelef6a5ea22019-10-15 16:08:54 -0700623 mBackgroundDrawable.setColor(Color.BLACK);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800624 updateTheme(false);
625 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700626 WallpaperManager wallpaperManager = getWallpaperManager();
627 if (wallpaperManager.isWallpaperSupported()) {
628 wallpaperManager.addOnColorsChangedListener(this, null);
629 }
630
631 WallpaperColors wallpaperColors =
632 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK);
633 mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors));
634 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800635 }
CY Cheng9a69c182018-06-15 21:20:10 +0800636
Leo Hsubc7553a2019-03-05 15:47:23 +0800637 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800638 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800639 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700640 }
641
642 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700643 protected void onResume() {
644 super.onResume();
645
646 // retrieve the DTMF tone play back setting.
647 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
648 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
649
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 // if the mToneGenerator creation fails, just continue without it. It is
651 // a local audio signal, and is not as important as the dtmf tone itself.
652 synchronized (mToneGeneratorLock) {
653 if (mToneGenerator == null) {
654 try {
655 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
656 TONE_RELATIVE_VOLUME);
657 } catch (RuntimeException e) {
658 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
659 mToneGenerator = null;
660 }
661 }
662 }
663
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 updateDialAndDeleteButtonStateEnabledAttr();
665 }
666
667 @Override
668 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700670 }
671
Lucas Dupineb9c5702017-05-10 16:57:09 -0700672 @Override
673 protected void onStop() {
674 super.onStop();
Michelef6a5ea22019-10-15 16:08:54 -0700675
676 WallpaperManager wallpaperManager = getWallpaperManager();
677 if (wallpaperManager.isWallpaperSupported()) {
678 wallpaperManager.removeOnColorsChangedListener(this);
679 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700680 }
681
682 /**
683 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800684 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700685 * @param supportsDarkText true if gradient supports dark text
686 */
687 private void updateTheme(boolean supportsDarkText) {
688 if (mSupportsDarkText == supportsDarkText) {
689 return;
690 }
691 mSupportsDarkText = supportsDarkText;
692
693 // We can't change themes after inflation, in this case we'll have to recreate
694 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700695 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700696 recreate();
697 return;
698 }
699
700 int vis = getWindow().getDecorView().getSystemUiVisibility();
701 if (supportsDarkText) {
702 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
703 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
704 setTheme(R.style.EmergencyDialerThemeDark);
705 } else {
706 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
707 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
708 setTheme(R.style.EmergencyDialerTheme);
709 }
710 getWindow().getDecorView().setSystemUiVisibility(vis);
711 }
712
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 /**
714 * place the call, but check to make sure it is a viable number.
715 */
716 private void placeCall() {
717 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900718
719 // Convert into emergency number according to emergency conversion map.
720 // If conversion map is not defined (this is default), this method does
721 // nothing and just returns input number.
722 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
723
Leo Hsubc7553a2019-03-05 15:47:23 +0800724 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800725 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800726 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800727 isEmergencyNumber = true;
728 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800729 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
730 // If a number from SIM/network/... is categoried as police/ambulance/fire,
731 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
732 // shortcut button because a number provided by database has higher priority.
733 isEmergencyNumber = true;
734 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsubc7553a2019-03-05 15:47:23 +0800735 } else {
Automerger Merge Worker96417f12020-02-27 19:34:58 +0000736 try {
737 isEmergencyNumber = getSystemService(TelephonyManager.class)
738 .isEmergencyNumber(mLastNumber);
739 } catch (IllegalStateException ise) {
740 isEmergencyNumber = false;
741 }
Leo Hsu43d670a2018-12-04 15:40:36 +0800742 }
743
744 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700745 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
746
747 // place the call if it is a valid number
748 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
749 // There is no number entered.
750 playTone(ToneGenerator.TONE_PROP_NACK);
751 return;
752 }
Shaotang Li8662a912018-07-04 16:53:01 +0800753
Hall Liu6f35de92020-01-23 14:29:26 -0800754 placeCall(mLastNumber, TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD,
Leo Hsu43d670a2018-12-04 15:40:36 +0800755 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700756 } else {
757 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
758
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700759 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
760 }
Yorke Lee9b341512014-10-17 11:36:41 -0700761 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 }
763
Leo Hsu43d670a2018-12-04 15:40:36 +0800764 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800765 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
766 + ", entry = " + entryTypeToString(mEntryType));
767
Leo Hsu43d670a2018-12-04 15:40:36 +0800768 Bundle extras = new Bundle();
769 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800770 /**
771 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
772 * when the dialed number is ambiguous and identified as both emergency number and any
773 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
774 * number in a country and a non-emergency number of a carrier's customer service hotline.
775 */
776 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800777
778 if (phone != null && phone.getPhoneAccountHandle() != null) {
779 // Requests to dial through the specified phone.
780 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
781 phone.getPhoneAccountHandle());
782 }
783
784 TelecomManager tm = this.getSystemService(TelecomManager.class);
785 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
786 }
787
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 /**
789 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
790 *
791 * The tone is played locally, using the audio stream for phone calls.
792 * Tones are played only if the "Audible touch tones" user preference
793 * is checked, and are NOT played if the device is in silent mode.
794 *
795 * @param tone a tone code from {@link ToneGenerator}
796 */
797 void playTone(int tone) {
798 // if local tone playback is disabled, just return.
799 if (!mDTMFToneEnabled) {
800 return;
801 }
802
803 // Also do nothing if the phone is in silent mode.
804 // We need to re-check the ringer mode for *every* playTone()
805 // call, rather than keeping a local flag that's updated in
806 // onResume(), since it's possible to toggle silent mode without
807 // leaving the current activity (via the ENDCALL-longpress menu.)
808 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
809 int ringerMode = audioManager.getRingerMode();
810 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800811 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700812 return;
813 }
814
815 synchronized (mToneGeneratorLock) {
816 if (mToneGenerator == null) {
817 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
818 return;
819 }
820
821 // Start the new tone (will stop any playing tone)
822 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
823 }
824 }
825
826 private CharSequence createErrorMessage(String number) {
827 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800828 String errorString = getString(R.string.dial_emergency_error, number);
829 int startingPosition = errorString.indexOf(number);
830 int endingPosition = startingPosition + number.length();
831 Spannable result = new SpannableString(errorString);
832 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
833 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 } else {
835 return getText(R.string.dial_emergency_empty_error).toString();
836 }
837 }
838
839 @Override
840 protected Dialog onCreateDialog(int id) {
841 AlertDialog dialog = null;
842 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
843 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700844 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
846 .setMessage(createErrorMessage(mLastNumber))
847 .setPositiveButton(R.string.ok, null)
848 .setCancelable(true).create();
849
850 // blur stuff behind the dialog
851 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700852 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 }
854 return dialog;
855 }
856
857 @Override
858 protected void onPrepareDialog(int id, Dialog dialog) {
859 super.onPrepareDialog(id, dialog);
860 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
861 AlertDialog alert = (AlertDialog) dialog;
862 alert.setMessage(createErrorMessage(mLastNumber));
863 }
864 }
865
Andrew Leed5631e82014-10-08 16:03:58 -0700866 @Override
867 public boolean onOptionsItemSelected(MenuItem item) {
868 final int itemId = item.getItemId();
869 if (itemId == android.R.id.home) {
870 onBackPressed();
871 return true;
872 }
873 return super.onOptionsItemSelected(item);
874 }
875
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700876 /**
877 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
878 */
879 private void updateDialAndDeleteButtonStateEnabledAttr() {
880 final boolean notEmpty = mDigits.length() != 0;
881
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 mDelete.setEnabled(notEmpty);
883 }
Yorke Lee91311662014-10-24 14:50:45 -0700884
885 /**
886 * Remove the digit just before the current position. Used by various long pressed callbacks
887 * to remove the digit that was populated as a result of the short click.
888 */
889 private void removePreviousDigitIfPossible() {
890 final int currentPosition = mDigits.getSelectionStart();
891 if (currentPosition > 0) {
892 mDigits.setSelection(currentPosition);
893 mDigits.getText().delete(currentPosition - 1, currentPosition);
894 }
895 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800896
897 /**
898 * Update the text-to-speech annotations in the edit field.
899 */
900 private void updateTtsSpans() {
901 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
902 mDigits.getText().removeSpan(o);
903 }
904 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
905 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700906
907 @Override
Michelef6a5ea22019-10-15 16:08:54 -0700908 public void onColorsChanged(WallpaperColors colors, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700909 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Michelef6a5ea22019-10-15 16:08:54 -0700910 mBackgroundDrawable.setColor(getPrimaryColor(colors));
911 updateTheme(supportsDarkText(colors));
Lucas Dupineb9c5702017-05-10 16:57:09 -0700912 }
913 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700914
915 /**
916 * Where a carrier requires a warning that emergency calling is not available while on WFC,
917 * add hint text above the dial pad which warns the user of this case.
918 */
919 private void maybeShowWfcEmergencyCallingWarning() {
920 if (!mIsWfcEmergencyCallingWarningEnabled) {
921 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
922 return;
923 }
924
925 // Use an async task rather than calling into Telephony on UI thread.
926 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
927 @Override
928 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800929 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700930 boolean isWfcAvailable = tm.isWifiCallingAvailable();
931 ServiceState ss = tm.getServiceState();
932 boolean isCellAvailable =
933 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
934 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800935 + " isCellAvailable=" + isCellAvailable
936 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700937 return isWfcAvailable && !isCellAvailable;
938 }
939
940 @Override
941 protected void onPostExecute(Boolean result) {
942 if (result.booleanValue()) {
943 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
944 mDigits.setHint(R.string.dial_emergency_calling_not_available);
945 } else {
946 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
947 mDigits.setHint("");
948 }
949 maybeChangeHintSize();
950 }
951 };
952 showWfcWarningTask.execute((Void) null);
953 }
954
955 /**
956 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
957 * edit view and set the font size to a smaller size appropriate for the emergency calling
958 * warning.
959 */
960 private void maybeChangeHintSize() {
961 if (TextUtils.isEmpty(mDigits.getHint())
962 || !TextUtils.isEmpty(mDigits.getText().toString())) {
963 // No hint or there are dialed digits, so use default size.
964 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
965 // By default, the digits view auto-resizes to fit the text it contains, so
966 // enable that now.
967 mDigits.setResizeEnabled(true);
968 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
969 } else {
970 // Hint present and no dialed digits, set custom font size appropriate for the warning.
971 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
972 R.dimen.emergency_call_warning_size));
973 // Since we're populating this with a static text string, disable auto-resize.
974 mDigits.setResizeEnabled(false);
975 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
976 }
977 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800978
Fan Zhang39d81f52022-03-21 22:47:21 +0000979 private void setupEmergencyDialpadViews() {
980 mEmergencyInfoInDialpad.setOnConfirmClickListener(this);
981 }
982
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800983 private void setupEmergencyShortcutsView() {
984 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
985 mDialpadView = findViewById(R.id.emergency_dialer);
986
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800987 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
988 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
989
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800990 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
991 dialpadButton.setOnClickListener(this);
992
Fan Zhang39d81f52022-03-21 22:47:21 +0000993 mEmergencyInfoInShortcut.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800994
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800995 mEmergencyShortcutButtonList = new ArrayList<>();
996 setupEmergencyCallShortcutButton();
997
Leo Hsu43d670a2018-12-04 15:40:36 +0800998 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800999
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001000 switchView(mEmergencyShortcutView, mDialpadView, false);
1001 }
1002
Leo Hsu43d670a2018-12-04 15:40:36 +08001003 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001004 final View locationInfo = findViewById(R.id.location_info);
1005
Leo Hsubc7553a2019-03-05 15:47:23 +08001006 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +08001007 String countryName = null;
1008 if (!TextUtils.isEmpty(countryIso)) {
1009 Locale locale = Locale.getDefault();
1010 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1011 .getDisplayCountry();
1012 }
1013 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001014 locationInfo.setVisibility(View.INVISIBLE);
1015 } else {
1016 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001017 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001018 locationInfo.setVisibility(View.VISIBLE);
1019 }
1020 }
1021
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001022 private void setupEmergencyCallShortcutButton() {
1023 final ViewGroup shortcutButtonContainer = findViewById(
1024 R.id.emergency_shortcut_buttons_container);
1025 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001026 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001027
CY Cheng9a69c182018-06-15 21:20:10 +08001028 mShortcutAdapter = new EccShortcutAdapter(this) {
1029 @Override
1030 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1031 CharSequence description, int iconRes) {
1032 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1033 .inflate(R.layout.emergency_shortcut_button, parent, false);
1034 button.setPhoneNumber(number);
1035 button.setPhoneDescription(description);
1036 button.setPhoneTypeIcon(iconRes);
1037 button.setOnConfirmClickListener(EmergencyDialer.this);
1038 return button;
1039 }
1040 };
1041 mShortcutDataSetObserver = new DataSetObserver() {
1042 @Override
1043 public void onChanged() {
1044 super.onChanged();
1045 updateLayout();
1046 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001047
CY Cheng9a69c182018-06-15 21:20:10 +08001048 @Override
1049 public void onInvalidated() {
1050 super.onInvalidated();
1051 updateLayout();
1052 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001053
CY Cheng9a69c182018-06-15 21:20:10 +08001054 private void updateLayout() {
1055 // clear previous added buttons
1056 shortcutButtonContainer.removeAllViews();
1057 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001058
CY Cheng9a69c182018-06-15 21:20:10 +08001059 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1060 EmergencyShortcutButton button = (EmergencyShortcutButton)
1061 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1062 mEmergencyShortcutButtonList.add(button);
1063 shortcutButtonContainer.addView(button);
1064 }
1065
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001066 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001067 if (mEmergencyShortcutButtonList.size() > 1) {
1068 emergencyNumberTitle.setText(getString(
1069 R.string.numerous_emergency_numbers_title));
1070 } else {
1071 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1072 }
1073 }
1074 };
1075 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1076 }
1077
Leo Hsu43d670a2018-12-04 15:40:36 +08001078 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001079 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001080 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001081 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001082 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001083 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001084 }
1085 }
1086
1087 /**
1088 * Called by the activity before a touch event is dispatched to the view hierarchy.
1089 */
1090 private void onPreTouchEvent(MotionEvent event) {
Fan Zhang39d81f52022-03-21 22:47:21 +00001091 mEmergencyInfoInDialpad.onPreTouchEvent(event);
1092 mEmergencyInfoInShortcut.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001093
1094 if (mEmergencyShortcutButtonList != null) {
1095 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1096 button.onPreTouchEvent(event);
1097 }
1098 }
1099 }
1100
1101 /**
1102 * Called by the activity after a touch event is dispatched to the view hierarchy.
1103 */
1104 private void onPostTouchEvent(MotionEvent event) {
Fan Zhang39d81f52022-03-21 22:47:21 +00001105 mEmergencyInfoInDialpad.onPostTouchEvent(event);
1106 mEmergencyInfoInShortcut.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001107
1108 if (mEmergencyShortcutButtonList != null) {
1109 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1110 button.onPostTouchEvent(event);
1111 }
1112 }
1113 }
1114
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001115 /**
1116 * Switch two view.
1117 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001118 * @param displayView the view would be displayed.
1119 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001120 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1121 */
1122 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1123 if (displayView == null || hideView == null) {
1124 return;
1125 }
1126
1127 if (displayView.getVisibility() == View.VISIBLE) {
1128 return;
1129 }
1130
1131 if (hasAnimation) {
1132 crossfade(hideView, displayView);
1133 } else {
1134 hideView.setVisibility(View.GONE);
1135 displayView.setVisibility(View.VISIBLE);
1136 }
1137 }
1138
1139 /**
1140 * Fade out and fade in animation between two view transition.
1141 */
1142 private void crossfade(View fadeOutView, View fadeInView) {
1143 if (fadeOutView == null || fadeInView == null) {
1144 return;
1145 }
1146 final int shortAnimationDuration = getResources().getInteger(
1147 android.R.integer.config_shortAnimTime);
1148
1149 fadeInView.setAlpha(0f);
1150 fadeInView.setVisibility(View.VISIBLE);
1151
1152 fadeInView.animate()
1153 .alpha(1f)
1154 .setDuration(shortAnimationDuration)
1155 .setListener(null);
1156
1157 fadeOutView.animate()
1158 .alpha(0f)
1159 .setDuration(shortAnimationDuration)
1160 .setListener(new AnimatorListenerAdapter() {
1161 @Override
1162 public void onAnimationEnd(Animator animation) {
1163 fadeOutView.setVisibility(View.GONE);
1164 }
1165 });
1166 }
Shaotang Li8662a912018-07-04 16:53:01 +08001167
Shaotang Li8662a912018-07-04 16:53:01 +08001168 private boolean isShortcutNumber(String number) {
1169 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1170 return false;
1171 }
1172
1173 boolean isShortcut = false;
1174 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1175 if (button != null && number.equals(button.getPhoneNumber())) {
1176 isShortcut = true;
1177 break;
1178 }
1179 }
1180 return isShortcut;
1181 }
Leo Hsue3512b62019-02-14 15:53:00 +08001182
1183 private String entryTypeToString(int entryType) {
1184 switch (entryType) {
1185 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1186 return "LockScreen";
1187 case ENTRY_TYPE_POWER_MENU:
1188 return "PowerMenu";
1189 default:
1190 return "Unknown-" + entryType;
1191 }
1192 }
1193
1194 private String callSourceToString(int callSource) {
1195 switch (callSource) {
Hall Liu6f35de92020-01-23 14:29:26 -08001196 case TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD:
Leo Hsue3512b62019-02-14 15:53:00 +08001197 return "DialPad";
Hall Liu6f35de92020-01-23 14:29:26 -08001198 case TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT:
Leo Hsue3512b62019-02-14 15:53:00 +08001199 return "Shortcut";
1200 default:
1201 return "Unknown-" + callSource;
1202 }
1203 }
Michelef6a5ea22019-10-15 16:08:54 -07001204
1205 private WallpaperManager getWallpaperManager() {
1206 return getSystemService(WallpaperManager.class);
1207 }
1208
1209 private static boolean supportsDarkText(WallpaperColors colors) {
1210 if (colors != null) {
1211 return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
1212 }
1213 // It's possible that wallpaper colors are null (e.g. when colors are being
1214 // processed or a live wallpaper is used). In this case, fallback to same
1215 // behavior as when shortcut view is enabled.
1216 return false;
1217 }
1218
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001219 private int getPrimaryColor(WallpaperColors colors) {
Michelef6a5ea22019-10-15 16:08:54 -07001220 if (colors != null) {
Grace Jia835d8e62020-04-01 15:47:39 -07001221 // Android accessibility scanner
1222 // (https://support.google.com/accessibility/android/answer/7158390)
1223 // suggest small text and graphics have a contrast ratio greater than
1224 // 4.5 with background color. The color generated from wallpaper may not
1225 // follow this rule. Calculate a proper color here.
1226 Color primary = colors.getPrimaryColor();
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001227 Color text;
1228 if (mSupportsDarkText) {
1229 text = Color.valueOf(Color.BLACK);
1230 } else {
1231 text = Color.valueOf(Color.WHITE);
1232 }
Grace Jia835d8e62020-04-01 15:47:39 -07001233 Color dial = Color.valueOf(DIALER_GREEN);
1234 // If current primary color can't follow the contrast ratio rule, make it
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001235 // deeper/lighter and try again.
Grace Jia835d8e62020-04-01 15:47:39 -07001236 while (!checkContrastRatio(primary, text)) {
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001237 primary = getNextColor(primary, mSupportsDarkText);
Grace Jia835d8e62020-04-01 15:47:39 -07001238 }
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001239 if (!mSupportsDarkText) {
1240 while (!checkContrastRatio(primary, dial)) {
1241 primary = getNextColor(primary, mSupportsDarkText);
1242 }
Grace Jia835d8e62020-04-01 15:47:39 -07001243 }
1244 return primary.toArgb();
Michelef6a5ea22019-10-15 16:08:54 -07001245 }
1246 // It's possible that wallpaper colors are null (e.g. when colors are being
1247 // processed or a live wallpaper is used). In this case, fallback to same
1248 // behavior as when shortcut view is enabled.
1249 return Color.BLACK;
1250 }
Grace Jia835d8e62020-04-01 15:47:39 -07001251
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001252 private Color getNextColor(Color color, boolean darkText) {
1253 float sign = darkText ? 1.f : -1.f;
1254 float r = color.red() + sign * COLOR_DELTA;
1255 float g = color.green() + sign * COLOR_DELTA;
1256 float b = color.blue() + sign * COLOR_DELTA;
Grace Jia835d8e62020-04-01 15:47:39 -07001257 if (r < 0f) r = 0f;
1258 if (g < 0f) g = 0f;
1259 if (b < 0f) b = 0f;
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001260 if (r > 1f) r = 1f;
1261 if (g > 1f) g = 1f;
1262 if (b > 1f) b = 1f;
Grace Jia835d8e62020-04-01 15:47:39 -07001263 return Color.valueOf(r, g, b);
1264 }
1265
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001266 private boolean checkContrastRatio(Color color1, Color color2) {
Grace Jia835d8e62020-04-01 15:47:39 -07001267 float lum1 = color1.luminance();
1268 float lum2 = color2.luminance();
1269 double cr;
1270 if (lum1 >= lum2) {
1271 cr = (lum1 + 0.05) / (lum2 + 0.05);
1272 } else {
1273 cr = (lum2 + 0.05) / (lum1 + 0.05);
1274 }
1275
1276 // Make cr greater than 5.0 instead of 4.5 to guarantee that transparent white
1277 // text and graphics can have contrast ratio greather than 4.5 with background.
1278 return cr > 5.0;
1279 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001280}