blob: 04c703f432b79f478786229402011cf12bb56f60 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunn1acf54a2018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangd301d9a2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Shaotang Li3b02aeb2018-07-04 16:53:01 +080026import android.app.KeyguardManager;
Lucas Dupineb9c5702017-05-10 16:57:09 -070027import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
CY Cheng42873672018-06-15 21:20:10 +080032import android.database.DataSetObserver;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +080033import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Shaotang Li3b02aeb2018-07-04 16:53:01 +080035import android.hardware.Sensor;
36import android.hardware.SensorEvent;
37import android.hardware.SensorEventListener;
38import android.hardware.SensorManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.media.AudioManager;
40import android.media.ToneGenerator;
Shaotang Li3b02aeb2018-07-04 16:53:01 +080041import android.metrics.LogMaker;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.net.Uri;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070043import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070045import android.os.PersistableBundle;
Shaotang Li3b02aeb2018-07-04 16:53:01 +080046import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.provider.Settings;
Shaotang Lif27c74d2018-07-19 17:32:42 +080048import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070049import android.telecom.PhoneAccount;
Tyler Gunnca7dfef2018-03-27 18:50:05 +000050import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.PhoneNumberUtils;
Billy Chi9614de82018-06-15 19:00:15 +080053import android.telephony.Rlog;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070054import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070055import android.telephony.SubscriptionManager;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070056import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070058import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080059import android.text.Spannable;
60import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
62import android.text.TextWatcher;
63import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080064import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.util.Log;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070066import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070067import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070069import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070070import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.view.View;
Chihhang Chuang96fcafe2018-08-02 22:25:06 +080072import android.view.View.AccessibilityDelegate;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080073import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import android.view.WindowManager;
Chihhang Chuang96fcafe2018-08-02 22:25:06 +080075import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang4f2b7812018-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 Li3b02aeb2018-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 Gunn1acf54a2018-03-28 14:15:34 -070085import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng42873672018-06-15 21:20:10 +080086import com.android.phone.ecc.CountryEccInfo;
87import com.android.phone.ecc.EccInfoHelper;
Leo Hsu8f1c6712018-07-10 11:37:52 +080088import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080090import java.util.ArrayList;
91import java.util.List;
CY Cheng42873672018-06-15 21:20:10 +080092import java.util.Locale;
Chihhang Chuang4f2b7812018-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:
99 * 1. Allows ONLY emergency calls to be dialed
100 * 2. Disallows voicemail functionality
101 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
102 * activity to stay in front of the keyguard.
103 *
104 * TODO: Even though this is an ultra-simplified version of the normal
105 * dialer, there's still lots of code duplication between this class and
106 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
107 * moved into a shared base class that would live in the framework?
108 * Or could we figure out some way to move *this* class into apps/Contacts
109 * also?
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800110 *
111 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800112 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
113 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800114 * TODO item:
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800115 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116 */
117public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700118 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800119 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800120 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener {
121
122 private class MetricsWriter {
123 // Metrics constants indicating the entry type that user opened emergency dialer.
124 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
125 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
126 public static final int ENTRY_TYPE_UNKNOWN = 0;
127 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
128 public static final int ENTRY_TYPE_POWER_MENU = 2;
129
130 // Metrics constants indicating the UI that user made phone call.
131 public static final int CALL_SOURCE_DIALPAD = 0;
132 public static final int CALL_SOURCE_SHORTCUT = 1;
133
134 // Metrics constants indicating the phone number type of a call user made.
135 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
136 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
137
138 // Metrics constants indicating the actions performed by user.
139 public static final int USER_ACTION_NONE = 0x0;
140 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
141 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
142 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
143 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
144
145 private MetricsLogger mMetricsLogger = new MetricsLogger();
146
147 public void writeMetricsForEnter() {
148 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
149 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
150 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
151 .setType(MetricsEvent.TYPE_OPEN)
152 .setSubtype(entryType)
153 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
154 keyguard.isKeyguardLocked() ? 1 : 0));
155 }
156
157 public void writeMetricsForExit() {
158 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
159 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
160 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
161 .setType(MetricsEvent.TYPE_CLOSE)
162 .setSubtype(entryType)
163 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
164 .addTaggedData(
165 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
166 }
167
168 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
169 boolean hasShortcut) {
170 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
171 .setType(MetricsEvent.TYPE_ACTION)
172 .setSubtype(callSource)
173 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
174 phoneNumberType)
175 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
176 hasShortcut ? 1 : 0)
177 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
178 mIsProximityNear ? 1 : 0));
179 }
180 }
181
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 // Keys used with onSaveInstanceState().
183 private static final String LAST_NUMBER = "lastNumber";
184
185 // Intent action for this activity.
186 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
187
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800188 /**
189 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
190 * the emergency dialer.
191 */
192 public static final String EXTRA_ENTRY_TYPE =
193 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
194
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 // List of dialer button IDs.
196 private static final int[] DIALER_KEYS = new int[] {
197 R.id.one, R.id.two, R.id.three,
198 R.id.four, R.id.five, R.id.six,
199 R.id.seven, R.id.eight, R.id.nine,
200 R.id.star, R.id.zero, R.id.pound };
201
202 // Debug constants.
203 private static final boolean DBG = false;
204 private static final String LOG_TAG = "EmergencyDialer";
205
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206 /** The length of DTMF tones in milliseconds */
207 private static final int TONE_LENGTH_MS = 150;
208
209 /** The DTMF tone volume relative to other sounds in the stream */
210 private static final int TONE_RELATIVE_VOLUME = 80;
211
212 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
213 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
214
215 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
216
Lucas Dupineb9c5702017-05-10 16:57:09 -0700217 /** 90% opacity, different from other gradients **/
218 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
219
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800220 /** 85% opacity for black background **/
221 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
222
CY Cheng42873672018-06-15 21:20:10 +0800223 /** Size limit of emergency shortcut buttons container. **/
224 private static final int SHORTCUT_SIZE_LIMIT = 3;
225
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700226 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 private View mDialButton;
228 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800229 private View mEmergencyShortcutView;
230 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700231
CY Cheng42873672018-06-15 21:20:10 +0800232 private EccInfoHelper mEccInfoHelper;
233
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800234 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800235 private EccShortcutAdapter mShortcutAdapter;
236 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800237
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 private ToneGenerator mToneGenerator;
239 private Object mToneGeneratorLock = new Object();
240
241 // determines if we want to playback local DTMF tones.
242 private boolean mDTMFToneEnabled;
243
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700244 private EmergencyActionGroup mEmergencyActionGroup;
245
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 // close activity when screen turns off
247 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
248 @Override
249 public void onReceive(Context context, Intent intent) {
250 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700251 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 }
253 }
254 };
255
Chihhang Chuang96fcafe2018-08-02 22:25:06 +0800256 /**
257 * Customize accessibility methods in View.
258 */
259 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
260
261 /**
262 * Stop AccessiblityService from reading the title of a hidden View.
263 *
264 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
265 * in the animation end. The view with an accessibility pane title would call the
266 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
267 * accessibility service to read the pane title of fade out view instead of pane title of
268 * fade in view. So it need to filter out the event called by vanished pane.
269 */
270 @Override
271 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
272 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
273 && host.getVisibility() == View.GONE) {
274 return;
275 }
276 super.onPopulateAccessibilityEvent(host, event);
277 }
278 };
279
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
281
Lucas Dupineb9c5702017-05-10 16:57:09 -0700282 // Background gradient
283 private ColorExtractor mColorExtractor;
284 private GradientDrawable mBackgroundGradient;
285 private boolean mSupportsDarkText;
286
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700287 private boolean mIsWfcEmergencyCallingWarningEnabled;
288 private float mDefaultDigitsTextSize;
289
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800290 private boolean mAreEmergencyDialerShortcutsEnabled;
291
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800292 private MetricsWriter mMetricsWriter;
293 private SensorManager mSensorManager;
294 private Sensor mProximitySensor;
295 private boolean mIsProximityNear = false;
296
297 /**
298 * The time, in millis, since boot when user opened emergency dialer.
299 * This is used when calculating the user stay duration for metrics data.
300 */
301 private long mUserEnterTimeMillis = 0;
302
303 /**
304 * Bit flag indicating the actions performed by user. This is used for metrics data.
305 */
306 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
307
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 @Override
309 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
310 // Do nothing
311 }
312
313 @Override
314 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700315 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 }
317
318 @Override
319 public void afterTextChanged(Editable input) {
320 // Check for special sequences, in particular the "**04" or "**05"
321 // sequences that allow you to enter PIN or PUK-related codes.
322 //
323 // But note we *don't* allow most other special sequences here,
324 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
325 // since those shouldn't be available if the device is locked.
326 //
327 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
328 // here, not the regular handleChars() method.
329 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
330 // A special sequence was entered, clear the digits
331 mDigits.getText().clear();
332 }
333
334 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800335 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 }
337
338 @Override
339 protected void onCreate(Bundle icicle) {
340 super.onCreate(icicle);
341
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800342 mMetricsWriter = new MetricsWriter();
343 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
344 if (mSensorManager != null) {
345 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
346 }
347
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 // Allow this activity to be displayed in front of the keyguard / lockscreen.
349 WindowManager.LayoutParams lp = getWindow().getAttributes();
350 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700351
352 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700353 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700354 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
355
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356 getWindow().setAttributes(lp);
357
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800358 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
359 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng42873672018-06-15 21:20:10 +0800360 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
361 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800362
Lucas Dupineb9c5702017-05-10 16:57:09 -0700363 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800364
365 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
366 // And the background color is black with 85% opacity.
367 if (mAreEmergencyDialerShortcutsEnabled) {
368 updateTheme(false);
369 } else {
370 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
371 ColorExtractor.TYPE_EXTRA_DARK);
372 updateTheme(lockScreenColors.supportsDarkText());
373 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700374
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 setContentView(R.layout.emergency_dialer);
376
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700377 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700378 mDigits.setKeyListener(DialerKeyListener.getInstance());
379 mDigits.setOnClickListener(this);
380 mDigits.setOnKeyListener(this);
381 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700382 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700383 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 maybeAddNumberFormatting();
385
Lucas Dupineb9c5702017-05-10 16:57:09 -0700386 mBackgroundGradient = new GradientDrawable(this);
387 Point displaySize = new Point();
388 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
389 .getDefaultDisplay().getSize(displaySize);
390 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800391 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
392 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700393 getWindow().setBackgroundDrawable(mBackgroundGradient);
394
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700395 // Check for the presence of the keypad
396 View view = findViewById(R.id.one);
397 if (view != null) {
398 setupKeypad();
399 }
400
401 mDelete = findViewById(R.id.deleteButton);
402 mDelete.setOnClickListener(this);
403 mDelete.setOnLongClickListener(this);
404
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700405 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700406
407 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700408 // Read carrier config through the public API because PhoneGlobals is not available when we
409 // run as a secondary user.
410 CarrierConfigManager configMgr =
411 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
412 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800413 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700414
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700415 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 mDialButton.setOnClickListener(this);
417 } else {
418 mDialButton.setVisibility(View.GONE);
419 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700420 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
421 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
422 maybeShowWfcEmergencyCallingWarning();
423
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700424 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700425
426 if (icicle != null) {
427 super.onRestoreInstanceState(icicle);
428 }
429
430 // Extract phone number from intent
431 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700432 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
434 if (number != null) {
435 mDigits.setText(number);
436 }
437 }
438
439 // if the mToneGenerator creation fails, just continue without it. It is
440 // a local audio signal, and is not as important as the dtmf tone itself.
441 synchronized (mToneGeneratorLock) {
442 if (mToneGenerator == null) {
443 try {
444 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
445 } catch (RuntimeException e) {
446 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
447 mToneGenerator = null;
448 }
449 }
450 }
451
452 final IntentFilter intentFilter = new IntentFilter();
453 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
454 registerReceiver(mBroadcastReceiver, intentFilter);
455
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700456 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800457
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800458 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu8f1c6712018-07-10 11:37:52 +0800459 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800460 setupEmergencyShortcutsView();
461 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700462 }
463
464 @Override
465 protected void onDestroy() {
466 super.onDestroy();
467 synchronized (mToneGeneratorLock) {
468 if (mToneGenerator != null) {
469 mToneGenerator.release();
470 mToneGenerator = null;
471 }
472 }
473 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800474 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
475 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
476 mShortcutDataSetObserver = null;
477 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700478 }
479
480 @Override
481 protected void onRestoreInstanceState(Bundle icicle) {
482 mLastNumber = icicle.getString(LAST_NUMBER);
483 }
484
485 @Override
486 protected void onSaveInstanceState(Bundle outState) {
487 super.onSaveInstanceState(outState);
488 outState.putString(LAST_NUMBER, mLastNumber);
489 }
490
491 /**
492 * Explicitly turn off number formatting, since it gets in the way of the emergency
493 * number detector
494 */
495 protected void maybeAddNumberFormatting() {
496 // Do nothing.
497 }
498
499 @Override
500 protected void onPostCreate(Bundle savedInstanceState) {
501 super.onPostCreate(savedInstanceState);
502
503 // This can't be done in onCreate(), since the auto-restoring of the digits
504 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
505 // is called. This method will be called every time the activity is created, and
506 // will always happen after onRestoreSavedInstanceState().
507 mDigits.addTextChangedListener(this);
508 }
509
510 private void setupKeypad() {
511 // Setup the listeners for the buttons
512 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700513 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
514 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 }
516
517 View view = findViewById(R.id.zero);
518 view.setOnLongClickListener(this);
519 }
520
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800521 @Override
522 public void onBackPressed() {
523 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800524 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800525 // Otherwise, it would finish the activity.
526 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
527 && 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 Chuang4f2b7812018-06-07 16:25:27 +0800581 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700582 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800583 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700584 return handled;
585 }
586
587 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800588 public void onConfirmClick(EmergencyShortcutButton button) {
589 if (button == null) return;
590
Shaotang Li3b02aeb2018-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 Chuang4f2b7812018-06-07 16:25:27 +0800598 String phoneNumber = button.getPhoneNumber();
599
600 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800601 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Lif27c74d2018-07-19 17:32:42 +0800602 Bundle extras = new Bundle();
603 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
604 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800605 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800606 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800607 } else {
608 Log.d(LOG_TAG, "emergency number is empty");
609 }
610 }
611
612 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700613 public void onClick(View view) {
614 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700615 case R.id.deleteButton: {
616 keyPressed(KeyEvent.KEYCODE_DEL);
617 return;
618 }
619 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700620 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700621 placeCall();
622 return;
623 }
624 case R.id.digits: {
625 if (mDigits.length() != 0) {
626 mDigits.setCursorVisible(true);
627 }
628 return;
629 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800630 case R.id.floating_action_button_dialpad: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800631 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800632 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800633 switchView(mDialpadView, mEmergencyShortcutView, true);
634 return;
635 }
636 case R.id.emergency_info_button: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800637 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800638 Intent intent = (Intent) view.getTag(R.id.tag_intent);
639 if (intent != null) {
640 startActivity(intent);
641 }
642 return;
643 }
Yorke Lee23a70732014-08-14 17:12:01 -0700644 }
645 }
646
647 @Override
648 public void onPressed(View view, boolean pressed) {
649 if (!pressed) {
650 return;
651 }
652 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700653 case R.id.one: {
654 playTone(ToneGenerator.TONE_DTMF_1);
655 keyPressed(KeyEvent.KEYCODE_1);
656 return;
657 }
658 case R.id.two: {
659 playTone(ToneGenerator.TONE_DTMF_2);
660 keyPressed(KeyEvent.KEYCODE_2);
661 return;
662 }
663 case R.id.three: {
664 playTone(ToneGenerator.TONE_DTMF_3);
665 keyPressed(KeyEvent.KEYCODE_3);
666 return;
667 }
668 case R.id.four: {
669 playTone(ToneGenerator.TONE_DTMF_4);
670 keyPressed(KeyEvent.KEYCODE_4);
671 return;
672 }
673 case R.id.five: {
674 playTone(ToneGenerator.TONE_DTMF_5);
675 keyPressed(KeyEvent.KEYCODE_5);
676 return;
677 }
678 case R.id.six: {
679 playTone(ToneGenerator.TONE_DTMF_6);
680 keyPressed(KeyEvent.KEYCODE_6);
681 return;
682 }
683 case R.id.seven: {
684 playTone(ToneGenerator.TONE_DTMF_7);
685 keyPressed(KeyEvent.KEYCODE_7);
686 return;
687 }
688 case R.id.eight: {
689 playTone(ToneGenerator.TONE_DTMF_8);
690 keyPressed(KeyEvent.KEYCODE_8);
691 return;
692 }
693 case R.id.nine: {
694 playTone(ToneGenerator.TONE_DTMF_9);
695 keyPressed(KeyEvent.KEYCODE_9);
696 return;
697 }
698 case R.id.zero: {
699 playTone(ToneGenerator.TONE_DTMF_0);
700 keyPressed(KeyEvent.KEYCODE_0);
701 return;
702 }
703 case R.id.pound: {
704 playTone(ToneGenerator.TONE_DTMF_P);
705 keyPressed(KeyEvent.KEYCODE_POUND);
706 return;
707 }
708 case R.id.star: {
709 playTone(ToneGenerator.TONE_DTMF_S);
710 keyPressed(KeyEvent.KEYCODE_STAR);
711 return;
712 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 }
714 }
715
716 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 * called for long touch events
718 */
719 @Override
720 public boolean onLongClick(View view) {
721 int id = view.getId();
722 switch (id) {
723 case R.id.deleteButton: {
724 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 return true;
726 }
727 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700728 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 keyPressed(KeyEvent.KEYCODE_PLUS);
730 return true;
731 }
732 }
733 return false;
734 }
735
736 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700737 protected void onStart() {
738 super.onStart();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800739
740 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
741 mUserActions = MetricsWriter.USER_ACTION_NONE;
742 mMetricsWriter.writeMetricsForEnter();
743
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800744 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
745 // And set background color to black.
746 if (mAreEmergencyDialerShortcutsEnabled) {
747 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
748 updateTheme(false);
749 } else {
750 mColorExtractor.addOnColorsChangedListener(this);
751 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
752 ColorExtractor.TYPE_EXTRA_DARK);
753 // Do not animate when view isn't visible yet, just set an initial state.
754 mBackgroundGradient.setColors(lockScreenColors, false);
755 updateTheme(lockScreenColors.supportsDarkText());
756 }
CY Cheng42873672018-06-15 21:20:10 +0800757
758 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
759 final Context context = this;
760 mEccInfoHelper.getCountryEccInfoAsync(context,
761 new EccInfoHelper.CountryEccInfoResultCallback() {
762 @Override
763 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
764 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
765 + Rlog.pii(LOG_TAG, iso));
766 updateLocationAndEccInfo(iso, countryEccInfo);
767 }
768
769 @Override
770 public void onDetectCountryFailed() {
771 Log.w(LOG_TAG, "Cannot detect current country.");
772 updateLocationAndEccInfo(null, null);
773 }
774
775 @Override
776 public void onRetrieveCountryEccInfoFailed(String iso) {
777 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
778 + Rlog.pii(LOG_TAG, iso));
779 updateLocationAndEccInfo(iso, null);
780 }
781 });
782 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700783 }
784
785 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 protected void onResume() {
787 super.onResume();
788
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800789 if (mProximitySensor != null) {
790 mSensorManager.registerListener(
791 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
792 }
793
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 // retrieve the DTMF tone play back setting.
795 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
796 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
797
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700798 // if the mToneGenerator creation fails, just continue without it. It is
799 // a local audio signal, and is not as important as the dtmf tone itself.
800 synchronized (mToneGeneratorLock) {
801 if (mToneGenerator == null) {
802 try {
803 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
804 TONE_RELATIVE_VOLUME);
805 } catch (RuntimeException e) {
806 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
807 mToneGenerator = null;
808 }
809 }
810 }
811
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700812 updateDialAndDeleteButtonStateEnabledAttr();
813 }
814
815 @Override
816 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 super.onPause();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800818 if (mProximitySensor != null) {
819 mSensorManager.unregisterListener(this, mProximitySensor);
820 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 }
822
Lucas Dupineb9c5702017-05-10 16:57:09 -0700823 @Override
824 protected void onStop() {
825 super.onStop();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800826 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700827 mColorExtractor.removeOnColorsChangedListener(this);
828 }
829
830 /**
831 * Sets theme based on gradient colors
832 * @param supportsDarkText true if gradient supports dark text
833 */
834 private void updateTheme(boolean supportsDarkText) {
835 if (mSupportsDarkText == supportsDarkText) {
836 return;
837 }
838 mSupportsDarkText = supportsDarkText;
839
840 // We can't change themes after inflation, in this case we'll have to recreate
841 // the whole activity.
842 if (mBackgroundGradient != null) {
843 recreate();
844 return;
845 }
846
847 int vis = getWindow().getDecorView().getSystemUiVisibility();
848 if (supportsDarkText) {
849 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
850 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
851 setTheme(R.style.EmergencyDialerThemeDark);
852 } else {
853 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
854 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
855 setTheme(R.style.EmergencyDialerTheme);
856 }
857 getWindow().getDecorView().setSystemUiVisibility(vis);
858 }
859
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 /**
861 * place the call, but check to make sure it is a viable number.
862 */
863 private void placeCall() {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800864 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900866
867 // Convert into emergency number according to emergency conversion map.
868 // If conversion map is not defined (this is default), this method does
869 // nothing and just returns input number.
870 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
871
Yorke Lee36bb2542014-06-05 08:09:52 -0700872 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700873 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
874
875 // place the call if it is a valid number
876 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
877 // There is no number entered.
878 playTone(ToneGenerator.TONE_PROP_NACK);
879 return;
880 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800881
882 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
883 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
884
Shaotang Lif27c74d2018-07-19 17:32:42 +0800885 Bundle extras = new Bundle();
886 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
887 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000888 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800889 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700890 } else {
891 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
892
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800893 // We interest on the context when user has intention to make phone call,
894 // so write metrics here for non-emergency numbers even these numbers are rejected.
895 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
896 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
897
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700898 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
899 }
Yorke Lee9b341512014-10-17 11:36:41 -0700900 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 }
902
903 /**
904 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
905 *
906 * The tone is played locally, using the audio stream for phone calls.
907 * Tones are played only if the "Audible touch tones" user preference
908 * is checked, and are NOT played if the device is in silent mode.
909 *
910 * @param tone a tone code from {@link ToneGenerator}
911 */
912 void playTone(int tone) {
913 // if local tone playback is disabled, just return.
914 if (!mDTMFToneEnabled) {
915 return;
916 }
917
918 // Also do nothing if the phone is in silent mode.
919 // We need to re-check the ringer mode for *every* playTone()
920 // call, rather than keeping a local flag that's updated in
921 // onResume(), since it's possible to toggle silent mode without
922 // leaving the current activity (via the ENDCALL-longpress menu.)
923 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
924 int ringerMode = audioManager.getRingerMode();
925 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
926 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
927 return;
928 }
929
930 synchronized (mToneGeneratorLock) {
931 if (mToneGenerator == null) {
932 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
933 return;
934 }
935
936 // Start the new tone (will stop any playing tone)
937 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
938 }
939 }
940
941 private CharSequence createErrorMessage(String number) {
942 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800943 String errorString = getString(R.string.dial_emergency_error, number);
944 int startingPosition = errorString.indexOf(number);
945 int endingPosition = startingPosition + number.length();
946 Spannable result = new SpannableString(errorString);
947 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
948 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 } else {
950 return getText(R.string.dial_emergency_empty_error).toString();
951 }
952 }
953
954 @Override
955 protected Dialog onCreateDialog(int id) {
956 AlertDialog dialog = null;
957 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
958 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700959 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
961 .setMessage(createErrorMessage(mLastNumber))
962 .setPositiveButton(R.string.ok, null)
963 .setCancelable(true).create();
964
965 // blur stuff behind the dialog
966 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700967 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 }
969 return dialog;
970 }
971
972 @Override
973 protected void onPrepareDialog(int id, Dialog dialog) {
974 super.onPrepareDialog(id, dialog);
975 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
976 AlertDialog alert = (AlertDialog) dialog;
977 alert.setMessage(createErrorMessage(mLastNumber));
978 }
979 }
980
Andrew Leed5631e82014-10-08 16:03:58 -0700981 @Override
982 public boolean onOptionsItemSelected(MenuItem item) {
983 final int itemId = item.getItemId();
984 if (itemId == android.R.id.home) {
985 onBackPressed();
986 return true;
987 }
988 return super.onOptionsItemSelected(item);
989 }
990
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 /**
992 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
993 */
994 private void updateDialAndDeleteButtonStateEnabledAttr() {
995 final boolean notEmpty = mDigits.length() != 0;
996
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 mDelete.setEnabled(notEmpty);
998 }
Yorke Lee91311662014-10-24 14:50:45 -0700999
1000 /**
1001 * Remove the digit just before the current position. Used by various long pressed callbacks
1002 * to remove the digit that was populated as a result of the short click.
1003 */
1004 private void removePreviousDigitIfPossible() {
1005 final int currentPosition = mDigits.getSelectionStart();
1006 if (currentPosition > 0) {
1007 mDigits.setSelection(currentPosition);
1008 mDigits.getText().delete(currentPosition - 1, currentPosition);
1009 }
1010 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -08001011
1012 /**
1013 * Update the text-to-speech annotations in the edit field.
1014 */
1015 private void updateTtsSpans() {
1016 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
1017 mDigits.getText().removeSpan(o);
1018 }
1019 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
1020 }
Lucas Dupineb9c5702017-05-10 16:57:09 -07001021
1022 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -07001023 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -07001024 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -07001025 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1026 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001027 mBackgroundGradient.setColors(colors);
1028 updateTheme(colors.supportsDarkText());
1029 }
1030 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -07001031
1032 /**
1033 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1034 * add hint text above the dial pad which warns the user of this case.
1035 */
1036 private void maybeShowWfcEmergencyCallingWarning() {
1037 if (!mIsWfcEmergencyCallingWarningEnabled) {
1038 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1039 return;
1040 }
1041
1042 // Use an async task rather than calling into Telephony on UI thread.
1043 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1044 @Override
1045 protected Boolean doInBackground(Void... voids) {
1046 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1047 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1048 ServiceState ss = tm.getServiceState();
1049 boolean isCellAvailable =
1050 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1051 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
1052 + " isCellAvailable=" + isCellAvailable
1053 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
1054 return isWfcAvailable && !isCellAvailable;
1055 }
1056
1057 @Override
1058 protected void onPostExecute(Boolean result) {
1059 if (result.booleanValue()) {
1060 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1061 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1062 } else {
1063 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1064 mDigits.setHint("");
1065 }
1066 maybeChangeHintSize();
1067 }
1068 };
1069 showWfcWarningTask.execute((Void) null);
1070 }
1071
1072 /**
1073 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1074 * edit view and set the font size to a smaller size appropriate for the emergency calling
1075 * warning.
1076 */
1077 private void maybeChangeHintSize() {
1078 if (TextUtils.isEmpty(mDigits.getHint())
1079 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1080 // No hint or there are dialed digits, so use default size.
1081 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1082 // By default, the digits view auto-resizes to fit the text it contains, so
1083 // enable that now.
1084 mDigits.setResizeEnabled(true);
1085 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1086 } else {
1087 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1088 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1089 R.dimen.emergency_call_warning_size));
1090 // Since we're populating this with a static text string, disable auto-resize.
1091 mDigits.setResizeEnabled(false);
1092 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1093 }
1094 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001095
1096 private void setupEmergencyShortcutsView() {
1097 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1098 mDialpadView = findViewById(R.id.emergency_dialer);
1099
Chihhang Chuang96fcafe2018-08-02 22:25:06 +08001100 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1101 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1102
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001103 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1104 dialpadButton.setOnClickListener(this);
1105
1106 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
1107 emergencyInfoButton.setOnClickListener(this);
1108
1109 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1110 mEmergencyActionGroup.setVisibility(View.GONE);
1111
1112 // Setup dialpad title.
1113 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1114 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1115
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001116 mEmergencyShortcutButtonList = new ArrayList<>();
1117 setupEmergencyCallShortcutButton();
1118
CY Cheng42873672018-06-15 21:20:10 +08001119 updateLocationAndEccInfo(null, null);
1120
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001121 switchView(mEmergencyShortcutView, mDialpadView, false);
1122 }
1123
CY Cheng42873672018-06-15 21:20:10 +08001124 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001125 final View locationInfo = findViewById(R.id.location_info);
1126
CY Cheng42873672018-06-15 21:20:10 +08001127 String countryName = null;
1128 if (!TextUtils.isEmpty(countryIso)) {
1129 Locale locale = Locale.getDefault();
1130 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1131 .getDisplayCountry();
1132 }
1133 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001134 locationInfo.setVisibility(View.INVISIBLE);
1135 } else {
1136 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +08001137 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001138 locationInfo.setVisibility(View.VISIBLE);
1139 }
1140 }
1141
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001142 private void setupEmergencyCallShortcutButton() {
1143 final ViewGroup shortcutButtonContainer = findViewById(
1144 R.id.emergency_shortcut_buttons_container);
1145 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +08001146 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001147
CY Cheng42873672018-06-15 21:20:10 +08001148 mShortcutAdapter = new EccShortcutAdapter(this) {
1149 @Override
1150 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1151 CharSequence description, int iconRes) {
1152 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1153 .inflate(R.layout.emergency_shortcut_button, parent, false);
1154 button.setPhoneNumber(number);
1155 button.setPhoneDescription(description);
1156 button.setPhoneTypeIcon(iconRes);
1157 button.setOnConfirmClickListener(EmergencyDialer.this);
1158 return button;
1159 }
1160 };
1161 mShortcutDataSetObserver = new DataSetObserver() {
1162 @Override
1163 public void onChanged() {
1164 super.onChanged();
1165 updateLayout();
1166 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001167
CY Cheng42873672018-06-15 21:20:10 +08001168 @Override
1169 public void onInvalidated() {
1170 super.onInvalidated();
1171 updateLayout();
1172 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001173
CY Cheng42873672018-06-15 21:20:10 +08001174 private void updateLayout() {
1175 // clear previous added buttons
1176 shortcutButtonContainer.removeAllViews();
1177 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001178
CY Cheng42873672018-06-15 21:20:10 +08001179 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1180 EmergencyShortcutButton button = (EmergencyShortcutButton)
1181 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1182 mEmergencyShortcutButtonList.add(button);
1183 shortcutButtonContainer.addView(button);
1184 }
1185
1186 // update emergency numbers title for numerous buttons.
1187 if (mEmergencyShortcutButtonList.size() > 1) {
1188 emergencyNumberTitle.setText(getString(
1189 R.string.numerous_emergency_numbers_title));
1190 } else {
1191 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1192 }
1193 }
1194 };
1195 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1196 }
1197
1198 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1199 if (!isFinishing() && !isDestroyed()) {
1200 setLocationInfo(iso);
1201 if (mShortcutAdapter != null) {
1202 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1203 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001204 }
1205 }
1206
1207 /**
1208 * Called by the activity before a touch event is dispatched to the view hierarchy.
1209 */
1210 private void onPreTouchEvent(MotionEvent event) {
1211 mEmergencyActionGroup.onPreTouchEvent(event);
1212
1213 if (mEmergencyShortcutButtonList != null) {
1214 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1215 button.onPreTouchEvent(event);
1216 }
1217 }
1218 }
1219
1220 /**
1221 * Called by the activity after a touch event is dispatched to the view hierarchy.
1222 */
1223 private void onPostTouchEvent(MotionEvent event) {
1224 mEmergencyActionGroup.onPostTouchEvent(event);
1225
1226 if (mEmergencyShortcutButtonList != null) {
1227 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1228 button.onPostTouchEvent(event);
1229 }
1230 }
1231 }
1232
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001233 /**
1234 * Switch two view.
1235 *
1236 * @param displayView the view would be displayed.
1237 * @param hideView the view would be hidden.
1238 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1239 */
1240 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1241 if (displayView == null || hideView == null) {
1242 return;
1243 }
1244
1245 if (displayView.getVisibility() == View.VISIBLE) {
1246 return;
1247 }
1248
1249 if (hasAnimation) {
1250 crossfade(hideView, displayView);
1251 } else {
1252 hideView.setVisibility(View.GONE);
1253 displayView.setVisibility(View.VISIBLE);
1254 }
1255 }
1256
1257 /**
1258 * Fade out and fade in animation between two view transition.
1259 */
1260 private void crossfade(View fadeOutView, View fadeInView) {
1261 if (fadeOutView == null || fadeInView == null) {
1262 return;
1263 }
1264 final int shortAnimationDuration = getResources().getInteger(
1265 android.R.integer.config_shortAnimTime);
1266
1267 fadeInView.setAlpha(0f);
1268 fadeInView.setVisibility(View.VISIBLE);
1269
1270 fadeInView.animate()
1271 .alpha(1f)
1272 .setDuration(shortAnimationDuration)
1273 .setListener(null);
1274
1275 fadeOutView.animate()
1276 .alpha(0f)
1277 .setDuration(shortAnimationDuration)
1278 .setListener(new AnimatorListenerAdapter() {
1279 @Override
1280 public void onAnimationEnd(Animator animation) {
1281 fadeOutView.setVisibility(View.GONE);
1282 }
1283 });
1284 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +08001285
1286 @Override
1287 public void onSensorChanged(SensorEvent event) {
1288 float distance = event.values[0];
1289 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1290 }
1291
1292 @Override
1293 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1294 // Not used.
1295 }
1296
1297 private boolean isShortcutNumber(String number) {
1298 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1299 return false;
1300 }
1301
1302 boolean isShortcut = false;
1303 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1304 if (button != null && number.equals(button.getPhoneNumber())) {
1305 isShortcut = true;
1306 break;
1307 }
1308 }
1309 return isShortcut;
1310 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001311}