blob: 0bd299356a5016fe427271b708cc3d78d52869d8 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Shaotang Li8662a912018-07-04 16:53:01 +080026import android.app.KeyguardManager;
Lucas Dupineb9c5702017-05-10 16:57:09 -070027import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080032import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080033import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Shaotang Li8662a912018-07-04 16:53:01 +080035import android.hardware.Sensor;
36import android.hardware.SensorEvent;
37import android.hardware.SensorEventListener;
38import android.hardware.SensorManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.media.AudioManager;
40import android.media.ToneGenerator;
Shaotang Li8662a912018-07-04 16:53:01 +080041import android.metrics.LogMaker;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070043import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070045import android.os.PersistableBundle;
Shaotang Li8662a912018-07-04 16:53:01 +080046import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.provider.Settings;
Shaotang Li5d906c12018-07-19 17:32:42 +080048import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070049import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070050import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080053import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070054import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070055import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070056import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070058import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080059import android.text.Spannable;
60import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
62import android.text.TextWatcher;
63import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080064import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070066import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070067import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070069import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070070import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080072import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080073import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080075import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080076import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Lucas Dupinaf9e9912017-06-22 12:39:39 -070078import com.android.internal.colorextraction.ColorExtractor;
79import com.android.internal.colorextraction.ColorExtractor.GradientColors;
80import com.android.internal.colorextraction.drawable.GradientDrawable;
Shaotang Li8662a912018-07-04 16:53:01 +080081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Yorke Lee23a70732014-08-14 17:12:01 -070083import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070084import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070085import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080086import com.android.phone.ecc.CountryEccInfo;
87import com.android.phone.ecc.EccInfoHelper;
Leo Hsu779569a2018-07-10 11:37:52 +080088import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
Chihhang Chuang92cfe512018-06-07 16:25:27 +080090import java.util.ArrayList;
91import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080092import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080093
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094/**
95 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
96 *
97 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
98 * activity from apps/Contacts) that:
Leo Hsu83ab2332018-12-18 15:20:45 +080099 * 1. Allows ONLY emergency calls to be dialed
100 * 2. Disallows voicemail functionality
101 * 3. Allows this activity to stay in front of the keyguard.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 *
103 * TODO: Even though this is an ultra-simplified version of the normal
104 * dialer, there's still lots of code duplication between this class and
105 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
106 * moved into a shared base class that would live in the framework?
107 * Or could we figure out some way to move *this* class into apps/Contacts
108 * also?
109 */
110public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700111 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800112 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800113 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener,
114 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-07-04 16:53:01 +0800115
116 private class MetricsWriter {
117 // Metrics constants indicating the entry type that user opened emergency dialer.
118 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
119 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
120 public static final int ENTRY_TYPE_UNKNOWN = 0;
121 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
122 public static final int ENTRY_TYPE_POWER_MENU = 2;
123
124 // Metrics constants indicating the UI that user made phone call.
125 public static final int CALL_SOURCE_DIALPAD = 0;
126 public static final int CALL_SOURCE_SHORTCUT = 1;
127
128 // Metrics constants indicating the phone number type of a call user made.
129 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
130 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
131
132 // Metrics constants indicating the actions performed by user.
133 public static final int USER_ACTION_NONE = 0x0;
134 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
135 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
136 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
137 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
138
139 private MetricsLogger mMetricsLogger = new MetricsLogger();
140
141 public void writeMetricsForEnter() {
142 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
143 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
144 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
145 .setType(MetricsEvent.TYPE_OPEN)
146 .setSubtype(entryType)
147 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
148 keyguard.isKeyguardLocked() ? 1 : 0));
149 }
150
151 public void writeMetricsForExit() {
152 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
153 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
154 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
155 .setType(MetricsEvent.TYPE_CLOSE)
156 .setSubtype(entryType)
157 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
158 .addTaggedData(
159 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
160 }
161
162 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
163 boolean hasShortcut) {
164 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
165 .setType(MetricsEvent.TYPE_ACTION)
166 .setSubtype(callSource)
167 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
168 phoneNumberType)
169 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
170 hasShortcut ? 1 : 0)
171 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
172 mIsProximityNear ? 1 : 0));
173 }
174 }
175
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176 // Keys used with onSaveInstanceState().
177 private static final String LAST_NUMBER = "lastNumber";
178
179 // Intent action for this activity.
180 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
181
Shaotang Li8662a912018-07-04 16:53:01 +0800182 /**
183 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
184 * the emergency dialer.
185 */
186 public static final String EXTRA_ENTRY_TYPE =
187 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
188
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 // List of dialer button IDs.
Leo Hsu83ab2332018-12-18 15:20:45 +0800190 private static final int[] DIALER_KEYS = new int[]{
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 R.id.one, R.id.two, R.id.three,
192 R.id.four, R.id.five, R.id.six,
193 R.id.seven, R.id.eight, R.id.nine,
Leo Hsu83ab2332018-12-18 15:20:45 +0800194 R.id.star, R.id.zero, R.id.pound};
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195
196 // Debug constants.
197 private static final boolean DBG = false;
198 private static final String LOG_TAG = "EmergencyDialer";
199
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 /** The length of DTMF tones in milliseconds */
201 private static final int TONE_LENGTH_MS = 150;
202
203 /** The DTMF tone volume relative to other sounds in the stream */
204 private static final int TONE_RELATIVE_VOLUME = 80;
205
206 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
207 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
208
209 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
210
Lucas Dupineb9c5702017-05-10 16:57:09 -0700211 /** 90% opacity, different from other gradients **/
212 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
213
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800214 /** 85% opacity for black background **/
215 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
216
CY Cheng9a69c182018-06-15 21:20:10 +0800217 /** Size limit of emergency shortcut buttons container. **/
218 private static final int SHORTCUT_SIZE_LIMIT = 3;
219
Tyler Gunnc5428972018-03-28 14:15:34 -0700220 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 private View mDialButton;
222 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800223 private View mEmergencyShortcutView;
224 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225
CY Cheng9a69c182018-06-15 21:20:10 +0800226 private EccInfoHelper mEccInfoHelper;
227
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800228 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800229 private EccShortcutAdapter mShortcutAdapter;
230 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800231
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232 private ToneGenerator mToneGenerator;
233 private Object mToneGeneratorLock = new Object();
234
235 // determines if we want to playback local DTMF tones.
236 private boolean mDTMFToneEnabled;
237
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700238 private EmergencyActionGroup mEmergencyActionGroup;
239
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800240 private EmergencyInfoGroup mEmergencyInfoGroup;
241
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700242 // close activity when screen turns off
243 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
244 @Override
245 public void onReceive(Context context, Intent intent) {
246 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700247 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 }
249 }
250 };
251
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800252 /**
253 * Customize accessibility methods in View.
254 */
255 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
256
257 /**
258 * Stop AccessiblityService from reading the title of a hidden View.
259 *
260 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
261 * in the animation end. The view with an accessibility pane title would call the
262 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
263 * accessibility service to read the pane title of fade out view instead of pane title of
264 * fade in view. So it need to filter out the event called by vanished pane.
265 */
266 @Override
267 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
268 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
269 && host.getVisibility() == View.GONE) {
270 return;
271 }
272 super.onPopulateAccessibilityEvent(host, event);
273 }
274 };
275
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
277
Lucas Dupineb9c5702017-05-10 16:57:09 -0700278 // Background gradient
279 private ColorExtractor mColorExtractor;
280 private GradientDrawable mBackgroundGradient;
281 private boolean mSupportsDarkText;
282
Tyler Gunnc5428972018-03-28 14:15:34 -0700283 private boolean mIsWfcEmergencyCallingWarningEnabled;
284 private float mDefaultDigitsTextSize;
285
Leo Hsu83ab2332018-12-18 15:20:45 +0800286 private boolean mIsShortcutViewEnabled;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800287
Shaotang Li8662a912018-07-04 16:53:01 +0800288 private MetricsWriter mMetricsWriter;
289 private SensorManager mSensorManager;
290 private Sensor mProximitySensor;
291 private boolean mIsProximityNear = false;
292
293 /**
294 * The time, in millis, since boot when user opened emergency dialer.
295 * This is used when calculating the user stay duration for metrics data.
296 */
297 private long mUserEnterTimeMillis = 0;
298
299 /**
300 * Bit flag indicating the actions performed by user. This is used for metrics data.
301 */
302 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
303
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 @Override
305 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
306 // Do nothing
307 }
308
309 @Override
310 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700311 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700312 }
313
314 @Override
315 public void afterTextChanged(Editable input) {
316 // Check for special sequences, in particular the "**04" or "**05"
317 // sequences that allow you to enter PIN or PUK-related codes.
318 //
319 // But note we *don't* allow most other special sequences here,
320 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
321 // since those shouldn't be available if the device is locked.
322 //
323 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
324 // here, not the regular handleChars() method.
325 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
326 // A special sequence was entered, clear the digits
327 mDigits.getText().clear();
328 }
329
330 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800331 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 }
333
334 @Override
335 protected void onCreate(Bundle icicle) {
336 super.onCreate(icicle);
337
Shaotang Li8662a912018-07-04 16:53:01 +0800338 mMetricsWriter = new MetricsWriter();
339 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
340 if (mSensorManager != null) {
341 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
342 }
343
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800345 setShowWhenLocked(true);
346 // Allow turning screen on
347 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348
Chuck Liaocdeebb42018-10-30 12:07:18 +0800349 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
350 PersistableBundle carrierConfig =
351 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
352
Leo Hsu83ab2332018-12-18 15:20:45 +0800353 mIsShortcutViewEnabled = false;
354 if (canEnableShortcutView(carrierConfig)) {
355 TelephonyManager tm = getSystemService(TelephonyManager.class);
356 String countryIso = tm.getNetworkCountryIso();
357 if (TextUtils.isEmpty(countryIso)) {
358 Log.d(LOG_TAG, "Unable to determine the country of current network.");
359 } else if (!EccInfoHelper.isCountryEccInfoAvailable(this, countryIso)) {
360 Log.d(LOG_TAG, "ECC info is unavailable.");
Leo Hsu233f5b32018-11-07 11:08:03 +0800361 } else {
Leo Hsu83ab2332018-12-18 15:20:45 +0800362 mIsShortcutViewEnabled = true;
Leo Hsu233f5b32018-11-07 11:08:03 +0800363 }
Chuck Liaocdeebb42018-10-30 12:07:18 +0800364 }
CY Cheng9a69c182018-06-15 21:20:10 +0800365 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsu83ab2332018-12-18 15:20:45 +0800366 + mIsShortcutViewEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800367
Lucas Dupineb9c5702017-05-10 16:57:09 -0700368 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800369
Leo Hsu83ab2332018-12-18 15:20:45 +0800370 if (mIsShortcutViewEnabled) {
371 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800372 updateTheme(false);
373 } else {
374 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
375 ColorExtractor.TYPE_EXTRA_DARK);
376 updateTheme(lockScreenColors.supportsDarkText());
377 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700378
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700379 setContentView(R.layout.emergency_dialer);
380
Tyler Gunnc5428972018-03-28 14:15:34 -0700381 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700382 mDigits.setKeyListener(DialerKeyListener.getInstance());
383 mDigits.setOnClickListener(this);
384 mDigits.setOnKeyListener(this);
385 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700386 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700387 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700388 maybeAddNumberFormatting();
389
Lucas Dupineb9c5702017-05-10 16:57:09 -0700390 mBackgroundGradient = new GradientDrawable(this);
391 Point displaySize = new Point();
392 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
393 .getDefaultDisplay().getSize(displaySize);
394 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Leo Hsu83ab2332018-12-18 15:20:45 +0800395 mBackgroundGradient.setAlpha(mIsShortcutViewEnabled
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800396 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700397 getWindow().setBackgroundDrawable(mBackgroundGradient);
398
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700399 // Check for the presence of the keypad
400 View view = findViewById(R.id.one);
401 if (view != null) {
402 setupKeypad();
403 }
404
405 mDelete = findViewById(R.id.deleteButton);
406 mDelete.setOnClickListener(this);
407 mDelete.setOnLongClickListener(this);
408
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700409 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410
411 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700412 // Read carrier config through the public API because PhoneGlobals is not available when we
413 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700414 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 mDialButton.setOnClickListener(this);
416 } else {
417 mDialButton.setVisibility(View.GONE);
418 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700419 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
420 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
421 maybeShowWfcEmergencyCallingWarning();
422
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700423 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700424
425 if (icicle != null) {
426 super.onRestoreInstanceState(icicle);
427 }
428
429 // Extract phone number from intent
430 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700431 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700432 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
433 if (number != null) {
434 mDigits.setText(number);
435 }
436 }
437
438 // if the mToneGenerator creation fails, just continue without it. It is
439 // a local audio signal, and is not as important as the dtmf tone itself.
440 synchronized (mToneGeneratorLock) {
441 if (mToneGenerator == null) {
442 try {
443 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
444 } catch (RuntimeException e) {
445 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
446 mToneGenerator = null;
447 }
448 }
449 }
450
451 final IntentFilter intentFilter = new IntentFilter();
452 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
453 registerReceiver(mBroadcastReceiver, intentFilter);
454
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700455 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800456
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800457 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
458
Leo Hsu83ab2332018-12-18 15:20:45 +0800459 if (mIsShortcutViewEnabled) {
Leo Hsu233f5b32018-11-07 11:08:03 +0800460 mEccInfoHelper = new EccInfoHelper(IsoToEccProtobufRepository.getInstance());
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800461 setupEmergencyShortcutsView();
462 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700463 }
464
465 @Override
466 protected void onDestroy() {
467 super.onDestroy();
468 synchronized (mToneGeneratorLock) {
469 if (mToneGenerator != null) {
470 mToneGenerator.release();
471 mToneGenerator = null;
472 }
473 }
474 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800475 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
476 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
477 mShortcutDataSetObserver = null;
478 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 }
480
481 @Override
482 protected void onRestoreInstanceState(Bundle icicle) {
483 mLastNumber = icicle.getString(LAST_NUMBER);
484 }
485
486 @Override
487 protected void onSaveInstanceState(Bundle outState) {
488 super.onSaveInstanceState(outState);
489 outState.putString(LAST_NUMBER, mLastNumber);
490 }
491
492 /**
493 * Explicitly turn off number formatting, since it gets in the way of the emergency
494 * number detector
495 */
496 protected void maybeAddNumberFormatting() {
497 // Do nothing.
498 }
499
500 @Override
501 protected void onPostCreate(Bundle savedInstanceState) {
502 super.onPostCreate(savedInstanceState);
503
504 // This can't be done in onCreate(), since the auto-restoring of the digits
505 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
506 // is called. This method will be called every time the activity is created, and
507 // will always happen after onRestoreSavedInstanceState().
508 mDigits.addTextChangedListener(this);
509 }
510
511 private void setupKeypad() {
512 // Setup the listeners for the buttons
513 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700514 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
515 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700516 }
517
518 View view = findViewById(R.id.zero);
519 view.setOnLongClickListener(this);
520 }
521
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800522 @Override
523 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800524 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
525 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
526 if (mIsShortcutViewEnabled && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800527 && mDialpadView.getVisibility() == View.VISIBLE) {
528 switchView(mEmergencyShortcutView, mDialpadView, true);
529 return;
530 }
531 super.onBackPressed();
532 }
533
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700534 /**
535 * handle key events
536 */
537 @Override
538 public boolean onKeyDown(int keyCode, KeyEvent event) {
539 switch (keyCode) {
540 // Happen when there's a "Call" hard button.
541 case KeyEvent.KEYCODE_CALL: {
542 if (TextUtils.isEmpty(mDigits.getText().toString())) {
543 // if we are adding a call from the InCallScreen and the phone
544 // number entered is empty, we just close the dialer to expose
545 // the InCallScreen under it.
546 finish();
547 } else {
548 // otherwise, we place the call.
549 placeCall();
550 }
551 return true;
552 }
553 }
554 return super.onKeyDown(keyCode, event);
555 }
556
557 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700558 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700559 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
560 mDigits.onKeyDown(keyCode, event);
561 }
562
563 @Override
564 public boolean onKey(View view, int keyCode, KeyEvent event) {
565 switch (view.getId()) {
566 case R.id.digits:
567 // Happen when "Done" button of the IME is pressed. This can happen when this
568 // Activity is forced into landscape mode due to a desk dock.
569 if (keyCode == KeyEvent.KEYCODE_ENTER
570 && event.getAction() == KeyEvent.ACTION_UP) {
571 placeCall();
572 return true;
573 }
574 break;
575 }
576 return false;
577 }
578
579 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700580 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800581 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700582 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800583 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700584 return handled;
585 }
586
587 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800588 public void onConfirmClick(EmergencyShortcutButton button) {
589 if (button == null) return;
590
Shaotang Li8662a912018-07-04 16:53:01 +0800591 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
592
593 // We interest on the context when user has intention to make phone call,
594 // so write metrics here for shortcut number even the call may not be created.
595 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
596 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
597
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800598 String phoneNumber = button.getPhoneNumber();
599
600 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800601 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Li5d906c12018-07-19 17:32:42 +0800602 Bundle extras = new Bundle();
603 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
604 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800605 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800606 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800607 } else {
608 Log.d(LOG_TAG, "emergency number is empty");
609 }
610 }
611
612 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800613 public void onConfirmClick(EmergencyInfoGroup button) {
614 if (button == null) return;
615
616 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
617 Intent intent = (Intent) button.getTag(R.id.tag_intent);
618 if (intent != null) {
619 startActivity(intent);
620 }
621 }
622
623 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 public void onClick(View view) {
625 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700626 case R.id.deleteButton: {
627 keyPressed(KeyEvent.KEYCODE_DEL);
628 return;
629 }
630 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700631 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700632 placeCall();
633 return;
634 }
635 case R.id.digits: {
636 if (mDigits.length() != 0) {
637 mDigits.setCursorVisible(true);
638 }
639 return;
640 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800641 case R.id.floating_action_button_dialpad: {
Shaotang Li8662a912018-07-04 16:53:01 +0800642 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800643 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800644 switchView(mDialpadView, mEmergencyShortcutView, true);
645 return;
646 }
Yorke Lee23a70732014-08-14 17:12:01 -0700647 }
648 }
649
650 @Override
651 public void onPressed(View view, boolean pressed) {
652 if (!pressed) {
653 return;
654 }
655 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700656 case R.id.one: {
657 playTone(ToneGenerator.TONE_DTMF_1);
658 keyPressed(KeyEvent.KEYCODE_1);
659 return;
660 }
661 case R.id.two: {
662 playTone(ToneGenerator.TONE_DTMF_2);
663 keyPressed(KeyEvent.KEYCODE_2);
664 return;
665 }
666 case R.id.three: {
667 playTone(ToneGenerator.TONE_DTMF_3);
668 keyPressed(KeyEvent.KEYCODE_3);
669 return;
670 }
671 case R.id.four: {
672 playTone(ToneGenerator.TONE_DTMF_4);
673 keyPressed(KeyEvent.KEYCODE_4);
674 return;
675 }
676 case R.id.five: {
677 playTone(ToneGenerator.TONE_DTMF_5);
678 keyPressed(KeyEvent.KEYCODE_5);
679 return;
680 }
681 case R.id.six: {
682 playTone(ToneGenerator.TONE_DTMF_6);
683 keyPressed(KeyEvent.KEYCODE_6);
684 return;
685 }
686 case R.id.seven: {
687 playTone(ToneGenerator.TONE_DTMF_7);
688 keyPressed(KeyEvent.KEYCODE_7);
689 return;
690 }
691 case R.id.eight: {
692 playTone(ToneGenerator.TONE_DTMF_8);
693 keyPressed(KeyEvent.KEYCODE_8);
694 return;
695 }
696 case R.id.nine: {
697 playTone(ToneGenerator.TONE_DTMF_9);
698 keyPressed(KeyEvent.KEYCODE_9);
699 return;
700 }
701 case R.id.zero: {
702 playTone(ToneGenerator.TONE_DTMF_0);
703 keyPressed(KeyEvent.KEYCODE_0);
704 return;
705 }
706 case R.id.pound: {
707 playTone(ToneGenerator.TONE_DTMF_P);
708 keyPressed(KeyEvent.KEYCODE_POUND);
709 return;
710 }
711 case R.id.star: {
712 playTone(ToneGenerator.TONE_DTMF_S);
713 keyPressed(KeyEvent.KEYCODE_STAR);
714 return;
715 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700716 }
717 }
718
719 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700720 * called for long touch events
721 */
722 @Override
723 public boolean onLongClick(View view) {
724 int id = view.getId();
725 switch (id) {
726 case R.id.deleteButton: {
727 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 return true;
729 }
730 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700731 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732 keyPressed(KeyEvent.KEYCODE_PLUS);
733 return true;
734 }
735 }
736 return false;
737 }
738
739 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700740 protected void onStart() {
741 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800742
743 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
744 mUserActions = MetricsWriter.USER_ACTION_NONE;
745 mMetricsWriter.writeMetricsForEnter();
746
Leo Hsu83ab2332018-12-18 15:20:45 +0800747 if (mIsShortcutViewEnabled) {
748 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800749 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
750 updateTheme(false);
751 } else {
752 mColorExtractor.addOnColorsChangedListener(this);
753 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
754 ColorExtractor.TYPE_EXTRA_DARK);
755 // Do not animate when view isn't visible yet, just set an initial state.
756 mBackgroundGradient.setColors(lockScreenColors, false);
757 updateTheme(lockScreenColors.supportsDarkText());
758 }
CY Cheng9a69c182018-06-15 21:20:10 +0800759
Leo Hsu83ab2332018-12-18 15:20:45 +0800760 if (mIsShortcutViewEnabled && mEccInfoHelper != null) {
CY Cheng9a69c182018-06-15 21:20:10 +0800761 final Context context = this;
762 mEccInfoHelper.getCountryEccInfoAsync(context,
763 new EccInfoHelper.CountryEccInfoResultCallback() {
764 @Override
765 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
766 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
767 + Rlog.pii(LOG_TAG, iso));
768 updateLocationAndEccInfo(iso, countryEccInfo);
769 }
770
771 @Override
772 public void onDetectCountryFailed() {
773 Log.w(LOG_TAG, "Cannot detect current country.");
774 updateLocationAndEccInfo(null, null);
775 }
776
777 @Override
778 public void onRetrieveCountryEccInfoFailed(String iso) {
779 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
780 + Rlog.pii(LOG_TAG, iso));
781 updateLocationAndEccInfo(iso, null);
782 }
783 });
784 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700785 }
786
787 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 protected void onResume() {
789 super.onResume();
790
Shaotang Li8662a912018-07-04 16:53:01 +0800791 if (mProximitySensor != null) {
792 mSensorManager.registerListener(
793 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
794 }
795
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 // retrieve the DTMF tone play back setting.
797 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
798 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
799
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 // if the mToneGenerator creation fails, just continue without it. It is
801 // a local audio signal, and is not as important as the dtmf tone itself.
802 synchronized (mToneGeneratorLock) {
803 if (mToneGenerator == null) {
804 try {
805 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
806 TONE_RELATIVE_VOLUME);
807 } catch (RuntimeException e) {
808 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
809 mToneGenerator = null;
810 }
811 }
812 }
813
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 updateDialAndDeleteButtonStateEnabledAttr();
815 }
816
817 @Override
818 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700819 super.onPause();
Shaotang Li8662a912018-07-04 16:53:01 +0800820 if (mProximitySensor != null) {
821 mSensorManager.unregisterListener(this, mProximitySensor);
822 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823 }
824
Lucas Dupineb9c5702017-05-10 16:57:09 -0700825 @Override
826 protected void onStop() {
827 super.onStop();
Shaotang Li8662a912018-07-04 16:53:01 +0800828 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700829 mColorExtractor.removeOnColorsChangedListener(this);
830 }
831
Leo Hsu83ab2332018-12-18 15:20:45 +0800832 private boolean canEnableShortcutView(PersistableBundle carrierConfig) {
833 if (!getResources().getBoolean(R.bool.config_emergency_shortcut_view_enabled)) {
834 // Disables shortcut view by project.
835 return false;
836 }
837 if (!carrierConfig.getBoolean(
838 CarrierConfigManager.KEY_SUPPORT_EMERGENCY_DIALER_SHORTCUT_BOOL)) {
839 Log.d(LOG_TAG, "Disables shortcut view by carrier requirement");
840 return false;
841 }
842 return true;
843 }
844
Lucas Dupineb9c5702017-05-10 16:57:09 -0700845 /**
846 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800847 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700848 * @param supportsDarkText true if gradient supports dark text
849 */
850 private void updateTheme(boolean supportsDarkText) {
851 if (mSupportsDarkText == supportsDarkText) {
852 return;
853 }
854 mSupportsDarkText = supportsDarkText;
855
856 // We can't change themes after inflation, in this case we'll have to recreate
857 // the whole activity.
858 if (mBackgroundGradient != null) {
859 recreate();
860 return;
861 }
862
863 int vis = getWindow().getDecorView().getSystemUiVisibility();
864 if (supportsDarkText) {
865 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
866 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
867 setTheme(R.style.EmergencyDialerThemeDark);
868 } else {
869 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
870 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
871 setTheme(R.style.EmergencyDialerTheme);
872 }
873 getWindow().getDecorView().setSystemUiVisibility(vis);
874 }
875
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700876 /**
877 * place the call, but check to make sure it is a viable number.
878 */
879 private void placeCall() {
Shaotang Li8662a912018-07-04 16:53:01 +0800880 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900882
883 // Convert into emergency number according to emergency conversion map.
884 // If conversion map is not defined (this is default), this method does
885 // nothing and just returns input number.
886 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
887
Yorke Lee36bb2542014-06-05 08:09:52 -0700888 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700889 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
890
891 // place the call if it is a valid number
892 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
893 // There is no number entered.
894 playTone(ToneGenerator.TONE_PROP_NACK);
895 return;
896 }
Shaotang Li8662a912018-07-04 16:53:01 +0800897
898 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
899 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
900
Shaotang Li5d906c12018-07-19 17:32:42 +0800901 Bundle extras = new Bundle();
902 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
903 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnfa77e202018-03-23 07:47:00 -0700904 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800905 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 } else {
907 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
908
Shaotang Li8662a912018-07-04 16:53:01 +0800909 // We interest on the context when user has intention to make phone call,
910 // so write metrics here for non-emergency numbers even these numbers are rejected.
911 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
912 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
913
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
915 }
Yorke Lee9b341512014-10-17 11:36:41 -0700916 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700917 }
918
919 /**
920 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
921 *
922 * The tone is played locally, using the audio stream for phone calls.
923 * Tones are played only if the "Audible touch tones" user preference
924 * is checked, and are NOT played if the device is in silent mode.
925 *
926 * @param tone a tone code from {@link ToneGenerator}
927 */
928 void playTone(int tone) {
929 // if local tone playback is disabled, just return.
930 if (!mDTMFToneEnabled) {
931 return;
932 }
933
934 // Also do nothing if the phone is in silent mode.
935 // We need to re-check the ringer mode for *every* playTone()
936 // call, rather than keeping a local flag that's updated in
937 // onResume(), since it's possible to toggle silent mode without
938 // leaving the current activity (via the ENDCALL-longpress menu.)
939 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
940 int ringerMode = audioManager.getRingerMode();
941 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800942 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 return;
944 }
945
946 synchronized (mToneGeneratorLock) {
947 if (mToneGenerator == null) {
948 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
949 return;
950 }
951
952 // Start the new tone (will stop any playing tone)
953 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
954 }
955 }
956
957 private CharSequence createErrorMessage(String number) {
958 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800959 String errorString = getString(R.string.dial_emergency_error, number);
960 int startingPosition = errorString.indexOf(number);
961 int endingPosition = startingPosition + number.length();
962 Spannable result = new SpannableString(errorString);
963 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
964 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 } else {
966 return getText(R.string.dial_emergency_empty_error).toString();
967 }
968 }
969
970 @Override
971 protected Dialog onCreateDialog(int id) {
972 AlertDialog dialog = null;
973 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
974 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700975 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
977 .setMessage(createErrorMessage(mLastNumber))
978 .setPositiveButton(R.string.ok, null)
979 .setCancelable(true).create();
980
981 // blur stuff behind the dialog
982 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700983 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700984 }
985 return dialog;
986 }
987
988 @Override
989 protected void onPrepareDialog(int id, Dialog dialog) {
990 super.onPrepareDialog(id, dialog);
991 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
992 AlertDialog alert = (AlertDialog) dialog;
993 alert.setMessage(createErrorMessage(mLastNumber));
994 }
995 }
996
Andrew Leed5631e82014-10-08 16:03:58 -0700997 @Override
998 public boolean onOptionsItemSelected(MenuItem item) {
999 final int itemId = item.getItemId();
1000 if (itemId == android.R.id.home) {
1001 onBackPressed();
1002 return true;
1003 }
1004 return super.onOptionsItemSelected(item);
1005 }
1006
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007 /**
1008 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
1009 */
1010 private void updateDialAndDeleteButtonStateEnabledAttr() {
1011 final boolean notEmpty = mDigits.length() != 0;
1012
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 mDelete.setEnabled(notEmpty);
1014 }
Yorke Lee91311662014-10-24 14:50:45 -07001015
1016 /**
1017 * Remove the digit just before the current position. Used by various long pressed callbacks
1018 * to remove the digit that was populated as a result of the short click.
1019 */
1020 private void removePreviousDigitIfPossible() {
1021 final int currentPosition = mDigits.getSelectionStart();
1022 if (currentPosition > 0) {
1023 mDigits.setSelection(currentPosition);
1024 mDigits.getText().delete(currentPosition - 1, currentPosition);
1025 }
1026 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -08001027
1028 /**
1029 * Update the text-to-speech annotations in the edit field.
1030 */
1031 private void updateTtsSpans() {
1032 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
1033 mDigits.getText().removeSpan(o);
1034 }
1035 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
1036 }
Lucas Dupineb9c5702017-05-10 16:57:09 -07001037
1038 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -07001039 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -07001040 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -07001041 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1042 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001043 mBackgroundGradient.setColors(colors);
1044 updateTheme(colors.supportsDarkText());
1045 }
1046 }
Tyler Gunnc5428972018-03-28 14:15:34 -07001047
1048 /**
1049 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1050 * add hint text above the dial pad which warns the user of this case.
1051 */
1052 private void maybeShowWfcEmergencyCallingWarning() {
1053 if (!mIsWfcEmergencyCallingWarningEnabled) {
1054 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1055 return;
1056 }
1057
1058 // Use an async task rather than calling into Telephony on UI thread.
1059 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1060 @Override
1061 protected Boolean doInBackground(Void... voids) {
1062 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1063 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1064 ServiceState ss = tm.getServiceState();
1065 boolean isCellAvailable =
1066 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1067 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +08001068 + " isCellAvailable=" + isCellAvailable
1069 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -07001070 return isWfcAvailable && !isCellAvailable;
1071 }
1072
1073 @Override
1074 protected void onPostExecute(Boolean result) {
1075 if (result.booleanValue()) {
1076 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1077 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1078 } else {
1079 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1080 mDigits.setHint("");
1081 }
1082 maybeChangeHintSize();
1083 }
1084 };
1085 showWfcWarningTask.execute((Void) null);
1086 }
1087
1088 /**
1089 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1090 * edit view and set the font size to a smaller size appropriate for the emergency calling
1091 * warning.
1092 */
1093 private void maybeChangeHintSize() {
1094 if (TextUtils.isEmpty(mDigits.getHint())
1095 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1096 // No hint or there are dialed digits, so use default size.
1097 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1098 // By default, the digits view auto-resizes to fit the text it contains, so
1099 // enable that now.
1100 mDigits.setResizeEnabled(true);
1101 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1102 } else {
1103 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1104 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1105 R.dimen.emergency_call_warning_size));
1106 // Since we're populating this with a static text string, disable auto-resize.
1107 mDigits.setResizeEnabled(false);
1108 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1109 }
1110 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001111
1112 private void setupEmergencyShortcutsView() {
1113 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1114 mDialpadView = findViewById(R.id.emergency_dialer);
1115
Chihhang Chuangcaba0da2018-08-02 22:25:06 +08001116 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1117 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1118
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001119 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1120 dialpadButton.setOnClickListener(this);
1121
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001122 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001123
1124 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1125 mEmergencyActionGroup.setVisibility(View.GONE);
1126
1127 // Setup dialpad title.
1128 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1129 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1130
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001131 mEmergencyShortcutButtonList = new ArrayList<>();
1132 setupEmergencyCallShortcutButton();
1133
CY Cheng9a69c182018-06-15 21:20:10 +08001134 updateLocationAndEccInfo(null, null);
1135
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001136 switchView(mEmergencyShortcutView, mDialpadView, false);
1137 }
1138
CY Cheng9a69c182018-06-15 21:20:10 +08001139 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001140 final View locationInfo = findViewById(R.id.location_info);
1141
CY Cheng9a69c182018-06-15 21:20:10 +08001142 String countryName = null;
1143 if (!TextUtils.isEmpty(countryIso)) {
1144 Locale locale = Locale.getDefault();
1145 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1146 .getDisplayCountry();
1147 }
1148 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001149 locationInfo.setVisibility(View.INVISIBLE);
1150 } else {
1151 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001152 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001153 locationInfo.setVisibility(View.VISIBLE);
1154 }
1155 }
1156
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001157 private void setupEmergencyCallShortcutButton() {
1158 final ViewGroup shortcutButtonContainer = findViewById(
1159 R.id.emergency_shortcut_buttons_container);
1160 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001161 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001162
CY Cheng9a69c182018-06-15 21:20:10 +08001163 mShortcutAdapter = new EccShortcutAdapter(this) {
1164 @Override
1165 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1166 CharSequence description, int iconRes) {
1167 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1168 .inflate(R.layout.emergency_shortcut_button, parent, false);
1169 button.setPhoneNumber(number);
1170 button.setPhoneDescription(description);
1171 button.setPhoneTypeIcon(iconRes);
1172 button.setOnConfirmClickListener(EmergencyDialer.this);
1173 return button;
1174 }
1175 };
1176 mShortcutDataSetObserver = new DataSetObserver() {
1177 @Override
1178 public void onChanged() {
1179 super.onChanged();
1180 updateLayout();
1181 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001182
CY Cheng9a69c182018-06-15 21:20:10 +08001183 @Override
1184 public void onInvalidated() {
1185 super.onInvalidated();
1186 updateLayout();
1187 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001188
CY Cheng9a69c182018-06-15 21:20:10 +08001189 private void updateLayout() {
1190 // clear previous added buttons
1191 shortcutButtonContainer.removeAllViews();
1192 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001193
CY Cheng9a69c182018-06-15 21:20:10 +08001194 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1195 EmergencyShortcutButton button = (EmergencyShortcutButton)
1196 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1197 mEmergencyShortcutButtonList.add(button);
1198 shortcutButtonContainer.addView(button);
1199 }
1200
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001201 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001202 if (mEmergencyShortcutButtonList.size() > 1) {
1203 emergencyNumberTitle.setText(getString(
1204 R.string.numerous_emergency_numbers_title));
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001205 // Update mEmergencyInfoGroup margin to avoid UI overlay when
1206 // emergency shortcut button more than 2.
1207 if (mEmergencyShortcutButtonList.size() > 2) {
1208 mEmergencyInfoGroup.updateLayoutMargin();
1209 }
CY Cheng9a69c182018-06-15 21:20:10 +08001210 } else {
1211 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1212 }
1213 }
1214 };
1215 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1216 }
1217
1218 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1219 if (!isFinishing() && !isDestroyed()) {
1220 setLocationInfo(iso);
1221 if (mShortcutAdapter != null) {
1222 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1223 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001224 }
1225 }
1226
1227 /**
1228 * Called by the activity before a touch event is dispatched to the view hierarchy.
1229 */
1230 private void onPreTouchEvent(MotionEvent event) {
1231 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001232 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001233
1234 if (mEmergencyShortcutButtonList != null) {
1235 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1236 button.onPreTouchEvent(event);
1237 }
1238 }
1239 }
1240
1241 /**
1242 * Called by the activity after a touch event is dispatched to the view hierarchy.
1243 */
1244 private void onPostTouchEvent(MotionEvent event) {
1245 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001246 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001247
1248 if (mEmergencyShortcutButtonList != null) {
1249 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1250 button.onPostTouchEvent(event);
1251 }
1252 }
1253 }
1254
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001255 /**
1256 * Switch two view.
1257 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001258 * @param displayView the view would be displayed.
1259 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001260 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1261 */
1262 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1263 if (displayView == null || hideView == null) {
1264 return;
1265 }
1266
1267 if (displayView.getVisibility() == View.VISIBLE) {
1268 return;
1269 }
1270
1271 if (hasAnimation) {
1272 crossfade(hideView, displayView);
1273 } else {
1274 hideView.setVisibility(View.GONE);
1275 displayView.setVisibility(View.VISIBLE);
1276 }
1277 }
1278
1279 /**
1280 * Fade out and fade in animation between two view transition.
1281 */
1282 private void crossfade(View fadeOutView, View fadeInView) {
1283 if (fadeOutView == null || fadeInView == null) {
1284 return;
1285 }
1286 final int shortAnimationDuration = getResources().getInteger(
1287 android.R.integer.config_shortAnimTime);
1288
1289 fadeInView.setAlpha(0f);
1290 fadeInView.setVisibility(View.VISIBLE);
1291
1292 fadeInView.animate()
1293 .alpha(1f)
1294 .setDuration(shortAnimationDuration)
1295 .setListener(null);
1296
1297 fadeOutView.animate()
1298 .alpha(0f)
1299 .setDuration(shortAnimationDuration)
1300 .setListener(new AnimatorListenerAdapter() {
1301 @Override
1302 public void onAnimationEnd(Animator animation) {
1303 fadeOutView.setVisibility(View.GONE);
1304 }
1305 });
1306 }
Shaotang Li8662a912018-07-04 16:53:01 +08001307
1308 @Override
1309 public void onSensorChanged(SensorEvent event) {
1310 float distance = event.values[0];
1311 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1312 }
1313
1314 @Override
1315 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1316 // Not used.
1317 }
1318
1319 private boolean isShortcutNumber(String number) {
1320 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1321 return false;
1322 }
1323
1324 boolean isShortcut = false;
1325 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1326 if (button != null && number.equals(button.getPhoneNumber())) {
1327 isShortcut = true;
1328 break;
1329 }
1330 }
1331 return isShortcut;
1332 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333}