blob: d4fdca6b23250a31c3c30635ab788e35082e4b47 [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
Leo Hsue3512b62019-02-14 15:53:00 +0800260 mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
261 Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
262
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800263 // Allow turning screen on
264 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265
Chuck Liaocdeebb42018-10-30 12:07:18 +0800266 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
Tomasz Wasilczyk8919d702024-04-01 10:58:20 -0700267 PersistableBundle carrierConfig = configMgr == null ? null :
Chuck Liaocdeebb42018-10-30 12:07:18 +0800268 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
269
Leo Hsubc7553a2019-03-05 15:47:23 +0800270 mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
CY Cheng9a69c182018-06-15 21:20:10 +0800271 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsubc7553a2019-03-05 15:47:23 +0800272 + mShortcutViewConfig.isEnabled());
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800273
Leo Hsubc7553a2019-03-05 15:47:23 +0800274 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800275 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800276 updateTheme(false);
277 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700278 WallpaperColors wallpaperColors =
279 getWallpaperManager().getWallpaperColors(WallpaperManager.FLAG_LOCK);
280 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800281 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700282
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283 setContentView(R.layout.emergency_dialer);
284
Tyler Gunnc5428972018-03-28 14:15:34 -0700285 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 mDigits.setKeyListener(DialerKeyListener.getInstance());
287 mDigits.setOnClickListener(this);
288 mDigits.setOnKeyListener(this);
289 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700290 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700291 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700292 maybeAddNumberFormatting();
293
Michelef6a5ea22019-10-15 16:08:54 -0700294 mBackgroundDrawable = new ColorDrawable();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700295 Point displaySize = new Point();
296 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
297 .getDefaultDisplay().getSize(displaySize);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700298 mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled()
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800299 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700300 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700301
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 // Check for the presence of the keypad
303 View view = findViewById(R.id.one);
304 if (view != null) {
305 setupKeypad();
306 }
307
308 mDelete = findViewById(R.id.deleteButton);
309 mDelete.setOnClickListener(this);
310 mDelete.setOnLongClickListener(this);
311
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700312 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313
314 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700315 // Read carrier config through the public API because PhoneGlobals is not available when we
316 // run as a secondary user.
Tomasz Wasilczyk8919d702024-04-01 10:58:20 -0700317 if (carrierConfig != null
318 && carrierConfig.getBoolean(
319 CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 mDialButton.setOnClickListener(this);
321 } else {
322 mDialButton.setVisibility(View.GONE);
323 }
Tomasz Wasilczyk8919d702024-04-01 10:58:20 -0700324 mIsWfcEmergencyCallingWarningEnabled = carrierConfig != null && carrierConfig.getInt(
Tyler Gunnc5428972018-03-28 14:15:34 -0700325 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
326 maybeShowWfcEmergencyCallingWarning();
327
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700328 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329
330 if (icicle != null) {
331 super.onRestoreInstanceState(icicle);
332 }
333
334 // Extract phone number from intent
335 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700336 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
338 if (number != null) {
339 mDigits.setText(number);
340 }
341 }
342
343 // if the mToneGenerator creation fails, just continue without it. It is
344 // a local audio signal, and is not as important as the dtmf tone itself.
345 synchronized (mToneGeneratorLock) {
346 if (mToneGenerator == null) {
347 try {
348 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
349 } catch (RuntimeException e) {
350 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
351 mToneGenerator = null;
352 }
353 }
354 }
355
356 final IntentFilter intentFilter = new IntentFilter();
357 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
358 registerReceiver(mBroadcastReceiver, intentFilter);
359
Fan Zhang39d81f52022-03-21 22:47:21 +0000360 mEmergencyInfoInDialpad = findViewById(R.id.emergency_dialer)
361 .findViewById(R.id.emergency_info_button);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800362
Fan Zhang39d81f52022-03-21 22:47:21 +0000363 mEmergencyInfoInShortcut = findViewById(R.id.emergency_dialer_shortcuts)
364 .findViewById(R.id.emergency_info_button);
365
366 setupEmergencyDialpadViews();
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800367
Leo Hsubc7553a2019-03-05 15:47:23 +0800368 if (mShortcutViewConfig.isEnabled()) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800369 setupEmergencyShortcutsView();
370 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371 }
372
373 @Override
374 protected void onDestroy() {
375 super.onDestroy();
376 synchronized (mToneGeneratorLock) {
377 if (mToneGenerator != null) {
378 mToneGenerator.release();
379 mToneGenerator = null;
380 }
381 }
382 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800383 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
384 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
385 mShortcutDataSetObserver = null;
386 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 }
388
389 @Override
390 protected void onRestoreInstanceState(Bundle icicle) {
391 mLastNumber = icicle.getString(LAST_NUMBER);
392 }
393
394 @Override
395 protected void onSaveInstanceState(Bundle outState) {
396 super.onSaveInstanceState(outState);
397 outState.putString(LAST_NUMBER, mLastNumber);
398 }
399
400 /**
401 * Explicitly turn off number formatting, since it gets in the way of the emergency
402 * number detector
403 */
404 protected void maybeAddNumberFormatting() {
405 // Do nothing.
406 }
407
408 @Override
409 protected void onPostCreate(Bundle savedInstanceState) {
410 super.onPostCreate(savedInstanceState);
411
412 // This can't be done in onCreate(), since the auto-restoring of the digits
413 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
414 // is called. This method will be called every time the activity is created, and
415 // will always happen after onRestoreSavedInstanceState().
416 mDigits.addTextChangedListener(this);
417 }
418
419 private void setupKeypad() {
420 // Setup the listeners for the buttons
421 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700422 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
423 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700424 }
425
426 View view = findViewById(R.id.zero);
427 view.setOnLongClickListener(this);
428 }
429
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800430 @Override
431 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800432 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
433 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
Leo Hsubc7553a2019-03-05 15:47:23 +0800434 if (mShortcutViewConfig.isEnabled() && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800435 && mDialpadView.getVisibility() == View.VISIBLE) {
436 switchView(mEmergencyShortcutView, mDialpadView, true);
437 return;
438 }
439 super.onBackPressed();
440 }
441
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700442 /**
443 * handle key events
444 */
445 @Override
446 public boolean onKeyDown(int keyCode, KeyEvent event) {
447 switch (keyCode) {
448 // Happen when there's a "Call" hard button.
449 case KeyEvent.KEYCODE_CALL: {
450 if (TextUtils.isEmpty(mDigits.getText().toString())) {
451 // if we are adding a call from the InCallScreen and the phone
452 // number entered is empty, we just close the dialer to expose
453 // the InCallScreen under it.
454 finish();
455 } else {
456 // otherwise, we place the call.
457 placeCall();
458 }
459 return true;
460 }
461 }
462 return super.onKeyDown(keyCode, event);
463 }
464
465 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700466 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
468 mDigits.onKeyDown(keyCode, event);
469 }
470
471 @Override
472 public boolean onKey(View view, int keyCode, KeyEvent event) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800473 if (view.getId()
474 == R.id.digits) { // Happen when "Done" button of the IME is pressed. This can
475 // happen when this
476 // Activity is forced into landscape mode due to a desk dock.
477 if (keyCode == KeyEvent.KEYCODE_ENTER
478 && event.getAction() == KeyEvent.ACTION_UP) {
479 placeCall();
480 return true;
481 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700482 }
483 return false;
484 }
485
486 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700487 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800488 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700489 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800490 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700491 return handled;
492 }
493
494 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800495 public void onConfirmClick(EmergencyShortcutButton button) {
496 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800497 String phoneNumber = button.getPhoneNumber();
498
499 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800500 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800501
Hall Liu6f35de92020-01-23 14:29:26 -0800502 placeCall(phoneNumber, TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800503 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800504 } else {
505 Log.d(LOG_TAG, "emergency number is empty");
506 }
507 }
508
509 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800510 public void onConfirmClick(EmergencyInfoGroup button) {
511 if (button == null) return;
512
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800513 Intent intent = (Intent) button.getTag(R.id.tag_intent);
514 if (intent != null) {
Jack Yu58d04bd2024-09-10 17:32:39 -0700515 startActivityAsUser(intent, UserHandle.CURRENT);
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800516 }
517 }
518
519 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700520 public void onClick(View view) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800521 if (view.getId() == R.id.deleteButton) {
522 keyPressed(KeyEvent.KEYCODE_DEL);
523 return;
524 } else if (view.getId() == R.id.floating_action_button) {
525 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
526 placeCall();
527 return;
528 } else if (view.getId() == R.id.digits) {
529 if (mDigits.length() != 0) {
530 mDigits.setCursorVisible(true);
Yorke Lee23a70732014-08-14 17:12:01 -0700531 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800532 return;
533 } else if (view.getId() == R.id.floating_action_button_dialpad) {
534 mDigits.getText().clear();
535 switchView(mDialpadView, mEmergencyShortcutView, true);
536 return;
Yorke Lee23a70732014-08-14 17:12:01 -0700537 }
538 }
539
540 @Override
541 public void onPressed(View view, boolean pressed) {
542 if (!pressed) {
543 return;
544 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800545 if (view.getId() == R.id.one) {
546 playTone(ToneGenerator.TONE_DTMF_1);
547 keyPressed(KeyEvent.KEYCODE_1);
548 return;
549 } else if (view.getId() == R.id.two) {
550 playTone(ToneGenerator.TONE_DTMF_2);
551 keyPressed(KeyEvent.KEYCODE_2);
552 return;
553 } else if (view.getId() == R.id.three) {
554 playTone(ToneGenerator.TONE_DTMF_3);
555 keyPressed(KeyEvent.KEYCODE_3);
556 return;
557 } else if (view.getId() == R.id.four) {
558 playTone(ToneGenerator.TONE_DTMF_4);
559 keyPressed(KeyEvent.KEYCODE_4);
560 return;
561 } else if (view.getId() == R.id.five) {
562 playTone(ToneGenerator.TONE_DTMF_5);
563 keyPressed(KeyEvent.KEYCODE_5);
564 return;
565 } else if (view.getId() == R.id.six) {
566 playTone(ToneGenerator.TONE_DTMF_6);
567 keyPressed(KeyEvent.KEYCODE_6);
568 return;
569 } else if (view.getId() == R.id.seven) {
570 playTone(ToneGenerator.TONE_DTMF_7);
571 keyPressed(KeyEvent.KEYCODE_7);
572 return;
573 } else if (view.getId() == R.id.eight) {
574 playTone(ToneGenerator.TONE_DTMF_8);
575 keyPressed(KeyEvent.KEYCODE_8);
576 return;
577 } else if (view.getId() == R.id.nine) {
578 playTone(ToneGenerator.TONE_DTMF_9);
579 keyPressed(KeyEvent.KEYCODE_9);
580 return;
581 } else if (view.getId() == R.id.zero) {
582 playTone(ToneGenerator.TONE_DTMF_0);
583 keyPressed(KeyEvent.KEYCODE_0);
584 return;
585 } else if (view.getId() == R.id.pound) {
586 playTone(ToneGenerator.TONE_DTMF_P);
587 keyPressed(KeyEvent.KEYCODE_POUND);
588 return;
589 } else if (view.getId() == R.id.star) {
590 playTone(ToneGenerator.TONE_DTMF_S);
591 keyPressed(KeyEvent.KEYCODE_STAR);
592 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 }
594 }
595
596 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700597 * called for long touch events
598 */
599 @Override
600 public boolean onLongClick(View view) {
601 int id = view.getId();
Amit Mahajan28a499c2019-11-20 15:13:42 -0800602 if (id == R.id.deleteButton) {
603 mDigits.getText().clear();
604 return true;
605 } else if (id == R.id.zero) {
606 removePreviousDigitIfPossible();
607 keyPressed(KeyEvent.KEYCODE_PLUS);
608 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700609 }
610 return false;
611 }
612
613 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700614 protected void onStart() {
615 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800616
Leo Hsubc7553a2019-03-05 15:47:23 +0800617 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800618 // Shortcut view doesn't support dark text theme.
Michelef6a5ea22019-10-15 16:08:54 -0700619 mBackgroundDrawable.setColor(Color.BLACK);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800620 updateTheme(false);
621 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700622 WallpaperManager wallpaperManager = getWallpaperManager();
623 if (wallpaperManager.isWallpaperSupported()) {
624 wallpaperManager.addOnColorsChangedListener(this, null);
625 }
626
627 WallpaperColors wallpaperColors =
628 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK);
629 mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors));
630 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800631 }
CY Cheng9a69c182018-06-15 21:20:10 +0800632
Leo Hsubc7553a2019-03-05 15:47:23 +0800633 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800634 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800635 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700636 }
637
638 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700639 protected void onResume() {
640 super.onResume();
641
642 // retrieve the DTMF tone play back setting.
643 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
644 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
645
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700646 // if the mToneGenerator creation fails, just continue without it. It is
647 // a local audio signal, and is not as important as the dtmf tone itself.
648 synchronized (mToneGeneratorLock) {
649 if (mToneGenerator == null) {
650 try {
651 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
652 TONE_RELATIVE_VOLUME);
653 } catch (RuntimeException e) {
654 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
655 mToneGenerator = null;
656 }
657 }
658 }
659
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700660 updateDialAndDeleteButtonStateEnabledAttr();
661 }
662
663 @Override
664 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700665 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700666 }
667
Lucas Dupineb9c5702017-05-10 16:57:09 -0700668 @Override
669 protected void onStop() {
670 super.onStop();
Michelef6a5ea22019-10-15 16:08:54 -0700671
672 WallpaperManager wallpaperManager = getWallpaperManager();
673 if (wallpaperManager.isWallpaperSupported()) {
674 wallpaperManager.removeOnColorsChangedListener(this);
675 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700676 }
677
678 /**
679 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800680 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700681 * @param supportsDarkText true if gradient supports dark text
682 */
683 private void updateTheme(boolean supportsDarkText) {
684 if (mSupportsDarkText == supportsDarkText) {
685 return;
686 }
687 mSupportsDarkText = supportsDarkText;
688
689 // We can't change themes after inflation, in this case we'll have to recreate
690 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700691 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700692 recreate();
693 return;
694 }
695
696 int vis = getWindow().getDecorView().getSystemUiVisibility();
697 if (supportsDarkText) {
698 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
699 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
700 setTheme(R.style.EmergencyDialerThemeDark);
701 } else {
702 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
703 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
704 setTheme(R.style.EmergencyDialerTheme);
705 }
706 getWindow().getDecorView().setSystemUiVisibility(vis);
707 }
708
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700709 /**
710 * place the call, but check to make sure it is a viable number.
711 */
712 private void placeCall() {
713 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900714
715 // Convert into emergency number according to emergency conversion map.
716 // If conversion map is not defined (this is default), this method does
717 // nothing and just returns input number.
718 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
719
Leo Hsubc7553a2019-03-05 15:47:23 +0800720 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800721 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800722 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800723 isEmergencyNumber = true;
724 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800725 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
726 // If a number from SIM/network/... is categoried as police/ambulance/fire,
727 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
728 // shortcut button because a number provided by database has higher priority.
729 isEmergencyNumber = true;
730 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsubc7553a2019-03-05 15:47:23 +0800731 } else {
Automerger Merge Worker96417f12020-02-27 19:34:58 +0000732 try {
733 isEmergencyNumber = getSystemService(TelephonyManager.class)
734 .isEmergencyNumber(mLastNumber);
735 } catch (IllegalStateException ise) {
736 isEmergencyNumber = false;
737 }
Leo Hsu43d670a2018-12-04 15:40:36 +0800738 }
739
740 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
742
743 // place the call if it is a valid number
744 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
745 // There is no number entered.
746 playTone(ToneGenerator.TONE_PROP_NACK);
747 return;
748 }
Shaotang Li8662a912018-07-04 16:53:01 +0800749
Hall Liu6f35de92020-01-23 14:29:26 -0800750 placeCall(mLastNumber, TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD,
Leo Hsu43d670a2018-12-04 15:40:36 +0800751 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700752 } else {
753 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
754
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
756 }
Yorke Lee9b341512014-10-17 11:36:41 -0700757 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 }
759
Leo Hsu43d670a2018-12-04 15:40:36 +0800760 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800761 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
762 + ", entry = " + entryTypeToString(mEntryType));
763
Leo Hsu43d670a2018-12-04 15:40:36 +0800764 Bundle extras = new Bundle();
765 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800766 /**
767 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
768 * when the dialed number is ambiguous and identified as both emergency number and any
769 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
770 * number in a country and a non-emergency number of a carrier's customer service hotline.
771 */
772 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800773
774 if (phone != null && phone.getPhoneAccountHandle() != null) {
775 // Requests to dial through the specified phone.
776 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
777 phone.getPhoneAccountHandle());
778 }
779
780 TelecomManager tm = this.getSystemService(TelecomManager.class);
781 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
782 }
783
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 /**
785 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
786 *
787 * The tone is played locally, using the audio stream for phone calls.
788 * Tones are played only if the "Audible touch tones" user preference
789 * is checked, and are NOT played if the device is in silent mode.
790 *
791 * @param tone a tone code from {@link ToneGenerator}
792 */
793 void playTone(int tone) {
794 // if local tone playback is disabled, just return.
795 if (!mDTMFToneEnabled) {
796 return;
797 }
798
799 // Also do nothing if the phone is in silent mode.
800 // We need to re-check the ringer mode for *every* playTone()
801 // call, rather than keeping a local flag that's updated in
802 // onResume(), since it's possible to toggle silent mode without
803 // leaving the current activity (via the ENDCALL-longpress menu.)
804 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
805 int ringerMode = audioManager.getRingerMode();
806 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800807 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 return;
809 }
810
811 synchronized (mToneGeneratorLock) {
812 if (mToneGenerator == null) {
813 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
814 return;
815 }
816
817 // Start the new tone (will stop any playing tone)
818 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
819 }
820 }
821
822 private CharSequence createErrorMessage(String number) {
823 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800824 String errorString = getString(R.string.dial_emergency_error, number);
825 int startingPosition = errorString.indexOf(number);
826 int endingPosition = startingPosition + number.length();
827 Spannable result = new SpannableString(errorString);
828 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
829 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 } else {
831 return getText(R.string.dial_emergency_empty_error).toString();
832 }
833 }
834
835 @Override
836 protected Dialog onCreateDialog(int id) {
837 AlertDialog dialog = null;
838 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
839 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700840 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
842 .setMessage(createErrorMessage(mLastNumber))
843 .setPositiveButton(R.string.ok, null)
844 .setCancelable(true).create();
845
846 // blur stuff behind the dialog
847 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700848 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700849 }
850 return dialog;
851 }
852
853 @Override
854 protected void onPrepareDialog(int id, Dialog dialog) {
855 super.onPrepareDialog(id, dialog);
856 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
857 AlertDialog alert = (AlertDialog) dialog;
858 alert.setMessage(createErrorMessage(mLastNumber));
859 }
860 }
861
Andrew Leed5631e82014-10-08 16:03:58 -0700862 @Override
863 public boolean onOptionsItemSelected(MenuItem item) {
864 final int itemId = item.getItemId();
865 if (itemId == android.R.id.home) {
866 onBackPressed();
867 return true;
868 }
869 return super.onOptionsItemSelected(item);
870 }
871
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 /**
873 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
874 */
875 private void updateDialAndDeleteButtonStateEnabledAttr() {
876 final boolean notEmpty = mDigits.length() != 0;
877
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 mDelete.setEnabled(notEmpty);
879 }
Yorke Lee91311662014-10-24 14:50:45 -0700880
881 /**
882 * Remove the digit just before the current position. Used by various long pressed callbacks
883 * to remove the digit that was populated as a result of the short click.
884 */
885 private void removePreviousDigitIfPossible() {
886 final int currentPosition = mDigits.getSelectionStart();
887 if (currentPosition > 0) {
888 mDigits.setSelection(currentPosition);
889 mDigits.getText().delete(currentPosition - 1, currentPosition);
890 }
891 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800892
893 /**
894 * Update the text-to-speech annotations in the edit field.
895 */
896 private void updateTtsSpans() {
897 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
898 mDigits.getText().removeSpan(o);
899 }
900 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
901 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700902
903 @Override
Michelef6a5ea22019-10-15 16:08:54 -0700904 public void onColorsChanged(WallpaperColors colors, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700905 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Michelef6a5ea22019-10-15 16:08:54 -0700906 mBackgroundDrawable.setColor(getPrimaryColor(colors));
907 updateTheme(supportsDarkText(colors));
Lucas Dupineb9c5702017-05-10 16:57:09 -0700908 }
909 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700910
911 /**
912 * Where a carrier requires a warning that emergency calling is not available while on WFC,
913 * add hint text above the dial pad which warns the user of this case.
914 */
915 private void maybeShowWfcEmergencyCallingWarning() {
916 if (!mIsWfcEmergencyCallingWarningEnabled) {
917 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
918 return;
919 }
920
921 // Use an async task rather than calling into Telephony on UI thread.
922 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
923 @Override
924 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800925 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700926 boolean isWfcAvailable = tm.isWifiCallingAvailable();
927 ServiceState ss = tm.getServiceState();
928 boolean isCellAvailable =
929 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
930 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800931 + " isCellAvailable=" + isCellAvailable
932 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700933 return isWfcAvailable && !isCellAvailable;
934 }
935
936 @Override
937 protected void onPostExecute(Boolean result) {
938 if (result.booleanValue()) {
939 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
940 mDigits.setHint(R.string.dial_emergency_calling_not_available);
941 } else {
942 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
943 mDigits.setHint("");
944 }
945 maybeChangeHintSize();
946 }
947 };
948 showWfcWarningTask.execute((Void) null);
949 }
950
951 /**
952 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
953 * edit view and set the font size to a smaller size appropriate for the emergency calling
954 * warning.
955 */
956 private void maybeChangeHintSize() {
957 if (TextUtils.isEmpty(mDigits.getHint())
958 || !TextUtils.isEmpty(mDigits.getText().toString())) {
959 // No hint or there are dialed digits, so use default size.
960 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
961 // By default, the digits view auto-resizes to fit the text it contains, so
962 // enable that now.
963 mDigits.setResizeEnabled(true);
964 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
965 } else {
966 // Hint present and no dialed digits, set custom font size appropriate for the warning.
967 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
968 R.dimen.emergency_call_warning_size));
969 // Since we're populating this with a static text string, disable auto-resize.
970 mDigits.setResizeEnabled(false);
971 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
972 }
973 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800974
Fan Zhang39d81f52022-03-21 22:47:21 +0000975 private void setupEmergencyDialpadViews() {
976 mEmergencyInfoInDialpad.setOnConfirmClickListener(this);
977 }
978
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800979 private void setupEmergencyShortcutsView() {
980 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
981 mDialpadView = findViewById(R.id.emergency_dialer);
982
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800983 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
984 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
985
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800986 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
987 dialpadButton.setOnClickListener(this);
988
Fan Zhang39d81f52022-03-21 22:47:21 +0000989 mEmergencyInfoInShortcut.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800990
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800991 mEmergencyShortcutButtonList = new ArrayList<>();
992 setupEmergencyCallShortcutButton();
993
Leo Hsu43d670a2018-12-04 15:40:36 +0800994 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800995
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800996 switchView(mEmergencyShortcutView, mDialpadView, false);
997 }
998
Leo Hsu43d670a2018-12-04 15:40:36 +0800999 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001000 final View locationInfo = findViewById(R.id.location_info);
1001
Leo Hsubc7553a2019-03-05 15:47:23 +08001002 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +08001003 String countryName = null;
1004 if (!TextUtils.isEmpty(countryIso)) {
1005 Locale locale = Locale.getDefault();
1006 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1007 .getDisplayCountry();
1008 }
1009 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001010 locationInfo.setVisibility(View.INVISIBLE);
1011 } else {
1012 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001013 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001014 locationInfo.setVisibility(View.VISIBLE);
1015 }
1016 }
1017
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001018 private void setupEmergencyCallShortcutButton() {
1019 final ViewGroup shortcutButtonContainer = findViewById(
1020 R.id.emergency_shortcut_buttons_container);
1021 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001022 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001023
CY Cheng9a69c182018-06-15 21:20:10 +08001024 mShortcutAdapter = new EccShortcutAdapter(this) {
1025 @Override
1026 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1027 CharSequence description, int iconRes) {
1028 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1029 .inflate(R.layout.emergency_shortcut_button, parent, false);
1030 button.setPhoneNumber(number);
1031 button.setPhoneDescription(description);
1032 button.setPhoneTypeIcon(iconRes);
1033 button.setOnConfirmClickListener(EmergencyDialer.this);
1034 return button;
1035 }
1036 };
1037 mShortcutDataSetObserver = new DataSetObserver() {
1038 @Override
1039 public void onChanged() {
1040 super.onChanged();
1041 updateLayout();
1042 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001043
CY Cheng9a69c182018-06-15 21:20:10 +08001044 @Override
1045 public void onInvalidated() {
1046 super.onInvalidated();
1047 updateLayout();
1048 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001049
CY Cheng9a69c182018-06-15 21:20:10 +08001050 private void updateLayout() {
1051 // clear previous added buttons
1052 shortcutButtonContainer.removeAllViews();
1053 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001054
CY Cheng9a69c182018-06-15 21:20:10 +08001055 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1056 EmergencyShortcutButton button = (EmergencyShortcutButton)
1057 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1058 mEmergencyShortcutButtonList.add(button);
1059 shortcutButtonContainer.addView(button);
1060 }
1061
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001062 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001063 if (mEmergencyShortcutButtonList.size() > 1) {
1064 emergencyNumberTitle.setText(getString(
1065 R.string.numerous_emergency_numbers_title));
1066 } else {
1067 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1068 }
1069 }
1070 };
1071 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1072 }
1073
Leo Hsu43d670a2018-12-04 15:40:36 +08001074 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001075 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001076 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001077 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001078 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001079 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001080 }
1081 }
1082
1083 /**
1084 * Called by the activity before a touch event is dispatched to the view hierarchy.
1085 */
1086 private void onPreTouchEvent(MotionEvent event) {
Fan Zhang39d81f52022-03-21 22:47:21 +00001087 mEmergencyInfoInDialpad.onPreTouchEvent(event);
1088 mEmergencyInfoInShortcut.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001089
1090 if (mEmergencyShortcutButtonList != null) {
1091 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1092 button.onPreTouchEvent(event);
1093 }
1094 }
1095 }
1096
1097 /**
1098 * Called by the activity after a touch event is dispatched to the view hierarchy.
1099 */
1100 private void onPostTouchEvent(MotionEvent event) {
Fan Zhang39d81f52022-03-21 22:47:21 +00001101 mEmergencyInfoInDialpad.onPostTouchEvent(event);
1102 mEmergencyInfoInShortcut.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001103
1104 if (mEmergencyShortcutButtonList != null) {
1105 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1106 button.onPostTouchEvent(event);
1107 }
1108 }
1109 }
1110
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001111 /**
1112 * Switch two view.
1113 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001114 * @param displayView the view would be displayed.
1115 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001116 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1117 */
1118 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1119 if (displayView == null || hideView == null) {
1120 return;
1121 }
1122
1123 if (displayView.getVisibility() == View.VISIBLE) {
1124 return;
1125 }
1126
1127 if (hasAnimation) {
1128 crossfade(hideView, displayView);
1129 } else {
1130 hideView.setVisibility(View.GONE);
1131 displayView.setVisibility(View.VISIBLE);
1132 }
1133 }
1134
1135 /**
1136 * Fade out and fade in animation between two view transition.
1137 */
1138 private void crossfade(View fadeOutView, View fadeInView) {
1139 if (fadeOutView == null || fadeInView == null) {
1140 return;
1141 }
1142 final int shortAnimationDuration = getResources().getInteger(
1143 android.R.integer.config_shortAnimTime);
1144
1145 fadeInView.setAlpha(0f);
1146 fadeInView.setVisibility(View.VISIBLE);
1147
1148 fadeInView.animate()
1149 .alpha(1f)
1150 .setDuration(shortAnimationDuration)
1151 .setListener(null);
1152
1153 fadeOutView.animate()
1154 .alpha(0f)
1155 .setDuration(shortAnimationDuration)
1156 .setListener(new AnimatorListenerAdapter() {
1157 @Override
1158 public void onAnimationEnd(Animator animation) {
1159 fadeOutView.setVisibility(View.GONE);
1160 }
1161 });
1162 }
Shaotang Li8662a912018-07-04 16:53:01 +08001163
Shaotang Li8662a912018-07-04 16:53:01 +08001164 private boolean isShortcutNumber(String number) {
1165 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1166 return false;
1167 }
1168
1169 boolean isShortcut = false;
1170 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1171 if (button != null && number.equals(button.getPhoneNumber())) {
1172 isShortcut = true;
1173 break;
1174 }
1175 }
1176 return isShortcut;
1177 }
Leo Hsue3512b62019-02-14 15:53:00 +08001178
1179 private String entryTypeToString(int entryType) {
1180 switch (entryType) {
1181 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1182 return "LockScreen";
1183 case ENTRY_TYPE_POWER_MENU:
1184 return "PowerMenu";
1185 default:
1186 return "Unknown-" + entryType;
1187 }
1188 }
1189
1190 private String callSourceToString(int callSource) {
1191 switch (callSource) {
Hall Liu6f35de92020-01-23 14:29:26 -08001192 case TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD:
Leo Hsue3512b62019-02-14 15:53:00 +08001193 return "DialPad";
Hall Liu6f35de92020-01-23 14:29:26 -08001194 case TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT:
Leo Hsue3512b62019-02-14 15:53:00 +08001195 return "Shortcut";
1196 default:
1197 return "Unknown-" + callSource;
1198 }
1199 }
Michelef6a5ea22019-10-15 16:08:54 -07001200
1201 private WallpaperManager getWallpaperManager() {
1202 return getSystemService(WallpaperManager.class);
1203 }
1204
1205 private static boolean supportsDarkText(WallpaperColors colors) {
1206 if (colors != null) {
1207 return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
1208 }
1209 // It's possible that wallpaper colors are null (e.g. when colors are being
1210 // processed or a live wallpaper is used). In this case, fallback to same
1211 // behavior as when shortcut view is enabled.
1212 return false;
1213 }
1214
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001215 private int getPrimaryColor(WallpaperColors colors) {
Michelef6a5ea22019-10-15 16:08:54 -07001216 if (colors != null) {
Grace Jia835d8e62020-04-01 15:47:39 -07001217 // Android accessibility scanner
1218 // (https://support.google.com/accessibility/android/answer/7158390)
1219 // suggest small text and graphics have a contrast ratio greater than
1220 // 4.5 with background color. The color generated from wallpaper may not
1221 // follow this rule. Calculate a proper color here.
1222 Color primary = colors.getPrimaryColor();
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001223 Color text;
1224 if (mSupportsDarkText) {
1225 text = Color.valueOf(Color.BLACK);
1226 } else {
1227 text = Color.valueOf(Color.WHITE);
1228 }
Grace Jia835d8e62020-04-01 15:47:39 -07001229 Color dial = Color.valueOf(DIALER_GREEN);
1230 // If current primary color can't follow the contrast ratio rule, make it
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001231 // deeper/lighter and try again.
Grace Jia835d8e62020-04-01 15:47:39 -07001232 while (!checkContrastRatio(primary, text)) {
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001233 primary = getNextColor(primary, mSupportsDarkText);
Grace Jia835d8e62020-04-01 15:47:39 -07001234 }
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001235 if (!mSupportsDarkText) {
1236 while (!checkContrastRatio(primary, dial)) {
1237 primary = getNextColor(primary, mSupportsDarkText);
1238 }
Grace Jia835d8e62020-04-01 15:47:39 -07001239 }
1240 return primary.toArgb();
Michelef6a5ea22019-10-15 16:08:54 -07001241 }
1242 // It's possible that wallpaper colors are null (e.g. when colors are being
1243 // processed or a live wallpaper is used). In this case, fallback to same
1244 // behavior as when shortcut view is enabled.
1245 return Color.BLACK;
1246 }
Grace Jia835d8e62020-04-01 15:47:39 -07001247
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001248 private Color getNextColor(Color color, boolean darkText) {
1249 float sign = darkText ? 1.f : -1.f;
1250 float r = color.red() + sign * COLOR_DELTA;
1251 float g = color.green() + sign * COLOR_DELTA;
1252 float b = color.blue() + sign * COLOR_DELTA;
Grace Jia835d8e62020-04-01 15:47:39 -07001253 if (r < 0f) r = 0f;
1254 if (g < 0f) g = 0f;
1255 if (b < 0f) b = 0f;
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001256 if (r > 1f) r = 1f;
1257 if (g > 1f) g = 1f;
1258 if (b > 1f) b = 1f;
Grace Jia835d8e62020-04-01 15:47:39 -07001259 return Color.valueOf(r, g, b);
1260 }
1261
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001262 private boolean checkContrastRatio(Color color1, Color color2) {
Grace Jia835d8e62020-04-01 15:47:39 -07001263 float lum1 = color1.luminance();
1264 float lum2 = color2.luminance();
1265 double cr;
1266 if (lum1 >= lum2) {
1267 cr = (lum1 + 0.05) / (lum2 + 0.05);
1268 } else {
1269 cr = (lum2 + 0.05) / (lum1 + 0.05);
1270 }
1271
1272 // Make cr greater than 5.0 instead of 4.5 to guarantee that transparent white
1273 // text and graphics can have contrast ratio greather than 4.5 with background.
1274 return cr > 5.0;
1275 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276}