Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 18 | |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 19 | import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN; |
| 20 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 23 | import android.annotation.ColorInt; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 24 | import android.app.Activity; |
| 25 | import android.app.AlertDialog; |
| 26 | import android.app.Dialog; |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 27 | import android.app.WallpaperColors; |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 28 | import android.app.WallpaperManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
| 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
| 32 | import android.content.IntentFilter; |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 33 | import android.database.DataSetObserver; |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 34 | import android.graphics.Color; |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 35 | import android.graphics.Point; |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 36 | import android.graphics.drawable.ColorDrawable; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 37 | import android.media.AudioManager; |
| 38 | import android.media.ToneGenerator; |
| 39 | import android.net.Uri; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 40 | import android.os.AsyncTask; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 41 | import android.os.Bundle; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 42 | import android.os.PersistableBundle; |
Jack Yu | 58d04bd | 2024-09-10 17:32:39 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 44 | import android.provider.Settings; |
Tyler Gunn | 4d45d1c | 2014-09-12 22:17:53 -0700 | [diff] [blame] | 45 | import android.telecom.PhoneAccount; |
Tyler Gunn | fa77e20 | 2018-03-23 07:47:00 -0700 | [diff] [blame] | 46 | import android.telecom.TelecomManager; |
Jonathan Basseri | 3649bdb | 2015-04-30 22:39:11 -0700 | [diff] [blame] | 47 | import android.telephony.CarrierConfigManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 48 | import android.telephony.PhoneNumberUtils; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 49 | import android.telephony.ServiceState; |
Santos Cordon | e137eed | 2015-06-23 15:34:47 -0700 | [diff] [blame] | 50 | import android.telephony.SubscriptionManager; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 51 | import android.telephony.TelephonyManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 52 | import android.text.Editable; |
Hall Liu | bdc9c88 | 2016-05-25 15:25:28 -0700 | [diff] [blame] | 53 | import android.text.InputType; |
Hall Liu | dc27431 | 2016-03-01 16:34:45 -0800 | [diff] [blame] | 54 | import android.text.Spannable; |
| 55 | import android.text.SpannableString; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 56 | import android.text.TextUtils; |
| 57 | import android.text.TextWatcher; |
| 58 | import android.text.method.DialerKeyListener; |
Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 59 | import android.text.style.TtsSpan; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 60 | import android.util.Log; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 61 | import android.util.TypedValue; |
Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 62 | import android.view.HapticFeedbackConstants; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 63 | import android.view.KeyEvent; |
Andrew Lee | d5631e8 | 2014-10-08 16:03:58 -0700 | [diff] [blame] | 64 | import android.view.MenuItem; |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 65 | import android.view.MotionEvent; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 66 | import android.view.View; |
Chihhang Chuang | caba0da | 2018-08-02 22:25:06 +0800 | [diff] [blame] | 67 | import android.view.View.AccessibilityDelegate; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 68 | import android.view.ViewGroup; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 69 | import android.view.WindowManager; |
Chihhang Chuang | caba0da | 2018-08-02 22:25:06 +0800 | [diff] [blame] | 70 | import android.view.accessibility.AccessibilityEvent; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 71 | import android.widget.TextView; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 72 | |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 73 | import com.android.phone.common.dialpad.DialpadKeyButton; |
Sai Cheemalapati | 14462b6 | 2014-06-18 13:53:56 -0700 | [diff] [blame] | 74 | import com.android.phone.common.util.ViewUtil; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 75 | import com.android.phone.common.widget.ResizingTextEditText; |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 76 | import com.android.telephony.Rlog; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 77 | |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 78 | import java.util.ArrayList; |
| 79 | import java.util.List; |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 80 | import java.util.Locale; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 81 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 82 | /** |
| 83 | * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls. |
| 84 | * |
| 85 | * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer |
| 86 | * activity from apps/Contacts) that: |
Leo Hsu | 83ab233 | 2018-12-18 15:20:45 +0800 | [diff] [blame] | 87 | * 1. Allows ONLY emergency calls to be dialed |
| 88 | * 2. Disallows voicemail functionality |
| 89 | * 3. Allows this activity to stay in front of the keyguard. |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 90 | * |
| 91 | * TODO: Even though this is an ultra-simplified version of the normal |
| 92 | * dialer, there's still lots of code duplication between this class and |
| 93 | * the TwelveKeyDialer class from apps/Contacts. Could the common code be |
| 94 | * moved into a shared base class that would live in the framework? |
| 95 | * Or could we figure out some way to move *this* class into apps/Contacts |
| 96 | * also? |
| 97 | */ |
| 98 | public class EmergencyDialer extends Activity implements View.OnClickListener, |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 99 | View.OnLongClickListener, View.OnKeyListener, TextWatcher, |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 100 | DialpadKeyButton.OnPressedListener, |
| 101 | WallpaperManager.OnColorsChangedListener, |
Leo Hsu | 7bcfb8e | 2019-05-03 21:54:45 +0800 | [diff] [blame] | 102 | EmergencyShortcutButton.OnConfirmClickListener, |
Wesley.CW Wang | 5e78539 | 2018-08-09 20:11:34 +0800 | [diff] [blame] | 103 | EmergencyInfoGroup.OnConfirmClickListener { |
Shaotang Li | 8662a91 | 2018-07-04 16:53:01 +0800 | [diff] [blame] | 104 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 105 | // Keys used with onSaveInstanceState(). |
| 106 | private static final String LAST_NUMBER = "lastNumber"; |
| 107 | |
| 108 | // Intent action for this activity. |
| 109 | public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL"; |
| 110 | |
Shaotang Li | 8662a91 | 2018-07-04 16:53:01 +0800 | [diff] [blame] | 111 | /** |
| 112 | * Extra included in {@link #ACTION_DIAL} to indicate the entry type that user starts |
| 113 | * the emergency dialer. |
| 114 | */ |
| 115 | public static final String EXTRA_ENTRY_TYPE = |
| 116 | "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE"; |
| 117 | |
Leo Hsu | e3512b6 | 2019-02-14 15:53:00 +0800 | [diff] [blame] | 118 | // Constants indicating the entry type that user opened emergency dialer. |
| 119 | // This info is sent from system UI with EXTRA_ENTRY_TYPE. Please make them being |
| 120 | // in sync with those in com.android.systemui.util.EmergencyDialerConstants. |
| 121 | public static final int ENTRY_TYPE_UNKNOWN = 0; |
| 122 | public static final int ENTRY_TYPE_LOCKSCREEN_BUTTON = 1; |
| 123 | public static final int ENTRY_TYPE_POWER_MENU = 2; |
| 124 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 125 | // List of dialer button IDs. |
Leo Hsu | 83ab233 | 2018-12-18 15:20:45 +0800 | [diff] [blame] | 126 | private static final int[] DIALER_KEYS = new int[]{ |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 127 | R.id.one, R.id.two, R.id.three, |
| 128 | R.id.four, R.id.five, R.id.six, |
| 129 | R.id.seven, R.id.eight, R.id.nine, |
Leo Hsu | 83ab233 | 2018-12-18 15:20:45 +0800 | [diff] [blame] | 130 | R.id.star, R.id.zero, R.id.pound}; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 131 | |
| 132 | // Debug constants. |
| 133 | private static final boolean DBG = false; |
| 134 | private static final String LOG_TAG = "EmergencyDialer"; |
| 135 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 136 | /** The length of DTMF tones in milliseconds */ |
| 137 | private static final int TONE_LENGTH_MS = 150; |
| 138 | |
| 139 | /** The DTMF tone volume relative to other sounds in the stream */ |
| 140 | private static final int TONE_RELATIVE_VOLUME = 80; |
| 141 | |
| 142 | /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */ |
| 143 | private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF; |
| 144 | |
| 145 | private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0; |
| 146 | |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 147 | /** 90% opacity, different from other gradients **/ |
| 148 | private static final int BACKGROUND_GRADIENT_ALPHA = 230; |
| 149 | |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 150 | /** 85% opacity for black background **/ |
| 151 | private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217; |
| 152 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 153 | /** Size limit of emergency shortcut buttons container. **/ |
| 154 | private static final int SHORTCUT_SIZE_LIMIT = 3; |
| 155 | |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 156 | private static final float COLOR_DELTA = 1.0f / 16.0f; |
| 157 | |
| 158 | /** Dial button color, from packages/apps/PhoneCommon/res/drawable-mdpi/fab_green.png **/ |
| 159 | @ColorInt private static final int DIALER_GREEN = 0xff00c853; |
| 160 | |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 161 | ResizingTextEditText mDigits; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 162 | private View mDialButton; |
| 163 | private View mDelete; |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 164 | private View mEmergencyShortcutView; |
| 165 | private View mDialpadView; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 166 | |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 167 | private List<EmergencyShortcutButton> mEmergencyShortcutButtonList; |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 168 | private EccShortcutAdapter mShortcutAdapter; |
| 169 | private DataSetObserver mShortcutDataSetObserver = null; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 170 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 171 | private ToneGenerator mToneGenerator; |
| 172 | private Object mToneGeneratorLock = new Object(); |
| 173 | |
| 174 | // determines if we want to playback local DTMF tones. |
| 175 | private boolean mDTMFToneEnabled; |
| 176 | |
Fan Zhang | 39d81f5 | 2022-03-21 22:47:21 +0000 | [diff] [blame] | 177 | private EmergencyInfoGroup mEmergencyInfoInDialpad; |
| 178 | private EmergencyInfoGroup mEmergencyInfoInShortcut; |
Wesley.CW Wang | 5e78539 | 2018-08-09 20:11:34 +0800 | [diff] [blame] | 179 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 180 | // close activity when screen turns off |
| 181 | private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 182 | @Override |
| 183 | public void onReceive(Context context, Intent intent) { |
| 184 | if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { |
Adrian Roos | 061c723 | 2015-04-21 12:37:07 -0700 | [diff] [blame] | 185 | finishAndRemoveTask(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | }; |
| 189 | |
Chihhang Chuang | caba0da | 2018-08-02 22:25:06 +0800 | [diff] [blame] | 190 | /** |
| 191 | * Customize accessibility methods in View. |
| 192 | */ |
| 193 | private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() { |
| 194 | |
| 195 | /** |
| 196 | * Stop AccessiblityService from reading the title of a hidden View. |
| 197 | * |
| 198 | * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE} |
| 199 | * in the animation end. The view with an accessibility pane title would call the |
| 200 | * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the |
| 201 | * accessibility service to read the pane title of fade out view instead of pane title of |
| 202 | * fade in view. So it need to filter out the event called by vanished pane. |
| 203 | */ |
| 204 | @Override |
| 205 | public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) { |
| 206 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED |
| 207 | && host.getVisibility() == View.GONE) { |
| 208 | return; |
| 209 | } |
| 210 | super.onPopulateAccessibilityEvent(host, event); |
| 211 | } |
| 212 | }; |
| 213 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 214 | private String mLastNumber; // last number we tried to dial. Used to restore error dialog. |
| 215 | |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 216 | // Background gradient |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 217 | private ColorDrawable mBackgroundDrawable; |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 218 | private boolean mSupportsDarkText; |
| 219 | |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 220 | private boolean mIsWfcEmergencyCallingWarningEnabled; |
| 221 | private float mDefaultDigitsTextSize; |
| 222 | |
Leo Hsu | e3512b6 | 2019-02-14 15:53:00 +0800 | [diff] [blame] | 223 | private int mEntryType; |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 224 | private ShortcutViewUtils.Config mShortcutViewConfig; |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 225 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 226 | @Override |
| 227 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 228 | // Do nothing |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void onTextChanged(CharSequence input, int start, int before, int changeCount) { |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 233 | maybeChangeHintSize(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void afterTextChanged(Editable input) { |
| 238 | // Check for special sequences, in particular the "**04" or "**05" |
| 239 | // sequences that allow you to enter PIN or PUK-related codes. |
| 240 | // |
| 241 | // But note we *don't* allow most other special sequences here, |
| 242 | // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"), |
| 243 | // since those shouldn't be available if the device is locked. |
| 244 | // |
| 245 | // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice() |
| 246 | // here, not the regular handleChars() method. |
| 247 | if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) { |
| 248 | // A special sequence was entered, clear the digits |
| 249 | mDigits.getText().clear(); |
| 250 | } |
| 251 | |
| 252 | updateDialAndDeleteButtonStateEnabledAttr(); |
Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 253 | updateTtsSpans(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | protected void onCreate(Bundle icicle) { |
| 258 | super.onCreate(icicle); |
| 259 | |
Leo Hsu | e3512b6 | 2019-02-14 15:53:00 +0800 | [diff] [blame] | 260 | mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN); |
| 261 | Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType)); |
| 262 | |
Mengjun Leng | b9d14f0 | 2017-10-31 14:28:14 +0800 | [diff] [blame] | 263 | // Allow turning screen on |
| 264 | setTurnScreenOn(true); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 265 | |
Chuck Liao | cdeebb4 | 2018-10-30 12:07:18 +0800 | [diff] [blame] | 266 | CarrierConfigManager configMgr = getSystemService(CarrierConfigManager.class); |
Tomasz Wasilczyk | 8919d70 | 2024-04-01 10:58:20 -0700 | [diff] [blame] | 267 | PersistableBundle carrierConfig = configMgr == null ? null : |
Chuck Liao | cdeebb4 | 2018-10-30 12:07:18 +0800 | [diff] [blame] | 268 | configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 269 | |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 270 | mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 271 | Log.d(LOG_TAG, "Enable emergency dialer shortcut: " |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 272 | + mShortcutViewConfig.isEnabled()); |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 273 | |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 274 | if (mShortcutViewConfig.isEnabled()) { |
Leo Hsu | 83ab233 | 2018-12-18 15:20:45 +0800 | [diff] [blame] | 275 | // Shortcut view doesn't support dark text theme. |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 276 | updateTheme(false); |
| 277 | } else { |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 278 | WallpaperColors wallpaperColors = |
| 279 | getWallpaperManager().getWallpaperColors(WallpaperManager.FLAG_LOCK); |
| 280 | updateTheme(supportsDarkText(wallpaperColors)); |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 281 | } |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 282 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 283 | setContentView(R.layout.emergency_dialer); |
| 284 | |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 285 | mDigits = (ResizingTextEditText) findViewById(R.id.digits); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 286 | mDigits.setKeyListener(DialerKeyListener.getInstance()); |
| 287 | mDigits.setOnClickListener(this); |
| 288 | mDigits.setOnKeyListener(this); |
| 289 | mDigits.setLongClickable(false); |
Hall Liu | bdc9c88 | 2016-05-25 15:25:28 -0700 | [diff] [blame] | 290 | mDigits.setInputType(InputType.TYPE_NULL); |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 291 | mDefaultDigitsTextSize = mDigits.getScaledTextSize(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 292 | maybeAddNumberFormatting(); |
| 293 | |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 294 | mBackgroundDrawable = new ColorDrawable(); |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 295 | Point displaySize = new Point(); |
| 296 | ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) |
| 297 | .getDefaultDisplay().getSize(displaySize); |
Lucas Dupin | 4946f6f | 2019-04-09 15:03:19 -0700 | [diff] [blame] | 298 | mBackgroundDrawable.setAlpha(mShortcutViewConfig.isEnabled() |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 299 | ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA); |
Lucas Dupin | 4946f6f | 2019-04-09 15:03:19 -0700 | [diff] [blame] | 300 | getWindow().setBackgroundDrawable(mBackgroundDrawable); |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 301 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 302 | // Check for the presence of the keypad |
| 303 | View view = findViewById(R.id.one); |
| 304 | if (view != null) { |
| 305 | setupKeypad(); |
| 306 | } |
| 307 | |
| 308 | mDelete = findViewById(R.id.deleteButton); |
| 309 | mDelete.setOnClickListener(this); |
| 310 | mDelete.setOnLongClickListener(this); |
| 311 | |
Sai Cheemalapati | 14462b6 | 2014-06-18 13:53:56 -0700 | [diff] [blame] | 312 | mDialButton = findViewById(R.id.floating_action_button); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 313 | |
| 314 | // Check whether we should show the onscreen "Dial" button and co. |
Jonathan Basseri | e619a4c | 2015-06-26 14:52:26 -0700 | [diff] [blame] | 315 | // Read carrier config through the public API because PhoneGlobals is not available when we |
| 316 | // run as a secondary user. |
Tomasz Wasilczyk | 8919d70 | 2024-04-01 10:58:20 -0700 | [diff] [blame] | 317 | if (carrierConfig != null |
| 318 | && carrierConfig.getBoolean( |
| 319 | CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 320 | mDialButton.setOnClickListener(this); |
| 321 | } else { |
| 322 | mDialButton.setVisibility(View.GONE); |
| 323 | } |
Tomasz Wasilczyk | 8919d70 | 2024-04-01 10:58:20 -0700 | [diff] [blame] | 324 | mIsWfcEmergencyCallingWarningEnabled = carrierConfig != null && carrierConfig.getInt( |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 325 | CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1; |
| 326 | maybeShowWfcEmergencyCallingWarning(); |
| 327 | |
Adrian Roos | c9fdb6c | 2015-05-25 15:10:21 -0700 | [diff] [blame] | 328 | ViewUtil.setupFloatingActionButton(mDialButton, getResources()); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 329 | |
| 330 | if (icicle != null) { |
| 331 | super.onRestoreInstanceState(icicle); |
| 332 | } |
| 333 | |
| 334 | // Extract phone number from intent |
| 335 | Uri data = getIntent().getData(); |
Jay Shrauner | 137458b | 2014-09-05 14:27:25 -0700 | [diff] [blame] | 336 | if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 337 | String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this); |
| 338 | if (number != null) { |
| 339 | mDigits.setText(number); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // if the mToneGenerator creation fails, just continue without it. It is |
| 344 | // a local audio signal, and is not as important as the dtmf tone itself. |
| 345 | synchronized (mToneGeneratorLock) { |
| 346 | if (mToneGenerator == null) { |
| 347 | try { |
| 348 | mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME); |
| 349 | } catch (RuntimeException e) { |
| 350 | Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e); |
| 351 | mToneGenerator = null; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | final IntentFilter intentFilter = new IntentFilter(); |
| 357 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 358 | registerReceiver(mBroadcastReceiver, intentFilter); |
| 359 | |
Fan Zhang | 39d81f5 | 2022-03-21 22:47:21 +0000 | [diff] [blame] | 360 | mEmergencyInfoInDialpad = findViewById(R.id.emergency_dialer) |
| 361 | .findViewById(R.id.emergency_info_button); |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 362 | |
Fan Zhang | 39d81f5 | 2022-03-21 22:47:21 +0000 | [diff] [blame] | 363 | mEmergencyInfoInShortcut = findViewById(R.id.emergency_dialer_shortcuts) |
| 364 | .findViewById(R.id.emergency_info_button); |
| 365 | |
| 366 | setupEmergencyDialpadViews(); |
Wesley.CW Wang | 5e78539 | 2018-08-09 20:11:34 +0800 | [diff] [blame] | 367 | |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 368 | if (mShortcutViewConfig.isEnabled()) { |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 369 | setupEmergencyShortcutsView(); |
| 370 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
| 374 | protected void onDestroy() { |
| 375 | super.onDestroy(); |
| 376 | synchronized (mToneGeneratorLock) { |
| 377 | if (mToneGenerator != null) { |
| 378 | mToneGenerator.release(); |
| 379 | mToneGenerator = null; |
| 380 | } |
| 381 | } |
| 382 | unregisterReceiver(mBroadcastReceiver); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 383 | if (mShortcutAdapter != null && mShortcutDataSetObserver != null) { |
| 384 | mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver); |
| 385 | mShortcutDataSetObserver = null; |
| 386 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | @Override |
| 390 | protected void onRestoreInstanceState(Bundle icicle) { |
| 391 | mLastNumber = icicle.getString(LAST_NUMBER); |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | protected void onSaveInstanceState(Bundle outState) { |
| 396 | super.onSaveInstanceState(outState); |
| 397 | outState.putString(LAST_NUMBER, mLastNumber); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Explicitly turn off number formatting, since it gets in the way of the emergency |
| 402 | * number detector |
| 403 | */ |
| 404 | protected void maybeAddNumberFormatting() { |
| 405 | // Do nothing. |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | protected void onPostCreate(Bundle savedInstanceState) { |
| 410 | super.onPostCreate(savedInstanceState); |
| 411 | |
| 412 | // This can't be done in onCreate(), since the auto-restoring of the digits |
| 413 | // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState() |
| 414 | // is called. This method will be called every time the activity is created, and |
| 415 | // will always happen after onRestoreSavedInstanceState(). |
| 416 | mDigits.addTextChangedListener(this); |
| 417 | } |
| 418 | |
| 419 | private void setupKeypad() { |
| 420 | // Setup the listeners for the buttons |
| 421 | for (int id : DIALER_KEYS) { |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 422 | final DialpadKeyButton key = (DialpadKeyButton) findViewById(id); |
| 423 | key.setOnPressedListener(this); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | View view = findViewById(R.id.zero); |
| 427 | view.setOnLongClickListener(this); |
| 428 | } |
| 429 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 430 | @Override |
| 431 | public void onBackPressed() { |
Leo Hsu | 83ab233 | 2018-12-18 15:20:45 +0800 | [diff] [blame] | 432 | // If shortcut view is enabled and Dialpad view is visible, pressing the back key will |
| 433 | // back to display EmergencyShortcutView view. Otherwise, it would finish the activity. |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 434 | if (mShortcutViewConfig.isEnabled() && mDialpadView != null |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 435 | && mDialpadView.getVisibility() == View.VISIBLE) { |
| 436 | switchView(mEmergencyShortcutView, mDialpadView, true); |
| 437 | return; |
| 438 | } |
| 439 | super.onBackPressed(); |
| 440 | } |
| 441 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 442 | /** |
| 443 | * handle key events |
| 444 | */ |
| 445 | @Override |
| 446 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 447 | switch (keyCode) { |
| 448 | // Happen when there's a "Call" hard button. |
| 449 | case KeyEvent.KEYCODE_CALL: { |
| 450 | if (TextUtils.isEmpty(mDigits.getText().toString())) { |
| 451 | // if we are adding a call from the InCallScreen and the phone |
| 452 | // number entered is empty, we just close the dialer to expose |
| 453 | // the InCallScreen under it. |
| 454 | finish(); |
| 455 | } else { |
| 456 | // otherwise, we place the call. |
| 457 | placeCall(); |
| 458 | } |
| 459 | return true; |
| 460 | } |
| 461 | } |
| 462 | return super.onKeyDown(keyCode, event); |
| 463 | } |
| 464 | |
| 465 | private void keyPressed(int keyCode) { |
Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 466 | mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 467 | KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); |
| 468 | mDigits.onKeyDown(keyCode, event); |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public boolean onKey(View view, int keyCode, KeyEvent event) { |
Amit Mahajan | 28a499c | 2019-11-20 15:13:42 -0800 | [diff] [blame] | 473 | if (view.getId() |
| 474 | == R.id.digits) { // Happen when "Done" button of the IME is pressed. This can |
| 475 | // happen when this |
| 476 | // Activity is forced into landscape mode due to a desk dock. |
| 477 | if (keyCode == KeyEvent.KEYCODE_ENTER |
| 478 | && event.getAction() == KeyEvent.ACTION_UP) { |
| 479 | placeCall(); |
| 480 | return true; |
| 481 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | @Override |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 487 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 488 | onPreTouchEvent(ev); |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 489 | boolean handled = super.dispatchTouchEvent(ev); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 490 | onPostTouchEvent(ev); |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 491 | return handled; |
| 492 | } |
| 493 | |
| 494 | @Override |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 495 | public void onConfirmClick(EmergencyShortcutButton button) { |
| 496 | if (button == null) return; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 497 | String phoneNumber = button.getPhoneNumber(); |
| 498 | |
| 499 | if (!TextUtils.isEmpty(phoneNumber)) { |
Billy Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 500 | if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber)); |
Leo Hsu | 7bcfb8e | 2019-05-03 21:54:45 +0800 | [diff] [blame] | 501 | |
Hall Liu | 6f35de9 | 2020-01-23 14:29:26 -0800 | [diff] [blame] | 502 | placeCall(phoneNumber, TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT, |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 503 | mShortcutViewConfig.getPhoneInfo()); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 504 | } else { |
| 505 | Log.d(LOG_TAG, "emergency number is empty"); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | @Override |
Wesley.CW Wang | 5e78539 | 2018-08-09 20:11:34 +0800 | [diff] [blame] | 510 | public void onConfirmClick(EmergencyInfoGroup button) { |
| 511 | if (button == null) return; |
| 512 | |
Wesley.CW Wang | 5e78539 | 2018-08-09 20:11:34 +0800 | [diff] [blame] | 513 | Intent intent = (Intent) button.getTag(R.id.tag_intent); |
| 514 | if (intent != null) { |
Jack Yu | 58d04bd | 2024-09-10 17:32:39 -0700 | [diff] [blame] | 515 | startActivityAsUser(intent, UserHandle.CURRENT); |
Wesley.CW Wang | 5e78539 | 2018-08-09 20:11:34 +0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | @Override |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 520 | public void onClick(View view) { |
Amit Mahajan | 28a499c | 2019-11-20 15:13:42 -0800 | [diff] [blame] | 521 | if (view.getId() == R.id.deleteButton) { |
| 522 | keyPressed(KeyEvent.KEYCODE_DEL); |
| 523 | return; |
| 524 | } else if (view.getId() == R.id.floating_action_button) { |
| 525 | view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
| 526 | placeCall(); |
| 527 | return; |
| 528 | } else if (view.getId() == R.id.digits) { |
| 529 | if (mDigits.length() != 0) { |
| 530 | mDigits.setCursorVisible(true); |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 531 | } |
Amit Mahajan | 28a499c | 2019-11-20 15:13:42 -0800 | [diff] [blame] | 532 | return; |
| 533 | } else if (view.getId() == R.id.floating_action_button_dialpad) { |
| 534 | mDigits.getText().clear(); |
| 535 | switchView(mDialpadView, mEmergencyShortcutView, true); |
| 536 | return; |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | @Override |
| 541 | public void onPressed(View view, boolean pressed) { |
| 542 | if (!pressed) { |
| 543 | return; |
| 544 | } |
Amit Mahajan | 28a499c | 2019-11-20 15:13:42 -0800 | [diff] [blame] | 545 | if (view.getId() == R.id.one) { |
| 546 | playTone(ToneGenerator.TONE_DTMF_1); |
| 547 | keyPressed(KeyEvent.KEYCODE_1); |
| 548 | return; |
| 549 | } else if (view.getId() == R.id.two) { |
| 550 | playTone(ToneGenerator.TONE_DTMF_2); |
| 551 | keyPressed(KeyEvent.KEYCODE_2); |
| 552 | return; |
| 553 | } else if (view.getId() == R.id.three) { |
| 554 | playTone(ToneGenerator.TONE_DTMF_3); |
| 555 | keyPressed(KeyEvent.KEYCODE_3); |
| 556 | return; |
| 557 | } else if (view.getId() == R.id.four) { |
| 558 | playTone(ToneGenerator.TONE_DTMF_4); |
| 559 | keyPressed(KeyEvent.KEYCODE_4); |
| 560 | return; |
| 561 | } else if (view.getId() == R.id.five) { |
| 562 | playTone(ToneGenerator.TONE_DTMF_5); |
| 563 | keyPressed(KeyEvent.KEYCODE_5); |
| 564 | return; |
| 565 | } else if (view.getId() == R.id.six) { |
| 566 | playTone(ToneGenerator.TONE_DTMF_6); |
| 567 | keyPressed(KeyEvent.KEYCODE_6); |
| 568 | return; |
| 569 | } else if (view.getId() == R.id.seven) { |
| 570 | playTone(ToneGenerator.TONE_DTMF_7); |
| 571 | keyPressed(KeyEvent.KEYCODE_7); |
| 572 | return; |
| 573 | } else if (view.getId() == R.id.eight) { |
| 574 | playTone(ToneGenerator.TONE_DTMF_8); |
| 575 | keyPressed(KeyEvent.KEYCODE_8); |
| 576 | return; |
| 577 | } else if (view.getId() == R.id.nine) { |
| 578 | playTone(ToneGenerator.TONE_DTMF_9); |
| 579 | keyPressed(KeyEvent.KEYCODE_9); |
| 580 | return; |
| 581 | } else if (view.getId() == R.id.zero) { |
| 582 | playTone(ToneGenerator.TONE_DTMF_0); |
| 583 | keyPressed(KeyEvent.KEYCODE_0); |
| 584 | return; |
| 585 | } else if (view.getId() == R.id.pound) { |
| 586 | playTone(ToneGenerator.TONE_DTMF_P); |
| 587 | keyPressed(KeyEvent.KEYCODE_POUND); |
| 588 | return; |
| 589 | } else if (view.getId() == R.id.star) { |
| 590 | playTone(ToneGenerator.TONE_DTMF_S); |
| 591 | keyPressed(KeyEvent.KEYCODE_STAR); |
| 592 | return; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 597 | * called for long touch events |
| 598 | */ |
| 599 | @Override |
| 600 | public boolean onLongClick(View view) { |
| 601 | int id = view.getId(); |
Amit Mahajan | 28a499c | 2019-11-20 15:13:42 -0800 | [diff] [blame] | 602 | if (id == R.id.deleteButton) { |
| 603 | mDigits.getText().clear(); |
| 604 | return true; |
| 605 | } else if (id == R.id.zero) { |
| 606 | removePreviousDigitIfPossible(); |
| 607 | keyPressed(KeyEvent.KEYCODE_PLUS); |
| 608 | return true; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 609 | } |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | @Override |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 614 | protected void onStart() { |
| 615 | super.onStart(); |
Shaotang Li | 8662a91 | 2018-07-04 16:53:01 +0800 | [diff] [blame] | 616 | |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 617 | if (mShortcutViewConfig.isEnabled()) { |
Leo Hsu | 83ab233 | 2018-12-18 15:20:45 +0800 | [diff] [blame] | 618 | // Shortcut view doesn't support dark text theme. |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 619 | mBackgroundDrawable.setColor(Color.BLACK); |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 620 | updateTheme(false); |
| 621 | } else { |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 622 | WallpaperManager wallpaperManager = getWallpaperManager(); |
| 623 | if (wallpaperManager.isWallpaperSupported()) { |
| 624 | wallpaperManager.addOnColorsChangedListener(this, null); |
| 625 | } |
| 626 | |
| 627 | WallpaperColors wallpaperColors = |
| 628 | wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK); |
| 629 | mBackgroundDrawable.setColor(getPrimaryColor(wallpaperColors)); |
| 630 | updateTheme(supportsDarkText(wallpaperColors)); |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 631 | } |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 632 | |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 633 | if (mShortcutViewConfig.isEnabled()) { |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 634 | updateLocationAndEccInfo(); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 635 | } |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | @Override |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 639 | protected void onResume() { |
| 640 | super.onResume(); |
| 641 | |
| 642 | // retrieve the DTMF tone play back setting. |
| 643 | mDTMFToneEnabled = Settings.System.getInt(getContentResolver(), |
| 644 | Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1; |
| 645 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 646 | // if the mToneGenerator creation fails, just continue without it. It is |
| 647 | // a local audio signal, and is not as important as the dtmf tone itself. |
| 648 | synchronized (mToneGeneratorLock) { |
| 649 | if (mToneGenerator == null) { |
| 650 | try { |
| 651 | mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF, |
| 652 | TONE_RELATIVE_VOLUME); |
| 653 | } catch (RuntimeException e) { |
| 654 | Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e); |
| 655 | mToneGenerator = null; |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 660 | updateDialAndDeleteButtonStateEnabledAttr(); |
| 661 | } |
| 662 | |
| 663 | @Override |
| 664 | public void onPause() { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 665 | super.onPause(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 668 | @Override |
| 669 | protected void onStop() { |
| 670 | super.onStop(); |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 671 | |
| 672 | WallpaperManager wallpaperManager = getWallpaperManager(); |
| 673 | if (wallpaperManager.isWallpaperSupported()) { |
| 674 | wallpaperManager.removeOnColorsChangedListener(this); |
| 675 | } |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Sets theme based on gradient colors |
Leo Hsu | 83ab233 | 2018-12-18 15:20:45 +0800 | [diff] [blame] | 680 | * |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 681 | * @param supportsDarkText true if gradient supports dark text |
| 682 | */ |
| 683 | private void updateTheme(boolean supportsDarkText) { |
| 684 | if (mSupportsDarkText == supportsDarkText) { |
| 685 | return; |
| 686 | } |
| 687 | mSupportsDarkText = supportsDarkText; |
| 688 | |
| 689 | // We can't change themes after inflation, in this case we'll have to recreate |
| 690 | // the whole activity. |
Lucas Dupin | 4946f6f | 2019-04-09 15:03:19 -0700 | [diff] [blame] | 691 | if (mBackgroundDrawable != null) { |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 692 | recreate(); |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | int vis = getWindow().getDecorView().getSystemUiVisibility(); |
| 697 | if (supportsDarkText) { |
| 698 | vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 699 | vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 700 | setTheme(R.style.EmergencyDialerThemeDark); |
| 701 | } else { |
| 702 | vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 703 | vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 704 | setTheme(R.style.EmergencyDialerTheme); |
| 705 | } |
| 706 | getWindow().getDecorView().setSystemUiVisibility(vis); |
| 707 | } |
| 708 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 709 | /** |
| 710 | * place the call, but check to make sure it is a viable number. |
| 711 | */ |
| 712 | private void placeCall() { |
| 713 | mLastNumber = mDigits.getText().toString(); |
Wei Huang | 6904b14 | 2017-04-21 19:06:40 +0900 | [diff] [blame] | 714 | |
| 715 | // Convert into emergency number according to emergency conversion map. |
| 716 | // If conversion map is not defined (this is default), this method does |
| 717 | // nothing and just returns input number. |
| 718 | mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber); |
| 719 | |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 720 | boolean isEmergencyNumber; |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 721 | ShortcutViewUtils.PhoneInfo phoneToMakeCall = null; |
Leo Hsu | 7bcfb8e | 2019-05-03 21:54:45 +0800 | [diff] [blame] | 722 | if (mShortcutAdapter != null && mShortcutAdapter.hasShortcut(mLastNumber)) { |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 723 | isEmergencyNumber = true; |
| 724 | phoneToMakeCall = mShortcutViewConfig.getPhoneInfo(); |
Leo Hsu | 7bcfb8e | 2019-05-03 21:54:45 +0800 | [diff] [blame] | 725 | } else if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) { |
| 726 | // If a number from SIM/network/... is categoried as police/ambulance/fire, |
| 727 | // hasPromotedEmergencyNumber() will return true, but it maybe not promoted as a |
| 728 | // shortcut button because a number provided by database has higher priority. |
| 729 | isEmergencyNumber = true; |
| 730 | phoneToMakeCall = mShortcutViewConfig.getPhoneInfo(); |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 731 | } else { |
Automerger Merge Worker | 96417f1 | 2020-02-27 19:34:58 +0000 | [diff] [blame] | 732 | try { |
| 733 | isEmergencyNumber = getSystemService(TelephonyManager.class) |
| 734 | .isEmergencyNumber(mLastNumber); |
| 735 | } catch (IllegalStateException ise) { |
| 736 | isEmergencyNumber = false; |
| 737 | } |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | if (isEmergencyNumber) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 741 | if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber); |
| 742 | |
| 743 | // place the call if it is a valid number |
| 744 | if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) { |
| 745 | // There is no number entered. |
| 746 | playTone(ToneGenerator.TONE_PROP_NACK); |
| 747 | return; |
| 748 | } |
Shaotang Li | 8662a91 | 2018-07-04 16:53:01 +0800 | [diff] [blame] | 749 | |
Hall Liu | 6f35de9 | 2020-01-23 14:29:26 -0800 | [diff] [blame] | 750 | placeCall(mLastNumber, TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD, |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 751 | phoneToMakeCall); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 752 | } else { |
| 753 | if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber); |
| 754 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 755 | showDialog(BAD_EMERGENCY_NUMBER_DIALOG); |
| 756 | } |
Yorke Lee | 9b34151 | 2014-10-17 11:36:41 -0700 | [diff] [blame] | 757 | mDigits.getText().delete(0, mDigits.getText().length()); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 760 | private void placeCall(String number, int callSource, ShortcutViewUtils.PhoneInfo phone) { |
Leo Hsu | e3512b6 | 2019-02-14 15:53:00 +0800 | [diff] [blame] | 761 | Log.d(LOG_TAG, "Place emergency call from " + callSourceToString(callSource) |
| 762 | + ", entry = " + entryTypeToString(mEntryType)); |
| 763 | |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 764 | Bundle extras = new Bundle(); |
| 765 | extras.putInt(TelecomManager.EXTRA_CALL_SOURCE, callSource); |
sqian | 756f606 | 2019-02-08 21:47:14 -0800 | [diff] [blame] | 766 | /** |
| 767 | * This is used for Telecom and Telephony to tell modem user's intent is emergency call, |
| 768 | * when the dialed number is ambiguous and identified as both emergency number and any |
| 769 | * other non-emergency number; e.g. in some situation, 611 could be both an emergency |
| 770 | * number in a country and a non-emergency number of a carrier's customer service hotline. |
| 771 | */ |
| 772 | extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true); |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 773 | |
| 774 | if (phone != null && phone.getPhoneAccountHandle() != null) { |
| 775 | // Requests to dial through the specified phone. |
| 776 | extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, |
| 777 | phone.getPhoneAccountHandle()); |
| 778 | } |
| 779 | |
| 780 | TelecomManager tm = this.getSystemService(TelecomManager.class); |
| 781 | tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null), extras); |
| 782 | } |
| 783 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 784 | /** |
| 785 | * Plays the specified tone for TONE_LENGTH_MS milliseconds. |
| 786 | * |
| 787 | * The tone is played locally, using the audio stream for phone calls. |
| 788 | * Tones are played only if the "Audible touch tones" user preference |
| 789 | * is checked, and are NOT played if the device is in silent mode. |
| 790 | * |
| 791 | * @param tone a tone code from {@link ToneGenerator} |
| 792 | */ |
| 793 | void playTone(int tone) { |
| 794 | // if local tone playback is disabled, just return. |
| 795 | if (!mDTMFToneEnabled) { |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | // Also do nothing if the phone is in silent mode. |
| 800 | // We need to re-check the ringer mode for *every* playTone() |
| 801 | // call, rather than keeping a local flag that's updated in |
| 802 | // onResume(), since it's possible to toggle silent mode without |
| 803 | // leaving the current activity (via the ENDCALL-longpress menu.) |
| 804 | AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
| 805 | int ringerMode = audioManager.getRingerMode(); |
| 806 | if ((ringerMode == AudioManager.RINGER_MODE_SILENT) |
Leo Hsu | 233f5b3 | 2018-11-07 11:08:03 +0800 | [diff] [blame] | 807 | || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 808 | return; |
| 809 | } |
| 810 | |
| 811 | synchronized (mToneGeneratorLock) { |
| 812 | if (mToneGenerator == null) { |
| 813 | Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | // Start the new tone (will stop any playing tone) |
| 818 | mToneGenerator.startTone(tone, TONE_LENGTH_MS); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | private CharSequence createErrorMessage(String number) { |
| 823 | if (!TextUtils.isEmpty(number)) { |
Hall Liu | dc27431 | 2016-03-01 16:34:45 -0800 | [diff] [blame] | 824 | String errorString = getString(R.string.dial_emergency_error, number); |
| 825 | int startingPosition = errorString.indexOf(number); |
| 826 | int endingPosition = startingPosition + number.length(); |
| 827 | Spannable result = new SpannableString(errorString); |
| 828 | PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition); |
| 829 | return result; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 830 | } else { |
| 831 | return getText(R.string.dial_emergency_empty_error).toString(); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | @Override |
| 836 | protected Dialog onCreateDialog(int id) { |
| 837 | AlertDialog dialog = null; |
| 838 | if (id == BAD_EMERGENCY_NUMBER_DIALOG) { |
| 839 | // construct dialog |
Lucas Dupin | 0d666f9 | 2017-06-01 14:04:48 -0700 | [diff] [blame] | 840 | dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme) |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 841 | .setTitle(getText(R.string.emergency_enable_radio_dialog_title)) |
| 842 | .setMessage(createErrorMessage(mLastNumber)) |
| 843 | .setPositiveButton(R.string.ok, null) |
| 844 | .setCancelable(true).create(); |
| 845 | |
| 846 | // blur stuff behind the dialog |
| 847 | dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
Tyler Gunn | cd6a1a9 | 2018-03-29 13:48:29 -0700 | [diff] [blame] | 848 | setShowWhenLocked(true); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 849 | } |
| 850 | return dialog; |
| 851 | } |
| 852 | |
| 853 | @Override |
| 854 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 855 | super.onPrepareDialog(id, dialog); |
| 856 | if (id == BAD_EMERGENCY_NUMBER_DIALOG) { |
| 857 | AlertDialog alert = (AlertDialog) dialog; |
| 858 | alert.setMessage(createErrorMessage(mLastNumber)); |
| 859 | } |
| 860 | } |
| 861 | |
Andrew Lee | d5631e8 | 2014-10-08 16:03:58 -0700 | [diff] [blame] | 862 | @Override |
| 863 | public boolean onOptionsItemSelected(MenuItem item) { |
| 864 | final int itemId = item.getItemId(); |
| 865 | if (itemId == android.R.id.home) { |
| 866 | onBackPressed(); |
| 867 | return true; |
| 868 | } |
| 869 | return super.onOptionsItemSelected(item); |
| 870 | } |
| 871 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 872 | /** |
| 873 | * Update the enabledness of the "Dial" and "Backspace" buttons if applicable. |
| 874 | */ |
| 875 | private void updateDialAndDeleteButtonStateEnabledAttr() { |
| 876 | final boolean notEmpty = mDigits.length() != 0; |
| 877 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 878 | mDelete.setEnabled(notEmpty); |
| 879 | } |
Yorke Lee | 9131166 | 2014-10-24 14:50:45 -0700 | [diff] [blame] | 880 | |
| 881 | /** |
| 882 | * Remove the digit just before the current position. Used by various long pressed callbacks |
| 883 | * to remove the digit that was populated as a result of the short click. |
| 884 | */ |
| 885 | private void removePreviousDigitIfPossible() { |
| 886 | final int currentPosition = mDigits.getSelectionStart(); |
| 887 | if (currentPosition > 0) { |
| 888 | mDigits.setSelection(currentPosition); |
| 889 | mDigits.getText().delete(currentPosition - 1, currentPosition); |
| 890 | } |
| 891 | } |
Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 892 | |
| 893 | /** |
| 894 | * Update the text-to-speech annotations in the edit field. |
| 895 | */ |
| 896 | private void updateTtsSpans() { |
| 897 | for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) { |
| 898 | mDigits.getText().removeSpan(o); |
| 899 | } |
| 900 | PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length()); |
| 901 | } |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 902 | |
| 903 | @Override |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 904 | public void onColorsChanged(WallpaperColors colors, int which) { |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 905 | if ((which & WallpaperManager.FLAG_LOCK) != 0) { |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 906 | mBackgroundDrawable.setColor(getPrimaryColor(colors)); |
| 907 | updateTheme(supportsDarkText(colors)); |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 910 | |
| 911 | /** |
| 912 | * Where a carrier requires a warning that emergency calling is not available while on WFC, |
| 913 | * add hint text above the dial pad which warns the user of this case. |
| 914 | */ |
| 915 | private void maybeShowWfcEmergencyCallingWarning() { |
| 916 | if (!mIsWfcEmergencyCallingWarningEnabled) { |
| 917 | Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier."); |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | // Use an async task rather than calling into Telephony on UI thread. |
| 922 | AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() { |
| 923 | @Override |
| 924 | protected Boolean doInBackground(Void... voids) { |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 925 | TelephonyManager tm = getSystemService(TelephonyManager.class); |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 926 | boolean isWfcAvailable = tm.isWifiCallingAvailable(); |
| 927 | ServiceState ss = tm.getServiceState(); |
| 928 | boolean isCellAvailable = |
| 929 | ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN; |
| 930 | Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable |
Leo Hsu | 233f5b3 | 2018-11-07 11:08:03 +0800 | [diff] [blame] | 931 | + " isCellAvailable=" + isCellAvailable |
| 932 | + "(rat=" + ss.getRilVoiceRadioTechnology() + ")"); |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 933 | return isWfcAvailable && !isCellAvailable; |
| 934 | } |
| 935 | |
| 936 | @Override |
| 937 | protected void onPostExecute(Boolean result) { |
| 938 | if (result.booleanValue()) { |
| 939 | Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning"); |
| 940 | mDigits.setHint(R.string.dial_emergency_calling_not_available); |
| 941 | } else { |
| 942 | Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning"); |
| 943 | mDigits.setHint(""); |
| 944 | } |
| 945 | maybeChangeHintSize(); |
| 946 | } |
| 947 | }; |
| 948 | showWfcWarningTask.execute((Void) null); |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits |
| 953 | * edit view and set the font size to a smaller size appropriate for the emergency calling |
| 954 | * warning. |
| 955 | */ |
| 956 | private void maybeChangeHintSize() { |
| 957 | if (TextUtils.isEmpty(mDigits.getHint()) |
| 958 | || !TextUtils.isEmpty(mDigits.getText().toString())) { |
| 959 | // No hint or there are dialed digits, so use default size. |
| 960 | mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize); |
| 961 | // By default, the digits view auto-resizes to fit the text it contains, so |
| 962 | // enable that now. |
| 963 | mDigits.setResizeEnabled(true); |
| 964 | Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize()); |
| 965 | } else { |
| 966 | // Hint present and no dialed digits, set custom font size appropriate for the warning. |
| 967 | mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize( |
| 968 | R.dimen.emergency_call_warning_size)); |
| 969 | // Since we're populating this with a static text string, disable auto-resize. |
| 970 | mDigits.setResizeEnabled(false); |
| 971 | Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize()); |
| 972 | } |
| 973 | } |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 974 | |
Fan Zhang | 39d81f5 | 2022-03-21 22:47:21 +0000 | [diff] [blame] | 975 | private void setupEmergencyDialpadViews() { |
| 976 | mEmergencyInfoInDialpad.setOnConfirmClickListener(this); |
| 977 | } |
| 978 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 979 | private void setupEmergencyShortcutsView() { |
| 980 | mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts); |
| 981 | mDialpadView = findViewById(R.id.emergency_dialer); |
| 982 | |
Chihhang Chuang | caba0da | 2018-08-02 22:25:06 +0800 | [diff] [blame] | 983 | mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate); |
| 984 | mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate); |
| 985 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 986 | final View dialpadButton = findViewById(R.id.floating_action_button_dialpad); |
| 987 | dialpadButton.setOnClickListener(this); |
| 988 | |
Fan Zhang | 39d81f5 | 2022-03-21 22:47:21 +0000 | [diff] [blame] | 989 | mEmergencyInfoInShortcut.setOnConfirmClickListener(this); |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 990 | |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 991 | mEmergencyShortcutButtonList = new ArrayList<>(); |
| 992 | setupEmergencyCallShortcutButton(); |
| 993 | |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 994 | updateLocationAndEccInfo(); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 995 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 996 | switchView(mEmergencyShortcutView, mDialpadView, false); |
| 997 | } |
| 998 | |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 999 | private void setLocationInfo() { |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1000 | final View locationInfo = findViewById(R.id.location_info); |
| 1001 | |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 1002 | String countryIso = mShortcutViewConfig.getCountryIso(); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1003 | String countryName = null; |
| 1004 | if (!TextUtils.isEmpty(countryIso)) { |
| 1005 | Locale locale = Locale.getDefault(); |
| 1006 | countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant()) |
| 1007 | .getDisplayCountry(); |
| 1008 | } |
| 1009 | if (TextUtils.isEmpty(countryName)) { |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1010 | locationInfo.setVisibility(View.INVISIBLE); |
| 1011 | } else { |
| 1012 | final TextView location = (TextView) locationInfo.findViewById(R.id.location_text); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1013 | location.setText(countryName); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1014 | locationInfo.setVisibility(View.VISIBLE); |
| 1015 | } |
| 1016 | } |
| 1017 | |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1018 | private void setupEmergencyCallShortcutButton() { |
| 1019 | final ViewGroup shortcutButtonContainer = findViewById( |
| 1020 | R.id.emergency_shortcut_buttons_container); |
| 1021 | shortcutButtonContainer.setClipToOutline(true); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1022 | final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1023 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1024 | mShortcutAdapter = new EccShortcutAdapter(this) { |
| 1025 | @Override |
| 1026 | public View inflateView(View convertView, ViewGroup parent, CharSequence number, |
| 1027 | CharSequence description, int iconRes) { |
| 1028 | EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater() |
| 1029 | .inflate(R.layout.emergency_shortcut_button, parent, false); |
| 1030 | button.setPhoneNumber(number); |
| 1031 | button.setPhoneDescription(description); |
| 1032 | button.setPhoneTypeIcon(iconRes); |
| 1033 | button.setOnConfirmClickListener(EmergencyDialer.this); |
| 1034 | return button; |
| 1035 | } |
| 1036 | }; |
| 1037 | mShortcutDataSetObserver = new DataSetObserver() { |
| 1038 | @Override |
| 1039 | public void onChanged() { |
| 1040 | super.onChanged(); |
| 1041 | updateLayout(); |
| 1042 | } |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1043 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1044 | @Override |
| 1045 | public void onInvalidated() { |
| 1046 | super.onInvalidated(); |
| 1047 | updateLayout(); |
| 1048 | } |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1049 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1050 | private void updateLayout() { |
| 1051 | // clear previous added buttons |
| 1052 | shortcutButtonContainer.removeAllViews(); |
| 1053 | mEmergencyShortcutButtonList.clear(); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1054 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1055 | for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) { |
| 1056 | EmergencyShortcutButton button = (EmergencyShortcutButton) |
| 1057 | mShortcutAdapter.getView(i, null, shortcutButtonContainer); |
| 1058 | mEmergencyShortcutButtonList.add(button); |
| 1059 | shortcutButtonContainer.addView(button); |
| 1060 | } |
| 1061 | |
Wesley.CW Wang | 5e78539 | 2018-08-09 20:11:34 +0800 | [diff] [blame] | 1062 | // Update emergency numbers title for numerous buttons. |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1063 | if (mEmergencyShortcutButtonList.size() > 1) { |
| 1064 | emergencyNumberTitle.setText(getString( |
| 1065 | R.string.numerous_emergency_numbers_title)); |
| 1066 | } else { |
| 1067 | emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title)); |
| 1068 | } |
| 1069 | } |
| 1070 | }; |
| 1071 | mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver); |
| 1072 | } |
| 1073 | |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 1074 | private void updateLocationAndEccInfo() { |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1075 | if (!isFinishing() && !isDestroyed()) { |
Leo Hsu | 43d670a | 2018-12-04 15:40:36 +0800 | [diff] [blame] | 1076 | setLocationInfo(); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1077 | if (mShortcutAdapter != null) { |
Leo Hsu | bc7553a | 2019-03-05 15:47:23 +0800 | [diff] [blame] | 1078 | mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo()); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1079 | } |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * Called by the activity before a touch event is dispatched to the view hierarchy. |
| 1085 | */ |
| 1086 | private void onPreTouchEvent(MotionEvent event) { |
Fan Zhang | 39d81f5 | 2022-03-21 22:47:21 +0000 | [diff] [blame] | 1087 | mEmergencyInfoInDialpad.onPreTouchEvent(event); |
| 1088 | mEmergencyInfoInShortcut.onPreTouchEvent(event); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1089 | |
| 1090 | if (mEmergencyShortcutButtonList != null) { |
| 1091 | for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) { |
| 1092 | button.onPreTouchEvent(event); |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * Called by the activity after a touch event is dispatched to the view hierarchy. |
| 1099 | */ |
| 1100 | private void onPostTouchEvent(MotionEvent event) { |
Fan Zhang | 39d81f5 | 2022-03-21 22:47:21 +0000 | [diff] [blame] | 1101 | mEmergencyInfoInDialpad.onPostTouchEvent(event); |
| 1102 | mEmergencyInfoInShortcut.onPostTouchEvent(event); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1103 | |
| 1104 | if (mEmergencyShortcutButtonList != null) { |
| 1105 | for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) { |
| 1106 | button.onPostTouchEvent(event); |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 1111 | /** |
| 1112 | * Switch two view. |
| 1113 | * |
Leo Hsu | 83ab233 | 2018-12-18 15:20:45 +0800 | [diff] [blame] | 1114 | * @param displayView the view would be displayed. |
| 1115 | * @param hideView the view would be hidden. |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 1116 | * @param hasAnimation is {@code true} when the view should be displayed with animation. |
| 1117 | */ |
| 1118 | private void switchView(View displayView, View hideView, boolean hasAnimation) { |
| 1119 | if (displayView == null || hideView == null) { |
| 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | if (displayView.getVisibility() == View.VISIBLE) { |
| 1124 | return; |
| 1125 | } |
| 1126 | |
| 1127 | if (hasAnimation) { |
| 1128 | crossfade(hideView, displayView); |
| 1129 | } else { |
| 1130 | hideView.setVisibility(View.GONE); |
| 1131 | displayView.setVisibility(View.VISIBLE); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * Fade out and fade in animation between two view transition. |
| 1137 | */ |
| 1138 | private void crossfade(View fadeOutView, View fadeInView) { |
| 1139 | if (fadeOutView == null || fadeInView == null) { |
| 1140 | return; |
| 1141 | } |
| 1142 | final int shortAnimationDuration = getResources().getInteger( |
| 1143 | android.R.integer.config_shortAnimTime); |
| 1144 | |
| 1145 | fadeInView.setAlpha(0f); |
| 1146 | fadeInView.setVisibility(View.VISIBLE); |
| 1147 | |
| 1148 | fadeInView.animate() |
| 1149 | .alpha(1f) |
| 1150 | .setDuration(shortAnimationDuration) |
| 1151 | .setListener(null); |
| 1152 | |
| 1153 | fadeOutView.animate() |
| 1154 | .alpha(0f) |
| 1155 | .setDuration(shortAnimationDuration) |
| 1156 | .setListener(new AnimatorListenerAdapter() { |
| 1157 | @Override |
| 1158 | public void onAnimationEnd(Animator animation) { |
| 1159 | fadeOutView.setVisibility(View.GONE); |
| 1160 | } |
| 1161 | }); |
| 1162 | } |
Shaotang Li | 8662a91 | 2018-07-04 16:53:01 +0800 | [diff] [blame] | 1163 | |
Shaotang Li | 8662a91 | 2018-07-04 16:53:01 +0800 | [diff] [blame] | 1164 | private boolean isShortcutNumber(String number) { |
| 1165 | if (TextUtils.isEmpty(number) || mEmergencyShortcutButtonList == null) { |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
| 1169 | boolean isShortcut = false; |
| 1170 | for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) { |
| 1171 | if (button != null && number.equals(button.getPhoneNumber())) { |
| 1172 | isShortcut = true; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | return isShortcut; |
| 1177 | } |
Leo Hsu | e3512b6 | 2019-02-14 15:53:00 +0800 | [diff] [blame] | 1178 | |
| 1179 | private String entryTypeToString(int entryType) { |
| 1180 | switch (entryType) { |
| 1181 | case ENTRY_TYPE_LOCKSCREEN_BUTTON: |
| 1182 | return "LockScreen"; |
| 1183 | case ENTRY_TYPE_POWER_MENU: |
| 1184 | return "PowerMenu"; |
| 1185 | default: |
| 1186 | return "Unknown-" + entryType; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | private String callSourceToString(int callSource) { |
| 1191 | switch (callSource) { |
Hall Liu | 6f35de9 | 2020-01-23 14:29:26 -0800 | [diff] [blame] | 1192 | case TelecomManager.CALL_SOURCE_EMERGENCY_DIALPAD: |
Leo Hsu | e3512b6 | 2019-02-14 15:53:00 +0800 | [diff] [blame] | 1193 | return "DialPad"; |
Hall Liu | 6f35de9 | 2020-01-23 14:29:26 -0800 | [diff] [blame] | 1194 | case TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT: |
Leo Hsu | e3512b6 | 2019-02-14 15:53:00 +0800 | [diff] [blame] | 1195 | return "Shortcut"; |
| 1196 | default: |
| 1197 | return "Unknown-" + callSource; |
| 1198 | } |
| 1199 | } |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 1200 | |
| 1201 | private WallpaperManager getWallpaperManager() { |
| 1202 | return getSystemService(WallpaperManager.class); |
| 1203 | } |
| 1204 | |
| 1205 | private static boolean supportsDarkText(WallpaperColors colors) { |
| 1206 | if (colors != null) { |
| 1207 | return (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0; |
| 1208 | } |
| 1209 | // It's possible that wallpaper colors are null (e.g. when colors are being |
| 1210 | // processed or a live wallpaper is used). In this case, fallback to same |
| 1211 | // behavior as when shortcut view is enabled. |
| 1212 | return false; |
| 1213 | } |
| 1214 | |
Grace Jia | 4c2e6ae | 2020-04-21 15:35:49 -0700 | [diff] [blame] | 1215 | private int getPrimaryColor(WallpaperColors colors) { |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 1216 | if (colors != null) { |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1217 | // Android accessibility scanner |
| 1218 | // (https://support.google.com/accessibility/android/answer/7158390) |
| 1219 | // suggest small text and graphics have a contrast ratio greater than |
| 1220 | // 4.5 with background color. The color generated from wallpaper may not |
| 1221 | // follow this rule. Calculate a proper color here. |
| 1222 | Color primary = colors.getPrimaryColor(); |
Grace Jia | 4c2e6ae | 2020-04-21 15:35:49 -0700 | [diff] [blame] | 1223 | Color text; |
| 1224 | if (mSupportsDarkText) { |
| 1225 | text = Color.valueOf(Color.BLACK); |
| 1226 | } else { |
| 1227 | text = Color.valueOf(Color.WHITE); |
| 1228 | } |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1229 | Color dial = Color.valueOf(DIALER_GREEN); |
| 1230 | // If current primary color can't follow the contrast ratio rule, make it |
Grace Jia | 4c2e6ae | 2020-04-21 15:35:49 -0700 | [diff] [blame] | 1231 | // deeper/lighter and try again. |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1232 | while (!checkContrastRatio(primary, text)) { |
Grace Jia | 4c2e6ae | 2020-04-21 15:35:49 -0700 | [diff] [blame] | 1233 | primary = getNextColor(primary, mSupportsDarkText); |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1234 | } |
Grace Jia | 4c2e6ae | 2020-04-21 15:35:49 -0700 | [diff] [blame] | 1235 | if (!mSupportsDarkText) { |
| 1236 | while (!checkContrastRatio(primary, dial)) { |
| 1237 | primary = getNextColor(primary, mSupportsDarkText); |
| 1238 | } |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1239 | } |
| 1240 | return primary.toArgb(); |
Michele | f6a5ea2 | 2019-10-15 16:08:54 -0700 | [diff] [blame] | 1241 | } |
| 1242 | // It's possible that wallpaper colors are null (e.g. when colors are being |
| 1243 | // processed or a live wallpaper is used). In this case, fallback to same |
| 1244 | // behavior as when shortcut view is enabled. |
| 1245 | return Color.BLACK; |
| 1246 | } |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1247 | |
Grace Jia | 4c2e6ae | 2020-04-21 15:35:49 -0700 | [diff] [blame] | 1248 | private Color getNextColor(Color color, boolean darkText) { |
| 1249 | float sign = darkText ? 1.f : -1.f; |
| 1250 | float r = color.red() + sign * COLOR_DELTA; |
| 1251 | float g = color.green() + sign * COLOR_DELTA; |
| 1252 | float b = color.blue() + sign * COLOR_DELTA; |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1253 | if (r < 0f) r = 0f; |
| 1254 | if (g < 0f) g = 0f; |
| 1255 | if (b < 0f) b = 0f; |
Grace Jia | 4c2e6ae | 2020-04-21 15:35:49 -0700 | [diff] [blame] | 1256 | if (r > 1f) r = 1f; |
| 1257 | if (g > 1f) g = 1f; |
| 1258 | if (b > 1f) b = 1f; |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1259 | return Color.valueOf(r, g, b); |
| 1260 | } |
| 1261 | |
Grace Jia | 4c2e6ae | 2020-04-21 15:35:49 -0700 | [diff] [blame] | 1262 | private boolean checkContrastRatio(Color color1, Color color2) { |
Grace Jia | 835d8e6 | 2020-04-01 15:47:39 -0700 | [diff] [blame] | 1263 | float lum1 = color1.luminance(); |
| 1264 | float lum2 = color2.luminance(); |
| 1265 | double cr; |
| 1266 | if (lum1 >= lum2) { |
| 1267 | cr = (lum1 + 0.05) / (lum2 + 0.05); |
| 1268 | } else { |
| 1269 | cr = (lum2 + 0.05) / (lum1 + 0.05); |
| 1270 | } |
| 1271 | |
| 1272 | // Make cr greater than 5.0 instead of 4.5 to guarantee that transparent white |
| 1273 | // text and graphics can have contrast ratio greather than 4.5 with background. |
| 1274 | return cr > 5.0; |
| 1275 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1276 | } |