blob: b9a64719e534a6864a8b5548d6f41826f3e69873 [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;
yuanjiahsue9f15282018-09-06 19:54:15 +080065import android.util.FeatureFlagUtils;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.util.Log;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070067import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070068import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070070import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070071import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import android.view.View;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080073import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import android.view.WindowManager;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080075import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076
Lucas Dupinaf9e9912017-06-22 12:39:39 -070077import com.android.internal.colorextraction.ColorExtractor;
78import com.android.internal.colorextraction.ColorExtractor.GradientColors;
79import com.android.internal.colorextraction.drawable.GradientDrawable;
Shaotang Li3b02aeb2018-07-04 16:53:01 +080080import com.android.internal.logging.MetricsLogger;
81import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Yorke Lee23a70732014-08-14 17:12:01 -070082import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070083import com.android.phone.common.util.ViewUtil;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070084import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng42873672018-06-15 21:20:10 +080085import com.android.phone.ecc.CountryEccInfo;
86import com.android.phone.ecc.EccInfoHelper;
Leo Hsu8f1c6712018-07-10 11:37:52 +080087import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080089import java.util.ArrayList;
90import java.util.List;
CY Cheng42873672018-06-15 21:20:10 +080091import java.util.Locale;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080092
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093/**
94 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
95 *
96 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
97 * activity from apps/Contacts) that:
98 * 1. Allows ONLY emergency calls to be dialed
99 * 2. Disallows voicemail functionality
100 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
101 * activity to stay in front of the keyguard.
102 *
103 * TODO: Even though this is an ultra-simplified version of the normal
104 * dialer, there's still lots of code duplication between this class and
105 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
106 * moved into a shared base class that would live in the framework?
107 * Or could we figure out some way to move *this* class into apps/Contacts
108 * also?
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800109 *
110 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800111 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
112 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800113 * TODO item:
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800114 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 */
116public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700117 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800118 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800119 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener {
120
121 private class MetricsWriter {
122 // Metrics constants indicating the entry type that user opened emergency dialer.
123 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
124 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
125 public static final int ENTRY_TYPE_UNKNOWN = 0;
126 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
127 public static final int ENTRY_TYPE_POWER_MENU = 2;
128
129 // Metrics constants indicating the UI that user made phone call.
130 public static final int CALL_SOURCE_DIALPAD = 0;
131 public static final int CALL_SOURCE_SHORTCUT = 1;
132
133 // Metrics constants indicating the phone number type of a call user made.
134 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
135 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
136
137 // Metrics constants indicating the actions performed by user.
138 public static final int USER_ACTION_NONE = 0x0;
139 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
140 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
141 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
142 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
143
144 private MetricsLogger mMetricsLogger = new MetricsLogger();
145
146 public void writeMetricsForEnter() {
147 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
148 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
149 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
150 .setType(MetricsEvent.TYPE_OPEN)
151 .setSubtype(entryType)
152 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
153 keyguard.isKeyguardLocked() ? 1 : 0));
154 }
155
156 public void writeMetricsForExit() {
157 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
158 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
159 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
160 .setType(MetricsEvent.TYPE_CLOSE)
161 .setSubtype(entryType)
162 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
163 .addTaggedData(
164 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
165 }
166
167 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
168 boolean hasShortcut) {
169 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
170 .setType(MetricsEvent.TYPE_ACTION)
171 .setSubtype(callSource)
172 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
173 phoneNumberType)
174 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
175 hasShortcut ? 1 : 0)
176 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
177 mIsProximityNear ? 1 : 0));
178 }
179 }
180
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 // Keys used with onSaveInstanceState().
182 private static final String LAST_NUMBER = "lastNumber";
183
184 // Intent action for this activity.
185 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
186
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800187 /**
188 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
189 * the emergency dialer.
190 */
191 public static final String EXTRA_ENTRY_TYPE =
192 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
193
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 // List of dialer button IDs.
195 private static final int[] DIALER_KEYS = new int[] {
196 R.id.one, R.id.two, R.id.three,
197 R.id.four, R.id.five, R.id.six,
198 R.id.seven, R.id.eight, R.id.nine,
199 R.id.star, R.id.zero, R.id.pound };
200
201 // Debug constants.
202 private static final boolean DBG = false;
203 private static final String LOG_TAG = "EmergencyDialer";
204
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 /** The length of DTMF tones in milliseconds */
206 private static final int TONE_LENGTH_MS = 150;
207
208 /** The DTMF tone volume relative to other sounds in the stream */
209 private static final int TONE_RELATIVE_VOLUME = 80;
210
211 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
212 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
213
214 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
215
Lucas Dupineb9c5702017-05-10 16:57:09 -0700216 /** 90% opacity, different from other gradients **/
217 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
218
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800219 /** 85% opacity for black background **/
220 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
221
CY Cheng42873672018-06-15 21:20:10 +0800222 /** Size limit of emergency shortcut buttons container. **/
223 private static final int SHORTCUT_SIZE_LIMIT = 3;
224
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700225 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 private View mDialButton;
227 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800228 private View mEmergencyShortcutView;
229 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230
CY Cheng42873672018-06-15 21:20:10 +0800231 private EccInfoHelper mEccInfoHelper;
232
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800233 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800234 private EccShortcutAdapter mShortcutAdapter;
235 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800236
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237 private ToneGenerator mToneGenerator;
238 private Object mToneGeneratorLock = new Object();
239
240 // determines if we want to playback local DTMF tones.
241 private boolean mDTMFToneEnabled;
242
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700243 private EmergencyActionGroup mEmergencyActionGroup;
244
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245 // close activity when screen turns off
246 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
247 @Override
248 public void onReceive(Context context, Intent intent) {
249 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700250 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 }
252 }
253 };
254
255 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
256
Lucas Dupineb9c5702017-05-10 16:57:09 -0700257 // Background gradient
258 private ColorExtractor mColorExtractor;
259 private GradientDrawable mBackgroundGradient;
260 private boolean mSupportsDarkText;
261
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700262 private boolean mIsWfcEmergencyCallingWarningEnabled;
263 private float mDefaultDigitsTextSize;
264
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800265 private boolean mAreEmergencyDialerShortcutsEnabled;
266
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800267 private MetricsWriter mMetricsWriter;
268 private SensorManager mSensorManager;
269 private Sensor mProximitySensor;
270 private boolean mIsProximityNear = false;
271
272 /**
273 * The time, in millis, since boot when user opened emergency dialer.
274 * This is used when calculating the user stay duration for metrics data.
275 */
276 private long mUserEnterTimeMillis = 0;
277
278 /**
279 * Bit flag indicating the actions performed by user. This is used for metrics data.
280 */
281 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
282
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283 @Override
284 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
285 // Do nothing
286 }
287
288 @Override
289 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700290 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 }
292
293 @Override
294 public void afterTextChanged(Editable input) {
295 // Check for special sequences, in particular the "**04" or "**05"
296 // sequences that allow you to enter PIN or PUK-related codes.
297 //
298 // But note we *don't* allow most other special sequences here,
299 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
300 // since those shouldn't be available if the device is locked.
301 //
302 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
303 // here, not the regular handleChars() method.
304 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
305 // A special sequence was entered, clear the digits
306 mDigits.getText().clear();
307 }
308
309 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800310 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 }
312
313 @Override
314 protected void onCreate(Bundle icicle) {
315 super.onCreate(icicle);
316
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800317 mMetricsWriter = new MetricsWriter();
318 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
319 if (mSensorManager != null) {
320 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
321 }
322
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 // Allow this activity to be displayed in front of the keyguard / lockscreen.
324 WindowManager.LayoutParams lp = getWindow().getAttributes();
325 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700326
327 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700328 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700329 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
330
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 getWindow().setAttributes(lp);
332
yuanjiahsue9f15282018-09-06 19:54:15 +0800333 mAreEmergencyDialerShortcutsEnabled = FeatureFlagUtils
334 .isEnabled(this, FeatureFlagUtils.EMERGENCY_DIAL_SHORTCUTS);
CY Cheng42873672018-06-15 21:20:10 +0800335 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
336 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800337
Lucas Dupineb9c5702017-05-10 16:57:09 -0700338 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800339
340 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
341 // And the background color is black with 85% opacity.
342 if (mAreEmergencyDialerShortcutsEnabled) {
343 updateTheme(false);
344 } else {
345 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
346 ColorExtractor.TYPE_EXTRA_DARK);
347 updateTheme(lockScreenColors.supportsDarkText());
348 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700349
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 setContentView(R.layout.emergency_dialer);
351
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700352 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 mDigits.setKeyListener(DialerKeyListener.getInstance());
354 mDigits.setOnClickListener(this);
355 mDigits.setOnKeyListener(this);
356 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700357 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700358 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 maybeAddNumberFormatting();
360
Lucas Dupineb9c5702017-05-10 16:57:09 -0700361 mBackgroundGradient = new GradientDrawable(this);
362 Point displaySize = new Point();
363 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
364 .getDefaultDisplay().getSize(displaySize);
365 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800366 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
367 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700368 getWindow().setBackgroundDrawable(mBackgroundGradient);
369
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370 // Check for the presence of the keypad
371 View view = findViewById(R.id.one);
372 if (view != null) {
373 setupKeypad();
374 }
375
376 mDelete = findViewById(R.id.deleteButton);
377 mDelete.setOnClickListener(this);
378 mDelete.setOnLongClickListener(this);
379
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700380 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700381
382 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700383 // Read carrier config through the public API because PhoneGlobals is not available when we
384 // run as a secondary user.
385 CarrierConfigManager configMgr =
386 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
387 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800388 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700389
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700390 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391 mDialButton.setOnClickListener(this);
392 } else {
393 mDialButton.setVisibility(View.GONE);
394 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700395 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
396 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
397 maybeShowWfcEmergencyCallingWarning();
398
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700399 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400
401 if (icicle != null) {
402 super.onRestoreInstanceState(icicle);
403 }
404
405 // Extract phone number from intent
406 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700407 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700408 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
409 if (number != null) {
410 mDigits.setText(number);
411 }
412 }
413
414 // if the mToneGenerator creation fails, just continue without it. It is
415 // a local audio signal, and is not as important as the dtmf tone itself.
416 synchronized (mToneGeneratorLock) {
417 if (mToneGenerator == null) {
418 try {
419 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
420 } catch (RuntimeException e) {
421 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
422 mToneGenerator = null;
423 }
424 }
425 }
426
427 final IntentFilter intentFilter = new IntentFilter();
428 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
429 registerReceiver(mBroadcastReceiver, intentFilter);
430
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700431 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800432
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800433 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu8f1c6712018-07-10 11:37:52 +0800434 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800435 setupEmergencyShortcutsView();
436 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700437 }
438
439 @Override
440 protected void onDestroy() {
441 super.onDestroy();
442 synchronized (mToneGeneratorLock) {
443 if (mToneGenerator != null) {
444 mToneGenerator.release();
445 mToneGenerator = null;
446 }
447 }
448 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800449 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
450 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
451 mShortcutDataSetObserver = null;
452 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453 }
454
455 @Override
456 protected void onRestoreInstanceState(Bundle icicle) {
457 mLastNumber = icicle.getString(LAST_NUMBER);
458 }
459
460 @Override
461 protected void onSaveInstanceState(Bundle outState) {
462 super.onSaveInstanceState(outState);
463 outState.putString(LAST_NUMBER, mLastNumber);
464 }
465
466 /**
467 * Explicitly turn off number formatting, since it gets in the way of the emergency
468 * number detector
469 */
470 protected void maybeAddNumberFormatting() {
471 // Do nothing.
472 }
473
474 @Override
475 protected void onPostCreate(Bundle savedInstanceState) {
476 super.onPostCreate(savedInstanceState);
477
478 // This can't be done in onCreate(), since the auto-restoring of the digits
479 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
480 // is called. This method will be called every time the activity is created, and
481 // will always happen after onRestoreSavedInstanceState().
482 mDigits.addTextChangedListener(this);
483 }
484
485 private void setupKeypad() {
486 // Setup the listeners for the buttons
487 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700488 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
489 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700490 }
491
492 View view = findViewById(R.id.zero);
493 view.setOnLongClickListener(this);
494 }
495
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800496 @Override
497 public void onBackPressed() {
498 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800499 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800500 // Otherwise, it would finish the activity.
501 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
502 && mDialpadView.getVisibility() == View.VISIBLE) {
503 switchView(mEmergencyShortcutView, mDialpadView, true);
504 return;
505 }
506 super.onBackPressed();
507 }
508
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700509 /**
510 * handle key events
511 */
512 @Override
513 public boolean onKeyDown(int keyCode, KeyEvent event) {
514 switch (keyCode) {
515 // Happen when there's a "Call" hard button.
516 case KeyEvent.KEYCODE_CALL: {
517 if (TextUtils.isEmpty(mDigits.getText().toString())) {
518 // if we are adding a call from the InCallScreen and the phone
519 // number entered is empty, we just close the dialer to expose
520 // the InCallScreen under it.
521 finish();
522 } else {
523 // otherwise, we place the call.
524 placeCall();
525 }
526 return true;
527 }
528 }
529 return super.onKeyDown(keyCode, event);
530 }
531
532 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700533 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700534 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
535 mDigits.onKeyDown(keyCode, event);
536 }
537
538 @Override
539 public boolean onKey(View view, int keyCode, KeyEvent event) {
540 switch (view.getId()) {
541 case R.id.digits:
542 // Happen when "Done" button of the IME is pressed. This can happen when this
543 // Activity is forced into landscape mode due to a desk dock.
544 if (keyCode == KeyEvent.KEYCODE_ENTER
545 && event.getAction() == KeyEvent.ACTION_UP) {
546 placeCall();
547 return true;
548 }
549 break;
550 }
551 return false;
552 }
553
554 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700555 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800556 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700557 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800558 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700559 return handled;
560 }
561
562 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800563 public void onConfirmClick(EmergencyShortcutButton button) {
564 if (button == null) return;
565
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800566 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
567
568 // We interest on the context when user has intention to make phone call,
569 // so write metrics here for shortcut number even the call may not be created.
570 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
571 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
572
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800573 String phoneNumber = button.getPhoneNumber();
574
575 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800576 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Lif27c74d2018-07-19 17:32:42 +0800577 Bundle extras = new Bundle();
578 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
579 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800580 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800581 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800582 } else {
583 Log.d(LOG_TAG, "emergency number is empty");
584 }
585 }
586
587 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700588 public void onClick(View view) {
589 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700590 case R.id.deleteButton: {
591 keyPressed(KeyEvent.KEYCODE_DEL);
592 return;
593 }
594 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700595 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700596 placeCall();
597 return;
598 }
599 case R.id.digits: {
600 if (mDigits.length() != 0) {
601 mDigits.setCursorVisible(true);
602 }
603 return;
604 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800605 case R.id.floating_action_button_dialpad: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800606 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800607 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800608 switchView(mDialpadView, mEmergencyShortcutView, true);
609 return;
610 }
611 case R.id.emergency_info_button: {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800612 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800613 Intent intent = (Intent) view.getTag(R.id.tag_intent);
614 if (intent != null) {
615 startActivity(intent);
616 }
617 return;
618 }
Yorke Lee23a70732014-08-14 17:12:01 -0700619 }
620 }
621
622 @Override
623 public void onPressed(View view, boolean pressed) {
624 if (!pressed) {
625 return;
626 }
627 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 case R.id.one: {
629 playTone(ToneGenerator.TONE_DTMF_1);
630 keyPressed(KeyEvent.KEYCODE_1);
631 return;
632 }
633 case R.id.two: {
634 playTone(ToneGenerator.TONE_DTMF_2);
635 keyPressed(KeyEvent.KEYCODE_2);
636 return;
637 }
638 case R.id.three: {
639 playTone(ToneGenerator.TONE_DTMF_3);
640 keyPressed(KeyEvent.KEYCODE_3);
641 return;
642 }
643 case R.id.four: {
644 playTone(ToneGenerator.TONE_DTMF_4);
645 keyPressed(KeyEvent.KEYCODE_4);
646 return;
647 }
648 case R.id.five: {
649 playTone(ToneGenerator.TONE_DTMF_5);
650 keyPressed(KeyEvent.KEYCODE_5);
651 return;
652 }
653 case R.id.six: {
654 playTone(ToneGenerator.TONE_DTMF_6);
655 keyPressed(KeyEvent.KEYCODE_6);
656 return;
657 }
658 case R.id.seven: {
659 playTone(ToneGenerator.TONE_DTMF_7);
660 keyPressed(KeyEvent.KEYCODE_7);
661 return;
662 }
663 case R.id.eight: {
664 playTone(ToneGenerator.TONE_DTMF_8);
665 keyPressed(KeyEvent.KEYCODE_8);
666 return;
667 }
668 case R.id.nine: {
669 playTone(ToneGenerator.TONE_DTMF_9);
670 keyPressed(KeyEvent.KEYCODE_9);
671 return;
672 }
673 case R.id.zero: {
674 playTone(ToneGenerator.TONE_DTMF_0);
675 keyPressed(KeyEvent.KEYCODE_0);
676 return;
677 }
678 case R.id.pound: {
679 playTone(ToneGenerator.TONE_DTMF_P);
680 keyPressed(KeyEvent.KEYCODE_POUND);
681 return;
682 }
683 case R.id.star: {
684 playTone(ToneGenerator.TONE_DTMF_S);
685 keyPressed(KeyEvent.KEYCODE_STAR);
686 return;
687 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 }
689 }
690
691 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700692 * called for long touch events
693 */
694 @Override
695 public boolean onLongClick(View view) {
696 int id = view.getId();
697 switch (id) {
698 case R.id.deleteButton: {
699 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 return true;
701 }
702 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700703 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704 keyPressed(KeyEvent.KEYCODE_PLUS);
705 return true;
706 }
707 }
708 return false;
709 }
710
711 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700712 protected void onStart() {
713 super.onStart();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800714
715 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
716 mUserActions = MetricsWriter.USER_ACTION_NONE;
717 mMetricsWriter.writeMetricsForEnter();
718
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800719 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
720 // And set background color to black.
721 if (mAreEmergencyDialerShortcutsEnabled) {
722 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
723 updateTheme(false);
724 } else {
725 mColorExtractor.addOnColorsChangedListener(this);
726 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
727 ColorExtractor.TYPE_EXTRA_DARK);
728 // Do not animate when view isn't visible yet, just set an initial state.
729 mBackgroundGradient.setColors(lockScreenColors, false);
730 updateTheme(lockScreenColors.supportsDarkText());
731 }
CY Cheng42873672018-06-15 21:20:10 +0800732
733 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
734 final Context context = this;
735 mEccInfoHelper.getCountryEccInfoAsync(context,
736 new EccInfoHelper.CountryEccInfoResultCallback() {
737 @Override
738 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
739 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
740 + Rlog.pii(LOG_TAG, iso));
741 updateLocationAndEccInfo(iso, countryEccInfo);
742 }
743
744 @Override
745 public void onDetectCountryFailed() {
746 Log.w(LOG_TAG, "Cannot detect current country.");
747 updateLocationAndEccInfo(null, null);
748 }
749
750 @Override
751 public void onRetrieveCountryEccInfoFailed(String iso) {
752 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
753 + Rlog.pii(LOG_TAG, iso));
754 updateLocationAndEccInfo(iso, null);
755 }
756 });
757 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700758 }
759
760 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700761 protected void onResume() {
762 super.onResume();
763
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800764 if (mProximitySensor != null) {
765 mSensorManager.registerListener(
766 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
767 }
768
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 // retrieve the DTMF tone play back setting.
770 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
771 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
772
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 // if the mToneGenerator creation fails, just continue without it. It is
774 // a local audio signal, and is not as important as the dtmf tone itself.
775 synchronized (mToneGeneratorLock) {
776 if (mToneGenerator == null) {
777 try {
778 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
779 TONE_RELATIVE_VOLUME);
780 } catch (RuntimeException e) {
781 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
782 mToneGenerator = null;
783 }
784 }
785 }
786
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 updateDialAndDeleteButtonStateEnabledAttr();
788 }
789
790 @Override
791 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 super.onPause();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800793 if (mProximitySensor != null) {
794 mSensorManager.unregisterListener(this, mProximitySensor);
795 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 }
797
Lucas Dupineb9c5702017-05-10 16:57:09 -0700798 @Override
799 protected void onStop() {
800 super.onStop();
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800801 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700802 mColorExtractor.removeOnColorsChangedListener(this);
803 }
804
805 /**
806 * Sets theme based on gradient colors
807 * @param supportsDarkText true if gradient supports dark text
808 */
809 private void updateTheme(boolean supportsDarkText) {
810 if (mSupportsDarkText == supportsDarkText) {
811 return;
812 }
813 mSupportsDarkText = supportsDarkText;
814
815 // We can't change themes after inflation, in this case we'll have to recreate
816 // the whole activity.
817 if (mBackgroundGradient != null) {
818 recreate();
819 return;
820 }
821
822 int vis = getWindow().getDecorView().getSystemUiVisibility();
823 if (supportsDarkText) {
824 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
825 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
826 setTheme(R.style.EmergencyDialerThemeDark);
827 } else {
828 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
829 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
830 setTheme(R.style.EmergencyDialerTheme);
831 }
832 getWindow().getDecorView().setSystemUiVisibility(vis);
833 }
834
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 /**
836 * place the call, but check to make sure it is a viable number.
837 */
838 private void placeCall() {
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800839 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900841
842 // Convert into emergency number according to emergency conversion map.
843 // If conversion map is not defined (this is default), this method does
844 // nothing and just returns input number.
845 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
846
Yorke Lee36bb2542014-06-05 08:09:52 -0700847 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
849
850 // place the call if it is a valid number
851 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
852 // There is no number entered.
853 playTone(ToneGenerator.TONE_PROP_NACK);
854 return;
855 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800856
857 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
858 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
859
Shaotang Lif27c74d2018-07-19 17:32:42 +0800860 Bundle extras = new Bundle();
861 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
862 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000863 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800864 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 } else {
866 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
867
Shaotang Li3b02aeb2018-07-04 16:53:01 +0800868 // We interest on the context when user has intention to make phone call,
869 // so write metrics here for non-emergency numbers even these numbers are rejected.
870 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
871 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
872
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700873 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
874 }
Yorke Lee9b341512014-10-17 11:36:41 -0700875 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700876 }
877
878 /**
879 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
880 *
881 * The tone is played locally, using the audio stream for phone calls.
882 * Tones are played only if the "Audible touch tones" user preference
883 * is checked, and are NOT played if the device is in silent mode.
884 *
885 * @param tone a tone code from {@link ToneGenerator}
886 */
887 void playTone(int tone) {
888 // if local tone playback is disabled, just return.
889 if (!mDTMFToneEnabled) {
890 return;
891 }
892
893 // Also do nothing if the phone is in silent mode.
894 // We need to re-check the ringer mode for *every* playTone()
895 // call, rather than keeping a local flag that's updated in
896 // onResume(), since it's possible to toggle silent mode without
897 // leaving the current activity (via the ENDCALL-longpress menu.)
898 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
899 int ringerMode = audioManager.getRingerMode();
900 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
901 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
902 return;
903 }
904
905 synchronized (mToneGeneratorLock) {
906 if (mToneGenerator == null) {
907 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
908 return;
909 }
910
911 // Start the new tone (will stop any playing tone)
912 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
913 }
914 }
915
916 private CharSequence createErrorMessage(String number) {
917 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800918 String errorString = getString(R.string.dial_emergency_error, number);
919 int startingPosition = errorString.indexOf(number);
920 int endingPosition = startingPosition + number.length();
921 Spannable result = new SpannableString(errorString);
922 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
923 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 } else {
925 return getText(R.string.dial_emergency_empty_error).toString();
926 }
927 }
928
929 @Override
930 protected Dialog onCreateDialog(int id) {
931 AlertDialog dialog = null;
932 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
933 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700934 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
936 .setMessage(createErrorMessage(mLastNumber))
937 .setPositiveButton(R.string.ok, null)
938 .setCancelable(true).create();
939
940 // blur stuff behind the dialog
941 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700942 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 }
944 return dialog;
945 }
946
947 @Override
948 protected void onPrepareDialog(int id, Dialog dialog) {
949 super.onPrepareDialog(id, dialog);
950 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
951 AlertDialog alert = (AlertDialog) dialog;
952 alert.setMessage(createErrorMessage(mLastNumber));
953 }
954 }
955
Andrew Leed5631e82014-10-08 16:03:58 -0700956 @Override
957 public boolean onOptionsItemSelected(MenuItem item) {
958 final int itemId = item.getItemId();
959 if (itemId == android.R.id.home) {
960 onBackPressed();
961 return true;
962 }
963 return super.onOptionsItemSelected(item);
964 }
965
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 /**
967 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
968 */
969 private void updateDialAndDeleteButtonStateEnabledAttr() {
970 final boolean notEmpty = mDigits.length() != 0;
971
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 mDelete.setEnabled(notEmpty);
973 }
Yorke Lee91311662014-10-24 14:50:45 -0700974
975 /**
976 * Remove the digit just before the current position. Used by various long pressed callbacks
977 * to remove the digit that was populated as a result of the short click.
978 */
979 private void removePreviousDigitIfPossible() {
980 final int currentPosition = mDigits.getSelectionStart();
981 if (currentPosition > 0) {
982 mDigits.setSelection(currentPosition);
983 mDigits.getText().delete(currentPosition - 1, currentPosition);
984 }
985 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800986
987 /**
988 * Update the text-to-speech annotations in the edit field.
989 */
990 private void updateTtsSpans() {
991 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
992 mDigits.getText().removeSpan(o);
993 }
994 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
995 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700996
997 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700998 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700999 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -07001000 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1001 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001002 mBackgroundGradient.setColors(colors);
1003 updateTheme(colors.supportsDarkText());
1004 }
1005 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -07001006
1007 /**
1008 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1009 * add hint text above the dial pad which warns the user of this case.
1010 */
1011 private void maybeShowWfcEmergencyCallingWarning() {
1012 if (!mIsWfcEmergencyCallingWarningEnabled) {
1013 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1014 return;
1015 }
1016
1017 // Use an async task rather than calling into Telephony on UI thread.
1018 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1019 @Override
1020 protected Boolean doInBackground(Void... voids) {
1021 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1022 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1023 ServiceState ss = tm.getServiceState();
1024 boolean isCellAvailable =
1025 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1026 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
1027 + " isCellAvailable=" + isCellAvailable
1028 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
1029 return isWfcAvailable && !isCellAvailable;
1030 }
1031
1032 @Override
1033 protected void onPostExecute(Boolean result) {
1034 if (result.booleanValue()) {
1035 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1036 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1037 } else {
1038 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1039 mDigits.setHint("");
1040 }
1041 maybeChangeHintSize();
1042 }
1043 };
1044 showWfcWarningTask.execute((Void) null);
1045 }
1046
1047 /**
1048 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1049 * edit view and set the font size to a smaller size appropriate for the emergency calling
1050 * warning.
1051 */
1052 private void maybeChangeHintSize() {
1053 if (TextUtils.isEmpty(mDigits.getHint())
1054 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1055 // No hint or there are dialed digits, so use default size.
1056 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1057 // By default, the digits view auto-resizes to fit the text it contains, so
1058 // enable that now.
1059 mDigits.setResizeEnabled(true);
1060 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1061 } else {
1062 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1063 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1064 R.dimen.emergency_call_warning_size));
1065 // Since we're populating this with a static text string, disable auto-resize.
1066 mDigits.setResizeEnabled(false);
1067 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1068 }
1069 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001070
1071 private void setupEmergencyShortcutsView() {
1072 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1073 mDialpadView = findViewById(R.id.emergency_dialer);
1074
1075 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1076 dialpadButton.setOnClickListener(this);
1077
1078 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
1079 emergencyInfoButton.setOnClickListener(this);
1080
1081 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1082 mEmergencyActionGroup.setVisibility(View.GONE);
1083
1084 // Setup dialpad title.
1085 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1086 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1087
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001088 mEmergencyShortcutButtonList = new ArrayList<>();
1089 setupEmergencyCallShortcutButton();
1090
CY Cheng42873672018-06-15 21:20:10 +08001091 updateLocationAndEccInfo(null, null);
1092
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001093 switchView(mEmergencyShortcutView, mDialpadView, false);
1094 }
1095
CY Cheng42873672018-06-15 21:20:10 +08001096 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001097 final View locationInfo = findViewById(R.id.location_info);
1098
CY Cheng42873672018-06-15 21:20:10 +08001099 String countryName = null;
1100 if (!TextUtils.isEmpty(countryIso)) {
1101 Locale locale = Locale.getDefault();
1102 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1103 .getDisplayCountry();
1104 }
1105 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001106 locationInfo.setVisibility(View.INVISIBLE);
1107 } else {
1108 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +08001109 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001110 locationInfo.setVisibility(View.VISIBLE);
1111 }
1112 }
1113
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001114 private void setupEmergencyCallShortcutButton() {
1115 final ViewGroup shortcutButtonContainer = findViewById(
1116 R.id.emergency_shortcut_buttons_container);
1117 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +08001118 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001119
CY Cheng42873672018-06-15 21:20:10 +08001120 mShortcutAdapter = new EccShortcutAdapter(this) {
1121 @Override
1122 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1123 CharSequence description, int iconRes) {
1124 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1125 .inflate(R.layout.emergency_shortcut_button, parent, false);
1126 button.setPhoneNumber(number);
1127 button.setPhoneDescription(description);
1128 button.setPhoneTypeIcon(iconRes);
1129 button.setOnConfirmClickListener(EmergencyDialer.this);
1130 return button;
1131 }
1132 };
1133 mShortcutDataSetObserver = new DataSetObserver() {
1134 @Override
1135 public void onChanged() {
1136 super.onChanged();
1137 updateLayout();
1138 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001139
CY Cheng42873672018-06-15 21:20:10 +08001140 @Override
1141 public void onInvalidated() {
1142 super.onInvalidated();
1143 updateLayout();
1144 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001145
CY Cheng42873672018-06-15 21:20:10 +08001146 private void updateLayout() {
1147 // clear previous added buttons
1148 shortcutButtonContainer.removeAllViews();
1149 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001150
CY Cheng42873672018-06-15 21:20:10 +08001151 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1152 EmergencyShortcutButton button = (EmergencyShortcutButton)
1153 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1154 mEmergencyShortcutButtonList.add(button);
1155 shortcutButtonContainer.addView(button);
1156 }
1157
1158 // update emergency numbers title for numerous buttons.
1159 if (mEmergencyShortcutButtonList.size() > 1) {
1160 emergencyNumberTitle.setText(getString(
1161 R.string.numerous_emergency_numbers_title));
1162 } else {
1163 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1164 }
1165 }
1166 };
1167 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1168 }
1169
1170 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1171 if (!isFinishing() && !isDestroyed()) {
1172 setLocationInfo(iso);
1173 if (mShortcutAdapter != null) {
1174 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1175 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001176 }
1177 }
1178
1179 /**
1180 * Called by the activity before a touch event is dispatched to the view hierarchy.
1181 */
1182 private void onPreTouchEvent(MotionEvent event) {
1183 mEmergencyActionGroup.onPreTouchEvent(event);
1184
1185 if (mEmergencyShortcutButtonList != null) {
1186 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1187 button.onPreTouchEvent(event);
1188 }
1189 }
1190 }
1191
1192 /**
1193 * Called by the activity after a touch event is dispatched to the view hierarchy.
1194 */
1195 private void onPostTouchEvent(MotionEvent event) {
1196 mEmergencyActionGroup.onPostTouchEvent(event);
1197
1198 if (mEmergencyShortcutButtonList != null) {
1199 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1200 button.onPostTouchEvent(event);
1201 }
1202 }
1203 }
1204
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001205 /**
1206 * Switch two view.
1207 *
1208 * @param displayView the view would be displayed.
1209 * @param hideView the view would be hidden.
1210 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1211 */
1212 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1213 if (displayView == null || hideView == null) {
1214 return;
1215 }
1216
1217 if (displayView.getVisibility() == View.VISIBLE) {
1218 return;
1219 }
1220
1221 if (hasAnimation) {
1222 crossfade(hideView, displayView);
1223 } else {
1224 hideView.setVisibility(View.GONE);
1225 displayView.setVisibility(View.VISIBLE);
1226 }
1227 }
1228
1229 /**
1230 * Fade out and fade in animation between two view transition.
1231 */
1232 private void crossfade(View fadeOutView, View fadeInView) {
1233 if (fadeOutView == null || fadeInView == null) {
1234 return;
1235 }
1236 final int shortAnimationDuration = getResources().getInteger(
1237 android.R.integer.config_shortAnimTime);
1238
1239 fadeInView.setAlpha(0f);
1240 fadeInView.setVisibility(View.VISIBLE);
1241
1242 fadeInView.animate()
1243 .alpha(1f)
1244 .setDuration(shortAnimationDuration)
1245 .setListener(null);
1246
1247 fadeOutView.animate()
1248 .alpha(0f)
1249 .setDuration(shortAnimationDuration)
1250 .setListener(new AnimatorListenerAdapter() {
1251 @Override
1252 public void onAnimationEnd(Animator animation) {
1253 fadeOutView.setVisibility(View.GONE);
1254 }
1255 });
1256 }
Shaotang Li3b02aeb2018-07-04 16:53:01 +08001257
1258 @Override
1259 public void onSensorChanged(SensorEvent event) {
1260 float distance = event.values[0];
1261 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1262 }
1263
1264 @Override
1265 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1266 // Not used.
1267 }
1268
1269 private boolean isShortcutNumber(String number) {
1270 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1271 return false;
1272 }
1273
1274 boolean isShortcut = false;
1275 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1276 if (button != null && number.equals(button.getPhoneNumber())) {
1277 isShortcut = true;
1278 break;
1279 }
1280 }
1281 return isShortcut;
1282 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283}