blob: 6e935f30902d6e7eed4b10433530a865187d9845 [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
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700176 private EmergencyActionGroup mEmergencyActionGroup;
177
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800178 private EmergencyInfoGroup mEmergencyInfoGroup;
179
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 // close activity when screen turns off
181 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
182 @Override
183 public void onReceive(Context context, Intent intent) {
184 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700185 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 }
187 }
188 };
189
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800190 /**
191 * Customize accessibility methods in View.
192 */
193 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
194
195 /**
196 * Stop AccessiblityService from reading the title of a hidden View.
197 *
198 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
199 * in the animation end. The view with an accessibility pane title would call the
200 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
201 * accessibility service to read the pane title of fade out view instead of pane title of
202 * fade in view. So it need to filter out the event called by vanished pane.
203 */
204 @Override
205 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
206 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
207 && host.getVisibility() == View.GONE) {
208 return;
209 }
210 super.onPopulateAccessibilityEvent(host, event);
211 }
212 };
213
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
215
Lucas Dupineb9c5702017-05-10 16:57:09 -0700216 // Background gradient
Michelef6a5ea22019-10-15 16:08:54 -0700217 private ColorDrawable mBackgroundDrawable;
Lucas Dupineb9c5702017-05-10 16:57:09 -0700218 private boolean mSupportsDarkText;
219
Tyler Gunnc5428972018-03-28 14:15:34 -0700220 private boolean mIsWfcEmergencyCallingWarningEnabled;
221 private float mDefaultDigitsTextSize;
222
Leo Hsue3512b62019-02-14 15:53:00 +0800223 private int mEntryType;
Leo Hsubc7553a2019-03-05 15:47:23 +0800224 private ShortcutViewUtils.Config mShortcutViewConfig;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +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 Hsue3512b62019-02-14 15:53:00 +0800260 mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
261 Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
262
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800264 setShowWhenLocked(true);
265 // Allow turning screen on
266 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267
Chuck Liaocdeebb42018-10-30 12:07:18 +0800268 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
269 PersistableBundle carrierConfig =
270 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
271
Leo Hsubc7553a2019-03-05 15:47:23 +0800272 mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
CY Cheng9a69c182018-06-15 21:20:10 +0800273 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsubc7553a2019-03-05 15:47:23 +0800274 + mShortcutViewConfig.isEnabled());
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800275
Leo Hsubc7553a2019-03-05 15:47:23 +0800276 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800277 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800278 updateTheme(false);
279 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700280 WallpaperColors wallpaperColors =
281 getWallpaperManager().getWallpaperColors(WallpaperManager.FLAG_LOCK);
282 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800283 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700284
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 setContentView(R.layout.emergency_dialer);
286
Tyler Gunnc5428972018-03-28 14:15:34 -0700287 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 mDigits.setKeyListener(DialerKeyListener.getInstance());
289 mDigits.setOnClickListener(this);
290 mDigits.setOnKeyListener(this);
291 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700292 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700293 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 maybeAddNumberFormatting();
295
Michelef6a5ea22019-10-15 16:08:54 -0700296 mBackgroundDrawable = new ColorDrawable();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700297 Point displaySize = new Point();
298 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
299 .getDefaultDisplay().getSize(displaySize);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700300 mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled()
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800301 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700302 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700303
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 // Check for the presence of the keypad
305 View view = findViewById(R.id.one);
306 if (view != null) {
307 setupKeypad();
308 }
309
310 mDelete = findViewById(R.id.deleteButton);
311 mDelete.setOnClickListener(this);
312 mDelete.setOnLongClickListener(this);
313
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700314 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315
316 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700317 // Read carrier config through the public API because PhoneGlobals is not available when we
318 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700319 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 mDialButton.setOnClickListener(this);
321 } else {
322 mDialButton.setVisibility(View.GONE);
323 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700324 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
325 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
326 maybeShowWfcEmergencyCallingWarning();
327
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700328 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329
330 if (icicle != null) {
331 super.onRestoreInstanceState(icicle);
332 }
333
334 // Extract phone number from intent
335 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700336 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
338 if (number != null) {
339 mDigits.setText(number);
340 }
341 }
342
343 // if the mToneGenerator creation fails, just continue without it. It is
344 // a local audio signal, and is not as important as the dtmf tone itself.
345 synchronized (mToneGeneratorLock) {
346 if (mToneGenerator == null) {
347 try {
348 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
349 } catch (RuntimeException e) {
350 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
351 mToneGenerator = null;
352 }
353 }
354 }
355
356 final IntentFilter intentFilter = new IntentFilter();
357 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
358 registerReceiver(mBroadcastReceiver, intentFilter);
359
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700360 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800361
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800362 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
363
Leo Hsubc7553a2019-03-05 15:47:23 +0800364 if (mShortcutViewConfig.isEnabled()) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800365 setupEmergencyShortcutsView();
366 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367 }
368
369 @Override
370 protected void onDestroy() {
371 super.onDestroy();
372 synchronized (mToneGeneratorLock) {
373 if (mToneGenerator != null) {
374 mToneGenerator.release();
375 mToneGenerator = null;
376 }
377 }
378 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800379 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
380 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
381 mShortcutDataSetObserver = null;
382 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700383 }
384
385 @Override
386 protected void onRestoreInstanceState(Bundle icicle) {
387 mLastNumber = icicle.getString(LAST_NUMBER);
388 }
389
390 @Override
391 protected void onSaveInstanceState(Bundle outState) {
392 super.onSaveInstanceState(outState);
393 outState.putString(LAST_NUMBER, mLastNumber);
394 }
395
396 /**
397 * Explicitly turn off number formatting, since it gets in the way of the emergency
398 * number detector
399 */
400 protected void maybeAddNumberFormatting() {
401 // Do nothing.
402 }
403
404 @Override
405 protected void onPostCreate(Bundle savedInstanceState) {
406 super.onPostCreate(savedInstanceState);
407
408 // This can't be done in onCreate(), since the auto-restoring of the digits
409 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
410 // is called. This method will be called every time the activity is created, and
411 // will always happen after onRestoreSavedInstanceState().
412 mDigits.addTextChangedListener(this);
413 }
414
415 private void setupKeypad() {
416 // Setup the listeners for the buttons
417 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700418 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
419 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420 }
421
422 View view = findViewById(R.id.zero);
423 view.setOnLongClickListener(this);
424 }
425
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800426 @Override
427 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800428 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
429 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
Leo Hsubc7553a2019-03-05 15:47:23 +0800430 if (mShortcutViewConfig.isEnabled() && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800431 && mDialpadView.getVisibility() == View.VISIBLE) {
432 switchView(mEmergencyShortcutView, mDialpadView, true);
433 return;
434 }
435 super.onBackPressed();
436 }
437
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700438 /**
439 * handle key events
440 */
441 @Override
442 public boolean onKeyDown(int keyCode, KeyEvent event) {
443 switch (keyCode) {
444 // Happen when there's a "Call" hard button.
445 case KeyEvent.KEYCODE_CALL: {
446 if (TextUtils.isEmpty(mDigits.getText().toString())) {
447 // if we are adding a call from the InCallScreen and the phone
448 // number entered is empty, we just close the dialer to expose
449 // the InCallScreen under it.
450 finish();
451 } else {
452 // otherwise, we place the call.
453 placeCall();
454 }
455 return true;
456 }
457 }
458 return super.onKeyDown(keyCode, event);
459 }
460
461 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700462 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700463 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
464 mDigits.onKeyDown(keyCode, event);
465 }
466
467 @Override
468 public boolean onKey(View view, int keyCode, KeyEvent event) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800469 if (view.getId()
470 == R.id.digits) { // Happen when "Done" button of the IME is pressed. This can
471 // happen when this
472 // Activity is forced into landscape mode due to a desk dock.
473 if (keyCode == KeyEvent.KEYCODE_ENTER
474 && event.getAction() == KeyEvent.ACTION_UP) {
475 placeCall();
476 return true;
477 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700478 }
479 return false;
480 }
481
482 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700483 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800484 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700485 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800486 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700487 return handled;
488 }
489
490 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800491 public void onConfirmClick(EmergencyShortcutButton button) {
492 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800493 String phoneNumber = button.getPhoneNumber();
494
495 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800496 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800497
Hall Liu6f35de92020-01-23 14:29:26 -0800498 placeCall(phoneNumber, TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800499 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800500 } else {
501 Log.d(LOG_TAG, "emergency number is empty");
502 }
503 }
504
505 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800506 public void onConfirmClick(EmergencyInfoGroup button) {
507 if (button == null) return;
508
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800509 Intent intent = (Intent) button.getTag(R.id.tag_intent);
510 if (intent != null) {
511 startActivity(intent);
512 }
513 }
514
515 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700516 public void onClick(View view) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800517 if (view.getId() == R.id.deleteButton) {
518 keyPressed(KeyEvent.KEYCODE_DEL);
519 return;
520 } else if (view.getId() == R.id.floating_action_button) {
521 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
522 placeCall();
523 return;
524 } else if (view.getId() == R.id.digits) {
525 if (mDigits.length() != 0) {
526 mDigits.setCursorVisible(true);
Yorke Lee23a70732014-08-14 17:12:01 -0700527 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800528 return;
529 } else if (view.getId() == R.id.floating_action_button_dialpad) {
530 mDigits.getText().clear();
531 switchView(mDialpadView, mEmergencyShortcutView, true);
532 return;
Yorke Lee23a70732014-08-14 17:12:01 -0700533 }
534 }
535
536 @Override
537 public void onPressed(View view, boolean pressed) {
538 if (!pressed) {
539 return;
540 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800541 if (view.getId() == R.id.one) {
542 playTone(ToneGenerator.TONE_DTMF_1);
543 keyPressed(KeyEvent.KEYCODE_1);
544 return;
545 } else if (view.getId() == R.id.two) {
546 playTone(ToneGenerator.TONE_DTMF_2);
547 keyPressed(KeyEvent.KEYCODE_2);
548 return;
549 } else if (view.getId() == R.id.three) {
550 playTone(ToneGenerator.TONE_DTMF_3);
551 keyPressed(KeyEvent.KEYCODE_3);
552 return;
553 } else if (view.getId() == R.id.four) {
554 playTone(ToneGenerator.TONE_DTMF_4);
555 keyPressed(KeyEvent.KEYCODE_4);
556 return;
557 } else if (view.getId() == R.id.five) {
558 playTone(ToneGenerator.TONE_DTMF_5);
559 keyPressed(KeyEvent.KEYCODE_5);
560 return;
561 } else if (view.getId() == R.id.six) {
562 playTone(ToneGenerator.TONE_DTMF_6);
563 keyPressed(KeyEvent.KEYCODE_6);
564 return;
565 } else if (view.getId() == R.id.seven) {
566 playTone(ToneGenerator.TONE_DTMF_7);
567 keyPressed(KeyEvent.KEYCODE_7);
568 return;
569 } else if (view.getId() == R.id.eight) {
570 playTone(ToneGenerator.TONE_DTMF_8);
571 keyPressed(KeyEvent.KEYCODE_8);
572 return;
573 } else if (view.getId() == R.id.nine) {
574 playTone(ToneGenerator.TONE_DTMF_9);
575 keyPressed(KeyEvent.KEYCODE_9);
576 return;
577 } else if (view.getId() == R.id.zero) {
578 playTone(ToneGenerator.TONE_DTMF_0);
579 keyPressed(KeyEvent.KEYCODE_0);
580 return;
581 } else if (view.getId() == R.id.pound) {
582 playTone(ToneGenerator.TONE_DTMF_P);
583 keyPressed(KeyEvent.KEYCODE_POUND);
584 return;
585 } else if (view.getId() == R.id.star) {
586 playTone(ToneGenerator.TONE_DTMF_S);
587 keyPressed(KeyEvent.KEYCODE_STAR);
588 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589 }
590 }
591
592 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 * called for long touch events
594 */
595 @Override
596 public boolean onLongClick(View view) {
597 int id = view.getId();
Amit Mahajan28a499c2019-11-20 15:13:42 -0800598 if (id == R.id.deleteButton) {
599 mDigits.getText().clear();
600 return true;
601 } else if (id == R.id.zero) {
602 removePreviousDigitIfPossible();
603 keyPressed(KeyEvent.KEYCODE_PLUS);
604 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605 }
606 return false;
607 }
608
609 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700610 protected void onStart() {
611 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800612
Leo Hsubc7553a2019-03-05 15:47:23 +0800613 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800614 // Shortcut view doesn't support dark text theme.
Michelef6a5ea22019-10-15 16:08:54 -0700615 mBackgroundDrawable.setColor(Color.BLACK);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800616 updateTheme(false);
617 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700618 WallpaperManager wallpaperManager = getWallpaperManager();
619 if (wallpaperManager.isWallpaperSupported()) {
620 wallpaperManager.addOnColorsChangedListener(this, null);
621 }
622
623 WallpaperColors wallpaperColors =
624 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK);
625 mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors));
626 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800627 }
CY Cheng9a69c182018-06-15 21:20:10 +0800628
Leo Hsubc7553a2019-03-05 15:47:23 +0800629 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800630 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800631 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700632 }
633
634 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 protected void onResume() {
636 super.onResume();
637
638 // retrieve the DTMF tone play back setting.
639 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
640 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
641
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 // if the mToneGenerator creation fails, just continue without it. It is
643 // a local audio signal, and is not as important as the dtmf tone itself.
644 synchronized (mToneGeneratorLock) {
645 if (mToneGenerator == null) {
646 try {
647 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
648 TONE_RELATIVE_VOLUME);
649 } catch (RuntimeException e) {
650 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
651 mToneGenerator = null;
652 }
653 }
654 }
655
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700656 updateDialAndDeleteButtonStateEnabledAttr();
657 }
658
659 @Override
660 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700661 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 }
663
Lucas Dupineb9c5702017-05-10 16:57:09 -0700664 @Override
665 protected void onStop() {
666 super.onStop();
Michelef6a5ea22019-10-15 16:08:54 -0700667
668 WallpaperManager wallpaperManager = getWallpaperManager();
669 if (wallpaperManager.isWallpaperSupported()) {
670 wallpaperManager.removeOnColorsChangedListener(this);
671 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700672 }
673
674 /**
675 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800676 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700677 * @param supportsDarkText true if gradient supports dark text
678 */
679 private void updateTheme(boolean supportsDarkText) {
680 if (mSupportsDarkText == supportsDarkText) {
681 return;
682 }
683 mSupportsDarkText = supportsDarkText;
684
685 // We can't change themes after inflation, in this case we'll have to recreate
686 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700687 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700688 recreate();
689 return;
690 }
691
692 int vis = getWindow().getDecorView().getSystemUiVisibility();
693 if (supportsDarkText) {
694 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
695 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
696 setTheme(R.style.EmergencyDialerThemeDark);
697 } else {
698 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
699 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
700 setTheme(R.style.EmergencyDialerTheme);
701 }
702 getWindow().getDecorView().setSystemUiVisibility(vis);
703 }
704
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700705 /**
706 * place the call, but check to make sure it is a viable number.
707 */
708 private void placeCall() {
709 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900710
711 // Convert into emergency number according to emergency conversion map.
712 // If conversion map is not defined (this is default), this method does
713 // nothing and just returns input number.
714 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
715
Leo Hsubc7553a2019-03-05 15:47:23 +0800716 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800717 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800718 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800719 isEmergencyNumber = true;
720 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800721 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
722 // If a number from SIM/network/... is categoried as police/ambulance/fire,
723 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
724 // shortcut button because a number provided by database has higher priority.
725 isEmergencyNumber = true;
726 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsubc7553a2019-03-05 15:47:23 +0800727 } else {
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800728 isEmergencyNumber = getSystemService(TelephonyManager.class)
729 .isEmergencyNumber(mLastNumber);
Leo Hsu43d670a2018-12-04 15:40:36 +0800730 }
731
732 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
734
735 // place the call if it is a valid number
736 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
737 // There is no number entered.
738 playTone(ToneGenerator.TONE_PROP_NACK);
739 return;
740 }
Shaotang Li8662a912018-07-04 16:53:01 +0800741
Hall Liu6f35de92020-01-23 14:29:26 -0800742 placeCall(mLastNumber, TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD,
Leo Hsu43d670a2018-12-04 15:40:36 +0800743 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 } else {
745 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
746
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700747 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
748 }
Yorke Lee9b341512014-10-17 11:36:41 -0700749 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700750 }
751
Leo Hsu43d670a2018-12-04 15:40:36 +0800752 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800753 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
754 + ", entry = " + entryTypeToString(mEntryType));
755
Leo Hsu43d670a2018-12-04 15:40:36 +0800756 Bundle extras = new Bundle();
757 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800758 /**
759 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
760 * when the dialed number is ambiguous and identified as both emergency number and any
761 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
762 * number in a country and a non-emergency number of a carrier's customer service hotline.
763 */
764 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800765
766 if (phone != null && phone.getPhoneAccountHandle() != null) {
767 // Requests to dial through the specified phone.
768 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
769 phone.getPhoneAccountHandle());
770 }
771
772 TelecomManager tm = this.getSystemService(TelecomManager.class);
773 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
774 }
775
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776 /**
777 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
778 *
779 * The tone is played locally, using the audio stream for phone calls.
780 * Tones are played only if the "Audible touch tones" user preference
781 * is checked, and are NOT played if the device is in silent mode.
782 *
783 * @param tone a tone code from {@link ToneGenerator}
784 */
785 void playTone(int tone) {
786 // if local tone playback is disabled, just return.
787 if (!mDTMFToneEnabled) {
788 return;
789 }
790
791 // Also do nothing if the phone is in silent mode.
792 // We need to re-check the ringer mode for *every* playTone()
793 // call, rather than keeping a local flag that's updated in
794 // onResume(), since it's possible to toggle silent mode without
795 // leaving the current activity (via the ENDCALL-longpress menu.)
796 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
797 int ringerMode = audioManager.getRingerMode();
798 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800799 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 return;
801 }
802
803 synchronized (mToneGeneratorLock) {
804 if (mToneGenerator == null) {
805 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
806 return;
807 }
808
809 // Start the new tone (will stop any playing tone)
810 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
811 }
812 }
813
814 private CharSequence createErrorMessage(String number) {
815 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800816 String errorString = getString(R.string.dial_emergency_error, number);
817 int startingPosition = errorString.indexOf(number);
818 int endingPosition = startingPosition + number.length();
819 Spannable result = new SpannableString(errorString);
820 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
821 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700822 } else {
823 return getText(R.string.dial_emergency_empty_error).toString();
824 }
825 }
826
827 @Override
828 protected Dialog onCreateDialog(int id) {
829 AlertDialog dialog = null;
830 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
831 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700832 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700833 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
834 .setMessage(createErrorMessage(mLastNumber))
835 .setPositiveButton(R.string.ok, null)
836 .setCancelable(true).create();
837
838 // blur stuff behind the dialog
839 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700840 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 }
842 return dialog;
843 }
844
845 @Override
846 protected void onPrepareDialog(int id, Dialog dialog) {
847 super.onPrepareDialog(id, dialog);
848 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
849 AlertDialog alert = (AlertDialog) dialog;
850 alert.setMessage(createErrorMessage(mLastNumber));
851 }
852 }
853
Andrew Leed5631e82014-10-08 16:03:58 -0700854 @Override
855 public boolean onOptionsItemSelected(MenuItem item) {
856 final int itemId = item.getItemId();
857 if (itemId == android.R.id.home) {
858 onBackPressed();
859 return true;
860 }
861 return super.onOptionsItemSelected(item);
862 }
863
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 /**
865 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
866 */
867 private void updateDialAndDeleteButtonStateEnabledAttr() {
868 final boolean notEmpty = mDigits.length() != 0;
869
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700870 mDelete.setEnabled(notEmpty);
871 }
Yorke Lee91311662014-10-24 14:50:45 -0700872
873 /**
874 * Remove the digit just before the current position. Used by various long pressed callbacks
875 * to remove the digit that was populated as a result of the short click.
876 */
877 private void removePreviousDigitIfPossible() {
878 final int currentPosition = mDigits.getSelectionStart();
879 if (currentPosition > 0) {
880 mDigits.setSelection(currentPosition);
881 mDigits.getText().delete(currentPosition - 1, currentPosition);
882 }
883 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800884
885 /**
886 * Update the text-to-speech annotations in the edit field.
887 */
888 private void updateTtsSpans() {
889 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
890 mDigits.getText().removeSpan(o);
891 }
892 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
893 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700894
895 @Override
Michelef6a5ea22019-10-15 16:08:54 -0700896 public void onColorsChanged(WallpaperColors colors, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700897 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Michelef6a5ea22019-10-15 16:08:54 -0700898 mBackgroundDrawable.setColor(getPrimaryColor(colors));
899 updateTheme(supportsDarkText(colors));
Lucas Dupineb9c5702017-05-10 16:57:09 -0700900 }
901 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700902
903 /**
904 * Where a carrier requires a warning that emergency calling is not available while on WFC,
905 * add hint text above the dial pad which warns the user of this case.
906 */
907 private void maybeShowWfcEmergencyCallingWarning() {
908 if (!mIsWfcEmergencyCallingWarningEnabled) {
909 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
910 return;
911 }
912
913 // Use an async task rather than calling into Telephony on UI thread.
914 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
915 @Override
916 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800917 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700918 boolean isWfcAvailable = tm.isWifiCallingAvailable();
919 ServiceState ss = tm.getServiceState();
920 boolean isCellAvailable =
921 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
922 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800923 + " isCellAvailable=" + isCellAvailable
924 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700925 return isWfcAvailable && !isCellAvailable;
926 }
927
928 @Override
929 protected void onPostExecute(Boolean result) {
930 if (result.booleanValue()) {
931 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
932 mDigits.setHint(R.string.dial_emergency_calling_not_available);
933 } else {
934 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
935 mDigits.setHint("");
936 }
937 maybeChangeHintSize();
938 }
939 };
940 showWfcWarningTask.execute((Void) null);
941 }
942
943 /**
944 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
945 * edit view and set the font size to a smaller size appropriate for the emergency calling
946 * warning.
947 */
948 private void maybeChangeHintSize() {
949 if (TextUtils.isEmpty(mDigits.getHint())
950 || !TextUtils.isEmpty(mDigits.getText().toString())) {
951 // No hint or there are dialed digits, so use default size.
952 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
953 // By default, the digits view auto-resizes to fit the text it contains, so
954 // enable that now.
955 mDigits.setResizeEnabled(true);
956 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
957 } else {
958 // Hint present and no dialed digits, set custom font size appropriate for the warning.
959 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
960 R.dimen.emergency_call_warning_size));
961 // Since we're populating this with a static text string, disable auto-resize.
962 mDigits.setResizeEnabled(false);
963 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
964 }
965 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800966
967 private void setupEmergencyShortcutsView() {
968 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
969 mDialpadView = findViewById(R.id.emergency_dialer);
970
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800971 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
972 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
973
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800974 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
975 dialpadButton.setOnClickListener(this);
976
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800977 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800978
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800979 mEmergencyShortcutButtonList = new ArrayList<>();
980 setupEmergencyCallShortcutButton();
981
Leo Hsu43d670a2018-12-04 15:40:36 +0800982 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800983
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800984 switchView(mEmergencyShortcutView, mDialpadView, false);
985 }
986
Leo Hsu43d670a2018-12-04 15:40:36 +0800987 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800988 final View locationInfo = findViewById(R.id.location_info);
989
Leo Hsubc7553a2019-03-05 15:47:23 +0800990 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +0800991 String countryName = null;
992 if (!TextUtils.isEmpty(countryIso)) {
993 Locale locale = Locale.getDefault();
994 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
995 .getDisplayCountry();
996 }
997 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800998 locationInfo.setVisibility(View.INVISIBLE);
999 } else {
1000 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001001 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001002 locationInfo.setVisibility(View.VISIBLE);
1003 }
1004 }
1005
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001006 private void setupEmergencyCallShortcutButton() {
1007 final ViewGroup shortcutButtonContainer = findViewById(
1008 R.id.emergency_shortcut_buttons_container);
1009 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001010 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001011
CY Cheng9a69c182018-06-15 21:20:10 +08001012 mShortcutAdapter = new EccShortcutAdapter(this) {
1013 @Override
1014 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1015 CharSequence description, int iconRes) {
1016 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1017 .inflate(R.layout.emergency_shortcut_button, parent, false);
1018 button.setPhoneNumber(number);
1019 button.setPhoneDescription(description);
1020 button.setPhoneTypeIcon(iconRes);
1021 button.setOnConfirmClickListener(EmergencyDialer.this);
1022 return button;
1023 }
1024 };
1025 mShortcutDataSetObserver = new DataSetObserver() {
1026 @Override
1027 public void onChanged() {
1028 super.onChanged();
1029 updateLayout();
1030 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001031
CY Cheng9a69c182018-06-15 21:20:10 +08001032 @Override
1033 public void onInvalidated() {
1034 super.onInvalidated();
1035 updateLayout();
1036 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001037
CY Cheng9a69c182018-06-15 21:20:10 +08001038 private void updateLayout() {
1039 // clear previous added buttons
1040 shortcutButtonContainer.removeAllViews();
1041 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001042
CY Cheng9a69c182018-06-15 21:20:10 +08001043 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1044 EmergencyShortcutButton button = (EmergencyShortcutButton)
1045 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1046 mEmergencyShortcutButtonList.add(button);
1047 shortcutButtonContainer.addView(button);
1048 }
1049
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001050 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001051 if (mEmergencyShortcutButtonList.size() > 1) {
1052 emergencyNumberTitle.setText(getString(
1053 R.string.numerous_emergency_numbers_title));
1054 } else {
1055 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1056 }
1057 }
1058 };
1059 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1060 }
1061
Leo Hsu43d670a2018-12-04 15:40:36 +08001062 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001063 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001064 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001065 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001066 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001067 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001068 }
1069 }
1070
1071 /**
1072 * Called by the activity before a touch event is dispatched to the view hierarchy.
1073 */
1074 private void onPreTouchEvent(MotionEvent event) {
1075 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001076 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001077
1078 if (mEmergencyShortcutButtonList != null) {
1079 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1080 button.onPreTouchEvent(event);
1081 }
1082 }
1083 }
1084
1085 /**
1086 * Called by the activity after a touch event is dispatched to the view hierarchy.
1087 */
1088 private void onPostTouchEvent(MotionEvent event) {
1089 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001090 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001091
1092 if (mEmergencyShortcutButtonList != null) {
1093 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1094 button.onPostTouchEvent(event);
1095 }
1096 }
1097 }
1098
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001099 /**
1100 * Switch two view.
1101 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001102 * @param displayView the view would be displayed.
1103 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001104 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1105 */
1106 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1107 if (displayView == null || hideView == null) {
1108 return;
1109 }
1110
1111 if (displayView.getVisibility() == View.VISIBLE) {
1112 return;
1113 }
1114
1115 if (hasAnimation) {
1116 crossfade(hideView, displayView);
1117 } else {
1118 hideView.setVisibility(View.GONE);
1119 displayView.setVisibility(View.VISIBLE);
1120 }
1121 }
1122
1123 /**
1124 * Fade out and fade in animation between two view transition.
1125 */
1126 private void crossfade(View fadeOutView, View fadeInView) {
1127 if (fadeOutView == null || fadeInView == null) {
1128 return;
1129 }
1130 final int shortAnimationDuration = getResources().getInteger(
1131 android.R.integer.config_shortAnimTime);
1132
1133 fadeInView.setAlpha(0f);
1134 fadeInView.setVisibility(View.VISIBLE);
1135
1136 fadeInView.animate()
1137 .alpha(1f)
1138 .setDuration(shortAnimationDuration)
1139 .setListener(null);
1140
1141 fadeOutView.animate()
1142 .alpha(0f)
1143 .setDuration(shortAnimationDuration)
1144 .setListener(new AnimatorListenerAdapter() {
1145 @Override
1146 public void onAnimationEnd(Animator animation) {
1147 fadeOutView.setVisibility(View.GONE);
1148 }
1149 });
1150 }
Shaotang Li8662a912018-07-04 16:53:01 +08001151
Shaotang Li8662a912018-07-04 16:53:01 +08001152 private boolean isShortcutNumber(String number) {
1153 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1154 return false;
1155 }
1156
1157 boolean isShortcut = false;
1158 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1159 if (button != null && number.equals(button.getPhoneNumber())) {
1160 isShortcut = true;
1161 break;
1162 }
1163 }
1164 return isShortcut;
1165 }
Leo Hsue3512b62019-02-14 15:53:00 +08001166
1167 private String entryTypeToString(int entryType) {
1168 switch (entryType) {
1169 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1170 return "LockScreen";
1171 case ENTRY_TYPE_POWER_MENU:
1172 return "PowerMenu";
1173 default:
1174 return "Unknown-" + entryType;
1175 }
1176 }
1177
1178 private String callSourceToString(int callSource) {
1179 switch (callSource) {
Hall Liu6f35de92020-01-23 14:29:26 -08001180 case TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD:
Leo Hsue3512b62019-02-14 15:53:00 +08001181 return "DialPad";
Hall Liu6f35de92020-01-23 14:29:26 -08001182 case TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT:
Leo Hsue3512b62019-02-14 15:53:00 +08001183 return "Shortcut";
1184 default:
1185 return "Unknown-" + callSource;
1186 }
1187 }
Michelef6a5ea22019-10-15 16:08:54 -07001188
1189 private WallpaperManager getWallpaperManager() {
1190 return getSystemService(WallpaperManager.class);
1191 }
1192
1193 private static boolean supportsDarkText(WallpaperColors colors) {
1194 if (colors != null) {
1195 return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
1196 }
1197 // It's possible that wallpaper colors are null (e.g. when colors are being
1198 // processed or a live wallpaper is used). In this case, fallback to same
1199 // behavior as when shortcut view is enabled.
1200 return false;
1201 }
1202
1203 private static int getPrimaryColor(WallpaperColors colors) {
1204 if (colors != null) {
Grace Jia835d8e62020-04-01 15:47:39 -07001205 // Android accessibility scanner
1206 // (https://support.google.com/accessibility/android/answer/7158390)
1207 // suggest small text and graphics have a contrast ratio greater than
1208 // 4.5 with background color. The color generated from wallpaper may not
1209 // follow this rule. Calculate a proper color here.
1210 Color primary = colors.getPrimaryColor();
1211 Color text = Color.valueOf(Color.WHITE);
1212 Color dial = Color.valueOf(DIALER_GREEN);
1213 // If current primary color can't follow the contrast ratio rule, make it
1214 // deeper and try again.
1215 while (!checkContrastRatio(primary, text)) {
1216 primary = getDeeper(primary);
1217 }
1218 while (!checkContrastRatio(primary, dial)) {
1219 primary = getDeeper(primary);
1220 }
1221 return primary.toArgb();
Michelef6a5ea22019-10-15 16:08:54 -07001222 }
1223 // It's possible that wallpaper colors are null (e.g. when colors are being
1224 // processed or a live wallpaper is used). In this case, fallback to same
1225 // behavior as when shortcut view is enabled.
1226 return Color.BLACK;
1227 }
Grace Jia835d8e62020-04-01 15:47:39 -07001228
1229 private static Color getDeeper(Color color) {
1230 float r = color.red() - COLOR_DELTA;
1231 float g = color.green() - COLOR_DELTA;
1232 float b = color.blue() - COLOR_DELTA;
1233 if (r < 0f) r = 0f;
1234 if (g < 0f) g = 0f;
1235 if (b < 0f) b = 0f;
1236 return Color.valueOf(r, g, b);
1237 }
1238
1239 private static boolean checkContrastRatio(Color color1, Color color2) {
1240 float lum1 = color1.luminance();
1241 float lum2 = color2.luminance();
1242 double cr;
1243 if (lum1 >= lum2) {
1244 cr = (lum1 + 0.05) / (lum2 + 0.05);
1245 } else {
1246 cr = (lum2 + 0.05) / (lum1 + 0.05);
1247 }
1248
1249 // Make cr greater than 5.0 instead of 4.5 to guarantee that transparent white
1250 // text and graphics can have contrast ratio greather than 4.5 with background.
1251 return cr > 5.0;
1252 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001253}