blob: c20281a23d87caeeffe70d7441d362eac2a442d8 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Michelef6a5ea22019-10-15 16:08:54 -070026import android.app.WallpaperColors;
Lucas Dupineb9c5702017-05-10 16:57:09 -070027import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080032import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080033import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Michelef6a5ea22019-10-15 16:08:54 -070035import android.graphics.drawable.ColorDrawable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.media.AudioManager;
37import android.media.ToneGenerator;
38import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070039import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.provider.Settings;
Shaotang Li5d906c12018-07-19 17:32:42 +080043import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070044import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070045import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070046import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080048import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070049import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070050import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070053import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080054import android.text.Spannable;
55import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.text.TextUtils;
57import android.text.TextWatcher;
58import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080059import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070061import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070062import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070064import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070065import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.view.View;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080067import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080068import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080070import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080071import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072
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) {
464 switch (view.getId()) {
465 case R.id.digits:
466 // Happen when "Done" button of the IME is pressed. This can 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 }
473 break;
474 }
475 return false;
476 }
477
478 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700479 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800480 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700481 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800482 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700483 return handled;
484 }
485
486 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800487 public void onConfirmClick(EmergencyShortcutButton button) {
488 if (button == null) return;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800489 String phoneNumber = button.getPhoneNumber();
490
491 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800492 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800493
Leo Hsu43d670a2018-12-04 15:40:36 +0800494 placeCall(phoneNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT,
Leo Hsubc7553a2019-03-05 15:47:23 +0800495 mShortcutViewConfig.getPhoneInfo());
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800496 } else {
497 Log.d(LOG_TAG, "emergency number is empty");
498 }
499 }
500
501 @Override
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800502 public void onConfirmClick(EmergencyInfoGroup button) {
503 if (button == null) return;
504
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800505 Intent intent = (Intent) button.getTag(R.id.tag_intent);
506 if (intent != null) {
507 startActivity(intent);
508 }
509 }
510
511 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 public void onClick(View view) {
513 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700514 case R.id.deleteButton: {
515 keyPressed(KeyEvent.KEYCODE_DEL);
516 return;
517 }
518 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700519 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700520 placeCall();
521 return;
522 }
523 case R.id.digits: {
524 if (mDigits.length() != 0) {
525 mDigits.setCursorVisible(true);
526 }
527 return;
528 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800529 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800530 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800531 switchView(mDialpadView, mEmergencyShortcutView, true);
532 return;
533 }
Yorke Lee23a70732014-08-14 17:12:01 -0700534 }
535 }
536
537 @Override
538 public void onPressed(View view, boolean pressed) {
539 if (!pressed) {
540 return;
541 }
542 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 case R.id.one: {
544 playTone(ToneGenerator.TONE_DTMF_1);
545 keyPressed(KeyEvent.KEYCODE_1);
546 return;
547 }
548 case R.id.two: {
549 playTone(ToneGenerator.TONE_DTMF_2);
550 keyPressed(KeyEvent.KEYCODE_2);
551 return;
552 }
553 case R.id.three: {
554 playTone(ToneGenerator.TONE_DTMF_3);
555 keyPressed(KeyEvent.KEYCODE_3);
556 return;
557 }
558 case R.id.four: {
559 playTone(ToneGenerator.TONE_DTMF_4);
560 keyPressed(KeyEvent.KEYCODE_4);
561 return;
562 }
563 case R.id.five: {
564 playTone(ToneGenerator.TONE_DTMF_5);
565 keyPressed(KeyEvent.KEYCODE_5);
566 return;
567 }
568 case R.id.six: {
569 playTone(ToneGenerator.TONE_DTMF_6);
570 keyPressed(KeyEvent.KEYCODE_6);
571 return;
572 }
573 case R.id.seven: {
574 playTone(ToneGenerator.TONE_DTMF_7);
575 keyPressed(KeyEvent.KEYCODE_7);
576 return;
577 }
578 case R.id.eight: {
579 playTone(ToneGenerator.TONE_DTMF_8);
580 keyPressed(KeyEvent.KEYCODE_8);
581 return;
582 }
583 case R.id.nine: {
584 playTone(ToneGenerator.TONE_DTMF_9);
585 keyPressed(KeyEvent.KEYCODE_9);
586 return;
587 }
588 case R.id.zero: {
589 playTone(ToneGenerator.TONE_DTMF_0);
590 keyPressed(KeyEvent.KEYCODE_0);
591 return;
592 }
593 case R.id.pound: {
594 playTone(ToneGenerator.TONE_DTMF_P);
595 keyPressed(KeyEvent.KEYCODE_POUND);
596 return;
597 }
598 case R.id.star: {
599 playTone(ToneGenerator.TONE_DTMF_S);
600 keyPressed(KeyEvent.KEYCODE_STAR);
601 return;
602 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700603 }
604 }
605
606 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 * called for long touch events
608 */
609 @Override
610 public boolean onLongClick(View view) {
611 int id = view.getId();
612 switch (id) {
613 case R.id.deleteButton: {
614 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700615 return true;
616 }
617 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700618 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700619 keyPressed(KeyEvent.KEYCODE_PLUS);
620 return true;
621 }
622 }
623 return false;
624 }
625
626 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700627 protected void onStart() {
628 super.onStart();
Shaotang Li8662a912018-07-04 16:53:01 +0800629
Leo Hsubc7553a2019-03-05 15:47:23 +0800630 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu83ab2332018-12-18 15:20:45 +0800631 // Shortcut view doesn't support dark text theme.
Michelef6a5ea22019-10-15 16:08:54 -0700632 mBackgroundDrawable.setColor(Color.BLACK);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800633 updateTheme(false);
634 } else {
Michelef6a5ea22019-10-15 16:08:54 -0700635 WallpaperManager wallpaperManager = getWallpaperManager();
636 if (wallpaperManager.isWallpaperSupported()) {
637 wallpaperManager.addOnColorsChangedListener(this, null);
638 }
639
640 WallpaperColors wallpaperColors =
641 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK);
642 mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors));
643 updateTheme(supportsDarkText(wallpaperColors));
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800644 }
CY Cheng9a69c182018-06-15 21:20:10 +0800645
Leo Hsubc7553a2019-03-05 15:47:23 +0800646 if (mShortcutViewConfig.isEnabled()) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800647 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +0800648 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700649 }
650
651 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700652 protected void onResume() {
653 super.onResume();
654
655 // retrieve the DTMF tone play back setting.
656 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
657 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
658
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700659 // if the mToneGenerator creation fails, just continue without it. It is
660 // a local audio signal, and is not as important as the dtmf tone itself.
661 synchronized (mToneGeneratorLock) {
662 if (mToneGenerator == null) {
663 try {
664 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
665 TONE_RELATIVE_VOLUME);
666 } catch (RuntimeException e) {
667 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
668 mToneGenerator = null;
669 }
670 }
671 }
672
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 updateDialAndDeleteButtonStateEnabledAttr();
674 }
675
676 @Override
677 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700678 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 }
680
Lucas Dupineb9c5702017-05-10 16:57:09 -0700681 @Override
682 protected void onStop() {
683 super.onStop();
Michelef6a5ea22019-10-15 16:08:54 -0700684
685 WallpaperManager wallpaperManager = getWallpaperManager();
686 if (wallpaperManager.isWallpaperSupported()) {
687 wallpaperManager.removeOnColorsChangedListener(this);
688 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700689 }
690
691 /**
692 * Sets theme based on gradient colors
Leo Hsu83ab2332018-12-18 15:20:45 +0800693 *
Lucas Dupineb9c5702017-05-10 16:57:09 -0700694 * @param supportsDarkText true if gradient supports dark text
695 */
696 private void updateTheme(boolean supportsDarkText) {
697 if (mSupportsDarkText == supportsDarkText) {
698 return;
699 }
700 mSupportsDarkText = supportsDarkText;
701
702 // We can't change themes after inflation, in this case we'll have to recreate
703 // the whole activity.
Lucas Dupin4946f6f2019-04-09 15:03:19 -0700704 if (mBackgroundDrawable != null) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700705 recreate();
706 return;
707 }
708
709 int vis = getWindow().getDecorView().getSystemUiVisibility();
710 if (supportsDarkText) {
711 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
712 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
713 setTheme(R.style.EmergencyDialerThemeDark);
714 } else {
715 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
716 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
717 setTheme(R.style.EmergencyDialerTheme);
718 }
719 getWindow().getDecorView().setSystemUiVisibility(vis);
720 }
721
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700722 /**
723 * place the call, but check to make sure it is a viable number.
724 */
725 private void placeCall() {
726 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900727
728 // Convert into emergency number according to emergency conversion map.
729 // If conversion map is not defined (this is default), this method does
730 // nothing and just returns input number.
731 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
732
Leo Hsubc7553a2019-03-05 15:47:23 +0800733 boolean isEmergencyNumber;
Leo Hsu43d670a2018-12-04 15:40:36 +0800734 ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800735 if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) {
Leo Hsubc7553a2019-03-05 15:47:23 +0800736 isEmergencyNumber = true;
737 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800738 } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
739 // If a number from SIM/network/... is categoried as police/ambulance/fire,
740 // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a
741 // shortcut button because a number provided by database has higher priority.
742 isEmergencyNumber = true;
743 phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
Leo Hsubc7553a2019-03-05 15:47:23 +0800744 } else {
Leo Hsu7bcfb8e2019-05-03 21:54:45 +0800745 isEmergencyNumber = getSystemService(TelephonyManager.class)
746 .isEmergencyNumber(mLastNumber);
Leo Hsu43d670a2018-12-04 15:40:36 +0800747 }
748
749 if (isEmergencyNumber) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700750 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
751
752 // place the call if it is a valid number
753 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
754 // There is no number entered.
755 playTone(ToneGenerator.TONE_PROP_NACK);
756 return;
757 }
Shaotang Li8662a912018-07-04 16:53:01 +0800758
Leo Hsu43d670a2018-12-04 15:40:36 +0800759 placeCall(mLastNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD,
760 phoneToMakeCall);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700761 } else {
762 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
763
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
765 }
Yorke Lee9b341512014-10-17 11:36:41 -0700766 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 }
768
Leo Hsu43d670a2018-12-04 15:40:36 +0800769 private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) {
Leo Hsue3512b62019-02-14 15:53:00 +0800770 Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource)
771 + ", entry = " + entryTypeToString(mEntryType));
772
Leo Hsu43d670a2018-12-04 15:40:36 +0800773 Bundle extras = new Bundle();
774 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource);
sqian756f6062019-02-08 21:47:14 -0800775 /**
776 * This is used for Telecom and Telephony to tell modem user's intent is emergency call,
777 * when the dialed number is ambiguous and identified as both emergency number and any
778 * other non-emergency number; e.g. in some situation, 611 could be both an emergency
779 * number in a country and a non-emergency number of a carrier's customer service hotline.
780 */
781 extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
Leo Hsu43d670a2018-12-04 15:40:36 +0800782
783 if (phone != null && phone.getPhoneAccountHandle() != null) {
784 // Requests to dial through the specified phone.
785 extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
786 phone.getPhoneAccountHandle());
787 }
788
789 TelecomManager tm = this.getSystemService(TelecomManager.class);
790 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras);
791 }
792
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700793 /**
794 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
795 *
796 * The tone is played locally, using the audio stream for phone calls.
797 * Tones are played only if the "Audible touch tones" user preference
798 * is checked, and are NOT played if the device is in silent mode.
799 *
800 * @param tone a tone code from {@link ToneGenerator}
801 */
802 void playTone(int tone) {
803 // if local tone playback is disabled, just return.
804 if (!mDTMFToneEnabled) {
805 return;
806 }
807
808 // Also do nothing if the phone is in silent mode.
809 // We need to re-check the ringer mode for *every* playTone()
810 // call, rather than keeping a local flag that's updated in
811 // onResume(), since it's possible to toggle silent mode without
812 // leaving the current activity (via the ENDCALL-longpress menu.)
813 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
814 int ringerMode = audioManager.getRingerMode();
815 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
Leo Hsu233f5b32018-11-07 11:08:03 +0800816 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 return;
818 }
819
820 synchronized (mToneGeneratorLock) {
821 if (mToneGenerator == null) {
822 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
823 return;
824 }
825
826 // Start the new tone (will stop any playing tone)
827 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
828 }
829 }
830
831 private CharSequence createErrorMessage(String number) {
832 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800833 String errorString = getString(R.string.dial_emergency_error, number);
834 int startingPosition = errorString.indexOf(number);
835 int endingPosition = startingPosition + number.length();
836 Spannable result = new SpannableString(errorString);
837 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
838 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 } else {
840 return getText(R.string.dial_emergency_empty_error).toString();
841 }
842 }
843
844 @Override
845 protected Dialog onCreateDialog(int id) {
846 AlertDialog dialog = null;
847 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
848 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700849 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700850 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
851 .setMessage(createErrorMessage(mLastNumber))
852 .setPositiveButton(R.string.ok, null)
853 .setCancelable(true).create();
854
855 // blur stuff behind the dialog
856 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700857 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 }
859 return dialog;
860 }
861
862 @Override
863 protected void onPrepareDialog(int id, Dialog dialog) {
864 super.onPrepareDialog(id, dialog);
865 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
866 AlertDialog alert = (AlertDialog) dialog;
867 alert.setMessage(createErrorMessage(mLastNumber));
868 }
869 }
870
Andrew Leed5631e82014-10-08 16:03:58 -0700871 @Override
872 public boolean onOptionsItemSelected(MenuItem item) {
873 final int itemId = item.getItemId();
874 if (itemId == android.R.id.home) {
875 onBackPressed();
876 return true;
877 }
878 return super.onOptionsItemSelected(item);
879 }
880
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 /**
882 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
883 */
884 private void updateDialAndDeleteButtonStateEnabledAttr() {
885 final boolean notEmpty = mDigits.length() != 0;
886
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700887 mDelete.setEnabled(notEmpty);
888 }
Yorke Lee91311662014-10-24 14:50:45 -0700889
890 /**
891 * Remove the digit just before the current position. Used by various long pressed callbacks
892 * to remove the digit that was populated as a result of the short click.
893 */
894 private void removePreviousDigitIfPossible() {
895 final int currentPosition = mDigits.getSelectionStart();
896 if (currentPosition > 0) {
897 mDigits.setSelection(currentPosition);
898 mDigits.getText().delete(currentPosition - 1, currentPosition);
899 }
900 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800901
902 /**
903 * Update the text-to-speech annotations in the edit field.
904 */
905 private void updateTtsSpans() {
906 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
907 mDigits.getText().removeSpan(o);
908 }
909 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
910 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700911
912 @Override
Michelef6a5ea22019-10-15 16:08:54 -0700913 public void onColorsChanged(WallpaperColors colors, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700914 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Michelef6a5ea22019-10-15 16:08:54 -0700915 mBackgroundDrawable.setColor(getPrimaryColor(colors));
916 updateTheme(supportsDarkText(colors));
Lucas Dupineb9c5702017-05-10 16:57:09 -0700917 }
918 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700919
920 /**
921 * Where a carrier requires a warning that emergency calling is not available while on WFC,
922 * add hint text above the dial pad which warns the user of this case.
923 */
924 private void maybeShowWfcEmergencyCallingWarning() {
925 if (!mIsWfcEmergencyCallingWarningEnabled) {
926 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
927 return;
928 }
929
930 // Use an async task rather than calling into Telephony on UI thread.
931 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
932 @Override
933 protected Boolean doInBackground(Void... voids) {
Leo Hsu43d670a2018-12-04 15:40:36 +0800934 TelephonyManager tm = getSystemService(TelephonyManager.class);
Tyler Gunnc5428972018-03-28 14:15:34 -0700935 boolean isWfcAvailable = tm.isWifiCallingAvailable();
936 ServiceState ss = tm.getServiceState();
937 boolean isCellAvailable =
938 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
939 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
Leo Hsu233f5b32018-11-07 11:08:03 +0800940 + " isCellAvailable=" + isCellAvailable
941 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
Tyler Gunnc5428972018-03-28 14:15:34 -0700942 return isWfcAvailable && !isCellAvailable;
943 }
944
945 @Override
946 protected void onPostExecute(Boolean result) {
947 if (result.booleanValue()) {
948 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
949 mDigits.setHint(R.string.dial_emergency_calling_not_available);
950 } else {
951 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
952 mDigits.setHint("");
953 }
954 maybeChangeHintSize();
955 }
956 };
957 showWfcWarningTask.execute((Void) null);
958 }
959
960 /**
961 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
962 * edit view and set the font size to a smaller size appropriate for the emergency calling
963 * warning.
964 */
965 private void maybeChangeHintSize() {
966 if (TextUtils.isEmpty(mDigits.getHint())
967 || !TextUtils.isEmpty(mDigits.getText().toString())) {
968 // No hint or there are dialed digits, so use default size.
969 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
970 // By default, the digits view auto-resizes to fit the text it contains, so
971 // enable that now.
972 mDigits.setResizeEnabled(true);
973 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
974 } else {
975 // Hint present and no dialed digits, set custom font size appropriate for the warning.
976 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
977 R.dimen.emergency_call_warning_size));
978 // Since we're populating this with a static text string, disable auto-resize.
979 mDigits.setResizeEnabled(false);
980 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
981 }
982 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800983
984 private void setupEmergencyShortcutsView() {
985 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
986 mDialpadView = findViewById(R.id.emergency_dialer);
987
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800988 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
989 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
990
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800991 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
992 dialpadButton.setOnClickListener(this);
993
Wesley.CW Wang5e785392018-08-09 20:11:34 +0800994 mEmergencyInfoGroup.setOnConfirmClickListener(this);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800995
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800996 mEmergencyShortcutButtonList = new ArrayList<>();
997 setupEmergencyCallShortcutButton();
998
Leo Hsu43d670a2018-12-04 15:40:36 +0800999 updateLocationAndEccInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001000
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001001 switchView(mEmergencyShortcutView, mDialpadView, false);
1002 }
1003
Leo Hsu43d670a2018-12-04 15:40:36 +08001004 private void setLocationInfo() {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001005 final View locationInfo = findViewById(R.id.location_info);
1006
Leo Hsubc7553a2019-03-05 15:47:23 +08001007 String countryIso = mShortcutViewConfig.getCountryIso();
CY Cheng9a69c182018-06-15 21:20:10 +08001008 String countryName = null;
1009 if (!TextUtils.isEmpty(countryIso)) {
1010 Locale locale = Locale.getDefault();
1011 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
1012 .getDisplayCountry();
1013 }
1014 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001015 locationInfo.setVisibility(View.INVISIBLE);
1016 } else {
1017 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +08001018 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001019 locationInfo.setVisibility(View.VISIBLE);
1020 }
1021 }
1022
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001023 private void setupEmergencyCallShortcutButton() {
1024 final ViewGroup shortcutButtonContainer = findViewById(
1025 R.id.emergency_shortcut_buttons_container);
1026 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001027 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001028
CY Cheng9a69c182018-06-15 21:20:10 +08001029 mShortcutAdapter = new EccShortcutAdapter(this) {
1030 @Override
1031 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1032 CharSequence description, int iconRes) {
1033 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1034 .inflate(R.layout.emergency_shortcut_button, parent, false);
1035 button.setPhoneNumber(number);
1036 button.setPhoneDescription(description);
1037 button.setPhoneTypeIcon(iconRes);
1038 button.setOnConfirmClickListener(EmergencyDialer.this);
1039 return button;
1040 }
1041 };
1042 mShortcutDataSetObserver = new DataSetObserver() {
1043 @Override
1044 public void onChanged() {
1045 super.onChanged();
1046 updateLayout();
1047 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001048
CY Cheng9a69c182018-06-15 21:20:10 +08001049 @Override
1050 public void onInvalidated() {
1051 super.onInvalidated();
1052 updateLayout();
1053 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001054
CY Cheng9a69c182018-06-15 21:20:10 +08001055 private void updateLayout() {
1056 // clear previous added buttons
1057 shortcutButtonContainer.removeAllViews();
1058 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001059
CY Cheng9a69c182018-06-15 21:20:10 +08001060 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1061 EmergencyShortcutButton button = (EmergencyShortcutButton)
1062 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1063 mEmergencyShortcutButtonList.add(button);
1064 shortcutButtonContainer.addView(button);
1065 }
1066
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001067 // Update emergency numbers title for numerous buttons.
CY Cheng9a69c182018-06-15 21:20:10 +08001068 if (mEmergencyShortcutButtonList.size() > 1) {
1069 emergencyNumberTitle.setText(getString(
1070 R.string.numerous_emergency_numbers_title));
1071 } else {
1072 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1073 }
1074 }
1075 };
1076 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1077 }
1078
Leo Hsu43d670a2018-12-04 15:40:36 +08001079 private void updateLocationAndEccInfo() {
CY Cheng9a69c182018-06-15 21:20:10 +08001080 if (!isFinishing() && !isDestroyed()) {
Leo Hsu43d670a2018-12-04 15:40:36 +08001081 setLocationInfo();
CY Cheng9a69c182018-06-15 21:20:10 +08001082 if (mShortcutAdapter != null) {
Leo Hsubc7553a2019-03-05 15:47:23 +08001083 mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
CY Cheng9a69c182018-06-15 21:20:10 +08001084 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001085 }
1086 }
1087
1088 /**
1089 * Called by the activity before a touch event is dispatched to the view hierarchy.
1090 */
1091 private void onPreTouchEvent(MotionEvent event) {
1092 mEmergencyActionGroup.onPreTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001093 mEmergencyInfoGroup.onPreTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001094
1095 if (mEmergencyShortcutButtonList != null) {
1096 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1097 button.onPreTouchEvent(event);
1098 }
1099 }
1100 }
1101
1102 /**
1103 * Called by the activity after a touch event is dispatched to the view hierarchy.
1104 */
1105 private void onPostTouchEvent(MotionEvent event) {
1106 mEmergencyActionGroup.onPostTouchEvent(event);
Wesley.CW Wang5e785392018-08-09 20:11:34 +08001107 mEmergencyInfoGroup.onPostTouchEvent(event);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001108
1109 if (mEmergencyShortcutButtonList != null) {
1110 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1111 button.onPostTouchEvent(event);
1112 }
1113 }
1114 }
1115
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001116 /**
1117 * Switch two view.
1118 *
Leo Hsu83ab2332018-12-18 15:20:45 +08001119 * @param displayView the view would be displayed.
1120 * @param hideView the view would be hidden.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001121 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1122 */
1123 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1124 if (displayView == null || hideView == null) {
1125 return;
1126 }
1127
1128 if (displayView.getVisibility() == View.VISIBLE) {
1129 return;
1130 }
1131
1132 if (hasAnimation) {
1133 crossfade(hideView, displayView);
1134 } else {
1135 hideView.setVisibility(View.GONE);
1136 displayView.setVisibility(View.VISIBLE);
1137 }
1138 }
1139
1140 /**
1141 * Fade out and fade in animation between two view transition.
1142 */
1143 private void crossfade(View fadeOutView, View fadeInView) {
1144 if (fadeOutView == null || fadeInView == null) {
1145 return;
1146 }
1147 final int shortAnimationDuration = getResources().getInteger(
1148 android.R.integer.config_shortAnimTime);
1149
1150 fadeInView.setAlpha(0f);
1151 fadeInView.setVisibility(View.VISIBLE);
1152
1153 fadeInView.animate()
1154 .alpha(1f)
1155 .setDuration(shortAnimationDuration)
1156 .setListener(null);
1157
1158 fadeOutView.animate()
1159 .alpha(0f)
1160 .setDuration(shortAnimationDuration)
1161 .setListener(new AnimatorListenerAdapter() {
1162 @Override
1163 public void onAnimationEnd(Animator animation) {
1164 fadeOutView.setVisibility(View.GONE);
1165 }
1166 });
1167 }
Shaotang Li8662a912018-07-04 16:53:01 +08001168
Shaotang Li8662a912018-07-04 16:53:01 +08001169 private boolean isShortcutNumber(String number) {
1170 if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) {
1171 return false;
1172 }
1173
1174 boolean isShortcut = false;
1175 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1176 if (button != null && number.equals(button.getPhoneNumber())) {
1177 isShortcut = true;
1178 break;
1179 }
1180 }
1181 return isShortcut;
1182 }
Leo Hsue3512b62019-02-14 15:53:00 +08001183
1184 private String entryTypeToString(int entryType) {
1185 switch (entryType) {
1186 case ENTRY_TYPE_LOCKSCREEN_BUTTON:
1187 return "LockScreen";
1188 case ENTRY_TYPE_POWER_MENU:
1189 return "PowerMenu";
1190 default:
1191 return "Unknown-" + entryType;
1192 }
1193 }
1194
1195 private String callSourceToString(int callSource) {
1196 switch (callSource) {
1197 case ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD:
1198 return "DialPad";
1199 case ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT:
1200 return "Shortcut";
1201 default:
1202 return "Unknown-" + callSource;
1203 }
1204 }
Michelef6a5ea22019-10-15 16:08:54 -07001205
1206 private WallpaperManager getWallpaperManager() {
1207 return getSystemService(WallpaperManager.class);
1208 }
1209
1210 private static boolean supportsDarkText(WallpaperColors colors) {
1211 if (colors != null) {
1212 return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
1213 }
1214 // It's possible that wallpaper colors are null (e.g. when colors are being
1215 // processed or a live wallpaper is used). In this case, fallback to same
1216 // behavior as when shortcut view is enabled.
1217 return false;
1218 }
1219
1220 private static int getPrimaryColor(WallpaperColors colors) {
1221 if (colors != null) {
1222 return colors.getPrimaryColor().toArgb();
1223 }
1224 // It's possible that wallpaper colors are null (e.g. when colors are being
1225 // processed or a live wallpaper is used). In this case, fallback to same
1226 // behavior as when shortcut view is enabled.
1227 return Color.BLACK;
1228 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001229}