blob: 3cf74c8bea1afdc3d9808bd4a5fad46db39fc248 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Lucas Dupineb9c5702017-05-10 16:57:09 -070026import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080031import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080032import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070033import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.media.AudioManager;
35import android.media.ToneGenerator;
36import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070037import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070039import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.provider.Settings;
Shaotang Li5d906c12018-07-19 17:32:42 +080041import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070042import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070043import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070044import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080046import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070047import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070048import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070049import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070051import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080052import android.text.Spannable;
53import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.text.TextUtils;
55import android.text.TextWatcher;
56import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080057import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070059import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070060import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070062import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070063import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080065import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080066import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080068import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080069import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070
Lucas Dupinaf9e9912017-06-22 12:39:39 -070071import com.android.internal.colorextraction.ColorExtractor;
72import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin4946f6f2019-04-09 15:03:19 -070073import com.android.internal.colorextraction.drawable.ScrimDrawable;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +080074import com.android.phone.EmergencyDialerMetricsLogger.DialedFrom;
75import com.android.phone.EmergencyDialerMetricsLogger.PhoneNumberType;
76import com.android.phone.EmergencyDialerMetricsLogger.UiModeErrorCode;
Yorke Lee23a70732014-08-14 17:12:01 -070077import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070078import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070079import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080
Chihhang Chuang92cfe512018-06-07 16:25:27 +080081import java.util.ArrayList;
82import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080083import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080084
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085/**
86 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
87 *
88 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
89 * activity from apps/Contacts) that:
Leo Hsu83ab2332018-12-18 15:20:45 +080090 * 1. Allows ONLY emergency calls to be dialed
91 * 2. Disallows voicemail functionality
92 * 3. Allows this activity to stay in front of the keyguard.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093 *
94 * TODO: Even though this is an ultra-simplified version of the normal
95 * dialer, there's still lots of code duplication between this class and
96 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
97 * moved into a shared base class that would live in the framework?
98 * Or could we figure out some way to move *this* class into apps/Contacts
99 * also?
100 */
101public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700102 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800103 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800104 EmergencyShortcutButton.OnConfirmClickListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800105 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-07-04 16:53:01 +0800106
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 // Keys used with onSaveInstanceState().
108 private static final String LAST_NUMBER = "lastNumber";
109
110 // Intent action for this activity.
111 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
112
Shaotang Li8662a912018-07-04 16:53:01 +0800113 /**
114 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
115 * the emergency dialer.
116 */
117 public static final String EXTRA_ENTRY_TYPE =
118 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
119
Leo Hsue3512b62019-02-14 15:53:00 +0800120 // Constants indicating the entry type that user opened emergency dialer.
121 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
122 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
123 public static final int ENTRY_TYPE_UNKNOWN = 0;
124 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
125 public static final int ENTRY_TYPE_POWER_MENU = 2;
126
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 // List of dialer button IDs.
Leo Hsu83ab2332018-12-18 15:20:45 +0800128 private static final int[] DIALER_KEYS = new int[]{
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129 R.id.one, R.id.two, R.id.three,
130 R.id.four, R.id.five, R.id.six,
131 R.id.seven, R.id.eight, R.id.nine,
Leo Hsu83ab2332018-12-18 15:20:45 +0800132 R.id.star, R.id.zero, R.id.pound};
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133
134 // Debug constants.
135 private static final boolean DBG = false;
136 private static final String LOG_TAG = "EmergencyDialer";
137
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138 /** The length of DTMF tones in milliseconds */
139 private static final int TONE_LENGTH_MS = 150;
140
141 /** The DTMF tone volume relative to other sounds in the stream */
142 private static final int TONE_RELATIVE_VOLUME = 80;
143
144 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
145 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
146
147 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
148
Lucas Dupineb9c5702017-05-10 16:57:09 -0700149 /** 90% opacity, different from other gradients **/
150 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
151
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800152 /** 85% opacity for black background **/
153 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
154
CY Cheng9a69c182018-06-15 21:20:10 +0800155 /** Size limit of emergency shortcut buttons container. **/
156 private static final int SHORTCUT_SIZE_LIMIT = 3;
157
Tyler Gunnc5428972018-03-28 14:15:34 -0700158 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 private View mDialButton;
160 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800161 private View mEmergencyShortcutView;
162 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800164 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800165 private EccShortcutAdapter mShortcutAdapter;
166 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 private ToneGenerator mToneGenerator;
169 private Object mToneGeneratorLock = new Object();
170
171 // determines if we want to playback local DTMF tones.
172 private boolean mDTMFToneEnabled;
173
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700174 private EmergencyActionGroup mEmergencyActionGroup;
175
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800176 private EmergencyInfoGroup mEmergencyInfoGroup;
177
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 // close activity when screen turns off
179 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
180 @Override
181 public void onReceive(Context context, Intent intent) {
182 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700183 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 }
185 }
186 };
187
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800188 /**
189 * Customize accessibility methods in View.
190 */
191 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
192
193 /**
194 * Stop AccessiblityService from reading the title of a hidden View.
195 *
196 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
197 * in the animation end. The view with an accessibility pane title would call the
198 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
199 * accessibility service to read the pane title of fade out view instead of pane title of
200 * fade in view. So it need to filter out the event called by vanished pane.
201 */
202 @Override
203 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
204 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
205 && host.getVisibility() == View.GONE) {
206 return;
207 }
208 super.onPopulateAccessibilityEvent(host, event);
209 }
210 };
211
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
213
Lucas Dupineb9c5702017-05-10 16:57:09 -0700214 // Background gradient
215 private ColorExtractor mColorExtractor;
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700216 private ScrimDrawable mBackgroundDrawable;
Lucas Dupineb9c5702017-05-10 16:57:09 -0700217 private boolean mSupportsDarkText;
218
Tyler Gunnc5428972018-03-28 14:15:34 -0700219 private boolean mIsWfcEmergencyCallingWarningEnabled;
220 private float mDefaultDigitsTextSize;
221
Leo Hsue3512b62019-02-14 15:53:00 +0800222 private int mEntryType;
Leo Hsubc7553a2019-03-05 15:47:23 +0800223 private ShortcutViewUtils.Config mShortcutViewConfig;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800224
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800225 private EmergencyDialerMetricsLogger mMetricsLogger;
Shaotang Li8662a912018-07-04 16:53:01 +0800226
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 @Override
228 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
229 // Do nothing
230 }
231
232 @Override
233 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700234 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235 }
236
237 @Override
238 public void afterTextChanged(Editable input) {
239 // Check for special sequences, in particular the "**04" or "**05"
240 // sequences that allow you to enter PIN or PUK-related codes.
241 //
242 // But note we *don't* allow most other special sequences here,
243 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
244 // since those shouldn't be available if the device is locked.
245 //
246 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
247 // here, not the regular handleChars() method.
248 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
249 // A special sequence was entered, clear the digits
250 mDigits.getText().clear();
251 }
252
253 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800254 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 }
256
257 @Override
258 protected void onCreate(Bundle icicle) {
259 super.onCreate(icicle);
260
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800261 mMetricsLogger = new EmergencyDialerMetricsLogger(this);
262
Leo Hsue3512b62019-02-14 15:53:00 +0800263 mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
264 Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
265
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800267 setShowWhenLocked(true);
268 // Allow turning screen on
269 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270
Chuck Liaocdeebb42018-10-30 12:07:18 +0800271 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
272 PersistableBundle carrierConfig =
273 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
274
Leo Hsubc7553a2019-03-05 15:47:23 +0800275 mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
CY Cheng9a69c182018-06-15 21:20:10 +0800276 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsubc7553a2019-03-05 15:47:23 +0800277 + mShortcutViewConfig.isEnabled());
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800278
Lucas Dupineb9c5702017-05-10 16:57:09 -0700279 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800280
Leo Hsubc7553a2019-03-05 15:47:23 +0800281 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800282 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800283 updateTheme(false);
284 } else {
285 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
286 ColorExtractor.TYPE_EXTRA_DARK);
287 updateTheme(lockScreenColors.supportsDarkText());
288 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700289
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 setContentView(R.layout.emergency_dialer);
291
Tyler Gunnc5428972018-03-28 14:15:34 -0700292 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293 mDigits.setKeyListener(DialerKeyListener.getInstance());
294 mDigits.setOnClickListener(this);
295 mDigits.setOnKeyListener(this);
296 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700297 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700298 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 maybeAddNumberFormatting();
300
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700301 mBackgroundDrawable = new ScrimDrawable();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700302 Point displaySize = new Point();
303 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
304 .getDefaultDisplay().getSize(displaySize);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700305 mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled()
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800306 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700307 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700308
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 // Check for the presence of the keypad
310 View view = findViewById(R.id.one);
311 if (view != null) {
312 setupKeypad();
313 }
314
315 mDelete = findViewById(R.id.deleteButton);
316 mDelete.setOnClickListener(this);
317 mDelete.setOnLongClickListener(this);
318
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700319 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320
321 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700322 // Read carrier config through the public API because PhoneGlobals is not available when we
323 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700324 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 mDialButton.setOnClickListener(this);
326 } else {
327 mDialButton.setVisibility(View.GONE);
328 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700329 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
330 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
331 maybeShowWfcEmergencyCallingWarning();
332
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700333 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334
335 if (icicle != null) {
336 super.onRestoreInstanceState(icicle);
337 }
338
339 // Extract phone number from intent
340 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700341 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
343 if (number != null) {
344 mDigits.setText(number);
345 }
346 }
347
348 // if the mToneGenerator creation fails, just continue without it. It is
349 // a local audio signal, and is not as important as the dtmf tone itself.
350 synchronized (mToneGeneratorLock) {
351 if (mToneGenerator == null) {
352 try {
353 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
354 } catch (RuntimeException e) {
355 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
356 mToneGenerator = null;
357 }
358 }
359 }
360
361 final IntentFilter intentFilter = new IntentFilter();
362 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
363 registerReceiver(mBroadcastReceiver, intentFilter);
364
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700365 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800366
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800367 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
368
Leo Hsubc7553a2019-03-05 15:47:23 +0800369 if (mShortcutViewConfig.isEnabled()) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800370 setupEmergencyShortcutsView();
371 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700372 }
373
374 @Override
375 protected void onDestroy() {
376 super.onDestroy();
377 synchronized (mToneGeneratorLock) {
378 if (mToneGenerator != null) {
379 mToneGenerator.release();
380 mToneGenerator = null;
381 }
382 }
383 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800384 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
385 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
386 mShortcutDataSetObserver = null;
387 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700388 }
389
390 @Override
391 protected void onRestoreInstanceState(Bundle icicle) {
392 mLastNumber = icicle.getString(LAST_NUMBER);
393 }
394
395 @Override
396 protected void onSaveInstanceState(Bundle outState) {
397 super.onSaveInstanceState(outState);
398 outState.putString(LAST_NUMBER, mLastNumber);
399 }
400
401 /**
402 * Explicitly turn off number formatting, since it gets in the way of the emergency
403 * number detector
404 */
405 protected void maybeAddNumberFormatting() {
406 // Do nothing.
407 }
408
409 @Override
410 protected void onPostCreate(Bundle savedInstanceState) {
411 super.onPostCreate(savedInstanceState);
412
413 // This can't be done in onCreate(), since the auto-restoring of the digits
414 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
415 // is called. This method will be called every time the activity is created, and
416 // will always happen after onRestoreSavedInstanceState().
417 mDigits.addTextChangedListener(this);
418 }
419
420 private void setupKeypad() {
421 // Setup the listeners for the buttons
422 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700423 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
424 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700425 }
426
427 View view = findViewById(R.id.zero);
428 view.setOnLongClickListener(this);
429 }
430
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800431 @Override
432 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800433 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
434 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
Leo Hsubc7553a2019-03-05 15:47:23 +0800435 if (mShortcutViewConfig.isEnabled() && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800436 && mDialpadView.getVisibility() == View.VISIBLE) {
437 switchView(mEmergencyShortcutView, mDialpadView, true);
438 return;
439 }
440 super.onBackPressed();
441 }
442
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700443 /**
444 * handle key events
445 */
446 @Override
447 public boolean onKeyDown(int keyCode, KeyEvent event) {
448 switch (keyCode) {
449 // Happen when there's a "Call" hard button.
450 case KeyEvent.KEYCODE_CALL: {
451 if (TextUtils.isEmpty(mDigits.getText().toString())) {
452 // if we are adding a call from the InCallScreen and the phone
453 // number entered is empty, we just close the dialer to expose
454 // the InCallScreen under it.
455 finish();
456 } else {
457 // otherwise, we place the call.
458 placeCall();
459 }
460 return true;
461 }
462 }
463 return super.onKeyDown(keyCode, event);
464 }
465
466 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700467 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
469 mDigits.onKeyDown(keyCode, event);
470 }
471
472 @Override
473 public boolean onKey(View view, int keyCode, KeyEvent event) {
474 switch (view.getId()) {
475 case R.id.digits:
476 // Happen when "Done" button of the IME is pressed. This can happen when this
477 // Activity is forced into landscape mode due to a desk dock.
478 if (keyCode == KeyEvent.KEYCODE_ENTER
479 && event.getAction() == KeyEvent.ACTION_UP) {
480 placeCall();
481 return true;
482 }
483 break;
484 }
485 return false;
486 }
487
488 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700489 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800490 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700491 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800492 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700493 return handled;
494 }
495
496 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800497 public void onConfirmClick(EmergencyShortcutButton button) {
498 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800499 String phoneNumber = button.getPhoneNumber();
500
501 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800502 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800503
504 // Write metrics when user has intention to make a call from a shortcut button.
505 mMetricsLogger.logPlaceCall(DialedFrom.SHORTCUT, PhoneNumberType.HAS_SHORTCUT,
506 mShortcutViewConfig.getPhoneInfo());
507
Leo Hsu43d670a2018-12-04 15:40:36 +0800508 placeCall(phoneNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800509 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800510 } else {
511 Log.d(LOG_TAG, "emergency number is empty");
512 }
513 }
514
515 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800516 public void onConfirmClick(EmergencyInfoGroup button) {
517 if (button == null) return;
518
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800519 Intent intent = (Intent) button.getTag(R.id.tag_intent);
520 if (intent != null) {
521 startActivity(intent);
522 }
523 }
524
525 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700526 public void onClick(View view) {
527 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700528 case R.id.deleteButton: {
529 keyPressed(KeyEvent.KEYCODE_DEL);
530 return;
531 }
532 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700533 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700534 placeCall();
535 return;
536 }
537 case R.id.digits: {
538 if (mDigits.length() != 0) {
539 mDigits.setCursorVisible(true);
540 }
541 return;
542 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800543 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800544 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800545 switchView(mDialpadView, mEmergencyShortcutView, true);
546 return;
547 }
Yorke Lee23a70732014-08-14 17:12:01 -0700548 }
549 }
550
551 @Override
552 public void onPressed(View view, boolean pressed) {
553 if (!pressed) {
554 return;
555 }
556 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700557 case R.id.one: {
558 playTone(ToneGenerator.TONE_DTMF_1);
559 keyPressed(KeyEvent.KEYCODE_1);
560 return;
561 }
562 case R.id.two: {
563 playTone(ToneGenerator.TONE_DTMF_2);
564 keyPressed(KeyEvent.KEYCODE_2);
565 return;
566 }
567 case R.id.three: {
568 playTone(ToneGenerator.TONE_DTMF_3);
569 keyPressed(KeyEvent.KEYCODE_3);
570 return;
571 }
572 case R.id.four: {
573 playTone(ToneGenerator.TONE_DTMF_4);
574 keyPressed(KeyEvent.KEYCODE_4);
575 return;
576 }
577 case R.id.five: {
578 playTone(ToneGenerator.TONE_DTMF_5);
579 keyPressed(KeyEvent.KEYCODE_5);
580 return;
581 }
582 case R.id.six: {
583 playTone(ToneGenerator.TONE_DTMF_6);
584 keyPressed(KeyEvent.KEYCODE_6);
585 return;
586 }
587 case R.id.seven: {
588 playTone(ToneGenerator.TONE_DTMF_7);
589 keyPressed(KeyEvent.KEYCODE_7);
590 return;
591 }
592 case R.id.eight: {
593 playTone(ToneGenerator.TONE_DTMF_8);
594 keyPressed(KeyEvent.KEYCODE_8);
595 return;
596 }
597 case R.id.nine: {
598 playTone(ToneGenerator.TONE_DTMF_9);
599 keyPressed(KeyEvent.KEYCODE_9);
600 return;
601 }
602 case R.id.zero: {
603 playTone(ToneGenerator.TONE_DTMF_0);
604 keyPressed(KeyEvent.KEYCODE_0);
605 return;
606 }
607 case R.id.pound: {
608 playTone(ToneGenerator.TONE_DTMF_P);
609 keyPressed(KeyEvent.KEYCODE_POUND);
610 return;
611 }
612 case R.id.star: {
613 playTone(ToneGenerator.TONE_DTMF_S);
614 keyPressed(KeyEvent.KEYCODE_STAR);
615 return;
616 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 }
618 }
619
620 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700621 * called for long touch events
622 */
623 @Override
624 public boolean onLongClick(View view) {
625 int id = view.getId();
626 switch (id) {
627 case R.id.deleteButton: {
628 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700629 return true;
630 }
631 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700632 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700633 keyPressed(KeyEvent.KEYCODE_PLUS);
634 return true;
635 }
636 }
637 return false;
638 }
639
640 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700641 protected void onStart() {
642 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800643
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800644 mMetricsLogger.logLaunchEmergencyDialer(mEntryType,
645 mShortcutViewConfig.isEnabled() ? UiModeErrorCode.SUCCESS
646 : UiModeErrorCode.UNSPECIFIED_ERROR);
Shaotang Li8662a912018-07-04 16:53:01 +0800647
Leo Hsubc7553a2019-03-05 15:47:23 +0800648 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800649 // Shortcut view doesn't support dark text theme.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700650 mBackgroundDrawable.setColor(Color.BLACK, false);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800651 updateTheme(false);
652 } else {
653 mColorExtractor.addOnColorsChangedListener(this);
654 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
655 ColorExtractor.TYPE_EXTRA_DARK);
656 // Do not animate when view isn't visible yet, just set an initial state.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700657 mBackgroundDrawable.setColor(lockScreenColors.getMainColor(), false);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800658 updateTheme(lockScreenColors.supportsDarkText());
659 }
CY Cheng9a69c182018-06-15 21:20:10 +0800660
Leo Hsubc7553a2019-03-05 15:47:23 +0800661 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800662 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800663 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700664 }
665
666 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 protected void onResume() {
668 super.onResume();
669
670 // retrieve the DTMF tone play back setting.
671 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
672 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
673
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700674 // if the mToneGenerator creation fails, just continue without it. It is
675 // a local audio signal, and is not as important as the dtmf tone itself.
676 synchronized (mToneGeneratorLock) {
677 if (mToneGenerator == null) {
678 try {
679 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
680 TONE_RELATIVE_VOLUME);
681 } catch (RuntimeException e) {
682 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
683 mToneGenerator = null;
684 }
685 }
686 }
687
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 updateDialAndDeleteButtonStateEnabledAttr();
689 }
690
691 @Override
692 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700693 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 }
695
Lucas Dupineb9c5702017-05-10 16:57:09 -0700696 @Override
697 protected void onStop() {
698 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700699 mColorExtractor.removeOnColorsChangedListener(this);
700 }
701
702 /**
703 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800704 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700705 * @param supportsDarkText true if gradient supports dark text
706 */
707 private void updateTheme(boolean supportsDarkText) {
708 if (mSupportsDarkText == supportsDarkText) {
709 return;
710 }
711 mSupportsDarkText = supportsDarkText;
712
713 // We can't change themes after inflation, in this case we'll have to recreate
714 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700715 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700716 recreate();
717 return;
718 }
719
720 int vis = getWindow().getDecorView().getSystemUiVisibility();
721 if (supportsDarkText) {
722 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
723 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
724 setTheme(R.style.EmergencyDialerThemeDark);
725 } else {
726 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
727 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
728 setTheme(R.style.EmergencyDialerTheme);
729 }
730 getWindow().getDecorView().setSystemUiVisibility(vis);
731 }
732
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 /**
734 * place the call, but check to make sure it is a viable number.
735 */
736 private void placeCall() {
737 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900738
739 // Convert into emergency number according to emergency conversion map.
740 // If conversion map is not defined (this is default), this method does
741 // nothing and just returns input number.
742 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
743
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800744 @DialedFrom final int dialedFrom =
745 mShortcutViewConfig.isEnabled() ? DialedFrom.FASTER_LAUNCHER_DIALPAD
746 : DialedFrom.TRADITIONAL_DIALPAD;
747 @PhoneNumberType final int phoneNumberType;
748
Leo Hsubc7553a2019-03-05 15:47:23 +0800749 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800750 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800751 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800752 isEmergencyNumber = true;
753 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800754 phoneNumberType = PhoneNumberType.HAS_SHORTCUT;
755 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
756 // If a number from SIM/network/... is categoried as police/ambulance/fire,
757 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
758 // shortcut button because a number provided by database has higher priority.
759 isEmergencyNumber = true;
760 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
761 phoneNumberType = PhoneNumberType.NO_SHORTCUT;
Leo Hsubc7553a2019-03-05 15:47:23 +0800762 } else {
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800763 isEmergencyNumber = getSystemService(TelephonyManager.class)
764 .isEmergencyNumber(mLastNumber);
765 phoneNumberType = PhoneNumberType.NO_SHORTCUT;
Leo Hsu43d670a2018-12-04 15:40:36 +0800766 }
767
768 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
770
771 // place the call if it is a valid number
772 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
773 // There is no number entered.
774 playTone(ToneGenerator.TONE_PROP_NACK);
775 return;
776 }
Shaotang Li8662a912018-07-04 16:53:01 +0800777
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800778 // Write metrics when user has intention to make a call from dialpad
779 mMetricsLogger.logPlaceCall(dialedFrom, phoneNumberType, phoneToMakeCall);
Shaotang Li8662a912018-07-04 16:53:01 +0800780
Leo Hsu43d670a2018-12-04 15:40:36 +0800781 placeCall(mLastNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD,
782 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700783 } else {
784 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
785
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800786 // Write metrics when user has intention to make a call of a non-emergency number, even
787 // this number is rejected.
788 mMetricsLogger.logPlaceCall(dialedFrom, PhoneNumberType.NOT_EMERGENCY_NUMBER,
789 phoneToMakeCall);
Shaotang Li8662a912018-07-04 16:53:01 +0800790
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
792 }
Yorke Lee9b341512014-10-17 11:36:41 -0700793 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 }
795
Leo Hsu43d670a2018-12-04 15:40:36 +0800796 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800797 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
798 + ", entry = " + entryTypeToString(mEntryType));
799
Leo Hsu43d670a2018-12-04 15:40:36 +0800800 Bundle extras = new Bundle();
801 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800802 /**
803 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
804 * when the dialed number is ambiguous and identified as both emergency number and any
805 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
806 * number in a country and a non-emergency number of a carrier's customer service hotline.
807 */
808 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800809
810 if (phone != null && phone.getPhoneAccountHandle() != null) {
811 // Requests to dial through the specified phone.
812 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
813 phone.getPhoneAccountHandle());
814 }
815
816 TelecomManager tm = this.getSystemService(TelecomManager.class);
817 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
818 }
819
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700820 /**
821 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
822 *
823 * The tone is played locally, using the audio stream for phone calls.
824 * Tones are played only if the "Audible touch tones" user preference
825 * is checked, and are NOT played if the device is in silent mode.
826 *
827 * @param tone a tone code from {@link ToneGenerator}
828 */
829 void playTone(int tone) {
830 // if local tone playback is disabled, just return.
831 if (!mDTMFToneEnabled) {
832 return;
833 }
834
835 // Also do nothing if the phone is in silent mode.
836 // We need to re-check the ringer mode for *every* playTone()
837 // call, rather than keeping a local flag that's updated in
838 // onResume(), since it's possible to toggle silent mode without
839 // leaving the current activity (via the ENDCALL-longpress menu.)
840 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
841 int ringerMode = audioManager.getRingerMode();
842 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800843 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700844 return;
845 }
846
847 synchronized (mToneGeneratorLock) {
848 if (mToneGenerator == null) {
849 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
850 return;
851 }
852
853 // Start the new tone (will stop any playing tone)
854 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
855 }
856 }
857
858 private CharSequence createErrorMessage(String number) {
859 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800860 String errorString = getString(R.string.dial_emergency_error, number);
861 int startingPosition = errorString.indexOf(number);
862 int endingPosition = startingPosition + number.length();
863 Spannable result = new SpannableString(errorString);
864 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
865 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866 } else {
867 return getText(R.string.dial_emergency_empty_error).toString();
868 }
869 }
870
871 @Override
872 protected Dialog onCreateDialog(int id) {
873 AlertDialog dialog = null;
874 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
875 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700876 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700877 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
878 .setMessage(createErrorMessage(mLastNumber))
879 .setPositiveButton(R.string.ok, null)
880 .setCancelable(true).create();
881
882 // blur stuff behind the dialog
883 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700884 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700885 }
886 return dialog;
887 }
888
889 @Override
890 protected void onPrepareDialog(int id, Dialog dialog) {
891 super.onPrepareDialog(id, dialog);
892 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
893 AlertDialog alert = (AlertDialog) dialog;
894 alert.setMessage(createErrorMessage(mLastNumber));
895 }
896 }
897
Andrew Leed5631e82014-10-08 16:03:58 -0700898 @Override
899 public boolean onOptionsItemSelected(MenuItem item) {
900 final int itemId = item.getItemId();
901 if (itemId == android.R.id.home) {
902 onBackPressed();
903 return true;
904 }
905 return super.onOptionsItemSelected(item);
906 }
907
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 /**
909 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
910 */
911 private void updateDialAndDeleteButtonStateEnabledAttr() {
912 final boolean notEmpty = mDigits.length() != 0;
913
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 mDelete.setEnabled(notEmpty);
915 }
Yorke Lee91311662014-10-24 14:50:45 -0700916
917 /**
918 * Remove the digit just before the current position. Used by various long pressed callbacks
919 * to remove the digit that was populated as a result of the short click.
920 */
921 private void removePreviousDigitIfPossible() {
922 final int currentPosition = mDigits.getSelectionStart();
923 if (currentPosition > 0) {
924 mDigits.setSelection(currentPosition);
925 mDigits.getText().delete(currentPosition - 1, currentPosition);
926 }
927 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800928
929 /**
930 * Update the text-to-speech annotations in the edit field.
931 */
932 private void updateTtsSpans() {
933 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
934 mDigits.getText().removeSpan(o);
935 }
936 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
937 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700938
939 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700940 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700941 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700942 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
943 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700944 mBackgroundDrawable.setColor(colors.getMainColor(), true /* animated */);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700945 updateTheme(colors.supportsDarkText());
946 }
947 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700948
949 /**
950 * Where a carrier requires a warning that emergency calling is not available while on WFC,
951 * add hint text above the dial pad which warns the user of this case.
952 */
953 private void maybeShowWfcEmergencyCallingWarning() {
954 if (!mIsWfcEmergencyCallingWarningEnabled) {
955 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
956 return;
957 }
958
959 // Use an async task rather than calling into Telephony on UI thread.
960 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
961 @Override
962 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800963 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700964 boolean isWfcAvailable = tm.isWifiCallingAvailable();
965 ServiceState ss = tm.getServiceState();
966 boolean isCellAvailable =
967 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
968 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800969 + " isCellAvailable=" + isCellAvailable
970 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700971 return isWfcAvailable && !isCellAvailable;
972 }
973
974 @Override
975 protected void onPostExecute(Boolean result) {
976 if (result.booleanValue()) {
977 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
978 mDigits.setHint(R.string.dial_emergency_calling_not_available);
979 } else {
980 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
981 mDigits.setHint("");
982 }
983 maybeChangeHintSize();
984 }
985 };
986 showWfcWarningTask.execute((Void) null);
987 }
988
989 /**
990 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
991 * edit view and set the font size to a smaller size appropriate for the emergency calling
992 * warning.
993 */
994 private void maybeChangeHintSize() {
995 if (TextUtils.isEmpty(mDigits.getHint())
996 || !TextUtils.isEmpty(mDigits.getText().toString())) {
997 // No hint or there are dialed digits, so use default size.
998 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
999 // By default, the digits view auto-resizes to fit the text it contains, so
1000 // enable that now.
1001 mDigits.setResizeEnabled(true);
1002 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1003 } else {
1004 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1005 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1006 R.dimen.emergency_call_warning_size));
1007 // Since we're populating this with a static text string, disable auto-resize.
1008 mDigits.setResizeEnabled(false);
1009 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1010 }
1011 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001012
1013 private void setupEmergencyShortcutsView() {
1014 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1015 mDialpadView = findViewById(R.id.emergency_dialer);
1016
Chihhang Chuangcaba0da2018-08-02 22:25:06 +08001017 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1018 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1019
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001020 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1021 dialpadButton.setOnClickListener(this);
1022
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001023 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001024
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001025 mEmergencyShortcutButtonList = new ArrayList<>();
1026 setupEmergencyCallShortcutButton();
1027
Leo Hsu43d670a2018-12-04 15:40:36 +08001028 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001029
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001030 switchView(mEmergencyShortcutView, mDialpadView, false);
1031 }
1032
Leo Hsu43d670a2018-12-04 15:40:36 +08001033 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001034 final View locationInfo = findViewById(R.id.location_info);
1035
Leo Hsubc7553a2019-03-05 15:47:23 +08001036 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +08001037 String countryName = null;
1038 if (!TextUtils.isEmpty(countryIso)) {
1039 Locale locale = Locale.getDefault();
1040 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1041 .getDisplayCountry();
1042 }
1043 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001044 locationInfo.setVisibility(View.INVISIBLE);
1045 } else {
1046 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001047 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001048 locationInfo.setVisibility(View.VISIBLE);
1049 }
1050 }
1051
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001052 private void setupEmergencyCallShortcutButton() {
1053 final ViewGroup shortcutButtonContainer = findViewById(
1054 R.id.emergency_shortcut_buttons_container);
1055 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001056 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001057
CY Cheng9a69c182018-06-15 21:20:10 +08001058 mShortcutAdapter = new EccShortcutAdapter(this) {
1059 @Override
1060 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1061 CharSequence description, int iconRes) {
1062 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1063 .inflate(R.layout.emergency_shortcut_button, parent, false);
1064 button.setPhoneNumber(number);
1065 button.setPhoneDescription(description);
1066 button.setPhoneTypeIcon(iconRes);
1067 button.setOnConfirmClickListener(EmergencyDialer.this);
1068 return button;
1069 }
1070 };
1071 mShortcutDataSetObserver = new DataSetObserver() {
1072 @Override
1073 public void onChanged() {
1074 super.onChanged();
1075 updateLayout();
1076 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001077
CY Cheng9a69c182018-06-15 21:20:10 +08001078 @Override
1079 public void onInvalidated() {
1080 super.onInvalidated();
1081 updateLayout();
1082 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001083
CY Cheng9a69c182018-06-15 21:20:10 +08001084 private void updateLayout() {
1085 // clear previous added buttons
1086 shortcutButtonContainer.removeAllViews();
1087 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001088
CY Cheng9a69c182018-06-15 21:20:10 +08001089 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1090 EmergencyShortcutButton button = (EmergencyShortcutButton)
1091 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1092 mEmergencyShortcutButtonList.add(button);
1093 shortcutButtonContainer.addView(button);
1094 }
1095
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001096 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001097 if (mEmergencyShortcutButtonList.size() > 1) {
1098 emergencyNumberTitle.setText(getString(
1099 R.string.numerous_emergency_numbers_title));
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001100 // Update mEmergencyInfoGroup margin to avoid UI overlay when
1101 // emergency shortcut button more than 2.
1102 if (mEmergencyShortcutButtonList.size() > 2) {
1103 mEmergencyInfoGroup.updateLayoutMargin();
1104 }
CY Cheng9a69c182018-06-15 21:20:10 +08001105 } else {
1106 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1107 }
1108 }
1109 };
1110 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1111 }
1112
Leo Hsu43d670a2018-12-04 15:40:36 +08001113 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001114 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001115 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001116 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001117 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001118 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001119 }
1120 }
1121
1122 /**
1123 * Called by the activity before a touch event is dispatched to the view hierarchy.
1124 */
1125 private void onPreTouchEvent(MotionEvent event) {
1126 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001127 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001128
1129 if (mEmergencyShortcutButtonList != null) {
1130 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1131 button.onPreTouchEvent(event);
1132 }
1133 }
1134 }
1135
1136 /**
1137 * Called by the activity after a touch event is dispatched to the view hierarchy.
1138 */
1139 private void onPostTouchEvent(MotionEvent event) {
1140 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001141 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001142
1143 if (mEmergencyShortcutButtonList != null) {
1144 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1145 button.onPostTouchEvent(event);
1146 }
1147 }
1148 }
1149
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001150 /**
1151 * Switch two view.
1152 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001153 * @param displayView the view would be displayed.
1154 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001155 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1156 */
1157 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1158 if (displayView == null || hideView == null) {
1159 return;
1160 }
1161
1162 if (displayView.getVisibility() == View.VISIBLE) {
1163 return;
1164 }
1165
1166 if (hasAnimation) {
1167 crossfade(hideView, displayView);
1168 } else {
1169 hideView.setVisibility(View.GONE);
1170 displayView.setVisibility(View.VISIBLE);
1171 }
1172 }
1173
1174 /**
1175 * Fade out and fade in animation between two view transition.
1176 */
1177 private void crossfade(View fadeOutView, View fadeInView) {
1178 if (fadeOutView == null || fadeInView == null) {
1179 return;
1180 }
1181 final int shortAnimationDuration = getResources().getInteger(
1182 android.R.integer.config_shortAnimTime);
1183
1184 fadeInView.setAlpha(0f);
1185 fadeInView.setVisibility(View.VISIBLE);
1186
1187 fadeInView.animate()
1188 .alpha(1f)
1189 .setDuration(shortAnimationDuration)
1190 .setListener(null);
1191
1192 fadeOutView.animate()
1193 .alpha(0f)
1194 .setDuration(shortAnimationDuration)
1195 .setListener(new AnimatorListenerAdapter() {
1196 @Override
1197 public void onAnimationEnd(Animator animation) {
1198 fadeOutView.setVisibility(View.GONE);
1199 }
1200 });
1201 }
Shaotang Li8662a912018-07-04 16:53:01 +08001202
Shaotang Li8662a912018-07-04 16:53:01 +08001203 private boolean isShortcutNumber(String number) {
1204 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1205 return false;
1206 }
1207
1208 boolean isShortcut = false;
1209 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1210 if (button != null && number.equals(button.getPhoneNumber())) {
1211 isShortcut = true;
1212 break;
1213 }
1214 }
1215 return isShortcut;
1216 }
Leo Hsue3512b62019-02-14 15:53:00 +08001217
1218 private String entryTypeToString(int entryType) {
1219 switch (entryType) {
1220 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1221 return "LockScreen";
1222 case ENTRY_TYPE_POWER_MENU:
1223 return "PowerMenu";
1224 default:
1225 return "Unknown-" + entryType;
1226 }
1227 }
1228
1229 private String callSourceToString(int callSource) {
1230 switch (callSource) {
1231 case ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD:
1232 return "DialPad";
1233 case ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT:
1234 return "Shortcut";
1235 default:
1236 return "Unknown-" + callSource;
1237 }
1238 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001239}