blob: b7f695d03772a748d2fad0c410d11937698fac12 [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 Chuang92cfe512018-06-07 16:25:27 +080072import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import android.view.WindowManager;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080074import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075
Lucas Dupinaf9e9912017-06-22 12:39:39 -070076import com.android.internal.colorextraction.ColorExtractor;
77import com.android.internal.colorextraction.ColorExtractor.GradientColors;
78import com.android.internal.colorextraction.drawable.GradientDrawable;
Shaotang Li8662a912018-07-04 16:53:01 +080079import com.android.internal.logging.MetricsLogger;
80import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Yorke Lee23a70732014-08-14 17:12:01 -070081import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070082import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070083import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080084import com.android.phone.ecc.CountryEccInfo;
85import com.android.phone.ecc.EccInfoHelper;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086
Chihhang Chuang92cfe512018-06-07 16:25:27 +080087import java.util.ArrayList;
88import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080089import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080090
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091/**
92 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
93 *
94 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
95 * activity from apps/Contacts) that:
96 * 1. Allows ONLY emergency calls to be dialed
97 * 2. Disallows voicemail functionality
98 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
99 * activity to stay in front of the keyguard.
100 *
101 * TODO: Even though this is an ultra-simplified version of the normal
102 * dialer, there's still lots of code duplication between this class and
103 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
104 * moved into a shared base class that would live in the framework?
105 * Or could we figure out some way to move *this* class into apps/Contacts
106 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800107 *
108 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800109 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
110 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800111 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800112 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113 */
114public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700115 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800116 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
Shaotang Li8662a912018-07-04 16:53:01 +0800117 EmergencyShortcutButton.OnConfirmClickListener, SensorEventListener {
118
119 private class MetricsWriter {
120 // Metrics constants indicating the entry type that user opened emergency dialer.
121 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
122 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
123 public static final int ENTRY_TYPE_UNKNOWN = 0;
124 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
125 public static final int ENTRY_TYPE_POWER_MENU = 2;
126
127 // Metrics constants indicating the UI that user made phone call.
128 public static final int CALL_SOURCE_DIALPAD = 0;
129 public static final int CALL_SOURCE_SHORTCUT = 1;
130
131 // Metrics constants indicating the phone number type of a call user made.
132 public static final int PHONE_NUMBER_TYPE_GENERAL = 0;
133 public static final int PHONE_NUMBER_TYPE_EMERGENCY = 1;
134
135 // Metrics constants indicating the actions performed by user.
136 public static final int USER_ACTION_NONE = 0x0;
137 public static final int USER_ACTION_OPEN_DIALPAD = 0x1;
138 public static final int USER_ACTION_OPEN_EMERGENCY_INFO = 0x2;
139 public static final int USER_ACTION_MAKE_CALL_VIA_DIALPAD = 0x4;
140 public static final int USER_ACTION_MAKE_CALL_VIA_SHORTCUT = 0x8;
141
142 private MetricsLogger mMetricsLogger = new MetricsLogger();
143
144 public void writeMetricsForEnter() {
145 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
146 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
147 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
148 .setType(MetricsEvent.TYPE_OPEN)
149 .setSubtype(entryType)
150 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IS_SCREEN_LOCKED,
151 keyguard.isKeyguardLocked() ? 1 : 0));
152 }
153
154 public void writeMetricsForExit() {
155 int entryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
156 long userStayDuration = SystemClock.elapsedRealtime() - mUserEnterTimeMillis;
157 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER)
158 .setType(MetricsEvent.TYPE_CLOSE)
159 .setSubtype(entryType)
160 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_USER_ACTIONS, mUserActions)
161 .addTaggedData(
162 MetricsEvent.FIELD_EMERGENCY_DIALER_DURATION_MS, userStayDuration));
163 }
164
165 public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
166 boolean hasShortcut) {
167 mMetricsLogger.write(new LogMaker(MetricsEvent.EMERGENCY_DIALER_MAKE_CALL)
168 .setType(MetricsEvent.TYPE_ACTION)
169 .setSubtype(callSource)
170 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_TYPE,
171 phoneNumberType)
172 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_PHONE_NUMBER_HAS_SHORTCUT,
173 hasShortcut ? 1 : 0)
174 .addTaggedData(MetricsEvent.FIELD_EMERGENCY_DIALER_IN_POCKET,
175 mIsProximityNear ? 1 : 0));
176 }
177 }
178
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179 // Keys used with onSaveInstanceState().
180 private static final String LAST_NUMBER = "lastNumber";
181
182 // Intent action for this activity.
183 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
184
Shaotang Li8662a912018-07-04 16:53:01 +0800185 /**
186 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
187 * the emergency dialer.
188 */
189 public static final String EXTRA_ENTRY_TYPE =
190 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
191
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 // List of dialer button IDs.
193 private static final int[] DIALER_KEYS = new int[] {
194 R.id.one, R.id.two, R.id.three,
195 R.id.four, R.id.five, R.id.six,
196 R.id.seven, R.id.eight, R.id.nine,
197 R.id.star, R.id.zero, R.id.pound };
198
199 // Debug constants.
200 private static final boolean DBG = false;
201 private static final String LOG_TAG = "EmergencyDialer";
202
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 /** The length of DTMF tones in milliseconds */
204 private static final int TONE_LENGTH_MS = 150;
205
206 /** The DTMF tone volume relative to other sounds in the stream */
207 private static final int TONE_RELATIVE_VOLUME = 80;
208
209 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
210 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
211
212 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
213
Lucas Dupineb9c5702017-05-10 16:57:09 -0700214 /** 90% opacity, different from other gradients **/
215 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
216
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800217 /** 85% opacity for black background **/
218 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
219
CY Cheng9a69c182018-06-15 21:20:10 +0800220 /** Size limit of emergency shortcut buttons container. **/
221 private static final int SHORTCUT_SIZE_LIMIT = 3;
222
Tyler Gunnc5428972018-03-28 14:15:34 -0700223 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 private View mDialButton;
225 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800226 private View mEmergencyShortcutView;
227 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228
CY Cheng9a69c182018-06-15 21:20:10 +0800229 private EccInfoHelper mEccInfoHelper;
230
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800231 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800232 private EccShortcutAdapter mShortcutAdapter;
233 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800234
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235 private ToneGenerator mToneGenerator;
236 private Object mToneGeneratorLock = new Object();
237
238 // determines if we want to playback local DTMF tones.
239 private boolean mDTMFToneEnabled;
240
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700241 private EmergencyActionGroup mEmergencyActionGroup;
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
253 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
254
Lucas Dupineb9c5702017-05-10 16:57:09 -0700255 // Background gradient
256 private ColorExtractor mColorExtractor;
257 private GradientDrawable mBackgroundGradient;
258 private boolean mSupportsDarkText;
259
Tyler Gunnc5428972018-03-28 14:15:34 -0700260 private boolean mIsWfcEmergencyCallingWarningEnabled;
261 private float mDefaultDigitsTextSize;
262
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800263 private boolean mAreEmergencyDialerShortcutsEnabled;
264
Shaotang Li8662a912018-07-04 16:53:01 +0800265 private MetricsWriter mMetricsWriter;
266 private SensorManager mSensorManager;
267 private Sensor mProximitySensor;
268 private boolean mIsProximityNear = false;
269
270 /**
271 * The time, in millis, since boot when user opened emergency dialer.
272 * This is used when calculating the user stay duration for metrics data.
273 */
274 private long mUserEnterTimeMillis = 0;
275
276 /**
277 * Bit flag indicating the actions performed by user. This is used for metrics data.
278 */
279 private int mUserActions = MetricsWriter.USER_ACTION_NONE;
280
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 @Override
282 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
283 // Do nothing
284 }
285
286 @Override
287 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700288 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 }
290
291 @Override
292 public void afterTextChanged(Editable input) {
293 // Check for special sequences, in particular the "**04" or "**05"
294 // sequences that allow you to enter PIN or PUK-related codes.
295 //
296 // But note we *don't* allow most other special sequences here,
297 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
298 // since those shouldn't be available if the device is locked.
299 //
300 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
301 // here, not the regular handleChars() method.
302 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
303 // A special sequence was entered, clear the digits
304 mDigits.getText().clear();
305 }
306
307 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800308 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 }
310
311 @Override
312 protected void onCreate(Bundle icicle) {
313 super.onCreate(icicle);
314
Shaotang Li8662a912018-07-04 16:53:01 +0800315 mMetricsWriter = new MetricsWriter();
316 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
317 if (mSensorManager != null) {
318 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
319 }
320
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700321 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800322 setShowWhenLocked(true);
323 // Allow turning screen on
324 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800326 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
327 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng9a69c182018-06-15 21:20:10 +0800328 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
329 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800330
Lucas Dupineb9c5702017-05-10 16:57:09 -0700331 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800332
333 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
334 // And the background color is black with 85% opacity.
335 if (mAreEmergencyDialerShortcutsEnabled) {
336 updateTheme(false);
337 } else {
338 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
339 ColorExtractor.TYPE_EXTRA_DARK);
340 updateTheme(lockScreenColors.supportsDarkText());
341 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700342
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 setContentView(R.layout.emergency_dialer);
344
Tyler Gunnc5428972018-03-28 14:15:34 -0700345 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346 mDigits.setKeyListener(DialerKeyListener.getInstance());
347 mDigits.setOnClickListener(this);
348 mDigits.setOnKeyListener(this);
349 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700350 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700351 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 maybeAddNumberFormatting();
353
Lucas Dupineb9c5702017-05-10 16:57:09 -0700354 mBackgroundGradient = new GradientDrawable(this);
355 Point displaySize = new Point();
356 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
357 .getDefaultDisplay().getSize(displaySize);
358 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800359 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
360 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700361 getWindow().setBackgroundDrawable(mBackgroundGradient);
362
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700363 // Check for the presence of the keypad
364 View view = findViewById(R.id.one);
365 if (view != null) {
366 setupKeypad();
367 }
368
369 mDelete = findViewById(R.id.deleteButton);
370 mDelete.setOnClickListener(this);
371 mDelete.setOnLongClickListener(this);
372
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700373 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374
375 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700376 // Read carrier config through the public API because PhoneGlobals is not available when we
377 // run as a secondary user.
378 CarrierConfigManager configMgr =
379 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
380 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800381 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700382
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700383 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 mDialButton.setOnClickListener(this);
385 } else {
386 mDialButton.setVisibility(View.GONE);
387 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700388 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
389 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
390 maybeShowWfcEmergencyCallingWarning();
391
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700392 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700393
394 if (icicle != null) {
395 super.onRestoreInstanceState(icicle);
396 }
397
398 // Extract phone number from intent
399 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700400 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700401 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
402 if (number != null) {
403 mDigits.setText(number);
404 }
405 }
406
407 // if the mToneGenerator creation fails, just continue without it. It is
408 // a local audio signal, and is not as important as the dtmf tone itself.
409 synchronized (mToneGeneratorLock) {
410 if (mToneGenerator == null) {
411 try {
412 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
413 } catch (RuntimeException e) {
414 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
415 mToneGenerator = null;
416 }
417 }
418 }
419
420 final IntentFilter intentFilter = new IntentFilter();
421 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
422 registerReceiver(mBroadcastReceiver, intentFilter);
423
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700424 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800425
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800426 if (mAreEmergencyDialerShortcutsEnabled) {
427 setupEmergencyShortcutsView();
428 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 }
430
431 @Override
432 protected void onDestroy() {
433 super.onDestroy();
434 synchronized (mToneGeneratorLock) {
435 if (mToneGenerator != null) {
436 mToneGenerator.release();
437 mToneGenerator = null;
438 }
439 }
440 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800441 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
442 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
443 mShortcutDataSetObserver = null;
444 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700445 }
446
447 @Override
448 protected void onRestoreInstanceState(Bundle icicle) {
449 mLastNumber = icicle.getString(LAST_NUMBER);
450 }
451
452 @Override
453 protected void onSaveInstanceState(Bundle outState) {
454 super.onSaveInstanceState(outState);
455 outState.putString(LAST_NUMBER, mLastNumber);
456 }
457
458 /**
459 * Explicitly turn off number formatting, since it gets in the way of the emergency
460 * number detector
461 */
462 protected void maybeAddNumberFormatting() {
463 // Do nothing.
464 }
465
466 @Override
467 protected void onPostCreate(Bundle savedInstanceState) {
468 super.onPostCreate(savedInstanceState);
469
470 // This can't be done in onCreate(), since the auto-restoring of the digits
471 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
472 // is called. This method will be called every time the activity is created, and
473 // will always happen after onRestoreSavedInstanceState().
474 mDigits.addTextChangedListener(this);
475 }
476
477 private void setupKeypad() {
478 // Setup the listeners for the buttons
479 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700480 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
481 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700482 }
483
484 View view = findViewById(R.id.zero);
485 view.setOnLongClickListener(this);
486 }
487
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800488 @Override
489 public void onBackPressed() {
490 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800491 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800492 // Otherwise, it would finish the activity.
493 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
494 && mDialpadView.getVisibility() == View.VISIBLE) {
495 switchView(mEmergencyShortcutView, mDialpadView, true);
496 return;
497 }
498 super.onBackPressed();
499 }
500
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501 /**
502 * handle key events
503 */
504 @Override
505 public boolean onKeyDown(int keyCode, KeyEvent event) {
506 switch (keyCode) {
507 // Happen when there's a "Call" hard button.
508 case KeyEvent.KEYCODE_CALL: {
509 if (TextUtils.isEmpty(mDigits.getText().toString())) {
510 // if we are adding a call from the InCallScreen and the phone
511 // number entered is empty, we just close the dialer to expose
512 // the InCallScreen under it.
513 finish();
514 } else {
515 // otherwise, we place the call.
516 placeCall();
517 }
518 return true;
519 }
520 }
521 return super.onKeyDown(keyCode, event);
522 }
523
524 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700525 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700526 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
527 mDigits.onKeyDown(keyCode, event);
528 }
529
530 @Override
531 public boolean onKey(View view, int keyCode, KeyEvent event) {
532 switch (view.getId()) {
533 case R.id.digits:
534 // Happen when "Done" button of the IME is pressed. This can happen when this
535 // Activity is forced into landscape mode due to a desk dock.
536 if (keyCode == KeyEvent.KEYCODE_ENTER
537 && event.getAction() == KeyEvent.ACTION_UP) {
538 placeCall();
539 return true;
540 }
541 break;
542 }
543 return false;
544 }
545
546 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700547 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800548 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700549 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800550 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700551 return handled;
552 }
553
554 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800555 public void onConfirmClick(EmergencyShortcutButton button) {
556 if (button == null) return;
557
Shaotang Li8662a912018-07-04 16:53:01 +0800558 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_SHORTCUT;
559
560 // We interest on the context when user has intention to make phone call,
561 // so write metrics here for shortcut number even the call may not be created.
562 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_SHORTCUT,
563 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, true);
564
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800565 String phoneNumber = button.getPhoneNumber();
566
567 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800568 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Li5d906c12018-07-19 17:32:42 +0800569 Bundle extras = new Bundle();
570 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
571 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800572 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800573 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800574 } else {
575 Log.d(LOG_TAG, "emergency number is empty");
576 }
577 }
578
579 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 public void onClick(View view) {
581 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700582 case R.id.deleteButton: {
583 keyPressed(KeyEvent.KEYCODE_DEL);
584 return;
585 }
586 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700587 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700588 placeCall();
589 return;
590 }
591 case R.id.digits: {
592 if (mDigits.length() != 0) {
593 mDigits.setCursorVisible(true);
594 }
595 return;
596 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800597 case R.id.floating_action_button_dialpad: {
Shaotang Li8662a912018-07-04 16:53:01 +0800598 mUserActions |= MetricsWriter.USER_ACTION_OPEN_DIALPAD;
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800599 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800600 switchView(mDialpadView, mEmergencyShortcutView, true);
601 return;
602 }
603 case R.id.emergency_info_button: {
Shaotang Li8662a912018-07-04 16:53:01 +0800604 mUserActions |= MetricsWriter.USER_ACTION_OPEN_EMERGENCY_INFO;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800605 Intent intent = (Intent) view.getTag(R.id.tag_intent);
606 if (intent != null) {
607 startActivity(intent);
608 }
609 return;
610 }
Yorke Lee23a70732014-08-14 17:12:01 -0700611 }
612 }
613
614 @Override
615 public void onPressed(View view, boolean pressed) {
616 if (!pressed) {
617 return;
618 }
619 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620 case R.id.one: {
621 playTone(ToneGenerator.TONE_DTMF_1);
622 keyPressed(KeyEvent.KEYCODE_1);
623 return;
624 }
625 case R.id.two: {
626 playTone(ToneGenerator.TONE_DTMF_2);
627 keyPressed(KeyEvent.KEYCODE_2);
628 return;
629 }
630 case R.id.three: {
631 playTone(ToneGenerator.TONE_DTMF_3);
632 keyPressed(KeyEvent.KEYCODE_3);
633 return;
634 }
635 case R.id.four: {
636 playTone(ToneGenerator.TONE_DTMF_4);
637 keyPressed(KeyEvent.KEYCODE_4);
638 return;
639 }
640 case R.id.five: {
641 playTone(ToneGenerator.TONE_DTMF_5);
642 keyPressed(KeyEvent.KEYCODE_5);
643 return;
644 }
645 case R.id.six: {
646 playTone(ToneGenerator.TONE_DTMF_6);
647 keyPressed(KeyEvent.KEYCODE_6);
648 return;
649 }
650 case R.id.seven: {
651 playTone(ToneGenerator.TONE_DTMF_7);
652 keyPressed(KeyEvent.KEYCODE_7);
653 return;
654 }
655 case R.id.eight: {
656 playTone(ToneGenerator.TONE_DTMF_8);
657 keyPressed(KeyEvent.KEYCODE_8);
658 return;
659 }
660 case R.id.nine: {
661 playTone(ToneGenerator.TONE_DTMF_9);
662 keyPressed(KeyEvent.KEYCODE_9);
663 return;
664 }
665 case R.id.zero: {
666 playTone(ToneGenerator.TONE_DTMF_0);
667 keyPressed(KeyEvent.KEYCODE_0);
668 return;
669 }
670 case R.id.pound: {
671 playTone(ToneGenerator.TONE_DTMF_P);
672 keyPressed(KeyEvent.KEYCODE_POUND);
673 return;
674 }
675 case R.id.star: {
676 playTone(ToneGenerator.TONE_DTMF_S);
677 keyPressed(KeyEvent.KEYCODE_STAR);
678 return;
679 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 }
681 }
682
683 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700684 * called for long touch events
685 */
686 @Override
687 public boolean onLongClick(View view) {
688 int id = view.getId();
689 switch (id) {
690 case R.id.deleteButton: {
691 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700692 return true;
693 }
694 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700695 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700696 keyPressed(KeyEvent.KEYCODE_PLUS);
697 return true;
698 }
699 }
700 return false;
701 }
702
703 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700704 protected void onStart() {
705 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800706
707 mUserEnterTimeMillis = SystemClock.elapsedRealtime();
708 mUserActions = MetricsWriter.USER_ACTION_NONE;
709 mMetricsWriter.writeMetricsForEnter();
710
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800711 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
712 // And set background color to black.
713 if (mAreEmergencyDialerShortcutsEnabled) {
714 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
715 updateTheme(false);
716 } else {
717 mColorExtractor.addOnColorsChangedListener(this);
718 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
719 ColorExtractor.TYPE_EXTRA_DARK);
720 // Do not animate when view isn't visible yet, just set an initial state.
721 mBackgroundGradient.setColors(lockScreenColors, false);
722 updateTheme(lockScreenColors.supportsDarkText());
723 }
CY Cheng9a69c182018-06-15 21:20:10 +0800724
725 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
726 final Context context = this;
727 mEccInfoHelper.getCountryEccInfoAsync(context,
728 new EccInfoHelper.CountryEccInfoResultCallback() {
729 @Override
730 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
731 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
732 + Rlog.pii(LOG_TAG, iso));
733 updateLocationAndEccInfo(iso, countryEccInfo);
734 }
735
736 @Override
737 public void onDetectCountryFailed() {
738 Log.w(LOG_TAG, "Cannot detect current country.");
739 updateLocationAndEccInfo(null, null);
740 }
741
742 @Override
743 public void onRetrieveCountryEccInfoFailed(String iso) {
744 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
745 + Rlog.pii(LOG_TAG, iso));
746 updateLocationAndEccInfo(iso, null);
747 }
748 });
749 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700750 }
751
752 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700753 protected void onResume() {
754 super.onResume();
755
Shaotang Li8662a912018-07-04 16:53:01 +0800756 if (mProximitySensor != null) {
757 mSensorManager.registerListener(
758 this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
759 }
760
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700761 // retrieve the DTMF tone play back setting.
762 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
763 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
764
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700765 // if the mToneGenerator creation fails, just continue without it. It is
766 // a local audio signal, and is not as important as the dtmf tone itself.
767 synchronized (mToneGeneratorLock) {
768 if (mToneGenerator == null) {
769 try {
770 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
771 TONE_RELATIVE_VOLUME);
772 } catch (RuntimeException e) {
773 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
774 mToneGenerator = null;
775 }
776 }
777 }
778
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779 updateDialAndDeleteButtonStateEnabledAttr();
780 }
781
782 @Override
783 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 super.onPause();
Shaotang Li8662a912018-07-04 16:53:01 +0800785 if (mProximitySensor != null) {
786 mSensorManager.unregisterListener(this, mProximitySensor);
787 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 }
789
Lucas Dupineb9c5702017-05-10 16:57:09 -0700790 @Override
791 protected void onStop() {
792 super.onStop();
Shaotang Li8662a912018-07-04 16:53:01 +0800793 mMetricsWriter.writeMetricsForExit();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700794 mColorExtractor.removeOnColorsChangedListener(this);
795 }
796
797 /**
798 * Sets theme based on gradient colors
799 * @param supportsDarkText true if gradient supports dark text
800 */
801 private void updateTheme(boolean supportsDarkText) {
802 if (mSupportsDarkText == supportsDarkText) {
803 return;
804 }
805 mSupportsDarkText = supportsDarkText;
806
807 // We can't change themes after inflation, in this case we'll have to recreate
808 // the whole activity.
809 if (mBackgroundGradient != null) {
810 recreate();
811 return;
812 }
813
814 int vis = getWindow().getDecorView().getSystemUiVisibility();
815 if (supportsDarkText) {
816 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
817 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
818 setTheme(R.style.EmergencyDialerThemeDark);
819 } else {
820 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
821 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
822 setTheme(R.style.EmergencyDialerTheme);
823 }
824 getWindow().getDecorView().setSystemUiVisibility(vis);
825 }
826
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 /**
828 * place the call, but check to make sure it is a viable number.
829 */
830 private void placeCall() {
Shaotang Li8662a912018-07-04 16:53:01 +0800831 mUserActions |= MetricsWriter.USER_ACTION_MAKE_CALL_VIA_DIALPAD;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700832 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900833
834 // Convert into emergency number according to emergency conversion map.
835 // If conversion map is not defined (this is default), this method does
836 // nothing and just returns input number.
837 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
838
Yorke Lee36bb2542014-06-05 08:09:52 -0700839 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
841
842 // place the call if it is a valid number
843 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
844 // There is no number entered.
845 playTone(ToneGenerator.TONE_PROP_NACK);
846 return;
847 }
Shaotang Li8662a912018-07-04 16:53:01 +0800848
849 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
850 MetricsWriter.PHONE_NUMBER_TYPE_EMERGENCY, isShortcutNumber(mLastNumber));
851
Shaotang Li5d906c12018-07-19 17:32:42 +0800852 Bundle extras = new Bundle();
853 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
854 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnfa77e202018-03-23 07:47:00 -0700855 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800856 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 } else {
858 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
859
Shaotang Li8662a912018-07-04 16:53:01 +0800860 // We interest on the context when user has intention to make phone call,
861 // so write metrics here for non-emergency numbers even these numbers are rejected.
862 mMetricsWriter.writeMetricsForMakingCall(MetricsWriter.CALL_SOURCE_DIALPAD,
863 MetricsWriter.PHONE_NUMBER_TYPE_GENERAL, false);
864
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
866 }
Yorke Lee9b341512014-10-17 11:36:41 -0700867 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700868 }
869
870 /**
871 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
872 *
873 * The tone is played locally, using the audio stream for phone calls.
874 * Tones are played only if the "Audible touch tones" user preference
875 * is checked, and are NOT played if the device is in silent mode.
876 *
877 * @param tone a tone code from {@link ToneGenerator}
878 */
879 void playTone(int tone) {
880 // if local tone playback is disabled, just return.
881 if (!mDTMFToneEnabled) {
882 return;
883 }
884
885 // Also do nothing if the phone is in silent mode.
886 // We need to re-check the ringer mode for *every* playTone()
887 // call, rather than keeping a local flag that's updated in
888 // onResume(), since it's possible to toggle silent mode without
889 // leaving the current activity (via the ENDCALL-longpress menu.)
890 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
891 int ringerMode = audioManager.getRingerMode();
892 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
893 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
894 return;
895 }
896
897 synchronized (mToneGeneratorLock) {
898 if (mToneGenerator == null) {
899 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
900 return;
901 }
902
903 // Start the new tone (will stop any playing tone)
904 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
905 }
906 }
907
908 private CharSequence createErrorMessage(String number) {
909 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800910 String errorString = getString(R.string.dial_emergency_error, number);
911 int startingPosition = errorString.indexOf(number);
912 int endingPosition = startingPosition + number.length();
913 Spannable result = new SpannableString(errorString);
914 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
915 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 } else {
917 return getText(R.string.dial_emergency_empty_error).toString();
918 }
919 }
920
921 @Override
922 protected Dialog onCreateDialog(int id) {
923 AlertDialog dialog = null;
924 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
925 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700926 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
928 .setMessage(createErrorMessage(mLastNumber))
929 .setPositiveButton(R.string.ok, null)
930 .setCancelable(true).create();
931
932 // blur stuff behind the dialog
933 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700934 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 }
936 return dialog;
937 }
938
939 @Override
940 protected void onPrepareDialog(int id, Dialog dialog) {
941 super.onPrepareDialog(id, dialog);
942 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
943 AlertDialog alert = (AlertDialog) dialog;
944 alert.setMessage(createErrorMessage(mLastNumber));
945 }
946 }
947
Andrew Leed5631e82014-10-08 16:03:58 -0700948 @Override
949 public boolean onOptionsItemSelected(MenuItem item) {
950 final int itemId = item.getItemId();
951 if (itemId == android.R.id.home) {
952 onBackPressed();
953 return true;
954 }
955 return super.onOptionsItemSelected(item);
956 }
957
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 /**
959 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
960 */
961 private void updateDialAndDeleteButtonStateEnabledAttr() {
962 final boolean notEmpty = mDigits.length() != 0;
963
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 mDelete.setEnabled(notEmpty);
965 }
Yorke Lee91311662014-10-24 14:50:45 -0700966
967 /**
968 * Remove the digit just before the current position. Used by various long pressed callbacks
969 * to remove the digit that was populated as a result of the short click.
970 */
971 private void removePreviousDigitIfPossible() {
972 final int currentPosition = mDigits.getSelectionStart();
973 if (currentPosition > 0) {
974 mDigits.setSelection(currentPosition);
975 mDigits.getText().delete(currentPosition - 1, currentPosition);
976 }
977 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800978
979 /**
980 * Update the text-to-speech annotations in the edit field.
981 */
982 private void updateTtsSpans() {
983 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
984 mDigits.getText().removeSpan(o);
985 }
986 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
987 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700988
989 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700990 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700991 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700992 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
993 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700994 mBackgroundGradient.setColors(colors);
995 updateTheme(colors.supportsDarkText());
996 }
997 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700998
999 /**
1000 * Where a carrier requires a warning that emergency calling is not available while on WFC,
1001 * add hint text above the dial pad which warns the user of this case.
1002 */
1003 private void maybeShowWfcEmergencyCallingWarning() {
1004 if (!mIsWfcEmergencyCallingWarningEnabled) {
1005 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
1006 return;
1007 }
1008
1009 // Use an async task rather than calling into Telephony on UI thread.
1010 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
1011 @Override
1012 protected Boolean doInBackground(Void... voids) {
1013 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
1014 boolean isWfcAvailable = tm.isWifiCallingAvailable();
1015 ServiceState ss = tm.getServiceState();
1016 boolean isCellAvailable =
1017 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
1018 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
1019 + " isCellAvailable=" + isCellAvailable
1020 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
1021 return isWfcAvailable && !isCellAvailable;
1022 }
1023
1024 @Override
1025 protected void onPostExecute(Boolean result) {
1026 if (result.booleanValue()) {
1027 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
1028 mDigits.setHint(R.string.dial_emergency_calling_not_available);
1029 } else {
1030 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
1031 mDigits.setHint("");
1032 }
1033 maybeChangeHintSize();
1034 }
1035 };
1036 showWfcWarningTask.execute((Void) null);
1037 }
1038
1039 /**
1040 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
1041 * edit view and set the font size to a smaller size appropriate for the emergency calling
1042 * warning.
1043 */
1044 private void maybeChangeHintSize() {
1045 if (TextUtils.isEmpty(mDigits.getHint())
1046 || !TextUtils.isEmpty(mDigits.getText().toString())) {
1047 // No hint or there are dialed digits, so use default size.
1048 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1049 // By default, the digits view auto-resizes to fit the text it contains, so
1050 // enable that now.
1051 mDigits.setResizeEnabled(true);
1052 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1053 } else {
1054 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1055 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1056 R.dimen.emergency_call_warning_size));
1057 // Since we're populating this with a static text string, disable auto-resize.
1058 mDigits.setResizeEnabled(false);
1059 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1060 }
1061 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001062
1063 private void setupEmergencyShortcutsView() {
1064 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1065 mDialpadView = findViewById(R.id.emergency_dialer);
1066
1067 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1068 dialpadButton.setOnClickListener(this);
1069
1070 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
1071 emergencyInfoButton.setOnClickListener(this);
1072
1073 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
1074 mEmergencyActionGroup.setVisibility(View.GONE);
1075
1076 // Setup dialpad title.
1077 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
1078 emergencyDialpadTitle.setVisibility(View.VISIBLE);
1079
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001080 mEmergencyShortcutButtonList = new ArrayList<>();
1081 setupEmergencyCallShortcutButton();
1082
CY Cheng9a69c182018-06-15 21:20:10 +08001083 updateLocationAndEccInfo(null, null);
1084
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001085 switchView(mEmergencyShortcutView, mDialpadView, false);
1086 }
1087
CY Cheng9a69c182018-06-15 21:20:10 +08001088 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001089 final View locationInfo = findViewById(R.id.location_info);
1090
CY Cheng9a69c182018-06-15 21:20:10 +08001091 String countryName = null;
1092 if (!TextUtils.isEmpty(countryIso)) {
1093 Locale locale = Locale.getDefault();
1094 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1095 .getDisplayCountry();
1096 }
1097 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001098 locationInfo.setVisibility(View.INVISIBLE);
1099 } else {
1100 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001101 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001102 locationInfo.setVisibility(View.VISIBLE);
1103 }
1104 }
1105
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001106 private void setupEmergencyCallShortcutButton() {
1107 final ViewGroup shortcutButtonContainer = findViewById(
1108 R.id.emergency_shortcut_buttons_container);
1109 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001110 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001111
CY Cheng9a69c182018-06-15 21:20:10 +08001112 mShortcutAdapter = new EccShortcutAdapter(this) {
1113 @Override
1114 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1115 CharSequence description, int iconRes) {
1116 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1117 .inflate(R.layout.emergency_shortcut_button, parent, false);
1118 button.setPhoneNumber(number);
1119 button.setPhoneDescription(description);
1120 button.setPhoneTypeIcon(iconRes);
1121 button.setOnConfirmClickListener(EmergencyDialer.this);
1122 return button;
1123 }
1124 };
1125 mShortcutDataSetObserver = new DataSetObserver() {
1126 @Override
1127 public void onChanged() {
1128 super.onChanged();
1129 updateLayout();
1130 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001131
CY Cheng9a69c182018-06-15 21:20:10 +08001132 @Override
1133 public void onInvalidated() {
1134 super.onInvalidated();
1135 updateLayout();
1136 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001137
CY Cheng9a69c182018-06-15 21:20:10 +08001138 private void updateLayout() {
1139 // clear previous added buttons
1140 shortcutButtonContainer.removeAllViews();
1141 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001142
CY Cheng9a69c182018-06-15 21:20:10 +08001143 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1144 EmergencyShortcutButton button = (EmergencyShortcutButton)
1145 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1146 mEmergencyShortcutButtonList.add(button);
1147 shortcutButtonContainer.addView(button);
1148 }
1149
1150 // update emergency numbers title for numerous buttons.
1151 if (mEmergencyShortcutButtonList.size() > 1) {
1152 emergencyNumberTitle.setText(getString(
1153 R.string.numerous_emergency_numbers_title));
1154 } else {
1155 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1156 }
1157 }
1158 };
1159 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1160 }
1161
1162 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1163 if (!isFinishing() && !isDestroyed()) {
1164 setLocationInfo(iso);
1165 if (mShortcutAdapter != null) {
1166 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1167 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001168 }
1169 }
1170
1171 /**
1172 * Called by the activity before a touch event is dispatched to the view hierarchy.
1173 */
1174 private void onPreTouchEvent(MotionEvent event) {
1175 mEmergencyActionGroup.onPreTouchEvent(event);
1176
1177 if (mEmergencyShortcutButtonList != null) {
1178 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1179 button.onPreTouchEvent(event);
1180 }
1181 }
1182 }
1183
1184 /**
1185 * Called by the activity after a touch event is dispatched to the view hierarchy.
1186 */
1187 private void onPostTouchEvent(MotionEvent event) {
1188 mEmergencyActionGroup.onPostTouchEvent(event);
1189
1190 if (mEmergencyShortcutButtonList != null) {
1191 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1192 button.onPostTouchEvent(event);
1193 }
1194 }
1195 }
1196
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001197 /**
1198 * Switch two view.
1199 *
1200 * @param displayView the view would be displayed.
1201 * @param hideView the view would be hidden.
1202 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1203 */
1204 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1205 if (displayView == null || hideView == null) {
1206 return;
1207 }
1208
1209 if (displayView.getVisibility() == View.VISIBLE) {
1210 return;
1211 }
1212
1213 if (hasAnimation) {
1214 crossfade(hideView, displayView);
1215 } else {
1216 hideView.setVisibility(View.GONE);
1217 displayView.setVisibility(View.VISIBLE);
1218 }
1219 }
1220
1221 /**
1222 * Fade out and fade in animation between two view transition.
1223 */
1224 private void crossfade(View fadeOutView, View fadeInView) {
1225 if (fadeOutView == null || fadeInView == null) {
1226 return;
1227 }
1228 final int shortAnimationDuration = getResources().getInteger(
1229 android.R.integer.config_shortAnimTime);
1230
1231 fadeInView.setAlpha(0f);
1232 fadeInView.setVisibility(View.VISIBLE);
1233
1234 fadeInView.animate()
1235 .alpha(1f)
1236 .setDuration(shortAnimationDuration)
1237 .setListener(null);
1238
1239 fadeOutView.animate()
1240 .alpha(0f)
1241 .setDuration(shortAnimationDuration)
1242 .setListener(new AnimatorListenerAdapter() {
1243 @Override
1244 public void onAnimationEnd(Animator animation) {
1245 fadeOutView.setVisibility(View.GONE);
1246 }
1247 });
1248 }
Shaotang Li8662a912018-07-04 16:53:01 +08001249
1250 @Override
1251 public void onSensorChanged(SensorEvent event) {
1252 float distance = event.values[0];
1253 mIsProximityNear = (distance < mProximitySensor.getMaximumRange());
1254 }
1255
1256 @Override
1257 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1258 // Not used.
1259 }
1260
1261 private boolean isShortcutNumber(String number) {
1262 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1263 return false;
1264 }
1265
1266 boolean isShortcut = false;
1267 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1268 if (button != null && number.equals(button.getPhoneNumber())) {
1269 isShortcut = true;
1270 break;
1271 }
1272 }
1273 return isShortcut;
1274 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275}