blob: 119c71b1a148bccc61ebb0f05e660bca8638900b [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Michelef6a5ea22019-10-15 16:08:54 -070026import android.app.WallpaperColors;
Lucas Dupineb9c5702017-05-10 16:57:09 -070027import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080032import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080033import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Michelef6a5ea22019-10-15 16:08:54 -070035import android.graphics.drawable.ColorDrawable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.media.AudioManager;
37import android.media.ToneGenerator;
38import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070039import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.provider.Settings;
Shaotang Li5d906c12018-07-19 17:32:42 +080043import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070044import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070045import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070046import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.PhoneNumberUtils;
Meng Wangebdba862020-01-08 18:00:39 -080048import com.android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070049import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070050import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070053import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080054import android.text.Spannable;
55import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.text.TextUtils;
57import android.text.TextWatcher;
58import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080059import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070061import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070062import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070064import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070065import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080067import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080068import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080070import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080071import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072
Yorke Lee23a70732014-08-14 17:12:01 -070073import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070074import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070075import com.android.phone.common.widget.ResizingTextEditText;
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
Tyler Gunnc5428972018-03-28 14:15:34 -0700155 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 private View mDialButton;
157 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800158 private View mEmergencyShortcutView;
159 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800161 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800162 private EccShortcutAdapter mShortcutAdapter;
163 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800164
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 private ToneGenerator mToneGenerator;
166 private Object mToneGeneratorLock = new Object();
167
168 // determines if we want to playback local DTMF tones.
169 private boolean mDTMFToneEnabled;
170
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700171 private EmergencyActionGroup mEmergencyActionGroup;
172
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800173 private EmergencyInfoGroup mEmergencyInfoGroup;
174
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175 // close activity when screen turns off
176 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
177 @Override
178 public void onReceive(Context context, Intent intent) {
179 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700180 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 }
182 }
183 };
184
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800185 /**
186 * Customize accessibility methods in View.
187 */
188 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
189
190 /**
191 * Stop AccessiblityService from reading the title of a hidden View.
192 *
193 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
194 * in the animation end. The view with an accessibility pane title would call the
195 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
196 * accessibility service to read the pane title of fade out view instead of pane title of
197 * fade in view. So it need to filter out the event called by vanished pane.
198 */
199 @Override
200 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
201 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
202 && host.getVisibility() == View.GONE) {
203 return;
204 }
205 super.onPopulateAccessibilityEvent(host, event);
206 }
207 };
208
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
210
Lucas Dupineb9c5702017-05-10 16:57:09 -0700211 // Background gradient
Michelef6a5ea22019-10-15 16:08:54 -0700212 private ColorDrawable mBackgroundDrawable;
Lucas Dupineb9c5702017-05-10 16:57:09 -0700213 private boolean mSupportsDarkText;
214
Tyler Gunnc5428972018-03-28 14:15:34 -0700215 private boolean mIsWfcEmergencyCallingWarningEnabled;
216 private float mDefaultDigitsTextSize;
217
Leo Hsue3512b62019-02-14 15:53:00 +0800218 private int mEntryType;
Leo Hsubc7553a2019-03-05 15:47:23 +0800219 private ShortcutViewUtils.Config mShortcutViewConfig;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800220
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 @Override
222 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
223 // Do nothing
224 }
225
226 @Override
227 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700228 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 }
230
231 @Override
232 public void afterTextChanged(Editable input) {
233 // Check for special sequences, in particular the "**04" or "**05"
234 // sequences that allow you to enter PIN or PUK-related codes.
235 //
236 // But note we *don't* allow most other special sequences here,
237 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
238 // since those shouldn't be available if the device is locked.
239 //
240 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
241 // here, not the regular handleChars() method.
242 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
243 // A special sequence was entered, clear the digits
244 mDigits.getText().clear();
245 }
246
247 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800248 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249 }
250
251 @Override
252 protected void onCreate(Bundle icicle) {
253 super.onCreate(icicle);
254
Leo Hsue3512b62019-02-14 15:53:00 +0800255 mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
256 Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
257
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800259 setShowWhenLocked(true);
260 // Allow turning screen on
261 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262
Chuck Liaocdeebb42018-10-30 12:07:18 +0800263 CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class);
264 PersistableBundle carrierConfig =
265 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
266
Leo Hsubc7553a2019-03-05 15:47:23 +0800267 mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
CY Cheng9a69c182018-06-15 21:20:10 +0800268 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
Leo Hsubc7553a2019-03-05 15:47:23 +0800269 + mShortcutViewConfig.isEnabled());
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800270
Leo Hsubc7553a2019-03-05 15:47:23 +0800271 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800272 // Shortcut view doesn't support dark text theme.
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800273 updateTheme(false);
274 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700275 WallpaperColors wallpaperColors =
276 getWallpaperManager().getWallpaperColors(WallpaperManager.FLAG_LOCK);
277 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800278 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700279
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 setContentView(R.layout.emergency_dialer);
281
Tyler Gunnc5428972018-03-28 14:15:34 -0700282 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283 mDigits.setKeyListener(DialerKeyListener.getInstance());
284 mDigits.setOnClickListener(this);
285 mDigits.setOnKeyListener(this);
286 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700287 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700288 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 maybeAddNumberFormatting();
290
Michelef6a5ea22019-10-15 16:08:54 -0700291 mBackgroundDrawable = new ColorDrawable();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700292 Point displaySize = new Point();
293 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
294 .getDefaultDisplay().getSize(displaySize);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700295 mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled()
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800296 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700297 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700298
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 // Check for the presence of the keypad
300 View view = findViewById(R.id.one);
301 if (view != null) {
302 setupKeypad();
303 }
304
305 mDelete = findViewById(R.id.deleteButton);
306 mDelete.setOnClickListener(this);
307 mDelete.setOnLongClickListener(this);
308
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700309 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310
311 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700312 // Read carrier config through the public API because PhoneGlobals is not available when we
313 // run as a secondary user.
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700314 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 mDialButton.setOnClickListener(this);
316 } else {
317 mDialButton.setVisibility(View.GONE);
318 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700319 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
320 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
321 maybeShowWfcEmergencyCallingWarning();
322
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700323 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700324
325 if (icicle != null) {
326 super.onRestoreInstanceState(icicle);
327 }
328
329 // Extract phone number from intent
330 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700331 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
333 if (number != null) {
334 mDigits.setText(number);
335 }
336 }
337
338 // if the mToneGenerator creation fails, just continue without it. It is
339 // a local audio signal, and is not as important as the dtmf tone itself.
340 synchronized (mToneGeneratorLock) {
341 if (mToneGenerator == null) {
342 try {
343 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
344 } catch (RuntimeException e) {
345 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
346 mToneGenerator = null;
347 }
348 }
349 }
350
351 final IntentFilter intentFilter = new IntentFilter();
352 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
353 registerReceiver(mBroadcastReceiver, intentFilter);
354
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700355 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800356
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800357 mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
358
Leo Hsubc7553a2019-03-05 15:47:23 +0800359 if (mShortcutViewConfig.isEnabled()) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800360 setupEmergencyShortcutsView();
361 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362 }
363
364 @Override
365 protected void onDestroy() {
366 super.onDestroy();
367 synchronized (mToneGeneratorLock) {
368 if (mToneGenerator != null) {
369 mToneGenerator.release();
370 mToneGenerator = null;
371 }
372 }
373 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800374 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
375 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
376 mShortcutDataSetObserver = null;
377 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700378 }
379
380 @Override
381 protected void onRestoreInstanceState(Bundle icicle) {
382 mLastNumber = icicle.getString(LAST_NUMBER);
383 }
384
385 @Override
386 protected void onSaveInstanceState(Bundle outState) {
387 super.onSaveInstanceState(outState);
388 outState.putString(LAST_NUMBER, mLastNumber);
389 }
390
391 /**
392 * Explicitly turn off number formatting, since it gets in the way of the emergency
393 * number detector
394 */
395 protected void maybeAddNumberFormatting() {
396 // Do nothing.
397 }
398
399 @Override
400 protected void onPostCreate(Bundle savedInstanceState) {
401 super.onPostCreate(savedInstanceState);
402
403 // This can't be done in onCreate(), since the auto-restoring of the digits
404 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
405 // is called. This method will be called every time the activity is created, and
406 // will always happen after onRestoreSavedInstanceState().
407 mDigits.addTextChangedListener(this);
408 }
409
410 private void setupKeypad() {
411 // Setup the listeners for the buttons
412 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700413 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
414 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 }
416
417 View view = findViewById(R.id.zero);
418 view.setOnLongClickListener(this);
419 }
420
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800421 @Override
422 public void onBackPressed() {
Leo Hsu83ab2332018-12-18 15:20:45 +0800423 // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
424 // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
Leo Hsubc7553a2019-03-05 15:47:23 +0800425 if (mShortcutViewConfig.isEnabled() && mDialpadView != null
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800426 && mDialpadView.getVisibility() == View.VISIBLE) {
427 switchView(mEmergencyShortcutView, mDialpadView, true);
428 return;
429 }
430 super.onBackPressed();
431 }
432
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 /**
434 * handle key events
435 */
436 @Override
437 public boolean onKeyDown(int keyCode, KeyEvent event) {
438 switch (keyCode) {
439 // Happen when there's a "Call" hard button.
440 case KeyEvent.KEYCODE_CALL: {
441 if (TextUtils.isEmpty(mDigits.getText().toString())) {
442 // if we are adding a call from the InCallScreen and the phone
443 // number entered is empty, we just close the dialer to expose
444 // the InCallScreen under it.
445 finish();
446 } else {
447 // otherwise, we place the call.
448 placeCall();
449 }
450 return true;
451 }
452 }
453 return super.onKeyDown(keyCode, event);
454 }
455
456 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700457 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700458 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
459 mDigits.onKeyDown(keyCode, event);
460 }
461
462 @Override
463 public boolean onKey(View view, int keyCode, KeyEvent event) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800464 if (view.getId()
465 == R.id.digits) { // Happen when "Done" button of the IME is pressed. This can
466 // happen when this
467 // Activity is forced into landscape mode due to a desk dock.
468 if (keyCode == KeyEvent.KEYCODE_ENTER
469 && event.getAction() == KeyEvent.ACTION_UP) {
470 placeCall();
471 return true;
472 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700473 }
474 return false;
475 }
476
477 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700478 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800479 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700480 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800481 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700482 return handled;
483 }
484
485 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800486 public void onConfirmClick(EmergencyShortcutButton button) {
487 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800488 String phoneNumber = button.getPhoneNumber();
489
490 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800491 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800492
Leo Hsu43d670a2018-12-04 15:40:36 +0800493 placeCall(phoneNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800494 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800495 } else {
496 Log.d(LOG_TAG, "emergency number is empty");
497 }
498 }
499
500 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800501 public void onConfirmClick(EmergencyInfoGroup button) {
502 if (button == null) return;
503
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800504 Intent intent = (Intent) button.getTag(R.id.tag_intent);
505 if (intent != null) {
506 startActivity(intent);
507 }
508 }
509
510 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700511 public void onClick(View view) {
Amit Mahajan28a499c2019-11-20 15:13:42 -0800512 if (view.getId() == R.id.deleteButton) {
513 keyPressed(KeyEvent.KEYCODE_DEL);
514 return;
515 } else if (view.getId() == R.id.floating_action_button) {
516 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
517 placeCall();
518 return;
519 } else if (view.getId() == R.id.digits) {
520 if (mDigits.length() != 0) {
521 mDigits.setCursorVisible(true);
Yorke Lee23a70732014-08-14 17:12:01 -0700522 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800523 return;
524 } else if (view.getId() == R.id.floating_action_button_dialpad) {
525 mDigits.getText().clear();
526 switchView(mDialpadView, mEmergencyShortcutView, true);
527 return;
Yorke Lee23a70732014-08-14 17:12:01 -0700528 }
529 }
530
531 @Override
532 public void onPressed(View view, boolean pressed) {
533 if (!pressed) {
534 return;
535 }
Amit Mahajan28a499c2019-11-20 15:13:42 -0800536 if (view.getId() == R.id.one) {
537 playTone(ToneGenerator.TONE_DTMF_1);
538 keyPressed(KeyEvent.KEYCODE_1);
539 return;
540 } else if (view.getId() == R.id.two) {
541 playTone(ToneGenerator.TONE_DTMF_2);
542 keyPressed(KeyEvent.KEYCODE_2);
543 return;
544 } else if (view.getId() == R.id.three) {
545 playTone(ToneGenerator.TONE_DTMF_3);
546 keyPressed(KeyEvent.KEYCODE_3);
547 return;
548 } else if (view.getId() == R.id.four) {
549 playTone(ToneGenerator.TONE_DTMF_4);
550 keyPressed(KeyEvent.KEYCODE_4);
551 return;
552 } else if (view.getId() == R.id.five) {
553 playTone(ToneGenerator.TONE_DTMF_5);
554 keyPressed(KeyEvent.KEYCODE_5);
555 return;
556 } else if (view.getId() == R.id.six) {
557 playTone(ToneGenerator.TONE_DTMF_6);
558 keyPressed(KeyEvent.KEYCODE_6);
559 return;
560 } else if (view.getId() == R.id.seven) {
561 playTone(ToneGenerator.TONE_DTMF_7);
562 keyPressed(KeyEvent.KEYCODE_7);
563 return;
564 } else if (view.getId() == R.id.eight) {
565 playTone(ToneGenerator.TONE_DTMF_8);
566 keyPressed(KeyEvent.KEYCODE_8);
567 return;
568 } else if (view.getId() == R.id.nine) {
569 playTone(ToneGenerator.TONE_DTMF_9);
570 keyPressed(KeyEvent.KEYCODE_9);
571 return;
572 } else if (view.getId() == R.id.zero) {
573 playTone(ToneGenerator.TONE_DTMF_0);
574 keyPressed(KeyEvent.KEYCODE_0);
575 return;
576 } else if (view.getId() == R.id.pound) {
577 playTone(ToneGenerator.TONE_DTMF_P);
578 keyPressed(KeyEvent.KEYCODE_POUND);
579 return;
580 } else if (view.getId() == R.id.star) {
581 playTone(ToneGenerator.TONE_DTMF_S);
582 keyPressed(KeyEvent.KEYCODE_STAR);
583 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700584 }
585 }
586
587 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700588 * called for long touch events
589 */
590 @Override
591 public boolean onLongClick(View view) {
592 int id = view.getId();
Amit Mahajan28a499c2019-11-20 15:13:42 -0800593 if (id == R.id.deleteButton) {
594 mDigits.getText().clear();
595 return true;
596 } else if (id == R.id.zero) {
597 removePreviousDigitIfPossible();
598 keyPressed(KeyEvent.KEYCODE_PLUS);
599 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 }
601 return false;
602 }
603
604 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700605 protected void onStart() {
606 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800607
Leo Hsubc7553a2019-03-05 15:47:23 +0800608 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800609 // Shortcut view doesn't support dark text theme.
Michelef6a5ea22019-10-15 16:08:54 -0700610 mBackgroundDrawable.setColor(Color.BLACK);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800611 updateTheme(false);
612 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700613 WallpaperManager wallpaperManager = getWallpaperManager();
614 if (wallpaperManager.isWallpaperSupported()) {
615 wallpaperManager.addOnColorsChangedListener(this, null);
616 }
617
618 WallpaperColors wallpaperColors =
619 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK);
620 mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors));
621 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800622 }
CY Cheng9a69c182018-06-15 21:20:10 +0800623
Leo Hsubc7553a2019-03-05 15:47:23 +0800624 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800625 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800626 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700627 }
628
629 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 protected void onResume() {
631 super.onResume();
632
633 // retrieve the DTMF tone play back setting.
634 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
635 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
636
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700637 // if the mToneGenerator creation fails, just continue without it. It is
638 // a local audio signal, and is not as important as the dtmf tone itself.
639 synchronized (mToneGeneratorLock) {
640 if (mToneGenerator == null) {
641 try {
642 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
643 TONE_RELATIVE_VOLUME);
644 } catch (RuntimeException e) {
645 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
646 mToneGenerator = null;
647 }
648 }
649 }
650
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700651 updateDialAndDeleteButtonStateEnabledAttr();
652 }
653
654 @Override
655 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700656 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 }
658
Lucas Dupineb9c5702017-05-10 16:57:09 -0700659 @Override
660 protected void onStop() {
661 super.onStop();
Michelef6a5ea22019-10-15 16:08:54 -0700662
663 WallpaperManager wallpaperManager = getWallpaperManager();
664 if (wallpaperManager.isWallpaperSupported()) {
665 wallpaperManager.removeOnColorsChangedListener(this);
666 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700667 }
668
669 /**
670 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800671 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700672 * @param supportsDarkText true if gradient supports dark text
673 */
674 private void updateTheme(boolean supportsDarkText) {
675 if (mSupportsDarkText == supportsDarkText) {
676 return;
677 }
678 mSupportsDarkText = supportsDarkText;
679
680 // We can't change themes after inflation, in this case we'll have to recreate
681 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700682 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700683 recreate();
684 return;
685 }
686
687 int vis = getWindow().getDecorView().getSystemUiVisibility();
688 if (supportsDarkText) {
689 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
690 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
691 setTheme(R.style.EmergencyDialerThemeDark);
692 } else {
693 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
694 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
695 setTheme(R.style.EmergencyDialerTheme);
696 }
697 getWindow().getDecorView().setSystemUiVisibility(vis);
698 }
699
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 /**
701 * place the call, but check to make sure it is a viable number.
702 */
703 private void placeCall() {
704 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900705
706 // Convert into emergency number according to emergency conversion map.
707 // If conversion map is not defined (this is default), this method does
708 // nothing and just returns input number.
709 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
710
Leo Hsubc7553a2019-03-05 15:47:23 +0800711 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800712 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800713 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800714 isEmergencyNumber = true;
715 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800716 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
717 // If a number from SIM/network/... is categoried as police/ambulance/fire,
718 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
719 // shortcut button because a number provided by database has higher priority.
720 isEmergencyNumber = true;
721 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsubc7553a2019-03-05 15:47:23 +0800722 } else {
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800723 isEmergencyNumber = getSystemService(TelephonyManager.class)
724 .isEmergencyNumber(mLastNumber);
Leo Hsu43d670a2018-12-04 15:40:36 +0800725 }
726
727 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
729
730 // place the call if it is a valid number
731 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
732 // There is no number entered.
733 playTone(ToneGenerator.TONE_PROP_NACK);
734 return;
735 }
Shaotang Li8662a912018-07-04 16:53:01 +0800736
Leo Hsu43d670a2018-12-04 15:40:36 +0800737 placeCall(mLastNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD,
738 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700739 } else {
740 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
741
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700742 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
743 }
Yorke Lee9b341512014-10-17 11:36:41 -0700744 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700745 }
746
Leo Hsu43d670a2018-12-04 15:40:36 +0800747 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800748 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
749 + ", entry = " + entryTypeToString(mEntryType));
750
Leo Hsu43d670a2018-12-04 15:40:36 +0800751 Bundle extras = new Bundle();
752 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800753 /**
754 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
755 * when the dialed number is ambiguous and identified as both emergency number and any
756 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
757 * number in a country and a non-emergency number of a carrier's customer service hotline.
758 */
759 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800760
761 if (phone != null && phone.getPhoneAccountHandle() != null) {
762 // Requests to dial through the specified phone.
763 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
764 phone.getPhoneAccountHandle());
765 }
766
767 TelecomManager tm = this.getSystemService(TelecomManager.class);
768 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
769 }
770
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700771 /**
772 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
773 *
774 * The tone is played locally, using the audio stream for phone calls.
775 * Tones are played only if the "Audible touch tones" user preference
776 * is checked, and are NOT played if the device is in silent mode.
777 *
778 * @param tone a tone code from {@link ToneGenerator}
779 */
780 void playTone(int tone) {
781 // if local tone playback is disabled, just return.
782 if (!mDTMFToneEnabled) {
783 return;
784 }
785
786 // Also do nothing if the phone is in silent mode.
787 // We need to re-check the ringer mode for *every* playTone()
788 // call, rather than keeping a local flag that's updated in
789 // onResume(), since it's possible to toggle silent mode without
790 // leaving the current activity (via the ENDCALL-longpress menu.)
791 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
792 int ringerMode = audioManager.getRingerMode();
793 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800794 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 return;
796 }
797
798 synchronized (mToneGeneratorLock) {
799 if (mToneGenerator == null) {
800 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
801 return;
802 }
803
804 // Start the new tone (will stop any playing tone)
805 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
806 }
807 }
808
809 private CharSequence createErrorMessage(String number) {
810 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800811 String errorString = getString(R.string.dial_emergency_error, number);
812 int startingPosition = errorString.indexOf(number);
813 int endingPosition = startingPosition + number.length();
814 Spannable result = new SpannableString(errorString);
815 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
816 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 } else {
818 return getText(R.string.dial_emergency_empty_error).toString();
819 }
820 }
821
822 @Override
823 protected Dialog onCreateDialog(int id) {
824 AlertDialog dialog = null;
825 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
826 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700827 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700828 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
829 .setMessage(createErrorMessage(mLastNumber))
830 .setPositiveButton(R.string.ok, null)
831 .setCancelable(true).create();
832
833 // blur stuff behind the dialog
834 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700835 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 }
837 return dialog;
838 }
839
840 @Override
841 protected void onPrepareDialog(int id, Dialog dialog) {
842 super.onPrepareDialog(id, dialog);
843 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
844 AlertDialog alert = (AlertDialog) dialog;
845 alert.setMessage(createErrorMessage(mLastNumber));
846 }
847 }
848
Andrew Leed5631e82014-10-08 16:03:58 -0700849 @Override
850 public boolean onOptionsItemSelected(MenuItem item) {
851 final int itemId = item.getItemId();
852 if (itemId == android.R.id.home) {
853 onBackPressed();
854 return true;
855 }
856 return super.onOptionsItemSelected(item);
857 }
858
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 /**
860 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
861 */
862 private void updateDialAndDeleteButtonStateEnabledAttr() {
863 final boolean notEmpty = mDigits.length() != 0;
864
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 mDelete.setEnabled(notEmpty);
866 }
Yorke Lee91311662014-10-24 14:50:45 -0700867
868 /**
869 * Remove the digit just before the current position. Used by various long pressed callbacks
870 * to remove the digit that was populated as a result of the short click.
871 */
872 private void removePreviousDigitIfPossible() {
873 final int currentPosition = mDigits.getSelectionStart();
874 if (currentPosition > 0) {
875 mDigits.setSelection(currentPosition);
876 mDigits.getText().delete(currentPosition - 1, currentPosition);
877 }
878 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800879
880 /**
881 * Update the text-to-speech annotations in the edit field.
882 */
883 private void updateTtsSpans() {
884 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
885 mDigits.getText().removeSpan(o);
886 }
887 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
888 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700889
890 @Override
Michelef6a5ea22019-10-15 16:08:54 -0700891 public void onColorsChanged(WallpaperColors colors, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700892 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Michelef6a5ea22019-10-15 16:08:54 -0700893 mBackgroundDrawable.setColor(getPrimaryColor(colors));
894 updateTheme(supportsDarkText(colors));
Lucas Dupineb9c5702017-05-10 16:57:09 -0700895 }
896 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700897
898 /**
899 * Where a carrier requires a warning that emergency calling is not available while on WFC,
900 * add hint text above the dial pad which warns the user of this case.
901 */
902 private void maybeShowWfcEmergencyCallingWarning() {
903 if (!mIsWfcEmergencyCallingWarningEnabled) {
904 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
905 return;
906 }
907
908 // Use an async task rather than calling into Telephony on UI thread.
909 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
910 @Override
911 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800912 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700913 boolean isWfcAvailable = tm.isWifiCallingAvailable();
914 ServiceState ss = tm.getServiceState();
915 boolean isCellAvailable =
916 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
917 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800918 + " isCellAvailable=" + isCellAvailable
919 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700920 return isWfcAvailable && !isCellAvailable;
921 }
922
923 @Override
924 protected void onPostExecute(Boolean result) {
925 if (result.booleanValue()) {
926 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
927 mDigits.setHint(R.string.dial_emergency_calling_not_available);
928 } else {
929 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
930 mDigits.setHint("");
931 }
932 maybeChangeHintSize();
933 }
934 };
935 showWfcWarningTask.execute((Void) null);
936 }
937
938 /**
939 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
940 * edit view and set the font size to a smaller size appropriate for the emergency calling
941 * warning.
942 */
943 private void maybeChangeHintSize() {
944 if (TextUtils.isEmpty(mDigits.getHint())
945 || !TextUtils.isEmpty(mDigits.getText().toString())) {
946 // No hint or there are dialed digits, so use default size.
947 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
948 // By default, the digits view auto-resizes to fit the text it contains, so
949 // enable that now.
950 mDigits.setResizeEnabled(true);
951 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
952 } else {
953 // Hint present and no dialed digits, set custom font size appropriate for the warning.
954 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
955 R.dimen.emergency_call_warning_size));
956 // Since we're populating this with a static text string, disable auto-resize.
957 mDigits.setResizeEnabled(false);
958 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
959 }
960 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800961
962 private void setupEmergencyShortcutsView() {
963 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
964 mDialpadView = findViewById(R.id.emergency_dialer);
965
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800966 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
967 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
968
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800969 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
970 dialpadButton.setOnClickListener(this);
971
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800972 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800973
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800974 mEmergencyShortcutButtonList = new ArrayList<>();
975 setupEmergencyCallShortcutButton();
976
Leo Hsu43d670a2018-12-04 15:40:36 +0800977 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800978
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800979 switchView(mEmergencyShortcutView, mDialpadView, false);
980 }
981
Leo Hsu43d670a2018-12-04 15:40:36 +0800982 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800983 final View locationInfo = findViewById(R.id.location_info);
984
Leo Hsubc7553a2019-03-05 15:47:23 +0800985 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +0800986 String countryName = null;
987 if (!TextUtils.isEmpty(countryIso)) {
988 Locale locale = Locale.getDefault();
989 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
990 .getDisplayCountry();
991 }
992 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800993 locationInfo.setVisibility(View.INVISIBLE);
994 } else {
995 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +0800996 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800997 locationInfo.setVisibility(View.VISIBLE);
998 }
999 }
1000
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001001 private void setupEmergencyCallShortcutButton() {
1002 final ViewGroup shortcutButtonContainer = findViewById(
1003 R.id.emergency_shortcut_buttons_container);
1004 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001005 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001006
CY Cheng9a69c182018-06-15 21:20:10 +08001007 mShortcutAdapter = new EccShortcutAdapter(this) {
1008 @Override
1009 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1010 CharSequence description, int iconRes) {
1011 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1012 .inflate(R.layout.emergency_shortcut_button, parent, false);
1013 button.setPhoneNumber(number);
1014 button.setPhoneDescription(description);
1015 button.setPhoneTypeIcon(iconRes);
1016 button.setOnConfirmClickListener(EmergencyDialer.this);
1017 return button;
1018 }
1019 };
1020 mShortcutDataSetObserver = new DataSetObserver() {
1021 @Override
1022 public void onChanged() {
1023 super.onChanged();
1024 updateLayout();
1025 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001026
CY Cheng9a69c182018-06-15 21:20:10 +08001027 @Override
1028 public void onInvalidated() {
1029 super.onInvalidated();
1030 updateLayout();
1031 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001032
CY Cheng9a69c182018-06-15 21:20:10 +08001033 private void updateLayout() {
1034 // clear previous added buttons
1035 shortcutButtonContainer.removeAllViews();
1036 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001037
CY Cheng9a69c182018-06-15 21:20:10 +08001038 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1039 EmergencyShortcutButton button = (EmergencyShortcutButton)
1040 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1041 mEmergencyShortcutButtonList.add(button);
1042 shortcutButtonContainer.addView(button);
1043 }
1044
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001045 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001046 if (mEmergencyShortcutButtonList.size() > 1) {
1047 emergencyNumberTitle.setText(getString(
1048 R.string.numerous_emergency_numbers_title));
1049 } else {
1050 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1051 }
1052 }
1053 };
1054 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1055 }
1056
Leo Hsu43d670a2018-12-04 15:40:36 +08001057 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001058 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001059 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001060 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001061 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001062 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001063 }
1064 }
1065
1066 /**
1067 * Called by the activity before a touch event is dispatched to the view hierarchy.
1068 */
1069 private void onPreTouchEvent(MotionEvent event) {
1070 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001071 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001072
1073 if (mEmergencyShortcutButtonList != null) {
1074 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1075 button.onPreTouchEvent(event);
1076 }
1077 }
1078 }
1079
1080 /**
1081 * Called by the activity after a touch event is dispatched to the view hierarchy.
1082 */
1083 private void onPostTouchEvent(MotionEvent event) {
1084 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001085 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001086
1087 if (mEmergencyShortcutButtonList != null) {
1088 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1089 button.onPostTouchEvent(event);
1090 }
1091 }
1092 }
1093
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001094 /**
1095 * Switch two view.
1096 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001097 * @param displayView the view would be displayed.
1098 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001099 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1100 */
1101 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1102 if (displayView == null || hideView == null) {
1103 return;
1104 }
1105
1106 if (displayView.getVisibility() == View.VISIBLE) {
1107 return;
1108 }
1109
1110 if (hasAnimation) {
1111 crossfade(hideView, displayView);
1112 } else {
1113 hideView.setVisibility(View.GONE);
1114 displayView.setVisibility(View.VISIBLE);
1115 }
1116 }
1117
1118 /**
1119 * Fade out and fade in animation between two view transition.
1120 */
1121 private void crossfade(View fadeOutView, View fadeInView) {
1122 if (fadeOutView == null || fadeInView == null) {
1123 return;
1124 }
1125 final int shortAnimationDuration = getResources().getInteger(
1126 android.R.integer.config_shortAnimTime);
1127
1128 fadeInView.setAlpha(0f);
1129 fadeInView.setVisibility(View.VISIBLE);
1130
1131 fadeInView.animate()
1132 .alpha(1f)
1133 .setDuration(shortAnimationDuration)
1134 .setListener(null);
1135
1136 fadeOutView.animate()
1137 .alpha(0f)
1138 .setDuration(shortAnimationDuration)
1139 .setListener(new AnimatorListenerAdapter() {
1140 @Override
1141 public void onAnimationEnd(Animator animation) {
1142 fadeOutView.setVisibility(View.GONE);
1143 }
1144 });
1145 }
Shaotang Li8662a912018-07-04 16:53:01 +08001146
Shaotang Li8662a912018-07-04 16:53:01 +08001147 private boolean isShortcutNumber(String number) {
1148 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1149 return false;
1150 }
1151
1152 boolean isShortcut = false;
1153 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1154 if (button != null && number.equals(button.getPhoneNumber())) {
1155 isShortcut = true;
1156 break;
1157 }
1158 }
1159 return isShortcut;
1160 }
Leo Hsue3512b62019-02-14 15:53:00 +08001161
1162 private String entryTypeToString(int entryType) {
1163 switch (entryType) {
1164 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1165 return "LockScreen";
1166 case ENTRY_TYPE_POWER_MENU:
1167 return "PowerMenu";
1168 default:
1169 return "Unknown-" + entryType;
1170 }
1171 }
1172
1173 private String callSourceToString(int callSource) {
1174 switch (callSource) {
1175 case ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD:
1176 return "DialPad";
1177 case ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT:
1178 return "Shortcut";
1179 default:
1180 return "Unknown-" + callSource;
1181 }
1182 }
Michelef6a5ea22019-10-15 16:08:54 -07001183
1184 private WallpaperManager getWallpaperManager() {
1185 return getSystemService(WallpaperManager.class);
1186 }
1187
1188 private static boolean supportsDarkText(WallpaperColors colors) {
1189 if (colors != null) {
1190 return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
1191 }
1192 // It's possible that wallpaper colors are null (e.g. when colors are being
1193 // processed or a live wallpaper is used). In this case, fallback to same
1194 // behavior as when shortcut view is enabled.
1195 return false;
1196 }
1197
1198 private static int getPrimaryColor(WallpaperColors colors) {
1199 if (colors != null) {
1200 return colors.getPrimaryColor().toArgb();
1201 }
1202 // It's possible that wallpaper colors are null (e.g. when colors are being
1203 // processed or a live wallpaper is used). In this case, fallback to same
1204 // behavior as when shortcut view is enabled.
1205 return Color.BLACK;
1206 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001207}