blob: 65edcf9fd2560a5402de117a4b900753c867cc2b [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Shaotang Li8662a912018-07-04 16:53:01 +080026import android.app.KeyguardManager;
Lucas Dupineb9c5702017-05-10 16:57:09 -070027import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080032import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080033import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Shaotang Li8662a912018-07-04 16:53:01 +080035import android.hardware.Sensor;
36import android.hardware.SensorEvent;
37import android.hardware.SensorEventListener;
38import android.hardware.SensorManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.media.AudioManager;
40import android.media.ToneGenerator;
Shaotang Li8662a912018-07-04 16:53:01 +080041import android.metrics.LogMaker;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070043import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070045import android.os.PersistableBundle;
Shaotang Li8662a912018-07-04 16:53:01 +080046import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.provider.Settings;
Shaotang Li5d906c12018-07-19 17:32:42 +080048import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070049import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070050import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080053import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070054import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070055import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070056import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070058import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080059import android.text.Spannable;
60import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
62import android.text.TextWatcher;
63import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080064import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070066import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070067import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070069import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070070import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080072import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080073import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080075import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080076import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Lucas Dupinaf9e9912017-06-22 12:39:39 -070078import com.android.internal.colorextraction.ColorExtractor;
79import com.android.internal.colorextraction.ColorExtractor.GradientColors;
80import com.android.internal.colorextraction.drawable.GradientDrawable;
Shaotang Li8662a912018-07-04 16:53:01 +080081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Yorke Lee23a70732014-08-14 17:12:01 -070083import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070084import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070085import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086
Chihhang Chuang92cfe512018-06-07 16:25:27 +080087import java.util.ArrayList;
88import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080089import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080090
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091/**
92 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
93 *
94 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
95 * activity from apps/Contacts) that:
Leo Hsu83ab2332018-12-18 15:20:45 +080096 * 1. Allows ONLY emergency calls to be dialed
97 * 2. Disallows voicemail functionality
98 * 3. Allows this activity to stay in front of the keyguard.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099 *
100 * TODO: Even though this is an ultra-simplified version of the normal
101 * dialer, there's still lots of code duplication between this class and
102 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
103 * moved into a shared base class that would live in the framework?
104 * Or could we figure out some way to move *this* class into apps/Contacts
105 * also?
106 */
107public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700108 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800109 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800110 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener,
111 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-07-04 16:53:01 +0800112
113 private class MetricsWriter {
114 // Metrics constants indicating the entry type that user opened emergency dialer.
115 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
116 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
117 public static final int ENTRY_TYPE_UNKNOWN = 0;
118 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
119 public static final int ENTRY_TYPE_POWER_MENU = 2;
120
121 // Metrics constants indicating the UI that user made phone call.
122 public static final int CALL_SOURCE_DIALPAD = 0;
123 public static final int CALL_SOURCE_SHORTCUT = 1;
124
125 // Metrics constants indicating the phone number type of a call user made.
126 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
127 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
128
129 // Metrics constants indicating the actions performed by user.
130 public static final int USER_ACTION_NONE = 0x0;
131 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
132 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
133 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
134 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
135
136 private MetricsLogger mMetricsLogger = new MetricsLogger();
137
138 public void writeMetricsForEnter() {
Leo Hsuaeb32d82018-12-25 16:01:27 +0800139 if (!mIsShortcutViewEnabled) {
140 return;
141 }
142
Shaotang Li8662a912018-07-04 16:53:01 +0800143 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
144 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
145 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
146 .setType(MetricsEvent.TYPE_OPEN)
147 .setSubtype(entryType)
148 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
149 keyguard.isKeyguardLocked() ? 1 : 0));
150 }
151
152 public void writeMetricsForExit() {
Leo Hsuaeb32d82018-12-25 16:01:27 +0800153 if (!mIsShortcutViewEnabled) {
154 return;
155 }
156
Shaotang Li8662a912018-07-04 16:53:01 +0800157 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
158 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
159 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
160 .setType(MetricsEvent.TYPE_CLOSE)
161 .setSubtype(entryType)
162 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
163 .addTaggedData(
164 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
165 }
166
167 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
168 boolean hasShortcut) {
Leo Hsuaeb32d82018-12-25 16:01:27 +0800169 if (!mIsShortcutViewEnabled) {
170 return;
171 }
172
Shaotang Li8662a912018-07-04 16:53:01 +0800173 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
174 .setType(MetricsEvent.TYPE_ACTION)
175 .setSubtype(callSource)
176 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
177 phoneNumberType)
178 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
179 hasShortcut ? 1 : 0)
180 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
181 mIsProximityNear ? 1 : 0));
182 }
183 }
184
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 // Keys used with onSaveInstanceState().
186 private static final String LAST_NUMBER = "lastNumber";
187
188 // Intent action for this activity.
189 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
190
Shaotang Li8662a912018-07-04 16:53:01 +0800191 /**
192 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
193 * the emergency dialer.
194 */
195 public static final String EXTRA_ENTRY_TYPE =
196 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
197
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 // List of dialer button IDs.
Leo Hsu83ab2332018-12-18 15:20:45 +0800199 private static final int[] DIALER_KEYS = new int[]{
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 R.id.one, R.id.two, R.id.three,
201 R.id.four, R.id.five, R.id.six,
202 R.id.seven, R.id.eight, R.id.nine,
Leo Hsu83ab2332018-12-18 15:20:45 +0800203 R.id.star, R.id.zero, R.id.pound};
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204
205 // Debug constants.
206 private static final boolean DBG = false;
207 private static final String LOG_TAG = "EmergencyDialer";
208
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 /** The length of DTMF tones in milliseconds */
210 private static final int TONE_LENGTH_MS = 150;
211
212 /** The DTMF tone volume relative to other sounds in the stream */
213 private static final int TONE_RELATIVE_VOLUME = 80;
214
215 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
216 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
217
218 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
219
Lucas Dupineb9c5702017-05-10 16:57:09 -0700220 /** 90% opacity, different from other gradients **/
221 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
222
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800223 /** 85% opacity for black background **/
224 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
225
CY Cheng9a69c182018-06-15 21:20:10 +0800226 /** Size limit of emergency shortcut buttons container. **/
227 private static final int SHORTCUT_SIZE_LIMIT = 3;
228
Tyler Gunnc5428972018-03-28 14:15:34 -0700229 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 private View mDialButton;
231 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800232 private View mEmergencyShortcutView;
233 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700234
Leo Hsu43d670a2018-12-04 15:40:36 +0800235 private ShortcutViewUtils.PhoneInfo mPhoneInfo;
CY Cheng9a69c182018-06-15 21:20:10 +0800236
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800237 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800238 private EccShortcutAdapter mShortcutAdapter;
239 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800240
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241 private ToneGenerator mToneGenerator;
242 private Object mToneGeneratorLock = new Object();
243
244 // determines if we want to playback local DTMF tones.
245 private boolean mDTMFToneEnabled;
246
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700247 private EmergencyActionGroup mEmergencyActionGroup;
248
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800249 private EmergencyInfoGroup mEmergencyInfoGroup;
250
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 // close activity when screen turns off
252 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
253 @Override
254 public void onReceive(Context context, Intent intent) {
255 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700256 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 }
258 }
259 };
260
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800261 /**
262 * Customize accessibility methods in View.
263 */
264 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
265
266 /**
267 * Stop AccessiblityService from reading the title of a hidden View.
268 *
269 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
270 * in the animation end. The view with an accessibility pane title would call the
271 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
272 * accessibility service to read the pane title of fade out view instead of pane title of
273 * fade in view. So it need to filter out the event called by vanished pane.
274 */
275 @Override
276 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
277 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
278 && host.getVisibility() == View.GONE) {
279 return;
280 }
281 super.onPopulateAccessibilityEvent(host, event);
282 }
283 };
284
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
286
Lucas Dupineb9c5702017-05-10 16:57:09 -0700287 // Background gradient
288 private ColorExtractor mColorExtractor;
289 private GradientDrawable mBackgroundGradient;
290 private boolean mSupportsDarkText;
291
Tyler Gunnc5428972018-03-28 14:15:34 -0700292 private boolean mIsWfcEmergencyCallingWarningEnabled;
293 private float mDefaultDigitsTextSize;
294
Leo Hsu83ab2332018-12-18 15:20:45 +0800295 private boolean mIsShortcutViewEnabled;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800296
Shaotang Li8662a912018-07-04 16:53:01 +0800297 private MetricsWriter mMetricsWriter;
298 private SensorManager mSensorManager;
299 private Sensor mProximitySensor;
300 private boolean mIsProximityNear = false;
301
302 /**
303 * The time, in millis, since boot when user opened emergency dialer.
304 * This is used when calculating the user stay duration for metrics data.
305 */
306 private long mUserEnterTimeMillis = 0;
307
308 /**
309 * Bit flag indicating the actions performed by user. This is used for metrics data.
310 */
311 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
312
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313 @Override
314 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
315 // Do nothing
316 }
317
318 @Override
319 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700320 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700321 }
322
323 @Override
324 public void afterTextChanged(Editable input) {
325 // Check for special sequences, in particular the "**04" or "**05"
326 // sequences that allow you to enter PIN or PUK-related codes.
327 //
328 // But note we *don't* allow most other special sequences here,
329 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
330 // since those shouldn't be available if the device is locked.
331 //
332 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
333 // here, not the regular handleChars() method.
334 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
335 // A special sequence was entered, clear the digits
336 mDigits.getText().clear();
337 }
338
339 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800340 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341 }
342
343 @Override
344 protected void onCreate(Bundle icicle) {
345 super.onCreate(icicle);
346
Shaotang Li8662a912018-07-04 16:53:01 +0800347 mMetricsWriter = new MetricsWriter();
348 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
349 if (mSensorManager != null) {
350 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
351 }
352
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800354 setShowWhenLocked(true);
355 // Allow turning screen on
356 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357
Chuck Liaocdeebb42018-10-30 12:07:18 +0800358 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
359 PersistableBundle carrierConfig =
360 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
361
Leo Hsu83ab2332018-12-18 15:20:45 +0800362 mIsShortcutViewEnabled = false;
Leo Hsu43d670a2018-12-04 15:40:36 +0800363 mPhoneInfo = null;
Leo Hsu83ab2332018-12-18 15:20:45 +0800364 if (canEnableShortcutView(carrierConfig)) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800365 mPhoneInfo = ShortcutViewUtils.pickPreferredPhone(this);
366 if (mPhoneInfo != null) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800367 mIsShortcutViewEnabled = true;
Leo Hsu233f5b32018-11-07 11:08:03 +0800368 }
Chuck Liaocdeebb42018-10-30 12:07:18 +0800369 }
CY Cheng9a69c182018-06-15 21:20:10 +0800370 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsu83ab2332018-12-18 15:20:45 +0800371 + mIsShortcutViewEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800372
Lucas Dupineb9c5702017-05-10 16:57:09 -0700373 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800374
Leo Hsu83ab2332018-12-18 15:20:45 +0800375 if (mIsShortcutViewEnabled) {
376 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800377 updateTheme(false);
378 } else {
379 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
380 ColorExtractor.TYPE_EXTRA_DARK);
381 updateTheme(lockScreenColors.supportsDarkText());
382 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700383
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 setContentView(R.layout.emergency_dialer);
385
Tyler Gunnc5428972018-03-28 14:15:34 -0700386 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 mDigits.setKeyListener(DialerKeyListener.getInstance());
388 mDigits.setOnClickListener(this);
389 mDigits.setOnKeyListener(this);
390 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700391 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700392 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700393 maybeAddNumberFormatting();
394
Lucas Dupineb9c5702017-05-10 16:57:09 -0700395 mBackgroundGradient = new GradientDrawable(this);
396 Point displaySize = new Point();
397 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
398 .getDefaultDisplay().getSize(displaySize);
399 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Leo Hsu83ab2332018-12-18 15:20:45 +0800400 mBackgroundGradient.setAlpha(mIsShortcutViewEnabled
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800401 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700402 getWindow().setBackgroundDrawable(mBackgroundGradient);
403
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700404 // Check for the presence of the keypad
405 View view = findViewById(R.id.one);
406 if (view != null) {
407 setupKeypad();
408 }
409
410 mDelete = findViewById(R.id.deleteButton);
411 mDelete.setOnClickListener(this);
412 mDelete.setOnLongClickListener(this);
413
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700414 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415
416 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700417 // Read carrier config through the public API because PhoneGlobals is not available when we
418 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700419 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420 mDialButton.setOnClickListener(this);
421 } else {
422 mDialButton.setVisibility(View.GONE);
423 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700424 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
425 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
426 maybeShowWfcEmergencyCallingWarning();
427
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700428 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429
430 if (icicle != null) {
431 super.onRestoreInstanceState(icicle);
432 }
433
434 // Extract phone number from intent
435 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700436 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700437 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
438 if (number != null) {
439 mDigits.setText(number);
440 }
441 }
442
443 // if the mToneGenerator creation fails, just continue without it. It is
444 // a local audio signal, and is not as important as the dtmf tone itself.
445 synchronized (mToneGeneratorLock) {
446 if (mToneGenerator == null) {
447 try {
448 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
449 } catch (RuntimeException e) {
450 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
451 mToneGenerator = null;
452 }
453 }
454 }
455
456 final IntentFilter intentFilter = new IntentFilter();
457 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
458 registerReceiver(mBroadcastReceiver, intentFilter);
459
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700460 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800461
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800462 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
463
Leo Hsu83ab2332018-12-18 15:20:45 +0800464 if (mIsShortcutViewEnabled) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800465 setupEmergencyShortcutsView();
466 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467 }
468
469 @Override
470 protected void onDestroy() {
471 super.onDestroy();
472 synchronized (mToneGeneratorLock) {
473 if (mToneGenerator != null) {
474 mToneGenerator.release();
475 mToneGenerator = null;
476 }
477 }
478 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800479 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
480 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
481 mShortcutDataSetObserver = null;
482 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700483 }
484
485 @Override
486 protected void onRestoreInstanceState(Bundle icicle) {
487 mLastNumber = icicle.getString(LAST_NUMBER);
488 }
489
490 @Override
491 protected void onSaveInstanceState(Bundle outState) {
492 super.onSaveInstanceState(outState);
493 outState.putString(LAST_NUMBER, mLastNumber);
494 }
495
496 /**
497 * Explicitly turn off number formatting, since it gets in the way of the emergency
498 * number detector
499 */
500 protected void maybeAddNumberFormatting() {
501 // Do nothing.
502 }
503
504 @Override
505 protected void onPostCreate(Bundle savedInstanceState) {
506 super.onPostCreate(savedInstanceState);
507
508 // This can't be done in onCreate(), since the auto-restoring of the digits
509 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
510 // is called. This method will be called every time the activity is created, and
511 // will always happen after onRestoreSavedInstanceState().
512 mDigits.addTextChangedListener(this);
513 }
514
515 private void setupKeypad() {
516 // Setup the listeners for the buttons
517 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700518 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
519 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700520 }
521
522 View view = findViewById(R.id.zero);
523 view.setOnLongClickListener(this);
524 }
525
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800526 @Override
527 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800528 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
529 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
530 if (mIsShortcutViewEnabled && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800531 && mDialpadView.getVisibility() == View.VISIBLE) {
532 switchView(mEmergencyShortcutView, mDialpadView, true);
533 return;
534 }
535 super.onBackPressed();
536 }
537
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 /**
539 * handle key events
540 */
541 @Override
542 public boolean onKeyDown(int keyCode, KeyEvent event) {
543 switch (keyCode) {
544 // Happen when there's a "Call" hard button.
545 case KeyEvent.KEYCODE_CALL: {
546 if (TextUtils.isEmpty(mDigits.getText().toString())) {
547 // if we are adding a call from the InCallScreen and the phone
548 // number entered is empty, we just close the dialer to expose
549 // the InCallScreen under it.
550 finish();
551 } else {
552 // otherwise, we place the call.
553 placeCall();
554 }
555 return true;
556 }
557 }
558 return super.onKeyDown(keyCode, event);
559 }
560
561 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700562 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
564 mDigits.onKeyDown(keyCode, event);
565 }
566
567 @Override
568 public boolean onKey(View view, int keyCode, KeyEvent event) {
569 switch (view.getId()) {
570 case R.id.digits:
571 // Happen when "Done" button of the IME is pressed. This can happen when this
572 // Activity is forced into landscape mode due to a desk dock.
573 if (keyCode == KeyEvent.KEYCODE_ENTER
574 && event.getAction() == KeyEvent.ACTION_UP) {
575 placeCall();
576 return true;
577 }
578 break;
579 }
580 return false;
581 }
582
583 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700584 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800585 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700586 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800587 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700588 return handled;
589 }
590
591 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800592 public void onConfirmClick(EmergencyShortcutButton button) {
593 if (button == null) return;
594
Shaotang Li8662a912018-07-04 16:53:01 +0800595 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
596
597 // We interest on the context when user has intention to make phone call,
598 // so write metrics here for shortcut number even the call may not be created.
599 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
600 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
601
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800602 String phoneNumber = button.getPhoneNumber();
603
604 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800605 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu43d670a2018-12-04 15:40:36 +0800606 placeCall(phoneNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT,
607 mPhoneInfo);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800608 } else {
609 Log.d(LOG_TAG, "emergency number is empty");
610 }
611 }
612
613 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800614 public void onConfirmClick(EmergencyInfoGroup button) {
615 if (button == null) return;
616
617 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
618 Intent intent = (Intent) button.getTag(R.id.tag_intent);
619 if (intent != null) {
620 startActivity(intent);
621 }
622 }
623
624 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700625 public void onClick(View view) {
626 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700627 case R.id.deleteButton: {
628 keyPressed(KeyEvent.KEYCODE_DEL);
629 return;
630 }
631 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700632 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700633 placeCall();
634 return;
635 }
636 case R.id.digits: {
637 if (mDigits.length() != 0) {
638 mDigits.setCursorVisible(true);
639 }
640 return;
641 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800642 case R.id.floating_action_button_dialpad: {
Shaotang Li8662a912018-07-04 16:53:01 +0800643 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800644 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800645 switchView(mDialpadView, mEmergencyShortcutView, true);
646 return;
647 }
Yorke Lee23a70732014-08-14 17:12:01 -0700648 }
649 }
650
651 @Override
652 public void onPressed(View view, boolean pressed) {
653 if (!pressed) {
654 return;
655 }
656 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 case R.id.one: {
658 playTone(ToneGenerator.TONE_DTMF_1);
659 keyPressed(KeyEvent.KEYCODE_1);
660 return;
661 }
662 case R.id.two: {
663 playTone(ToneGenerator.TONE_DTMF_2);
664 keyPressed(KeyEvent.KEYCODE_2);
665 return;
666 }
667 case R.id.three: {
668 playTone(ToneGenerator.TONE_DTMF_3);
669 keyPressed(KeyEvent.KEYCODE_3);
670 return;
671 }
672 case R.id.four: {
673 playTone(ToneGenerator.TONE_DTMF_4);
674 keyPressed(KeyEvent.KEYCODE_4);
675 return;
676 }
677 case R.id.five: {
678 playTone(ToneGenerator.TONE_DTMF_5);
679 keyPressed(KeyEvent.KEYCODE_5);
680 return;
681 }
682 case R.id.six: {
683 playTone(ToneGenerator.TONE_DTMF_6);
684 keyPressed(KeyEvent.KEYCODE_6);
685 return;
686 }
687 case R.id.seven: {
688 playTone(ToneGenerator.TONE_DTMF_7);
689 keyPressed(KeyEvent.KEYCODE_7);
690 return;
691 }
692 case R.id.eight: {
693 playTone(ToneGenerator.TONE_DTMF_8);
694 keyPressed(KeyEvent.KEYCODE_8);
695 return;
696 }
697 case R.id.nine: {
698 playTone(ToneGenerator.TONE_DTMF_9);
699 keyPressed(KeyEvent.KEYCODE_9);
700 return;
701 }
702 case R.id.zero: {
703 playTone(ToneGenerator.TONE_DTMF_0);
704 keyPressed(KeyEvent.KEYCODE_0);
705 return;
706 }
707 case R.id.pound: {
708 playTone(ToneGenerator.TONE_DTMF_P);
709 keyPressed(KeyEvent.KEYCODE_POUND);
710 return;
711 }
712 case R.id.star: {
713 playTone(ToneGenerator.TONE_DTMF_S);
714 keyPressed(KeyEvent.KEYCODE_STAR);
715 return;
716 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 }
718 }
719
720 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 * called for long touch events
722 */
723 @Override
724 public boolean onLongClick(View view) {
725 int id = view.getId();
726 switch (id) {
727 case R.id.deleteButton: {
728 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 return true;
730 }
731 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700732 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 keyPressed(KeyEvent.KEYCODE_PLUS);
734 return true;
735 }
736 }
737 return false;
738 }
739
740 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700741 protected void onStart() {
742 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800743
744 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
745 mUserActions = MetricsWriter.USER_ACTION_NONE;
746 mMetricsWriter.writeMetricsForEnter();
747
Leo Hsu83ab2332018-12-18 15:20:45 +0800748 if (mIsShortcutViewEnabled) {
749 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800750 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
751 updateTheme(false);
752 } else {
753 mColorExtractor.addOnColorsChangedListener(this);
754 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
755 ColorExtractor.TYPE_EXTRA_DARK);
756 // Do not animate when view isn't visible yet, just set an initial state.
757 mBackgroundGradient.setColors(lockScreenColors, false);
758 updateTheme(lockScreenColors.supportsDarkText());
759 }
CY Cheng9a69c182018-06-15 21:20:10 +0800760
Leo Hsu43d670a2018-12-04 15:40:36 +0800761 if (mIsShortcutViewEnabled) {
762 mPhoneInfo = ShortcutViewUtils.pickPreferredPhone(this);
763 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800764 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700765 }
766
767 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 protected void onResume() {
769 super.onResume();
770
Shaotang Li8662a912018-07-04 16:53:01 +0800771 if (mProximitySensor != null) {
772 mSensorManager.registerListener(
773 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
774 }
775
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776 // retrieve the DTMF tone play back setting.
777 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
778 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
779
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 // if the mToneGenerator creation fails, just continue without it. It is
781 // a local audio signal, and is not as important as the dtmf tone itself.
782 synchronized (mToneGeneratorLock) {
783 if (mToneGenerator == null) {
784 try {
785 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
786 TONE_RELATIVE_VOLUME);
787 } catch (RuntimeException e) {
788 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
789 mToneGenerator = null;
790 }
791 }
792 }
793
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 updateDialAndDeleteButtonStateEnabledAttr();
795 }
796
797 @Override
798 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 super.onPause();
Shaotang Li8662a912018-07-04 16:53:01 +0800800 if (mProximitySensor != null) {
801 mSensorManager.unregisterListener(this, mProximitySensor);
802 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 }
804
Lucas Dupineb9c5702017-05-10 16:57:09 -0700805 @Override
806 protected void onStop() {
807 super.onStop();
Shaotang Li8662a912018-07-04 16:53:01 +0800808 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700809 mColorExtractor.removeOnColorsChangedListener(this);
810 }
811
Leo Hsu83ab2332018-12-18 15:20:45 +0800812 private boolean canEnableShortcutView(PersistableBundle carrierConfig) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800813 if (!carrierConfig.getBoolean(
814 CarrierConfigManager.KEY_SUPPORT_EMERGENCY_DIALER_SHORTCUT_BOOL)) {
815 Log.d(LOG_TAG, "Disables shortcut view by carrier requirement");
816 return false;
817 }
818 return true;
819 }
820
Lucas Dupineb9c5702017-05-10 16:57:09 -0700821 /**
822 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800823 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700824 * @param supportsDarkText true if gradient supports dark text
825 */
826 private void updateTheme(boolean supportsDarkText) {
827 if (mSupportsDarkText == supportsDarkText) {
828 return;
829 }
830 mSupportsDarkText = supportsDarkText;
831
832 // We can't change themes after inflation, in this case we'll have to recreate
833 // the whole activity.
834 if (mBackgroundGradient != null) {
835 recreate();
836 return;
837 }
838
839 int vis = getWindow().getDecorView().getSystemUiVisibility();
840 if (supportsDarkText) {
841 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
842 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
843 setTheme(R.style.EmergencyDialerThemeDark);
844 } else {
845 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
846 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
847 setTheme(R.style.EmergencyDialerTheme);
848 }
849 getWindow().getDecorView().setSystemUiVisibility(vis);
850 }
851
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700852 /**
853 * place the call, but check to make sure it is a viable number.
854 */
855 private void placeCall() {
Shaotang Li8662a912018-07-04 16:53:01 +0800856 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900858
859 // Convert into emergency number according to emergency conversion map.
860 // If conversion map is not defined (this is default), this method does
861 // nothing and just returns input number.
862 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
863
Leo Hsu43d670a2018-12-04 15:40:36 +0800864 boolean isEmergencyNumber = false;
865 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
866 if (mPhoneInfo != null) {
867 isEmergencyNumber = mPhoneInfo.hasPromotedEmergencyNumber(mLastNumber);
868 if (isEmergencyNumber) {
869 phoneToMakeCall = mPhoneInfo;
870 }
871 }
872 if (!isEmergencyNumber) {
873 TelephonyManager tm = getSystemService(TelephonyManager.class);
874 isEmergencyNumber = tm.isCurrentEmergencyNumber(mLastNumber);
875 }
876
877 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
879
880 // place the call if it is a valid number
881 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
882 // There is no number entered.
883 playTone(ToneGenerator.TONE_PROP_NACK);
884 return;
885 }
Shaotang Li8662a912018-07-04 16:53:01 +0800886
887 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
888 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
889
Leo Hsu43d670a2018-12-04 15:40:36 +0800890 placeCall(mLastNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD,
891 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700892 } else {
893 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
894
Shaotang Li8662a912018-07-04 16:53:01 +0800895 // We interest on the context when user has intention to make phone call,
896 // so write metrics here for non-emergency numbers even these numbers are rejected.
897 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
898 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
899
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700900 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
901 }
Yorke Lee9b341512014-10-17 11:36:41 -0700902 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700903 }
904
Leo Hsu43d670a2018-12-04 15:40:36 +0800905 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
906 Bundle extras = new Bundle();
907 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800908 /**
909 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
910 * when the dialed number is ambiguous and identified as both emergency number and any
911 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
912 * number in a country and a non-emergency number of a carrier's customer service hotline.
913 */
914 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800915
916 if (phone != null && phone.getPhoneAccountHandle() != null) {
917 // Requests to dial through the specified phone.
918 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
919 phone.getPhoneAccountHandle());
920 }
921
922 TelecomManager tm = this.getSystemService(TelecomManager.class);
923 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
924 }
925
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 /**
927 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
928 *
929 * The tone is played locally, using the audio stream for phone calls.
930 * Tones are played only if the "Audible touch tones" user preference
931 * is checked, and are NOT played if the device is in silent mode.
932 *
933 * @param tone a tone code from {@link ToneGenerator}
934 */
935 void playTone(int tone) {
936 // if local tone playback is disabled, just return.
937 if (!mDTMFToneEnabled) {
938 return;
939 }
940
941 // Also do nothing if the phone is in silent mode.
942 // We need to re-check the ringer mode for *every* playTone()
943 // call, rather than keeping a local flag that's updated in
944 // onResume(), since it's possible to toggle silent mode without
945 // leaving the current activity (via the ENDCALL-longpress menu.)
946 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
947 int ringerMode = audioManager.getRingerMode();
948 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800949 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 return;
951 }
952
953 synchronized (mToneGeneratorLock) {
954 if (mToneGenerator == null) {
955 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
956 return;
957 }
958
959 // Start the new tone (will stop any playing tone)
960 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
961 }
962 }
963
964 private CharSequence createErrorMessage(String number) {
965 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800966 String errorString = getString(R.string.dial_emergency_error, number);
967 int startingPosition = errorString.indexOf(number);
968 int endingPosition = startingPosition + number.length();
969 Spannable result = new SpannableString(errorString);
970 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
971 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 } else {
973 return getText(R.string.dial_emergency_empty_error).toString();
974 }
975 }
976
977 @Override
978 protected Dialog onCreateDialog(int id) {
979 AlertDialog dialog = null;
980 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
981 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700982 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
984 .setMessage(createErrorMessage(mLastNumber))
985 .setPositiveButton(R.string.ok, null)
986 .setCancelable(true).create();
987
988 // blur stuff behind the dialog
989 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700990 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 }
992 return dialog;
993 }
994
995 @Override
996 protected void onPrepareDialog(int id, Dialog dialog) {
997 super.onPrepareDialog(id, dialog);
998 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
999 AlertDialog alert = (AlertDialog) dialog;
1000 alert.setMessage(createErrorMessage(mLastNumber));
1001 }
1002 }
1003
Andrew Leed5631e82014-10-08 16:03:58 -07001004 @Override
1005 public boolean onOptionsItemSelected(MenuItem item) {
1006 final int itemId = item.getItemId();
1007 if (itemId == android.R.id.home) {
1008 onBackPressed();
1009 return true;
1010 }
1011 return super.onOptionsItemSelected(item);
1012 }
1013
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001014 /**
1015 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
1016 */
1017 private void updateDialAndDeleteButtonStateEnabledAttr() {
1018 final boolean notEmpty = mDigits.length() != 0;
1019
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 mDelete.setEnabled(notEmpty);
1021 }
Yorke Lee91311662014-10-24 14:50:45 -07001022
1023 /**
1024 * Remove the digit just before the current position. Used by various long pressed callbacks
1025 * to remove the digit that was populated as a result of the short click.
1026 */
1027 private void removePreviousDigitIfPossible() {
1028 final int currentPosition = mDigits.getSelectionStart();
1029 if (currentPosition > 0) {
1030 mDigits.setSelection(currentPosition);
1031 mDigits.getText().delete(currentPosition - 1, currentPosition);
1032 }
1033 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -08001034
1035 /**
1036 * Update the text-to-speech annotations in the edit field.
1037 */
1038 private void updateTtsSpans() {
1039 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
1040 mDigits.getText().removeSpan(o);
1041 }
1042 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
1043 }
Lucas Dupineb9c5702017-05-10 16:57:09 -07001044
1045 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -07001046 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -07001047 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -07001048 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1049 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001050 mBackgroundGradient.setColors(colors);
1051 updateTheme(colors.supportsDarkText());
1052 }
1053 }
Tyler Gunnc5428972018-03-28 14:15:34 -07001054
1055 /**
1056 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1057 * add hint text above the dial pad which warns the user of this case.
1058 */
1059 private void maybeShowWfcEmergencyCallingWarning() {
1060 if (!mIsWfcEmergencyCallingWarningEnabled) {
1061 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1062 return;
1063 }
1064
1065 // Use an async task rather than calling into Telephony on UI thread.
1066 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1067 @Override
1068 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001069 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -07001070 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1071 ServiceState ss = tm.getServiceState();
1072 boolean isCellAvailable =
1073 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1074 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +08001075 + " isCellAvailable=" + isCellAvailable
1076 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -07001077 return isWfcAvailable && !isCellAvailable;
1078 }
1079
1080 @Override
1081 protected void onPostExecute(Boolean result) {
1082 if (result.booleanValue()) {
1083 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1084 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1085 } else {
1086 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1087 mDigits.setHint("");
1088 }
1089 maybeChangeHintSize();
1090 }
1091 };
1092 showWfcWarningTask.execute((Void) null);
1093 }
1094
1095 /**
1096 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1097 * edit view and set the font size to a smaller size appropriate for the emergency calling
1098 * warning.
1099 */
1100 private void maybeChangeHintSize() {
1101 if (TextUtils.isEmpty(mDigits.getHint())
1102 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1103 // No hint or there are dialed digits, so use default size.
1104 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1105 // By default, the digits view auto-resizes to fit the text it contains, so
1106 // enable that now.
1107 mDigits.setResizeEnabled(true);
1108 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1109 } else {
1110 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1111 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1112 R.dimen.emergency_call_warning_size));
1113 // Since we're populating this with a static text string, disable auto-resize.
1114 mDigits.setResizeEnabled(false);
1115 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1116 }
1117 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001118
1119 private void setupEmergencyShortcutsView() {
1120 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1121 mDialpadView = findViewById(R.id.emergency_dialer);
1122
Chihhang Chuangcaba0da2018-08-02 22:25:06 +08001123 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1124 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1125
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001126 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1127 dialpadButton.setOnClickListener(this);
1128
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001129 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001130
1131 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1132 mEmergencyActionGroup.setVisibility(View.GONE);
1133
1134 // Setup dialpad title.
1135 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1136 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1137
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001138 mEmergencyShortcutButtonList = new ArrayList<>();
1139 setupEmergencyCallShortcutButton();
1140
Leo Hsu43d670a2018-12-04 15:40:36 +08001141 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001142
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001143 switchView(mEmergencyShortcutView, mDialpadView, false);
1144 }
1145
Leo Hsu43d670a2018-12-04 15:40:36 +08001146 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001147 final View locationInfo = findViewById(R.id.location_info);
1148
Leo Hsu43d670a2018-12-04 15:40:36 +08001149 String countryIso = mPhoneInfo != null ? mPhoneInfo.getCountryIso() : null;
CY Cheng9a69c182018-06-15 21:20:10 +08001150 String countryName = null;
1151 if (!TextUtils.isEmpty(countryIso)) {
1152 Locale locale = Locale.getDefault();
1153 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1154 .getDisplayCountry();
1155 }
1156 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001157 locationInfo.setVisibility(View.INVISIBLE);
1158 } else {
1159 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001160 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001161 locationInfo.setVisibility(View.VISIBLE);
1162 }
1163 }
1164
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001165 private void setupEmergencyCallShortcutButton() {
1166 final ViewGroup shortcutButtonContainer = findViewById(
1167 R.id.emergency_shortcut_buttons_container);
1168 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001169 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001170
CY Cheng9a69c182018-06-15 21:20:10 +08001171 mShortcutAdapter = new EccShortcutAdapter(this) {
1172 @Override
1173 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1174 CharSequence description, int iconRes) {
1175 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1176 .inflate(R.layout.emergency_shortcut_button, parent, false);
1177 button.setPhoneNumber(number);
1178 button.setPhoneDescription(description);
1179 button.setPhoneTypeIcon(iconRes);
1180 button.setOnConfirmClickListener(EmergencyDialer.this);
1181 return button;
1182 }
1183 };
1184 mShortcutDataSetObserver = new DataSetObserver() {
1185 @Override
1186 public void onChanged() {
1187 super.onChanged();
1188 updateLayout();
1189 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001190
CY Cheng9a69c182018-06-15 21:20:10 +08001191 @Override
1192 public void onInvalidated() {
1193 super.onInvalidated();
1194 updateLayout();
1195 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001196
CY Cheng9a69c182018-06-15 21:20:10 +08001197 private void updateLayout() {
1198 // clear previous added buttons
1199 shortcutButtonContainer.removeAllViews();
1200 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001201
CY Cheng9a69c182018-06-15 21:20:10 +08001202 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1203 EmergencyShortcutButton button = (EmergencyShortcutButton)
1204 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1205 mEmergencyShortcutButtonList.add(button);
1206 shortcutButtonContainer.addView(button);
1207 }
1208
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001209 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001210 if (mEmergencyShortcutButtonList.size() > 1) {
1211 emergencyNumberTitle.setText(getString(
1212 R.string.numerous_emergency_numbers_title));
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001213 // Update mEmergencyInfoGroup margin to avoid UI overlay when
1214 // emergency shortcut button more than 2.
1215 if (mEmergencyShortcutButtonList.size() > 2) {
1216 mEmergencyInfoGroup.updateLayoutMargin();
1217 }
CY Cheng9a69c182018-06-15 21:20:10 +08001218 } else {
1219 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1220 }
1221 }
1222 };
1223 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1224 }
1225
Leo Hsu43d670a2018-12-04 15:40:36 +08001226 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001227 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001228 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001229 if (mShortcutAdapter != null) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001230 mShortcutAdapter.updateCountryEccInfo(this, mPhoneInfo);
CY Cheng9a69c182018-06-15 21:20:10 +08001231 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001232 }
1233 }
1234
1235 /**
1236 * Called by the activity before a touch event is dispatched to the view hierarchy.
1237 */
1238 private void onPreTouchEvent(MotionEvent event) {
1239 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001240 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001241
1242 if (mEmergencyShortcutButtonList != null) {
1243 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1244 button.onPreTouchEvent(event);
1245 }
1246 }
1247 }
1248
1249 /**
1250 * Called by the activity after a touch event is dispatched to the view hierarchy.
1251 */
1252 private void onPostTouchEvent(MotionEvent event) {
1253 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001254 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001255
1256 if (mEmergencyShortcutButtonList != null) {
1257 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1258 button.onPostTouchEvent(event);
1259 }
1260 }
1261 }
1262
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001263 /**
1264 * Switch two view.
1265 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001266 * @param displayView the view would be displayed.
1267 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001268 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1269 */
1270 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1271 if (displayView == null || hideView == null) {
1272 return;
1273 }
1274
1275 if (displayView.getVisibility() == View.VISIBLE) {
1276 return;
1277 }
1278
1279 if (hasAnimation) {
1280 crossfade(hideView, displayView);
1281 } else {
1282 hideView.setVisibility(View.GONE);
1283 displayView.setVisibility(View.VISIBLE);
1284 }
1285 }
1286
1287 /**
1288 * Fade out and fade in animation between two view transition.
1289 */
1290 private void crossfade(View fadeOutView, View fadeInView) {
1291 if (fadeOutView == null || fadeInView == null) {
1292 return;
1293 }
1294 final int shortAnimationDuration = getResources().getInteger(
1295 android.R.integer.config_shortAnimTime);
1296
1297 fadeInView.setAlpha(0f);
1298 fadeInView.setVisibility(View.VISIBLE);
1299
1300 fadeInView.animate()
1301 .alpha(1f)
1302 .setDuration(shortAnimationDuration)
1303 .setListener(null);
1304
1305 fadeOutView.animate()
1306 .alpha(0f)
1307 .setDuration(shortAnimationDuration)
1308 .setListener(new AnimatorListenerAdapter() {
1309 @Override
1310 public void onAnimationEnd(Animator animation) {
1311 fadeOutView.setVisibility(View.GONE);
1312 }
1313 });
1314 }
Shaotang Li8662a912018-07-04 16:53:01 +08001315
1316 @Override
1317 public void onSensorChanged(SensorEvent event) {
1318 float distance = event.values[0];
1319 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1320 }
1321
1322 @Override
1323 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1324 // Not used.
1325 }
1326
1327 private boolean isShortcutNumber(String number) {
1328 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1329 return false;
1330 }
1331
1332 boolean isShortcut = false;
1333 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1334 if (button != null && number.equals(button.getPhoneNumber())) {
1335 isShortcut = true;
1336 break;
1337 }
1338 }
1339 return isShortcut;
1340 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341}