blob: c89ddc6db9623a37110e9c4ac6305fec79c6da2d [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070066import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070067import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070069import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070070import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080072import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080073import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080075import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080076import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Lucas Dupinaf9e9912017-06-22 12:39:39 -070078import com.android.internal.colorextraction.ColorExtractor;
79import com.android.internal.colorextraction.ColorExtractor.GradientColors;
80import com.android.internal.colorextraction.drawable.GradientDrawable;
Shaotang Li8662a912018-07-04 16:53:01 +080081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Yorke Lee23a70732014-08-14 17:12:01 -070083import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070084import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070085import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080086import com.android.phone.ecc.CountryEccInfo;
87import com.android.phone.ecc.EccInfoHelper;
Leo Hsu779569a2018-07-10 11:37:52 +080088import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
Chihhang Chuang92cfe512018-06-07 16:25:27 +080090import java.util.ArrayList;
91import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080092import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080093
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094/**
95 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
96 *
97 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
98 * activity from apps/Contacts) that:
Leo Hsu83ab2332018-12-18 15:20:45 +080099 * 1. Allows ONLY emergency calls to be dialed
100 * 2. Disallows voicemail functionality
101 * 3. Allows this activity to stay in front of the keyguard.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 *
103 * TODO: Even though this is an ultra-simplified version of the normal
104 * dialer, there's still lots of code duplication between this class and
105 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
106 * moved into a shared base class that would live in the framework?
107 * Or could we figure out some way to move *this* class into apps/Contacts
108 * also?
109 */
110public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700111 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800112 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800113 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener,
114 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-07-04 16:53:01 +0800115
116 private class MetricsWriter {
117 // Metrics constants indicating the entry type that user opened emergency dialer.
118 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
119 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
120 public static final int ENTRY_TYPE_UNKNOWN = 0;
121 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
122 public static final int ENTRY_TYPE_POWER_MENU = 2;
123
124 // Metrics constants indicating the UI that user made phone call.
125 public static final int CALL_SOURCE_DIALPAD = 0;
126 public static final int CALL_SOURCE_SHORTCUT = 1;
127
128 // Metrics constants indicating the phone number type of a call user made.
129 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
130 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
131
132 // Metrics constants indicating the actions performed by user.
133 public static final int USER_ACTION_NONE = 0x0;
134 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
135 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
136 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
137 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
138
139 private MetricsLogger mMetricsLogger = new MetricsLogger();
140
141 public void writeMetricsForEnter() {
Leo Hsuaeb32d82018-12-25 16:01:27 +0800142 if (!mIsShortcutViewEnabled) {
143 return;
144 }
145
Shaotang Li8662a912018-07-04 16:53:01 +0800146 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
147 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
148 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
149 .setType(MetricsEvent.TYPE_OPEN)
150 .setSubtype(entryType)
151 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
152 keyguard.isKeyguardLocked() ? 1 : 0));
153 }
154
155 public void writeMetricsForExit() {
Leo Hsuaeb32d82018-12-25 16:01:27 +0800156 if (!mIsShortcutViewEnabled) {
157 return;
158 }
159
Shaotang Li8662a912018-07-04 16:53:01 +0800160 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
161 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
162 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
163 .setType(MetricsEvent.TYPE_CLOSE)
164 .setSubtype(entryType)
165 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
166 .addTaggedData(
167 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
168 }
169
170 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
171 boolean hasShortcut) {
Leo Hsuaeb32d82018-12-25 16:01:27 +0800172 if (!mIsShortcutViewEnabled) {
173 return;
174 }
175
Shaotang Li8662a912018-07-04 16:53:01 +0800176 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
177 .setType(MetricsEvent.TYPE_ACTION)
178 .setSubtype(callSource)
179 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
180 phoneNumberType)
181 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
182 hasShortcut ? 1 : 0)
183 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
184 mIsProximityNear ? 1 : 0));
185 }
186 }
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 // Keys used with onSaveInstanceState().
189 private static final String LAST_NUMBER = "lastNumber";
190
191 // Intent action for this activity.
192 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
193
Shaotang Li8662a912018-07-04 16:53:01 +0800194 /**
195 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
196 * the emergency dialer.
197 */
198 public static final String EXTRA_ENTRY_TYPE =
199 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
200
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 // List of dialer button IDs.
Leo Hsu83ab2332018-12-18 15:20:45 +0800202 private static final int[] DIALER_KEYS = new int[]{
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 R.id.one, R.id.two, R.id.three,
204 R.id.four, R.id.five, R.id.six,
205 R.id.seven, R.id.eight, R.id.nine,
Leo Hsu83ab2332018-12-18 15:20:45 +0800206 R.id.star, R.id.zero, R.id.pound};
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207
208 // Debug constants.
209 private static final boolean DBG = false;
210 private static final String LOG_TAG = "EmergencyDialer";
211
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 /** The length of DTMF tones in milliseconds */
213 private static final int TONE_LENGTH_MS = 150;
214
215 /** The DTMF tone volume relative to other sounds in the stream */
216 private static final int TONE_RELATIVE_VOLUME = 80;
217
218 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
219 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
220
221 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
222
Lucas Dupineb9c5702017-05-10 16:57:09 -0700223 /** 90% opacity, different from other gradients **/
224 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
225
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800226 /** 85% opacity for black background **/
227 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
228
CY Cheng9a69c182018-06-15 21:20:10 +0800229 /** Size limit of emergency shortcut buttons container. **/
230 private static final int SHORTCUT_SIZE_LIMIT = 3;
231
Tyler Gunnc5428972018-03-28 14:15:34 -0700232 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 private View mDialButton;
234 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800235 private View mEmergencyShortcutView;
236 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237
CY Cheng9a69c182018-06-15 21:20:10 +0800238 private EccInfoHelper mEccInfoHelper;
239
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800240 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800241 private EccShortcutAdapter mShortcutAdapter;
242 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800243
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 private ToneGenerator mToneGenerator;
245 private Object mToneGeneratorLock = new Object();
246
247 // determines if we want to playback local DTMF tones.
248 private boolean mDTMFToneEnabled;
249
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700250 private EmergencyActionGroup mEmergencyActionGroup;
251
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800252 private EmergencyInfoGroup mEmergencyInfoGroup;
253
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 // close activity when screen turns off
255 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
256 @Override
257 public void onReceive(Context context, Intent intent) {
258 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700259 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 }
261 }
262 };
263
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800264 /**
265 * Customize accessibility methods in View.
266 */
267 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
268
269 /**
270 * Stop AccessiblityService from reading the title of a hidden View.
271 *
272 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
273 * in the animation end. The view with an accessibility pane title would call the
274 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
275 * accessibility service to read the pane title of fade out view instead of pane title of
276 * fade in view. So it need to filter out the event called by vanished pane.
277 */
278 @Override
279 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
280 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
281 && host.getVisibility() == View.GONE) {
282 return;
283 }
284 super.onPopulateAccessibilityEvent(host, event);
285 }
286 };
287
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
289
Lucas Dupineb9c5702017-05-10 16:57:09 -0700290 // Background gradient
291 private ColorExtractor mColorExtractor;
292 private GradientDrawable mBackgroundGradient;
293 private boolean mSupportsDarkText;
294
Tyler Gunnc5428972018-03-28 14:15:34 -0700295 private boolean mIsWfcEmergencyCallingWarningEnabled;
296 private float mDefaultDigitsTextSize;
297
Leo Hsu83ab2332018-12-18 15:20:45 +0800298 private boolean mIsShortcutViewEnabled;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800299
Shaotang Li8662a912018-07-04 16:53:01 +0800300 private MetricsWriter mMetricsWriter;
301 private SensorManager mSensorManager;
302 private Sensor mProximitySensor;
303 private boolean mIsProximityNear = false;
304
305 /**
306 * The time, in millis, since boot when user opened emergency dialer.
307 * This is used when calculating the user stay duration for metrics data.
308 */
309 private long mUserEnterTimeMillis = 0;
310
311 /**
312 * Bit flag indicating the actions performed by user. This is used for metrics data.
313 */
314 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
315
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 @Override
317 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
318 // Do nothing
319 }
320
321 @Override
322 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700323 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700324 }
325
326 @Override
327 public void afterTextChanged(Editable input) {
328 // Check for special sequences, in particular the "**04" or "**05"
329 // sequences that allow you to enter PIN or PUK-related codes.
330 //
331 // But note we *don't* allow most other special sequences here,
332 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
333 // since those shouldn't be available if the device is locked.
334 //
335 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
336 // here, not the regular handleChars() method.
337 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
338 // A special sequence was entered, clear the digits
339 mDigits.getText().clear();
340 }
341
342 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800343 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 }
345
346 @Override
347 protected void onCreate(Bundle icicle) {
348 super.onCreate(icicle);
349
Shaotang Li8662a912018-07-04 16:53:01 +0800350 mMetricsWriter = new MetricsWriter();
351 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
352 if (mSensorManager != null) {
353 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
354 }
355
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800357 setShowWhenLocked(true);
358 // Allow turning screen on
359 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360
Chuck Liaocdeebb42018-10-30 12:07:18 +0800361 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
362 PersistableBundle carrierConfig =
363 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
364
Leo Hsu83ab2332018-12-18 15:20:45 +0800365 mIsShortcutViewEnabled = false;
366 if (canEnableShortcutView(carrierConfig)) {
367 TelephonyManager tm = getSystemService(TelephonyManager.class);
368 String countryIso = tm.getNetworkCountryIso();
369 if (TextUtils.isEmpty(countryIso)) {
370 Log.d(LOG_TAG, "Unable to determine the country of current network.");
371 } else if (!EccInfoHelper.isCountryEccInfoAvailable(this, countryIso)) {
372 Log.d(LOG_TAG, "ECC info is unavailable.");
Leo Hsu233f5b32018-11-07 11:08:03 +0800373 } else {
Leo Hsu83ab2332018-12-18 15:20:45 +0800374 mIsShortcutViewEnabled = true;
Leo Hsu233f5b32018-11-07 11:08:03 +0800375 }
Chuck Liaocdeebb42018-10-30 12:07:18 +0800376 }
CY Cheng9a69c182018-06-15 21:20:10 +0800377 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsu83ab2332018-12-18 15:20:45 +0800378 + mIsShortcutViewEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800379
Lucas Dupineb9c5702017-05-10 16:57:09 -0700380 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800381
Leo Hsu83ab2332018-12-18 15:20:45 +0800382 if (mIsShortcutViewEnabled) {
383 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800384 updateTheme(false);
385 } else {
386 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
387 ColorExtractor.TYPE_EXTRA_DARK);
388 updateTheme(lockScreenColors.supportsDarkText());
389 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700390
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391 setContentView(R.layout.emergency_dialer);
392
Tyler Gunnc5428972018-03-28 14:15:34 -0700393 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700394 mDigits.setKeyListener(DialerKeyListener.getInstance());
395 mDigits.setOnClickListener(this);
396 mDigits.setOnKeyListener(this);
397 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700398 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700399 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400 maybeAddNumberFormatting();
401
Lucas Dupineb9c5702017-05-10 16:57:09 -0700402 mBackgroundGradient = new GradientDrawable(this);
403 Point displaySize = new Point();
404 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
405 .getDefaultDisplay().getSize(displaySize);
406 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Leo Hsu83ab2332018-12-18 15:20:45 +0800407 mBackgroundGradient.setAlpha(mIsShortcutViewEnabled
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800408 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700409 getWindow().setBackgroundDrawable(mBackgroundGradient);
410
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411 // Check for the presence of the keypad
412 View view = findViewById(R.id.one);
413 if (view != null) {
414 setupKeypad();
415 }
416
417 mDelete = findViewById(R.id.deleteButton);
418 mDelete.setOnClickListener(this);
419 mDelete.setOnLongClickListener(this);
420
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700421 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700422
423 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700424 // Read carrier config through the public API because PhoneGlobals is not available when we
425 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700426 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700427 mDialButton.setOnClickListener(this);
428 } else {
429 mDialButton.setVisibility(View.GONE);
430 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700431 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
432 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
433 maybeShowWfcEmergencyCallingWarning();
434
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700435 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700436
437 if (icicle != null) {
438 super.onRestoreInstanceState(icicle);
439 }
440
441 // Extract phone number from intent
442 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700443 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700444 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
445 if (number != null) {
446 mDigits.setText(number);
447 }
448 }
449
450 // if the mToneGenerator creation fails, just continue without it. It is
451 // a local audio signal, and is not as important as the dtmf tone itself.
452 synchronized (mToneGeneratorLock) {
453 if (mToneGenerator == null) {
454 try {
455 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
456 } catch (RuntimeException e) {
457 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
458 mToneGenerator = null;
459 }
460 }
461 }
462
463 final IntentFilter intentFilter = new IntentFilter();
464 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
465 registerReceiver(mBroadcastReceiver, intentFilter);
466
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700467 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800468
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800469 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
470
Leo Hsu83ab2332018-12-18 15:20:45 +0800471 if (mIsShortcutViewEnabled) {
Leo Hsu233f5b32018-11-07 11:08:03 +0800472 mEccInfoHelper = new EccInfoHelper(IsoToEccProtobufRepository.getInstance());
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800473 setupEmergencyShortcutsView();
474 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700475 }
476
477 @Override
478 protected void onDestroy() {
479 super.onDestroy();
480 synchronized (mToneGeneratorLock) {
481 if (mToneGenerator != null) {
482 mToneGenerator.release();
483 mToneGenerator = null;
484 }
485 }
486 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800487 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
488 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
489 mShortcutDataSetObserver = null;
490 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700491 }
492
493 @Override
494 protected void onRestoreInstanceState(Bundle icicle) {
495 mLastNumber = icicle.getString(LAST_NUMBER);
496 }
497
498 @Override
499 protected void onSaveInstanceState(Bundle outState) {
500 super.onSaveInstanceState(outState);
501 outState.putString(LAST_NUMBER, mLastNumber);
502 }
503
504 /**
505 * Explicitly turn off number formatting, since it gets in the way of the emergency
506 * number detector
507 */
508 protected void maybeAddNumberFormatting() {
509 // Do nothing.
510 }
511
512 @Override
513 protected void onPostCreate(Bundle savedInstanceState) {
514 super.onPostCreate(savedInstanceState);
515
516 // This can't be done in onCreate(), since the auto-restoring of the digits
517 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
518 // is called. This method will be called every time the activity is created, and
519 // will always happen after onRestoreSavedInstanceState().
520 mDigits.addTextChangedListener(this);
521 }
522
523 private void setupKeypad() {
524 // Setup the listeners for the buttons
525 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700526 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
527 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700528 }
529
530 View view = findViewById(R.id.zero);
531 view.setOnLongClickListener(this);
532 }
533
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800534 @Override
535 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800536 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
537 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
538 if (mIsShortcutViewEnabled && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800539 && mDialpadView.getVisibility() == View.VISIBLE) {
540 switchView(mEmergencyShortcutView, mDialpadView, true);
541 return;
542 }
543 super.onBackPressed();
544 }
545
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546 /**
547 * handle key events
548 */
549 @Override
550 public boolean onKeyDown(int keyCode, KeyEvent event) {
551 switch (keyCode) {
552 // Happen when there's a "Call" hard button.
553 case KeyEvent.KEYCODE_CALL: {
554 if (TextUtils.isEmpty(mDigits.getText().toString())) {
555 // if we are adding a call from the InCallScreen and the phone
556 // number entered is empty, we just close the dialer to expose
557 // the InCallScreen under it.
558 finish();
559 } else {
560 // otherwise, we place the call.
561 placeCall();
562 }
563 return true;
564 }
565 }
566 return super.onKeyDown(keyCode, event);
567 }
568
569 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700570 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700571 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
572 mDigits.onKeyDown(keyCode, event);
573 }
574
575 @Override
576 public boolean onKey(View view, int keyCode, KeyEvent event) {
577 switch (view.getId()) {
578 case R.id.digits:
579 // Happen when "Done" button of the IME is pressed. This can happen when this
580 // Activity is forced into landscape mode due to a desk dock.
581 if (keyCode == KeyEvent.KEYCODE_ENTER
582 && event.getAction() == KeyEvent.ACTION_UP) {
583 placeCall();
584 return true;
585 }
586 break;
587 }
588 return false;
589 }
590
591 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700592 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800593 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700594 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800595 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700596 return handled;
597 }
598
599 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800600 public void onConfirmClick(EmergencyShortcutButton button) {
601 if (button == null) return;
602
Shaotang Li8662a912018-07-04 16:53:01 +0800603 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
604
605 // We interest on the context when user has intention to make phone call,
606 // so write metrics here for shortcut number even the call may not be created.
607 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
608 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
609
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800610 String phoneNumber = button.getPhoneNumber();
611
612 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800613 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Li5d906c12018-07-19 17:32:42 +0800614 Bundle extras = new Bundle();
615 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
616 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800617 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800618 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800619 } else {
620 Log.d(LOG_TAG, "emergency number is empty");
621 }
622 }
623
624 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800625 public void onConfirmClick(EmergencyInfoGroup button) {
626 if (button == null) return;
627
628 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
629 Intent intent = (Intent) button.getTag(R.id.tag_intent);
630 if (intent != null) {
631 startActivity(intent);
632 }
633 }
634
635 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 public void onClick(View view) {
637 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700638 case R.id.deleteButton: {
639 keyPressed(KeyEvent.KEYCODE_DEL);
640 return;
641 }
642 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700643 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700644 placeCall();
645 return;
646 }
647 case R.id.digits: {
648 if (mDigits.length() != 0) {
649 mDigits.setCursorVisible(true);
650 }
651 return;
652 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800653 case R.id.floating_action_button_dialpad: {
Shaotang Li8662a912018-07-04 16:53:01 +0800654 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800655 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800656 switchView(mDialpadView, mEmergencyShortcutView, true);
657 return;
658 }
Yorke Lee23a70732014-08-14 17:12:01 -0700659 }
660 }
661
662 @Override
663 public void onPressed(View view, boolean pressed) {
664 if (!pressed) {
665 return;
666 }
667 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 case R.id.one: {
669 playTone(ToneGenerator.TONE_DTMF_1);
670 keyPressed(KeyEvent.KEYCODE_1);
671 return;
672 }
673 case R.id.two: {
674 playTone(ToneGenerator.TONE_DTMF_2);
675 keyPressed(KeyEvent.KEYCODE_2);
676 return;
677 }
678 case R.id.three: {
679 playTone(ToneGenerator.TONE_DTMF_3);
680 keyPressed(KeyEvent.KEYCODE_3);
681 return;
682 }
683 case R.id.four: {
684 playTone(ToneGenerator.TONE_DTMF_4);
685 keyPressed(KeyEvent.KEYCODE_4);
686 return;
687 }
688 case R.id.five: {
689 playTone(ToneGenerator.TONE_DTMF_5);
690 keyPressed(KeyEvent.KEYCODE_5);
691 return;
692 }
693 case R.id.six: {
694 playTone(ToneGenerator.TONE_DTMF_6);
695 keyPressed(KeyEvent.KEYCODE_6);
696 return;
697 }
698 case R.id.seven: {
699 playTone(ToneGenerator.TONE_DTMF_7);
700 keyPressed(KeyEvent.KEYCODE_7);
701 return;
702 }
703 case R.id.eight: {
704 playTone(ToneGenerator.TONE_DTMF_8);
705 keyPressed(KeyEvent.KEYCODE_8);
706 return;
707 }
708 case R.id.nine: {
709 playTone(ToneGenerator.TONE_DTMF_9);
710 keyPressed(KeyEvent.KEYCODE_9);
711 return;
712 }
713 case R.id.zero: {
714 playTone(ToneGenerator.TONE_DTMF_0);
715 keyPressed(KeyEvent.KEYCODE_0);
716 return;
717 }
718 case R.id.pound: {
719 playTone(ToneGenerator.TONE_DTMF_P);
720 keyPressed(KeyEvent.KEYCODE_POUND);
721 return;
722 }
723 case R.id.star: {
724 playTone(ToneGenerator.TONE_DTMF_S);
725 keyPressed(KeyEvent.KEYCODE_STAR);
726 return;
727 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 }
729 }
730
731 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732 * called for long touch events
733 */
734 @Override
735 public boolean onLongClick(View view) {
736 int id = view.getId();
737 switch (id) {
738 case R.id.deleteButton: {
739 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 return true;
741 }
742 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700743 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 keyPressed(KeyEvent.KEYCODE_PLUS);
745 return true;
746 }
747 }
748 return false;
749 }
750
751 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700752 protected void onStart() {
753 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800754
755 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
756 mUserActions = MetricsWriter.USER_ACTION_NONE;
757 mMetricsWriter.writeMetricsForEnter();
758
Leo Hsu83ab2332018-12-18 15:20:45 +0800759 if (mIsShortcutViewEnabled) {
760 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800761 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
762 updateTheme(false);
763 } else {
764 mColorExtractor.addOnColorsChangedListener(this);
765 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
766 ColorExtractor.TYPE_EXTRA_DARK);
767 // Do not animate when view isn't visible yet, just set an initial state.
768 mBackgroundGradient.setColors(lockScreenColors, false);
769 updateTheme(lockScreenColors.supportsDarkText());
770 }
CY Cheng9a69c182018-06-15 21:20:10 +0800771
Leo Hsu83ab2332018-12-18 15:20:45 +0800772 if (mIsShortcutViewEnabled && mEccInfoHelper != null) {
CY Cheng9a69c182018-06-15 21:20:10 +0800773 final Context context = this;
774 mEccInfoHelper.getCountryEccInfoAsync(context,
775 new EccInfoHelper.CountryEccInfoResultCallback() {
776 @Override
777 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
778 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
779 + Rlog.pii(LOG_TAG, iso));
780 updateLocationAndEccInfo(iso, countryEccInfo);
781 }
782
783 @Override
784 public void onDetectCountryFailed() {
785 Log.w(LOG_TAG, "Cannot detect current country.");
786 updateLocationAndEccInfo(null, null);
787 }
788
789 @Override
790 public void onRetrieveCountryEccInfoFailed(String iso) {
791 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
792 + Rlog.pii(LOG_TAG, iso));
793 updateLocationAndEccInfo(iso, null);
794 }
795 });
796 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700797 }
798
799 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 protected void onResume() {
801 super.onResume();
802
Shaotang Li8662a912018-07-04 16:53:01 +0800803 if (mProximitySensor != null) {
804 mSensorManager.registerListener(
805 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
806 }
807
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 // retrieve the DTMF tone play back setting.
809 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
810 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
811
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700812 // if the mToneGenerator creation fails, just continue without it. It is
813 // a local audio signal, and is not as important as the dtmf tone itself.
814 synchronized (mToneGeneratorLock) {
815 if (mToneGenerator == null) {
816 try {
817 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
818 TONE_RELATIVE_VOLUME);
819 } catch (RuntimeException e) {
820 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
821 mToneGenerator = null;
822 }
823 }
824 }
825
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700826 updateDialAndDeleteButtonStateEnabledAttr();
827 }
828
829 @Override
830 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700831 super.onPause();
Shaotang Li8662a912018-07-04 16:53:01 +0800832 if (mProximitySensor != null) {
833 mSensorManager.unregisterListener(this, mProximitySensor);
834 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 }
836
Lucas Dupineb9c5702017-05-10 16:57:09 -0700837 @Override
838 protected void onStop() {
839 super.onStop();
Shaotang Li8662a912018-07-04 16:53:01 +0800840 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700841 mColorExtractor.removeOnColorsChangedListener(this);
842 }
843
Leo Hsu83ab2332018-12-18 15:20:45 +0800844 private boolean canEnableShortcutView(PersistableBundle carrierConfig) {
845 if (!getResources().getBoolean(R.bool.config_emergency_shortcut_view_enabled)) {
846 // Disables shortcut view by project.
847 return false;
848 }
849 if (!carrierConfig.getBoolean(
850 CarrierConfigManager.KEY_SUPPORT_EMERGENCY_DIALER_SHORTCUT_BOOL)) {
851 Log.d(LOG_TAG, "Disables shortcut view by carrier requirement");
852 return false;
853 }
854 return true;
855 }
856
Lucas Dupineb9c5702017-05-10 16:57:09 -0700857 /**
858 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800859 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700860 * @param supportsDarkText true if gradient supports dark text
861 */
862 private void updateTheme(boolean supportsDarkText) {
863 if (mSupportsDarkText == supportsDarkText) {
864 return;
865 }
866 mSupportsDarkText = supportsDarkText;
867
868 // We can't change themes after inflation, in this case we'll have to recreate
869 // the whole activity.
870 if (mBackgroundGradient != null) {
871 recreate();
872 return;
873 }
874
875 int vis = getWindow().getDecorView().getSystemUiVisibility();
876 if (supportsDarkText) {
877 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
878 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
879 setTheme(R.style.EmergencyDialerThemeDark);
880 } else {
881 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
882 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
883 setTheme(R.style.EmergencyDialerTheme);
884 }
885 getWindow().getDecorView().setSystemUiVisibility(vis);
886 }
887
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 /**
889 * place the call, but check to make sure it is a viable number.
890 */
891 private void placeCall() {
Shaotang Li8662a912018-07-04 16:53:01 +0800892 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900894
895 // Convert into emergency number according to emergency conversion map.
896 // If conversion map is not defined (this is default), this method does
897 // nothing and just returns input number.
898 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
899
Yorke Lee36bb2542014-06-05 08:09:52 -0700900 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
902
903 // place the call if it is a valid number
904 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
905 // There is no number entered.
906 playTone(ToneGenerator.TONE_PROP_NACK);
907 return;
908 }
Shaotang Li8662a912018-07-04 16:53:01 +0800909
910 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
911 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
912
Shaotang Li5d906c12018-07-19 17:32:42 +0800913 Bundle extras = new Bundle();
914 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
915 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnfa77e202018-03-23 07:47:00 -0700916 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800917 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 } else {
919 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
920
Shaotang Li8662a912018-07-04 16:53:01 +0800921 // We interest on the context when user has intention to make phone call,
922 // so write metrics here for non-emergency numbers even these numbers are rejected.
923 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
924 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
925
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
927 }
Yorke Lee9b341512014-10-17 11:36:41 -0700928 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929 }
930
931 /**
932 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
933 *
934 * The tone is played locally, using the audio stream for phone calls.
935 * Tones are played only if the "Audible touch tones" user preference
936 * is checked, and are NOT played if the device is in silent mode.
937 *
938 * @param tone a tone code from {@link ToneGenerator}
939 */
940 void playTone(int tone) {
941 // if local tone playback is disabled, just return.
942 if (!mDTMFToneEnabled) {
943 return;
944 }
945
946 // Also do nothing if the phone is in silent mode.
947 // We need to re-check the ringer mode for *every* playTone()
948 // call, rather than keeping a local flag that's updated in
949 // onResume(), since it's possible to toggle silent mode without
950 // leaving the current activity (via the ENDCALL-longpress menu.)
951 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
952 int ringerMode = audioManager.getRingerMode();
953 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800954 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 return;
956 }
957
958 synchronized (mToneGeneratorLock) {
959 if (mToneGenerator == null) {
960 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
961 return;
962 }
963
964 // Start the new tone (will stop any playing tone)
965 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
966 }
967 }
968
969 private CharSequence createErrorMessage(String number) {
970 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800971 String errorString = getString(R.string.dial_emergency_error, number);
972 int startingPosition = errorString.indexOf(number);
973 int endingPosition = startingPosition + number.length();
974 Spannable result = new SpannableString(errorString);
975 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
976 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 } else {
978 return getText(R.string.dial_emergency_empty_error).toString();
979 }
980 }
981
982 @Override
983 protected Dialog onCreateDialog(int id) {
984 AlertDialog dialog = null;
985 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
986 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700987 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
989 .setMessage(createErrorMessage(mLastNumber))
990 .setPositiveButton(R.string.ok, null)
991 .setCancelable(true).create();
992
993 // blur stuff behind the dialog
994 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700995 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 }
997 return dialog;
998 }
999
1000 @Override
1001 protected void onPrepareDialog(int id, Dialog dialog) {
1002 super.onPrepareDialog(id, dialog);
1003 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
1004 AlertDialog alert = (AlertDialog) dialog;
1005 alert.setMessage(createErrorMessage(mLastNumber));
1006 }
1007 }
1008
Andrew Leed5631e82014-10-08 16:03:58 -07001009 @Override
1010 public boolean onOptionsItemSelected(MenuItem item) {
1011 final int itemId = item.getItemId();
1012 if (itemId == android.R.id.home) {
1013 onBackPressed();
1014 return true;
1015 }
1016 return super.onOptionsItemSelected(item);
1017 }
1018
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 /**
1020 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
1021 */
1022 private void updateDialAndDeleteButtonStateEnabledAttr() {
1023 final boolean notEmpty = mDigits.length() != 0;
1024
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025 mDelete.setEnabled(notEmpty);
1026 }
Yorke Lee91311662014-10-24 14:50:45 -07001027
1028 /**
1029 * Remove the digit just before the current position. Used by various long pressed callbacks
1030 * to remove the digit that was populated as a result of the short click.
1031 */
1032 private void removePreviousDigitIfPossible() {
1033 final int currentPosition = mDigits.getSelectionStart();
1034 if (currentPosition > 0) {
1035 mDigits.setSelection(currentPosition);
1036 mDigits.getText().delete(currentPosition - 1, currentPosition);
1037 }
1038 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -08001039
1040 /**
1041 * Update the text-to-speech annotations in the edit field.
1042 */
1043 private void updateTtsSpans() {
1044 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
1045 mDigits.getText().removeSpan(o);
1046 }
1047 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
1048 }
Lucas Dupineb9c5702017-05-10 16:57:09 -07001049
1050 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -07001051 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -07001052 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -07001053 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1054 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001055 mBackgroundGradient.setColors(colors);
1056 updateTheme(colors.supportsDarkText());
1057 }
1058 }
Tyler Gunnc5428972018-03-28 14:15:34 -07001059
1060 /**
1061 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1062 * add hint text above the dial pad which warns the user of this case.
1063 */
1064 private void maybeShowWfcEmergencyCallingWarning() {
1065 if (!mIsWfcEmergencyCallingWarningEnabled) {
1066 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1067 return;
1068 }
1069
1070 // Use an async task rather than calling into Telephony on UI thread.
1071 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1072 @Override
1073 protected Boolean doInBackground(Void... voids) {
1074 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1075 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1076 ServiceState ss = tm.getServiceState();
1077 boolean isCellAvailable =
1078 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1079 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +08001080 + " isCellAvailable=" + isCellAvailable
1081 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -07001082 return isWfcAvailable && !isCellAvailable;
1083 }
1084
1085 @Override
1086 protected void onPostExecute(Boolean result) {
1087 if (result.booleanValue()) {
1088 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1089 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1090 } else {
1091 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1092 mDigits.setHint("");
1093 }
1094 maybeChangeHintSize();
1095 }
1096 };
1097 showWfcWarningTask.execute((Void) null);
1098 }
1099
1100 /**
1101 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1102 * edit view and set the font size to a smaller size appropriate for the emergency calling
1103 * warning.
1104 */
1105 private void maybeChangeHintSize() {
1106 if (TextUtils.isEmpty(mDigits.getHint())
1107 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1108 // No hint or there are dialed digits, so use default size.
1109 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1110 // By default, the digits view auto-resizes to fit the text it contains, so
1111 // enable that now.
1112 mDigits.setResizeEnabled(true);
1113 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1114 } else {
1115 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1116 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1117 R.dimen.emergency_call_warning_size));
1118 // Since we're populating this with a static text string, disable auto-resize.
1119 mDigits.setResizeEnabled(false);
1120 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1121 }
1122 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001123
1124 private void setupEmergencyShortcutsView() {
1125 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1126 mDialpadView = findViewById(R.id.emergency_dialer);
1127
Chihhang Chuangcaba0da2018-08-02 22:25:06 +08001128 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1129 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1130
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001131 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1132 dialpadButton.setOnClickListener(this);
1133
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001134 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001135
1136 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1137 mEmergencyActionGroup.setVisibility(View.GONE);
1138
1139 // Setup dialpad title.
1140 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1141 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1142
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001143 mEmergencyShortcutButtonList = new ArrayList<>();
1144 setupEmergencyCallShortcutButton();
1145
CY Cheng9a69c182018-06-15 21:20:10 +08001146 updateLocationAndEccInfo(null, null);
1147
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001148 switchView(mEmergencyShortcutView, mDialpadView, false);
1149 }
1150
CY Cheng9a69c182018-06-15 21:20:10 +08001151 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001152 final View locationInfo = findViewById(R.id.location_info);
1153
CY Cheng9a69c182018-06-15 21:20:10 +08001154 String countryName = null;
1155 if (!TextUtils.isEmpty(countryIso)) {
1156 Locale locale = Locale.getDefault();
1157 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1158 .getDisplayCountry();
1159 }
1160 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001161 locationInfo.setVisibility(View.INVISIBLE);
1162 } else {
1163 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001164 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001165 locationInfo.setVisibility(View.VISIBLE);
1166 }
1167 }
1168
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001169 private void setupEmergencyCallShortcutButton() {
1170 final ViewGroup shortcutButtonContainer = findViewById(
1171 R.id.emergency_shortcut_buttons_container);
1172 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001173 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001174
CY Cheng9a69c182018-06-15 21:20:10 +08001175 mShortcutAdapter = new EccShortcutAdapter(this) {
1176 @Override
1177 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1178 CharSequence description, int iconRes) {
1179 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1180 .inflate(R.layout.emergency_shortcut_button, parent, false);
1181 button.setPhoneNumber(number);
1182 button.setPhoneDescription(description);
1183 button.setPhoneTypeIcon(iconRes);
1184 button.setOnConfirmClickListener(EmergencyDialer.this);
1185 return button;
1186 }
1187 };
1188 mShortcutDataSetObserver = new DataSetObserver() {
1189 @Override
1190 public void onChanged() {
1191 super.onChanged();
1192 updateLayout();
1193 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001194
CY Cheng9a69c182018-06-15 21:20:10 +08001195 @Override
1196 public void onInvalidated() {
1197 super.onInvalidated();
1198 updateLayout();
1199 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001200
CY Cheng9a69c182018-06-15 21:20:10 +08001201 private void updateLayout() {
1202 // clear previous added buttons
1203 shortcutButtonContainer.removeAllViews();
1204 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001205
CY Cheng9a69c182018-06-15 21:20:10 +08001206 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1207 EmergencyShortcutButton button = (EmergencyShortcutButton)
1208 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1209 mEmergencyShortcutButtonList.add(button);
1210 shortcutButtonContainer.addView(button);
1211 }
1212
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001213 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001214 if (mEmergencyShortcutButtonList.size() > 1) {
1215 emergencyNumberTitle.setText(getString(
1216 R.string.numerous_emergency_numbers_title));
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001217 // Update mEmergencyInfoGroup margin to avoid UI overlay when
1218 // emergency shortcut button more than 2.
1219 if (mEmergencyShortcutButtonList.size() > 2) {
1220 mEmergencyInfoGroup.updateLayoutMargin();
1221 }
CY Cheng9a69c182018-06-15 21:20:10 +08001222 } else {
1223 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1224 }
1225 }
1226 };
1227 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1228 }
1229
1230 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1231 if (!isFinishing() && !isDestroyed()) {
1232 setLocationInfo(iso);
1233 if (mShortcutAdapter != null) {
1234 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1235 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001236 }
1237 }
1238
1239 /**
1240 * Called by the activity before a touch event is dispatched to the view hierarchy.
1241 */
1242 private void onPreTouchEvent(MotionEvent event) {
1243 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001244 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001245
1246 if (mEmergencyShortcutButtonList != null) {
1247 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1248 button.onPreTouchEvent(event);
1249 }
1250 }
1251 }
1252
1253 /**
1254 * Called by the activity after a touch event is dispatched to the view hierarchy.
1255 */
1256 private void onPostTouchEvent(MotionEvent event) {
1257 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001258 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001259
1260 if (mEmergencyShortcutButtonList != null) {
1261 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1262 button.onPostTouchEvent(event);
1263 }
1264 }
1265 }
1266
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001267 /**
1268 * Switch two view.
1269 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001270 * @param displayView the view would be displayed.
1271 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001272 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1273 */
1274 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1275 if (displayView == null || hideView == null) {
1276 return;
1277 }
1278
1279 if (displayView.getVisibility() == View.VISIBLE) {
1280 return;
1281 }
1282
1283 if (hasAnimation) {
1284 crossfade(hideView, displayView);
1285 } else {
1286 hideView.setVisibility(View.GONE);
1287 displayView.setVisibility(View.VISIBLE);
1288 }
1289 }
1290
1291 /**
1292 * Fade out and fade in animation between two view transition.
1293 */
1294 private void crossfade(View fadeOutView, View fadeInView) {
1295 if (fadeOutView == null || fadeInView == null) {
1296 return;
1297 }
1298 final int shortAnimationDuration = getResources().getInteger(
1299 android.R.integer.config_shortAnimTime);
1300
1301 fadeInView.setAlpha(0f);
1302 fadeInView.setVisibility(View.VISIBLE);
1303
1304 fadeInView.animate()
1305 .alpha(1f)
1306 .setDuration(shortAnimationDuration)
1307 .setListener(null);
1308
1309 fadeOutView.animate()
1310 .alpha(0f)
1311 .setDuration(shortAnimationDuration)
1312 .setListener(new AnimatorListenerAdapter() {
1313 @Override
1314 public void onAnimationEnd(Animator animation) {
1315 fadeOutView.setVisibility(View.GONE);
1316 }
1317 });
1318 }
Shaotang Li8662a912018-07-04 16:53:01 +08001319
1320 @Override
1321 public void onSensorChanged(SensorEvent event) {
1322 float distance = event.values[0];
1323 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1324 }
1325
1326 @Override
1327 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1328 // Not used.
1329 }
1330
1331 private boolean isShortcutNumber(String number) {
1332 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1333 return false;
1334 }
1335
1336 boolean isShortcut = false;
1337 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1338 if (button != null && number.equals(button.getPhoneNumber())) {
1339 isShortcut = true;
1340 break;
1341 }
1342 }
1343 return isShortcut;
1344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345}