blob: 66701d59b49aae8c629eace1261d78c583e0f494 [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;
Shaotang Li8662a912018-07-04 16:53:01 +080026import android.app.KeyguardManager;
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;
Shaotang Li8662a912018-07-04 16:53:01 +080035import android.hardware.Sensor;
36import android.hardware.SensorEvent;
37import android.hardware.SensorEventListener;
38import android.hardware.SensorManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.media.AudioManager;
40import android.media.ToneGenerator;
Shaotang Li8662a912018-07-04 16:53:01 +080041import android.metrics.LogMaker;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070043import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070045import android.os.PersistableBundle;
Shaotang Li8662a912018-07-04 16:53:01 +080046import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.provider.Settings;
Shaotang Li5d906c12018-07-19 17:32:42 +080048import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070049import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070050import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080053import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070054import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070055import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070056import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070058import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080059import android.text.Spannable;
60import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
62import android.text.TextWatcher;
63import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080064import android.text.style.TtsSpan;
yuanjiahsuce5a28c2018-09-06 19:54:15 +080065import android.util.FeatureFlagUtils;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070067import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070068import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070070import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070071import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080073import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080074import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080076import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080077import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078
Lucas Dupinaf9e9912017-06-22 12:39:39 -070079import com.android.internal.colorextraction.ColorExtractor;
80import com.android.internal.colorextraction.ColorExtractor.GradientColors;
81import com.android.internal.colorextraction.drawable.GradientDrawable;
Shaotang Li8662a912018-07-04 16:53:01 +080082import com.android.internal.logging.MetricsLogger;
83import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Yorke Lee23a70732014-08-14 17:12:01 -070084import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070085import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070086import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080087import com.android.phone.ecc.CountryEccInfo;
88import com.android.phone.ecc.EccInfoHelper;
Leo Hsu779569a2018-07-10 11:37:52 +080089import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090
Chihhang Chuang92cfe512018-06-07 16:25:27 +080091import java.util.ArrayList;
92import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080093import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080094
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095/**
96 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
97 *
98 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
99 * activity from apps/Contacts) that:
100 * 1. Allows ONLY emergency calls to be dialed
101 * 2. Disallows voicemail functionality
102 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
103 * activity to stay in front of the keyguard.
104 *
105 * TODO: Even though this is an ultra-simplified version of the normal
106 * dialer, there's still lots of code duplication between this class and
107 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
108 * moved into a shared base class that would live in the framework?
109 * Or could we figure out some way to move *this* class into apps/Contacts
110 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800111 *
112 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800113 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
114 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800115 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800116 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700117 */
118public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700119 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800120 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Shaotang Li8662a912018-07-04 16:53:01 +0800121 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener {
122
123 private class MetricsWriter {
124 // Metrics constants indicating the entry type that user opened emergency dialer.
125 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
126 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
127 public static final int ENTRY_TYPE_UNKNOWN = 0;
128 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
129 public static final int ENTRY_TYPE_POWER_MENU = 2;
130
131 // Metrics constants indicating the UI that user made phone call.
132 public static final int CALL_SOURCE_DIALPAD = 0;
133 public static final int CALL_SOURCE_SHORTCUT = 1;
134
135 // Metrics constants indicating the phone number type of a call user made.
136 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
137 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
138
139 // Metrics constants indicating the actions performed by user.
140 public static final int USER_ACTION_NONE = 0x0;
141 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
142 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
143 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
144 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
145
146 private MetricsLogger mMetricsLogger = new MetricsLogger();
147
148 public void writeMetricsForEnter() {
149 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
150 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
151 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
152 .setType(MetricsEvent.TYPE_OPEN)
153 .setSubtype(entryType)
154 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
155 keyguard.isKeyguardLocked() ? 1 : 0));
156 }
157
158 public void writeMetricsForExit() {
159 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
160 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
161 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
162 .setType(MetricsEvent.TYPE_CLOSE)
163 .setSubtype(entryType)
164 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
165 .addTaggedData(
166 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
167 }
168
169 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
170 boolean hasShortcut) {
171 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
172 .setType(MetricsEvent.TYPE_ACTION)
173 .setSubtype(callSource)
174 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
175 phoneNumberType)
176 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
177 hasShortcut ? 1 : 0)
178 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
179 mIsProximityNear ? 1 : 0));
180 }
181 }
182
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 // Keys used with onSaveInstanceState().
184 private static final String LAST_NUMBER = "lastNumber";
185
186 // Intent action for this activity.
187 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
188
Shaotang Li8662a912018-07-04 16:53:01 +0800189 /**
190 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
191 * the emergency dialer.
192 */
193 public static final String EXTRA_ENTRY_TYPE =
194 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
195
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 // List of dialer button IDs.
197 private static final int[] DIALER_KEYS = new int[] {
198 R.id.one, R.id.two, R.id.three,
199 R.id.four, R.id.five, R.id.six,
200 R.id.seven, R.id.eight, R.id.nine,
201 R.id.star, R.id.zero, R.id.pound };
202
203 // Debug constants.
204 private static final boolean DBG = false;
205 private static final String LOG_TAG = "EmergencyDialer";
206
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207 /** The length of DTMF tones in milliseconds */
208 private static final int TONE_LENGTH_MS = 150;
209
210 /** The DTMF tone volume relative to other sounds in the stream */
211 private static final int TONE_RELATIVE_VOLUME = 80;
212
213 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
214 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
215
216 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
217
Lucas Dupineb9c5702017-05-10 16:57:09 -0700218 /** 90% opacity, different from other gradients **/
219 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
220
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800221 /** 85% opacity for black background **/
222 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
223
CY Cheng9a69c182018-06-15 21:20:10 +0800224 /** Size limit of emergency shortcut buttons container. **/
225 private static final int SHORTCUT_SIZE_LIMIT = 3;
226
Tyler Gunnc5428972018-03-28 14:15:34 -0700227 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 private View mDialButton;
229 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800230 private View mEmergencyShortcutView;
231 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232
CY Cheng9a69c182018-06-15 21:20:10 +0800233 private EccInfoHelper mEccInfoHelper;
234
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800235 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800236 private EccShortcutAdapter mShortcutAdapter;
237 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800238
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 private ToneGenerator mToneGenerator;
240 private Object mToneGeneratorLock = new Object();
241
242 // determines if we want to playback local DTMF tones.
243 private boolean mDTMFToneEnabled;
244
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700245 private EmergencyActionGroup mEmergencyActionGroup;
246
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 // close activity when screen turns off
248 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
249 @Override
250 public void onReceive(Context context, Intent intent) {
251 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700252 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 }
254 }
255 };
256
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800257 /**
258 * Customize accessibility methods in View.
259 */
260 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
261
262 /**
263 * Stop AccessiblityService from reading the title of a hidden View.
264 *
265 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
266 * in the animation end. The view with an accessibility pane title would call the
267 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
268 * accessibility service to read the pane title of fade out view instead of pane title of
269 * fade in view. So it need to filter out the event called by vanished pane.
270 */
271 @Override
272 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
273 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
274 && host.getVisibility() == View.GONE) {
275 return;
276 }
277 super.onPopulateAccessibilityEvent(host, event);
278 }
279 };
280
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
282
Lucas Dupineb9c5702017-05-10 16:57:09 -0700283 // Background gradient
284 private ColorExtractor mColorExtractor;
285 private GradientDrawable mBackgroundGradient;
286 private boolean mSupportsDarkText;
287
Tyler Gunnc5428972018-03-28 14:15:34 -0700288 private boolean mIsWfcEmergencyCallingWarningEnabled;
289 private float mDefaultDigitsTextSize;
290
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800291 private boolean mAreEmergencyDialerShortcutsEnabled;
292
Shaotang Li8662a912018-07-04 16:53:01 +0800293 private MetricsWriter mMetricsWriter;
294 private SensorManager mSensorManager;
295 private Sensor mProximitySensor;
296 private boolean mIsProximityNear = false;
297
298 /**
299 * The time, in millis, since boot when user opened emergency dialer.
300 * This is used when calculating the user stay duration for metrics data.
301 */
302 private long mUserEnterTimeMillis = 0;
303
304 /**
305 * Bit flag indicating the actions performed by user. This is used for metrics data.
306 */
307 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
308
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 @Override
310 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
311 // Do nothing
312 }
313
314 @Override
315 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700316 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 }
318
319 @Override
320 public void afterTextChanged(Editable input) {
321 // Check for special sequences, in particular the "**04" or "**05"
322 // sequences that allow you to enter PIN or PUK-related codes.
323 //
324 // But note we *don't* allow most other special sequences here,
325 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
326 // since those shouldn't be available if the device is locked.
327 //
328 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
329 // here, not the regular handleChars() method.
330 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
331 // A special sequence was entered, clear the digits
332 mDigits.getText().clear();
333 }
334
335 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800336 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 }
338
339 @Override
340 protected void onCreate(Bundle icicle) {
341 super.onCreate(icicle);
342
Shaotang Li8662a912018-07-04 16:53:01 +0800343 mMetricsWriter = new MetricsWriter();
344 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
345 if (mSensorManager != null) {
346 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
347 }
348
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700349 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800350 setShowWhenLocked(true);
351 // Allow turning screen on
352 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353
yuanjiahsuce5a28c2018-09-06 19:54:15 +0800354 mAreEmergencyDialerShortcutsEnabled = FeatureFlagUtils
355 .isEnabled(this, FeatureFlagUtils.EMERGENCY_DIAL_SHORTCUTS);
CY Cheng9a69c182018-06-15 21:20:10 +0800356 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
357 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800358
Lucas Dupineb9c5702017-05-10 16:57:09 -0700359 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800360
361 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
362 // And the background color is black with 85% opacity.
363 if (mAreEmergencyDialerShortcutsEnabled) {
364 updateTheme(false);
365 } else {
366 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
367 ColorExtractor.TYPE_EXTRA_DARK);
368 updateTheme(lockScreenColors.supportsDarkText());
369 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700370
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371 setContentView(R.layout.emergency_dialer);
372
Tyler Gunnc5428972018-03-28 14:15:34 -0700373 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374 mDigits.setKeyListener(DialerKeyListener.getInstance());
375 mDigits.setOnClickListener(this);
376 mDigits.setOnKeyListener(this);
377 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700378 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700379 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 maybeAddNumberFormatting();
381
Lucas Dupineb9c5702017-05-10 16:57:09 -0700382 mBackgroundGradient = new GradientDrawable(this);
383 Point displaySize = new Point();
384 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
385 .getDefaultDisplay().getSize(displaySize);
386 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800387 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
388 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700389 getWindow().setBackgroundDrawable(mBackgroundGradient);
390
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391 // Check for the presence of the keypad
392 View view = findViewById(R.id.one);
393 if (view != null) {
394 setupKeypad();
395 }
396
397 mDelete = findViewById(R.id.deleteButton);
398 mDelete.setOnClickListener(this);
399 mDelete.setOnLongClickListener(this);
400
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700401 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402
403 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700404 // Read carrier config through the public API because PhoneGlobals is not available when we
405 // run as a secondary user.
406 CarrierConfigManager configMgr =
407 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
408 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800409 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700410
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700411 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700412 mDialButton.setOnClickListener(this);
413 } else {
414 mDialButton.setVisibility(View.GONE);
415 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700416 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
417 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
418 maybeShowWfcEmergencyCallingWarning();
419
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700420 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700421
422 if (icicle != null) {
423 super.onRestoreInstanceState(icicle);
424 }
425
426 // Extract phone number from intent
427 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700428 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
430 if (number != null) {
431 mDigits.setText(number);
432 }
433 }
434
435 // if the mToneGenerator creation fails, just continue without it. It is
436 // a local audio signal, and is not as important as the dtmf tone itself.
437 synchronized (mToneGeneratorLock) {
438 if (mToneGenerator == null) {
439 try {
440 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
441 } catch (RuntimeException e) {
442 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
443 mToneGenerator = null;
444 }
445 }
446 }
447
448 final IntentFilter intentFilter = new IntentFilter();
449 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
450 registerReceiver(mBroadcastReceiver, intentFilter);
451
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700452 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800453
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800454 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu779569a2018-07-10 11:37:52 +0800455 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800456 setupEmergencyShortcutsView();
457 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700458 }
459
460 @Override
461 protected void onDestroy() {
462 super.onDestroy();
463 synchronized (mToneGeneratorLock) {
464 if (mToneGenerator != null) {
465 mToneGenerator.release();
466 mToneGenerator = null;
467 }
468 }
469 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800470 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
471 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
472 mShortcutDataSetObserver = null;
473 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700474 }
475
476 @Override
477 protected void onRestoreInstanceState(Bundle icicle) {
478 mLastNumber = icicle.getString(LAST_NUMBER);
479 }
480
481 @Override
482 protected void onSaveInstanceState(Bundle outState) {
483 super.onSaveInstanceState(outState);
484 outState.putString(LAST_NUMBER, mLastNumber);
485 }
486
487 /**
488 * Explicitly turn off number formatting, since it gets in the way of the emergency
489 * number detector
490 */
491 protected void maybeAddNumberFormatting() {
492 // Do nothing.
493 }
494
495 @Override
496 protected void onPostCreate(Bundle savedInstanceState) {
497 super.onPostCreate(savedInstanceState);
498
499 // This can't be done in onCreate(), since the auto-restoring of the digits
500 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
501 // is called. This method will be called every time the activity is created, and
502 // will always happen after onRestoreSavedInstanceState().
503 mDigits.addTextChangedListener(this);
504 }
505
506 private void setupKeypad() {
507 // Setup the listeners for the buttons
508 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700509 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
510 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700511 }
512
513 View view = findViewById(R.id.zero);
514 view.setOnLongClickListener(this);
515 }
516
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800517 @Override
518 public void onBackPressed() {
519 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800520 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800521 // Otherwise, it would finish the activity.
522 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
523 && mDialpadView.getVisibility() == View.VISIBLE) {
524 switchView(mEmergencyShortcutView, mDialpadView, true);
525 return;
526 }
527 super.onBackPressed();
528 }
529
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530 /**
531 * handle key events
532 */
533 @Override
534 public boolean onKeyDown(int keyCode, KeyEvent event) {
535 switch (keyCode) {
536 // Happen when there's a "Call" hard button.
537 case KeyEvent.KEYCODE_CALL: {
538 if (TextUtils.isEmpty(mDigits.getText().toString())) {
539 // if we are adding a call from the InCallScreen and the phone
540 // number entered is empty, we just close the dialer to expose
541 // the InCallScreen under it.
542 finish();
543 } else {
544 // otherwise, we place the call.
545 placeCall();
546 }
547 return true;
548 }
549 }
550 return super.onKeyDown(keyCode, event);
551 }
552
553 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700554 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
556 mDigits.onKeyDown(keyCode, event);
557 }
558
559 @Override
560 public boolean onKey(View view, int keyCode, KeyEvent event) {
561 switch (view.getId()) {
562 case R.id.digits:
563 // Happen when "Done" button of the IME is pressed. This can happen when this
564 // Activity is forced into landscape mode due to a desk dock.
565 if (keyCode == KeyEvent.KEYCODE_ENTER
566 && event.getAction() == KeyEvent.ACTION_UP) {
567 placeCall();
568 return true;
569 }
570 break;
571 }
572 return false;
573 }
574
575 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700576 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800577 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700578 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800579 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700580 return handled;
581 }
582
583 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800584 public void onConfirmClick(EmergencyShortcutButton button) {
585 if (button == null) return;
586
Shaotang Li8662a912018-07-04 16:53:01 +0800587 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
588
589 // We interest on the context when user has intention to make phone call,
590 // so write metrics here for shortcut number even the call may not be created.
591 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
592 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
593
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800594 String phoneNumber = button.getPhoneNumber();
595
596 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800597 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Li5d906c12018-07-19 17:32:42 +0800598 Bundle extras = new Bundle();
599 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
600 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800601 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800602 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800603 } else {
604 Log.d(LOG_TAG, "emergency number is empty");
605 }
606 }
607
608 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700609 public void onClick(View view) {
610 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700611 case R.id.deleteButton: {
612 keyPressed(KeyEvent.KEYCODE_DEL);
613 return;
614 }
615 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700616 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700617 placeCall();
618 return;
619 }
620 case R.id.digits: {
621 if (mDigits.length() != 0) {
622 mDigits.setCursorVisible(true);
623 }
624 return;
625 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800626 case R.id.floating_action_button_dialpad: {
Shaotang Li8662a912018-07-04 16:53:01 +0800627 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800628 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800629 switchView(mDialpadView, mEmergencyShortcutView, true);
630 return;
631 }
632 case R.id.emergency_info_button: {
Shaotang Li8662a912018-07-04 16:53:01 +0800633 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800634 Intent intent = (Intent) view.getTag(R.id.tag_intent);
635 if (intent != null) {
636 startActivity(intent);
637 }
638 return;
639 }
Yorke Lee23a70732014-08-14 17:12:01 -0700640 }
641 }
642
643 @Override
644 public void onPressed(View view, boolean pressed) {
645 if (!pressed) {
646 return;
647 }
648 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700649 case R.id.one: {
650 playTone(ToneGenerator.TONE_DTMF_1);
651 keyPressed(KeyEvent.KEYCODE_1);
652 return;
653 }
654 case R.id.two: {
655 playTone(ToneGenerator.TONE_DTMF_2);
656 keyPressed(KeyEvent.KEYCODE_2);
657 return;
658 }
659 case R.id.three: {
660 playTone(ToneGenerator.TONE_DTMF_3);
661 keyPressed(KeyEvent.KEYCODE_3);
662 return;
663 }
664 case R.id.four: {
665 playTone(ToneGenerator.TONE_DTMF_4);
666 keyPressed(KeyEvent.KEYCODE_4);
667 return;
668 }
669 case R.id.five: {
670 playTone(ToneGenerator.TONE_DTMF_5);
671 keyPressed(KeyEvent.KEYCODE_5);
672 return;
673 }
674 case R.id.six: {
675 playTone(ToneGenerator.TONE_DTMF_6);
676 keyPressed(KeyEvent.KEYCODE_6);
677 return;
678 }
679 case R.id.seven: {
680 playTone(ToneGenerator.TONE_DTMF_7);
681 keyPressed(KeyEvent.KEYCODE_7);
682 return;
683 }
684 case R.id.eight: {
685 playTone(ToneGenerator.TONE_DTMF_8);
686 keyPressed(KeyEvent.KEYCODE_8);
687 return;
688 }
689 case R.id.nine: {
690 playTone(ToneGenerator.TONE_DTMF_9);
691 keyPressed(KeyEvent.KEYCODE_9);
692 return;
693 }
694 case R.id.zero: {
695 playTone(ToneGenerator.TONE_DTMF_0);
696 keyPressed(KeyEvent.KEYCODE_0);
697 return;
698 }
699 case R.id.pound: {
700 playTone(ToneGenerator.TONE_DTMF_P);
701 keyPressed(KeyEvent.KEYCODE_POUND);
702 return;
703 }
704 case R.id.star: {
705 playTone(ToneGenerator.TONE_DTMF_S);
706 keyPressed(KeyEvent.KEYCODE_STAR);
707 return;
708 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700709 }
710 }
711
712 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 * called for long touch events
714 */
715 @Override
716 public boolean onLongClick(View view) {
717 int id = view.getId();
718 switch (id) {
719 case R.id.deleteButton: {
720 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 return true;
722 }
723 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700724 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 keyPressed(KeyEvent.KEYCODE_PLUS);
726 return true;
727 }
728 }
729 return false;
730 }
731
732 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700733 protected void onStart() {
734 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800735
736 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
737 mUserActions = MetricsWriter.USER_ACTION_NONE;
738 mMetricsWriter.writeMetricsForEnter();
739
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800740 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
741 // And set background color to black.
742 if (mAreEmergencyDialerShortcutsEnabled) {
743 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
744 updateTheme(false);
745 } else {
746 mColorExtractor.addOnColorsChangedListener(this);
747 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
748 ColorExtractor.TYPE_EXTRA_DARK);
749 // Do not animate when view isn't visible yet, just set an initial state.
750 mBackgroundGradient.setColors(lockScreenColors, false);
751 updateTheme(lockScreenColors.supportsDarkText());
752 }
CY Cheng9a69c182018-06-15 21:20:10 +0800753
754 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
755 final Context context = this;
756 mEccInfoHelper.getCountryEccInfoAsync(context,
757 new EccInfoHelper.CountryEccInfoResultCallback() {
758 @Override
759 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
760 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
761 + Rlog.pii(LOG_TAG, iso));
762 updateLocationAndEccInfo(iso, countryEccInfo);
763 }
764
765 @Override
766 public void onDetectCountryFailed() {
767 Log.w(LOG_TAG, "Cannot detect current country.");
768 updateLocationAndEccInfo(null, null);
769 }
770
771 @Override
772 public void onRetrieveCountryEccInfoFailed(String iso) {
773 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
774 + Rlog.pii(LOG_TAG, iso));
775 updateLocationAndEccInfo(iso, null);
776 }
777 });
778 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700779 }
780
781 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 protected void onResume() {
783 super.onResume();
784
Shaotang Li8662a912018-07-04 16:53:01 +0800785 if (mProximitySensor != null) {
786 mSensorManager.registerListener(
787 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
788 }
789
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 // retrieve the DTMF tone play back setting.
791 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
792 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
793
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 // if the mToneGenerator creation fails, just continue without it. It is
795 // a local audio signal, and is not as important as the dtmf tone itself.
796 synchronized (mToneGeneratorLock) {
797 if (mToneGenerator == null) {
798 try {
799 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
800 TONE_RELATIVE_VOLUME);
801 } catch (RuntimeException e) {
802 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
803 mToneGenerator = null;
804 }
805 }
806 }
807
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 updateDialAndDeleteButtonStateEnabledAttr();
809 }
810
811 @Override
812 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700813 super.onPause();
Shaotang Li8662a912018-07-04 16:53:01 +0800814 if (mProximitySensor != null) {
815 mSensorManager.unregisterListener(this, mProximitySensor);
816 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 }
818
Lucas Dupineb9c5702017-05-10 16:57:09 -0700819 @Override
820 protected void onStop() {
821 super.onStop();
Shaotang Li8662a912018-07-04 16:53:01 +0800822 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700823 mColorExtractor.removeOnColorsChangedListener(this);
824 }
825
826 /**
827 * Sets theme based on gradient colors
828 * @param supportsDarkText true if gradient supports dark text
829 */
830 private void updateTheme(boolean supportsDarkText) {
831 if (mSupportsDarkText == supportsDarkText) {
832 return;
833 }
834 mSupportsDarkText = supportsDarkText;
835
836 // We can't change themes after inflation, in this case we'll have to recreate
837 // the whole activity.
838 if (mBackgroundGradient != null) {
839 recreate();
840 return;
841 }
842
843 int vis = getWindow().getDecorView().getSystemUiVisibility();
844 if (supportsDarkText) {
845 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
846 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
847 setTheme(R.style.EmergencyDialerThemeDark);
848 } else {
849 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
850 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
851 setTheme(R.style.EmergencyDialerTheme);
852 }
853 getWindow().getDecorView().setSystemUiVisibility(vis);
854 }
855
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 /**
857 * place the call, but check to make sure it is a viable number.
858 */
859 private void placeCall() {
Shaotang Li8662a912018-07-04 16:53:01 +0800860 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900862
863 // Convert into emergency number according to emergency conversion map.
864 // If conversion map is not defined (this is default), this method does
865 // nothing and just returns input number.
866 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
867
Yorke Lee36bb2542014-06-05 08:09:52 -0700868 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
870
871 // place the call if it is a valid number
872 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
873 // There is no number entered.
874 playTone(ToneGenerator.TONE_PROP_NACK);
875 return;
876 }
Shaotang Li8662a912018-07-04 16:53:01 +0800877
878 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
879 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
880
Shaotang Li5d906c12018-07-19 17:32:42 +0800881 Bundle extras = new Bundle();
882 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
883 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnfa77e202018-03-23 07:47:00 -0700884 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800885 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700886 } else {
887 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
888
Shaotang Li8662a912018-07-04 16:53:01 +0800889 // We interest on the context when user has intention to make phone call,
890 // so write metrics here for non-emergency numbers even these numbers are rejected.
891 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
892 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
893
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700894 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
895 }
Yorke Lee9b341512014-10-17 11:36:41 -0700896 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700897 }
898
899 /**
900 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
901 *
902 * The tone is played locally, using the audio stream for phone calls.
903 * Tones are played only if the "Audible touch tones" user preference
904 * is checked, and are NOT played if the device is in silent mode.
905 *
906 * @param tone a tone code from {@link ToneGenerator}
907 */
908 void playTone(int tone) {
909 // if local tone playback is disabled, just return.
910 if (!mDTMFToneEnabled) {
911 return;
912 }
913
914 // Also do nothing if the phone is in silent mode.
915 // We need to re-check the ringer mode for *every* playTone()
916 // call, rather than keeping a local flag that's updated in
917 // onResume(), since it's possible to toggle silent mode without
918 // leaving the current activity (via the ENDCALL-longpress menu.)
919 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
920 int ringerMode = audioManager.getRingerMode();
921 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
922 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
923 return;
924 }
925
926 synchronized (mToneGeneratorLock) {
927 if (mToneGenerator == null) {
928 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
929 return;
930 }
931
932 // Start the new tone (will stop any playing tone)
933 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
934 }
935 }
936
937 private CharSequence createErrorMessage(String number) {
938 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800939 String errorString = getString(R.string.dial_emergency_error, number);
940 int startingPosition = errorString.indexOf(number);
941 int endingPosition = startingPosition + number.length();
942 Spannable result = new SpannableString(errorString);
943 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
944 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 } else {
946 return getText(R.string.dial_emergency_empty_error).toString();
947 }
948 }
949
950 @Override
951 protected Dialog onCreateDialog(int id) {
952 AlertDialog dialog = null;
953 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
954 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700955 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700956 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
957 .setMessage(createErrorMessage(mLastNumber))
958 .setPositiveButton(R.string.ok, null)
959 .setCancelable(true).create();
960
961 // blur stuff behind the dialog
962 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700963 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 }
965 return dialog;
966 }
967
968 @Override
969 protected void onPrepareDialog(int id, Dialog dialog) {
970 super.onPrepareDialog(id, dialog);
971 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
972 AlertDialog alert = (AlertDialog) dialog;
973 alert.setMessage(createErrorMessage(mLastNumber));
974 }
975 }
976
Andrew Leed5631e82014-10-08 16:03:58 -0700977 @Override
978 public boolean onOptionsItemSelected(MenuItem item) {
979 final int itemId = item.getItemId();
980 if (itemId == android.R.id.home) {
981 onBackPressed();
982 return true;
983 }
984 return super.onOptionsItemSelected(item);
985 }
986
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 /**
988 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
989 */
990 private void updateDialAndDeleteButtonStateEnabledAttr() {
991 final boolean notEmpty = mDigits.length() != 0;
992
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700993 mDelete.setEnabled(notEmpty);
994 }
Yorke Lee91311662014-10-24 14:50:45 -0700995
996 /**
997 * Remove the digit just before the current position. Used by various long pressed callbacks
998 * to remove the digit that was populated as a result of the short click.
999 */
1000 private void removePreviousDigitIfPossible() {
1001 final int currentPosition = mDigits.getSelectionStart();
1002 if (currentPosition > 0) {
1003 mDigits.setSelection(currentPosition);
1004 mDigits.getText().delete(currentPosition - 1, currentPosition);
1005 }
1006 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -08001007
1008 /**
1009 * Update the text-to-speech annotations in the edit field.
1010 */
1011 private void updateTtsSpans() {
1012 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
1013 mDigits.getText().removeSpan(o);
1014 }
1015 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
1016 }
Lucas Dupineb9c5702017-05-10 16:57:09 -07001017
1018 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -07001019 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -07001020 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -07001021 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1022 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001023 mBackgroundGradient.setColors(colors);
1024 updateTheme(colors.supportsDarkText());
1025 }
1026 }
Tyler Gunnc5428972018-03-28 14:15:34 -07001027
1028 /**
1029 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1030 * add hint text above the dial pad which warns the user of this case.
1031 */
1032 private void maybeShowWfcEmergencyCallingWarning() {
1033 if (!mIsWfcEmergencyCallingWarningEnabled) {
1034 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1035 return;
1036 }
1037
1038 // Use an async task rather than calling into Telephony on UI thread.
1039 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1040 @Override
1041 protected Boolean doInBackground(Void... voids) {
1042 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1043 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1044 ServiceState ss = tm.getServiceState();
1045 boolean isCellAvailable =
1046 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1047 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
1048 + " isCellAvailable=" + isCellAvailable
1049 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
1050 return isWfcAvailable && !isCellAvailable;
1051 }
1052
1053 @Override
1054 protected void onPostExecute(Boolean result) {
1055 if (result.booleanValue()) {
1056 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1057 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1058 } else {
1059 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1060 mDigits.setHint("");
1061 }
1062 maybeChangeHintSize();
1063 }
1064 };
1065 showWfcWarningTask.execute((Void) null);
1066 }
1067
1068 /**
1069 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1070 * edit view and set the font size to a smaller size appropriate for the emergency calling
1071 * warning.
1072 */
1073 private void maybeChangeHintSize() {
1074 if (TextUtils.isEmpty(mDigits.getHint())
1075 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1076 // No hint or there are dialed digits, so use default size.
1077 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1078 // By default, the digits view auto-resizes to fit the text it contains, so
1079 // enable that now.
1080 mDigits.setResizeEnabled(true);
1081 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1082 } else {
1083 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1084 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1085 R.dimen.emergency_call_warning_size));
1086 // Since we're populating this with a static text string, disable auto-resize.
1087 mDigits.setResizeEnabled(false);
1088 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1089 }
1090 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001091
1092 private void setupEmergencyShortcutsView() {
1093 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1094 mDialpadView = findViewById(R.id.emergency_dialer);
1095
Chihhang Chuangcaba0da2018-08-02 22:25:06 +08001096 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1097 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1098
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001099 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1100 dialpadButton.setOnClickListener(this);
1101
1102 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
1103 emergencyInfoButton.setOnClickListener(this);
1104
1105 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1106 mEmergencyActionGroup.setVisibility(View.GONE);
1107
1108 // Setup dialpad title.
1109 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1110 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1111
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001112 mEmergencyShortcutButtonList = new ArrayList<>();
1113 setupEmergencyCallShortcutButton();
1114
CY Cheng9a69c182018-06-15 21:20:10 +08001115 updateLocationAndEccInfo(null, null);
1116
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001117 switchView(mEmergencyShortcutView, mDialpadView, false);
1118 }
1119
CY Cheng9a69c182018-06-15 21:20:10 +08001120 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001121 final View locationInfo = findViewById(R.id.location_info);
1122
CY Cheng9a69c182018-06-15 21:20:10 +08001123 String countryName = null;
1124 if (!TextUtils.isEmpty(countryIso)) {
1125 Locale locale = Locale.getDefault();
1126 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1127 .getDisplayCountry();
1128 }
1129 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001130 locationInfo.setVisibility(View.INVISIBLE);
1131 } else {
1132 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001133 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001134 locationInfo.setVisibility(View.VISIBLE);
1135 }
1136 }
1137
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001138 private void setupEmergencyCallShortcutButton() {
1139 final ViewGroup shortcutButtonContainer = findViewById(
1140 R.id.emergency_shortcut_buttons_container);
1141 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001142 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001143
CY Cheng9a69c182018-06-15 21:20:10 +08001144 mShortcutAdapter = new EccShortcutAdapter(this) {
1145 @Override
1146 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1147 CharSequence description, int iconRes) {
1148 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1149 .inflate(R.layout.emergency_shortcut_button, parent, false);
1150 button.setPhoneNumber(number);
1151 button.setPhoneDescription(description);
1152 button.setPhoneTypeIcon(iconRes);
1153 button.setOnConfirmClickListener(EmergencyDialer.this);
1154 return button;
1155 }
1156 };
1157 mShortcutDataSetObserver = new DataSetObserver() {
1158 @Override
1159 public void onChanged() {
1160 super.onChanged();
1161 updateLayout();
1162 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001163
CY Cheng9a69c182018-06-15 21:20:10 +08001164 @Override
1165 public void onInvalidated() {
1166 super.onInvalidated();
1167 updateLayout();
1168 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001169
CY Cheng9a69c182018-06-15 21:20:10 +08001170 private void updateLayout() {
1171 // clear previous added buttons
1172 shortcutButtonContainer.removeAllViews();
1173 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001174
CY Cheng9a69c182018-06-15 21:20:10 +08001175 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1176 EmergencyShortcutButton button = (EmergencyShortcutButton)
1177 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1178 mEmergencyShortcutButtonList.add(button);
1179 shortcutButtonContainer.addView(button);
1180 }
1181
1182 // update emergency numbers title for numerous buttons.
1183 if (mEmergencyShortcutButtonList.size() > 1) {
1184 emergencyNumberTitle.setText(getString(
1185 R.string.numerous_emergency_numbers_title));
1186 } else {
1187 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1188 }
1189 }
1190 };
1191 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1192 }
1193
1194 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1195 if (!isFinishing() && !isDestroyed()) {
1196 setLocationInfo(iso);
1197 if (mShortcutAdapter != null) {
1198 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1199 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001200 }
1201 }
1202
1203 /**
1204 * Called by the activity before a touch event is dispatched to the view hierarchy.
1205 */
1206 private void onPreTouchEvent(MotionEvent event) {
1207 mEmergencyActionGroup.onPreTouchEvent(event);
1208
1209 if (mEmergencyShortcutButtonList != null) {
1210 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1211 button.onPreTouchEvent(event);
1212 }
1213 }
1214 }
1215
1216 /**
1217 * Called by the activity after a touch event is dispatched to the view hierarchy.
1218 */
1219 private void onPostTouchEvent(MotionEvent event) {
1220 mEmergencyActionGroup.onPostTouchEvent(event);
1221
1222 if (mEmergencyShortcutButtonList != null) {
1223 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1224 button.onPostTouchEvent(event);
1225 }
1226 }
1227 }
1228
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001229 /**
1230 * Switch two view.
1231 *
1232 * @param displayView the view would be displayed.
1233 * @param hideView the view would be hidden.
1234 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1235 */
1236 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1237 if (displayView == null || hideView == null) {
1238 return;
1239 }
1240
1241 if (displayView.getVisibility() == View.VISIBLE) {
1242 return;
1243 }
1244
1245 if (hasAnimation) {
1246 crossfade(hideView, displayView);
1247 } else {
1248 hideView.setVisibility(View.GONE);
1249 displayView.setVisibility(View.VISIBLE);
1250 }
1251 }
1252
1253 /**
1254 * Fade out and fade in animation between two view transition.
1255 */
1256 private void crossfade(View fadeOutView, View fadeInView) {
1257 if (fadeOutView == null || fadeInView == null) {
1258 return;
1259 }
1260 final int shortAnimationDuration = getResources().getInteger(
1261 android.R.integer.config_shortAnimTime);
1262
1263 fadeInView.setAlpha(0f);
1264 fadeInView.setVisibility(View.VISIBLE);
1265
1266 fadeInView.animate()
1267 .alpha(1f)
1268 .setDuration(shortAnimationDuration)
1269 .setListener(null);
1270
1271 fadeOutView.animate()
1272 .alpha(0f)
1273 .setDuration(shortAnimationDuration)
1274 .setListener(new AnimatorListenerAdapter() {
1275 @Override
1276 public void onAnimationEnd(Animator animation) {
1277 fadeOutView.setVisibility(View.GONE);
1278 }
1279 });
1280 }
Shaotang Li8662a912018-07-04 16:53:01 +08001281
1282 @Override
1283 public void onSensorChanged(SensorEvent event) {
1284 float distance = event.values[0];
1285 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1286 }
1287
1288 @Override
1289 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1290 // Not used.
1291 }
1292
1293 private boolean isShortcutNumber(String number) {
1294 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1295 return false;
1296 }
1297
1298 boolean isShortcut = false;
1299 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1300 if (button != null && number.equals(button.getPhoneNumber())) {
1301 isShortcut = true;
1302 break;
1303 }
1304 }
1305 return isShortcut;
1306 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307}