blob: f5c14b92ac59107f45d040adb5c1633a8c0f25af [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;
Michelef6a5ea22019-10-15 16:08:54 -070026import android.app.WallpaperColors;
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;
Michelef6a5ea22019-10-15 16:08:54 -070035import android.graphics.drawable.ColorDrawable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.media.AudioManager;
37import android.media.ToneGenerator;
38import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070039import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.provider.Settings;
Shaotang Li5d906c12018-07-19 17:32:42 +080043import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070044import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070045import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070046import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080048import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070049import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070050import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070053import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080054import android.text.Spannable;
55import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.text.TextUtils;
57import android.text.TextWatcher;
58import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080059import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070061import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070062import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070064import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070065import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080067import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080068import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080070import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080071import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072
Leo Hsu7bcfb8e2019-05-03 21:54:45 +080073import com.android.phone.EmergencyDialerMetricsLogger.DialedFrom;
74import com.android.phone.EmergencyDialerMetricsLogger.PhoneNumberType;
75import com.android.phone.EmergencyDialerMetricsLogger.UiModeErrorCode;
Yorke Lee23a70732014-08-14 17:12:01 -070076import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070077import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070078import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079
Chihhang Chuang92cfe512018-06-07 16:25:27 +080080import java.util.ArrayList;
81import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080082import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080083
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084/**
85 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
86 *
87 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
88 * activity from apps/Contacts) that:
Leo Hsu83ab2332018-12-18 15:20:45 +080089 * 1. Allows ONLY emergency calls to be dialed
90 * 2. Disallows voicemail functionality
91 * 3. Allows this activity to stay in front of the keyguard.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092 *
93 * TODO: Even though this is an ultra-simplified version of the normal
94 * dialer, there's still lots of code duplication between this class and
95 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
96 * moved into a shared base class that would live in the framework?
97 * Or could we figure out some way to move *this* class into apps/Contacts
98 * also?
99 */
100public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700101 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Michelef6a5ea22019-10-15 16:08:54 -0700102 DialpadKeyButton.OnPressedListener,
103 WallpaperManager.OnColorsChangedListener,
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800104 EmergencyShortcutButton.OnConfirmClickListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800105 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-07-04 16:53:01 +0800106
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 // Keys used with onSaveInstanceState().
108 private static final String LAST_NUMBER = "lastNumber";
109
110 // Intent action for this activity.
111 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
112
Shaotang Li8662a912018-07-04 16:53:01 +0800113 /**
114 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
115 * the emergency dialer.
116 */
117 public static final String EXTRA_ENTRY_TYPE =
118 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
119
Leo Hsue3512b62019-02-14 15:53:00 +0800120 // 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
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 // List of dialer button IDs.
Leo Hsu83ab2332018-12-18 15:20:45 +0800128 private static final int[] DIALER_KEYS = new int[]{
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129 R.id.one, R.id.two, R.id.three,
130 R.id.four, R.id.five, R.id.six,
131 R.id.seven, R.id.eight, R.id.nine,
Leo Hsu83ab2332018-12-18 15:20:45 +0800132 R.id.star, R.id.zero, R.id.pound};
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133
134 // Debug constants.
135 private static final boolean DBG = false;
136 private static final String LOG_TAG = "EmergencyDialer";
137
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138 /** The length of DTMF tones in milliseconds */
139 private static final int TONE_LENGTH_MS = 150;
140
141 /** The DTMF tone volume relative to other sounds in the stream */
142 private static final int TONE_RELATIVE_VOLUME = 80;
143
144 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
145 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
146
147 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
148
Lucas Dupineb9c5702017-05-10 16:57:09 -0700149 /** 90% opacity, different from other gradients **/
150 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
151
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800152 /** 85% opacity for black background **/
153 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
154
CY Cheng9a69c182018-06-15 21:20:10 +0800155 /** Size limit of emergency shortcut buttons container. **/
156 private static final int SHORTCUT_SIZE_LIMIT = 3;
157
Tyler Gunnc5428972018-03-28 14:15:34 -0700158 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 private View mDialButton;
160 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800161 private View mEmergencyShortcutView;
162 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800164 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800165 private EccShortcutAdapter mShortcutAdapter;
166 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 private ToneGenerator mToneGenerator;
169 private Object mToneGeneratorLock = new Object();
170
171 // determines if we want to playback local DTMF tones.
172 private boolean mDTMFToneEnabled;
173
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700174 private EmergencyActionGroup mEmergencyActionGroup;
175
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800176 private EmergencyInfoGroup mEmergencyInfoGroup;
177
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 // close activity when screen turns off
179 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
180 @Override
181 public void onReceive(Context context, Intent intent) {
182 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700183 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 }
185 }
186 };
187
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800188 /**
189 * Customize accessibility methods in View.
190 */
191 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
192
193 /**
194 * Stop AccessiblityService from reading the title of a hidden View.
195 *
196 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
197 * in the animation end. The view with an accessibility pane title would call the
198 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
199 * accessibility service to read the pane title of fade out view instead of pane title of
200 * fade in view. So it need to filter out the event called by vanished pane.
201 */
202 @Override
203 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
204 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
205 && host.getVisibility() == View.GONE) {
206 return;
207 }
208 super.onPopulateAccessibilityEvent(host, event);
209 }
210 };
211
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
213
Lucas Dupineb9c5702017-05-10 16:57:09 -0700214 // Background gradient
Michelef6a5ea22019-10-15 16:08:54 -0700215 private ColorDrawable mBackgroundDrawable;
Lucas Dupineb9c5702017-05-10 16:57:09 -0700216 private boolean mSupportsDarkText;
217
Tyler Gunnc5428972018-03-28 14:15:34 -0700218 private boolean mIsWfcEmergencyCallingWarningEnabled;
219 private float mDefaultDigitsTextSize;
220
Leo Hsue3512b62019-02-14 15:53:00 +0800221 private int mEntryType;
Leo Hsubc7553a2019-03-05 15:47:23 +0800222 private ShortcutViewUtils.Config mShortcutViewConfig;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800223
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800224 private EmergencyDialerMetricsLogger mMetricsLogger;
Shaotang Li8662a912018-07-04 16:53:01 +0800225
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 @Override
227 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
228 // Do nothing
229 }
230
231 @Override
232 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700233 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700234 }
235
236 @Override
237 public void afterTextChanged(Editable input) {
238 // Check for special sequences, in particular the "**04" or "**05"
239 // sequences that allow you to enter PIN or PUK-related codes.
240 //
241 // But note we *don't* allow most other special sequences here,
242 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
243 // since those shouldn't be available if the device is locked.
244 //
245 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
246 // here, not the regular handleChars() method.
247 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
248 // A special sequence was entered, clear the digits
249 mDigits.getText().clear();
250 }
251
252 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800253 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 }
255
256 @Override
257 protected void onCreate(Bundle icicle) {
258 super.onCreate(icicle);
259
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800260 mMetricsLogger = new EmergencyDialerMetricsLogger(this);
261
Leo Hsue3512b62019-02-14 15:53:00 +0800262 mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
263 Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
264
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800266 setShowWhenLocked(true);
267 // Allow turning screen on
268 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269
Chuck Liaocdeebb42018-10-30 12:07:18 +0800270 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
271 PersistableBundle carrierConfig =
272 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
273
Leo Hsubc7553a2019-03-05 15:47:23 +0800274 mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
CY Cheng9a69c182018-06-15 21:20:10 +0800275 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsubc7553a2019-03-05 15:47:23 +0800276 + mShortcutViewConfig.isEnabled());
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800277
Leo Hsubc7553a2019-03-05 15:47:23 +0800278 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800279 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800280 updateTheme(false);
281 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700282 WallpaperColors wallpaperColors =
283 getWallpaperManager().getWallpaperColors(WallpaperManager.FLAG_LOCK);
284 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800285 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700286
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 setContentView(R.layout.emergency_dialer);
288
Tyler Gunnc5428972018-03-28 14:15:34 -0700289 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 mDigits.setKeyListener(DialerKeyListener.getInstance());
291 mDigits.setOnClickListener(this);
292 mDigits.setOnKeyListener(this);
293 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700294 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700295 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 maybeAddNumberFormatting();
297
Michelef6a5ea22019-10-15 16:08:54 -0700298 mBackgroundDrawable = new ColorDrawable();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700299 Point displaySize = new Point();
300 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
301 .getDefaultDisplay().getSize(displaySize);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700302 mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled()
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800303 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700304 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700305
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 // Check for the presence of the keypad
307 View view = findViewById(R.id.one);
308 if (view != null) {
309 setupKeypad();
310 }
311
312 mDelete = findViewById(R.id.deleteButton);
313 mDelete.setOnClickListener(this);
314 mDelete.setOnLongClickListener(this);
315
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700316 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317
318 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700319 // Read carrier config through the public API because PhoneGlobals is not available when we
320 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700321 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 mDialButton.setOnClickListener(this);
323 } else {
324 mDialButton.setVisibility(View.GONE);
325 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700326 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
327 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
328 maybeShowWfcEmergencyCallingWarning();
329
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700330 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331
332 if (icicle != null) {
333 super.onRestoreInstanceState(icicle);
334 }
335
336 // Extract phone number from intent
337 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700338 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
340 if (number != null) {
341 mDigits.setText(number);
342 }
343 }
344
345 // if the mToneGenerator creation fails, just continue without it. It is
346 // a local audio signal, and is not as important as the dtmf tone itself.
347 synchronized (mToneGeneratorLock) {
348 if (mToneGenerator == null) {
349 try {
350 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
351 } catch (RuntimeException e) {
352 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
353 mToneGenerator = null;
354 }
355 }
356 }
357
358 final IntentFilter intentFilter = new IntentFilter();
359 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
360 registerReceiver(mBroadcastReceiver, intentFilter);
361
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700362 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800363
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800364 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
365
Leo Hsubc7553a2019-03-05 15:47:23 +0800366 if (mShortcutViewConfig.isEnabled()) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800367 setupEmergencyShortcutsView();
368 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700369 }
370
371 @Override
372 protected void onDestroy() {
373 super.onDestroy();
374 synchronized (mToneGeneratorLock) {
375 if (mToneGenerator != null) {
376 mToneGenerator.release();
377 mToneGenerator = null;
378 }
379 }
380 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800381 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
382 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
383 mShortcutDataSetObserver = null;
384 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700385 }
386
387 @Override
388 protected void onRestoreInstanceState(Bundle icicle) {
389 mLastNumber = icicle.getString(LAST_NUMBER);
390 }
391
392 @Override
393 protected void onSaveInstanceState(Bundle outState) {
394 super.onSaveInstanceState(outState);
395 outState.putString(LAST_NUMBER, mLastNumber);
396 }
397
398 /**
399 * Explicitly turn off number formatting, since it gets in the way of the emergency
400 * number detector
401 */
402 protected void maybeAddNumberFormatting() {
403 // Do nothing.
404 }
405
406 @Override
407 protected void onPostCreate(Bundle savedInstanceState) {
408 super.onPostCreate(savedInstanceState);
409
410 // This can't be done in onCreate(), since the auto-restoring of the digits
411 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
412 // is called. This method will be called every time the activity is created, and
413 // will always happen after onRestoreSavedInstanceState().
414 mDigits.addTextChangedListener(this);
415 }
416
417 private void setupKeypad() {
418 // Setup the listeners for the buttons
419 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700420 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
421 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700422 }
423
424 View view = findViewById(R.id.zero);
425 view.setOnLongClickListener(this);
426 }
427
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800428 @Override
429 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800430 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
431 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
Leo Hsubc7553a2019-03-05 15:47:23 +0800432 if (mShortcutViewConfig.isEnabled() && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800433 && mDialpadView.getVisibility() == View.VISIBLE) {
434 switchView(mEmergencyShortcutView, mDialpadView, true);
435 return;
436 }
437 super.onBackPressed();
438 }
439
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700440 /**
441 * handle key events
442 */
443 @Override
444 public boolean onKeyDown(int keyCode, KeyEvent event) {
445 switch (keyCode) {
446 // Happen when there's a "Call" hard button.
447 case KeyEvent.KEYCODE_CALL: {
448 if (TextUtils.isEmpty(mDigits.getText().toString())) {
449 // if we are adding a call from the InCallScreen and the phone
450 // number entered is empty, we just close the dialer to expose
451 // the InCallScreen under it.
452 finish();
453 } else {
454 // otherwise, we place the call.
455 placeCall();
456 }
457 return true;
458 }
459 }
460 return super.onKeyDown(keyCode, event);
461 }
462
463 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700464 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700465 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
466 mDigits.onKeyDown(keyCode, event);
467 }
468
469 @Override
470 public boolean onKey(View view, int keyCode, KeyEvent event) {
471 switch (view.getId()) {
472 case R.id.digits:
473 // Happen when "Done" button of the IME is pressed. This can happen when this
474 // Activity is forced into landscape mode due to a desk dock.
475 if (keyCode == KeyEvent.KEYCODE_ENTER
476 && event.getAction() == KeyEvent.ACTION_UP) {
477 placeCall();
478 return true;
479 }
480 break;
481 }
482 return false;
483 }
484
485 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700486 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800487 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700488 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800489 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700490 return handled;
491 }
492
493 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800494 public void onConfirmClick(EmergencyShortcutButton button) {
495 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800496 String phoneNumber = button.getPhoneNumber();
497
498 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800499 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800500
501 // Write metrics when user has intention to make a call from a shortcut button.
502 mMetricsLogger.logPlaceCall(DialedFrom.SHORTCUT, PhoneNumberType.HAS_SHORTCUT,
503 mShortcutViewConfig.getPhoneInfo());
504
Leo Hsu43d670a2018-12-04 15:40:36 +0800505 placeCall(phoneNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800506 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800507 } else {
508 Log.d(LOG_TAG, "emergency number is empty");
509 }
510 }
511
512 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800513 public void onConfirmClick(EmergencyInfoGroup button) {
514 if (button == null) return;
515
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800516 Intent intent = (Intent) button.getTag(R.id.tag_intent);
517 if (intent != null) {
518 startActivity(intent);
519 }
520 }
521
522 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523 public void onClick(View view) {
524 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700525 case R.id.deleteButton: {
526 keyPressed(KeyEvent.KEYCODE_DEL);
527 return;
528 }
529 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700530 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700531 placeCall();
532 return;
533 }
534 case R.id.digits: {
535 if (mDigits.length() != 0) {
536 mDigits.setCursorVisible(true);
537 }
538 return;
539 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800540 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800541 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800542 switchView(mDialpadView, mEmergencyShortcutView, true);
543 return;
544 }
Yorke Lee23a70732014-08-14 17:12:01 -0700545 }
546 }
547
548 @Override
549 public void onPressed(View view, boolean pressed) {
550 if (!pressed) {
551 return;
552 }
553 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700554 case R.id.one: {
555 playTone(ToneGenerator.TONE_DTMF_1);
556 keyPressed(KeyEvent.KEYCODE_1);
557 return;
558 }
559 case R.id.two: {
560 playTone(ToneGenerator.TONE_DTMF_2);
561 keyPressed(KeyEvent.KEYCODE_2);
562 return;
563 }
564 case R.id.three: {
565 playTone(ToneGenerator.TONE_DTMF_3);
566 keyPressed(KeyEvent.KEYCODE_3);
567 return;
568 }
569 case R.id.four: {
570 playTone(ToneGenerator.TONE_DTMF_4);
571 keyPressed(KeyEvent.KEYCODE_4);
572 return;
573 }
574 case R.id.five: {
575 playTone(ToneGenerator.TONE_DTMF_5);
576 keyPressed(KeyEvent.KEYCODE_5);
577 return;
578 }
579 case R.id.six: {
580 playTone(ToneGenerator.TONE_DTMF_6);
581 keyPressed(KeyEvent.KEYCODE_6);
582 return;
583 }
584 case R.id.seven: {
585 playTone(ToneGenerator.TONE_DTMF_7);
586 keyPressed(KeyEvent.KEYCODE_7);
587 return;
588 }
589 case R.id.eight: {
590 playTone(ToneGenerator.TONE_DTMF_8);
591 keyPressed(KeyEvent.KEYCODE_8);
592 return;
593 }
594 case R.id.nine: {
595 playTone(ToneGenerator.TONE_DTMF_9);
596 keyPressed(KeyEvent.KEYCODE_9);
597 return;
598 }
599 case R.id.zero: {
600 playTone(ToneGenerator.TONE_DTMF_0);
601 keyPressed(KeyEvent.KEYCODE_0);
602 return;
603 }
604 case R.id.pound: {
605 playTone(ToneGenerator.TONE_DTMF_P);
606 keyPressed(KeyEvent.KEYCODE_POUND);
607 return;
608 }
609 case R.id.star: {
610 playTone(ToneGenerator.TONE_DTMF_S);
611 keyPressed(KeyEvent.KEYCODE_STAR);
612 return;
613 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 }
615 }
616
617 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 * called for long touch events
619 */
620 @Override
621 public boolean onLongClick(View view) {
622 int id = view.getId();
623 switch (id) {
624 case R.id.deleteButton: {
625 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 return true;
627 }
628 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700629 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 keyPressed(KeyEvent.KEYCODE_PLUS);
631 return true;
632 }
633 }
634 return false;
635 }
636
637 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700638 protected void onStart() {
639 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800640
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800641 mMetricsLogger.logLaunchEmergencyDialer(mEntryType,
642 mShortcutViewConfig.isEnabled() ? UiModeErrorCode.SUCCESS
643 : UiModeErrorCode.UNSPECIFIED_ERROR);
Shaotang Li8662a912018-07-04 16:53:01 +0800644
Leo Hsubc7553a2019-03-05 15:47:23 +0800645 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800646 // Shortcut view doesn't support dark text theme.
Michelef6a5ea22019-10-15 16:08:54 -0700647 mBackgroundDrawable.setColor(Color.BLACK);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800648 updateTheme(false);
649 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700650 WallpaperManager wallpaperManager = getWallpaperManager();
651 if (wallpaperManager.isWallpaperSupported()) {
652 wallpaperManager.addOnColorsChangedListener(this, null);
653 }
654
655 WallpaperColors wallpaperColors =
656 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK);
657 mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors));
658 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800659 }
CY Cheng9a69c182018-06-15 21:20:10 +0800660
Leo Hsubc7553a2019-03-05 15:47:23 +0800661 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800662 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800663 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700664 }
665
666 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 protected void onResume() {
668 super.onResume();
669
670 // retrieve the DTMF tone play back setting.
671 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
672 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
673
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700674 // if the mToneGenerator creation fails, just continue without it. It is
675 // a local audio signal, and is not as important as the dtmf tone itself.
676 synchronized (mToneGeneratorLock) {
677 if (mToneGenerator == null) {
678 try {
679 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
680 TONE_RELATIVE_VOLUME);
681 } catch (RuntimeException e) {
682 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
683 mToneGenerator = null;
684 }
685 }
686 }
687
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 updateDialAndDeleteButtonStateEnabledAttr();
689 }
690
691 @Override
692 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700693 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 }
695
Lucas Dupineb9c5702017-05-10 16:57:09 -0700696 @Override
697 protected void onStop() {
698 super.onStop();
Michelef6a5ea22019-10-15 16:08:54 -0700699
700 WallpaperManager wallpaperManager = getWallpaperManager();
701 if (wallpaperManager.isWallpaperSupported()) {
702 wallpaperManager.removeOnColorsChangedListener(this);
703 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700704 }
705
706 /**
707 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800708 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700709 * @param supportsDarkText true if gradient supports dark text
710 */
711 private void updateTheme(boolean supportsDarkText) {
712 if (mSupportsDarkText == supportsDarkText) {
713 return;
714 }
715 mSupportsDarkText = supportsDarkText;
716
717 // We can't change themes after inflation, in this case we'll have to recreate
718 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700719 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700720 recreate();
721 return;
722 }
723
724 int vis = getWindow().getDecorView().getSystemUiVisibility();
725 if (supportsDarkText) {
726 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
727 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
728 setTheme(R.style.EmergencyDialerThemeDark);
729 } else {
730 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
731 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
732 setTheme(R.style.EmergencyDialerTheme);
733 }
734 getWindow().getDecorView().setSystemUiVisibility(vis);
735 }
736
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 /**
738 * place the call, but check to make sure it is a viable number.
739 */
740 private void placeCall() {
741 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900742
743 // Convert into emergency number according to emergency conversion map.
744 // If conversion map is not defined (this is default), this method does
745 // nothing and just returns input number.
746 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
747
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800748 @DialedFrom final int dialedFrom =
749 mShortcutViewConfig.isEnabled() ? DialedFrom.FASTER_LAUNCHER_DIALPAD
750 : DialedFrom.TRADITIONAL_DIALPAD;
751 @PhoneNumberType final int phoneNumberType;
752
Leo Hsubc7553a2019-03-05 15:47:23 +0800753 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800754 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800755 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800756 isEmergencyNumber = true;
757 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800758 phoneNumberType = PhoneNumberType.HAS_SHORTCUT;
759 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
760 // If a number from SIM/network/... is categoried as police/ambulance/fire,
761 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
762 // shortcut button because a number provided by database has higher priority.
763 isEmergencyNumber = true;
764 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
765 phoneNumberType = PhoneNumberType.NO_SHORTCUT;
Leo Hsubc7553a2019-03-05 15:47:23 +0800766 } else {
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800767 isEmergencyNumber = getSystemService(TelephonyManager.class)
768 .isEmergencyNumber(mLastNumber);
769 phoneNumberType = PhoneNumberType.NO_SHORTCUT;
Leo Hsu43d670a2018-12-04 15:40:36 +0800770 }
771
772 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
774
775 // place the call if it is a valid number
776 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
777 // There is no number entered.
778 playTone(ToneGenerator.TONE_PROP_NACK);
779 return;
780 }
Shaotang Li8662a912018-07-04 16:53:01 +0800781
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800782 // Write metrics when user has intention to make a call from dialpad
783 mMetricsLogger.logPlaceCall(dialedFrom, phoneNumberType, phoneToMakeCall);
Shaotang Li8662a912018-07-04 16:53:01 +0800784
Leo Hsu43d670a2018-12-04 15:40:36 +0800785 placeCall(mLastNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD,
786 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 } else {
788 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
789
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800790 // Write metrics when user has intention to make a call of a non-emergency number, even
791 // this number is rejected.
792 mMetricsLogger.logPlaceCall(dialedFrom, PhoneNumberType.NOT_EMERGENCY_NUMBER,
793 phoneToMakeCall);
Shaotang Li8662a912018-07-04 16:53:01 +0800794
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
796 }
Yorke Lee9b341512014-10-17 11:36:41 -0700797 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700798 }
799
Leo Hsu43d670a2018-12-04 15:40:36 +0800800 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800801 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
802 + ", entry = " + entryTypeToString(mEntryType));
803
Leo Hsu43d670a2018-12-04 15:40:36 +0800804 Bundle extras = new Bundle();
805 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800806 /**
807 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
808 * when the dialed number is ambiguous and identified as both emergency number and any
809 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
810 * number in a country and a non-emergency number of a carrier's customer service hotline.
811 */
812 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800813
814 if (phone != null && phone.getPhoneAccountHandle() != null) {
815 // Requests to dial through the specified phone.
816 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
817 phone.getPhoneAccountHandle());
818 }
819
820 TelecomManager tm = this.getSystemService(TelecomManager.class);
821 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
822 }
823
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 /**
825 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
826 *
827 * The tone is played locally, using the audio stream for phone calls.
828 * Tones are played only if the "Audible touch tones" user preference
829 * is checked, and are NOT played if the device is in silent mode.
830 *
831 * @param tone a tone code from {@link ToneGenerator}
832 */
833 void playTone(int tone) {
834 // if local tone playback is disabled, just return.
835 if (!mDTMFToneEnabled) {
836 return;
837 }
838
839 // Also do nothing if the phone is in silent mode.
840 // We need to re-check the ringer mode for *every* playTone()
841 // call, rather than keeping a local flag that's updated in
842 // onResume(), since it's possible to toggle silent mode without
843 // leaving the current activity (via the ENDCALL-longpress menu.)
844 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
845 int ringerMode = audioManager.getRingerMode();
846 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800847 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848 return;
849 }
850
851 synchronized (mToneGeneratorLock) {
852 if (mToneGenerator == null) {
853 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
854 return;
855 }
856
857 // Start the new tone (will stop any playing tone)
858 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
859 }
860 }
861
862 private CharSequence createErrorMessage(String number) {
863 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800864 String errorString = getString(R.string.dial_emergency_error, number);
865 int startingPosition = errorString.indexOf(number);
866 int endingPosition = startingPosition + number.length();
867 Spannable result = new SpannableString(errorString);
868 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
869 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700870 } else {
871 return getText(R.string.dial_emergency_empty_error).toString();
872 }
873 }
874
875 @Override
876 protected Dialog onCreateDialog(int id) {
877 AlertDialog dialog = null;
878 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
879 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700880 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
882 .setMessage(createErrorMessage(mLastNumber))
883 .setPositiveButton(R.string.ok, null)
884 .setCancelable(true).create();
885
886 // blur stuff behind the dialog
887 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700888 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700889 }
890 return dialog;
891 }
892
893 @Override
894 protected void onPrepareDialog(int id, Dialog dialog) {
895 super.onPrepareDialog(id, dialog);
896 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
897 AlertDialog alert = (AlertDialog) dialog;
898 alert.setMessage(createErrorMessage(mLastNumber));
899 }
900 }
901
Andrew Leed5631e82014-10-08 16:03:58 -0700902 @Override
903 public boolean onOptionsItemSelected(MenuItem item) {
904 final int itemId = item.getItemId();
905 if (itemId == android.R.id.home) {
906 onBackPressed();
907 return true;
908 }
909 return super.onOptionsItemSelected(item);
910 }
911
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700912 /**
913 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
914 */
915 private void updateDialAndDeleteButtonStateEnabledAttr() {
916 final boolean notEmpty = mDigits.length() != 0;
917
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 mDelete.setEnabled(notEmpty);
919 }
Yorke Lee91311662014-10-24 14:50:45 -0700920
921 /**
922 * Remove the digit just before the current position. Used by various long pressed callbacks
923 * to remove the digit that was populated as a result of the short click.
924 */
925 private void removePreviousDigitIfPossible() {
926 final int currentPosition = mDigits.getSelectionStart();
927 if (currentPosition > 0) {
928 mDigits.setSelection(currentPosition);
929 mDigits.getText().delete(currentPosition - 1, currentPosition);
930 }
931 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800932
933 /**
934 * Update the text-to-speech annotations in the edit field.
935 */
936 private void updateTtsSpans() {
937 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
938 mDigits.getText().removeSpan(o);
939 }
940 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
941 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700942
943 @Override
Michelef6a5ea22019-10-15 16:08:54 -0700944 public void onColorsChanged(WallpaperColors colors, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700945 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Michelef6a5ea22019-10-15 16:08:54 -0700946 mBackgroundDrawable.setColor(getPrimaryColor(colors));
947 updateTheme(supportsDarkText(colors));
Lucas Dupineb9c5702017-05-10 16:57:09 -0700948 }
949 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700950
951 /**
952 * Where a carrier requires a warning that emergency calling is not available while on WFC,
953 * add hint text above the dial pad which warns the user of this case.
954 */
955 private void maybeShowWfcEmergencyCallingWarning() {
956 if (!mIsWfcEmergencyCallingWarningEnabled) {
957 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
958 return;
959 }
960
961 // Use an async task rather than calling into Telephony on UI thread.
962 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
963 @Override
964 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800965 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700966 boolean isWfcAvailable = tm.isWifiCallingAvailable();
967 ServiceState ss = tm.getServiceState();
968 boolean isCellAvailable =
969 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
970 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800971 + " isCellAvailable=" + isCellAvailable
972 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700973 return isWfcAvailable && !isCellAvailable;
974 }
975
976 @Override
977 protected void onPostExecute(Boolean result) {
978 if (result.booleanValue()) {
979 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
980 mDigits.setHint(R.string.dial_emergency_calling_not_available);
981 } else {
982 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
983 mDigits.setHint("");
984 }
985 maybeChangeHintSize();
986 }
987 };
988 showWfcWarningTask.execute((Void) null);
989 }
990
991 /**
992 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
993 * edit view and set the font size to a smaller size appropriate for the emergency calling
994 * warning.
995 */
996 private void maybeChangeHintSize() {
997 if (TextUtils.isEmpty(mDigits.getHint())
998 || !TextUtils.isEmpty(mDigits.getText().toString())) {
999 // No hint or there are dialed digits, so use default size.
1000 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
1001 // By default, the digits view auto-resizes to fit the text it contains, so
1002 // enable that now.
1003 mDigits.setResizeEnabled(true);
1004 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
1005 } else {
1006 // Hint present and no dialed digits, set custom font size appropriate for the warning.
1007 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
1008 R.dimen.emergency_call_warning_size));
1009 // Since we're populating this with a static text string, disable auto-resize.
1010 mDigits.setResizeEnabled(false);
1011 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
1012 }
1013 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001014
1015 private void setupEmergencyShortcutsView() {
1016 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
1017 mDialpadView = findViewById(R.id.emergency_dialer);
1018
Chihhang Chuangcaba0da2018-08-02 22:25:06 +08001019 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
1020 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
1021
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001022 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
1023 dialpadButton.setOnClickListener(this);
1024
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001025 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001026
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001027 mEmergencyShortcutButtonList = new ArrayList<>();
1028 setupEmergencyCallShortcutButton();
1029
Leo Hsu43d670a2018-12-04 15:40:36 +08001030 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001031
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001032 switchView(mEmergencyShortcutView, mDialpadView, false);
1033 }
1034
Leo Hsu43d670a2018-12-04 15:40:36 +08001035 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001036 final View locationInfo = findViewById(R.id.location_info);
1037
Leo Hsubc7553a2019-03-05 15:47:23 +08001038 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +08001039 String countryName = null;
1040 if (!TextUtils.isEmpty(countryIso)) {
1041 Locale locale = Locale.getDefault();
1042 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1043 .getDisplayCountry();
1044 }
1045 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001046 locationInfo.setVisibility(View.INVISIBLE);
1047 } else {
1048 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001049 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001050 locationInfo.setVisibility(View.VISIBLE);
1051 }
1052 }
1053
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001054 private void setupEmergencyCallShortcutButton() {
1055 final ViewGroup shortcutButtonContainer = findViewById(
1056 R.id.emergency_shortcut_buttons_container);
1057 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001058 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001059
CY Cheng9a69c182018-06-15 21:20:10 +08001060 mShortcutAdapter = new EccShortcutAdapter(this) {
1061 @Override
1062 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1063 CharSequence description, int iconRes) {
1064 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1065 .inflate(R.layout.emergency_shortcut_button, parent, false);
1066 button.setPhoneNumber(number);
1067 button.setPhoneDescription(description);
1068 button.setPhoneTypeIcon(iconRes);
1069 button.setOnConfirmClickListener(EmergencyDialer.this);
1070 return button;
1071 }
1072 };
1073 mShortcutDataSetObserver = new DataSetObserver() {
1074 @Override
1075 public void onChanged() {
1076 super.onChanged();
1077 updateLayout();
1078 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001079
CY Cheng9a69c182018-06-15 21:20:10 +08001080 @Override
1081 public void onInvalidated() {
1082 super.onInvalidated();
1083 updateLayout();
1084 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001085
CY Cheng9a69c182018-06-15 21:20:10 +08001086 private void updateLayout() {
1087 // clear previous added buttons
1088 shortcutButtonContainer.removeAllViews();
1089 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001090
CY Cheng9a69c182018-06-15 21:20:10 +08001091 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1092 EmergencyShortcutButton button = (EmergencyShortcutButton)
1093 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1094 mEmergencyShortcutButtonList.add(button);
1095 shortcutButtonContainer.addView(button);
1096 }
1097
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001098 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001099 if (mEmergencyShortcutButtonList.size() > 1) {
1100 emergencyNumberTitle.setText(getString(
1101 R.string.numerous_emergency_numbers_title));
1102 } else {
1103 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1104 }
1105 }
1106 };
1107 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1108 }
1109
Leo Hsu43d670a2018-12-04 15:40:36 +08001110 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001111 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001112 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001113 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001114 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001115 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001116 }
1117 }
1118
1119 /**
1120 * Called by the activity before a touch event is dispatched to the view hierarchy.
1121 */
1122 private void onPreTouchEvent(MotionEvent event) {
1123 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001124 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001125
1126 if (mEmergencyShortcutButtonList != null) {
1127 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1128 button.onPreTouchEvent(event);
1129 }
1130 }
1131 }
1132
1133 /**
1134 * Called by the activity after a touch event is dispatched to the view hierarchy.
1135 */
1136 private void onPostTouchEvent(MotionEvent event) {
1137 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001138 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001139
1140 if (mEmergencyShortcutButtonList != null) {
1141 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1142 button.onPostTouchEvent(event);
1143 }
1144 }
1145 }
1146
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001147 /**
1148 * Switch two view.
1149 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001150 * @param displayView the view would be displayed.
1151 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001152 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1153 */
1154 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1155 if (displayView == null || hideView == null) {
1156 return;
1157 }
1158
1159 if (displayView.getVisibility() == View.VISIBLE) {
1160 return;
1161 }
1162
1163 if (hasAnimation) {
1164 crossfade(hideView, displayView);
1165 } else {
1166 hideView.setVisibility(View.GONE);
1167 displayView.setVisibility(View.VISIBLE);
1168 }
1169 }
1170
1171 /**
1172 * Fade out and fade in animation between two view transition.
1173 */
1174 private void crossfade(View fadeOutView, View fadeInView) {
1175 if (fadeOutView == null || fadeInView == null) {
1176 return;
1177 }
1178 final int shortAnimationDuration = getResources().getInteger(
1179 android.R.integer.config_shortAnimTime);
1180
1181 fadeInView.setAlpha(0f);
1182 fadeInView.setVisibility(View.VISIBLE);
1183
1184 fadeInView.animate()
1185 .alpha(1f)
1186 .setDuration(shortAnimationDuration)
1187 .setListener(null);
1188
1189 fadeOutView.animate()
1190 .alpha(0f)
1191 .setDuration(shortAnimationDuration)
1192 .setListener(new AnimatorListenerAdapter() {
1193 @Override
1194 public void onAnimationEnd(Animator animation) {
1195 fadeOutView.setVisibility(View.GONE);
1196 }
1197 });
1198 }
Shaotang Li8662a912018-07-04 16:53:01 +08001199
Shaotang Li8662a912018-07-04 16:53:01 +08001200 private boolean isShortcutNumber(String number) {
1201 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1202 return false;
1203 }
1204
1205 boolean isShortcut = false;
1206 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1207 if (button != null && number.equals(button.getPhoneNumber())) {
1208 isShortcut = true;
1209 break;
1210 }
1211 }
1212 return isShortcut;
1213 }
Leo Hsue3512b62019-02-14 15:53:00 +08001214
1215 private String entryTypeToString(int entryType) {
1216 switch (entryType) {
1217 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1218 return "LockScreen";
1219 case ENTRY_TYPE_POWER_MENU:
1220 return "PowerMenu";
1221 default:
1222 return "Unknown-" + entryType;
1223 }
1224 }
1225
1226 private String callSourceToString(int callSource) {
1227 switch (callSource) {
1228 case ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD:
1229 return "DialPad";
1230 case ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT:
1231 return "Shortcut";
1232 default:
1233 return "Unknown-" + callSource;
1234 }
1235 }
Michelef6a5ea22019-10-15 16:08:54 -07001236
1237 private WallpaperManager getWallpaperManager() {
1238 return getSystemService(WallpaperManager.class);
1239 }
1240
1241 private static boolean supportsDarkText(WallpaperColors colors) {
1242 if (colors != null) {
1243 return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
1244 }
1245 // It's possible that wallpaper colors are null (e.g. when colors are being
1246 // processed or a live wallpaper is used). In this case, fallback to same
1247 // behavior as when shortcut view is enabled.
1248 return false;
1249 }
1250
1251 private static int getPrimaryColor(WallpaperColors colors) {
1252 if (colors != null) {
1253 return colors.getPrimaryColor().toArgb();
1254 }
1255 // It's possible that wallpaper colors are null (e.g. when colors are being
1256 // processed or a live wallpaper is used). In this case, fallback to same
1257 // behavior as when shortcut view is enabled.
1258 return Color.BLACK;
1259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260}