blob: 1619aa17f3025f904aaf0733918c8f15d16f8441 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Shaotang Li8662a912018-07-04 16:53:01 +080026import android.app.KeyguardManager;
Lucas Dupineb9c5702017-05-10 16:57:09 -070027import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080032import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080033import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Shaotang Li8662a912018-07-04 16:53:01 +080035import android.hardware.Sensor;
36import android.hardware.SensorEvent;
37import android.hardware.SensorEventListener;
38import android.hardware.SensorManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.media.AudioManager;
40import android.media.ToneGenerator;
Shaotang Li8662a912018-07-04 16:53:01 +080041import android.metrics.LogMaker;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070043import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070045import android.os.PersistableBundle;
Shaotang Li8662a912018-07-04 16:53:01 +080046import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.provider.Settings;
Shaotang Li5d906c12018-07-19 17:32:42 +080048import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070049import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070050import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080053import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070054import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070055import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070056import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070058import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080059import android.text.Spannable;
60import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
62import android.text.TextWatcher;
63import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080064import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070066import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070067import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070069import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070070import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080072import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080073import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080075import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080076import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Lucas Dupinaf9e9912017-06-22 12:39:39 -070078import com.android.internal.colorextraction.ColorExtractor;
79import com.android.internal.colorextraction.ColorExtractor.GradientColors;
80import com.android.internal.colorextraction.drawable.GradientDrawable;
Shaotang Li8662a912018-07-04 16:53:01 +080081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Yorke Lee23a70732014-08-14 17:12:01 -070083import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070084import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070085import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080086import com.android.phone.ecc.CountryEccInfo;
87import com.android.phone.ecc.EccInfoHelper;
Leo Hsu779569a2018-07-10 11:37:52 +080088import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
Chihhang Chuang92cfe512018-06-07 16:25:27 +080090import java.util.ArrayList;
91import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080092import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080093
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094/**
95 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
96 *
97 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
98 * activity from apps/Contacts) that:
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 Chuang92cfe512018-06-07 16:25:27 +0800113 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800114 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener,
115 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-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 Li8662a912018-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
Lucas Dupineb9c5702017-05-10 16:57:09 -0700212 /** 90% opacity, different from other gradients **/
213 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
214
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800215 /** 85% opacity for black background **/
216 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
217
CY Cheng9a69c182018-06-15 21:20:10 +0800218 /** Size limit of emergency shortcut buttons container. **/
219 private static final int SHORTCUT_SIZE_LIMIT = 3;
220
Tyler Gunnc5428972018-03-28 14:15:34 -0700221 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 private View mDialButton;
223 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800224 private View mEmergencyShortcutView;
225 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226
CY Cheng9a69c182018-06-15 21:20:10 +0800227 private EccInfoHelper mEccInfoHelper;
228
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800229 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800230 private EccShortcutAdapter mShortcutAdapter;
231 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800232
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 private ToneGenerator mToneGenerator;
234 private Object mToneGeneratorLock = new Object();
235
236 // determines if we want to playback local DTMF tones.
237 private boolean mDTMFToneEnabled;
238
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700239 private EmergencyActionGroup mEmergencyActionGroup;
240
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800241 private EmergencyInfoGroup mEmergencyInfoGroup;
242
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243 // close activity when screen turns off
244 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
245 @Override
246 public void onReceive(Context context, Intent intent) {
247 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700248 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249 }
250 }
251 };
252
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800253 /**
254 * Customize accessibility methods in View.
255 */
256 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
257
258 /**
259 * Stop AccessiblityService from reading the title of a hidden View.
260 *
261 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
262 * in the animation end. The view with an accessibility pane title would call the
263 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
264 * accessibility service to read the pane title of fade out view instead of pane title of
265 * fade in view. So it need to filter out the event called by vanished pane.
266 */
267 @Override
268 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
269 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
270 && host.getVisibility() == View.GONE) {
271 return;
272 }
273 super.onPopulateAccessibilityEvent(host, event);
274 }
275 };
276
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700277 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
278
Lucas Dupineb9c5702017-05-10 16:57:09 -0700279 // Background gradient
280 private ColorExtractor mColorExtractor;
281 private GradientDrawable mBackgroundGradient;
282 private boolean mSupportsDarkText;
283
Tyler Gunnc5428972018-03-28 14:15:34 -0700284 private boolean mIsWfcEmergencyCallingWarningEnabled;
285 private float mDefaultDigitsTextSize;
286
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800287 private boolean mAreEmergencyDialerShortcutsEnabled;
288
Shaotang Li8662a912018-07-04 16:53:01 +0800289 private MetricsWriter mMetricsWriter;
290 private SensorManager mSensorManager;
291 private Sensor mProximitySensor;
292 private boolean mIsProximityNear = false;
293
294 /**
295 * The time, in millis, since boot when user opened emergency dialer.
296 * This is used when calculating the user stay duration for metrics data.
297 */
298 private long mUserEnterTimeMillis = 0;
299
300 /**
301 * Bit flag indicating the actions performed by user. This is used for metrics data.
302 */
303 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
304
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 @Override
306 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
307 // Do nothing
308 }
309
310 @Override
311 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700312 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313 }
314
315 @Override
316 public void afterTextChanged(Editable input) {
317 // Check for special sequences, in particular the "**04" or "**05"
318 // sequences that allow you to enter PIN or PUK-related codes.
319 //
320 // But note we *don't* allow most other special sequences here,
321 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
322 // since those shouldn't be available if the device is locked.
323 //
324 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
325 // here, not the regular handleChars() method.
326 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
327 // A special sequence was entered, clear the digits
328 mDigits.getText().clear();
329 }
330
331 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800332 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333 }
334
335 @Override
336 protected void onCreate(Bundle icicle) {
337 super.onCreate(icicle);
338
Shaotang Li8662a912018-07-04 16:53:01 +0800339 mMetricsWriter = new MetricsWriter();
340 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
341 if (mSensorManager != null) {
342 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
343 }
344
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800346 setShowWhenLocked(true);
347 // Allow turning screen on
348 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700349
Chuck Liaocdeebb42018-10-30 12:07:18 +0800350 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
351 PersistableBundle carrierConfig =
352 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
353
354 // Disable emergency dialer shortcut when can't get the location information or inserting
355 // the SIM of the blacklisted carrier.
356 boolean isSupport = carrierConfig.getBoolean(
357 CarrierConfigManager.KEY_SUPPORT_EMERGENCY_DIALER_SHORTCUT_BOOL);
358 Log.d(LOG_TAG, "Is the carrier supported: " + isSupport);
359 TelephonyManager tm = getSystemService(TelephonyManager.class);
360 if (isSupport && !TextUtils.isEmpty(tm.getNetworkCountryIso())) {
361 mAreEmergencyDialerShortcutsEnabled = true;
362 } else {
363 mAreEmergencyDialerShortcutsEnabled = false;
364 }
CY Cheng9a69c182018-06-15 21:20:10 +0800365 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
366 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800367
Lucas Dupineb9c5702017-05-10 16:57:09 -0700368 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800369
370 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
371 // And the background color is black with 85% opacity.
372 if (mAreEmergencyDialerShortcutsEnabled) {
373 updateTheme(false);
374 } else {
375 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
376 ColorExtractor.TYPE_EXTRA_DARK);
377 updateTheme(lockScreenColors.supportsDarkText());
378 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700379
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 setContentView(R.layout.emergency_dialer);
381
Tyler Gunnc5428972018-03-28 14:15:34 -0700382 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700383 mDigits.setKeyListener(DialerKeyListener.getInstance());
384 mDigits.setOnClickListener(this);
385 mDigits.setOnKeyListener(this);
386 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700387 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700388 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 maybeAddNumberFormatting();
390
Lucas Dupineb9c5702017-05-10 16:57:09 -0700391 mBackgroundGradient = new GradientDrawable(this);
392 Point displaySize = new Point();
393 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
394 .getDefaultDisplay().getSize(displaySize);
395 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800396 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
397 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700398 getWindow().setBackgroundDrawable(mBackgroundGradient);
399
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400 // Check for the presence of the keypad
401 View view = findViewById(R.id.one);
402 if (view != null) {
403 setupKeypad();
404 }
405
406 mDelete = findViewById(R.id.deleteButton);
407 mDelete.setOnClickListener(this);
408 mDelete.setOnLongClickListener(this);
409
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700410 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411
412 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700413 // Read carrier config through the public API because PhoneGlobals is not available when we
414 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700415 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 mDialButton.setOnClickListener(this);
417 } else {
418 mDialButton.setVisibility(View.GONE);
419 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700420 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
421 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
422 maybeShowWfcEmergencyCallingWarning();
423
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700424 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700425
426 if (icicle != null) {
427 super.onRestoreInstanceState(icicle);
428 }
429
430 // Extract phone number from intent
431 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700432 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
434 if (number != null) {
435 mDigits.setText(number);
436 }
437 }
438
439 // if the mToneGenerator creation fails, just continue without it. It is
440 // a local audio signal, and is not as important as the dtmf tone itself.
441 synchronized (mToneGeneratorLock) {
442 if (mToneGenerator == null) {
443 try {
444 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
445 } catch (RuntimeException e) {
446 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
447 mToneGenerator = null;
448 }
449 }
450 }
451
452 final IntentFilter intentFilter = new IntentFilter();
453 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
454 registerReceiver(mBroadcastReceiver, intentFilter);
455
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700456 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800457
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800458 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
459
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800460 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu779569a2018-07-10 11:37:52 +0800461 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800462 setupEmergencyShortcutsView();
463 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 }
465
466 @Override
467 protected void onDestroy() {
468 super.onDestroy();
469 synchronized (mToneGeneratorLock) {
470 if (mToneGenerator != null) {
471 mToneGenerator.release();
472 mToneGenerator = null;
473 }
474 }
475 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800476 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
477 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
478 mShortcutDataSetObserver = null;
479 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480 }
481
482 @Override
483 protected void onRestoreInstanceState(Bundle icicle) {
484 mLastNumber = icicle.getString(LAST_NUMBER);
485 }
486
487 @Override
488 protected void onSaveInstanceState(Bundle outState) {
489 super.onSaveInstanceState(outState);
490 outState.putString(LAST_NUMBER, mLastNumber);
491 }
492
493 /**
494 * Explicitly turn off number formatting, since it gets in the way of the emergency
495 * number detector
496 */
497 protected void maybeAddNumberFormatting() {
498 // Do nothing.
499 }
500
501 @Override
502 protected void onPostCreate(Bundle savedInstanceState) {
503 super.onPostCreate(savedInstanceState);
504
505 // This can't be done in onCreate(), since the auto-restoring of the digits
506 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
507 // is called. This method will be called every time the activity is created, and
508 // will always happen after onRestoreSavedInstanceState().
509 mDigits.addTextChangedListener(this);
510 }
511
512 private void setupKeypad() {
513 // Setup the listeners for the buttons
514 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700515 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
516 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517 }
518
519 View view = findViewById(R.id.zero);
520 view.setOnLongClickListener(this);
521 }
522
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800523 @Override
524 public void onBackPressed() {
525 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800526 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800527 // Otherwise, it would finish the activity.
528 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
529 && mDialpadView.getVisibility() == View.VISIBLE) {
530 switchView(mEmergencyShortcutView, mDialpadView, true);
531 return;
532 }
533 super.onBackPressed();
534 }
535
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700536 /**
537 * handle key events
538 */
539 @Override
540 public boolean onKeyDown(int keyCode, KeyEvent event) {
541 switch (keyCode) {
542 // Happen when there's a "Call" hard button.
543 case KeyEvent.KEYCODE_CALL: {
544 if (TextUtils.isEmpty(mDigits.getText().toString())) {
545 // if we are adding a call from the InCallScreen and the phone
546 // number entered is empty, we just close the dialer to expose
547 // the InCallScreen under it.
548 finish();
549 } else {
550 // otherwise, we place the call.
551 placeCall();
552 }
553 return true;
554 }
555 }
556 return super.onKeyDown(keyCode, event);
557 }
558
559 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700560 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
562 mDigits.onKeyDown(keyCode, event);
563 }
564
565 @Override
566 public boolean onKey(View view, int keyCode, KeyEvent event) {
567 switch (view.getId()) {
568 case R.id.digits:
569 // Happen when "Done" button of the IME is pressed. This can happen when this
570 // Activity is forced into landscape mode due to a desk dock.
571 if (keyCode == KeyEvent.KEYCODE_ENTER
572 && event.getAction() == KeyEvent.ACTION_UP) {
573 placeCall();
574 return true;
575 }
576 break;
577 }
578 return false;
579 }
580
581 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700582 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800583 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700584 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800585 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700586 return handled;
587 }
588
589 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800590 public void onConfirmClick(EmergencyShortcutButton button) {
591 if (button == null) return;
592
Shaotang Li8662a912018-07-04 16:53:01 +0800593 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
594
595 // We interest on the context when user has intention to make phone call,
596 // so write metrics here for shortcut number even the call may not be created.
597 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
598 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
599
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800600 String phoneNumber = button.getPhoneNumber();
601
602 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800603 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Li5d906c12018-07-19 17:32:42 +0800604 Bundle extras = new Bundle();
605 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
606 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800607 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800608 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800609 } else {
610 Log.d(LOG_TAG, "emergency number is empty");
611 }
612 }
613
614 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800615 public void onConfirmClick(EmergencyInfoGroup button) {
616 if (button == null) return;
617
618 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
619 Intent intent = (Intent) button.getTag(R.id.tag_intent);
620 if (intent != null) {
621 startActivity(intent);
622 }
623 }
624
625 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 public void onClick(View view) {
627 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700628 case R.id.deleteButton: {
629 keyPressed(KeyEvent.KEYCODE_DEL);
630 return;
631 }
632 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700633 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700634 placeCall();
635 return;
636 }
637 case R.id.digits: {
638 if (mDigits.length() != 0) {
639 mDigits.setCursorVisible(true);
640 }
641 return;
642 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800643 case R.id.floating_action_button_dialpad: {
Shaotang Li8662a912018-07-04 16:53:01 +0800644 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800645 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800646 switchView(mDialpadView, mEmergencyShortcutView, true);
647 return;
648 }
Yorke Lee23a70732014-08-14 17:12:01 -0700649 }
650 }
651
652 @Override
653 public void onPressed(View view, boolean pressed) {
654 if (!pressed) {
655 return;
656 }
657 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700658 case R.id.one: {
659 playTone(ToneGenerator.TONE_DTMF_1);
660 keyPressed(KeyEvent.KEYCODE_1);
661 return;
662 }
663 case R.id.two: {
664 playTone(ToneGenerator.TONE_DTMF_2);
665 keyPressed(KeyEvent.KEYCODE_2);
666 return;
667 }
668 case R.id.three: {
669 playTone(ToneGenerator.TONE_DTMF_3);
670 keyPressed(KeyEvent.KEYCODE_3);
671 return;
672 }
673 case R.id.four: {
674 playTone(ToneGenerator.TONE_DTMF_4);
675 keyPressed(KeyEvent.KEYCODE_4);
676 return;
677 }
678 case R.id.five: {
679 playTone(ToneGenerator.TONE_DTMF_5);
680 keyPressed(KeyEvent.KEYCODE_5);
681 return;
682 }
683 case R.id.six: {
684 playTone(ToneGenerator.TONE_DTMF_6);
685 keyPressed(KeyEvent.KEYCODE_6);
686 return;
687 }
688 case R.id.seven: {
689 playTone(ToneGenerator.TONE_DTMF_7);
690 keyPressed(KeyEvent.KEYCODE_7);
691 return;
692 }
693 case R.id.eight: {
694 playTone(ToneGenerator.TONE_DTMF_8);
695 keyPressed(KeyEvent.KEYCODE_8);
696 return;
697 }
698 case R.id.nine: {
699 playTone(ToneGenerator.TONE_DTMF_9);
700 keyPressed(KeyEvent.KEYCODE_9);
701 return;
702 }
703 case R.id.zero: {
704 playTone(ToneGenerator.TONE_DTMF_0);
705 keyPressed(KeyEvent.KEYCODE_0);
706 return;
707 }
708 case R.id.pound: {
709 playTone(ToneGenerator.TONE_DTMF_P);
710 keyPressed(KeyEvent.KEYCODE_POUND);
711 return;
712 }
713 case R.id.star: {
714 playTone(ToneGenerator.TONE_DTMF_S);
715 keyPressed(KeyEvent.KEYCODE_STAR);
716 return;
717 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700718 }
719 }
720
721 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700722 * called for long touch events
723 */
724 @Override
725 public boolean onLongClick(View view) {
726 int id = view.getId();
727 switch (id) {
728 case R.id.deleteButton: {
729 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700730 return true;
731 }
732 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700733 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 keyPressed(KeyEvent.KEYCODE_PLUS);
735 return true;
736 }
737 }
738 return false;
739 }
740
741 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700742 protected void onStart() {
743 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800744
745 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
746 mUserActions = MetricsWriter.USER_ACTION_NONE;
747 mMetricsWriter.writeMetricsForEnter();
748
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800749 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
750 // And set background color to black.
751 if (mAreEmergencyDialerShortcutsEnabled) {
752 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
753 updateTheme(false);
754 } else {
755 mColorExtractor.addOnColorsChangedListener(this);
756 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
757 ColorExtractor.TYPE_EXTRA_DARK);
758 // Do not animate when view isn't visible yet, just set an initial state.
759 mBackgroundGradient.setColors(lockScreenColors, false);
760 updateTheme(lockScreenColors.supportsDarkText());
761 }
CY Cheng9a69c182018-06-15 21:20:10 +0800762
763 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
764 final Context context = this;
765 mEccInfoHelper.getCountryEccInfoAsync(context,
766 new EccInfoHelper.CountryEccInfoResultCallback() {
767 @Override
768 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
769 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
770 + Rlog.pii(LOG_TAG, iso));
771 updateLocationAndEccInfo(iso, countryEccInfo);
772 }
773
774 @Override
775 public void onDetectCountryFailed() {
776 Log.w(LOG_TAG, "Cannot detect current country.");
777 updateLocationAndEccInfo(null, null);
778 }
779
780 @Override
781 public void onRetrieveCountryEccInfoFailed(String iso) {
782 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
783 + Rlog.pii(LOG_TAG, iso));
784 updateLocationAndEccInfo(iso, null);
785 }
786 });
787 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700788 }
789
790 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 protected void onResume() {
792 super.onResume();
793
Shaotang Li8662a912018-07-04 16:53:01 +0800794 if (mProximitySensor != null) {
795 mSensorManager.registerListener(
796 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
797 }
798
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 // retrieve the DTMF tone play back setting.
800 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
801 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
802
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 // if the mToneGenerator creation fails, just continue without it. It is
804 // a local audio signal, and is not as important as the dtmf tone itself.
805 synchronized (mToneGeneratorLock) {
806 if (mToneGenerator == null) {
807 try {
808 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
809 TONE_RELATIVE_VOLUME);
810 } catch (RuntimeException e) {
811 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
812 mToneGenerator = null;
813 }
814 }
815 }
816
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 updateDialAndDeleteButtonStateEnabledAttr();
818 }
819
820 @Override
821 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700822 super.onPause();
Shaotang Li8662a912018-07-04 16:53:01 +0800823 if (mProximitySensor != null) {
824 mSensorManager.unregisterListener(this, mProximitySensor);
825 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700826 }
827
Lucas Dupineb9c5702017-05-10 16:57:09 -0700828 @Override
829 protected void onStop() {
830 super.onStop();
Shaotang Li8662a912018-07-04 16:53:01 +0800831 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700832 mColorExtractor.removeOnColorsChangedListener(this);
833 }
834
835 /**
836 * Sets theme based on gradient colors
837 * @param supportsDarkText true if gradient supports dark text
838 */
839 private void updateTheme(boolean supportsDarkText) {
840 if (mSupportsDarkText == supportsDarkText) {
841 return;
842 }
843 mSupportsDarkText = supportsDarkText;
844
845 // We can't change themes after inflation, in this case we'll have to recreate
846 // the whole activity.
847 if (mBackgroundGradient != null) {
848 recreate();
849 return;
850 }
851
852 int vis = getWindow().getDecorView().getSystemUiVisibility();
853 if (supportsDarkText) {
854 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
855 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
856 setTheme(R.style.EmergencyDialerThemeDark);
857 } else {
858 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
859 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
860 setTheme(R.style.EmergencyDialerTheme);
861 }
862 getWindow().getDecorView().setSystemUiVisibility(vis);
863 }
864
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 /**
866 * place the call, but check to make sure it is a viable number.
867 */
868 private void placeCall() {
Shaotang Li8662a912018-07-04 16:53:01 +0800869 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700870 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900871
872 // Convert into emergency number according to emergency conversion map.
873 // If conversion map is not defined (this is default), this method does
874 // nothing and just returns input number.
875 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
876
Yorke Lee36bb2542014-06-05 08:09:52 -0700877 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
879
880 // place the call if it is a valid number
881 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
882 // There is no number entered.
883 playTone(ToneGenerator.TONE_PROP_NACK);
884 return;
885 }
Shaotang Li8662a912018-07-04 16:53:01 +0800886
887 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
888 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
889
Shaotang Li5d906c12018-07-19 17:32:42 +0800890 Bundle extras = new Bundle();
891 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
892 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnfa77e202018-03-23 07:47:00 -0700893 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800894 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700895 } else {
896 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
897
Shaotang Li8662a912018-07-04 16:53:01 +0800898 // We interest on the context when user has intention to make phone call,
899 // so write metrics here for non-emergency numbers even these numbers are rejected.
900 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
901 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
902
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700903 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
904 }
Yorke Lee9b341512014-10-17 11:36:41 -0700905 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 }
907
908 /**
909 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
910 *
911 * The tone is played locally, using the audio stream for phone calls.
912 * Tones are played only if the "Audible touch tones" user preference
913 * is checked, and are NOT played if the device is in silent mode.
914 *
915 * @param tone a tone code from {@link ToneGenerator}
916 */
917 void playTone(int tone) {
918 // if local tone playback is disabled, just return.
919 if (!mDTMFToneEnabled) {
920 return;
921 }
922
923 // Also do nothing if the phone is in silent mode.
924 // We need to re-check the ringer mode for *every* playTone()
925 // call, rather than keeping a local flag that's updated in
926 // onResume(), since it's possible to toggle silent mode without
927 // leaving the current activity (via the ENDCALL-longpress menu.)
928 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
929 int ringerMode = audioManager.getRingerMode();
930 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
931 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
932 return;
933 }
934
935 synchronized (mToneGeneratorLock) {
936 if (mToneGenerator == null) {
937 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
938 return;
939 }
940
941 // Start the new tone (will stop any playing tone)
942 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
943 }
944 }
945
946 private CharSequence createErrorMessage(String number) {
947 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800948 String errorString = getString(R.string.dial_emergency_error, number);
949 int startingPosition = errorString.indexOf(number);
950 int endingPosition = startingPosition + number.length();
951 Spannable result = new SpannableString(errorString);
952 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
953 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 } else {
955 return getText(R.string.dial_emergency_empty_error).toString();
956 }
957 }
958
959 @Override
960 protected Dialog onCreateDialog(int id) {
961 AlertDialog dialog = null;
962 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
963 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700964 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
966 .setMessage(createErrorMessage(mLastNumber))
967 .setPositiveButton(R.string.ok, null)
968 .setCancelable(true).create();
969
970 // blur stuff behind the dialog
971 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700972 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700973 }
974 return dialog;
975 }
976
977 @Override
978 protected void onPrepareDialog(int id, Dialog dialog) {
979 super.onPrepareDialog(id, dialog);
980 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
981 AlertDialog alert = (AlertDialog) dialog;
982 alert.setMessage(createErrorMessage(mLastNumber));
983 }
984 }
985
Andrew Leed5631e82014-10-08 16:03:58 -0700986 @Override
987 public boolean onOptionsItemSelected(MenuItem item) {
988 final int itemId = item.getItemId();
989 if (itemId == android.R.id.home) {
990 onBackPressed();
991 return true;
992 }
993 return super.onOptionsItemSelected(item);
994 }
995
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 /**
997 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
998 */
999 private void updateDialAndDeleteButtonStateEnabledAttr() {
1000 final boolean notEmpty = mDigits.length() != 0;
1001
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001002 mDelete.setEnabled(notEmpty);
1003 }
Yorke Lee91311662014-10-24 14:50:45 -07001004
1005 /**
1006 * Remove the digit just before the current position. Used by various long pressed callbacks
1007 * to remove the digit that was populated as a result of the short click.
1008 */
1009 private void removePreviousDigitIfPossible() {
1010 final int currentPosition = mDigits.getSelectionStart();
1011 if (currentPosition > 0) {
1012 mDigits.setSelection(currentPosition);
1013 mDigits.getText().delete(currentPosition - 1, currentPosition);
1014 }
1015 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -08001016
1017 /**
1018 * Update the text-to-speech annotations in the edit field.
1019 */
1020 private void updateTtsSpans() {
1021 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
1022 mDigits.getText().removeSpan(o);
1023 }
1024 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
1025 }
Lucas Dupineb9c5702017-05-10 16:57:09 -07001026
1027 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -07001028 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -07001029 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -07001030 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1031 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001032 mBackgroundGradient.setColors(colors);
1033 updateTheme(colors.supportsDarkText());
1034 }
1035 }
Tyler Gunnc5428972018-03-28 14:15:34 -07001036
1037 /**
1038 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1039 * add hint text above the dial pad which warns the user of this case.
1040 */
1041 private void maybeShowWfcEmergencyCallingWarning() {
1042 if (!mIsWfcEmergencyCallingWarningEnabled) {
1043 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1044 return;
1045 }
1046
1047 // Use an async task rather than calling into Telephony on UI thread.
1048 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1049 @Override
1050 protected Boolean doInBackground(Void... voids) {
1051 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1052 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1053 ServiceState ss = tm.getServiceState();
1054 boolean isCellAvailable =
1055 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1056 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
1057 + " isCellAvailable=" + isCellAvailable
1058 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
1059 return isWfcAvailable && !isCellAvailable;
1060 }
1061
1062 @Override
1063 protected void onPostExecute(Boolean result) {
1064 if (result.booleanValue()) {
1065 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1066 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1067 } else {
1068 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1069 mDigits.setHint("");
1070 }
1071 maybeChangeHintSize();
1072 }
1073 };
1074 showWfcWarningTask.execute((Void) null);
1075 }
1076
1077 /**
1078 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1079 * edit view and set the font size to a smaller size appropriate for the emergency calling
1080 * warning.
1081 */
1082 private void maybeChangeHintSize() {
1083 if (TextUtils.isEmpty(mDigits.getHint())
1084 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1085 // No hint or there are dialed digits, so use default size.
1086 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1087 // By default, the digits view auto-resizes to fit the text it contains, so
1088 // enable that now.
1089 mDigits.setResizeEnabled(true);
1090 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1091 } else {
1092 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1093 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1094 R.dimen.emergency_call_warning_size));
1095 // Since we're populating this with a static text string, disable auto-resize.
1096 mDigits.setResizeEnabled(false);
1097 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1098 }
1099 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001100
1101 private void setupEmergencyShortcutsView() {
1102 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1103 mDialpadView = findViewById(R.id.emergency_dialer);
1104
Chihhang Chuangcaba0da2018-08-02 22:25:06 +08001105 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1106 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1107
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001108 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1109 dialpadButton.setOnClickListener(this);
1110
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001111 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001112
1113 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1114 mEmergencyActionGroup.setVisibility(View.GONE);
1115
1116 // Setup dialpad title.
1117 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1118 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1119
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001120 mEmergencyShortcutButtonList = new ArrayList<>();
1121 setupEmergencyCallShortcutButton();
1122
CY Cheng9a69c182018-06-15 21:20:10 +08001123 updateLocationAndEccInfo(null, null);
1124
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001125 switchView(mEmergencyShortcutView, mDialpadView, false);
1126 }
1127
CY Cheng9a69c182018-06-15 21:20:10 +08001128 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001129 final View locationInfo = findViewById(R.id.location_info);
1130
CY Cheng9a69c182018-06-15 21:20:10 +08001131 String countryName = null;
1132 if (!TextUtils.isEmpty(countryIso)) {
1133 Locale locale = Locale.getDefault();
1134 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1135 .getDisplayCountry();
1136 }
1137 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001138 locationInfo.setVisibility(View.INVISIBLE);
1139 } else {
1140 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001141 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001142 locationInfo.setVisibility(View.VISIBLE);
1143 }
1144 }
1145
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001146 private void setupEmergencyCallShortcutButton() {
1147 final ViewGroup shortcutButtonContainer = findViewById(
1148 R.id.emergency_shortcut_buttons_container);
1149 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001150 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001151
CY Cheng9a69c182018-06-15 21:20:10 +08001152 mShortcutAdapter = new EccShortcutAdapter(this) {
1153 @Override
1154 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1155 CharSequence description, int iconRes) {
1156 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1157 .inflate(R.layout.emergency_shortcut_button, parent, false);
1158 button.setPhoneNumber(number);
1159 button.setPhoneDescription(description);
1160 button.setPhoneTypeIcon(iconRes);
1161 button.setOnConfirmClickListener(EmergencyDialer.this);
1162 return button;
1163 }
1164 };
1165 mShortcutDataSetObserver = new DataSetObserver() {
1166 @Override
1167 public void onChanged() {
1168 super.onChanged();
1169 updateLayout();
1170 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001171
CY Cheng9a69c182018-06-15 21:20:10 +08001172 @Override
1173 public void onInvalidated() {
1174 super.onInvalidated();
1175 updateLayout();
1176 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001177
CY Cheng9a69c182018-06-15 21:20:10 +08001178 private void updateLayout() {
1179 // clear previous added buttons
1180 shortcutButtonContainer.removeAllViews();
1181 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001182
CY Cheng9a69c182018-06-15 21:20:10 +08001183 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1184 EmergencyShortcutButton button = (EmergencyShortcutButton)
1185 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1186 mEmergencyShortcutButtonList.add(button);
1187 shortcutButtonContainer.addView(button);
1188 }
1189
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001190 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001191 if (mEmergencyShortcutButtonList.size() > 1) {
1192 emergencyNumberTitle.setText(getString(
1193 R.string.numerous_emergency_numbers_title));
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001194 // Update mEmergencyInfoGroup margin to avoid UI overlay when
1195 // emergency shortcut button more than 2.
1196 if (mEmergencyShortcutButtonList.size() > 2) {
1197 mEmergencyInfoGroup.updateLayoutMargin();
1198 }
CY Cheng9a69c182018-06-15 21:20:10 +08001199 } else {
1200 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1201 }
1202 }
1203 };
1204 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1205 }
1206
1207 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1208 if (!isFinishing() && !isDestroyed()) {
1209 setLocationInfo(iso);
1210 if (mShortcutAdapter != null) {
1211 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1212 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001213 }
1214 }
1215
1216 /**
1217 * Called by the activity before a touch event is dispatched to the view hierarchy.
1218 */
1219 private void onPreTouchEvent(MotionEvent event) {
1220 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001221 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001222
1223 if (mEmergencyShortcutButtonList != null) {
1224 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1225 button.onPreTouchEvent(event);
1226 }
1227 }
1228 }
1229
1230 /**
1231 * Called by the activity after a touch event is dispatched to the view hierarchy.
1232 */
1233 private void onPostTouchEvent(MotionEvent event) {
1234 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001235 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001236
1237 if (mEmergencyShortcutButtonList != null) {
1238 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1239 button.onPostTouchEvent(event);
1240 }
1241 }
1242 }
1243
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001244 /**
1245 * Switch two view.
1246 *
1247 * @param displayView the view would be displayed.
1248 * @param hideView the view would be hidden.
1249 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1250 */
1251 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1252 if (displayView == null || hideView == null) {
1253 return;
1254 }
1255
1256 if (displayView.getVisibility() == View.VISIBLE) {
1257 return;
1258 }
1259
1260 if (hasAnimation) {
1261 crossfade(hideView, displayView);
1262 } else {
1263 hideView.setVisibility(View.GONE);
1264 displayView.setVisibility(View.VISIBLE);
1265 }
1266 }
1267
1268 /**
1269 * Fade out and fade in animation between two view transition.
1270 */
1271 private void crossfade(View fadeOutView, View fadeInView) {
1272 if (fadeOutView == null || fadeInView == null) {
1273 return;
1274 }
1275 final int shortAnimationDuration = getResources().getInteger(
1276 android.R.integer.config_shortAnimTime);
1277
1278 fadeInView.setAlpha(0f);
1279 fadeInView.setVisibility(View.VISIBLE);
1280
1281 fadeInView.animate()
1282 .alpha(1f)
1283 .setDuration(shortAnimationDuration)
1284 .setListener(null);
1285
1286 fadeOutView.animate()
1287 .alpha(0f)
1288 .setDuration(shortAnimationDuration)
1289 .setListener(new AnimatorListenerAdapter() {
1290 @Override
1291 public void onAnimationEnd(Animator animation) {
1292 fadeOutView.setVisibility(View.GONE);
1293 }
1294 });
1295 }
Shaotang Li8662a912018-07-04 16:53:01 +08001296
1297 @Override
1298 public void onSensorChanged(SensorEvent event) {
1299 float distance = event.values[0];
1300 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1301 }
1302
1303 @Override
1304 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1305 // Not used.
1306 }
1307
1308 private boolean isShortcutNumber(String number) {
1309 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1310 return false;
1311 }
1312
1313 boolean isShortcut = false;
1314 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1315 if (button != null && number.equals(button.getPhoneNumber())) {
1316 isShortcut = true;
1317 break;
1318 }
1319 }
1320 return isShortcut;
1321 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001322}