blob: 11da00344a75a493186ee2e2ccc51763da20e348 [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
Lucas Dupineb9c5702017-05-10 16:57:09 -0700215 /** 90% opacity, different from other gradients **/
216 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
217
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800218 /** 85% opacity for black background **/
219 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
220
CY Cheng42873672018-06-15 21:20:10 +0800221 /** Size limit of emergency shortcut buttons container. **/
222 private static final int SHORTCUT_SIZE_LIMIT = 3;
223
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700224 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 private View mDialButton;
226 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800227 private View mEmergencyShortcutView;
228 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229
CY Cheng42873672018-06-15 21:20:10 +0800230 private EccInfoHelper mEccInfoHelper;
231
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800232 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800233 private EccShortcutAdapter mShortcutAdapter;
234 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800235
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700236 private ToneGenerator mToneGenerator;
237 private Object mToneGeneratorLock = new Object();
238
239 // determines if we want to playback local DTMF tones.
240 private boolean mDTMFToneEnabled;
241
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700242 private EmergencyActionGroup mEmergencyActionGroup;
243
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 // close activity when screen turns off
245 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
246 @Override
247 public void onReceive(Context context, Intent intent) {
248 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700249 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 }
251 }
252 };
253
254 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
255
Lucas Dupineb9c5702017-05-10 16:57:09 -0700256 // Background gradient
257 private ColorExtractor mColorExtractor;
258 private GradientDrawable mBackgroundGradient;
259 private boolean mSupportsDarkText;
260
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700261 private boolean mIsWfcEmergencyCallingWarningEnabled;
262 private float mDefaultDigitsTextSize;
263
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800264 private boolean mAreEmergencyDialerShortcutsEnabled;
265
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800266 private MetricsWriter mMetricsWriter;
267 private SensorManager mSensorManager;
268 private Sensor mProximitySensor;
269 private boolean mIsProximityNear = false;
270
271 /**
272 * The time, in millis, since boot when user opened emergency dialer.
273 * This is used when calculating the user stay duration for metrics data.
274 */
275 private long mUserEnterTimeMillis = 0;
276
277 /**
278 * Bit flag indicating the actions performed by user. This is used for metrics data.
279 */
280 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
281
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282 @Override
283 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
284 // Do nothing
285 }
286
287 @Override
288 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700289 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 }
291
292 @Override
293 public void afterTextChanged(Editable input) {
294 // Check for special sequences, in particular the "**04" or "**05"
295 // sequences that allow you to enter PIN or PUK-related codes.
296 //
297 // But note we *don't* allow most other special sequences here,
298 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
299 // since those shouldn't be available if the device is locked.
300 //
301 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
302 // here, not the regular handleChars() method.
303 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
304 // A special sequence was entered, clear the digits
305 mDigits.getText().clear();
306 }
307
308 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800309 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 }
311
312 @Override
313 protected void onCreate(Bundle icicle) {
314 super.onCreate(icicle);
315
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800316 mMetricsWriter = new MetricsWriter();
317 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
318 if (mSensorManager != null) {
319 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
320 }
321
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 // Allow this activity to be displayed in front of the keyguard / lockscreen.
323 WindowManager.LayoutParams lp = getWindow().getAttributes();
324 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700325
326 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700327 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700328 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
329
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 getWindow().setAttributes(lp);
331
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800332 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
333 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng42873672018-06-15 21:20:10 +0800334 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
335 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800336
Lucas Dupineb9c5702017-05-10 16:57:09 -0700337 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800338
339 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
340 // And the background color is black with 85% opacity.
341 if (mAreEmergencyDialerShortcutsEnabled) {
342 updateTheme(false);
343 } else {
344 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
345 ColorExtractor.TYPE_EXTRA_DARK);
346 updateTheme(lockScreenColors.supportsDarkText());
347 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700348
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700349 setContentView(R.layout.emergency_dialer);
350
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700351 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 mDigits.setKeyListener(DialerKeyListener.getInstance());
353 mDigits.setOnClickListener(this);
354 mDigits.setOnKeyListener(this);
355 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700356 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700357 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700358 maybeAddNumberFormatting();
359
Lucas Dupineb9c5702017-05-10 16:57:09 -0700360 mBackgroundGradient = new GradientDrawable(this);
361 Point displaySize = new Point();
362 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
363 .getDefaultDisplay().getSize(displaySize);
364 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800365 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
366 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700367 getWindow().setBackgroundDrawable(mBackgroundGradient);
368
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700369 // Check for the presence of the keypad
370 View view = findViewById(R.id.one);
371 if (view != null) {
372 setupKeypad();
373 }
374
375 mDelete = findViewById(R.id.deleteButton);
376 mDelete.setOnClickListener(this);
377 mDelete.setOnLongClickListener(this);
378
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700379 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380
381 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700382 // Read carrier config through the public API because PhoneGlobals is not available when we
383 // run as a secondary user.
384 CarrierConfigManager configMgr =
385 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
386 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800387 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700388
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700389 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700390 mDialButton.setOnClickListener(this);
391 } else {
392 mDialButton.setVisibility(View.GONE);
393 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700394 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
395 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
396 maybeShowWfcEmergencyCallingWarning();
397
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700398 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700399
400 if (icicle != null) {
401 super.onRestoreInstanceState(icicle);
402 }
403
404 // Extract phone number from intent
405 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700406 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700407 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
408 if (number != null) {
409 mDigits.setText(number);
410 }
411 }
412
413 // if the mToneGenerator creation fails, just continue without it. It is
414 // a local audio signal, and is not as important as the dtmf tone itself.
415 synchronized (mToneGeneratorLock) {
416 if (mToneGenerator == null) {
417 try {
418 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
419 } catch (RuntimeException e) {
420 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
421 mToneGenerator = null;
422 }
423 }
424 }
425
426 final IntentFilter intentFilter = new IntentFilter();
427 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
428 registerReceiver(mBroadcastReceiver, intentFilter);
429
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700430 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800431
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800432 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu8f1c6712018-07-10 11:37:52 +0800433 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800434 setupEmergencyShortcutsView();
435 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700436 }
437
438 @Override
439 protected void onDestroy() {
440 super.onDestroy();
441 synchronized (mToneGeneratorLock) {
442 if (mToneGenerator != null) {
443 mToneGenerator.release();
444 mToneGenerator = null;
445 }
446 }
447 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800448 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
449 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
450 mShortcutDataSetObserver = null;
451 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 }
453
454 @Override
455 protected void onRestoreInstanceState(Bundle icicle) {
456 mLastNumber = icicle.getString(LAST_NUMBER);
457 }
458
459 @Override
460 protected void onSaveInstanceState(Bundle outState) {
461 super.onSaveInstanceState(outState);
462 outState.putString(LAST_NUMBER, mLastNumber);
463 }
464
465 /**
466 * Explicitly turn off number formatting, since it gets in the way of the emergency
467 * number detector
468 */
469 protected void maybeAddNumberFormatting() {
470 // Do nothing.
471 }
472
473 @Override
474 protected void onPostCreate(Bundle savedInstanceState) {
475 super.onPostCreate(savedInstanceState);
476
477 // This can't be done in onCreate(), since the auto-restoring of the digits
478 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
479 // is called. This method will be called every time the activity is created, and
480 // will always happen after onRestoreSavedInstanceState().
481 mDigits.addTextChangedListener(this);
482 }
483
484 private void setupKeypad() {
485 // Setup the listeners for the buttons
486 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700487 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
488 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700489 }
490
491 View view = findViewById(R.id.zero);
492 view.setOnLongClickListener(this);
493 }
494
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800495 @Override
496 public void onBackPressed() {
497 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800498 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800499 // Otherwise, it would finish the activity.
500 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
501 && mDialpadView.getVisibility() == View.VISIBLE) {
502 switchView(mEmergencyShortcutView, mDialpadView, true);
503 return;
504 }
505 super.onBackPressed();
506 }
507
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508 /**
509 * handle key events
510 */
511 @Override
512 public boolean onKeyDown(int keyCode, KeyEvent event) {
513 switch (keyCode) {
514 // Happen when there's a "Call" hard button.
515 case KeyEvent.KEYCODE_CALL: {
516 if (TextUtils.isEmpty(mDigits.getText().toString())) {
517 // if we are adding a call from the InCallScreen and the phone
518 // number entered is empty, we just close the dialer to expose
519 // the InCallScreen under it.
520 finish();
521 } else {
522 // otherwise, we place the call.
523 placeCall();
524 }
525 return true;
526 }
527 }
528 return super.onKeyDown(keyCode, event);
529 }
530
531 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700532 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
534 mDigits.onKeyDown(keyCode, event);
535 }
536
537 @Override
538 public boolean onKey(View view, int keyCode, KeyEvent event) {
539 switch (view.getId()) {
540 case R.id.digits:
541 // Happen when "Done" button of the IME is pressed. This can happen when this
542 // Activity is forced into landscape mode due to a desk dock.
543 if (keyCode == KeyEvent.KEYCODE_ENTER
544 && event.getAction() == KeyEvent.ACTION_UP) {
545 placeCall();
546 return true;
547 }
548 break;
549 }
550 return false;
551 }
552
553 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700554 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800555 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700556 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800557 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700558 return handled;
559 }
560
561 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800562 public void onConfirmClick(EmergencyShortcutButton button) {
563 if (button == null) return;
564
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800565 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
566
567 // We interest on the context when user has intention to make phone call,
568 // so write metrics here for shortcut number even the call may not be created.
569 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
570 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
571
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800572 String phoneNumber = button.getPhoneNumber();
573
574 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800575 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Lif27c74d2018-07-19 17:32:42 +0800576 Bundle extras = new Bundle();
577 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
578 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800579 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800580 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800581 } else {
582 Log.d(LOG_TAG, "emergency number is empty");
583 }
584 }
585
586 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 public void onClick(View view) {
588 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700589 case R.id.deleteButton: {
590 keyPressed(KeyEvent.KEYCODE_DEL);
591 return;
592 }
593 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700594 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700595 placeCall();
596 return;
597 }
598 case R.id.digits: {
599 if (mDigits.length() != 0) {
600 mDigits.setCursorVisible(true);
601 }
602 return;
603 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800604 case R.id.floating_action_button_dialpad: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800605 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800606 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800607 switchView(mDialpadView, mEmergencyShortcutView, true);
608 return;
609 }
610 case R.id.emergency_info_button: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800611 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800612 Intent intent = (Intent) view.getTag(R.id.tag_intent);
613 if (intent != null) {
614 startActivity(intent);
615 }
616 return;
617 }
Yorke Lee23a70732014-08-14 17:12:01 -0700618 }
619 }
620
621 @Override
622 public void onPressed(View view, boolean pressed) {
623 if (!pressed) {
624 return;
625 }
626 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 case R.id.one: {
628 playTone(ToneGenerator.TONE_DTMF_1);
629 keyPressed(KeyEvent.KEYCODE_1);
630 return;
631 }
632 case R.id.two: {
633 playTone(ToneGenerator.TONE_DTMF_2);
634 keyPressed(KeyEvent.KEYCODE_2);
635 return;
636 }
637 case R.id.three: {
638 playTone(ToneGenerator.TONE_DTMF_3);
639 keyPressed(KeyEvent.KEYCODE_3);
640 return;
641 }
642 case R.id.four: {
643 playTone(ToneGenerator.TONE_DTMF_4);
644 keyPressed(KeyEvent.KEYCODE_4);
645 return;
646 }
647 case R.id.five: {
648 playTone(ToneGenerator.TONE_DTMF_5);
649 keyPressed(KeyEvent.KEYCODE_5);
650 return;
651 }
652 case R.id.six: {
653 playTone(ToneGenerator.TONE_DTMF_6);
654 keyPressed(KeyEvent.KEYCODE_6);
655 return;
656 }
657 case R.id.seven: {
658 playTone(ToneGenerator.TONE_DTMF_7);
659 keyPressed(KeyEvent.KEYCODE_7);
660 return;
661 }
662 case R.id.eight: {
663 playTone(ToneGenerator.TONE_DTMF_8);
664 keyPressed(KeyEvent.KEYCODE_8);
665 return;
666 }
667 case R.id.nine: {
668 playTone(ToneGenerator.TONE_DTMF_9);
669 keyPressed(KeyEvent.KEYCODE_9);
670 return;
671 }
672 case R.id.zero: {
673 playTone(ToneGenerator.TONE_DTMF_0);
674 keyPressed(KeyEvent.KEYCODE_0);
675 return;
676 }
677 case R.id.pound: {
678 playTone(ToneGenerator.TONE_DTMF_P);
679 keyPressed(KeyEvent.KEYCODE_POUND);
680 return;
681 }
682 case R.id.star: {
683 playTone(ToneGenerator.TONE_DTMF_S);
684 keyPressed(KeyEvent.KEYCODE_STAR);
685 return;
686 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700687 }
688 }
689
690 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 * called for long touch events
692 */
693 @Override
694 public boolean onLongClick(View view) {
695 int id = view.getId();
696 switch (id) {
697 case R.id.deleteButton: {
698 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700699 return true;
700 }
701 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700702 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700703 keyPressed(KeyEvent.KEYCODE_PLUS);
704 return true;
705 }
706 }
707 return false;
708 }
709
710 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700711 protected void onStart() {
712 super.onStart();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800713
714 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
715 mUserActions = MetricsWriter.USER_ACTION_NONE;
716 mMetricsWriter.writeMetricsForEnter();
717
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800718 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
719 // And set background color to black.
720 if (mAreEmergencyDialerShortcutsEnabled) {
721 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
722 updateTheme(false);
723 } else {
724 mColorExtractor.addOnColorsChangedListener(this);
725 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
726 ColorExtractor.TYPE_EXTRA_DARK);
727 // Do not animate when view isn't visible yet, just set an initial state.
728 mBackgroundGradient.setColors(lockScreenColors, false);
729 updateTheme(lockScreenColors.supportsDarkText());
730 }
CY Cheng42873672018-06-15 21:20:10 +0800731
732 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
733 final Context context = this;
734 mEccInfoHelper.getCountryEccInfoAsync(context,
735 new EccInfoHelper.CountryEccInfoResultCallback() {
736 @Override
737 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
738 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
739 + Rlog.pii(LOG_TAG, iso));
740 updateLocationAndEccInfo(iso, countryEccInfo);
741 }
742
743 @Override
744 public void onDetectCountryFailed() {
745 Log.w(LOG_TAG, "Cannot detect current country.");
746 updateLocationAndEccInfo(null, null);
747 }
748
749 @Override
750 public void onRetrieveCountryEccInfoFailed(String iso) {
751 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
752 + Rlog.pii(LOG_TAG, iso));
753 updateLocationAndEccInfo(iso, null);
754 }
755 });
756 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700757 }
758
759 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 protected void onResume() {
761 super.onResume();
762
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800763 if (mProximitySensor != null) {
764 mSensorManager.registerListener(
765 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
766 }
767
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 // retrieve the DTMF tone play back setting.
769 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
770 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
771
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772 // if the mToneGenerator creation fails, just continue without it. It is
773 // a local audio signal, and is not as important as the dtmf tone itself.
774 synchronized (mToneGeneratorLock) {
775 if (mToneGenerator == null) {
776 try {
777 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
778 TONE_RELATIVE_VOLUME);
779 } catch (RuntimeException e) {
780 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
781 mToneGenerator = null;
782 }
783 }
784 }
785
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 updateDialAndDeleteButtonStateEnabledAttr();
787 }
788
789 @Override
790 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 super.onPause();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800792 if (mProximitySensor != null) {
793 mSensorManager.unregisterListener(this, mProximitySensor);
794 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 }
796
Lucas Dupineb9c5702017-05-10 16:57:09 -0700797 @Override
798 protected void onStop() {
799 super.onStop();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800800 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700801 mColorExtractor.removeOnColorsChangedListener(this);
802 }
803
804 /**
805 * Sets theme based on gradient colors
806 * @param supportsDarkText true if gradient supports dark text
807 */
808 private void updateTheme(boolean supportsDarkText) {
809 if (mSupportsDarkText == supportsDarkText) {
810 return;
811 }
812 mSupportsDarkText = supportsDarkText;
813
814 // We can't change themes after inflation, in this case we'll have to recreate
815 // the whole activity.
816 if (mBackgroundGradient != null) {
817 recreate();
818 return;
819 }
820
821 int vis = getWindow().getDecorView().getSystemUiVisibility();
822 if (supportsDarkText) {
823 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
824 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
825 setTheme(R.style.EmergencyDialerThemeDark);
826 } else {
827 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
828 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
829 setTheme(R.style.EmergencyDialerTheme);
830 }
831 getWindow().getDecorView().setSystemUiVisibility(vis);
832 }
833
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 /**
835 * place the call, but check to make sure it is a viable number.
836 */
837 private void placeCall() {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800838 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900840
841 // Convert into emergency number according to emergency conversion map.
842 // If conversion map is not defined (this is default), this method does
843 // nothing and just returns input number.
844 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
845
Yorke Lee36bb2542014-06-05 08:09:52 -0700846 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700847 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
848
849 // place the call if it is a valid number
850 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
851 // There is no number entered.
852 playTone(ToneGenerator.TONE_PROP_NACK);
853 return;
854 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800855
856 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
857 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
858
Shaotang Lif27c74d2018-07-19 17:32:42 +0800859 Bundle extras = new Bundle();
860 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
861 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000862 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800863 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 } else {
865 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
866
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800867 // We interest on the context when user has intention to make phone call,
868 // so write metrics here for non-emergency numbers even these numbers are rejected.
869 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
870 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
871
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
873 }
Yorke Lee9b341512014-10-17 11:36:41 -0700874 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 }
876
877 /**
878 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
879 *
880 * The tone is played locally, using the audio stream for phone calls.
881 * Tones are played only if the "Audible touch tones" user preference
882 * is checked, and are NOT played if the device is in silent mode.
883 *
884 * @param tone a tone code from {@link ToneGenerator}
885 */
886 void playTone(int tone) {
887 // if local tone playback is disabled, just return.
888 if (!mDTMFToneEnabled) {
889 return;
890 }
891
892 // Also do nothing if the phone is in silent mode.
893 // We need to re-check the ringer mode for *every* playTone()
894 // call, rather than keeping a local flag that's updated in
895 // onResume(), since it's possible to toggle silent mode without
896 // leaving the current activity (via the ENDCALL-longpress menu.)
897 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
898 int ringerMode = audioManager.getRingerMode();
899 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
900 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
901 return;
902 }
903
904 synchronized (mToneGeneratorLock) {
905 if (mToneGenerator == null) {
906 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
907 return;
908 }
909
910 // Start the new tone (will stop any playing tone)
911 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
912 }
913 }
914
915 private CharSequence createErrorMessage(String number) {
916 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800917 String errorString = getString(R.string.dial_emergency_error, number);
918 int startingPosition = errorString.indexOf(number);
919 int endingPosition = startingPosition + number.length();
920 Spannable result = new SpannableString(errorString);
921 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
922 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700923 } else {
924 return getText(R.string.dial_emergency_empty_error).toString();
925 }
926 }
927
928 @Override
929 protected Dialog onCreateDialog(int id) {
930 AlertDialog dialog = null;
931 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
932 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700933 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
935 .setMessage(createErrorMessage(mLastNumber))
936 .setPositiveButton(R.string.ok, null)
937 .setCancelable(true).create();
938
939 // blur stuff behind the dialog
940 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700941 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 }
943 return dialog;
944 }
945
946 @Override
947 protected void onPrepareDialog(int id, Dialog dialog) {
948 super.onPrepareDialog(id, dialog);
949 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
950 AlertDialog alert = (AlertDialog) dialog;
951 alert.setMessage(createErrorMessage(mLastNumber));
952 }
953 }
954
Andrew Leed5631e82014-10-08 16:03:58 -0700955 @Override
956 public boolean onOptionsItemSelected(MenuItem item) {
957 final int itemId = item.getItemId();
958 if (itemId == android.R.id.home) {
959 onBackPressed();
960 return true;
961 }
962 return super.onOptionsItemSelected(item);
963 }
964
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 /**
966 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
967 */
968 private void updateDialAndDeleteButtonStateEnabledAttr() {
969 final boolean notEmpty = mDigits.length() != 0;
970
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 mDelete.setEnabled(notEmpty);
972 }
Yorke Lee91311662014-10-24 14:50:45 -0700973
974 /**
975 * Remove the digit just before the current position. Used by various long pressed callbacks
976 * to remove the digit that was populated as a result of the short click.
977 */
978 private void removePreviousDigitIfPossible() {
979 final int currentPosition = mDigits.getSelectionStart();
980 if (currentPosition > 0) {
981 mDigits.setSelection(currentPosition);
982 mDigits.getText().delete(currentPosition - 1, currentPosition);
983 }
984 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800985
986 /**
987 * Update the text-to-speech annotations in the edit field.
988 */
989 private void updateTtsSpans() {
990 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
991 mDigits.getText().removeSpan(o);
992 }
993 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
994 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700995
996 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700997 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700998 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700999 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1000 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001001 mBackgroundGradient.setColors(colors);
1002 updateTheme(colors.supportsDarkText());
1003 }
1004 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -07001005
1006 /**
1007 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1008 * add hint text above the dial pad which warns the user of this case.
1009 */
1010 private void maybeShowWfcEmergencyCallingWarning() {
1011 if (!mIsWfcEmergencyCallingWarningEnabled) {
1012 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1013 return;
1014 }
1015
1016 // Use an async task rather than calling into Telephony on UI thread.
1017 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1018 @Override
1019 protected Boolean doInBackground(Void... voids) {
1020 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1021 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1022 ServiceState ss = tm.getServiceState();
1023 boolean isCellAvailable =
1024 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1025 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
1026 + " isCellAvailable=" + isCellAvailable
1027 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
1028 return isWfcAvailable && !isCellAvailable;
1029 }
1030
1031 @Override
1032 protected void onPostExecute(Boolean result) {
1033 if (result.booleanValue()) {
1034 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1035 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1036 } else {
1037 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1038 mDigits.setHint("");
1039 }
1040 maybeChangeHintSize();
1041 }
1042 };
1043 showWfcWarningTask.execute((Void) null);
1044 }
1045
1046 /**
1047 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1048 * edit view and set the font size to a smaller size appropriate for the emergency calling
1049 * warning.
1050 */
1051 private void maybeChangeHintSize() {
1052 if (TextUtils.isEmpty(mDigits.getHint())
1053 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1054 // No hint or there are dialed digits, so use default size.
1055 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1056 // By default, the digits view auto-resizes to fit the text it contains, so
1057 // enable that now.
1058 mDigits.setResizeEnabled(true);
1059 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1060 } else {
1061 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1062 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1063 R.dimen.emergency_call_warning_size));
1064 // Since we're populating this with a static text string, disable auto-resize.
1065 mDigits.setResizeEnabled(false);
1066 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1067 }
1068 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001069
1070 private void setupEmergencyShortcutsView() {
1071 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1072 mDialpadView = findViewById(R.id.emergency_dialer);
1073
1074 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1075 dialpadButton.setOnClickListener(this);
1076
1077 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
1078 emergencyInfoButton.setOnClickListener(this);
1079
1080 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1081 mEmergencyActionGroup.setVisibility(View.GONE);
1082
1083 // Setup dialpad title.
1084 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1085 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1086
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001087 mEmergencyShortcutButtonList = new ArrayList<>();
1088 setupEmergencyCallShortcutButton();
1089
CY Cheng42873672018-06-15 21:20:10 +08001090 updateLocationAndEccInfo(null, null);
1091
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001092 switchView(mEmergencyShortcutView, mDialpadView, false);
1093 }
1094
CY Cheng42873672018-06-15 21:20:10 +08001095 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001096 final View locationInfo = findViewById(R.id.location_info);
1097
CY Cheng42873672018-06-15 21:20:10 +08001098 String countryName = null;
1099 if (!TextUtils.isEmpty(countryIso)) {
1100 Locale locale = Locale.getDefault();
1101 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1102 .getDisplayCountry();
1103 }
1104 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001105 locationInfo.setVisibility(View.INVISIBLE);
1106 } else {
1107 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +08001108 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001109 locationInfo.setVisibility(View.VISIBLE);
1110 }
1111 }
1112
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001113 private void setupEmergencyCallShortcutButton() {
1114 final ViewGroup shortcutButtonContainer = findViewById(
1115 R.id.emergency_shortcut_buttons_container);
1116 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +08001117 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001118
CY Cheng42873672018-06-15 21:20:10 +08001119 mShortcutAdapter = new EccShortcutAdapter(this) {
1120 @Override
1121 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1122 CharSequence description, int iconRes) {
1123 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1124 .inflate(R.layout.emergency_shortcut_button, parent, false);
1125 button.setPhoneNumber(number);
1126 button.setPhoneDescription(description);
1127 button.setPhoneTypeIcon(iconRes);
1128 button.setOnConfirmClickListener(EmergencyDialer.this);
1129 return button;
1130 }
1131 };
1132 mShortcutDataSetObserver = new DataSetObserver() {
1133 @Override
1134 public void onChanged() {
1135 super.onChanged();
1136 updateLayout();
1137 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001138
CY Cheng42873672018-06-15 21:20:10 +08001139 @Override
1140 public void onInvalidated() {
1141 super.onInvalidated();
1142 updateLayout();
1143 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001144
CY Cheng42873672018-06-15 21:20:10 +08001145 private void updateLayout() {
1146 // clear previous added buttons
1147 shortcutButtonContainer.removeAllViews();
1148 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001149
CY Cheng42873672018-06-15 21:20:10 +08001150 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1151 EmergencyShortcutButton button = (EmergencyShortcutButton)
1152 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1153 mEmergencyShortcutButtonList.add(button);
1154 shortcutButtonContainer.addView(button);
1155 }
1156
1157 // update emergency numbers title for numerous buttons.
1158 if (mEmergencyShortcutButtonList.size() > 1) {
1159 emergencyNumberTitle.setText(getString(
1160 R.string.numerous_emergency_numbers_title));
1161 } else {
1162 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1163 }
1164 }
1165 };
1166 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1167 }
1168
1169 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1170 if (!isFinishing() && !isDestroyed()) {
1171 setLocationInfo(iso);
1172 if (mShortcutAdapter != null) {
1173 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1174 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001175 }
1176 }
1177
1178 /**
1179 * Called by the activity before a touch event is dispatched to the view hierarchy.
1180 */
1181 private void onPreTouchEvent(MotionEvent event) {
1182 mEmergencyActionGroup.onPreTouchEvent(event);
1183
1184 if (mEmergencyShortcutButtonList != null) {
1185 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1186 button.onPreTouchEvent(event);
1187 }
1188 }
1189 }
1190
1191 /**
1192 * Called by the activity after a touch event is dispatched to the view hierarchy.
1193 */
1194 private void onPostTouchEvent(MotionEvent event) {
1195 mEmergencyActionGroup.onPostTouchEvent(event);
1196
1197 if (mEmergencyShortcutButtonList != null) {
1198 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1199 button.onPostTouchEvent(event);
1200 }
1201 }
1202 }
1203
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001204 /**
1205 * Switch two view.
1206 *
1207 * @param displayView the view would be displayed.
1208 * @param hideView the view would be hidden.
1209 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1210 */
1211 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1212 if (displayView == null || hideView == null) {
1213 return;
1214 }
1215
1216 if (displayView.getVisibility() == View.VISIBLE) {
1217 return;
1218 }
1219
1220 if (hasAnimation) {
1221 crossfade(hideView, displayView);
1222 } else {
1223 hideView.setVisibility(View.GONE);
1224 displayView.setVisibility(View.VISIBLE);
1225 }
1226 }
1227
1228 /**
1229 * Fade out and fade in animation between two view transition.
1230 */
1231 private void crossfade(View fadeOutView, View fadeInView) {
1232 if (fadeOutView == null || fadeInView == null) {
1233 return;
1234 }
1235 final int shortAnimationDuration = getResources().getInteger(
1236 android.R.integer.config_shortAnimTime);
1237
1238 fadeInView.setAlpha(0f);
1239 fadeInView.setVisibility(View.VISIBLE);
1240
1241 fadeInView.animate()
1242 .alpha(1f)
1243 .setDuration(shortAnimationDuration)
1244 .setListener(null);
1245
1246 fadeOutView.animate()
1247 .alpha(0f)
1248 .setDuration(shortAnimationDuration)
1249 .setListener(new AnimatorListenerAdapter() {
1250 @Override
1251 public void onAnimationEnd(Animator animation) {
1252 fadeOutView.setVisibility(View.GONE);
1253 }
1254 });
1255 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +08001256
1257 @Override
1258 public void onSensorChanged(SensorEvent event) {
1259 float distance = event.values[0];
1260 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1261 }
1262
1263 @Override
1264 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1265 // Not used.
1266 }
1267
1268 private boolean isShortcutNumber(String number) {
1269 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1270 return false;
1271 }
1272
1273 boolean isShortcut = false;
1274 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1275 if (button != null && number.equals(button.getPhoneNumber())) {
1276 isShortcut = true;
1277 break;
1278 }
1279 }
1280 return isShortcut;
1281 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282}