blob: cb12cbb54fa428752ed1e3232fd17a398c192c7d [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);
Leo Hsu233f5b32018-11-07 11:08:03 +0800360 String countryIso = tm.getNetworkCountryIso();
361 mAreEmergencyDialerShortcutsEnabled = false;
362 if (isSupport && !TextUtils.isEmpty(countryIso)) {
363 if (EccInfoHelper.isCountryEccInfoAvailable(this, countryIso)) {
364 mAreEmergencyDialerShortcutsEnabled = true;
365 } else {
366 Log.d(LOG_TAG, "ECC info is unavailable. Disable emergency dialer shortcut.");
367 }
Chuck Liaocdeebb42018-10-30 12:07:18 +0800368 }
CY Cheng9a69c182018-06-15 21:20:10 +0800369 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
370 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800371
Lucas Dupineb9c5702017-05-10 16:57:09 -0700372 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800373
374 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
375 // And the background color is black with 85% opacity.
376 if (mAreEmergencyDialerShortcutsEnabled) {
377 updateTheme(false);
378 } else {
379 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
380 ColorExtractor.TYPE_EXTRA_DARK);
381 updateTheme(lockScreenColors.supportsDarkText());
382 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700383
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 setContentView(R.layout.emergency_dialer);
385
Tyler Gunnc5428972018-03-28 14:15:34 -0700386 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 mDigits.setKeyListener(DialerKeyListener.getInstance());
388 mDigits.setOnClickListener(this);
389 mDigits.setOnKeyListener(this);
390 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700391 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700392 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700393 maybeAddNumberFormatting();
394
Lucas Dupineb9c5702017-05-10 16:57:09 -0700395 mBackgroundGradient = new GradientDrawable(this);
396 Point displaySize = new Point();
397 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
398 .getDefaultDisplay().getSize(displaySize);
399 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800400 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
401 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700402 getWindow().setBackgroundDrawable(mBackgroundGradient);
403
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700404 // Check for the presence of the keypad
405 View view = findViewById(R.id.one);
406 if (view != null) {
407 setupKeypad();
408 }
409
410 mDelete = findViewById(R.id.deleteButton);
411 mDelete.setOnClickListener(this);
412 mDelete.setOnLongClickListener(this);
413
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700414 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415
416 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700417 // Read carrier config through the public API because PhoneGlobals is not available when we
418 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700419 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420 mDialButton.setOnClickListener(this);
421 } else {
422 mDialButton.setVisibility(View.GONE);
423 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700424 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
425 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
426 maybeShowWfcEmergencyCallingWarning();
427
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700428 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429
430 if (icicle != null) {
431 super.onRestoreInstanceState(icicle);
432 }
433
434 // Extract phone number from intent
435 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700436 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700437 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
438 if (number != null) {
439 mDigits.setText(number);
440 }
441 }
442
443 // if the mToneGenerator creation fails, just continue without it. It is
444 // a local audio signal, and is not as important as the dtmf tone itself.
445 synchronized (mToneGeneratorLock) {
446 if (mToneGenerator == null) {
447 try {
448 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
449 } catch (RuntimeException e) {
450 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
451 mToneGenerator = null;
452 }
453 }
454 }
455
456 final IntentFilter intentFilter = new IntentFilter();
457 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
458 registerReceiver(mBroadcastReceiver, intentFilter);
459
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700460 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800461
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800462 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
463
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800464 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu233f5b32018-11-07 11:08:03 +0800465 mEccInfoHelper = new EccInfoHelper(IsoToEccProtobufRepository.getInstance());
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800466 setupEmergencyShortcutsView();
467 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 }
469
470 @Override
471 protected void onDestroy() {
472 super.onDestroy();
473 synchronized (mToneGeneratorLock) {
474 if (mToneGenerator != null) {
475 mToneGenerator.release();
476 mToneGenerator = null;
477 }
478 }
479 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800480 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
481 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
482 mShortcutDataSetObserver = null;
483 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484 }
485
486 @Override
487 protected void onRestoreInstanceState(Bundle icicle) {
488 mLastNumber = icicle.getString(LAST_NUMBER);
489 }
490
491 @Override
492 protected void onSaveInstanceState(Bundle outState) {
493 super.onSaveInstanceState(outState);
494 outState.putString(LAST_NUMBER, mLastNumber);
495 }
496
497 /**
498 * Explicitly turn off number formatting, since it gets in the way of the emergency
499 * number detector
500 */
501 protected void maybeAddNumberFormatting() {
502 // Do nothing.
503 }
504
505 @Override
506 protected void onPostCreate(Bundle savedInstanceState) {
507 super.onPostCreate(savedInstanceState);
508
509 // This can't be done in onCreate(), since the auto-restoring of the digits
510 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
511 // is called. This method will be called every time the activity is created, and
512 // will always happen after onRestoreSavedInstanceState().
513 mDigits.addTextChangedListener(this);
514 }
515
516 private void setupKeypad() {
517 // Setup the listeners for the buttons
518 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700519 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
520 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700521 }
522
523 View view = findViewById(R.id.zero);
524 view.setOnLongClickListener(this);
525 }
526
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800527 @Override
528 public void onBackPressed() {
529 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800530 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800531 // Otherwise, it would finish the activity.
532 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
533 && mDialpadView.getVisibility() == View.VISIBLE) {
534 switchView(mEmergencyShortcutView, mDialpadView, true);
535 return;
536 }
537 super.onBackPressed();
538 }
539
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540 /**
541 * handle key events
542 */
543 @Override
544 public boolean onKeyDown(int keyCode, KeyEvent event) {
545 switch (keyCode) {
546 // Happen when there's a "Call" hard button.
547 case KeyEvent.KEYCODE_CALL: {
548 if (TextUtils.isEmpty(mDigits.getText().toString())) {
549 // if we are adding a call from the InCallScreen and the phone
550 // number entered is empty, we just close the dialer to expose
551 // the InCallScreen under it.
552 finish();
553 } else {
554 // otherwise, we place the call.
555 placeCall();
556 }
557 return true;
558 }
559 }
560 return super.onKeyDown(keyCode, event);
561 }
562
563 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700564 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
566 mDigits.onKeyDown(keyCode, event);
567 }
568
569 @Override
570 public boolean onKey(View view, int keyCode, KeyEvent event) {
571 switch (view.getId()) {
572 case R.id.digits:
573 // Happen when "Done" button of the IME is pressed. This can happen when this
574 // Activity is forced into landscape mode due to a desk dock.
575 if (keyCode == KeyEvent.KEYCODE_ENTER
576 && event.getAction() == KeyEvent.ACTION_UP) {
577 placeCall();
578 return true;
579 }
580 break;
581 }
582 return false;
583 }
584
585 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700586 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800587 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700588 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800589 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700590 return handled;
591 }
592
593 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800594 public void onConfirmClick(EmergencyShortcutButton button) {
595 if (button == null) return;
596
Shaotang Li8662a912018-07-04 16:53:01 +0800597 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
598
599 // We interest on the context when user has intention to make phone call,
600 // so write metrics here for shortcut number even the call may not be created.
601 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
602 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
603
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800604 String phoneNumber = button.getPhoneNumber();
605
606 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800607 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Li5d906c12018-07-19 17:32:42 +0800608 Bundle extras = new Bundle();
609 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
610 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800611 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800612 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800613 } else {
614 Log.d(LOG_TAG, "emergency number is empty");
615 }
616 }
617
618 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800619 public void onConfirmClick(EmergencyInfoGroup button) {
620 if (button == null) return;
621
622 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
623 Intent intent = (Intent) button.getTag(R.id.tag_intent);
624 if (intent != null) {
625 startActivity(intent);
626 }
627 }
628
629 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 public void onClick(View view) {
631 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700632 case R.id.deleteButton: {
633 keyPressed(KeyEvent.KEYCODE_DEL);
634 return;
635 }
636 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700637 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700638 placeCall();
639 return;
640 }
641 case R.id.digits: {
642 if (mDigits.length() != 0) {
643 mDigits.setCursorVisible(true);
644 }
645 return;
646 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800647 case R.id.floating_action_button_dialpad: {
Shaotang Li8662a912018-07-04 16:53:01 +0800648 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800649 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800650 switchView(mDialpadView, mEmergencyShortcutView, true);
651 return;
652 }
Yorke Lee23a70732014-08-14 17:12:01 -0700653 }
654 }
655
656 @Override
657 public void onPressed(View view, boolean pressed) {
658 if (!pressed) {
659 return;
660 }
661 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 case R.id.one: {
663 playTone(ToneGenerator.TONE_DTMF_1);
664 keyPressed(KeyEvent.KEYCODE_1);
665 return;
666 }
667 case R.id.two: {
668 playTone(ToneGenerator.TONE_DTMF_2);
669 keyPressed(KeyEvent.KEYCODE_2);
670 return;
671 }
672 case R.id.three: {
673 playTone(ToneGenerator.TONE_DTMF_3);
674 keyPressed(KeyEvent.KEYCODE_3);
675 return;
676 }
677 case R.id.four: {
678 playTone(ToneGenerator.TONE_DTMF_4);
679 keyPressed(KeyEvent.KEYCODE_4);
680 return;
681 }
682 case R.id.five: {
683 playTone(ToneGenerator.TONE_DTMF_5);
684 keyPressed(KeyEvent.KEYCODE_5);
685 return;
686 }
687 case R.id.six: {
688 playTone(ToneGenerator.TONE_DTMF_6);
689 keyPressed(KeyEvent.KEYCODE_6);
690 return;
691 }
692 case R.id.seven: {
693 playTone(ToneGenerator.TONE_DTMF_7);
694 keyPressed(KeyEvent.KEYCODE_7);
695 return;
696 }
697 case R.id.eight: {
698 playTone(ToneGenerator.TONE_DTMF_8);
699 keyPressed(KeyEvent.KEYCODE_8);
700 return;
701 }
702 case R.id.nine: {
703 playTone(ToneGenerator.TONE_DTMF_9);
704 keyPressed(KeyEvent.KEYCODE_9);
705 return;
706 }
707 case R.id.zero: {
708 playTone(ToneGenerator.TONE_DTMF_0);
709 keyPressed(KeyEvent.KEYCODE_0);
710 return;
711 }
712 case R.id.pound: {
713 playTone(ToneGenerator.TONE_DTMF_P);
714 keyPressed(KeyEvent.KEYCODE_POUND);
715 return;
716 }
717 case R.id.star: {
718 playTone(ToneGenerator.TONE_DTMF_S);
719 keyPressed(KeyEvent.KEYCODE_STAR);
720 return;
721 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700722 }
723 }
724
725 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 * called for long touch events
727 */
728 @Override
729 public boolean onLongClick(View view) {
730 int id = view.getId();
731 switch (id) {
732 case R.id.deleteButton: {
733 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 return true;
735 }
736 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700737 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700738 keyPressed(KeyEvent.KEYCODE_PLUS);
739 return true;
740 }
741 }
742 return false;
743 }
744
745 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700746 protected void onStart() {
747 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800748
749 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
750 mUserActions = MetricsWriter.USER_ACTION_NONE;
751 mMetricsWriter.writeMetricsForEnter();
752
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800753 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
754 // And set background color to black.
755 if (mAreEmergencyDialerShortcutsEnabled) {
756 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
757 updateTheme(false);
758 } else {
759 mColorExtractor.addOnColorsChangedListener(this);
760 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
761 ColorExtractor.TYPE_EXTRA_DARK);
762 // Do not animate when view isn't visible yet, just set an initial state.
763 mBackgroundGradient.setColors(lockScreenColors, false);
764 updateTheme(lockScreenColors.supportsDarkText());
765 }
CY Cheng9a69c182018-06-15 21:20:10 +0800766
767 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
768 final Context context = this;
769 mEccInfoHelper.getCountryEccInfoAsync(context,
770 new EccInfoHelper.CountryEccInfoResultCallback() {
771 @Override
772 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
773 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
774 + Rlog.pii(LOG_TAG, iso));
775 updateLocationAndEccInfo(iso, countryEccInfo);
776 }
777
778 @Override
779 public void onDetectCountryFailed() {
780 Log.w(LOG_TAG, "Cannot detect current country.");
781 updateLocationAndEccInfo(null, null);
782 }
783
784 @Override
785 public void onRetrieveCountryEccInfoFailed(String iso) {
786 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
787 + Rlog.pii(LOG_TAG, iso));
788 updateLocationAndEccInfo(iso, null);
789 }
790 });
791 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700792 }
793
794 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 protected void onResume() {
796 super.onResume();
797
Shaotang Li8662a912018-07-04 16:53:01 +0800798 if (mProximitySensor != null) {
799 mSensorManager.registerListener(
800 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
801 }
802
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 // retrieve the DTMF tone play back setting.
804 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
805 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
806
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700807 // if the mToneGenerator creation fails, just continue without it. It is
808 // a local audio signal, and is not as important as the dtmf tone itself.
809 synchronized (mToneGeneratorLock) {
810 if (mToneGenerator == null) {
811 try {
812 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
813 TONE_RELATIVE_VOLUME);
814 } catch (RuntimeException e) {
815 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
816 mToneGenerator = null;
817 }
818 }
819 }
820
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 updateDialAndDeleteButtonStateEnabledAttr();
822 }
823
824 @Override
825 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700826 super.onPause();
Shaotang Li8662a912018-07-04 16:53:01 +0800827 if (mProximitySensor != null) {
828 mSensorManager.unregisterListener(this, mProximitySensor);
829 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 }
831
Lucas Dupineb9c5702017-05-10 16:57:09 -0700832 @Override
833 protected void onStop() {
834 super.onStop();
Shaotang Li8662a912018-07-04 16:53:01 +0800835 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700836 mColorExtractor.removeOnColorsChangedListener(this);
837 }
838
839 /**
840 * Sets theme based on gradient colors
841 * @param supportsDarkText true if gradient supports dark text
842 */
843 private void updateTheme(boolean supportsDarkText) {
844 if (mSupportsDarkText == supportsDarkText) {
845 return;
846 }
847 mSupportsDarkText = supportsDarkText;
848
849 // We can't change themes after inflation, in this case we'll have to recreate
850 // the whole activity.
851 if (mBackgroundGradient != null) {
852 recreate();
853 return;
854 }
855
856 int vis = getWindow().getDecorView().getSystemUiVisibility();
857 if (supportsDarkText) {
858 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
859 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
860 setTheme(R.style.EmergencyDialerThemeDark);
861 } else {
862 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
863 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
864 setTheme(R.style.EmergencyDialerTheme);
865 }
866 getWindow().getDecorView().setSystemUiVisibility(vis);
867 }
868
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 /**
870 * place the call, but check to make sure it is a viable number.
871 */
872 private void placeCall() {
Shaotang Li8662a912018-07-04 16:53:01 +0800873 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700874 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900875
876 // Convert into emergency number according to emergency conversion map.
877 // If conversion map is not defined (this is default), this method does
878 // nothing and just returns input number.
879 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
880
Yorke Lee36bb2542014-06-05 08:09:52 -0700881 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
883
884 // place the call if it is a valid number
885 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
886 // There is no number entered.
887 playTone(ToneGenerator.TONE_PROP_NACK);
888 return;
889 }
Shaotang Li8662a912018-07-04 16:53:01 +0800890
891 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
892 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
893
Shaotang Li5d906c12018-07-19 17:32:42 +0800894 Bundle extras = new Bundle();
895 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
896 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnfa77e202018-03-23 07:47:00 -0700897 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800898 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700899 } else {
900 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
901
Shaotang Li8662a912018-07-04 16:53:01 +0800902 // We interest on the context when user has intention to make phone call,
903 // so write metrics here for non-emergency numbers even these numbers are rejected.
904 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
905 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
906
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700907 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
908 }
Yorke Lee9b341512014-10-17 11:36:41 -0700909 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700910 }
911
912 /**
913 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
914 *
915 * The tone is played locally, using the audio stream for phone calls.
916 * Tones are played only if the "Audible touch tones" user preference
917 * is checked, and are NOT played if the device is in silent mode.
918 *
919 * @param tone a tone code from {@link ToneGenerator}
920 */
921 void playTone(int tone) {
922 // if local tone playback is disabled, just return.
923 if (!mDTMFToneEnabled) {
924 return;
925 }
926
927 // Also do nothing if the phone is in silent mode.
928 // We need to re-check the ringer mode for *every* playTone()
929 // call, rather than keeping a local flag that's updated in
930 // onResume(), since it's possible to toggle silent mode without
931 // leaving the current activity (via the ENDCALL-longpress menu.)
932 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
933 int ringerMode = audioManager.getRingerMode();
934 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800935 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 return;
937 }
938
939 synchronized (mToneGeneratorLock) {
940 if (mToneGenerator == null) {
941 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
942 return;
943 }
944
945 // Start the new tone (will stop any playing tone)
946 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
947 }
948 }
949
950 private CharSequence createErrorMessage(String number) {
951 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800952 String errorString = getString(R.string.dial_emergency_error, number);
953 int startingPosition = errorString.indexOf(number);
954 int endingPosition = startingPosition + number.length();
955 Spannable result = new SpannableString(errorString);
956 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
957 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 } else {
959 return getText(R.string.dial_emergency_empty_error).toString();
960 }
961 }
962
963 @Override
964 protected Dialog onCreateDialog(int id) {
965 AlertDialog dialog = null;
966 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
967 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700968 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
970 .setMessage(createErrorMessage(mLastNumber))
971 .setPositiveButton(R.string.ok, null)
972 .setCancelable(true).create();
973
974 // blur stuff behind the dialog
975 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700976 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 }
978 return dialog;
979 }
980
981 @Override
982 protected void onPrepareDialog(int id, Dialog dialog) {
983 super.onPrepareDialog(id, dialog);
984 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
985 AlertDialog alert = (AlertDialog) dialog;
986 alert.setMessage(createErrorMessage(mLastNumber));
987 }
988 }
989
Andrew Leed5631e82014-10-08 16:03:58 -0700990 @Override
991 public boolean onOptionsItemSelected(MenuItem item) {
992 final int itemId = item.getItemId();
993 if (itemId == android.R.id.home) {
994 onBackPressed();
995 return true;
996 }
997 return super.onOptionsItemSelected(item);
998 }
999
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 /**
1001 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
1002 */
1003 private void updateDialAndDeleteButtonStateEnabledAttr() {
1004 final boolean notEmpty = mDigits.length() != 0;
1005
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001006 mDelete.setEnabled(notEmpty);
1007 }
Yorke Lee91311662014-10-24 14:50:45 -07001008
1009 /**
1010 * Remove the digit just before the current position. Used by various long pressed callbacks
1011 * to remove the digit that was populated as a result of the short click.
1012 */
1013 private void removePreviousDigitIfPossible() {
1014 final int currentPosition = mDigits.getSelectionStart();
1015 if (currentPosition > 0) {
1016 mDigits.setSelection(currentPosition);
1017 mDigits.getText().delete(currentPosition - 1, currentPosition);
1018 }
1019 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -08001020
1021 /**
1022 * Update the text-to-speech annotations in the edit field.
1023 */
1024 private void updateTtsSpans() {
1025 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
1026 mDigits.getText().removeSpan(o);
1027 }
1028 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
1029 }
Lucas Dupineb9c5702017-05-10 16:57:09 -07001030
1031 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -07001032 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -07001033 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -07001034 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
1035 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -07001036 mBackgroundGradient.setColors(colors);
1037 updateTheme(colors.supportsDarkText());
1038 }
1039 }
Tyler Gunnc5428972018-03-28 14:15:34 -07001040
1041 /**
1042 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1043 * add hint text above the dial pad which warns the user of this case.
1044 */
1045 private void maybeShowWfcEmergencyCallingWarning() {
1046 if (!mIsWfcEmergencyCallingWarningEnabled) {
1047 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1048 return;
1049 }
1050
1051 // Use an async task rather than calling into Telephony on UI thread.
1052 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1053 @Override
1054 protected Boolean doInBackground(Void... voids) {
1055 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1056 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1057 ServiceState ss = tm.getServiceState();
1058 boolean isCellAvailable =
1059 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1060 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +08001061 + " isCellAvailable=" + isCellAvailable
1062 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -07001063 return isWfcAvailable && !isCellAvailable;
1064 }
1065
1066 @Override
1067 protected void onPostExecute(Boolean result) {
1068 if (result.booleanValue()) {
1069 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1070 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1071 } else {
1072 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1073 mDigits.setHint("");
1074 }
1075 maybeChangeHintSize();
1076 }
1077 };
1078 showWfcWarningTask.execute((Void) null);
1079 }
1080
1081 /**
1082 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1083 * edit view and set the font size to a smaller size appropriate for the emergency calling
1084 * warning.
1085 */
1086 private void maybeChangeHintSize() {
1087 if (TextUtils.isEmpty(mDigits.getHint())
1088 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1089 // No hint or there are dialed digits, so use default size.
1090 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1091 // By default, the digits view auto-resizes to fit the text it contains, so
1092 // enable that now.
1093 mDigits.setResizeEnabled(true);
1094 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1095 } else {
1096 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1097 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1098 R.dimen.emergency_call_warning_size));
1099 // Since we're populating this with a static text string, disable auto-resize.
1100 mDigits.setResizeEnabled(false);
1101 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1102 }
1103 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001104
1105 private void setupEmergencyShortcutsView() {
1106 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1107 mDialpadView = findViewById(R.id.emergency_dialer);
1108
Chihhang Chuangcaba0da2018-08-02 22:25:06 +08001109 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1110 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1111
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001112 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1113 dialpadButton.setOnClickListener(this);
1114
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001115 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001116
1117 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1118 mEmergencyActionGroup.setVisibility(View.GONE);
1119
1120 // Setup dialpad title.
1121 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1122 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1123
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001124 mEmergencyShortcutButtonList = new ArrayList<>();
1125 setupEmergencyCallShortcutButton();
1126
CY Cheng9a69c182018-06-15 21:20:10 +08001127 updateLocationAndEccInfo(null, null);
1128
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001129 switchView(mEmergencyShortcutView, mDialpadView, false);
1130 }
1131
CY Cheng9a69c182018-06-15 21:20:10 +08001132 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001133 final View locationInfo = findViewById(R.id.location_info);
1134
CY Cheng9a69c182018-06-15 21:20:10 +08001135 String countryName = null;
1136 if (!TextUtils.isEmpty(countryIso)) {
1137 Locale locale = Locale.getDefault();
1138 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1139 .getDisplayCountry();
1140 }
1141 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001142 locationInfo.setVisibility(View.INVISIBLE);
1143 } else {
1144 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001145 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001146 locationInfo.setVisibility(View.VISIBLE);
1147 }
1148 }
1149
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001150 private void setupEmergencyCallShortcutButton() {
1151 final ViewGroup shortcutButtonContainer = findViewById(
1152 R.id.emergency_shortcut_buttons_container);
1153 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001154 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001155
CY Cheng9a69c182018-06-15 21:20:10 +08001156 mShortcutAdapter = new EccShortcutAdapter(this) {
1157 @Override
1158 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1159 CharSequence description, int iconRes) {
1160 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1161 .inflate(R.layout.emergency_shortcut_button, parent, false);
1162 button.setPhoneNumber(number);
1163 button.setPhoneDescription(description);
1164 button.setPhoneTypeIcon(iconRes);
1165 button.setOnConfirmClickListener(EmergencyDialer.this);
1166 return button;
1167 }
1168 };
1169 mShortcutDataSetObserver = new DataSetObserver() {
1170 @Override
1171 public void onChanged() {
1172 super.onChanged();
1173 updateLayout();
1174 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001175
CY Cheng9a69c182018-06-15 21:20:10 +08001176 @Override
1177 public void onInvalidated() {
1178 super.onInvalidated();
1179 updateLayout();
1180 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001181
CY Cheng9a69c182018-06-15 21:20:10 +08001182 private void updateLayout() {
1183 // clear previous added buttons
1184 shortcutButtonContainer.removeAllViews();
1185 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001186
CY Cheng9a69c182018-06-15 21:20:10 +08001187 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1188 EmergencyShortcutButton button = (EmergencyShortcutButton)
1189 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1190 mEmergencyShortcutButtonList.add(button);
1191 shortcutButtonContainer.addView(button);
1192 }
1193
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001194 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001195 if (mEmergencyShortcutButtonList.size() > 1) {
1196 emergencyNumberTitle.setText(getString(
1197 R.string.numerous_emergency_numbers_title));
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001198 // Update mEmergencyInfoGroup margin to avoid UI overlay when
1199 // emergency shortcut button more than 2.
1200 if (mEmergencyShortcutButtonList.size() > 2) {
1201 mEmergencyInfoGroup.updateLayoutMargin();
1202 }
CY Cheng9a69c182018-06-15 21:20:10 +08001203 } else {
1204 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1205 }
1206 }
1207 };
1208 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1209 }
1210
1211 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1212 if (!isFinishing() && !isDestroyed()) {
1213 setLocationInfo(iso);
1214 if (mShortcutAdapter != null) {
1215 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1216 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001217 }
1218 }
1219
1220 /**
1221 * Called by the activity before a touch event is dispatched to the view hierarchy.
1222 */
1223 private void onPreTouchEvent(MotionEvent event) {
1224 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001225 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001226
1227 if (mEmergencyShortcutButtonList != null) {
1228 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1229 button.onPreTouchEvent(event);
1230 }
1231 }
1232 }
1233
1234 /**
1235 * Called by the activity after a touch event is dispatched to the view hierarchy.
1236 */
1237 private void onPostTouchEvent(MotionEvent event) {
1238 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001239 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001240
1241 if (mEmergencyShortcutButtonList != null) {
1242 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1243 button.onPostTouchEvent(event);
1244 }
1245 }
1246 }
1247
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001248 /**
1249 * Switch two view.
1250 *
1251 * @param displayView the view would be displayed.
1252 * @param hideView the view would be hidden.
1253 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1254 */
1255 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1256 if (displayView == null || hideView == null) {
1257 return;
1258 }
1259
1260 if (displayView.getVisibility() == View.VISIBLE) {
1261 return;
1262 }
1263
1264 if (hasAnimation) {
1265 crossfade(hideView, displayView);
1266 } else {
1267 hideView.setVisibility(View.GONE);
1268 displayView.setVisibility(View.VISIBLE);
1269 }
1270 }
1271
1272 /**
1273 * Fade out and fade in animation between two view transition.
1274 */
1275 private void crossfade(View fadeOutView, View fadeInView) {
1276 if (fadeOutView == null || fadeInView == null) {
1277 return;
1278 }
1279 final int shortAnimationDuration = getResources().getInteger(
1280 android.R.integer.config_shortAnimTime);
1281
1282 fadeInView.setAlpha(0f);
1283 fadeInView.setVisibility(View.VISIBLE);
1284
1285 fadeInView.animate()
1286 .alpha(1f)
1287 .setDuration(shortAnimationDuration)
1288 .setListener(null);
1289
1290 fadeOutView.animate()
1291 .alpha(0f)
1292 .setDuration(shortAnimationDuration)
1293 .setListener(new AnimatorListenerAdapter() {
1294 @Override
1295 public void onAnimationEnd(Animator animation) {
1296 fadeOutView.setVisibility(View.GONE);
1297 }
1298 });
1299 }
Shaotang Li8662a912018-07-04 16:53:01 +08001300
1301 @Override
1302 public void onSensorChanged(SensorEvent event) {
1303 float distance = event.values[0];
1304 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1305 }
1306
1307 @Override
1308 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1309 // Not used.
1310 }
1311
1312 private boolean isShortcutNumber(String number) {
1313 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1314 return false;
1315 }
1316
1317 boolean isShortcut = false;
1318 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1319 if (button != null && number.equals(button.getPhoneNumber())) {
1320 isShortcut = true;
1321 break;
1322 }
1323 }
1324 return isShortcut;
1325 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326}