blob: 5fe87084fc8d3add1221e47a69974fc088273438 [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;
Grace Jia835d8e62020-04-01 15:47:39 -070023import android.annotation.ColorInt;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.app.Activity;
25import android.app.AlertDialog;
26import android.app.Dialog;
Michelef6a5ea22019-10-15 16:08:54 -070027import android.app.WallpaperColors;
Lucas Dupineb9c5702017-05-10 16:57:09 -070028import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.content.BroadcastReceiver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080033import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080034import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070035import android.graphics.Point;
Michelef6a5ea22019-10-15 16:08:54 -070036import android.graphics.drawable.ColorDrawable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.media.AudioManager;
38import android.media.ToneGenerator;
39import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070040import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070042import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.provider.Settings;
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;
Tyler Gunnc5428972018-03-28 14:15:34 -070048import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070049import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070050import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070052import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080053import android.text.Spannable;
54import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.text.TextUtils;
56import android.text.TextWatcher;
57import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080058import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070060import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070061import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070063import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070064import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080066import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080067import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080069import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080070import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071
Yorke Lee23a70732014-08-14 17:12:01 -070072import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070073import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070074import com.android.phone.common.widget.ResizingTextEditText;
Grace Jia835d8e62020-04-01 15:47:39 -070075import com.android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076
Chihhang Chuang92cfe512018-06-07 16:25:27 +080077import java.util.ArrayList;
78import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080079import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080080
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081/**
82 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
83 *
84 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
85 * activity from apps/Contacts) that:
Leo Hsu83ab2332018-12-18 15:20:45 +080086 * 1. Allows ONLY emergency calls to be dialed
87 * 2. Disallows voicemail functionality
88 * 3. Allows this activity to stay in front of the keyguard.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089 *
90 * TODO: Even though this is an ultra-simplified version of the normal
91 * dialer, there's still lots of code duplication between this class and
92 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
93 * moved into a shared base class that would live in the framework?
94 * Or could we figure out some way to move *this* class into apps/Contacts
95 * also?
96 */
97public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -070098 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Michelef6a5ea22019-10-15 16:08:54 -070099 DialpadKeyButton.OnPressedListener,
100 WallpaperManager.OnColorsChangedListener,
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800101 EmergencyShortcutButton.OnConfirmClickListener,
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800102 EmergencyInfoGroup.OnConfirmClickListener {
Shaotang Li8662a912018-07-04 16:53:01 +0800103
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104 // Keys used with onSaveInstanceState().
105 private static final String LAST_NUMBER = "lastNumber";
106
107 // Intent action for this activity.
108 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
109
Shaotang Li8662a912018-07-04 16:53:01 +0800110 /**
111 * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts
112 * the emergency dialer.
113 */
114 public static final String EXTRA_ENTRY_TYPE =
115 "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE";
116
Leo Hsue3512b62019-02-14 15:53:00 +0800117 // Constants indicating the entry type that user opened emergency dialer.
118 // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being
119 // in sync with those in com.android.systemui.util.EmergencyDialerConstants.
120 public static final int ENTRY_TYPE_UNKNOWN = 0;
121 public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1;
122 public static final int ENTRY_TYPE_POWER_MENU = 2;
123
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 // List of dialer button IDs.
Leo Hsu83ab2332018-12-18 15:20:45 +0800125 private static final int[] DIALER_KEYS = new int[]{
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 R.id.one, R.id.two, R.id.three,
127 R.id.four, R.id.five, R.id.six,
128 R.id.seven, R.id.eight, R.id.nine,
Leo Hsu83ab2332018-12-18 15:20:45 +0800129 R.id.star, R.id.zero, R.id.pound};
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130
131 // Debug constants.
132 private static final boolean DBG = false;
133 private static final String LOG_TAG = "EmergencyDialer";
134
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700135 /** The length of DTMF tones in milliseconds */
136 private static final int TONE_LENGTH_MS = 150;
137
138 /** The DTMF tone volume relative to other sounds in the stream */
139 private static final int TONE_RELATIVE_VOLUME = 80;
140
141 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
142 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
143
144 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
145
Lucas Dupineb9c5702017-05-10 16:57:09 -0700146 /** 90% opacity, different from other gradients **/
147 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
148
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800149 /** 85% opacity for black background **/
150 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
151
CY Cheng9a69c182018-06-15 21:20:10 +0800152 /** Size limit of emergency shortcut buttons container. **/
153 private static final int SHORTCUT_SIZE_LIMIT = 3;
154
Grace Jia835d8e62020-04-01 15:47:39 -0700155 private static final float COLOR_DELTA = 1.0f / 16.0f;
156
157 /** Dial button color, from packages/apps/PhoneCommon/res/drawable-mdpi/fab_green.png **/
158 @ColorInt private static final int DIALER_GREEN = 0xff00c853;
159
Tyler Gunnc5428972018-03-28 14:15:34 -0700160 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161 private View mDialButton;
162 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800163 private View mEmergencyShortcutView;
164 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800166 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800167 private EccShortcutAdapter mShortcutAdapter;
168 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800169
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700170 private ToneGenerator mToneGenerator;
171 private Object mToneGeneratorLock = new Object();
172
173 // determines if we want to playback local DTMF tones.
174 private boolean mDTMFToneEnabled;
175
Fan Zhang39d81f52022-03-21 22:47:21 +0000176 private EmergencyInfoGroup mEmergencyInfoInDialpad;
177 private EmergencyInfoGroup mEmergencyInfoInShortcut;
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800178
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179 // close activity when screen turns off
180 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
181 @Override
182 public void onReceive(Context context, Intent intent) {
183 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700184 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 }
186 }
187 };
188
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800189 /**
190 * Customize accessibility methods in View.
191 */
192 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
193
194 /**
195 * Stop AccessiblityService from reading the title of a hidden View.
196 *
197 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
198 * in the animation end. The view with an accessibility pane title would call the
199 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
200 * accessibility service to read the pane title of fade out view instead of pane title of
201 * fade in view. So it need to filter out the event called by vanished pane.
202 */
203 @Override
204 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
205 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
206 && host.getVisibility() == View.GONE) {
207 return;
208 }
209 super.onPopulateAccessibilityEvent(host, event);
210 }
211 };
212
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
214
Lucas Dupineb9c5702017-05-10 16:57:09 -0700215 // Background gradient
Michelef6a5ea22019-10-15 16:08:54 -0700216 private ColorDrawable mBackgroundDrawable;
Lucas Dupineb9c5702017-05-10 16:57:09 -0700217 private boolean mSupportsDarkText;
218
Tyler Gunnc5428972018-03-28 14:15:34 -0700219 private boolean mIsWfcEmergencyCallingWarningEnabled;
220 private float mDefaultDigitsTextSize;
221
Leo Hsue3512b62019-02-14 15:53:00 +0800222 private int mEntryType;
Leo Hsubc7553a2019-03-05 15:47:23 +0800223 private ShortcutViewUtils.Config mShortcutViewConfig;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800224
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 @Override
226 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
227 // Do nothing
228 }
229
230 @Override
231 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700232 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 }
234
235 @Override
236 public void afterTextChanged(Editable input) {
237 // Check for special sequences, in particular the "**04" or "**05"
238 // sequences that allow you to enter PIN or PUK-related codes.
239 //
240 // But note we *don't* allow most other special sequences here,
241 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
242 // since those shouldn't be available if the device is locked.
243 //
244 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
245 // here, not the regular handleChars() method.
246 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
247 // A special sequence was entered, clear the digits
248 mDigits.getText().clear();
249 }
250
251 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800252 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 }
254
255 @Override
256 protected void onCreate(Bundle icicle) {
257 super.onCreate(icicle);
258
Leo Hsue3512b62019-02-14 15:53:00 +0800259 mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
260 Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
261
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800262 // Allow turning screen on
263 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264
Chuck Liaocdeebb42018-10-30 12:07:18 +0800265 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
266 PersistableBundle carrierConfig =
267 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
268
Leo Hsubc7553a2019-03-05 15:47:23 +0800269 mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
CY Cheng9a69c182018-06-15 21:20:10 +0800270 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsubc7553a2019-03-05 15:47:23 +0800271 + mShortcutViewConfig.isEnabled());
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800272
Leo Hsubc7553a2019-03-05 15:47:23 +0800273 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800274 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800275 updateTheme(false);
276 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700277 WallpaperColors wallpaperColors =
278 getWallpaperManager().getWallpaperColors(WallpaperManager.FLAG_LOCK);
279 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800280 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700281
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282 setContentView(R.layout.emergency_dialer);
283
Tyler Gunnc5428972018-03-28 14:15:34 -0700284 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 mDigits.setKeyListener(DialerKeyListener.getInstance());
286 mDigits.setOnClickListener(this);
287 mDigits.setOnKeyListener(this);
288 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700289 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700290 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 maybeAddNumberFormatting();
292
Michelef6a5ea22019-10-15 16:08:54 -0700293 mBackgroundDrawable = new ColorDrawable();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700294 Point displaySize = new Point();
295 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
296 .getDefaultDisplay().getSize(displaySize);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700297 mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled()
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800298 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700299 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700300
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 // Check for the presence of the keypad
302 View view = findViewById(R.id.one);
303 if (view != null) {
304 setupKeypad();
305 }
306
307 mDelete = findViewById(R.id.deleteButton);
308 mDelete.setOnClickListener(this);
309 mDelete.setOnLongClickListener(this);
310
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700311 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700312
313 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700314 // Read carrier config through the public API because PhoneGlobals is not available when we
315 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700316 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 mDialButton.setOnClickListener(this);
318 } else {
319 mDialButton.setVisibility(View.GONE);
320 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700321 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
322 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
323 maybeShowWfcEmergencyCallingWarning();
324
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700325 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326
327 if (icicle != null) {
328 super.onRestoreInstanceState(icicle);
329 }
330
331 // Extract phone number from intent
332 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700333 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
335 if (number != null) {
336 mDigits.setText(number);
337 }
338 }
339
340 // if the mToneGenerator creation fails, just continue without it. It is
341 // a local audio signal, and is not as important as the dtmf tone itself.
342 synchronized (mToneGeneratorLock) {
343 if (mToneGenerator == null) {
344 try {
345 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
346 } catch (RuntimeException e) {
347 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
348 mToneGenerator = null;
349 }
350 }
351 }
352
353 final IntentFilter intentFilter = new IntentFilter();
354 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
355 registerReceiver(mBroadcastReceiver, intentFilter);
356
Fan Zhang39d81f52022-03-21 22:47:21 +0000357 mEmergencyInfoInDialpad = findViewById(R.id.emergency_dialer)
358 .findViewById(R.id.emergency_info_button);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800359
Fan Zhang39d81f52022-03-21 22:47:21 +0000360 mEmergencyInfoInShortcut = findViewById(R.id.emergency_dialer_shortcuts)
361 .findViewById(R.id.emergency_info_button);
362
363 setupEmergencyDialpadViews();
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800364
Leo Hsubc7553a2019-03-05 15:47:23 +0800365 if (mShortcutViewConfig.isEnabled()) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800366 setupEmergencyShortcutsView();
367 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700368 }
369
370 @Override
371 protected void onDestroy() {
372 super.onDestroy();
373 synchronized (mToneGeneratorLock) {
374 if (mToneGenerator != null) {
375 mToneGenerator.release();
376 mToneGenerator = null;
377 }
378 }
379 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800380 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
381 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
382 mShortcutDataSetObserver = null;
383 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 }
385
386 @Override
387 protected void onRestoreInstanceState(Bundle icicle) {
388 mLastNumber = icicle.getString(LAST_NUMBER);
389 }
390
391 @Override
392 protected void onSaveInstanceState(Bundle outState) {
393 super.onSaveInstanceState(outState);
394 outState.putString(LAST_NUMBER, mLastNumber);
395 }
396
397 /**
398 * Explicitly turn off number formatting, since it gets in the way of the emergency
399 * number detector
400 */
401 protected void maybeAddNumberFormatting() {
402 // Do nothing.
403 }
404
405 @Override
406 protected void onPostCreate(Bundle savedInstanceState) {
407 super.onPostCreate(savedInstanceState);
408
409 // This can't be done in onCreate(), since the auto-restoring of the digits
410 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
411 // is called. This method will be called every time the activity is created, and
412 // will always happen after onRestoreSavedInstanceState().
413 mDigits.addTextChangedListener(this);
414 }
415
416 private void setupKeypad() {
417 // Setup the listeners for the buttons
418 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700419 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
420 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700421 }
422
423 View view = findViewById(R.id.zero);
424 view.setOnLongClickListener(this);
425 }
426
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800427 @Override
428 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800429 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
430 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
Leo Hsubc7553a2019-03-05 15:47:23 +0800431 if (mShortcutViewConfig.isEnabled() && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800432 && mDialpadView.getVisibility() == View.VISIBLE) {
433 switchView(mEmergencyShortcutView, mDialpadView, true);
434 return;
435 }
436 super.onBackPressed();
437 }
438
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439 /**
440 * handle key events
441 */
442 @Override
443 public boolean onKeyDown(int keyCode, KeyEvent event) {
444 switch (keyCode) {
445 // Happen when there's a "Call" hard button.
446 case KeyEvent.KEYCODE_CALL: {
447 if (TextUtils.isEmpty(mDigits.getText().toString())) {
448 // if we are adding a call from the InCallScreen and the phone
449 // number entered is empty, we just close the dialer to expose
450 // the InCallScreen under it.
451 finish();
452 } else {
453 // otherwise, we place the call.
454 placeCall();
455 }
456 return true;
457 }
458 }
459 return super.onKeyDown(keyCode, event);
460 }
461
462 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700463 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
465 mDigits.onKeyDown(keyCode, event);
466 }
467
468 @Override
469 public boolean onKey(View view, int keyCode, KeyEvent event) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800470 if (view.getId()
471 == R.id.digits) { // Happen when "Done" button of the IME is pressed. This can
472 // happen when this
473 // Activity is forced into landscape mode due to a desk dock.
474 if (keyCode == KeyEvent.KEYCODE_ENTER
475 && event.getAction() == KeyEvent.ACTION_UP) {
476 placeCall();
477 return true;
478 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 }
480 return false;
481 }
482
483 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700484 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800485 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700486 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800487 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700488 return handled;
489 }
490
491 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800492 public void onConfirmClick(EmergencyShortcutButton button) {
493 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800494 String phoneNumber = button.getPhoneNumber();
495
496 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800497 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800498
Hall Liu6f35de92020-01-23 14:29:26 -0800499 placeCall(phoneNumber, TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800500 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800501 } else {
502 Log.d(LOG_TAG, "emergency number is empty");
503 }
504 }
505
506 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800507 public void onConfirmClick(EmergencyInfoGroup button) {
508 if (button == null) return;
509
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800510 Intent intent = (Intent) button.getTag(R.id.tag_intent);
511 if (intent != null) {
512 startActivity(intent);
513 }
514 }
515
516 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517 public void onClick(View view) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800518 if (view.getId() == R.id.deleteButton) {
519 keyPressed(KeyEvent.KEYCODE_DEL);
520 return;
521 } else if (view.getId() == R.id.floating_action_button) {
522 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
523 placeCall();
524 return;
525 } else if (view.getId() == R.id.digits) {
526 if (mDigits.length() != 0) {
527 mDigits.setCursorVisible(true);
Yorke Lee23a70732014-08-14 17:12:01 -0700528 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800529 return;
530 } else if (view.getId() == R.id.floating_action_button_dialpad) {
531 mDigits.getText().clear();
532 switchView(mDialpadView, mEmergencyShortcutView, true);
533 return;
Yorke Lee23a70732014-08-14 17:12:01 -0700534 }
535 }
536
537 @Override
538 public void onPressed(View view, boolean pressed) {
539 if (!pressed) {
540 return;
541 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800542 if (view.getId() == R.id.one) {
543 playTone(ToneGenerator.TONE_DTMF_1);
544 keyPressed(KeyEvent.KEYCODE_1);
545 return;
546 } else if (view.getId() == R.id.two) {
547 playTone(ToneGenerator.TONE_DTMF_2);
548 keyPressed(KeyEvent.KEYCODE_2);
549 return;
550 } else if (view.getId() == R.id.three) {
551 playTone(ToneGenerator.TONE_DTMF_3);
552 keyPressed(KeyEvent.KEYCODE_3);
553 return;
554 } else if (view.getId() == R.id.four) {
555 playTone(ToneGenerator.TONE_DTMF_4);
556 keyPressed(KeyEvent.KEYCODE_4);
557 return;
558 } else if (view.getId() == R.id.five) {
559 playTone(ToneGenerator.TONE_DTMF_5);
560 keyPressed(KeyEvent.KEYCODE_5);
561 return;
562 } else if (view.getId() == R.id.six) {
563 playTone(ToneGenerator.TONE_DTMF_6);
564 keyPressed(KeyEvent.KEYCODE_6);
565 return;
566 } else if (view.getId() == R.id.seven) {
567 playTone(ToneGenerator.TONE_DTMF_7);
568 keyPressed(KeyEvent.KEYCODE_7);
569 return;
570 } else if (view.getId() == R.id.eight) {
571 playTone(ToneGenerator.TONE_DTMF_8);
572 keyPressed(KeyEvent.KEYCODE_8);
573 return;
574 } else if (view.getId() == R.id.nine) {
575 playTone(ToneGenerator.TONE_DTMF_9);
576 keyPressed(KeyEvent.KEYCODE_9);
577 return;
578 } else if (view.getId() == R.id.zero) {
579 playTone(ToneGenerator.TONE_DTMF_0);
580 keyPressed(KeyEvent.KEYCODE_0);
581 return;
582 } else if (view.getId() == R.id.pound) {
583 playTone(ToneGenerator.TONE_DTMF_P);
584 keyPressed(KeyEvent.KEYCODE_POUND);
585 return;
586 } else if (view.getId() == R.id.star) {
587 playTone(ToneGenerator.TONE_DTMF_S);
588 keyPressed(KeyEvent.KEYCODE_STAR);
589 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 }
591 }
592
593 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 * called for long touch events
595 */
596 @Override
597 public boolean onLongClick(View view) {
598 int id = view.getId();
Amit Mahajan28a499c2019-11-20 15:13:42 -0800599 if (id == R.id.deleteButton) {
600 mDigits.getText().clear();
601 return true;
602 } else if (id == R.id.zero) {
603 removePreviousDigitIfPossible();
604 keyPressed(KeyEvent.KEYCODE_PLUS);
605 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 }
607 return false;
608 }
609
610 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700611 protected void onStart() {
612 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800613
Leo Hsubc7553a2019-03-05 15:47:23 +0800614 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800615 // Shortcut view doesn't support dark text theme.
Michelef6a5ea22019-10-15 16:08:54 -0700616 mBackgroundDrawable.setColor(Color.BLACK);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800617 updateTheme(false);
618 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700619 WallpaperManager wallpaperManager = getWallpaperManager();
620 if (wallpaperManager.isWallpaperSupported()) {
621 wallpaperManager.addOnColorsChangedListener(this, null);
622 }
623
624 WallpaperColors wallpaperColors =
625 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK);
626 mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors));
627 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800628 }
CY Cheng9a69c182018-06-15 21:20:10 +0800629
Leo Hsubc7553a2019-03-05 15:47:23 +0800630 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800631 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800632 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700633 }
634
635 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 protected void onResume() {
637 super.onResume();
638
639 // retrieve the DTMF tone play back setting.
640 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
641 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
642
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700643 // if the mToneGenerator creation fails, just continue without it. It is
644 // a local audio signal, and is not as important as the dtmf tone itself.
645 synchronized (mToneGeneratorLock) {
646 if (mToneGenerator == null) {
647 try {
648 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
649 TONE_RELATIVE_VOLUME);
650 } catch (RuntimeException e) {
651 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
652 mToneGenerator = null;
653 }
654 }
655 }
656
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 updateDialAndDeleteButtonStateEnabledAttr();
658 }
659
660 @Override
661 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700663 }
664
Lucas Dupineb9c5702017-05-10 16:57:09 -0700665 @Override
666 protected void onStop() {
667 super.onStop();
Michelef6a5ea22019-10-15 16:08:54 -0700668
669 WallpaperManager wallpaperManager = getWallpaperManager();
670 if (wallpaperManager.isWallpaperSupported()) {
671 wallpaperManager.removeOnColorsChangedListener(this);
672 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700673 }
674
675 /**
676 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800677 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700678 * @param supportsDarkText true if gradient supports dark text
679 */
680 private void updateTheme(boolean supportsDarkText) {
681 if (mSupportsDarkText == supportsDarkText) {
682 return;
683 }
684 mSupportsDarkText = supportsDarkText;
685
686 // We can't change themes after inflation, in this case we'll have to recreate
687 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700688 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700689 recreate();
690 return;
691 }
692
693 int vis = getWindow().getDecorView().getSystemUiVisibility();
694 if (supportsDarkText) {
695 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
696 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
697 setTheme(R.style.EmergencyDialerThemeDark);
698 } else {
699 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
700 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
701 setTheme(R.style.EmergencyDialerTheme);
702 }
703 getWindow().getDecorView().setSystemUiVisibility(vis);
704 }
705
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700706 /**
707 * place the call, but check to make sure it is a viable number.
708 */
709 private void placeCall() {
710 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900711
712 // Convert into emergency number according to emergency conversion map.
713 // If conversion map is not defined (this is default), this method does
714 // nothing and just returns input number.
715 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
716
Leo Hsubc7553a2019-03-05 15:47:23 +0800717 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800718 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800719 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800720 isEmergencyNumber = true;
721 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800722 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
723 // If a number from SIM/network/... is categoried as police/ambulance/fire,
724 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
725 // shortcut button because a number provided by database has higher priority.
726 isEmergencyNumber = true;
727 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsubc7553a2019-03-05 15:47:23 +0800728 } else {
Automerger Merge Worker96417f12020-02-27 19:34:58 +0000729 try {
730 isEmergencyNumber = getSystemService(TelephonyManager.class)
731 .isEmergencyNumber(mLastNumber);
732 } catch (IllegalStateException ise) {
733 isEmergencyNumber = false;
734 }
Leo Hsu43d670a2018-12-04 15:40:36 +0800735 }
736
737 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700738 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
739
740 // place the call if it is a valid number
741 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
742 // There is no number entered.
743 playTone(ToneGenerator.TONE_PROP_NACK);
744 return;
745 }
Shaotang Li8662a912018-07-04 16:53:01 +0800746
Hall Liu6f35de92020-01-23 14:29:26 -0800747 placeCall(mLastNumber, TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD,
Leo Hsu43d670a2018-12-04 15:40:36 +0800748 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700749 } else {
750 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
751
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700752 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
753 }
Yorke Lee9b341512014-10-17 11:36:41 -0700754 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 }
756
Leo Hsu43d670a2018-12-04 15:40:36 +0800757 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800758 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
759 + ", entry = " + entryTypeToString(mEntryType));
760
Leo Hsu43d670a2018-12-04 15:40:36 +0800761 Bundle extras = new Bundle();
762 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800763 /**
764 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
765 * when the dialed number is ambiguous and identified as both emergency number and any
766 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
767 * number in a country and a non-emergency number of a carrier's customer service hotline.
768 */
769 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800770
771 if (phone != null && phone.getPhoneAccountHandle() != null) {
772 // Requests to dial through the specified phone.
773 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
774 phone.getPhoneAccountHandle());
775 }
776
777 TelecomManager tm = this.getSystemService(TelecomManager.class);
778 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
779 }
780
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700781 /**
782 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
783 *
784 * The tone is played locally, using the audio stream for phone calls.
785 * Tones are played only if the "Audible touch tones" user preference
786 * is checked, and are NOT played if the device is in silent mode.
787 *
788 * @param tone a tone code from {@link ToneGenerator}
789 */
790 void playTone(int tone) {
791 // if local tone playback is disabled, just return.
792 if (!mDTMFToneEnabled) {
793 return;
794 }
795
796 // Also do nothing if the phone is in silent mode.
797 // We need to re-check the ringer mode for *every* playTone()
798 // call, rather than keeping a local flag that's updated in
799 // onResume(), since it's possible to toggle silent mode without
800 // leaving the current activity (via the ENDCALL-longpress menu.)
801 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
802 int ringerMode = audioManager.getRingerMode();
803 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800804 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700805 return;
806 }
807
808 synchronized (mToneGeneratorLock) {
809 if (mToneGenerator == null) {
810 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
811 return;
812 }
813
814 // Start the new tone (will stop any playing tone)
815 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
816 }
817 }
818
819 private CharSequence createErrorMessage(String number) {
820 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800821 String errorString = getString(R.string.dial_emergency_error, number);
822 int startingPosition = errorString.indexOf(number);
823 int endingPosition = startingPosition + number.length();
824 Spannable result = new SpannableString(errorString);
825 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
826 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 } else {
828 return getText(R.string.dial_emergency_empty_error).toString();
829 }
830 }
831
832 @Override
833 protected Dialog onCreateDialog(int id) {
834 AlertDialog dialog = null;
835 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
836 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700837 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
839 .setMessage(createErrorMessage(mLastNumber))
840 .setPositiveButton(R.string.ok, null)
841 .setCancelable(true).create();
842
843 // blur stuff behind the dialog
844 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700845 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846 }
847 return dialog;
848 }
849
850 @Override
851 protected void onPrepareDialog(int id, Dialog dialog) {
852 super.onPrepareDialog(id, dialog);
853 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
854 AlertDialog alert = (AlertDialog) dialog;
855 alert.setMessage(createErrorMessage(mLastNumber));
856 }
857 }
858
Andrew Leed5631e82014-10-08 16:03:58 -0700859 @Override
860 public boolean onOptionsItemSelected(MenuItem item) {
861 final int itemId = item.getItemId();
862 if (itemId == android.R.id.home) {
863 onBackPressed();
864 return true;
865 }
866 return super.onOptionsItemSelected(item);
867 }
868
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 /**
870 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
871 */
872 private void updateDialAndDeleteButtonStateEnabledAttr() {
873 final boolean notEmpty = mDigits.length() != 0;
874
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 mDelete.setEnabled(notEmpty);
876 }
Yorke Lee91311662014-10-24 14:50:45 -0700877
878 /**
879 * Remove the digit just before the current position. Used by various long pressed callbacks
880 * to remove the digit that was populated as a result of the short click.
881 */
882 private void removePreviousDigitIfPossible() {
883 final int currentPosition = mDigits.getSelectionStart();
884 if (currentPosition > 0) {
885 mDigits.setSelection(currentPosition);
886 mDigits.getText().delete(currentPosition - 1, currentPosition);
887 }
888 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800889
890 /**
891 * Update the text-to-speech annotations in the edit field.
892 */
893 private void updateTtsSpans() {
894 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
895 mDigits.getText().removeSpan(o);
896 }
897 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
898 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700899
900 @Override
Michelef6a5ea22019-10-15 16:08:54 -0700901 public void onColorsChanged(WallpaperColors colors, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700902 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Michelef6a5ea22019-10-15 16:08:54 -0700903 mBackgroundDrawable.setColor(getPrimaryColor(colors));
904 updateTheme(supportsDarkText(colors));
Lucas Dupineb9c5702017-05-10 16:57:09 -0700905 }
906 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700907
908 /**
909 * Where a carrier requires a warning that emergency calling is not available while on WFC,
910 * add hint text above the dial pad which warns the user of this case.
911 */
912 private void maybeShowWfcEmergencyCallingWarning() {
913 if (!mIsWfcEmergencyCallingWarningEnabled) {
914 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
915 return;
916 }
917
918 // Use an async task rather than calling into Telephony on UI thread.
919 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
920 @Override
921 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800922 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700923 boolean isWfcAvailable = tm.isWifiCallingAvailable();
924 ServiceState ss = tm.getServiceState();
925 boolean isCellAvailable =
926 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
927 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800928 + " isCellAvailable=" + isCellAvailable
929 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700930 return isWfcAvailable && !isCellAvailable;
931 }
932
933 @Override
934 protected void onPostExecute(Boolean result) {
935 if (result.booleanValue()) {
936 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
937 mDigits.setHint(R.string.dial_emergency_calling_not_available);
938 } else {
939 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
940 mDigits.setHint("");
941 }
942 maybeChangeHintSize();
943 }
944 };
945 showWfcWarningTask.execute((Void) null);
946 }
947
948 /**
949 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
950 * edit view and set the font size to a smaller size appropriate for the emergency calling
951 * warning.
952 */
953 private void maybeChangeHintSize() {
954 if (TextUtils.isEmpty(mDigits.getHint())
955 || !TextUtils.isEmpty(mDigits.getText().toString())) {
956 // No hint or there are dialed digits, so use default size.
957 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
958 // By default, the digits view auto-resizes to fit the text it contains, so
959 // enable that now.
960 mDigits.setResizeEnabled(true);
961 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
962 } else {
963 // Hint present and no dialed digits, set custom font size appropriate for the warning.
964 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
965 R.dimen.emergency_call_warning_size));
966 // Since we're populating this with a static text string, disable auto-resize.
967 mDigits.setResizeEnabled(false);
968 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
969 }
970 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800971
Fan Zhang39d81f52022-03-21 22:47:21 +0000972 private void setupEmergencyDialpadViews() {
973 mEmergencyInfoInDialpad.setOnConfirmClickListener(this);
974 }
975
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800976 private void setupEmergencyShortcutsView() {
977 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
978 mDialpadView = findViewById(R.id.emergency_dialer);
979
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800980 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
981 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
982
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800983 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
984 dialpadButton.setOnClickListener(this);
985
Fan Zhang39d81f52022-03-21 22:47:21 +0000986 mEmergencyInfoInShortcut.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800987
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800988 mEmergencyShortcutButtonList = new ArrayList<>();
989 setupEmergencyCallShortcutButton();
990
Leo Hsu43d670a2018-12-04 15:40:36 +0800991 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800992
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800993 switchView(mEmergencyShortcutView, mDialpadView, false);
994 }
995
Leo Hsu43d670a2018-12-04 15:40:36 +0800996 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800997 final View locationInfo = findViewById(R.id.location_info);
998
Leo Hsubc7553a2019-03-05 15:47:23 +0800999 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +08001000 String countryName = null;
1001 if (!TextUtils.isEmpty(countryIso)) {
1002 Locale locale = Locale.getDefault();
1003 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1004 .getDisplayCountry();
1005 }
1006 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001007 locationInfo.setVisibility(View.INVISIBLE);
1008 } else {
1009 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001010 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001011 locationInfo.setVisibility(View.VISIBLE);
1012 }
1013 }
1014
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001015 private void setupEmergencyCallShortcutButton() {
1016 final ViewGroup shortcutButtonContainer = findViewById(
1017 R.id.emergency_shortcut_buttons_container);
1018 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001019 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001020
CY Cheng9a69c182018-06-15 21:20:10 +08001021 mShortcutAdapter = new EccShortcutAdapter(this) {
1022 @Override
1023 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1024 CharSequence description, int iconRes) {
1025 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1026 .inflate(R.layout.emergency_shortcut_button, parent, false);
1027 button.setPhoneNumber(number);
1028 button.setPhoneDescription(description);
1029 button.setPhoneTypeIcon(iconRes);
1030 button.setOnConfirmClickListener(EmergencyDialer.this);
1031 return button;
1032 }
1033 };
1034 mShortcutDataSetObserver = new DataSetObserver() {
1035 @Override
1036 public void onChanged() {
1037 super.onChanged();
1038 updateLayout();
1039 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001040
CY Cheng9a69c182018-06-15 21:20:10 +08001041 @Override
1042 public void onInvalidated() {
1043 super.onInvalidated();
1044 updateLayout();
1045 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001046
CY Cheng9a69c182018-06-15 21:20:10 +08001047 private void updateLayout() {
1048 // clear previous added buttons
1049 shortcutButtonContainer.removeAllViews();
1050 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001051
CY Cheng9a69c182018-06-15 21:20:10 +08001052 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1053 EmergencyShortcutButton button = (EmergencyShortcutButton)
1054 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1055 mEmergencyShortcutButtonList.add(button);
1056 shortcutButtonContainer.addView(button);
1057 }
1058
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001059 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001060 if (mEmergencyShortcutButtonList.size() > 1) {
1061 emergencyNumberTitle.setText(getString(
1062 R.string.numerous_emergency_numbers_title));
1063 } else {
1064 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1065 }
1066 }
1067 };
1068 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1069 }
1070
Leo Hsu43d670a2018-12-04 15:40:36 +08001071 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001072 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001073 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001074 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001075 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001076 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001077 }
1078 }
1079
1080 /**
1081 * Called by the activity before a touch event is dispatched to the view hierarchy.
1082 */
1083 private void onPreTouchEvent(MotionEvent event) {
Fan Zhang39d81f52022-03-21 22:47:21 +00001084 mEmergencyInfoInDialpad.onPreTouchEvent(event);
1085 mEmergencyInfoInShortcut.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001086
1087 if (mEmergencyShortcutButtonList != null) {
1088 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1089 button.onPreTouchEvent(event);
1090 }
1091 }
1092 }
1093
1094 /**
1095 * Called by the activity after a touch event is dispatched to the view hierarchy.
1096 */
1097 private void onPostTouchEvent(MotionEvent event) {
Fan Zhang39d81f52022-03-21 22:47:21 +00001098 mEmergencyInfoInDialpad.onPostTouchEvent(event);
1099 mEmergencyInfoInShortcut.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001100
1101 if (mEmergencyShortcutButtonList != null) {
1102 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1103 button.onPostTouchEvent(event);
1104 }
1105 }
1106 }
1107
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001108 /**
1109 * Switch two view.
1110 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001111 * @param displayView the view would be displayed.
1112 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001113 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1114 */
1115 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1116 if (displayView == null || hideView == null) {
1117 return;
1118 }
1119
1120 if (displayView.getVisibility() == View.VISIBLE) {
1121 return;
1122 }
1123
1124 if (hasAnimation) {
1125 crossfade(hideView, displayView);
1126 } else {
1127 hideView.setVisibility(View.GONE);
1128 displayView.setVisibility(View.VISIBLE);
1129 }
1130 }
1131
1132 /**
1133 * Fade out and fade in animation between two view transition.
1134 */
1135 private void crossfade(View fadeOutView, View fadeInView) {
1136 if (fadeOutView == null || fadeInView == null) {
1137 return;
1138 }
1139 final int shortAnimationDuration = getResources().getInteger(
1140 android.R.integer.config_shortAnimTime);
1141
1142 fadeInView.setAlpha(0f);
1143 fadeInView.setVisibility(View.VISIBLE);
1144
1145 fadeInView.animate()
1146 .alpha(1f)
1147 .setDuration(shortAnimationDuration)
1148 .setListener(null);
1149
1150 fadeOutView.animate()
1151 .alpha(0f)
1152 .setDuration(shortAnimationDuration)
1153 .setListener(new AnimatorListenerAdapter() {
1154 @Override
1155 public void onAnimationEnd(Animator animation) {
1156 fadeOutView.setVisibility(View.GONE);
1157 }
1158 });
1159 }
Shaotang Li8662a912018-07-04 16:53:01 +08001160
Shaotang Li8662a912018-07-04 16:53:01 +08001161 private boolean isShortcutNumber(String number) {
1162 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1163 return false;
1164 }
1165
1166 boolean isShortcut = false;
1167 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1168 if (button != null && number.equals(button.getPhoneNumber())) {
1169 isShortcut = true;
1170 break;
1171 }
1172 }
1173 return isShortcut;
1174 }
Leo Hsue3512b62019-02-14 15:53:00 +08001175
1176 private String entryTypeToString(int entryType) {
1177 switch (entryType) {
1178 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1179 return "LockScreen";
1180 case ENTRY_TYPE_POWER_MENU:
1181 return "PowerMenu";
1182 default:
1183 return "Unknown-" + entryType;
1184 }
1185 }
1186
1187 private String callSourceToString(int callSource) {
1188 switch (callSource) {
Hall Liu6f35de92020-01-23 14:29:26 -08001189 case TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD:
Leo Hsue3512b62019-02-14 15:53:00 +08001190 return "DialPad";
Hall Liu6f35de92020-01-23 14:29:26 -08001191 case TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT:
Leo Hsue3512b62019-02-14 15:53:00 +08001192 return "Shortcut";
1193 default:
1194 return "Unknown-" + callSource;
1195 }
1196 }
Michelef6a5ea22019-10-15 16:08:54 -07001197
1198 private WallpaperManager getWallpaperManager() {
1199 return getSystemService(WallpaperManager.class);
1200 }
1201
1202 private static boolean supportsDarkText(WallpaperColors colors) {
1203 if (colors != null) {
1204 return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
1205 }
1206 // It's possible that wallpaper colors are null (e.g. when colors are being
1207 // processed or a live wallpaper is used). In this case, fallback to same
1208 // behavior as when shortcut view is enabled.
1209 return false;
1210 }
1211
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001212 private int getPrimaryColor(WallpaperColors colors) {
Michelef6a5ea22019-10-15 16:08:54 -07001213 if (colors != null) {
Grace Jia835d8e62020-04-01 15:47:39 -07001214 // Android accessibility scanner
1215 // (https://support.google.com/accessibility/android/answer/7158390)
1216 // suggest small text and graphics have a contrast ratio greater than
1217 // 4.5 with background color. The color generated from wallpaper may not
1218 // follow this rule. Calculate a proper color here.
1219 Color primary = colors.getPrimaryColor();
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001220 Color text;
1221 if (mSupportsDarkText) {
1222 text = Color.valueOf(Color.BLACK);
1223 } else {
1224 text = Color.valueOf(Color.WHITE);
1225 }
Grace Jia835d8e62020-04-01 15:47:39 -07001226 Color dial = Color.valueOf(DIALER_GREEN);
1227 // If current primary color can't follow the contrast ratio rule, make it
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001228 // deeper/lighter and try again.
Grace Jia835d8e62020-04-01 15:47:39 -07001229 while (!checkContrastRatio(primary, text)) {
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001230 primary = getNextColor(primary, mSupportsDarkText);
Grace Jia835d8e62020-04-01 15:47:39 -07001231 }
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001232 if (!mSupportsDarkText) {
1233 while (!checkContrastRatio(primary, dial)) {
1234 primary = getNextColor(primary, mSupportsDarkText);
1235 }
Grace Jia835d8e62020-04-01 15:47:39 -07001236 }
1237 return primary.toArgb();
Michelef6a5ea22019-10-15 16:08:54 -07001238 }
1239 // It's possible that wallpaper colors are null (e.g. when colors are being
1240 // processed or a live wallpaper is used). In this case, fallback to same
1241 // behavior as when shortcut view is enabled.
1242 return Color.BLACK;
1243 }
Grace Jia835d8e62020-04-01 15:47:39 -07001244
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001245 private Color getNextColor(Color color, boolean darkText) {
1246 float sign = darkText ? 1.f : -1.f;
1247 float r = color.red() + sign * COLOR_DELTA;
1248 float g = color.green() + sign * COLOR_DELTA;
1249 float b = color.blue() + sign * COLOR_DELTA;
Grace Jia835d8e62020-04-01 15:47:39 -07001250 if (r < 0f) r = 0f;
1251 if (g < 0f) g = 0f;
1252 if (b < 0f) b = 0f;
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001253 if (r > 1f) r = 1f;
1254 if (g > 1f) g = 1f;
1255 if (b > 1f) b = 1f;
Grace Jia835d8e62020-04-01 15:47:39 -07001256 return Color.valueOf(r, g, b);
1257 }
1258
Grace Jia4c2e6ae2020-04-21 15:35:49 -07001259 private boolean checkContrastRatio(Color color1, Color color2) {
Grace Jia835d8e62020-04-01 15:47:39 -07001260 float lum1 = color1.luminance();
1261 float lum2 = color2.luminance();
1262 double cr;
1263 if (lum1 >= lum2) {
1264 cr = (lum1 + 0.05) / (lum2 + 0.05);
1265 } else {
1266 cr = (lum2 + 0.05) / (lum1 + 0.05);
1267 }
1268
1269 // Make cr greater than 5.0 instead of 4.5 to guarantee that transparent white
1270 // text and graphics can have contrast ratio greather than 4.5 with background.
1271 return cr > 5.0;
1272 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001273}