blob: 9e5061c65714b73da8bc6736915c559d1d16afb5 [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?
108 */
109public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700110 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800111 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +0800112 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener,
113 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800114
115 private class MetricsWriter {
116 // Metrics constants indicating the entry type that user opened emergency dialer.
117 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
118 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
119 public static final int ENTRY_TYPE_UNKNOWN = 0;
120 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
121 public static final int ENTRY_TYPE_POWER_MENU = 2;
122
123 // Metrics constants indicating the UI that user made phone call.
124 public static final int CALL_SOURCE_DIALPAD = 0;
125 public static final int CALL_SOURCE_SHORTCUT = 1;
126
127 // Metrics constants indicating the phone number type of a call user made.
128 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
129 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
130
131 // Metrics constants indicating the actions performed by user.
132 public static final int USER_ACTION_NONE = 0x0;
133 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
134 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
135 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
136 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
137
138 private MetricsLogger mMetricsLogger = new MetricsLogger();
139
140 public void writeMetricsForEnter() {
141 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
142 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
143 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
144 .setType(MetricsEvent.TYPE_OPEN)
145 .setSubtype(entryType)
146 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
147 keyguard.isKeyguardLocked() ? 1 : 0));
148 }
149
150 public void writeMetricsForExit() {
151 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
152 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
153 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
154 .setType(MetricsEvent.TYPE_CLOSE)
155 .setSubtype(entryType)
156 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
157 .addTaggedData(
158 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
159 }
160
161 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
162 boolean hasShortcut) {
163 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
164 .setType(MetricsEvent.TYPE_ACTION)
165 .setSubtype(callSource)
166 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
167 phoneNumberType)
168 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
169 hasShortcut ? 1 : 0)
170 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
171 mIsProximityNear ? 1 : 0));
172 }
173 }
174
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175 // Keys used with onSaveInstanceState().
176 private static final String LAST_NUMBER = "lastNumber";
177
178 // Intent action for this activity.
179 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
180
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800181 /**
182 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
183 * the emergency dialer.
184 */
185 public static final String EXTRA_ENTRY_TYPE =
186 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 // List of dialer button IDs.
189 private static final int[] DIALER_KEYS = new int[] {
190 R.id.one, R.id.two, R.id.three,
191 R.id.four, R.id.five, R.id.six,
192 R.id.seven, R.id.eight, R.id.nine,
193 R.id.star, R.id.zero, R.id.pound };
194
195 // Debug constants.
196 private static final boolean DBG = false;
197 private static final String LOG_TAG = "EmergencyDialer";
198
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 /** The length of DTMF tones in milliseconds */
200 private static final int TONE_LENGTH_MS = 150;
201
202 /** The DTMF tone volume relative to other sounds in the stream */
203 private static final int TONE_RELATIVE_VOLUME = 80;
204
205 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
206 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
207
208 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
209
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800210 /** 85% opacity for black background **/
211 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
212
CY Cheng42873672018-06-15 21:20:10 +0800213 /** Size limit of emergency shortcut buttons container. **/
214 private static final int SHORTCUT_SIZE_LIMIT = 3;
215
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700216 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 private View mDialButton;
218 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800219 private View mEmergencyShortcutView;
220 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221
CY Cheng42873672018-06-15 21:20:10 +0800222 private EccInfoHelper mEccInfoHelper;
223
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800224 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800225 private EccShortcutAdapter mShortcutAdapter;
226 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800227
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 private ToneGenerator mToneGenerator;
229 private Object mToneGeneratorLock = new Object();
230
231 // determines if we want to playback local DTMF tones.
232 private boolean mDTMFToneEnabled;
233
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700234 private EmergencyActionGroup mEmergencyActionGroup;
235
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +0800236 private EmergencyInfoGroup mEmergencyInfoGroup;
237
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 // close activity when screen turns off
239 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
240 @Override
241 public void onReceive(Context context, Intent intent) {
242 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700243 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 }
245 }
246 };
247
248 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
249
Lucas Dupineb9c5702017-05-10 16:57:09 -0700250 // Background gradient
251 private ColorExtractor mColorExtractor;
252 private GradientDrawable mBackgroundGradient;
253 private boolean mSupportsDarkText;
254
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700255 private boolean mIsWfcEmergencyCallingWarningEnabled;
256 private float mDefaultDigitsTextSize;
257
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800258 private MetricsWriter mMetricsWriter;
259 private SensorManager mSensorManager;
260 private Sensor mProximitySensor;
261 private boolean mIsProximityNear = false;
262
263 /**
264 * The time, in millis, since boot when user opened emergency dialer.
265 * This is used when calculating the user stay duration for metrics data.
266 */
267 private long mUserEnterTimeMillis = 0;
268
269 /**
270 * Bit flag indicating the actions performed by user. This is used for metrics data.
271 */
272 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
273
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 @Override
275 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
276 // Do nothing
277 }
278
279 @Override
280 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700281 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282 }
283
284 @Override
285 public void afterTextChanged(Editable input) {
286 // Check for special sequences, in particular the "**04" or "**05"
287 // sequences that allow you to enter PIN or PUK-related codes.
288 //
289 // But note we *don't* allow most other special sequences here,
290 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
291 // since those shouldn't be available if the device is locked.
292 //
293 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
294 // here, not the regular handleChars() method.
295 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
296 // A special sequence was entered, clear the digits
297 mDigits.getText().clear();
298 }
299
300 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800301 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 }
303
304 @Override
305 protected void onCreate(Bundle icicle) {
306 super.onCreate(icicle);
307
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800308 mMetricsWriter = new MetricsWriter();
309 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
310 if (mSensorManager != null) {
311 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
312 }
313
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314 // Allow this activity to be displayed in front of the keyguard / lockscreen.
315 WindowManager.LayoutParams lp = getWindow().getAttributes();
316 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700317
318 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700319 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700320 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
321
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 getWindow().setAttributes(lp);
323
Lucas Dupineb9c5702017-05-10 16:57:09 -0700324 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800325
326 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
327 // And the background color is black with 85% opacity.
Leo Hsu83665d82018-10-02 09:57:03 +0800328 updateTheme(false);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700329
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 setContentView(R.layout.emergency_dialer);
331
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700332 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333 mDigits.setKeyListener(DialerKeyListener.getInstance());
334 mDigits.setOnClickListener(this);
335 mDigits.setOnKeyListener(this);
336 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700337 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700338 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 maybeAddNumberFormatting();
340
Lucas Dupineb9c5702017-05-10 16:57:09 -0700341 mBackgroundGradient = new GradientDrawable(this);
342 Point displaySize = new Point();
343 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
344 .getDefaultDisplay().getSize(displaySize);
345 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Leo Hsu83665d82018-10-02 09:57:03 +0800346 mBackgroundGradient.setAlpha(BLACK_BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700347 getWindow().setBackgroundDrawable(mBackgroundGradient);
348
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700349 // Check for the presence of the keypad
350 View view = findViewById(R.id.one);
351 if (view != null) {
352 setupKeypad();
353 }
354
355 mDelete = findViewById(R.id.deleteButton);
356 mDelete.setOnClickListener(this);
357 mDelete.setOnLongClickListener(this);
358
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700359 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360
361 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700362 // Read carrier config through the public API because PhoneGlobals is not available when we
363 // run as a secondary user.
364 CarrierConfigManager configMgr =
365 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
366 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800367 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700368
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700369 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370 mDialButton.setOnClickListener(this);
371 } else {
372 mDialButton.setVisibility(View.GONE);
373 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700374 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
375 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
376 maybeShowWfcEmergencyCallingWarning();
377
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700378 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700379
380 if (icicle != null) {
381 super.onRestoreInstanceState(icicle);
382 }
383
384 // Extract phone number from intent
385 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700386 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
388 if (number != null) {
389 mDigits.setText(number);
390 }
391 }
392
393 // if the mToneGenerator creation fails, just continue without it. It is
394 // a local audio signal, and is not as important as the dtmf tone itself.
395 synchronized (mToneGeneratorLock) {
396 if (mToneGenerator == null) {
397 try {
398 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
399 } catch (RuntimeException e) {
400 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
401 mToneGenerator = null;
402 }
403 }
404 }
405
406 final IntentFilter intentFilter = new IntentFilter();
407 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
408 registerReceiver(mBroadcastReceiver, intentFilter);
409
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700410 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800411
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +0800412 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
413
Leo Hsu83665d82018-10-02 09:57:03 +0800414 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
415 setupEmergencyShortcutsView();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 }
417
418 @Override
419 protected void onDestroy() {
420 super.onDestroy();
421 synchronized (mToneGeneratorLock) {
422 if (mToneGenerator != null) {
423 mToneGenerator.release();
424 mToneGenerator = null;
425 }
426 }
427 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800428 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
429 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
430 mShortcutDataSetObserver = null;
431 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700432 }
433
434 @Override
435 protected void onRestoreInstanceState(Bundle icicle) {
436 mLastNumber = icicle.getString(LAST_NUMBER);
437 }
438
439 @Override
440 protected void onSaveInstanceState(Bundle outState) {
441 super.onSaveInstanceState(outState);
442 outState.putString(LAST_NUMBER, mLastNumber);
443 }
444
445 /**
446 * Explicitly turn off number formatting, since it gets in the way of the emergency
447 * number detector
448 */
449 protected void maybeAddNumberFormatting() {
450 // Do nothing.
451 }
452
453 @Override
454 protected void onPostCreate(Bundle savedInstanceState) {
455 super.onPostCreate(savedInstanceState);
456
457 // This can't be done in onCreate(), since the auto-restoring of the digits
458 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
459 // is called. This method will be called every time the activity is created, and
460 // will always happen after onRestoreSavedInstanceState().
461 mDigits.addTextChangedListener(this);
462 }
463
464 private void setupKeypad() {
465 // Setup the listeners for the buttons
466 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700467 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
468 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 }
470
471 View view = findViewById(R.id.zero);
472 view.setOnLongClickListener(this);
473 }
474
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800475 @Override
476 public void onBackPressed() {
477 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800478 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800479 // Otherwise, it would finish the activity.
Leo Hsu83665d82018-10-02 09:57:03 +0800480 if (mDialpadView != null && mDialpadView.getVisibility() == View.VISIBLE) {
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800481 switchView(mEmergencyShortcutView, mDialpadView, true);
482 return;
483 }
484 super.onBackPressed();
485 }
486
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700487 /**
488 * handle key events
489 */
490 @Override
491 public boolean onKeyDown(int keyCode, KeyEvent event) {
492 switch (keyCode) {
493 // Happen when there's a "Call" hard button.
494 case KeyEvent.KEYCODE_CALL: {
495 if (TextUtils.isEmpty(mDigits.getText().toString())) {
496 // if we are adding a call from the InCallScreen and the phone
497 // number entered is empty, we just close the dialer to expose
498 // the InCallScreen under it.
499 finish();
500 } else {
501 // otherwise, we place the call.
502 placeCall();
503 }
504 return true;
505 }
506 }
507 return super.onKeyDown(keyCode, event);
508 }
509
510 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700511 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
513 mDigits.onKeyDown(keyCode, event);
514 }
515
516 @Override
517 public boolean onKey(View view, int keyCode, KeyEvent event) {
518 switch (view.getId()) {
519 case R.id.digits:
520 // Happen when "Done" button of the IME is pressed. This can happen when this
521 // Activity is forced into landscape mode due to a desk dock.
522 if (keyCode == KeyEvent.KEYCODE_ENTER
523 && event.getAction() == KeyEvent.ACTION_UP) {
524 placeCall();
525 return true;
526 }
527 break;
528 }
529 return false;
530 }
531
532 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700533 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800534 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700535 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800536 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700537 return handled;
538 }
539
540 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800541 public void onConfirmClick(EmergencyShortcutButton button) {
542 if (button == null) return;
543
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800544 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
545
546 // We interest on the context when user has intention to make phone call,
547 // so write metrics here for shortcut number even the call may not be created.
548 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
549 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
550
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800551 String phoneNumber = button.getPhoneNumber();
552
553 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800554 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Lif27c74d2018-07-19 17:32:42 +0800555 Bundle extras = new Bundle();
556 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
557 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800558 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800559 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800560 } else {
561 Log.d(LOG_TAG, "emergency number is empty");
562 }
563 }
564
565 @Override
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +0800566 public void onConfirmClick(EmergencyInfoGroup button) {
567 if (button == null) return;
568
569 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
570 Intent intent = (Intent) button.getTag(R.id.tag_intent);
571 if (intent != null) {
572 startActivity(intent);
573 }
574 }
575
576 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 public void onClick(View view) {
578 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700579 case R.id.deleteButton: {
580 keyPressed(KeyEvent.KEYCODE_DEL);
581 return;
582 }
583 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700584 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700585 placeCall();
586 return;
587 }
588 case R.id.digits: {
589 if (mDigits.length() != 0) {
590 mDigits.setCursorVisible(true);
591 }
592 return;
593 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800594 case R.id.floating_action_button_dialpad: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800595 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800596 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800597 switchView(mDialpadView, mEmergencyShortcutView, true);
598 return;
599 }
Yorke Lee23a70732014-08-14 17:12:01 -0700600 }
601 }
602
603 @Override
604 public void onPressed(View view, boolean pressed) {
605 if (!pressed) {
606 return;
607 }
608 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700609 case R.id.one: {
610 playTone(ToneGenerator.TONE_DTMF_1);
611 keyPressed(KeyEvent.KEYCODE_1);
612 return;
613 }
614 case R.id.two: {
615 playTone(ToneGenerator.TONE_DTMF_2);
616 keyPressed(KeyEvent.KEYCODE_2);
617 return;
618 }
619 case R.id.three: {
620 playTone(ToneGenerator.TONE_DTMF_3);
621 keyPressed(KeyEvent.KEYCODE_3);
622 return;
623 }
624 case R.id.four: {
625 playTone(ToneGenerator.TONE_DTMF_4);
626 keyPressed(KeyEvent.KEYCODE_4);
627 return;
628 }
629 case R.id.five: {
630 playTone(ToneGenerator.TONE_DTMF_5);
631 keyPressed(KeyEvent.KEYCODE_5);
632 return;
633 }
634 case R.id.six: {
635 playTone(ToneGenerator.TONE_DTMF_6);
636 keyPressed(KeyEvent.KEYCODE_6);
637 return;
638 }
639 case R.id.seven: {
640 playTone(ToneGenerator.TONE_DTMF_7);
641 keyPressed(KeyEvent.KEYCODE_7);
642 return;
643 }
644 case R.id.eight: {
645 playTone(ToneGenerator.TONE_DTMF_8);
646 keyPressed(KeyEvent.KEYCODE_8);
647 return;
648 }
649 case R.id.nine: {
650 playTone(ToneGenerator.TONE_DTMF_9);
651 keyPressed(KeyEvent.KEYCODE_9);
652 return;
653 }
654 case R.id.zero: {
655 playTone(ToneGenerator.TONE_DTMF_0);
656 keyPressed(KeyEvent.KEYCODE_0);
657 return;
658 }
659 case R.id.pound: {
660 playTone(ToneGenerator.TONE_DTMF_P);
661 keyPressed(KeyEvent.KEYCODE_POUND);
662 return;
663 }
664 case R.id.star: {
665 playTone(ToneGenerator.TONE_DTMF_S);
666 keyPressed(KeyEvent.KEYCODE_STAR);
667 return;
668 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 }
670 }
671
672 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 * called for long touch events
674 */
675 @Override
676 public boolean onLongClick(View view) {
677 int id = view.getId();
678 switch (id) {
679 case R.id.deleteButton: {
680 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700681 return true;
682 }
683 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700684 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700685 keyPressed(KeyEvent.KEYCODE_PLUS);
686 return true;
687 }
688 }
689 return false;
690 }
691
692 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700693 protected void onStart() {
694 super.onStart();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800695
696 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
697 mUserActions = MetricsWriter.USER_ACTION_NONE;
698 mMetricsWriter.writeMetricsForEnter();
699
Leo Hsu83665d82018-10-02 09:57:03 +0800700 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
701 updateTheme(false);
CY Cheng42873672018-06-15 21:20:10 +0800702
Leo Hsu83665d82018-10-02 09:57:03 +0800703 if (mEccInfoHelper != null) {
CY Cheng42873672018-06-15 21:20:10 +0800704 final Context context = this;
705 mEccInfoHelper.getCountryEccInfoAsync(context,
706 new EccInfoHelper.CountryEccInfoResultCallback() {
707 @Override
708 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
709 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
710 + Rlog.pii(LOG_TAG, iso));
711 updateLocationAndEccInfo(iso, countryEccInfo);
712 }
713
714 @Override
715 public void onDetectCountryFailed() {
716 Log.w(LOG_TAG, "Cannot detect current country.");
717 updateLocationAndEccInfo(null, null);
718 }
719
720 @Override
721 public void onRetrieveCountryEccInfoFailed(String iso) {
722 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
723 + Rlog.pii(LOG_TAG, iso));
724 updateLocationAndEccInfo(iso, null);
725 }
726 });
727 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700728 }
729
730 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700731 protected void onResume() {
732 super.onResume();
733
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800734 if (mProximitySensor != null) {
735 mSensorManager.registerListener(
736 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
737 }
738
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700739 // retrieve the DTMF tone play back setting.
740 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
741 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
742
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743 // if the mToneGenerator creation fails, just continue without it. It is
744 // a local audio signal, and is not as important as the dtmf tone itself.
745 synchronized (mToneGeneratorLock) {
746 if (mToneGenerator == null) {
747 try {
748 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
749 TONE_RELATIVE_VOLUME);
750 } catch (RuntimeException e) {
751 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
752 mToneGenerator = null;
753 }
754 }
755 }
756
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700757 updateDialAndDeleteButtonStateEnabledAttr();
758 }
759
760 @Override
761 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 super.onPause();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800763 if (mProximitySensor != null) {
764 mSensorManager.unregisterListener(this, mProximitySensor);
765 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 }
767
Lucas Dupineb9c5702017-05-10 16:57:09 -0700768 @Override
769 protected void onStop() {
770 super.onStop();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800771 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700772 mColorExtractor.removeOnColorsChangedListener(this);
773 }
774
775 /**
776 * Sets theme based on gradient colors
777 * @param supportsDarkText true if gradient supports dark text
778 */
779 private void updateTheme(boolean supportsDarkText) {
780 if (mSupportsDarkText == supportsDarkText) {
781 return;
782 }
783 mSupportsDarkText = supportsDarkText;
784
785 // We can't change themes after inflation, in this case we'll have to recreate
786 // the whole activity.
787 if (mBackgroundGradient != null) {
788 recreate();
789 return;
790 }
791
792 int vis = getWindow().getDecorView().getSystemUiVisibility();
793 if (supportsDarkText) {
794 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
795 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
796 setTheme(R.style.EmergencyDialerThemeDark);
797 } else {
798 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
799 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
800 setTheme(R.style.EmergencyDialerTheme);
801 }
802 getWindow().getDecorView().setSystemUiVisibility(vis);
803 }
804
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700805 /**
806 * place the call, but check to make sure it is a viable number.
807 */
808 private void placeCall() {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800809 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900811
812 // Convert into emergency number according to emergency conversion map.
813 // If conversion map is not defined (this is default), this method does
814 // nothing and just returns input number.
815 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
816
Yorke Lee36bb2542014-06-05 08:09:52 -0700817 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700818 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
819
820 // place the call if it is a valid number
821 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
822 // There is no number entered.
823 playTone(ToneGenerator.TONE_PROP_NACK);
824 return;
825 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800826
827 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
828 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
829
Shaotang Lif27c74d2018-07-19 17:32:42 +0800830 Bundle extras = new Bundle();
831 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
832 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000833 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800834 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 } else {
836 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
837
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800838 // We interest on the context when user has intention to make phone call,
839 // so write metrics here for non-emergency numbers even these numbers are rejected.
840 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
841 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
842
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700843 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
844 }
Yorke Lee9b341512014-10-17 11:36:41 -0700845 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846 }
847
848 /**
849 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
850 *
851 * The tone is played locally, using the audio stream for phone calls.
852 * Tones are played only if the "Audible touch tones" user preference
853 * is checked, and are NOT played if the device is in silent mode.
854 *
855 * @param tone a tone code from {@link ToneGenerator}
856 */
857 void playTone(int tone) {
858 // if local tone playback is disabled, just return.
859 if (!mDTMFToneEnabled) {
860 return;
861 }
862
863 // Also do nothing if the phone is in silent mode.
864 // We need to re-check the ringer mode for *every* playTone()
865 // call, rather than keeping a local flag that's updated in
866 // onResume(), since it's possible to toggle silent mode without
867 // leaving the current activity (via the ENDCALL-longpress menu.)
868 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
869 int ringerMode = audioManager.getRingerMode();
870 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
871 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
872 return;
873 }
874
875 synchronized (mToneGeneratorLock) {
876 if (mToneGenerator == null) {
877 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
878 return;
879 }
880
881 // Start the new tone (will stop any playing tone)
882 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
883 }
884 }
885
886 private CharSequence createErrorMessage(String number) {
887 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800888 String errorString = getString(R.string.dial_emergency_error, number);
889 int startingPosition = errorString.indexOf(number);
890 int endingPosition = startingPosition + number.length();
891 Spannable result = new SpannableString(errorString);
892 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
893 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700894 } else {
895 return getText(R.string.dial_emergency_empty_error).toString();
896 }
897 }
898
899 @Override
900 protected Dialog onCreateDialog(int id) {
901 AlertDialog dialog = null;
902 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
903 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700904 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700905 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
906 .setMessage(createErrorMessage(mLastNumber))
907 .setPositiveButton(R.string.ok, null)
908 .setCancelable(true).create();
909
910 // blur stuff behind the dialog
911 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700912 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 }
914 return dialog;
915 }
916
917 @Override
918 protected void onPrepareDialog(int id, Dialog dialog) {
919 super.onPrepareDialog(id, dialog);
920 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
921 AlertDialog alert = (AlertDialog) dialog;
922 alert.setMessage(createErrorMessage(mLastNumber));
923 }
924 }
925
Andrew Leed5631e82014-10-08 16:03:58 -0700926 @Override
927 public boolean onOptionsItemSelected(MenuItem item) {
928 final int itemId = item.getItemId();
929 if (itemId == android.R.id.home) {
930 onBackPressed();
931 return true;
932 }
933 return super.onOptionsItemSelected(item);
934 }
935
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 /**
937 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
938 */
939 private void updateDialAndDeleteButtonStateEnabledAttr() {
940 final boolean notEmpty = mDigits.length() != 0;
941
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 mDelete.setEnabled(notEmpty);
943 }
Yorke Lee91311662014-10-24 14:50:45 -0700944
945 /**
946 * Remove the digit just before the current position. Used by various long pressed callbacks
947 * to remove the digit that was populated as a result of the short click.
948 */
949 private void removePreviousDigitIfPossible() {
950 final int currentPosition = mDigits.getSelectionStart();
951 if (currentPosition > 0) {
952 mDigits.setSelection(currentPosition);
953 mDigits.getText().delete(currentPosition - 1, currentPosition);
954 }
955 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800956
957 /**
958 * Update the text-to-speech annotations in the edit field.
959 */
960 private void updateTtsSpans() {
961 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
962 mDigits.getText().removeSpan(o);
963 }
964 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
965 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700966
967 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700968 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700969 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700970 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
971 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700972 mBackgroundGradient.setColors(colors);
973 updateTheme(colors.supportsDarkText());
974 }
975 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700976
977 /**
978 * Where a carrier requires a warning that emergency calling is not available while on WFC,
979 * add hint text above the dial pad which warns the user of this case.
980 */
981 private void maybeShowWfcEmergencyCallingWarning() {
982 if (!mIsWfcEmergencyCallingWarningEnabled) {
983 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
984 return;
985 }
986
987 // Use an async task rather than calling into Telephony on UI thread.
988 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
989 @Override
990 protected Boolean doInBackground(Void... voids) {
991 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
992 boolean isWfcAvailable = tm.isWifiCallingAvailable();
993 ServiceState ss = tm.getServiceState();
994 boolean isCellAvailable =
995 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
996 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
997 + " isCellAvailable=" + isCellAvailable
998 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
999 return isWfcAvailable && !isCellAvailable;
1000 }
1001
1002 @Override
1003 protected void onPostExecute(Boolean result) {
1004 if (result.booleanValue()) {
1005 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1006 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1007 } else {
1008 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1009 mDigits.setHint("");
1010 }
1011 maybeChangeHintSize();
1012 }
1013 };
1014 showWfcWarningTask.execute((Void) null);
1015 }
1016
1017 /**
1018 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1019 * edit view and set the font size to a smaller size appropriate for the emergency calling
1020 * warning.
1021 */
1022 private void maybeChangeHintSize() {
1023 if (TextUtils.isEmpty(mDigits.getHint())
1024 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1025 // No hint or there are dialed digits, so use default size.
1026 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1027 // By default, the digits view auto-resizes to fit the text it contains, so
1028 // enable that now.
1029 mDigits.setResizeEnabled(true);
1030 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1031 } else {
1032 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1033 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1034 R.dimen.emergency_call_warning_size));
1035 // Since we're populating this with a static text string, disable auto-resize.
1036 mDigits.setResizeEnabled(false);
1037 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1038 }
1039 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001040
1041 private void setupEmergencyShortcutsView() {
1042 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1043 mDialpadView = findViewById(R.id.emergency_dialer);
1044
1045 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1046 dialpadButton.setOnClickListener(this);
1047
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001048 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001049
1050 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1051 mEmergencyActionGroup.setVisibility(View.GONE);
1052
1053 // Setup dialpad title.
1054 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1055 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1056
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001057 mEmergencyShortcutButtonList = new ArrayList<>();
1058 setupEmergencyCallShortcutButton();
1059
CY Cheng42873672018-06-15 21:20:10 +08001060 updateLocationAndEccInfo(null, null);
1061
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001062 switchView(mEmergencyShortcutView, mDialpadView, false);
1063 }
1064
CY Cheng42873672018-06-15 21:20:10 +08001065 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001066 final View locationInfo = findViewById(R.id.location_info);
1067
CY Cheng42873672018-06-15 21:20:10 +08001068 String countryName = null;
1069 if (!TextUtils.isEmpty(countryIso)) {
1070 Locale locale = Locale.getDefault();
1071 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1072 .getDisplayCountry();
1073 }
1074 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001075 locationInfo.setVisibility(View.INVISIBLE);
1076 } else {
1077 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +08001078 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001079 locationInfo.setVisibility(View.VISIBLE);
1080 }
1081 }
1082
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001083 private void setupEmergencyCallShortcutButton() {
1084 final ViewGroup shortcutButtonContainer = findViewById(
1085 R.id.emergency_shortcut_buttons_container);
1086 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +08001087 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001088
CY Cheng42873672018-06-15 21:20:10 +08001089 mShortcutAdapter = new EccShortcutAdapter(this) {
1090 @Override
1091 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1092 CharSequence description, int iconRes) {
1093 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1094 .inflate(R.layout.emergency_shortcut_button, parent, false);
1095 button.setPhoneNumber(number);
1096 button.setPhoneDescription(description);
1097 button.setPhoneTypeIcon(iconRes);
1098 button.setOnConfirmClickListener(EmergencyDialer.this);
1099 return button;
1100 }
1101 };
1102 mShortcutDataSetObserver = new DataSetObserver() {
1103 @Override
1104 public void onChanged() {
1105 super.onChanged();
1106 updateLayout();
1107 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001108
CY Cheng42873672018-06-15 21:20:10 +08001109 @Override
1110 public void onInvalidated() {
1111 super.onInvalidated();
1112 updateLayout();
1113 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001114
CY Cheng42873672018-06-15 21:20:10 +08001115 private void updateLayout() {
1116 // clear previous added buttons
1117 shortcutButtonContainer.removeAllViews();
1118 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001119
CY Cheng42873672018-06-15 21:20:10 +08001120 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1121 EmergencyShortcutButton button = (EmergencyShortcutButton)
1122 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1123 mEmergencyShortcutButtonList.add(button);
1124 shortcutButtonContainer.addView(button);
1125 }
1126
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001127 // Update emergency numbers title for numerous buttons.
CY Cheng42873672018-06-15 21:20:10 +08001128 if (mEmergencyShortcutButtonList.size() > 1) {
1129 emergencyNumberTitle.setText(getString(
1130 R.string.numerous_emergency_numbers_title));
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001131 // Update mEmergencyInfoGroup margin to avoid UI overlay when
1132 // emergency shortcut button more than 2.
1133 if (mEmergencyShortcutButtonList.size() > 2) {
1134 mEmergencyInfoGroup.updateLayoutMargin();
1135 }
CY Cheng42873672018-06-15 21:20:10 +08001136 } else {
1137 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1138 }
1139 }
1140 };
1141 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1142 }
1143
1144 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1145 if (!isFinishing() && !isDestroyed()) {
1146 setLocationInfo(iso);
1147 if (mShortcutAdapter != null) {
1148 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1149 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001150 }
1151 }
1152
1153 /**
1154 * Called by the activity before a touch event is dispatched to the view hierarchy.
1155 */
1156 private void onPreTouchEvent(MotionEvent event) {
1157 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001158 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001159
1160 if (mEmergencyShortcutButtonList != null) {
1161 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1162 button.onPreTouchEvent(event);
1163 }
1164 }
1165 }
1166
1167 /**
1168 * Called by the activity after a touch event is dispatched to the view hierarchy.
1169 */
1170 private void onPostTouchEvent(MotionEvent event) {
1171 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001172 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001173
1174 if (mEmergencyShortcutButtonList != null) {
1175 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1176 button.onPostTouchEvent(event);
1177 }
1178 }
1179 }
1180
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001181 /**
1182 * Switch two view.
1183 *
1184 * @param displayView the view would be displayed.
1185 * @param hideView the view would be hidden.
1186 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1187 */
1188 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1189 if (displayView == null || hideView == null) {
1190 return;
1191 }
1192
1193 if (displayView.getVisibility() == View.VISIBLE) {
1194 return;
1195 }
1196
1197 if (hasAnimation) {
1198 crossfade(hideView, displayView);
1199 } else {
1200 hideView.setVisibility(View.GONE);
1201 displayView.setVisibility(View.VISIBLE);
1202 }
1203 }
1204
1205 /**
1206 * Fade out and fade in animation between two view transition.
1207 */
1208 private void crossfade(View fadeOutView, View fadeInView) {
1209 if (fadeOutView == null || fadeInView == null) {
1210 return;
1211 }
1212 final int shortAnimationDuration = getResources().getInteger(
1213 android.R.integer.config_shortAnimTime);
1214
1215 fadeInView.setAlpha(0f);
1216 fadeInView.setVisibility(View.VISIBLE);
1217
1218 fadeInView.animate()
1219 .alpha(1f)
1220 .setDuration(shortAnimationDuration)
1221 .setListener(null);
1222
1223 fadeOutView.animate()
1224 .alpha(0f)
1225 .setDuration(shortAnimationDuration)
1226 .setListener(new AnimatorListenerAdapter() {
1227 @Override
1228 public void onAnimationEnd(Animator animation) {
1229 fadeOutView.setVisibility(View.GONE);
1230 }
1231 });
1232 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +08001233
1234 @Override
1235 public void onSensorChanged(SensorEvent event) {
1236 float distance = event.values[0];
1237 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1238 }
1239
1240 @Override
1241 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1242 // Not used.
1243 }
1244
1245 private boolean isShortcutNumber(String number) {
1246 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1247 return false;
1248 }
1249
1250 boolean isShortcut = false;
1251 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1252 if (button != null && number.equals(button.getPhoneNumber())) {
1253 isShortcut = true;
1254 break;
1255 }
1256 }
1257 return isShortcut;
1258 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001259}