blob: ddc6f09dccf6cddebfe6e1b31838a077cf63b156 [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 Gunn1acf54a2018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangd301d9a2018-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 Li3b02aeb2018-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 Cheng42873672018-06-15 21:20:10 +080032import android.database.DataSetObserver;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +080033import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Shaotang Li3b02aeb2018-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 Li3b02aeb2018-07-04 16:53:01 +080041import android.metrics.LogMaker;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.net.Uri;
Tyler Gunn1acf54a2018-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 Li3b02aeb2018-07-04 16:53:01 +080046import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.provider.Settings;
Shaotang Lif27c74d2018-07-19 17:32:42 +080048import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070049import android.telecom.PhoneAccount;
Tyler Gunnca7dfef2018-03-27 18:50:05 +000050import 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 Chi9614de82018-06-15 19:00:15 +080053import android.telephony.Rlog;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070054import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070055import android.telephony.SubscriptionManager;
Tyler Gunn1acf54a2018-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 Gunn1acf54a2018-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 Chuang4f2b7812018-06-07 16:25:27 +080072import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import android.view.WindowManager;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080074import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075
Lucas Dupinaf9e9912017-06-22 12:39:39 -070076import com.android.internal.colorextraction.ColorExtractor;
77import com.android.internal.colorextraction.ColorExtractor.GradientColors;
78import com.android.internal.colorextraction.drawable.GradientDrawable;
Shaotang Li3b02aeb2018-07-04 16:53:01 +080079import com.android.internal.logging.MetricsLogger;
80import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Yorke Lee23a70732014-08-14 17:12:01 -070081import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070082import com.android.phone.common.util.ViewUtil;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070083import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng42873672018-06-15 21:20:10 +080084import com.android.phone.ecc.CountryEccInfo;
85import com.android.phone.ecc.EccInfoHelper;
Leo Hsu8f1c6712018-07-10 11:37:52 +080086import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080088import java.util.ArrayList;
89import java.util.List;
CY Cheng42873672018-06-15 21:20:10 +080090import java.util.Locale;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080091
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092/**
93 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
94 *
95 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
96 * activity from apps/Contacts) that:
97 * 1. Allows ONLY emergency calls to be dialed
98 * 2. Disallows voicemail functionality
99 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
100 * activity to stay in front of the keyguard.
101 *
102 * TODO: Even though this is an ultra-simplified version of the normal
103 * dialer, there's still lots of code duplication between this class and
104 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
105 * moved into a shared base class that would live in the framework?
106 * Or could we figure out some way to move *this* class into apps/Contacts
107 * also?
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800108 *
109 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800110 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
111 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800112 * TODO item:
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800113 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 */
115public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700116 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800117 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800118 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener {
119
120 private class MetricsWriter {
121 // Metrics constants indicating the entry type that user opened emergency dialer.
122 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
123 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
124 public static final int ENTRY_TYPE_UNKNOWN = 0;
125 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
126 public static final int ENTRY_TYPE_POWER_MENU = 2;
127
128 // Metrics constants indicating the UI that user made phone call.
129 public static final int CALL_SOURCE_DIALPAD = 0;
130 public static final int CALL_SOURCE_SHORTCUT = 1;
131
132 // Metrics constants indicating the phone number type of a call user made.
133 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
134 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
135
136 // Metrics constants indicating the actions performed by user.
137 public static final int USER_ACTION_NONE = 0x0;
138 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
139 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
140 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
141 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
142
143 private MetricsLogger mMetricsLogger = new MetricsLogger();
144
145 public void writeMetricsForEnter() {
146 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() {
156 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
157 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
158 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
159 .setType(MetricsEvent.TYPE_CLOSE)
160 .setSubtype(entryType)
161 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
162 .addTaggedData(
163 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
164 }
165
166 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
167 boolean hasShortcut) {
168 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
169 .setType(MetricsEvent.TYPE_ACTION)
170 .setSubtype(callSource)
171 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
172 phoneNumberType)
173 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
174 hasShortcut ? 1 : 0)
175 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
176 mIsProximityNear ? 1 : 0));
177 }
178 }
179
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 // Keys used with onSaveInstanceState().
181 private static final String LAST_NUMBER = "lastNumber";
182
183 // Intent action for this activity.
184 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
185
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800186 /**
187 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
188 * the emergency dialer.
189 */
190 public static final String EXTRA_ENTRY_TYPE =
191 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
192
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193 // List of dialer button IDs.
194 private static final int[] DIALER_KEYS = new int[] {
195 R.id.one, R.id.two, R.id.three,
196 R.id.four, R.id.five, R.id.six,
197 R.id.seven, R.id.eight, R.id.nine,
198 R.id.star, R.id.zero, R.id.pound };
199
200 // Debug constants.
201 private static final boolean DBG = false;
202 private static final String LOG_TAG = "EmergencyDialer";
203
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204 /** The length of DTMF tones in milliseconds */
205 private static final int TONE_LENGTH_MS = 150;
206
207 /** The DTMF tone volume relative to other sounds in the stream */
208 private static final int TONE_RELATIVE_VOLUME = 80;
209
210 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
211 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
212
213 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
214
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800215 /** 85% opacity for black background **/
216 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
217
CY Cheng42873672018-06-15 21:20:10 +0800218 /** Size limit of emergency shortcut buttons container. **/
219 private static final int SHORTCUT_SIZE_LIMIT = 3;
220
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700221 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 private View mDialButton;
223 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800224 private View mEmergencyShortcutView;
225 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226
CY Cheng42873672018-06-15 21:20:10 +0800227 private EccInfoHelper mEccInfoHelper;
228
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800229 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800230 private EccShortcutAdapter mShortcutAdapter;
231 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800232
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 private ToneGenerator mToneGenerator;
234 private Object mToneGeneratorLock = new Object();
235
236 // determines if we want to playback local DTMF tones.
237 private boolean mDTMFToneEnabled;
238
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700239 private EmergencyActionGroup mEmergencyActionGroup;
240
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241 // close activity when screen turns off
242 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
243 @Override
244 public void onReceive(Context context, Intent intent) {
245 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700246 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 }
248 }
249 };
250
251 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
252
Lucas Dupineb9c5702017-05-10 16:57:09 -0700253 // Background gradient
254 private ColorExtractor mColorExtractor;
255 private GradientDrawable mBackgroundGradient;
256 private boolean mSupportsDarkText;
257
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700258 private boolean mIsWfcEmergencyCallingWarningEnabled;
259 private float mDefaultDigitsTextSize;
260
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800261 private MetricsWriter mMetricsWriter;
262 private SensorManager mSensorManager;
263 private Sensor mProximitySensor;
264 private boolean mIsProximityNear = false;
265
266 /**
267 * The time, in millis, since boot when user opened emergency dialer.
268 * This is used when calculating the user stay duration for metrics data.
269 */
270 private long mUserEnterTimeMillis = 0;
271
272 /**
273 * Bit flag indicating the actions performed by user. This is used for metrics data.
274 */
275 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
276
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700277 @Override
278 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
279 // Do nothing
280 }
281
282 @Override
283 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700284 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 }
286
287 @Override
288 public void afterTextChanged(Editable input) {
289 // Check for special sequences, in particular the "**04" or "**05"
290 // sequences that allow you to enter PIN or PUK-related codes.
291 //
292 // But note we *don't* allow most other special sequences here,
293 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
294 // since those shouldn't be available if the device is locked.
295 //
296 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
297 // here, not the regular handleChars() method.
298 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
299 // A special sequence was entered, clear the digits
300 mDigits.getText().clear();
301 }
302
303 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800304 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 }
306
307 @Override
308 protected void onCreate(Bundle icicle) {
309 super.onCreate(icicle);
310
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800311 mMetricsWriter = new MetricsWriter();
312 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
313 if (mSensorManager != null) {
314 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
315 }
316
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 // Allow this activity to be displayed in front of the keyguard / lockscreen.
318 WindowManager.LayoutParams lp = getWindow().getAttributes();
319 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700320
321 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700322 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700323 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
324
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 getWindow().setAttributes(lp);
326
Lucas Dupineb9c5702017-05-10 16:57:09 -0700327 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800328
329 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
330 // And the background color is black with 85% opacity.
Leo Hsu83665d82018-10-02 09:57:03 +0800331 updateTheme(false);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700332
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333 setContentView(R.layout.emergency_dialer);
334
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700335 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 mDigits.setKeyListener(DialerKeyListener.getInstance());
337 mDigits.setOnClickListener(this);
338 mDigits.setOnKeyListener(this);
339 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700340 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700341 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 maybeAddNumberFormatting();
343
Lucas Dupineb9c5702017-05-10 16:57:09 -0700344 mBackgroundGradient = new GradientDrawable(this);
345 Point displaySize = new Point();
346 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
347 .getDefaultDisplay().getSize(displaySize);
348 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Leo Hsu83665d82018-10-02 09:57:03 +0800349 mBackgroundGradient.setAlpha(BLACK_BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700350 getWindow().setBackgroundDrawable(mBackgroundGradient);
351
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 // Check for the presence of the keypad
353 View view = findViewById(R.id.one);
354 if (view != null) {
355 setupKeypad();
356 }
357
358 mDelete = findViewById(R.id.deleteButton);
359 mDelete.setOnClickListener(this);
360 mDelete.setOnLongClickListener(this);
361
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700362 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700363
364 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700365 // Read carrier config through the public API because PhoneGlobals is not available when we
366 // run as a secondary user.
367 CarrierConfigManager configMgr =
368 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
369 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800370 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700371
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700372 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373 mDialButton.setOnClickListener(this);
374 } else {
375 mDialButton.setVisibility(View.GONE);
376 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700377 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
378 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
379 maybeShowWfcEmergencyCallingWarning();
380
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700381 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700382
383 if (icicle != null) {
384 super.onRestoreInstanceState(icicle);
385 }
386
387 // Extract phone number from intent
388 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700389 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700390 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
391 if (number != null) {
392 mDigits.setText(number);
393 }
394 }
395
396 // if the mToneGenerator creation fails, just continue without it. It is
397 // a local audio signal, and is not as important as the dtmf tone itself.
398 synchronized (mToneGeneratorLock) {
399 if (mToneGenerator == null) {
400 try {
401 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
402 } catch (RuntimeException e) {
403 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
404 mToneGenerator = null;
405 }
406 }
407 }
408
409 final IntentFilter intentFilter = new IntentFilter();
410 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
411 registerReceiver(mBroadcastReceiver, intentFilter);
412
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700413 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800414
Leo Hsu83665d82018-10-02 09:57:03 +0800415 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
416 setupEmergencyShortcutsView();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700417 }
418
419 @Override
420 protected void onDestroy() {
421 super.onDestroy();
422 synchronized (mToneGeneratorLock) {
423 if (mToneGenerator != null) {
424 mToneGenerator.release();
425 mToneGenerator = null;
426 }
427 }
428 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800429 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
430 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
431 mShortcutDataSetObserver = null;
432 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 }
434
435 @Override
436 protected void onRestoreInstanceState(Bundle icicle) {
437 mLastNumber = icicle.getString(LAST_NUMBER);
438 }
439
440 @Override
441 protected void onSaveInstanceState(Bundle outState) {
442 super.onSaveInstanceState(outState);
443 outState.putString(LAST_NUMBER, mLastNumber);
444 }
445
446 /**
447 * Explicitly turn off number formatting, since it gets in the way of the emergency
448 * number detector
449 */
450 protected void maybeAddNumberFormatting() {
451 // Do nothing.
452 }
453
454 @Override
455 protected void onPostCreate(Bundle savedInstanceState) {
456 super.onPostCreate(savedInstanceState);
457
458 // This can't be done in onCreate(), since the auto-restoring of the digits
459 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
460 // is called. This method will be called every time the activity is created, and
461 // will always happen after onRestoreSavedInstanceState().
462 mDigits.addTextChangedListener(this);
463 }
464
465 private void setupKeypad() {
466 // Setup the listeners for the buttons
467 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700468 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
469 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700470 }
471
472 View view = findViewById(R.id.zero);
473 view.setOnLongClickListener(this);
474 }
475
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800476 @Override
477 public void onBackPressed() {
478 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800479 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800480 // Otherwise, it would finish the activity.
Leo Hsu83665d82018-10-02 09:57:03 +0800481 if (mDialpadView != null && mDialpadView.getVisibility() == View.VISIBLE) {
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800482 switchView(mEmergencyShortcutView, mDialpadView, true);
483 return;
484 }
485 super.onBackPressed();
486 }
487
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700488 /**
489 * handle key events
490 */
491 @Override
492 public boolean onKeyDown(int keyCode, KeyEvent event) {
493 switch (keyCode) {
494 // Happen when there's a "Call" hard button.
495 case KeyEvent.KEYCODE_CALL: {
496 if (TextUtils.isEmpty(mDigits.getText().toString())) {
497 // if we are adding a call from the InCallScreen and the phone
498 // number entered is empty, we just close the dialer to expose
499 // the InCallScreen under it.
500 finish();
501 } else {
502 // otherwise, we place the call.
503 placeCall();
504 }
505 return true;
506 }
507 }
508 return super.onKeyDown(keyCode, event);
509 }
510
511 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700512 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700513 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
514 mDigits.onKeyDown(keyCode, event);
515 }
516
517 @Override
518 public boolean onKey(View view, int keyCode, KeyEvent event) {
519 switch (view.getId()) {
520 case R.id.digits:
521 // Happen when "Done" button of the IME is pressed. This can happen when this
522 // Activity is forced into landscape mode due to a desk dock.
523 if (keyCode == KeyEvent.KEYCODE_ENTER
524 && event.getAction() == KeyEvent.ACTION_UP) {
525 placeCall();
526 return true;
527 }
528 break;
529 }
530 return false;
531 }
532
533 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700534 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800535 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700536 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800537 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700538 return handled;
539 }
540
541 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800542 public void onConfirmClick(EmergencyShortcutButton button) {
543 if (button == null) return;
544
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800545 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
546
547 // We interest on the context when user has intention to make phone call,
548 // so write metrics here for shortcut number even the call may not be created.
549 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
550 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
551
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800552 String phoneNumber = button.getPhoneNumber();
553
554 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800555 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Lif27c74d2018-07-19 17:32:42 +0800556 Bundle extras = new Bundle();
557 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
558 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800559 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800560 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800561 } else {
562 Log.d(LOG_TAG, "emergency number is empty");
563 }
564 }
565
566 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 public void onClick(View view) {
568 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700569 case R.id.deleteButton: {
570 keyPressed(KeyEvent.KEYCODE_DEL);
571 return;
572 }
573 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700574 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700575 placeCall();
576 return;
577 }
578 case R.id.digits: {
579 if (mDigits.length() != 0) {
580 mDigits.setCursorVisible(true);
581 }
582 return;
583 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800584 case R.id.floating_action_button_dialpad: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800585 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800586 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800587 switchView(mDialpadView, mEmergencyShortcutView, true);
588 return;
589 }
590 case R.id.emergency_info_button: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800591 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800592 Intent intent = (Intent) view.getTag(R.id.tag_intent);
593 if (intent != null) {
594 startActivity(intent);
595 }
596 return;
597 }
Yorke Lee23a70732014-08-14 17:12:01 -0700598 }
599 }
600
601 @Override
602 public void onPressed(View view, boolean pressed) {
603 if (!pressed) {
604 return;
605 }
606 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 case R.id.one: {
608 playTone(ToneGenerator.TONE_DTMF_1);
609 keyPressed(KeyEvent.KEYCODE_1);
610 return;
611 }
612 case R.id.two: {
613 playTone(ToneGenerator.TONE_DTMF_2);
614 keyPressed(KeyEvent.KEYCODE_2);
615 return;
616 }
617 case R.id.three: {
618 playTone(ToneGenerator.TONE_DTMF_3);
619 keyPressed(KeyEvent.KEYCODE_3);
620 return;
621 }
622 case R.id.four: {
623 playTone(ToneGenerator.TONE_DTMF_4);
624 keyPressed(KeyEvent.KEYCODE_4);
625 return;
626 }
627 case R.id.five: {
628 playTone(ToneGenerator.TONE_DTMF_5);
629 keyPressed(KeyEvent.KEYCODE_5);
630 return;
631 }
632 case R.id.six: {
633 playTone(ToneGenerator.TONE_DTMF_6);
634 keyPressed(KeyEvent.KEYCODE_6);
635 return;
636 }
637 case R.id.seven: {
638 playTone(ToneGenerator.TONE_DTMF_7);
639 keyPressed(KeyEvent.KEYCODE_7);
640 return;
641 }
642 case R.id.eight: {
643 playTone(ToneGenerator.TONE_DTMF_8);
644 keyPressed(KeyEvent.KEYCODE_8);
645 return;
646 }
647 case R.id.nine: {
648 playTone(ToneGenerator.TONE_DTMF_9);
649 keyPressed(KeyEvent.KEYCODE_9);
650 return;
651 }
652 case R.id.zero: {
653 playTone(ToneGenerator.TONE_DTMF_0);
654 keyPressed(KeyEvent.KEYCODE_0);
655 return;
656 }
657 case R.id.pound: {
658 playTone(ToneGenerator.TONE_DTMF_P);
659 keyPressed(KeyEvent.KEYCODE_POUND);
660 return;
661 }
662 case R.id.star: {
663 playTone(ToneGenerator.TONE_DTMF_S);
664 keyPressed(KeyEvent.KEYCODE_STAR);
665 return;
666 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 }
668 }
669
670 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700671 * called for long touch events
672 */
673 @Override
674 public boolean onLongClick(View view) {
675 int id = view.getId();
676 switch (id) {
677 case R.id.deleteButton: {
678 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 return true;
680 }
681 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700682 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700683 keyPressed(KeyEvent.KEYCODE_PLUS);
684 return true;
685 }
686 }
687 return false;
688 }
689
690 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700691 protected void onStart() {
692 super.onStart();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800693
694 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
695 mUserActions = MetricsWriter.USER_ACTION_NONE;
696 mMetricsWriter.writeMetricsForEnter();
697
Leo Hsu83665d82018-10-02 09:57:03 +0800698 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
699 updateTheme(false);
CY Cheng42873672018-06-15 21:20:10 +0800700
Leo Hsu83665d82018-10-02 09:57:03 +0800701 if (mEccInfoHelper != null) {
CY Cheng42873672018-06-15 21:20:10 +0800702 final Context context = this;
703 mEccInfoHelper.getCountryEccInfoAsync(context,
704 new EccInfoHelper.CountryEccInfoResultCallback() {
705 @Override
706 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
707 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
708 + Rlog.pii(LOG_TAG, iso));
709 updateLocationAndEccInfo(iso, countryEccInfo);
710 }
711
712 @Override
713 public void onDetectCountryFailed() {
714 Log.w(LOG_TAG, "Cannot detect current country.");
715 updateLocationAndEccInfo(null, null);
716 }
717
718 @Override
719 public void onRetrieveCountryEccInfoFailed(String iso) {
720 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
721 + Rlog.pii(LOG_TAG, iso));
722 updateLocationAndEccInfo(iso, null);
723 }
724 });
725 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700726 }
727
728 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 protected void onResume() {
730 super.onResume();
731
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800732 if (mProximitySensor != null) {
733 mSensorManager.registerListener(
734 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
735 }
736
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 // retrieve the DTMF tone play back setting.
738 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
739 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
740
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 // if the mToneGenerator creation fails, just continue without it. It is
742 // a local audio signal, and is not as important as the dtmf tone itself.
743 synchronized (mToneGeneratorLock) {
744 if (mToneGenerator == null) {
745 try {
746 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
747 TONE_RELATIVE_VOLUME);
748 } catch (RuntimeException e) {
749 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
750 mToneGenerator = null;
751 }
752 }
753 }
754
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 updateDialAndDeleteButtonStateEnabledAttr();
756 }
757
758 @Override
759 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 super.onPause();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800761 if (mProximitySensor != null) {
762 mSensorManager.unregisterListener(this, mProximitySensor);
763 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 }
765
Lucas Dupineb9c5702017-05-10 16:57:09 -0700766 @Override
767 protected void onStop() {
768 super.onStop();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800769 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700770 mColorExtractor.removeOnColorsChangedListener(this);
771 }
772
773 /**
774 * Sets theme based on gradient colors
775 * @param supportsDarkText true if gradient supports dark text
776 */
777 private void updateTheme(boolean supportsDarkText) {
778 if (mSupportsDarkText == supportsDarkText) {
779 return;
780 }
781 mSupportsDarkText = supportsDarkText;
782
783 // We can't change themes after inflation, in this case we'll have to recreate
784 // the whole activity.
785 if (mBackgroundGradient != null) {
786 recreate();
787 return;
788 }
789
790 int vis = getWindow().getDecorView().getSystemUiVisibility();
791 if (supportsDarkText) {
792 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
793 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
794 setTheme(R.style.EmergencyDialerThemeDark);
795 } else {
796 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
797 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
798 setTheme(R.style.EmergencyDialerTheme);
799 }
800 getWindow().getDecorView().setSystemUiVisibility(vis);
801 }
802
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 /**
804 * place the call, but check to make sure it is a viable number.
805 */
806 private void placeCall() {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800807 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900809
810 // Convert into emergency number according to emergency conversion map.
811 // If conversion map is not defined (this is default), this method does
812 // nothing and just returns input number.
813 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
814
Yorke Lee36bb2542014-06-05 08:09:52 -0700815 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700816 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
817
818 // place the call if it is a valid number
819 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
820 // There is no number entered.
821 playTone(ToneGenerator.TONE_PROP_NACK);
822 return;
823 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800824
825 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
826 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
827
Shaotang Lif27c74d2018-07-19 17:32:42 +0800828 Bundle extras = new Bundle();
829 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
830 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000831 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800832 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700833 } else {
834 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
835
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800836 // We interest on the context when user has intention to make phone call,
837 // so write metrics here for non-emergency numbers even these numbers are rejected.
838 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
839 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
840
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
842 }
Yorke Lee9b341512014-10-17 11:36:41 -0700843 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700844 }
845
846 /**
847 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
848 *
849 * The tone is played locally, using the audio stream for phone calls.
850 * Tones are played only if the "Audible touch tones" user preference
851 * is checked, and are NOT played if the device is in silent mode.
852 *
853 * @param tone a tone code from {@link ToneGenerator}
854 */
855 void playTone(int tone) {
856 // if local tone playback is disabled, just return.
857 if (!mDTMFToneEnabled) {
858 return;
859 }
860
861 // Also do nothing if the phone is in silent mode.
862 // We need to re-check the ringer mode for *every* playTone()
863 // call, rather than keeping a local flag that's updated in
864 // onResume(), since it's possible to toggle silent mode without
865 // leaving the current activity (via the ENDCALL-longpress menu.)
866 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
867 int ringerMode = audioManager.getRingerMode();
868 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
869 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
870 return;
871 }
872
873 synchronized (mToneGeneratorLock) {
874 if (mToneGenerator == null) {
875 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
876 return;
877 }
878
879 // Start the new tone (will stop any playing tone)
880 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
881 }
882 }
883
884 private CharSequence createErrorMessage(String number) {
885 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800886 String errorString = getString(R.string.dial_emergency_error, number);
887 int startingPosition = errorString.indexOf(number);
888 int endingPosition = startingPosition + number.length();
889 Spannable result = new SpannableString(errorString);
890 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
891 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700892 } else {
893 return getText(R.string.dial_emergency_empty_error).toString();
894 }
895 }
896
897 @Override
898 protected Dialog onCreateDialog(int id) {
899 AlertDialog dialog = null;
900 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
901 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700902 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700903 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
904 .setMessage(createErrorMessage(mLastNumber))
905 .setPositiveButton(R.string.ok, null)
906 .setCancelable(true).create();
907
908 // blur stuff behind the dialog
909 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700910 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 }
912 return dialog;
913 }
914
915 @Override
916 protected void onPrepareDialog(int id, Dialog dialog) {
917 super.onPrepareDialog(id, dialog);
918 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
919 AlertDialog alert = (AlertDialog) dialog;
920 alert.setMessage(createErrorMessage(mLastNumber));
921 }
922 }
923
Andrew Leed5631e82014-10-08 16:03:58 -0700924 @Override
925 public boolean onOptionsItemSelected(MenuItem item) {
926 final int itemId = item.getItemId();
927 if (itemId == android.R.id.home) {
928 onBackPressed();
929 return true;
930 }
931 return super.onOptionsItemSelected(item);
932 }
933
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 /**
935 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
936 */
937 private void updateDialAndDeleteButtonStateEnabledAttr() {
938 final boolean notEmpty = mDigits.length() != 0;
939
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 mDelete.setEnabled(notEmpty);
941 }
Yorke Lee91311662014-10-24 14:50:45 -0700942
943 /**
944 * Remove the digit just before the current position. Used by various long pressed callbacks
945 * to remove the digit that was populated as a result of the short click.
946 */
947 private void removePreviousDigitIfPossible() {
948 final int currentPosition = mDigits.getSelectionStart();
949 if (currentPosition > 0) {
950 mDigits.setSelection(currentPosition);
951 mDigits.getText().delete(currentPosition - 1, currentPosition);
952 }
953 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800954
955 /**
956 * Update the text-to-speech annotations in the edit field.
957 */
958 private void updateTtsSpans() {
959 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
960 mDigits.getText().removeSpan(o);
961 }
962 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
963 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700964
965 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700966 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700967 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700968 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
969 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700970 mBackgroundGradient.setColors(colors);
971 updateTheme(colors.supportsDarkText());
972 }
973 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700974
975 /**
976 * Where a carrier requires a warning that emergency calling is not available while on WFC,
977 * add hint text above the dial pad which warns the user of this case.
978 */
979 private void maybeShowWfcEmergencyCallingWarning() {
980 if (!mIsWfcEmergencyCallingWarningEnabled) {
981 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
982 return;
983 }
984
985 // Use an async task rather than calling into Telephony on UI thread.
986 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
987 @Override
988 protected Boolean doInBackground(Void... voids) {
989 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
990 boolean isWfcAvailable = tm.isWifiCallingAvailable();
991 ServiceState ss = tm.getServiceState();
992 boolean isCellAvailable =
993 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
994 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
995 + " isCellAvailable=" + isCellAvailable
996 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
997 return isWfcAvailable && !isCellAvailable;
998 }
999
1000 @Override
1001 protected void onPostExecute(Boolean result) {
1002 if (result.booleanValue()) {
1003 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1004 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1005 } else {
1006 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1007 mDigits.setHint("");
1008 }
1009 maybeChangeHintSize();
1010 }
1011 };
1012 showWfcWarningTask.execute((Void) null);
1013 }
1014
1015 /**
1016 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1017 * edit view and set the font size to a smaller size appropriate for the emergency calling
1018 * warning.
1019 */
1020 private void maybeChangeHintSize() {
1021 if (TextUtils.isEmpty(mDigits.getHint())
1022 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1023 // No hint or there are dialed digits, so use default size.
1024 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1025 // By default, the digits view auto-resizes to fit the text it contains, so
1026 // enable that now.
1027 mDigits.setResizeEnabled(true);
1028 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1029 } else {
1030 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1031 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1032 R.dimen.emergency_call_warning_size));
1033 // Since we're populating this with a static text string, disable auto-resize.
1034 mDigits.setResizeEnabled(false);
1035 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1036 }
1037 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001038
1039 private void setupEmergencyShortcutsView() {
1040 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1041 mDialpadView = findViewById(R.id.emergency_dialer);
1042
1043 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1044 dialpadButton.setOnClickListener(this);
1045
1046 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
1047 emergencyInfoButton.setOnClickListener(this);
1048
1049 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1050 mEmergencyActionGroup.setVisibility(View.GONE);
1051
1052 // Setup dialpad title.
1053 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1054 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1055
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001056 mEmergencyShortcutButtonList = new ArrayList<>();
1057 setupEmergencyCallShortcutButton();
1058
CY Cheng42873672018-06-15 21:20:10 +08001059 updateLocationAndEccInfo(null, null);
1060
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001061 switchView(mEmergencyShortcutView, mDialpadView, false);
1062 }
1063
CY Cheng42873672018-06-15 21:20:10 +08001064 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001065 final View locationInfo = findViewById(R.id.location_info);
1066
CY Cheng42873672018-06-15 21:20:10 +08001067 String countryName = null;
1068 if (!TextUtils.isEmpty(countryIso)) {
1069 Locale locale = Locale.getDefault();
1070 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1071 .getDisplayCountry();
1072 }
1073 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001074 locationInfo.setVisibility(View.INVISIBLE);
1075 } else {
1076 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +08001077 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001078 locationInfo.setVisibility(View.VISIBLE);
1079 }
1080 }
1081
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001082 private void setupEmergencyCallShortcutButton() {
1083 final ViewGroup shortcutButtonContainer = findViewById(
1084 R.id.emergency_shortcut_buttons_container);
1085 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +08001086 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001087
CY Cheng42873672018-06-15 21:20:10 +08001088 mShortcutAdapter = new EccShortcutAdapter(this) {
1089 @Override
1090 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1091 CharSequence description, int iconRes) {
1092 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1093 .inflate(R.layout.emergency_shortcut_button, parent, false);
1094 button.setPhoneNumber(number);
1095 button.setPhoneDescription(description);
1096 button.setPhoneTypeIcon(iconRes);
1097 button.setOnConfirmClickListener(EmergencyDialer.this);
1098 return button;
1099 }
1100 };
1101 mShortcutDataSetObserver = new DataSetObserver() {
1102 @Override
1103 public void onChanged() {
1104 super.onChanged();
1105 updateLayout();
1106 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001107
CY Cheng42873672018-06-15 21:20:10 +08001108 @Override
1109 public void onInvalidated() {
1110 super.onInvalidated();
1111 updateLayout();
1112 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001113
CY Cheng42873672018-06-15 21:20:10 +08001114 private void updateLayout() {
1115 // clear previous added buttons
1116 shortcutButtonContainer.removeAllViews();
1117 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001118
CY Cheng42873672018-06-15 21:20:10 +08001119 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1120 EmergencyShortcutButton button = (EmergencyShortcutButton)
1121 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1122 mEmergencyShortcutButtonList.add(button);
1123 shortcutButtonContainer.addView(button);
1124 }
1125
1126 // update emergency numbers title for numerous buttons.
1127 if (mEmergencyShortcutButtonList.size() > 1) {
1128 emergencyNumberTitle.setText(getString(
1129 R.string.numerous_emergency_numbers_title));
1130 } else {
1131 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1132 }
1133 }
1134 };
1135 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1136 }
1137
1138 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1139 if (!isFinishing() && !isDestroyed()) {
1140 setLocationInfo(iso);
1141 if (mShortcutAdapter != null) {
1142 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1143 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001144 }
1145 }
1146
1147 /**
1148 * Called by the activity before a touch event is dispatched to the view hierarchy.
1149 */
1150 private void onPreTouchEvent(MotionEvent event) {
1151 mEmergencyActionGroup.onPreTouchEvent(event);
1152
1153 if (mEmergencyShortcutButtonList != null) {
1154 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1155 button.onPreTouchEvent(event);
1156 }
1157 }
1158 }
1159
1160 /**
1161 * Called by the activity after a touch event is dispatched to the view hierarchy.
1162 */
1163 private void onPostTouchEvent(MotionEvent event) {
1164 mEmergencyActionGroup.onPostTouchEvent(event);
1165
1166 if (mEmergencyShortcutButtonList != null) {
1167 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1168 button.onPostTouchEvent(event);
1169 }
1170 }
1171 }
1172
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001173 /**
1174 * Switch two view.
1175 *
1176 * @param displayView the view would be displayed.
1177 * @param hideView the view would be hidden.
1178 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1179 */
1180 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1181 if (displayView == null || hideView == null) {
1182 return;
1183 }
1184
1185 if (displayView.getVisibility() == View.VISIBLE) {
1186 return;
1187 }
1188
1189 if (hasAnimation) {
1190 crossfade(hideView, displayView);
1191 } else {
1192 hideView.setVisibility(View.GONE);
1193 displayView.setVisibility(View.VISIBLE);
1194 }
1195 }
1196
1197 /**
1198 * Fade out and fade in animation between two view transition.
1199 */
1200 private void crossfade(View fadeOutView, View fadeInView) {
1201 if (fadeOutView == null || fadeInView == null) {
1202 return;
1203 }
1204 final int shortAnimationDuration = getResources().getInteger(
1205 android.R.integer.config_shortAnimTime);
1206
1207 fadeInView.setAlpha(0f);
1208 fadeInView.setVisibility(View.VISIBLE);
1209
1210 fadeInView.animate()
1211 .alpha(1f)
1212 .setDuration(shortAnimationDuration)
1213 .setListener(null);
1214
1215 fadeOutView.animate()
1216 .alpha(0f)
1217 .setDuration(shortAnimationDuration)
1218 .setListener(new AnimatorListenerAdapter() {
1219 @Override
1220 public void onAnimationEnd(Animator animation) {
1221 fadeOutView.setVisibility(View.GONE);
1222 }
1223 });
1224 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +08001225
1226 @Override
1227 public void onSensorChanged(SensorEvent event) {
1228 float distance = event.values[0];
1229 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1230 }
1231
1232 @Override
1233 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1234 // Not used.
1235 }
1236
1237 private boolean isShortcutNumber(String number) {
1238 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1239 return false;
1240 }
1241
1242 boolean isShortcut = false;
1243 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1244 if (button != null && number.equals(button.getPhoneNumber())) {
1245 isShortcut = true;
1246 break;
1247 }
1248 }
1249 return isShortcut;
1250 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001251}