blob: db4fcd22db9b7a67d2e4888423a7e82940162087 [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?
110 */
111public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700112 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800113 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +0800114 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener,
115 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800116
117 private class MetricsWriter {
118 // Metrics constants indicating the entry type that user opened emergency dialer.
119 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
120 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
121 public static final int ENTRY_TYPE_UNKNOWN = 0;
122 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
123 public static final int ENTRY_TYPE_POWER_MENU = 2;
124
125 // Metrics constants indicating the UI that user made phone call.
126 public static final int CALL_SOURCE_DIALPAD = 0;
127 public static final int CALL_SOURCE_SHORTCUT = 1;
128
129 // Metrics constants indicating the phone number type of a call user made.
130 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
131 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
132
133 // Metrics constants indicating the actions performed by user.
134 public static final int USER_ACTION_NONE = 0x0;
135 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
136 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
137 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
138 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
139
140 private MetricsLogger mMetricsLogger = new MetricsLogger();
141
142 public void writeMetricsForEnter() {
143 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
144 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
145 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
146 .setType(MetricsEvent.TYPE_OPEN)
147 .setSubtype(entryType)
148 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
149 keyguard.isKeyguardLocked() ? 1 : 0));
150 }
151
152 public void writeMetricsForExit() {
153 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
154 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
155 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
156 .setType(MetricsEvent.TYPE_CLOSE)
157 .setSubtype(entryType)
158 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
159 .addTaggedData(
160 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
161 }
162
163 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
164 boolean hasShortcut) {
165 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
166 .setType(MetricsEvent.TYPE_ACTION)
167 .setSubtype(callSource)
168 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
169 phoneNumberType)
170 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
171 hasShortcut ? 1 : 0)
172 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
173 mIsProximityNear ? 1 : 0));
174 }
175 }
176
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177 // Keys used with onSaveInstanceState().
178 private static final String LAST_NUMBER = "lastNumber";
179
180 // Intent action for this activity.
181 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
182
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800183 /**
184 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
185 * the emergency dialer.
186 */
187 public static final String EXTRA_ENTRY_TYPE =
188 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
189
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190 // List of dialer button IDs.
191 private static final int[] DIALER_KEYS = new int[] {
192 R.id.one, R.id.two, R.id.three,
193 R.id.four, R.id.five, R.id.six,
194 R.id.seven, R.id.eight, R.id.nine,
195 R.id.star, R.id.zero, R.id.pound };
196
197 // Debug constants.
198 private static final boolean DBG = false;
199 private static final String LOG_TAG = "EmergencyDialer";
200
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 /** The length of DTMF tones in milliseconds */
202 private static final int TONE_LENGTH_MS = 150;
203
204 /** The DTMF tone volume relative to other sounds in the stream */
205 private static final int TONE_RELATIVE_VOLUME = 80;
206
207 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
208 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
209
210 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
211
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800212 /** 85% opacity for black background **/
213 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
214
CY Cheng42873672018-06-15 21:20:10 +0800215 /** Size limit of emergency shortcut buttons container. **/
216 private static final int SHORTCUT_SIZE_LIMIT = 3;
217
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700218 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700219 private View mDialButton;
220 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800221 private View mEmergencyShortcutView;
222 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223
CY Cheng42873672018-06-15 21:20:10 +0800224 private EccInfoHelper mEccInfoHelper;
225
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800226 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800227 private EccShortcutAdapter mShortcutAdapter;
228 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800229
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 private ToneGenerator mToneGenerator;
231 private Object mToneGeneratorLock = new Object();
232
233 // determines if we want to playback local DTMF tones.
234 private boolean mDTMFToneEnabled;
235
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700236 private EmergencyActionGroup mEmergencyActionGroup;
237
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +0800238 private EmergencyInfoGroup mEmergencyInfoGroup;
239
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 // close activity when screen turns off
241 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
242 @Override
243 public void onReceive(Context context, Intent intent) {
244 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700245 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 }
247 }
248 };
249
Chihhang Chuang96fcafe2018-08-02 22:25:06 +0800250 /**
251 * Customize accessibility methods in View.
252 */
253 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
254
255 /**
256 * Stop AccessiblityService from reading the title of a hidden View.
257 *
258 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
259 * in the animation end. The view with an accessibility pane title would call the
260 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
261 * accessibility service to read the pane title of fade out view instead of pane title of
262 * fade in view. So it need to filter out the event called by vanished pane.
263 */
264 @Override
265 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
266 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
267 && host.getVisibility() == View.GONE) {
268 return;
269 }
270 super.onPopulateAccessibilityEvent(host, event);
271 }
272 };
273
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
275
Lucas Dupineb9c5702017-05-10 16:57:09 -0700276 // Background gradient
277 private ColorExtractor mColorExtractor;
278 private GradientDrawable mBackgroundGradient;
279 private boolean mSupportsDarkText;
280
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700281 private boolean mIsWfcEmergencyCallingWarningEnabled;
282 private float mDefaultDigitsTextSize;
283
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800284 private MetricsWriter mMetricsWriter;
285 private SensorManager mSensorManager;
286 private Sensor mProximitySensor;
287 private boolean mIsProximityNear = false;
288
289 /**
290 * The time, in millis, since boot when user opened emergency dialer.
291 * This is used when calculating the user stay duration for metrics data.
292 */
293 private long mUserEnterTimeMillis = 0;
294
295 /**
296 * Bit flag indicating the actions performed by user. This is used for metrics data.
297 */
298 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
299
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 @Override
301 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
302 // Do nothing
303 }
304
305 @Override
306 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700307 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 }
309
310 @Override
311 public void afterTextChanged(Editable input) {
312 // Check for special sequences, in particular the "**04" or "**05"
313 // sequences that allow you to enter PIN or PUK-related codes.
314 //
315 // But note we *don't* allow most other special sequences here,
316 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
317 // since those shouldn't be available if the device is locked.
318 //
319 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
320 // here, not the regular handleChars() method.
321 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
322 // A special sequence was entered, clear the digits
323 mDigits.getText().clear();
324 }
325
326 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800327 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 }
329
330 @Override
331 protected void onCreate(Bundle icicle) {
332 super.onCreate(icicle);
333
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800334 mMetricsWriter = new MetricsWriter();
335 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
336 if (mSensorManager != null) {
337 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
338 }
339
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 // Allow this activity to be displayed in front of the keyguard / lockscreen.
341 WindowManager.LayoutParams lp = getWindow().getAttributes();
342 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700343
344 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700345 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700346 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
347
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 getWindow().setAttributes(lp);
349
Lucas Dupineb9c5702017-05-10 16:57:09 -0700350 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800351
352 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
353 // And the background color is black with 85% opacity.
Leo Hsu83665d82018-10-02 09:57:03 +0800354 updateTheme(false);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700355
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356 setContentView(R.layout.emergency_dialer);
357
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700358 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 mDigits.setKeyListener(DialerKeyListener.getInstance());
360 mDigits.setOnClickListener(this);
361 mDigits.setOnKeyListener(this);
362 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700363 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700364 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 maybeAddNumberFormatting();
366
Lucas Dupineb9c5702017-05-10 16:57:09 -0700367 mBackgroundGradient = new GradientDrawable(this);
368 Point displaySize = new Point();
369 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
370 .getDefaultDisplay().getSize(displaySize);
371 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Leo Hsu83665d82018-10-02 09:57:03 +0800372 mBackgroundGradient.setAlpha(BLACK_BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700373 getWindow().setBackgroundDrawable(mBackgroundGradient);
374
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 // Check for the presence of the keypad
376 View view = findViewById(R.id.one);
377 if (view != null) {
378 setupKeypad();
379 }
380
381 mDelete = findViewById(R.id.deleteButton);
382 mDelete.setOnClickListener(this);
383 mDelete.setOnLongClickListener(this);
384
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700385 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386
387 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700388 // Read carrier config through the public API because PhoneGlobals is not available when we
389 // run as a secondary user.
390 CarrierConfigManager configMgr =
391 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
392 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800393 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700394
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700395 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 mDialButton.setOnClickListener(this);
397 } else {
398 mDialButton.setVisibility(View.GONE);
399 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700400 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
401 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
402 maybeShowWfcEmergencyCallingWarning();
403
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700404 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405
406 if (icicle != null) {
407 super.onRestoreInstanceState(icicle);
408 }
409
410 // Extract phone number from intent
411 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700412 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700413 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
414 if (number != null) {
415 mDigits.setText(number);
416 }
417 }
418
419 // if the mToneGenerator creation fails, just continue without it. It is
420 // a local audio signal, and is not as important as the dtmf tone itself.
421 synchronized (mToneGeneratorLock) {
422 if (mToneGenerator == null) {
423 try {
424 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
425 } catch (RuntimeException e) {
426 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
427 mToneGenerator = null;
428 }
429 }
430 }
431
432 final IntentFilter intentFilter = new IntentFilter();
433 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
434 registerReceiver(mBroadcastReceiver, intentFilter);
435
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700436 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800437
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +0800438 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
439
Leo Hsu83665d82018-10-02 09:57:03 +0800440 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
441 setupEmergencyShortcutsView();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700442 }
443
444 @Override
445 protected void onDestroy() {
446 super.onDestroy();
447 synchronized (mToneGeneratorLock) {
448 if (mToneGenerator != null) {
449 mToneGenerator.release();
450 mToneGenerator = null;
451 }
452 }
453 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800454 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
455 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
456 mShortcutDataSetObserver = null;
457 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700458 }
459
460 @Override
461 protected void onRestoreInstanceState(Bundle icicle) {
462 mLastNumber = icicle.getString(LAST_NUMBER);
463 }
464
465 @Override
466 protected void onSaveInstanceState(Bundle outState) {
467 super.onSaveInstanceState(outState);
468 outState.putString(LAST_NUMBER, mLastNumber);
469 }
470
471 /**
472 * Explicitly turn off number formatting, since it gets in the way of the emergency
473 * number detector
474 */
475 protected void maybeAddNumberFormatting() {
476 // Do nothing.
477 }
478
479 @Override
480 protected void onPostCreate(Bundle savedInstanceState) {
481 super.onPostCreate(savedInstanceState);
482
483 // This can't be done in onCreate(), since the auto-restoring of the digits
484 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
485 // is called. This method will be called every time the activity is created, and
486 // will always happen after onRestoreSavedInstanceState().
487 mDigits.addTextChangedListener(this);
488 }
489
490 private void setupKeypad() {
491 // Setup the listeners for the buttons
492 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700493 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
494 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700495 }
496
497 View view = findViewById(R.id.zero);
498 view.setOnLongClickListener(this);
499 }
500
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800501 @Override
502 public void onBackPressed() {
503 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800504 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800505 // Otherwise, it would finish the activity.
Leo Hsu83665d82018-10-02 09:57:03 +0800506 if (mDialpadView != null && mDialpadView.getVisibility() == View.VISIBLE) {
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800507 switchView(mEmergencyShortcutView, mDialpadView, true);
508 return;
509 }
510 super.onBackPressed();
511 }
512
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700513 /**
514 * handle key events
515 */
516 @Override
517 public boolean onKeyDown(int keyCode, KeyEvent event) {
518 switch (keyCode) {
519 // Happen when there's a "Call" hard button.
520 case KeyEvent.KEYCODE_CALL: {
521 if (TextUtils.isEmpty(mDigits.getText().toString())) {
522 // if we are adding a call from the InCallScreen and the phone
523 // number entered is empty, we just close the dialer to expose
524 // the InCallScreen under it.
525 finish();
526 } else {
527 // otherwise, we place the call.
528 placeCall();
529 }
530 return true;
531 }
532 }
533 return super.onKeyDown(keyCode, event);
534 }
535
536 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700537 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
539 mDigits.onKeyDown(keyCode, event);
540 }
541
542 @Override
543 public boolean onKey(View view, int keyCode, KeyEvent event) {
544 switch (view.getId()) {
545 case R.id.digits:
546 // Happen when "Done" button of the IME is pressed. This can happen when this
547 // Activity is forced into landscape mode due to a desk dock.
548 if (keyCode == KeyEvent.KEYCODE_ENTER
549 && event.getAction() == KeyEvent.ACTION_UP) {
550 placeCall();
551 return true;
552 }
553 break;
554 }
555 return false;
556 }
557
558 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700559 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800560 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700561 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800562 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700563 return handled;
564 }
565
566 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800567 public void onConfirmClick(EmergencyShortcutButton button) {
568 if (button == null) return;
569
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800570 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
571
572 // We interest on the context when user has intention to make phone call,
573 // so write metrics here for shortcut number even the call may not be created.
574 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
575 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
576
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800577 String phoneNumber = button.getPhoneNumber();
578
579 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800580 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Lif27c74d2018-07-19 17:32:42 +0800581 Bundle extras = new Bundle();
582 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
583 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800584 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800585 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800586 } else {
587 Log.d(LOG_TAG, "emergency number is empty");
588 }
589 }
590
591 @Override
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +0800592 public void onConfirmClick(EmergencyInfoGroup button) {
593 if (button == null) return;
594
595 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
596 Intent intent = (Intent) button.getTag(R.id.tag_intent);
597 if (intent != null) {
598 startActivity(intent);
599 }
600 }
601
602 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700603 public void onClick(View view) {
604 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700605 case R.id.deleteButton: {
606 keyPressed(KeyEvent.KEYCODE_DEL);
607 return;
608 }
609 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700610 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700611 placeCall();
612 return;
613 }
614 case R.id.digits: {
615 if (mDigits.length() != 0) {
616 mDigits.setCursorVisible(true);
617 }
618 return;
619 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800620 case R.id.floating_action_button_dialpad: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800621 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800622 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800623 switchView(mDialpadView, mEmergencyShortcutView, true);
624 return;
625 }
Yorke Lee23a70732014-08-14 17:12:01 -0700626 }
627 }
628
629 @Override
630 public void onPressed(View view, boolean pressed) {
631 if (!pressed) {
632 return;
633 }
634 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 case R.id.one: {
636 playTone(ToneGenerator.TONE_DTMF_1);
637 keyPressed(KeyEvent.KEYCODE_1);
638 return;
639 }
640 case R.id.two: {
641 playTone(ToneGenerator.TONE_DTMF_2);
642 keyPressed(KeyEvent.KEYCODE_2);
643 return;
644 }
645 case R.id.three: {
646 playTone(ToneGenerator.TONE_DTMF_3);
647 keyPressed(KeyEvent.KEYCODE_3);
648 return;
649 }
650 case R.id.four: {
651 playTone(ToneGenerator.TONE_DTMF_4);
652 keyPressed(KeyEvent.KEYCODE_4);
653 return;
654 }
655 case R.id.five: {
656 playTone(ToneGenerator.TONE_DTMF_5);
657 keyPressed(KeyEvent.KEYCODE_5);
658 return;
659 }
660 case R.id.six: {
661 playTone(ToneGenerator.TONE_DTMF_6);
662 keyPressed(KeyEvent.KEYCODE_6);
663 return;
664 }
665 case R.id.seven: {
666 playTone(ToneGenerator.TONE_DTMF_7);
667 keyPressed(KeyEvent.KEYCODE_7);
668 return;
669 }
670 case R.id.eight: {
671 playTone(ToneGenerator.TONE_DTMF_8);
672 keyPressed(KeyEvent.KEYCODE_8);
673 return;
674 }
675 case R.id.nine: {
676 playTone(ToneGenerator.TONE_DTMF_9);
677 keyPressed(KeyEvent.KEYCODE_9);
678 return;
679 }
680 case R.id.zero: {
681 playTone(ToneGenerator.TONE_DTMF_0);
682 keyPressed(KeyEvent.KEYCODE_0);
683 return;
684 }
685 case R.id.pound: {
686 playTone(ToneGenerator.TONE_DTMF_P);
687 keyPressed(KeyEvent.KEYCODE_POUND);
688 return;
689 }
690 case R.id.star: {
691 playTone(ToneGenerator.TONE_DTMF_S);
692 keyPressed(KeyEvent.KEYCODE_STAR);
693 return;
694 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700695 }
696 }
697
698 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700699 * called for long touch events
700 */
701 @Override
702 public boolean onLongClick(View view) {
703 int id = view.getId();
704 switch (id) {
705 case R.id.deleteButton: {
706 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 return true;
708 }
709 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700710 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711 keyPressed(KeyEvent.KEYCODE_PLUS);
712 return true;
713 }
714 }
715 return false;
716 }
717
718 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700719 protected void onStart() {
720 super.onStart();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800721
722 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
723 mUserActions = MetricsWriter.USER_ACTION_NONE;
724 mMetricsWriter.writeMetricsForEnter();
725
Leo Hsu83665d82018-10-02 09:57:03 +0800726 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
727 updateTheme(false);
CY Cheng42873672018-06-15 21:20:10 +0800728
Leo Hsu83665d82018-10-02 09:57:03 +0800729 if (mEccInfoHelper != null) {
CY Cheng42873672018-06-15 21:20:10 +0800730 final Context context = this;
731 mEccInfoHelper.getCountryEccInfoAsync(context,
732 new EccInfoHelper.CountryEccInfoResultCallback() {
733 @Override
734 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
735 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
736 + Rlog.pii(LOG_TAG, iso));
737 updateLocationAndEccInfo(iso, countryEccInfo);
738 }
739
740 @Override
741 public void onDetectCountryFailed() {
742 Log.w(LOG_TAG, "Cannot detect current country.");
743 updateLocationAndEccInfo(null, null);
744 }
745
746 @Override
747 public void onRetrieveCountryEccInfoFailed(String iso) {
748 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
749 + Rlog.pii(LOG_TAG, iso));
750 updateLocationAndEccInfo(iso, null);
751 }
752 });
753 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700754 }
755
756 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700757 protected void onResume() {
758 super.onResume();
759
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800760 if (mProximitySensor != null) {
761 mSensorManager.registerListener(
762 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
763 }
764
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700765 // retrieve the DTMF tone play back setting.
766 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
767 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
768
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 // if the mToneGenerator creation fails, just continue without it. It is
770 // a local audio signal, and is not as important as the dtmf tone itself.
771 synchronized (mToneGeneratorLock) {
772 if (mToneGenerator == null) {
773 try {
774 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
775 TONE_RELATIVE_VOLUME);
776 } catch (RuntimeException e) {
777 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
778 mToneGenerator = null;
779 }
780 }
781 }
782
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700783 updateDialAndDeleteButtonStateEnabledAttr();
784 }
785
786 @Override
787 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 super.onPause();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800789 if (mProximitySensor != null) {
790 mSensorManager.unregisterListener(this, mProximitySensor);
791 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 }
793
Lucas Dupineb9c5702017-05-10 16:57:09 -0700794 @Override
795 protected void onStop() {
796 super.onStop();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800797 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700798 mColorExtractor.removeOnColorsChangedListener(this);
799 }
800
801 /**
802 * Sets theme based on gradient colors
803 * @param supportsDarkText true if gradient supports dark text
804 */
805 private void updateTheme(boolean supportsDarkText) {
806 if (mSupportsDarkText == supportsDarkText) {
807 return;
808 }
809 mSupportsDarkText = supportsDarkText;
810
811 // We can't change themes after inflation, in this case we'll have to recreate
812 // the whole activity.
813 if (mBackgroundGradient != null) {
814 recreate();
815 return;
816 }
817
818 int vis = getWindow().getDecorView().getSystemUiVisibility();
819 if (supportsDarkText) {
820 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
821 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
822 setTheme(R.style.EmergencyDialerThemeDark);
823 } else {
824 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
825 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
826 setTheme(R.style.EmergencyDialerTheme);
827 }
828 getWindow().getDecorView().setSystemUiVisibility(vis);
829 }
830
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700831 /**
832 * place the call, but check to make sure it is a viable number.
833 */
834 private void placeCall() {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800835 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900837
838 // Convert into emergency number according to emergency conversion map.
839 // If conversion map is not defined (this is default), this method does
840 // nothing and just returns input number.
841 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
842
Yorke Lee36bb2542014-06-05 08:09:52 -0700843 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700844 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
845
846 // place the call if it is a valid number
847 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
848 // There is no number entered.
849 playTone(ToneGenerator.TONE_PROP_NACK);
850 return;
851 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800852
853 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
854 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
855
Shaotang Lif27c74d2018-07-19 17:32:42 +0800856 Bundle extras = new Bundle();
857 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
858 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000859 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800860 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 } else {
862 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
863
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800864 // We interest on the context when user has intention to make phone call,
865 // so write metrics here for non-emergency numbers even these numbers are rejected.
866 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
867 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
868
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
870 }
Yorke Lee9b341512014-10-17 11:36:41 -0700871 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 }
873
874 /**
875 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
876 *
877 * The tone is played locally, using the audio stream for phone calls.
878 * Tones are played only if the "Audible touch tones" user preference
879 * is checked, and are NOT played if the device is in silent mode.
880 *
881 * @param tone a tone code from {@link ToneGenerator}
882 */
883 void playTone(int tone) {
884 // if local tone playback is disabled, just return.
885 if (!mDTMFToneEnabled) {
886 return;
887 }
888
889 // Also do nothing if the phone is in silent mode.
890 // We need to re-check the ringer mode for *every* playTone()
891 // call, rather than keeping a local flag that's updated in
892 // onResume(), since it's possible to toggle silent mode without
893 // leaving the current activity (via the ENDCALL-longpress menu.)
894 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
895 int ringerMode = audioManager.getRingerMode();
896 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
897 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
898 return;
899 }
900
901 synchronized (mToneGeneratorLock) {
902 if (mToneGenerator == null) {
903 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
904 return;
905 }
906
907 // Start the new tone (will stop any playing tone)
908 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
909 }
910 }
911
912 private CharSequence createErrorMessage(String number) {
913 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800914 String errorString = getString(R.string.dial_emergency_error, number);
915 int startingPosition = errorString.indexOf(number);
916 int endingPosition = startingPosition + number.length();
917 Spannable result = new SpannableString(errorString);
918 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
919 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 } else {
921 return getText(R.string.dial_emergency_empty_error).toString();
922 }
923 }
924
925 @Override
926 protected Dialog onCreateDialog(int id) {
927 AlertDialog dialog = null;
928 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
929 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700930 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
932 .setMessage(createErrorMessage(mLastNumber))
933 .setPositiveButton(R.string.ok, null)
934 .setCancelable(true).create();
935
936 // blur stuff behind the dialog
937 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700938 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 }
940 return dialog;
941 }
942
943 @Override
944 protected void onPrepareDialog(int id, Dialog dialog) {
945 super.onPrepareDialog(id, dialog);
946 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
947 AlertDialog alert = (AlertDialog) dialog;
948 alert.setMessage(createErrorMessage(mLastNumber));
949 }
950 }
951
Andrew Leed5631e82014-10-08 16:03:58 -0700952 @Override
953 public boolean onOptionsItemSelected(MenuItem item) {
954 final int itemId = item.getItemId();
955 if (itemId == android.R.id.home) {
956 onBackPressed();
957 return true;
958 }
959 return super.onOptionsItemSelected(item);
960 }
961
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 /**
963 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
964 */
965 private void updateDialAndDeleteButtonStateEnabledAttr() {
966 final boolean notEmpty = mDigits.length() != 0;
967
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 mDelete.setEnabled(notEmpty);
969 }
Yorke Lee91311662014-10-24 14:50:45 -0700970
971 /**
972 * Remove the digit just before the current position. Used by various long pressed callbacks
973 * to remove the digit that was populated as a result of the short click.
974 */
975 private void removePreviousDigitIfPossible() {
976 final int currentPosition = mDigits.getSelectionStart();
977 if (currentPosition > 0) {
978 mDigits.setSelection(currentPosition);
979 mDigits.getText().delete(currentPosition - 1, currentPosition);
980 }
981 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800982
983 /**
984 * Update the text-to-speech annotations in the edit field.
985 */
986 private void updateTtsSpans() {
987 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
988 mDigits.getText().removeSpan(o);
989 }
990 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
991 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700992
993 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700994 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700995 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700996 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
997 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700998 mBackgroundGradient.setColors(colors);
999 updateTheme(colors.supportsDarkText());
1000 }
1001 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -07001002
1003 /**
1004 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1005 * add hint text above the dial pad which warns the user of this case.
1006 */
1007 private void maybeShowWfcEmergencyCallingWarning() {
1008 if (!mIsWfcEmergencyCallingWarningEnabled) {
1009 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1010 return;
1011 }
1012
1013 // Use an async task rather than calling into Telephony on UI thread.
1014 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1015 @Override
1016 protected Boolean doInBackground(Void... voids) {
1017 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1018 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1019 ServiceState ss = tm.getServiceState();
1020 boolean isCellAvailable =
1021 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1022 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
1023 + " isCellAvailable=" + isCellAvailable
1024 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
1025 return isWfcAvailable && !isCellAvailable;
1026 }
1027
1028 @Override
1029 protected void onPostExecute(Boolean result) {
1030 if (result.booleanValue()) {
1031 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1032 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1033 } else {
1034 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1035 mDigits.setHint("");
1036 }
1037 maybeChangeHintSize();
1038 }
1039 };
1040 showWfcWarningTask.execute((Void) null);
1041 }
1042
1043 /**
1044 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1045 * edit view and set the font size to a smaller size appropriate for the emergency calling
1046 * warning.
1047 */
1048 private void maybeChangeHintSize() {
1049 if (TextUtils.isEmpty(mDigits.getHint())
1050 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1051 // No hint or there are dialed digits, so use default size.
1052 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1053 // By default, the digits view auto-resizes to fit the text it contains, so
1054 // enable that now.
1055 mDigits.setResizeEnabled(true);
1056 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1057 } else {
1058 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1059 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1060 R.dimen.emergency_call_warning_size));
1061 // Since we're populating this with a static text string, disable auto-resize.
1062 mDigits.setResizeEnabled(false);
1063 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1064 }
1065 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001066
1067 private void setupEmergencyShortcutsView() {
1068 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1069 mDialpadView = findViewById(R.id.emergency_dialer);
1070
Chihhang Chuang96fcafe2018-08-02 22:25:06 +08001071 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1072 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1073
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001074 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1075 dialpadButton.setOnClickListener(this);
1076
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001077 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001078
1079 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1080 mEmergencyActionGroup.setVisibility(View.GONE);
1081
1082 // Setup dialpad title.
1083 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1084 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1085
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001086 mEmergencyShortcutButtonList = new ArrayList<>();
1087 setupEmergencyCallShortcutButton();
1088
CY Cheng42873672018-06-15 21:20:10 +08001089 updateLocationAndEccInfo(null, null);
1090
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001091 switchView(mEmergencyShortcutView, mDialpadView, false);
1092 }
1093
CY Cheng42873672018-06-15 21:20:10 +08001094 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001095 final View locationInfo = findViewById(R.id.location_info);
1096
CY Cheng42873672018-06-15 21:20:10 +08001097 String countryName = null;
1098 if (!TextUtils.isEmpty(countryIso)) {
1099 Locale locale = Locale.getDefault();
1100 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1101 .getDisplayCountry();
1102 }
1103 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001104 locationInfo.setVisibility(View.INVISIBLE);
1105 } else {
1106 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +08001107 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001108 locationInfo.setVisibility(View.VISIBLE);
1109 }
1110 }
1111
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001112 private void setupEmergencyCallShortcutButton() {
1113 final ViewGroup shortcutButtonContainer = findViewById(
1114 R.id.emergency_shortcut_buttons_container);
1115 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +08001116 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001117
CY Cheng42873672018-06-15 21:20:10 +08001118 mShortcutAdapter = new EccShortcutAdapter(this) {
1119 @Override
1120 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1121 CharSequence description, int iconRes) {
1122 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1123 .inflate(R.layout.emergency_shortcut_button, parent, false);
1124 button.setPhoneNumber(number);
1125 button.setPhoneDescription(description);
1126 button.setPhoneTypeIcon(iconRes);
1127 button.setOnConfirmClickListener(EmergencyDialer.this);
1128 return button;
1129 }
1130 };
1131 mShortcutDataSetObserver = new DataSetObserver() {
1132 @Override
1133 public void onChanged() {
1134 super.onChanged();
1135 updateLayout();
1136 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001137
CY Cheng42873672018-06-15 21:20:10 +08001138 @Override
1139 public void onInvalidated() {
1140 super.onInvalidated();
1141 updateLayout();
1142 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001143
CY Cheng42873672018-06-15 21:20:10 +08001144 private void updateLayout() {
1145 // clear previous added buttons
1146 shortcutButtonContainer.removeAllViews();
1147 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001148
CY Cheng42873672018-06-15 21:20:10 +08001149 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1150 EmergencyShortcutButton button = (EmergencyShortcutButton)
1151 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1152 mEmergencyShortcutButtonList.add(button);
1153 shortcutButtonContainer.addView(button);
1154 }
1155
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001156 // Update emergency numbers title for numerous buttons.
CY Cheng42873672018-06-15 21:20:10 +08001157 if (mEmergencyShortcutButtonList.size() > 1) {
1158 emergencyNumberTitle.setText(getString(
1159 R.string.numerous_emergency_numbers_title));
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001160 // Update mEmergencyInfoGroup margin to avoid UI overlay when
1161 // emergency shortcut button more than 2.
1162 if (mEmergencyShortcutButtonList.size() > 2) {
1163 mEmergencyInfoGroup.updateLayoutMargin();
1164 }
CY Cheng42873672018-06-15 21:20:10 +08001165 } else {
1166 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1167 }
1168 }
1169 };
1170 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1171 }
1172
1173 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1174 if (!isFinishing() && !isDestroyed()) {
1175 setLocationInfo(iso);
1176 if (mShortcutAdapter != null) {
1177 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1178 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001179 }
1180 }
1181
1182 /**
1183 * Called by the activity before a touch event is dispatched to the view hierarchy.
1184 */
1185 private void onPreTouchEvent(MotionEvent event) {
1186 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001187 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001188
1189 if (mEmergencyShortcutButtonList != null) {
1190 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1191 button.onPreTouchEvent(event);
1192 }
1193 }
1194 }
1195
1196 /**
1197 * Called by the activity after a touch event is dispatched to the view hierarchy.
1198 */
1199 private void onPostTouchEvent(MotionEvent event) {
1200 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang3ba3fb52018-08-09 20:11:34 +08001201 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001202
1203 if (mEmergencyShortcutButtonList != null) {
1204 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1205 button.onPostTouchEvent(event);
1206 }
1207 }
1208 }
1209
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001210 /**
1211 * Switch two view.
1212 *
1213 * @param displayView the view would be displayed.
1214 * @param hideView the view would be hidden.
1215 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1216 */
1217 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1218 if (displayView == null || hideView == null) {
1219 return;
1220 }
1221
1222 if (displayView.getVisibility() == View.VISIBLE) {
1223 return;
1224 }
1225
1226 if (hasAnimation) {
1227 crossfade(hideView, displayView);
1228 } else {
1229 hideView.setVisibility(View.GONE);
1230 displayView.setVisibility(View.VISIBLE);
1231 }
1232 }
1233
1234 /**
1235 * Fade out and fade in animation between two view transition.
1236 */
1237 private void crossfade(View fadeOutView, View fadeInView) {
1238 if (fadeOutView == null || fadeInView == null) {
1239 return;
1240 }
1241 final int shortAnimationDuration = getResources().getInteger(
1242 android.R.integer.config_shortAnimTime);
1243
1244 fadeInView.setAlpha(0f);
1245 fadeInView.setVisibility(View.VISIBLE);
1246
1247 fadeInView.animate()
1248 .alpha(1f)
1249 .setDuration(shortAnimationDuration)
1250 .setListener(null);
1251
1252 fadeOutView.animate()
1253 .alpha(0f)
1254 .setDuration(shortAnimationDuration)
1255 .setListener(new AnimatorListenerAdapter() {
1256 @Override
1257 public void onAnimationEnd(Animator animation) {
1258 fadeOutView.setVisibility(View.GONE);
1259 }
1260 });
1261 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +08001262
1263 @Override
1264 public void onSensorChanged(SensorEvent event) {
1265 float distance = event.values[0];
1266 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1267 }
1268
1269 @Override
1270 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1271 // Not used.
1272 }
1273
1274 private boolean isShortcutNumber(String number) {
1275 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1276 return false;
1277 }
1278
1279 boolean isShortcut = false;
1280 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1281 if (button != null && number.equals(button.getPhoneNumber())) {
1282 isShortcut = true;
1283 break;
1284 }
1285 }
1286 return isShortcut;
1287 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288}