blob: c6bac02cf0d18689778847c7ceff8c057a4f5c5a [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Michelef6a5ea22019-10-15 16:08:54 -070026import android.app.WallpaperColors;
Lucas Dupineb9c5702017-05-10 16:57:09 -070027import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080032import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080033import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Michelef6a5ea22019-10-15 16:08:54 -070035import android.graphics.drawable.ColorDrawable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.media.AudioManager;
37import android.media.ToneGenerator;
38import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070039import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070043import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070044import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070045import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.PhoneNumberUtils;
Meng Wangebdba862020-01-08 18:00:39 -080047import com.android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070048import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070049import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070050import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070052import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080053import android.text.Spannable;
54import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.text.TextUtils;
56import android.text.TextWatcher;
57import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080058import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070060import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070061import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070063import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070064import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080066import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080067import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080069import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080070import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071
Yorke Lee23a70732014-08-14 17:12:01 -070072import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070073import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070074import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075
Chihhang Chuang92cfe512018-06-07 16:25:27 +080076import java.util.ArrayList;
77import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080078import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080079
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080/**
81 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
82 *
83 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
84 * activity from apps/Contacts) that:
Leo Hsu83ab2332018-12-18 15:20:45 +080085 * 1. Allows ONLY emergency calls to be dialed
86 * 2. Disallows voicemail functionality
87 * 3. Allows this activity to stay in front of the keyguard.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088 *
89 * TODO: Even though this is an ultra-simplified version of the normal
90 * dialer, there's still lots of code duplication between this class and
91 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
92 * moved into a shared base class that would live in the framework?
93 * Or could we figure out some way to move *this* class into apps/Contacts
94 * also?
95 */
96public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070097 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Michelef6a5ea22019-10-15 16:08:54 -070098 DialpadKeyButton.OnPressedListener,
99 WallpaperManager.OnColorsChangedListener,
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800100 EmergencyShortcutButton.OnConfirmClickListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800101 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-07-04 16:53:01 +0800102
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103 // Keys used with onSaveInstanceState().
104 private static final String LAST_NUMBER = "lastNumber";
105
106 // Intent action for this activity.
107 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
108
Shaotang Li8662a912018-07-04 16:53:01 +0800109 /**
110 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
111 * the emergency dialer.
112 */
113 public static final String EXTRA_ENTRY_TYPE =
114 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
115
Leo Hsue3512b62019-02-14 15:53:00 +0800116 // Constants indicating the entry type that user opened emergency dialer.
117 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
118 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
119 public static final int ENTRY_TYPE_UNKNOWN = 0;
120 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
121 public static final int ENTRY_TYPE_POWER_MENU = 2;
122
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123 // List of dialer button IDs.
Leo Hsu83ab2332018-12-18 15:20:45 +0800124 private static final int[] DIALER_KEYS = new int[]{
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125 R.id.one, R.id.two, R.id.three,
126 R.id.four, R.id.five, R.id.six,
127 R.id.seven, R.id.eight, R.id.nine,
Leo Hsu83ab2332018-12-18 15:20:45 +0800128 R.id.star, R.id.zero, R.id.pound};
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129
130 // Debug constants.
131 private static final boolean DBG = false;
132 private static final String LOG_TAG = "EmergencyDialer";
133
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700134 /** The length of DTMF tones in milliseconds */
135 private static final int TONE_LENGTH_MS = 150;
136
137 /** The DTMF tone volume relative to other sounds in the stream */
138 private static final int TONE_RELATIVE_VOLUME = 80;
139
140 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
141 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
142
143 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
144
Lucas Dupineb9c5702017-05-10 16:57:09 -0700145 /** 90% opacity, different from other gradients **/
146 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
147
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800148 /** 85% opacity for black background **/
149 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
150
CY Cheng9a69c182018-06-15 21:20:10 +0800151 /** Size limit of emergency shortcut buttons container. **/
152 private static final int SHORTCUT_SIZE_LIMIT = 3;
153
Tyler Gunnc5428972018-03-28 14:15:34 -0700154 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 private View mDialButton;
156 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800157 private View mEmergencyShortcutView;
158 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800160 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800161 private EccShortcutAdapter mShortcutAdapter;
162 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800163
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 private ToneGenerator mToneGenerator;
165 private Object mToneGeneratorLock = new Object();
166
167 // determines if we want to playback local DTMF tones.
168 private boolean mDTMFToneEnabled;
169
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700170 private EmergencyActionGroup mEmergencyActionGroup;
171
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800172 private EmergencyInfoGroup mEmergencyInfoGroup;
173
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174 // close activity when screen turns off
175 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
176 @Override
177 public void onReceive(Context context, Intent intent) {
178 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700179 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 }
181 }
182 };
183
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800184 /**
185 * Customize accessibility methods in View.
186 */
187 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
188
189 /**
190 * Stop AccessiblityService from reading the title of a hidden View.
191 *
192 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
193 * in the animation end. The view with an accessibility pane title would call the
194 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
195 * accessibility service to read the pane title of fade out view instead of pane title of
196 * fade in view. So it need to filter out the event called by vanished pane.
197 */
198 @Override
199 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
200 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
201 && host.getVisibility() == View.GONE) {
202 return;
203 }
204 super.onPopulateAccessibilityEvent(host, event);
205 }
206 };
207
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
209
Lucas Dupineb9c5702017-05-10 16:57:09 -0700210 // Background gradient
Michelef6a5ea22019-10-15 16:08:54 -0700211 private ColorDrawable mBackgroundDrawable;
Lucas Dupineb9c5702017-05-10 16:57:09 -0700212 private boolean mSupportsDarkText;
213
Tyler Gunnc5428972018-03-28 14:15:34 -0700214 private boolean mIsWfcEmergencyCallingWarningEnabled;
215 private float mDefaultDigitsTextSize;
216
Leo Hsue3512b62019-02-14 15:53:00 +0800217 private int mEntryType;
Leo Hsubc7553a2019-03-05 15:47:23 +0800218 private ShortcutViewUtils.Config mShortcutViewConfig;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800219
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 @Override
221 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
222 // Do nothing
223 }
224
225 @Override
226 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700227 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 }
229
230 @Override
231 public void afterTextChanged(Editable input) {
232 // Check for special sequences, in particular the "**04" or "**05"
233 // sequences that allow you to enter PIN or PUK-related codes.
234 //
235 // But note we *don't* allow most other special sequences here,
236 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
237 // since those shouldn't be available if the device is locked.
238 //
239 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
240 // here, not the regular handleChars() method.
241 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
242 // A special sequence was entered, clear the digits
243 mDigits.getText().clear();
244 }
245
246 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800247 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 }
249
250 @Override
251 protected void onCreate(Bundle icicle) {
252 super.onCreate(icicle);
253
Leo Hsue3512b62019-02-14 15:53:00 +0800254 mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
255 Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
256
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800258 setShowWhenLocked(true);
259 // Allow turning screen on
260 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261
Chuck Liaocdeebb42018-10-30 12:07:18 +0800262 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
263 PersistableBundle carrierConfig =
264 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
265
Leo Hsubc7553a2019-03-05 15:47:23 +0800266 mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
CY Cheng9a69c182018-06-15 21:20:10 +0800267 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsubc7553a2019-03-05 15:47:23 +0800268 + mShortcutViewConfig.isEnabled());
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800269
Leo Hsubc7553a2019-03-05 15:47:23 +0800270 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800271 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800272 updateTheme(false);
273 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700274 WallpaperColors wallpaperColors =
275 getWallpaperManager().getWallpaperColors(WallpaperManager.FLAG_LOCK);
276 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800277 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700278
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279 setContentView(R.layout.emergency_dialer);
280
Tyler Gunnc5428972018-03-28 14:15:34 -0700281 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282 mDigits.setKeyListener(DialerKeyListener.getInstance());
283 mDigits.setOnClickListener(this);
284 mDigits.setOnKeyListener(this);
285 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700286 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700287 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 maybeAddNumberFormatting();
289
Michelef6a5ea22019-10-15 16:08:54 -0700290 mBackgroundDrawable = new ColorDrawable();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700291 Point displaySize = new Point();
292 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
293 .getDefaultDisplay().getSize(displaySize);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700294 mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled()
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800295 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700296 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700297
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 // Check for the presence of the keypad
299 View view = findViewById(R.id.one);
300 if (view != null) {
301 setupKeypad();
302 }
303
304 mDelete = findViewById(R.id.deleteButton);
305 mDelete.setOnClickListener(this);
306 mDelete.setOnLongClickListener(this);
307
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700308 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309
310 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700311 // Read carrier config through the public API because PhoneGlobals is not available when we
312 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700313 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314 mDialButton.setOnClickListener(this);
315 } else {
316 mDialButton.setVisibility(View.GONE);
317 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700318 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
319 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
320 maybeShowWfcEmergencyCallingWarning();
321
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700322 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323
324 if (icicle != null) {
325 super.onRestoreInstanceState(icicle);
326 }
327
328 // Extract phone number from intent
329 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700330 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
332 if (number != null) {
333 mDigits.setText(number);
334 }
335 }
336
337 // if the mToneGenerator creation fails, just continue without it. It is
338 // a local audio signal, and is not as important as the dtmf tone itself.
339 synchronized (mToneGeneratorLock) {
340 if (mToneGenerator == null) {
341 try {
342 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
343 } catch (RuntimeException e) {
344 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
345 mToneGenerator = null;
346 }
347 }
348 }
349
350 final IntentFilter intentFilter = new IntentFilter();
351 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
352 registerReceiver(mBroadcastReceiver, intentFilter);
353
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700354 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800355
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800356 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
357
Leo Hsubc7553a2019-03-05 15:47:23 +0800358 if (mShortcutViewConfig.isEnabled()) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800359 setupEmergencyShortcutsView();
360 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361 }
362
363 @Override
364 protected void onDestroy() {
365 super.onDestroy();
366 synchronized (mToneGeneratorLock) {
367 if (mToneGenerator != null) {
368 mToneGenerator.release();
369 mToneGenerator = null;
370 }
371 }
372 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800373 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
374 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
375 mShortcutDataSetObserver = null;
376 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 }
378
379 @Override
380 protected void onRestoreInstanceState(Bundle icicle) {
381 mLastNumber = icicle.getString(LAST_NUMBER);
382 }
383
384 @Override
385 protected void onSaveInstanceState(Bundle outState) {
386 super.onSaveInstanceState(outState);
387 outState.putString(LAST_NUMBER, mLastNumber);
388 }
389
390 /**
391 * Explicitly turn off number formatting, since it gets in the way of the emergency
392 * number detector
393 */
394 protected void maybeAddNumberFormatting() {
395 // Do nothing.
396 }
397
398 @Override
399 protected void onPostCreate(Bundle savedInstanceState) {
400 super.onPostCreate(savedInstanceState);
401
402 // This can't be done in onCreate(), since the auto-restoring of the digits
403 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
404 // is called. This method will be called every time the activity is created, and
405 // will always happen after onRestoreSavedInstanceState().
406 mDigits.addTextChangedListener(this);
407 }
408
409 private void setupKeypad() {
410 // Setup the listeners for the buttons
411 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700412 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
413 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700414 }
415
416 View view = findViewById(R.id.zero);
417 view.setOnLongClickListener(this);
418 }
419
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800420 @Override
421 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800422 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
423 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
Leo Hsubc7553a2019-03-05 15:47:23 +0800424 if (mShortcutViewConfig.isEnabled() && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800425 && mDialpadView.getVisibility() == View.VISIBLE) {
426 switchView(mEmergencyShortcutView, mDialpadView, true);
427 return;
428 }
429 super.onBackPressed();
430 }
431
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700432 /**
433 * handle key events
434 */
435 @Override
436 public boolean onKeyDown(int keyCode, KeyEvent event) {
437 switch (keyCode) {
438 // Happen when there's a "Call" hard button.
439 case KeyEvent.KEYCODE_CALL: {
440 if (TextUtils.isEmpty(mDigits.getText().toString())) {
441 // if we are adding a call from the InCallScreen and the phone
442 // number entered is empty, we just close the dialer to expose
443 // the InCallScreen under it.
444 finish();
445 } else {
446 // otherwise, we place the call.
447 placeCall();
448 }
449 return true;
450 }
451 }
452 return super.onKeyDown(keyCode, event);
453 }
454
455 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700456 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
458 mDigits.onKeyDown(keyCode, event);
459 }
460
461 @Override
462 public boolean onKey(View view, int keyCode, KeyEvent event) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800463 if (view.getId()
464 == R.id.digits) { // Happen when "Done" button of the IME is pressed. This can
465 // happen when this
466 // Activity is forced into landscape mode due to a desk dock.
467 if (keyCode == KeyEvent.KEYCODE_ENTER
468 && event.getAction() == KeyEvent.ACTION_UP) {
469 placeCall();
470 return true;
471 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700472 }
473 return false;
474 }
475
476 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700477 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800478 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700479 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800480 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700481 return handled;
482 }
483
484 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800485 public void onConfirmClick(EmergencyShortcutButton button) {
486 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800487 String phoneNumber = button.getPhoneNumber();
488
489 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800490 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800491
Hall Liu6f35de92020-01-23 14:29:26 -0800492 placeCall(phoneNumber, TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800493 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800494 } else {
495 Log.d(LOG_TAG, "emergency number is empty");
496 }
497 }
498
499 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800500 public void onConfirmClick(EmergencyInfoGroup button) {
501 if (button == null) return;
502
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800503 Intent intent = (Intent) button.getTag(R.id.tag_intent);
504 if (intent != null) {
505 startActivity(intent);
506 }
507 }
508
509 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700510 public void onClick(View view) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800511 if (view.getId() == R.id.deleteButton) {
512 keyPressed(KeyEvent.KEYCODE_DEL);
513 return;
514 } else if (view.getId() == R.id.floating_action_button) {
515 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
516 placeCall();
517 return;
518 } else if (view.getId() == R.id.digits) {
519 if (mDigits.length() != 0) {
520 mDigits.setCursorVisible(true);
Yorke Lee23a70732014-08-14 17:12:01 -0700521 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800522 return;
523 } else if (view.getId() == R.id.floating_action_button_dialpad) {
524 mDigits.getText().clear();
525 switchView(mDialpadView, mEmergencyShortcutView, true);
526 return;
Yorke Lee23a70732014-08-14 17:12:01 -0700527 }
528 }
529
530 @Override
531 public void onPressed(View view, boolean pressed) {
532 if (!pressed) {
533 return;
534 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800535 if (view.getId() == R.id.one) {
536 playTone(ToneGenerator.TONE_DTMF_1);
537 keyPressed(KeyEvent.KEYCODE_1);
538 return;
539 } else if (view.getId() == R.id.two) {
540 playTone(ToneGenerator.TONE_DTMF_2);
541 keyPressed(KeyEvent.KEYCODE_2);
542 return;
543 } else if (view.getId() == R.id.three) {
544 playTone(ToneGenerator.TONE_DTMF_3);
545 keyPressed(KeyEvent.KEYCODE_3);
546 return;
547 } else if (view.getId() == R.id.four) {
548 playTone(ToneGenerator.TONE_DTMF_4);
549 keyPressed(KeyEvent.KEYCODE_4);
550 return;
551 } else if (view.getId() == R.id.five) {
552 playTone(ToneGenerator.TONE_DTMF_5);
553 keyPressed(KeyEvent.KEYCODE_5);
554 return;
555 } else if (view.getId() == R.id.six) {
556 playTone(ToneGenerator.TONE_DTMF_6);
557 keyPressed(KeyEvent.KEYCODE_6);
558 return;
559 } else if (view.getId() == R.id.seven) {
560 playTone(ToneGenerator.TONE_DTMF_7);
561 keyPressed(KeyEvent.KEYCODE_7);
562 return;
563 } else if (view.getId() == R.id.eight) {
564 playTone(ToneGenerator.TONE_DTMF_8);
565 keyPressed(KeyEvent.KEYCODE_8);
566 return;
567 } else if (view.getId() == R.id.nine) {
568 playTone(ToneGenerator.TONE_DTMF_9);
569 keyPressed(KeyEvent.KEYCODE_9);
570 return;
571 } else if (view.getId() == R.id.zero) {
572 playTone(ToneGenerator.TONE_DTMF_0);
573 keyPressed(KeyEvent.KEYCODE_0);
574 return;
575 } else if (view.getId() == R.id.pound) {
576 playTone(ToneGenerator.TONE_DTMF_P);
577 keyPressed(KeyEvent.KEYCODE_POUND);
578 return;
579 } else if (view.getId() == R.id.star) {
580 playTone(ToneGenerator.TONE_DTMF_S);
581 keyPressed(KeyEvent.KEYCODE_STAR);
582 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 }
584 }
585
586 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 * called for long touch events
588 */
589 @Override
590 public boolean onLongClick(View view) {
591 int id = view.getId();
Amit Mahajan28a499c2019-11-20 15:13:42 -0800592 if (id == R.id.deleteButton) {
593 mDigits.getText().clear();
594 return true;
595 } else if (id == R.id.zero) {
596 removePreviousDigitIfPossible();
597 keyPressed(KeyEvent.KEYCODE_PLUS);
598 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 }
600 return false;
601 }
602
603 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700604 protected void onStart() {
605 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800606
Leo Hsubc7553a2019-03-05 15:47:23 +0800607 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800608 // Shortcut view doesn't support dark text theme.
Michelef6a5ea22019-10-15 16:08:54 -0700609 mBackgroundDrawable.setColor(Color.BLACK);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800610 updateTheme(false);
611 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700612 WallpaperManager wallpaperManager = getWallpaperManager();
613 if (wallpaperManager.isWallpaperSupported()) {
614 wallpaperManager.addOnColorsChangedListener(this, null);
615 }
616
617 WallpaperColors wallpaperColors =
618 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK);
619 mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors));
620 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800621 }
CY Cheng9a69c182018-06-15 21:20:10 +0800622
Leo Hsubc7553a2019-03-05 15:47:23 +0800623 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800624 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800625 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700626 }
627
628 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700629 protected void onResume() {
630 super.onResume();
631
632 // retrieve the DTMF tone play back setting.
633 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
634 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
635
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 // if the mToneGenerator creation fails, just continue without it. It is
637 // a local audio signal, and is not as important as the dtmf tone itself.
638 synchronized (mToneGeneratorLock) {
639 if (mToneGenerator == null) {
640 try {
641 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
642 TONE_RELATIVE_VOLUME);
643 } catch (RuntimeException e) {
644 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
645 mToneGenerator = null;
646 }
647 }
648 }
649
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 updateDialAndDeleteButtonStateEnabledAttr();
651 }
652
653 @Override
654 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700655 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700656 }
657
Lucas Dupineb9c5702017-05-10 16:57:09 -0700658 @Override
659 protected void onStop() {
660 super.onStop();
Michelef6a5ea22019-10-15 16:08:54 -0700661
662 WallpaperManager wallpaperManager = getWallpaperManager();
663 if (wallpaperManager.isWallpaperSupported()) {
664 wallpaperManager.removeOnColorsChangedListener(this);
665 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700666 }
667
668 /**
669 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800670 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700671 * @param supportsDarkText true if gradient supports dark text
672 */
673 private void updateTheme(boolean supportsDarkText) {
674 if (mSupportsDarkText == supportsDarkText) {
675 return;
676 }
677 mSupportsDarkText = supportsDarkText;
678
679 // We can't change themes after inflation, in this case we'll have to recreate
680 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700681 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700682 recreate();
683 return;
684 }
685
686 int vis = getWindow().getDecorView().getSystemUiVisibility();
687 if (supportsDarkText) {
688 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
689 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
690 setTheme(R.style.EmergencyDialerThemeDark);
691 } else {
692 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
693 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
694 setTheme(R.style.EmergencyDialerTheme);
695 }
696 getWindow().getDecorView().setSystemUiVisibility(vis);
697 }
698
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700699 /**
700 * place the call, but check to make sure it is a viable number.
701 */
702 private void placeCall() {
703 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900704
705 // Convert into emergency number according to emergency conversion map.
706 // If conversion map is not defined (this is default), this method does
707 // nothing and just returns input number.
708 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
709
Leo Hsubc7553a2019-03-05 15:47:23 +0800710 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800711 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800712 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800713 isEmergencyNumber = true;
714 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800715 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
716 // If a number from SIM/network/... is categoried as police/ambulance/fire,
717 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
718 // shortcut button because a number provided by database has higher priority.
719 isEmergencyNumber = true;
720 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsubc7553a2019-03-05 15:47:23 +0800721 } else {
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800722 isEmergencyNumber = getSystemService(TelephonyManager.class)
723 .isEmergencyNumber(mLastNumber);
Leo Hsu43d670a2018-12-04 15:40:36 +0800724 }
725
726 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700727 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
728
729 // place the call if it is a valid number
730 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
731 // There is no number entered.
732 playTone(ToneGenerator.TONE_PROP_NACK);
733 return;
734 }
Shaotang Li8662a912018-07-04 16:53:01 +0800735
Hall Liu6f35de92020-01-23 14:29:26 -0800736 placeCall(mLastNumber, TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD,
Leo Hsu43d670a2018-12-04 15:40:36 +0800737 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700738 } else {
739 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
740
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
742 }
Yorke Lee9b341512014-10-17 11:36:41 -0700743 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 }
745
Leo Hsu43d670a2018-12-04 15:40:36 +0800746 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800747 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
748 + ", entry = " + entryTypeToString(mEntryType));
749
Leo Hsu43d670a2018-12-04 15:40:36 +0800750 Bundle extras = new Bundle();
751 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800752 /**
753 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
754 * when the dialed number is ambiguous and identified as both emergency number and any
755 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
756 * number in a country and a non-emergency number of a carrier's customer service hotline.
757 */
758 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800759
760 if (phone != null && phone.getPhoneAccountHandle() != null) {
761 // Requests to dial through the specified phone.
762 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
763 phone.getPhoneAccountHandle());
764 }
765
766 TelecomManager tm = this.getSystemService(TelecomManager.class);
767 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
768 }
769
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700770 /**
771 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
772 *
773 * The tone is played locally, using the audio stream for phone calls.
774 * Tones are played only if the "Audible touch tones" user preference
775 * is checked, and are NOT played if the device is in silent mode.
776 *
777 * @param tone a tone code from {@link ToneGenerator}
778 */
779 void playTone(int tone) {
780 // if local tone playback is disabled, just return.
781 if (!mDTMFToneEnabled) {
782 return;
783 }
784
785 // Also do nothing if the phone is in silent mode.
786 // We need to re-check the ringer mode for *every* playTone()
787 // call, rather than keeping a local flag that's updated in
788 // onResume(), since it's possible to toggle silent mode without
789 // leaving the current activity (via the ENDCALL-longpress menu.)
790 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
791 int ringerMode = audioManager.getRingerMode();
792 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800793 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 return;
795 }
796
797 synchronized (mToneGeneratorLock) {
798 if (mToneGenerator == null) {
799 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
800 return;
801 }
802
803 // Start the new tone (will stop any playing tone)
804 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
805 }
806 }
807
808 private CharSequence createErrorMessage(String number) {
809 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800810 String errorString = getString(R.string.dial_emergency_error, number);
811 int startingPosition = errorString.indexOf(number);
812 int endingPosition = startingPosition + number.length();
813 Spannable result = new SpannableString(errorString);
814 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
815 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700816 } else {
817 return getText(R.string.dial_emergency_empty_error).toString();
818 }
819 }
820
821 @Override
822 protected Dialog onCreateDialog(int id) {
823 AlertDialog dialog = null;
824 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
825 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700826 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
828 .setMessage(createErrorMessage(mLastNumber))
829 .setPositiveButton(R.string.ok, null)
830 .setCancelable(true).create();
831
832 // blur stuff behind the dialog
833 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700834 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 }
836 return dialog;
837 }
838
839 @Override
840 protected void onPrepareDialog(int id, Dialog dialog) {
841 super.onPrepareDialog(id, dialog);
842 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
843 AlertDialog alert = (AlertDialog) dialog;
844 alert.setMessage(createErrorMessage(mLastNumber));
845 }
846 }
847
Andrew Leed5631e82014-10-08 16:03:58 -0700848 @Override
849 public boolean onOptionsItemSelected(MenuItem item) {
850 final int itemId = item.getItemId();
851 if (itemId == android.R.id.home) {
852 onBackPressed();
853 return true;
854 }
855 return super.onOptionsItemSelected(item);
856 }
857
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 /**
859 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
860 */
861 private void updateDialAndDeleteButtonStateEnabledAttr() {
862 final boolean notEmpty = mDigits.length() != 0;
863
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 mDelete.setEnabled(notEmpty);
865 }
Yorke Lee91311662014-10-24 14:50:45 -0700866
867 /**
868 * Remove the digit just before the current position. Used by various long pressed callbacks
869 * to remove the digit that was populated as a result of the short click.
870 */
871 private void removePreviousDigitIfPossible() {
872 final int currentPosition = mDigits.getSelectionStart();
873 if (currentPosition > 0) {
874 mDigits.setSelection(currentPosition);
875 mDigits.getText().delete(currentPosition - 1, currentPosition);
876 }
877 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800878
879 /**
880 * Update the text-to-speech annotations in the edit field.
881 */
882 private void updateTtsSpans() {
883 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
884 mDigits.getText().removeSpan(o);
885 }
886 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
887 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700888
889 @Override
Michelef6a5ea22019-10-15 16:08:54 -0700890 public void onColorsChanged(WallpaperColors colors, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700891 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Michelef6a5ea22019-10-15 16:08:54 -0700892 mBackgroundDrawable.setColor(getPrimaryColor(colors));
893 updateTheme(supportsDarkText(colors));
Lucas Dupineb9c5702017-05-10 16:57:09 -0700894 }
895 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700896
897 /**
898 * Where a carrier requires a warning that emergency calling is not available while on WFC,
899 * add hint text above the dial pad which warns the user of this case.
900 */
901 private void maybeShowWfcEmergencyCallingWarning() {
902 if (!mIsWfcEmergencyCallingWarningEnabled) {
903 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
904 return;
905 }
906
907 // Use an async task rather than calling into Telephony on UI thread.
908 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
909 @Override
910 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800911 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700912 boolean isWfcAvailable = tm.isWifiCallingAvailable();
913 ServiceState ss = tm.getServiceState();
914 boolean isCellAvailable =
915 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
916 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800917 + " isCellAvailable=" + isCellAvailable
918 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700919 return isWfcAvailable && !isCellAvailable;
920 }
921
922 @Override
923 protected void onPostExecute(Boolean result) {
924 if (result.booleanValue()) {
925 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
926 mDigits.setHint(R.string.dial_emergency_calling_not_available);
927 } else {
928 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
929 mDigits.setHint("");
930 }
931 maybeChangeHintSize();
932 }
933 };
934 showWfcWarningTask.execute((Void) null);
935 }
936
937 /**
938 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
939 * edit view and set the font size to a smaller size appropriate for the emergency calling
940 * warning.
941 */
942 private void maybeChangeHintSize() {
943 if (TextUtils.isEmpty(mDigits.getHint())
944 || !TextUtils.isEmpty(mDigits.getText().toString())) {
945 // No hint or there are dialed digits, so use default size.
946 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
947 // By default, the digits view auto-resizes to fit the text it contains, so
948 // enable that now.
949 mDigits.setResizeEnabled(true);
950 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
951 } else {
952 // Hint present and no dialed digits, set custom font size appropriate for the warning.
953 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
954 R.dimen.emergency_call_warning_size));
955 // Since we're populating this with a static text string, disable auto-resize.
956 mDigits.setResizeEnabled(false);
957 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
958 }
959 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800960
961 private void setupEmergencyShortcutsView() {
962 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
963 mDialpadView = findViewById(R.id.emergency_dialer);
964
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800965 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
966 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
967
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800968 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
969 dialpadButton.setOnClickListener(this);
970
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800971 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800972
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800973 mEmergencyShortcutButtonList = new ArrayList<>();
974 setupEmergencyCallShortcutButton();
975
Leo Hsu43d670a2018-12-04 15:40:36 +0800976 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800977
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800978 switchView(mEmergencyShortcutView, mDialpadView, false);
979 }
980
Leo Hsu43d670a2018-12-04 15:40:36 +0800981 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800982 final View locationInfo = findViewById(R.id.location_info);
983
Leo Hsubc7553a2019-03-05 15:47:23 +0800984 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +0800985 String countryName = null;
986 if (!TextUtils.isEmpty(countryIso)) {
987 Locale locale = Locale.getDefault();
988 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
989 .getDisplayCountry();
990 }
991 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800992 locationInfo.setVisibility(View.INVISIBLE);
993 } else {
994 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +0800995 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800996 locationInfo.setVisibility(View.VISIBLE);
997 }
998 }
999
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001000 private void setupEmergencyCallShortcutButton() {
1001 final ViewGroup shortcutButtonContainer = findViewById(
1002 R.id.emergency_shortcut_buttons_container);
1003 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001004 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001005
CY Cheng9a69c182018-06-15 21:20:10 +08001006 mShortcutAdapter = new EccShortcutAdapter(this) {
1007 @Override
1008 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1009 CharSequence description, int iconRes) {
1010 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1011 .inflate(R.layout.emergency_shortcut_button, parent, false);
1012 button.setPhoneNumber(number);
1013 button.setPhoneDescription(description);
1014 button.setPhoneTypeIcon(iconRes);
1015 button.setOnConfirmClickListener(EmergencyDialer.this);
1016 return button;
1017 }
1018 };
1019 mShortcutDataSetObserver = new DataSetObserver() {
1020 @Override
1021 public void onChanged() {
1022 super.onChanged();
1023 updateLayout();
1024 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001025
CY Cheng9a69c182018-06-15 21:20:10 +08001026 @Override
1027 public void onInvalidated() {
1028 super.onInvalidated();
1029 updateLayout();
1030 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001031
CY Cheng9a69c182018-06-15 21:20:10 +08001032 private void updateLayout() {
1033 // clear previous added buttons
1034 shortcutButtonContainer.removeAllViews();
1035 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001036
CY Cheng9a69c182018-06-15 21:20:10 +08001037 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1038 EmergencyShortcutButton button = (EmergencyShortcutButton)
1039 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1040 mEmergencyShortcutButtonList.add(button);
1041 shortcutButtonContainer.addView(button);
1042 }
1043
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001044 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001045 if (mEmergencyShortcutButtonList.size() > 1) {
1046 emergencyNumberTitle.setText(getString(
1047 R.string.numerous_emergency_numbers_title));
1048 } else {
1049 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1050 }
1051 }
1052 };
1053 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1054 }
1055
Leo Hsu43d670a2018-12-04 15:40:36 +08001056 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001057 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001058 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001059 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001060 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001061 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001062 }
1063 }
1064
1065 /**
1066 * Called by the activity before a touch event is dispatched to the view hierarchy.
1067 */
1068 private void onPreTouchEvent(MotionEvent event) {
1069 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001070 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001071
1072 if (mEmergencyShortcutButtonList != null) {
1073 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1074 button.onPreTouchEvent(event);
1075 }
1076 }
1077 }
1078
1079 /**
1080 * Called by the activity after a touch event is dispatched to the view hierarchy.
1081 */
1082 private void onPostTouchEvent(MotionEvent event) {
1083 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001084 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001085
1086 if (mEmergencyShortcutButtonList != null) {
1087 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1088 button.onPostTouchEvent(event);
1089 }
1090 }
1091 }
1092
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001093 /**
1094 * Switch two view.
1095 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001096 * @param displayView the view would be displayed.
1097 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001098 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1099 */
1100 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1101 if (displayView == null || hideView == null) {
1102 return;
1103 }
1104
1105 if (displayView.getVisibility() == View.VISIBLE) {
1106 return;
1107 }
1108
1109 if (hasAnimation) {
1110 crossfade(hideView, displayView);
1111 } else {
1112 hideView.setVisibility(View.GONE);
1113 displayView.setVisibility(View.VISIBLE);
1114 }
1115 }
1116
1117 /**
1118 * Fade out and fade in animation between two view transition.
1119 */
1120 private void crossfade(View fadeOutView, View fadeInView) {
1121 if (fadeOutView == null || fadeInView == null) {
1122 return;
1123 }
1124 final int shortAnimationDuration = getResources().getInteger(
1125 android.R.integer.config_shortAnimTime);
1126
1127 fadeInView.setAlpha(0f);
1128 fadeInView.setVisibility(View.VISIBLE);
1129
1130 fadeInView.animate()
1131 .alpha(1f)
1132 .setDuration(shortAnimationDuration)
1133 .setListener(null);
1134
1135 fadeOutView.animate()
1136 .alpha(0f)
1137 .setDuration(shortAnimationDuration)
1138 .setListener(new AnimatorListenerAdapter() {
1139 @Override
1140 public void onAnimationEnd(Animator animation) {
1141 fadeOutView.setVisibility(View.GONE);
1142 }
1143 });
1144 }
Shaotang Li8662a912018-07-04 16:53:01 +08001145
Shaotang Li8662a912018-07-04 16:53:01 +08001146 private boolean isShortcutNumber(String number) {
1147 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1148 return false;
1149 }
1150
1151 boolean isShortcut = false;
1152 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1153 if (button != null && number.equals(button.getPhoneNumber())) {
1154 isShortcut = true;
1155 break;
1156 }
1157 }
1158 return isShortcut;
1159 }
Leo Hsue3512b62019-02-14 15:53:00 +08001160
1161 private String entryTypeToString(int entryType) {
1162 switch (entryType) {
1163 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1164 return "LockScreen";
1165 case ENTRY_TYPE_POWER_MENU:
1166 return "PowerMenu";
1167 default:
1168 return "Unknown-" + entryType;
1169 }
1170 }
1171
1172 private String callSourceToString(int callSource) {
1173 switch (callSource) {
Hall Liu6f35de92020-01-23 14:29:26 -08001174 case TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD:
Leo Hsue3512b62019-02-14 15:53:00 +08001175 return "DialPad";
Hall Liu6f35de92020-01-23 14:29:26 -08001176 case TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT:
Leo Hsue3512b62019-02-14 15:53:00 +08001177 return "Shortcut";
1178 default:
1179 return "Unknown-" + callSource;
1180 }
1181 }
Michelef6a5ea22019-10-15 16:08:54 -07001182
1183 private WallpaperManager getWallpaperManager() {
1184 return getSystemService(WallpaperManager.class);
1185 }
1186
1187 private static boolean supportsDarkText(WallpaperColors colors) {
1188 if (colors != null) {
1189 return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
1190 }
1191 // It's possible that wallpaper colors are null (e.g. when colors are being
1192 // processed or a live wallpaper is used). In this case, fallback to same
1193 // behavior as when shortcut view is enabled.
1194 return false;
1195 }
1196
1197 private static int getPrimaryColor(WallpaperColors colors) {
1198 if (colors != null) {
1199 return colors.getPrimaryColor().toArgb();
1200 }
1201 // It's possible that wallpaper colors are null (e.g. when colors are being
1202 // processed or a live wallpaper is used). In this case, fallback to same
1203 // behavior as when shortcut view is enabled.
1204 return Color.BLACK;
1205 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206}