| 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 | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 19 | import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN; |
| 20 | |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 23 | import android.app.Activity; |
| 24 | import android.app.AlertDialog; |
| 25 | import android.app.Dialog; |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 26 | import android.app.WallpaperManager; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 31 | import android.graphics.Point; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 32 | import android.media.AudioManager; |
| 33 | import android.media.ToneGenerator; |
| 34 | import android.net.Uri; |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 35 | import android.os.AsyncTask; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 36 | import android.os.Bundle; |
| Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 37 | import android.os.PersistableBundle; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 38 | import android.provider.Settings; |
| Tyler Gunn | 4d45d1c | 2014-09-12 22:17:53 -0700 | [diff] [blame] | 39 | import android.telecom.PhoneAccount; |
| Tyler Gunn | ca7dfef | 2018-03-27 18:50:05 +0000 | [diff] [blame] | 40 | import android.telecom.TelecomManager; |
| Jonathan Basseri | 3649bdb | 2015-04-30 22:39:11 -0700 | [diff] [blame] | 41 | import android.telephony.CarrierConfigManager; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 42 | import android.telephony.PhoneNumberUtils; |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 43 | import android.telephony.ServiceState; |
| Santos Cordon | e137eed | 2015-06-23 15:34:47 -0700 | [diff] [blame] | 44 | import android.telephony.SubscriptionManager; |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 45 | import android.telephony.TelephonyManager; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 46 | import android.text.Editable; |
| Hall Liu | bdc9c88 | 2016-05-25 15:25:28 -0700 | [diff] [blame] | 47 | import android.text.InputType; |
| Hall Liu | dc27431 | 2016-03-01 16:34:45 -0800 | [diff] [blame] | 48 | import android.text.Spannable; |
| 49 | import android.text.SpannableString; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 50 | import android.text.TextUtils; |
| 51 | import android.text.TextWatcher; |
| 52 | import android.text.method.DialerKeyListener; |
| Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 53 | import android.text.style.TtsSpan; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 54 | import android.util.Log; |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 55 | import android.util.TypedValue; |
| Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 56 | import android.view.HapticFeedbackConstants; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 57 | import android.view.KeyEvent; |
| Andrew Lee | d5631e8 | 2014-10-08 16:03:58 -0700 | [diff] [blame] | 58 | import android.view.MenuItem; |
| Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 59 | import android.view.MotionEvent; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 60 | import android.view.View; |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 61 | import android.view.ViewGroup; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 62 | import android.view.WindowManager; |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 63 | import android.widget.TextView; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 64 | |
| Lucas Dupin | af9e991 | 2017-06-22 12:39:39 -0700 | [diff] [blame] | 65 | import com.android.internal.colorextraction.ColorExtractor; |
| 66 | import com.android.internal.colorextraction.ColorExtractor.GradientColors; |
| 67 | import com.android.internal.colorextraction.drawable.GradientDrawable; |
| Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 68 | import com.android.phone.common.dialpad.DialpadKeyButton; |
| Sai Cheemalapati | 14462b6 | 2014-06-18 13:53:56 -0700 | [diff] [blame] | 69 | import com.android.phone.common.util.ViewUtil; |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 70 | import com.android.phone.common.widget.ResizingTextEditText; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 71 | |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 72 | import java.util.ArrayList; |
| 73 | import java.util.List; |
| 74 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 75 | /** |
| 76 | * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls. |
| 77 | * |
| 78 | * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer |
| 79 | * activity from apps/Contacts) that: |
| 80 | * 1. Allows ONLY emergency calls to be dialed |
| 81 | * 2. Disallows voicemail functionality |
| 82 | * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this |
| 83 | * activity to stay in front of the keyguard. |
| 84 | * |
| 85 | * TODO: Even though this is an ultra-simplified version of the normal |
| 86 | * dialer, there's still lots of code duplication between this class and |
| 87 | * the TwelveKeyDialer class from apps/Contacts. Could the common code be |
| 88 | * moved into a shared base class that would live in the framework? |
| 89 | * Or could we figure out some way to move *this* class into apps/Contacts |
| 90 | * also? |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 91 | * |
| 92 | * TODO: Implement emergency dialer shortcut. |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 93 | * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button |
| 94 | * to place an emergency phone call without entering numbers from dialpad. |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 95 | * TODO item: |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 96 | * 1.integrate emergency phone number table. |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 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, |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 100 | DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener, |
| 101 | EmergencyShortcutButton.OnConfirmClickListener { |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 102 | // Keys used with onSaveInstanceState(). |
| 103 | private static final String LAST_NUMBER = "lastNumber"; |
| 104 | |
| 105 | // Intent action for this activity. |
| 106 | public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL"; |
| 107 | |
| 108 | // List of dialer button IDs. |
| 109 | private static final int[] DIALER_KEYS = new int[] { |
| 110 | R.id.one, R.id.two, R.id.three, |
| 111 | R.id.four, R.id.five, R.id.six, |
| 112 | R.id.seven, R.id.eight, R.id.nine, |
| 113 | R.id.star, R.id.zero, R.id.pound }; |
| 114 | |
| 115 | // Debug constants. |
| 116 | private static final boolean DBG = false; |
| 117 | private static final String LOG_TAG = "EmergencyDialer"; |
| 118 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 119 | /** The length of DTMF tones in milliseconds */ |
| 120 | private static final int TONE_LENGTH_MS = 150; |
| 121 | |
| 122 | /** The DTMF tone volume relative to other sounds in the stream */ |
| 123 | private static final int TONE_RELATIVE_VOLUME = 80; |
| 124 | |
| 125 | /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */ |
| 126 | private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF; |
| 127 | |
| 128 | private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0; |
| 129 | |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 130 | /** 90% opacity, different from other gradients **/ |
| 131 | private static final int BACKGROUND_GRADIENT_ALPHA = 230; |
| 132 | |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 133 | ResizingTextEditText mDigits; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 134 | private View mDialButton; |
| 135 | private View mDelete; |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 136 | private View mEmergencyShortcutView; |
| 137 | private View mDialpadView; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 138 | |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 139 | private List<EmergencyShortcutButton> mEmergencyShortcutButtonList; |
| 140 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 141 | private ToneGenerator mToneGenerator; |
| 142 | private Object mToneGeneratorLock = new Object(); |
| 143 | |
| 144 | // determines if we want to playback local DTMF tones. |
| 145 | private boolean mDTMFToneEnabled; |
| 146 | |
| Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 147 | private EmergencyActionGroup mEmergencyActionGroup; |
| 148 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 149 | // close activity when screen turns off |
| 150 | private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 151 | @Override |
| 152 | public void onReceive(Context context, Intent intent) { |
| 153 | if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { |
| Adrian Roos | 061c723 | 2015-04-21 12:37:07 -0700 | [diff] [blame] | 154 | finishAndRemoveTask(); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | private String mLastNumber; // last number we tried to dial. Used to restore error dialog. |
| 160 | |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 161 | // Background gradient |
| 162 | private ColorExtractor mColorExtractor; |
| 163 | private GradientDrawable mBackgroundGradient; |
| 164 | private boolean mSupportsDarkText; |
| 165 | |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 166 | private boolean mIsWfcEmergencyCallingWarningEnabled; |
| 167 | private float mDefaultDigitsTextSize; |
| 168 | |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 169 | private boolean mAreEmergencyDialerShortcutsEnabled; |
| 170 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 171 | @Override |
| 172 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 173 | // Do nothing |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void onTextChanged(CharSequence input, int start, int before, int changeCount) { |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 178 | maybeChangeHintSize(); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void afterTextChanged(Editable input) { |
| 183 | // Check for special sequences, in particular the "**04" or "**05" |
| 184 | // sequences that allow you to enter PIN or PUK-related codes. |
| 185 | // |
| 186 | // But note we *don't* allow most other special sequences here, |
| 187 | // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"), |
| 188 | // since those shouldn't be available if the device is locked. |
| 189 | // |
| 190 | // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice() |
| 191 | // here, not the regular handleChars() method. |
| 192 | if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) { |
| 193 | // A special sequence was entered, clear the digits |
| 194 | mDigits.getText().clear(); |
| 195 | } |
| 196 | |
| 197 | updateDialAndDeleteButtonStateEnabledAttr(); |
| Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 198 | updateTtsSpans(); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | protected void onCreate(Bundle icicle) { |
| 203 | super.onCreate(icicle); |
| 204 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 205 | // Allow this activity to be displayed in front of the keyguard / lockscreen. |
| 206 | WindowManager.LayoutParams lp = getWindow().getAttributes(); |
| 207 | lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; |
| Santos Cordon | fc30981 | 2013-08-20 18:33:16 -0700 | [diff] [blame] | 208 | |
| 209 | // When no proximity sensor is available, use a shorter timeout. |
| Christine Chen | 07fae16 | 2013-09-19 15:05:56 -0700 | [diff] [blame] | 210 | // TODO: Do we enable this for non proximity devices any more? |
| Santos Cordon | fc30981 | 2013-08-20 18:33:16 -0700 | [diff] [blame] | 211 | // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR; |
| 212 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 213 | getWindow().setAttributes(lp); |
| 214 | |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 215 | mColorExtractor = new ColorExtractor(this); |
| Lucas Dupin | 94b566f | 2017-05-28 11:45:52 -0700 | [diff] [blame] | 216 | GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK, |
| 217 | ColorExtractor.TYPE_EXTRA_DARK); |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 218 | updateTheme(lockScreenColors.supportsDarkText()); |
| 219 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 220 | setContentView(R.layout.emergency_dialer); |
| 221 | |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 222 | mDigits = (ResizingTextEditText) findViewById(R.id.digits); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 223 | mDigits.setKeyListener(DialerKeyListener.getInstance()); |
| 224 | mDigits.setOnClickListener(this); |
| 225 | mDigits.setOnKeyListener(this); |
| 226 | mDigits.setLongClickable(false); |
| Hall Liu | bdc9c88 | 2016-05-25 15:25:28 -0700 | [diff] [blame] | 227 | mDigits.setInputType(InputType.TYPE_NULL); |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 228 | mDefaultDigitsTextSize = mDigits.getScaledTextSize(); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 229 | maybeAddNumberFormatting(); |
| 230 | |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 231 | mBackgroundGradient = new GradientDrawable(this); |
| 232 | Point displaySize = new Point(); |
| 233 | ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) |
| 234 | .getDefaultDisplay().getSize(displaySize); |
| 235 | mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y); |
| 236 | mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA); |
| 237 | getWindow().setBackgroundDrawable(mBackgroundGradient); |
| 238 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 239 | // Check for the presence of the keypad |
| 240 | View view = findViewById(R.id.one); |
| 241 | if (view != null) { |
| 242 | setupKeypad(); |
| 243 | } |
| 244 | |
| 245 | mDelete = findViewById(R.id.deleteButton); |
| 246 | mDelete.setOnClickListener(this); |
| 247 | mDelete.setOnLongClickListener(this); |
| 248 | |
| Sai Cheemalapati | 14462b6 | 2014-06-18 13:53:56 -0700 | [diff] [blame] | 249 | mDialButton = findViewById(R.id.floating_action_button); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 250 | |
| 251 | // Check whether we should show the onscreen "Dial" button and co. |
| Jonathan Basseri | e619a4c | 2015-06-26 14:52:26 -0700 | [diff] [blame] | 252 | // Read carrier config through the public API because PhoneGlobals is not available when we |
| 253 | // run as a secondary user. |
| 254 | CarrierConfigManager configMgr = |
| 255 | (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE); |
| 256 | PersistableBundle carrierConfig = |
| Shishir Agrawal | d3480e0 | 2016-01-25 13:05:49 -0800 | [diff] [blame] | 257 | configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 258 | |
| Jonathan Basseri | 9504c6b | 2015-06-04 14:23:32 -0700 | [diff] [blame] | 259 | if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) { |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 260 | mDialButton.setOnClickListener(this); |
| 261 | } else { |
| 262 | mDialButton.setVisibility(View.GONE); |
| 263 | } |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 264 | mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt( |
| 265 | CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1; |
| 266 | maybeShowWfcEmergencyCallingWarning(); |
| 267 | |
| Adrian Roos | c9fdb6c | 2015-05-25 15:10:21 -0700 | [diff] [blame] | 268 | ViewUtil.setupFloatingActionButton(mDialButton, getResources()); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 269 | |
| 270 | if (icicle != null) { |
| 271 | super.onRestoreInstanceState(icicle); |
| 272 | } |
| 273 | |
| 274 | // Extract phone number from intent |
| 275 | Uri data = getIntent().getData(); |
| Jay Shrauner | 137458b | 2014-09-05 14:27:25 -0700 | [diff] [blame] | 276 | if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) { |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 277 | String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this); |
| 278 | if (number != null) { |
| 279 | mDigits.setText(number); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // if the mToneGenerator creation fails, just continue without it. It is |
| 284 | // a local audio signal, and is not as important as the dtmf tone itself. |
| 285 | synchronized (mToneGeneratorLock) { |
| 286 | if (mToneGenerator == null) { |
| 287 | try { |
| 288 | mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME); |
| 289 | } catch (RuntimeException e) { |
| 290 | Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e); |
| 291 | mToneGenerator = null; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | final IntentFilter intentFilter = new IntentFilter(); |
| 297 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 298 | registerReceiver(mBroadcastReceiver, intentFilter); |
| 299 | |
| Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 300 | mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group); |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 301 | |
| 302 | mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(), |
| 303 | Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0; |
| 304 | |
| 305 | if (mAreEmergencyDialerShortcutsEnabled) { |
| 306 | setupEmergencyShortcutsView(); |
| 307 | } |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | @Override |
| 311 | protected void onDestroy() { |
| 312 | super.onDestroy(); |
| 313 | synchronized (mToneGeneratorLock) { |
| 314 | if (mToneGenerator != null) { |
| 315 | mToneGenerator.release(); |
| 316 | mToneGenerator = null; |
| 317 | } |
| 318 | } |
| 319 | unregisterReceiver(mBroadcastReceiver); |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | protected void onRestoreInstanceState(Bundle icicle) { |
| 324 | mLastNumber = icicle.getString(LAST_NUMBER); |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | protected void onSaveInstanceState(Bundle outState) { |
| 329 | super.onSaveInstanceState(outState); |
| 330 | outState.putString(LAST_NUMBER, mLastNumber); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Explicitly turn off number formatting, since it gets in the way of the emergency |
| 335 | * number detector |
| 336 | */ |
| 337 | protected void maybeAddNumberFormatting() { |
| 338 | // Do nothing. |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | protected void onPostCreate(Bundle savedInstanceState) { |
| 343 | super.onPostCreate(savedInstanceState); |
| 344 | |
| 345 | // This can't be done in onCreate(), since the auto-restoring of the digits |
| 346 | // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState() |
| 347 | // is called. This method will be called every time the activity is created, and |
| 348 | // will always happen after onRestoreSavedInstanceState(). |
| 349 | mDigits.addTextChangedListener(this); |
| 350 | } |
| 351 | |
| 352 | private void setupKeypad() { |
| 353 | // Setup the listeners for the buttons |
| 354 | for (int id : DIALER_KEYS) { |
| Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 355 | final DialpadKeyButton key = (DialpadKeyButton) findViewById(id); |
| 356 | key.setOnPressedListener(this); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | View view = findViewById(R.id.zero); |
| 360 | view.setOnLongClickListener(this); |
| 361 | } |
| 362 | |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 363 | @Override |
| 364 | public void onBackPressed() { |
| 365 | // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the |
| 366 | // back key will back to display FasterEmergencyDialer view. |
| 367 | // Otherwise, it would finish the activity. |
| 368 | if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null |
| 369 | && mDialpadView.getVisibility() == View.VISIBLE) { |
| 370 | switchView(mEmergencyShortcutView, mDialpadView, true); |
| 371 | return; |
| 372 | } |
| 373 | super.onBackPressed(); |
| 374 | } |
| 375 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 376 | /** |
| 377 | * handle key events |
| 378 | */ |
| 379 | @Override |
| 380 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 381 | switch (keyCode) { |
| 382 | // Happen when there's a "Call" hard button. |
| 383 | case KeyEvent.KEYCODE_CALL: { |
| 384 | if (TextUtils.isEmpty(mDigits.getText().toString())) { |
| 385 | // if we are adding a call from the InCallScreen and the phone |
| 386 | // number entered is empty, we just close the dialer to expose |
| 387 | // the InCallScreen under it. |
| 388 | finish(); |
| 389 | } else { |
| 390 | // otherwise, we place the call. |
| 391 | placeCall(); |
| 392 | } |
| 393 | return true; |
| 394 | } |
| 395 | } |
| 396 | return super.onKeyDown(keyCode, event); |
| 397 | } |
| 398 | |
| 399 | private void keyPressed(int keyCode) { |
| Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 400 | mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 401 | KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); |
| 402 | mDigits.onKeyDown(keyCode, event); |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public boolean onKey(View view, int keyCode, KeyEvent event) { |
| 407 | switch (view.getId()) { |
| 408 | case R.id.digits: |
| 409 | // Happen when "Done" button of the IME is pressed. This can happen when this |
| 410 | // Activity is forced into landscape mode due to a desk dock. |
| 411 | if (keyCode == KeyEvent.KEYCODE_ENTER |
| 412 | && event.getAction() == KeyEvent.ACTION_UP) { |
| 413 | placeCall(); |
| 414 | return true; |
| 415 | } |
| 416 | break; |
| 417 | } |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | @Override |
| Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 422 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 423 | onPreTouchEvent(ev); |
| Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 424 | boolean handled = super.dispatchTouchEvent(ev); |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 425 | onPostTouchEvent(ev); |
| Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 426 | return handled; |
| 427 | } |
| 428 | |
| 429 | @Override |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 430 | public void onConfirmClick(EmergencyShortcutButton button) { |
| 431 | if (button == null) return; |
| 432 | |
| 433 | String phoneNumber = button.getPhoneNumber(); |
| 434 | |
| 435 | if (!TextUtils.isEmpty(phoneNumber)) { |
| 436 | Log.d(LOG_TAG, "dial emergency number: " + phoneNumber); |
| 437 | TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE); |
| 438 | tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null); |
| 439 | } else { |
| 440 | Log.d(LOG_TAG, "emergency number is empty"); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | @Override |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 445 | public void onClick(View view) { |
| 446 | switch (view.getId()) { |
| Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 447 | case R.id.deleteButton: { |
| 448 | keyPressed(KeyEvent.KEYCODE_DEL); |
| 449 | return; |
| 450 | } |
| 451 | case R.id.floating_action_button: { |
| Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 452 | view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
| Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 453 | placeCall(); |
| 454 | return; |
| 455 | } |
| 456 | case R.id.digits: { |
| 457 | if (mDigits.length() != 0) { |
| 458 | mDigits.setCursorVisible(true); |
| 459 | } |
| 460 | return; |
| 461 | } |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 462 | case R.id.floating_action_button_dialpad: { |
| 463 | switchView(mDialpadView, mEmergencyShortcutView, true); |
| 464 | return; |
| 465 | } |
| 466 | case R.id.emergency_info_button: { |
| 467 | Intent intent = (Intent) view.getTag(R.id.tag_intent); |
| 468 | if (intent != null) { |
| 469 | startActivity(intent); |
| 470 | } |
| 471 | return; |
| 472 | } |
| Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void onPressed(View view, boolean pressed) { |
| 478 | if (!pressed) { |
| 479 | return; |
| 480 | } |
| 481 | switch (view.getId()) { |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 482 | case R.id.one: { |
| 483 | playTone(ToneGenerator.TONE_DTMF_1); |
| 484 | keyPressed(KeyEvent.KEYCODE_1); |
| 485 | return; |
| 486 | } |
| 487 | case R.id.two: { |
| 488 | playTone(ToneGenerator.TONE_DTMF_2); |
| 489 | keyPressed(KeyEvent.KEYCODE_2); |
| 490 | return; |
| 491 | } |
| 492 | case R.id.three: { |
| 493 | playTone(ToneGenerator.TONE_DTMF_3); |
| 494 | keyPressed(KeyEvent.KEYCODE_3); |
| 495 | return; |
| 496 | } |
| 497 | case R.id.four: { |
| 498 | playTone(ToneGenerator.TONE_DTMF_4); |
| 499 | keyPressed(KeyEvent.KEYCODE_4); |
| 500 | return; |
| 501 | } |
| 502 | case R.id.five: { |
| 503 | playTone(ToneGenerator.TONE_DTMF_5); |
| 504 | keyPressed(KeyEvent.KEYCODE_5); |
| 505 | return; |
| 506 | } |
| 507 | case R.id.six: { |
| 508 | playTone(ToneGenerator.TONE_DTMF_6); |
| 509 | keyPressed(KeyEvent.KEYCODE_6); |
| 510 | return; |
| 511 | } |
| 512 | case R.id.seven: { |
| 513 | playTone(ToneGenerator.TONE_DTMF_7); |
| 514 | keyPressed(KeyEvent.KEYCODE_7); |
| 515 | return; |
| 516 | } |
| 517 | case R.id.eight: { |
| 518 | playTone(ToneGenerator.TONE_DTMF_8); |
| 519 | keyPressed(KeyEvent.KEYCODE_8); |
| 520 | return; |
| 521 | } |
| 522 | case R.id.nine: { |
| 523 | playTone(ToneGenerator.TONE_DTMF_9); |
| 524 | keyPressed(KeyEvent.KEYCODE_9); |
| 525 | return; |
| 526 | } |
| 527 | case R.id.zero: { |
| 528 | playTone(ToneGenerator.TONE_DTMF_0); |
| 529 | keyPressed(KeyEvent.KEYCODE_0); |
| 530 | return; |
| 531 | } |
| 532 | case R.id.pound: { |
| 533 | playTone(ToneGenerator.TONE_DTMF_P); |
| 534 | keyPressed(KeyEvent.KEYCODE_POUND); |
| 535 | return; |
| 536 | } |
| 537 | case R.id.star: { |
| 538 | playTone(ToneGenerator.TONE_DTMF_S); |
| 539 | keyPressed(KeyEvent.KEYCODE_STAR); |
| 540 | return; |
| 541 | } |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 546 | * called for long touch events |
| 547 | */ |
| 548 | @Override |
| 549 | public boolean onLongClick(View view) { |
| 550 | int id = view.getId(); |
| 551 | switch (id) { |
| 552 | case R.id.deleteButton: { |
| 553 | mDigits.getText().clear(); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 554 | return true; |
| 555 | } |
| 556 | case R.id.zero: { |
| Yorke Lee | 9131166 | 2014-10-24 14:50:45 -0700 | [diff] [blame] | 557 | removePreviousDigitIfPossible(); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 558 | keyPressed(KeyEvent.KEYCODE_PLUS); |
| 559 | return true; |
| 560 | } |
| 561 | } |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | @Override |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 566 | protected void onStart() { |
| 567 | super.onStart(); |
| 568 | |
| 569 | mColorExtractor.addOnColorsChangedListener(this); |
| Lucas Dupin | 94b566f | 2017-05-28 11:45:52 -0700 | [diff] [blame] | 570 | GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK, |
| 571 | ColorExtractor.TYPE_EXTRA_DARK); |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 572 | // Do not animate when view isn't visible yet, just set an initial state. |
| 573 | mBackgroundGradient.setColors(lockScreenColors, false); |
| 574 | updateTheme(lockScreenColors.supportsDarkText()); |
| 575 | } |
| 576 | |
| 577 | @Override |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 578 | protected void onResume() { |
| 579 | super.onResume(); |
| 580 | |
| 581 | // retrieve the DTMF tone play back setting. |
| 582 | mDTMFToneEnabled = Settings.System.getInt(getContentResolver(), |
| 583 | Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1; |
| 584 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 585 | // if the mToneGenerator creation fails, just continue without it. It is |
| 586 | // a local audio signal, and is not as important as the dtmf tone itself. |
| 587 | synchronized (mToneGeneratorLock) { |
| 588 | if (mToneGenerator == null) { |
| 589 | try { |
| 590 | mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF, |
| 591 | TONE_RELATIVE_VOLUME); |
| 592 | } catch (RuntimeException e) { |
| 593 | Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e); |
| 594 | mToneGenerator = null; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 599 | updateDialAndDeleteButtonStateEnabledAttr(); |
| 600 | } |
| 601 | |
| 602 | @Override |
| 603 | public void onPause() { |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 604 | super.onPause(); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 607 | @Override |
| 608 | protected void onStop() { |
| 609 | super.onStop(); |
| 610 | |
| 611 | mColorExtractor.removeOnColorsChangedListener(this); |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Sets theme based on gradient colors |
| 616 | * @param supportsDarkText true if gradient supports dark text |
| 617 | */ |
| 618 | private void updateTheme(boolean supportsDarkText) { |
| 619 | if (mSupportsDarkText == supportsDarkText) { |
| 620 | return; |
| 621 | } |
| 622 | mSupportsDarkText = supportsDarkText; |
| 623 | |
| 624 | // We can't change themes after inflation, in this case we'll have to recreate |
| 625 | // the whole activity. |
| 626 | if (mBackgroundGradient != null) { |
| 627 | recreate(); |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | int vis = getWindow().getDecorView().getSystemUiVisibility(); |
| 632 | if (supportsDarkText) { |
| 633 | vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 634 | vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 635 | setTheme(R.style.EmergencyDialerThemeDark); |
| 636 | } else { |
| 637 | vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 638 | vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 639 | setTheme(R.style.EmergencyDialerTheme); |
| 640 | } |
| 641 | getWindow().getDecorView().setSystemUiVisibility(vis); |
| 642 | } |
| 643 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 644 | /** |
| 645 | * place the call, but check to make sure it is a viable number. |
| 646 | */ |
| 647 | private void placeCall() { |
| 648 | mLastNumber = mDigits.getText().toString(); |
| Wei Huang | 6904b14 | 2017-04-21 19:06:40 +0900 | [diff] [blame] | 649 | |
| 650 | // Convert into emergency number according to emergency conversion map. |
| 651 | // If conversion map is not defined (this is default), this method does |
| 652 | // nothing and just returns input number. |
| 653 | mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber); |
| 654 | |
| Yorke Lee | 36bb254 | 2014-06-05 08:09:52 -0700 | [diff] [blame] | 655 | if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) { |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 656 | if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber); |
| 657 | |
| 658 | // place the call if it is a valid number |
| 659 | if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) { |
| 660 | // There is no number entered. |
| 661 | playTone(ToneGenerator.TONE_PROP_NACK); |
| 662 | return; |
| 663 | } |
| Tyler Gunn | ca7dfef | 2018-03-27 18:50:05 +0000 | [diff] [blame] | 664 | TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE); |
| 665 | tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 666 | } else { |
| 667 | if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber); |
| 668 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 669 | showDialog(BAD_EMERGENCY_NUMBER_DIALOG); |
| 670 | } |
| Yorke Lee | 9b34151 | 2014-10-17 11:36:41 -0700 | [diff] [blame] | 671 | mDigits.getText().delete(0, mDigits.getText().length()); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Plays the specified tone for TONE_LENGTH_MS milliseconds. |
| 676 | * |
| 677 | * The tone is played locally, using the audio stream for phone calls. |
| 678 | * Tones are played only if the "Audible touch tones" user preference |
| 679 | * is checked, and are NOT played if the device is in silent mode. |
| 680 | * |
| 681 | * @param tone a tone code from {@link ToneGenerator} |
| 682 | */ |
| 683 | void playTone(int tone) { |
| 684 | // if local tone playback is disabled, just return. |
| 685 | if (!mDTMFToneEnabled) { |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | // Also do nothing if the phone is in silent mode. |
| 690 | // We need to re-check the ringer mode for *every* playTone() |
| 691 | // call, rather than keeping a local flag that's updated in |
| 692 | // onResume(), since it's possible to toggle silent mode without |
| 693 | // leaving the current activity (via the ENDCALL-longpress menu.) |
| 694 | AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
| 695 | int ringerMode = audioManager.getRingerMode(); |
| 696 | if ((ringerMode == AudioManager.RINGER_MODE_SILENT) |
| 697 | || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) { |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | synchronized (mToneGeneratorLock) { |
| 702 | if (mToneGenerator == null) { |
| 703 | Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone); |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | // Start the new tone (will stop any playing tone) |
| 708 | mToneGenerator.startTone(tone, TONE_LENGTH_MS); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | private CharSequence createErrorMessage(String number) { |
| 713 | if (!TextUtils.isEmpty(number)) { |
| Hall Liu | dc27431 | 2016-03-01 16:34:45 -0800 | [diff] [blame] | 714 | String errorString = getString(R.string.dial_emergency_error, number); |
| 715 | int startingPosition = errorString.indexOf(number); |
| 716 | int endingPosition = startingPosition + number.length(); |
| 717 | Spannable result = new SpannableString(errorString); |
| 718 | PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition); |
| 719 | return result; |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 720 | } else { |
| 721 | return getText(R.string.dial_emergency_empty_error).toString(); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | @Override |
| 726 | protected Dialog onCreateDialog(int id) { |
| 727 | AlertDialog dialog = null; |
| 728 | if (id == BAD_EMERGENCY_NUMBER_DIALOG) { |
| 729 | // construct dialog |
| Lucas Dupin | 0d666f9 | 2017-06-01 14:04:48 -0700 | [diff] [blame] | 730 | dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme) |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 731 | .setTitle(getText(R.string.emergency_enable_radio_dialog_title)) |
| 732 | .setMessage(createErrorMessage(mLastNumber)) |
| 733 | .setPositiveButton(R.string.ok, null) |
| 734 | .setCancelable(true).create(); |
| 735 | |
| 736 | // blur stuff behind the dialog |
| 737 | dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
| Tyler Gunn | 7563860 | 2018-03-29 13:48:29 -0700 | [diff] [blame] | 738 | setShowWhenLocked(true); |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 739 | } |
| 740 | return dialog; |
| 741 | } |
| 742 | |
| 743 | @Override |
| 744 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 745 | super.onPrepareDialog(id, dialog); |
| 746 | if (id == BAD_EMERGENCY_NUMBER_DIALOG) { |
| 747 | AlertDialog alert = (AlertDialog) dialog; |
| 748 | alert.setMessage(createErrorMessage(mLastNumber)); |
| 749 | } |
| 750 | } |
| 751 | |
| Andrew Lee | d5631e8 | 2014-10-08 16:03:58 -0700 | [diff] [blame] | 752 | @Override |
| 753 | public boolean onOptionsItemSelected(MenuItem item) { |
| 754 | final int itemId = item.getItemId(); |
| 755 | if (itemId == android.R.id.home) { |
| 756 | onBackPressed(); |
| 757 | return true; |
| 758 | } |
| 759 | return super.onOptionsItemSelected(item); |
| 760 | } |
| 761 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 762 | /** |
| 763 | * Update the enabledness of the "Dial" and "Backspace" buttons if applicable. |
| 764 | */ |
| 765 | private void updateDialAndDeleteButtonStateEnabledAttr() { |
| 766 | final boolean notEmpty = mDigits.length() != 0; |
| 767 | |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 768 | mDelete.setEnabled(notEmpty); |
| 769 | } |
| Yorke Lee | 9131166 | 2014-10-24 14:50:45 -0700 | [diff] [blame] | 770 | |
| 771 | /** |
| 772 | * Remove the digit just before the current position. Used by various long pressed callbacks |
| 773 | * to remove the digit that was populated as a result of the short click. |
| 774 | */ |
| 775 | private void removePreviousDigitIfPossible() { |
| 776 | final int currentPosition = mDigits.getSelectionStart(); |
| 777 | if (currentPosition > 0) { |
| 778 | mDigits.setSelection(currentPosition); |
| 779 | mDigits.getText().delete(currentPosition - 1, currentPosition); |
| 780 | } |
| 781 | } |
| Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 782 | |
| 783 | /** |
| 784 | * Update the text-to-speech annotations in the edit field. |
| 785 | */ |
| 786 | private void updateTtsSpans() { |
| 787 | for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) { |
| 788 | mDigits.getText().removeSpan(o); |
| 789 | } |
| 790 | PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length()); |
| 791 | } |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 792 | |
| 793 | @Override |
| Lucas Dupin | 94b566f | 2017-05-28 11:45:52 -0700 | [diff] [blame] | 794 | public void onColorsChanged(ColorExtractor extractor, int which) { |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 795 | if ((which & WallpaperManager.FLAG_LOCK) != 0) { |
| Lucas Dupin | 94b566f | 2017-05-28 11:45:52 -0700 | [diff] [blame] | 796 | GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK, |
| 797 | ColorExtractor.TYPE_EXTRA_DARK); |
| Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 798 | mBackgroundGradient.setColors(colors); |
| 799 | updateTheme(colors.supportsDarkText()); |
| 800 | } |
| 801 | } |
| Tyler Gunn | 1acf54a | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 802 | |
| 803 | /** |
| 804 | * Where a carrier requires a warning that emergency calling is not available while on WFC, |
| 805 | * add hint text above the dial pad which warns the user of this case. |
| 806 | */ |
| 807 | private void maybeShowWfcEmergencyCallingWarning() { |
| 808 | if (!mIsWfcEmergencyCallingWarningEnabled) { |
| 809 | Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier."); |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | // Use an async task rather than calling into Telephony on UI thread. |
| 814 | AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() { |
| 815 | @Override |
| 816 | protected Boolean doInBackground(Void... voids) { |
| 817 | TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); |
| 818 | boolean isWfcAvailable = tm.isWifiCallingAvailable(); |
| 819 | ServiceState ss = tm.getServiceState(); |
| 820 | boolean isCellAvailable = |
| 821 | ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN; |
| 822 | Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable |
| 823 | + " isCellAvailable=" + isCellAvailable |
| 824 | + "(rat=" + ss.getRilVoiceRadioTechnology() + ")"); |
| 825 | return isWfcAvailable && !isCellAvailable; |
| 826 | } |
| 827 | |
| 828 | @Override |
| 829 | protected void onPostExecute(Boolean result) { |
| 830 | if (result.booleanValue()) { |
| 831 | Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning"); |
| 832 | mDigits.setHint(R.string.dial_emergency_calling_not_available); |
| 833 | } else { |
| 834 | Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning"); |
| 835 | mDigits.setHint(""); |
| 836 | } |
| 837 | maybeChangeHintSize(); |
| 838 | } |
| 839 | }; |
| 840 | showWfcWarningTask.execute((Void) null); |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits |
| 845 | * edit view and set the font size to a smaller size appropriate for the emergency calling |
| 846 | * warning. |
| 847 | */ |
| 848 | private void maybeChangeHintSize() { |
| 849 | if (TextUtils.isEmpty(mDigits.getHint()) |
| 850 | || !TextUtils.isEmpty(mDigits.getText().toString())) { |
| 851 | // No hint or there are dialed digits, so use default size. |
| 852 | mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize); |
| 853 | // By default, the digits view auto-resizes to fit the text it contains, so |
| 854 | // enable that now. |
| 855 | mDigits.setResizeEnabled(true); |
| 856 | Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize()); |
| 857 | } else { |
| 858 | // Hint present and no dialed digits, set custom font size appropriate for the warning. |
| 859 | mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize( |
| 860 | R.dimen.emergency_call_warning_size)); |
| 861 | // Since we're populating this with a static text string, disable auto-resize. |
| 862 | mDigits.setResizeEnabled(false); |
| 863 | Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize()); |
| 864 | } |
| 865 | } |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 866 | |
| 867 | private void setupEmergencyShortcutsView() { |
| 868 | mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts); |
| 869 | mDialpadView = findViewById(R.id.emergency_dialer); |
| 870 | |
| 871 | final View dialpadButton = findViewById(R.id.floating_action_button_dialpad); |
| 872 | dialpadButton.setOnClickListener(this); |
| 873 | |
| 874 | final View emergencyInfoButton = findViewById(R.id.emergency_info_button); |
| 875 | emergencyInfoButton.setOnClickListener(this); |
| 876 | |
| 877 | // EmergencyActionGroup is replaced by EmergencyInfoGroup. |
| 878 | mEmergencyActionGroup.setVisibility(View.GONE); |
| 879 | |
| 880 | // Setup dialpad title. |
| 881 | final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container); |
| 882 | emergencyDialpadTitle.setVisibility(View.VISIBLE); |
| 883 | |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 884 | // TODO: Integrating emergency phone number table will get location information. |
| 885 | // Using null to present no location status. |
| 886 | setLocationInfo(null); |
| 887 | |
| 888 | mEmergencyShortcutButtonList = new ArrayList<>(); |
| 889 | setupEmergencyCallShortcutButton(); |
| 890 | |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 891 | switchView(mEmergencyShortcutView, mDialpadView, false); |
| 892 | } |
| 893 | |
| Chihhang Chuang | 4f2b781 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 894 | private void setLocationInfo(String country) { |
| 895 | final View locationInfo = findViewById(R.id.location_info); |
| 896 | |
| 897 | if (TextUtils.isEmpty(country)) { |
| 898 | locationInfo.setVisibility(View.INVISIBLE); |
| 899 | } else { |
| 900 | final TextView location = (TextView) locationInfo.findViewById(R.id.location_text); |
| 901 | location.setText(country); |
| 902 | locationInfo.setVisibility(View.VISIBLE); |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | // TODO: Integrate emergency phone number table. |
| 907 | // Using default layout(no location, phone number is 112, description is Emergency, |
| 908 | // and icon is cross shape) until integrating emergency phone number table. |
| 909 | private void setupEmergencyCallShortcutButton() { |
| 910 | final ViewGroup shortcutButtonContainer = findViewById( |
| 911 | R.id.emergency_shortcut_buttons_container); |
| 912 | shortcutButtonContainer.setClipToOutline(true); |
| 913 | |
| 914 | final EmergencyShortcutButton button = |
| 915 | (EmergencyShortcutButton) getLayoutInflater().inflate( |
| 916 | R.layout.emergency_shortcut_button, |
| 917 | shortcutButtonContainer, false); |
| 918 | |
| 919 | button.setPhoneNumber("112"); |
| 920 | button.setPhoneDescription("Emergency"); |
| 921 | button.setPhoneTypeIcon(R.drawable.ic_emergency_number_24); |
| 922 | button.setOnConfirmClickListener(this); |
| 923 | |
| 924 | shortcutButtonContainer.addView(button); |
| 925 | mEmergencyShortcutButtonList.add(button); |
| 926 | |
| 927 | //Set emergency number title for numerous buttons. |
| 928 | if (shortcutButtonContainer.getChildCount() > 1) { |
| 929 | final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title); |
| 930 | emergencyNumberTitle.setText(getString(R.string.numerous_emergency_numbers_title)); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Called by the activity before a touch event is dispatched to the view hierarchy. |
| 936 | */ |
| 937 | private void onPreTouchEvent(MotionEvent event) { |
| 938 | mEmergencyActionGroup.onPreTouchEvent(event); |
| 939 | |
| 940 | if (mEmergencyShortcutButtonList != null) { |
| 941 | for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) { |
| 942 | button.onPreTouchEvent(event); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * Called by the activity after a touch event is dispatched to the view hierarchy. |
| 949 | */ |
| 950 | private void onPostTouchEvent(MotionEvent event) { |
| 951 | mEmergencyActionGroup.onPostTouchEvent(event); |
| 952 | |
| 953 | if (mEmergencyShortcutButtonList != null) { |
| 954 | for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) { |
| 955 | button.onPostTouchEvent(event); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| Chihhang Chuang | d301d9a | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 960 | /** |
| 961 | * Switch two view. |
| 962 | * |
| 963 | * @param displayView the view would be displayed. |
| 964 | * @param hideView the view would be hidden. |
| 965 | * @param hasAnimation is {@code true} when the view should be displayed with animation. |
| 966 | */ |
| 967 | private void switchView(View displayView, View hideView, boolean hasAnimation) { |
| 968 | if (displayView == null || hideView == null) { |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | if (displayView.getVisibility() == View.VISIBLE) { |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | if (hasAnimation) { |
| 977 | crossfade(hideView, displayView); |
| 978 | } else { |
| 979 | hideView.setVisibility(View.GONE); |
| 980 | displayView.setVisibility(View.VISIBLE); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * Fade out and fade in animation between two view transition. |
| 986 | */ |
| 987 | private void crossfade(View fadeOutView, View fadeInView) { |
| 988 | if (fadeOutView == null || fadeInView == null) { |
| 989 | return; |
| 990 | } |
| 991 | final int shortAnimationDuration = getResources().getInteger( |
| 992 | android.R.integer.config_shortAnimTime); |
| 993 | |
| 994 | fadeInView.setAlpha(0f); |
| 995 | fadeInView.setVisibility(View.VISIBLE); |
| 996 | |
| 997 | fadeInView.animate() |
| 998 | .alpha(1f) |
| 999 | .setDuration(shortAnimationDuration) |
| 1000 | .setListener(null); |
| 1001 | |
| 1002 | fadeOutView.animate() |
| 1003 | .alpha(0f) |
| 1004 | .setDuration(shortAnimationDuration) |
| 1005 | .setListener(new AnimatorListenerAdapter() { |
| 1006 | @Override |
| 1007 | public void onAnimationEnd(Animator animation) { |
| 1008 | fadeOutView.setVisibility(View.GONE); |
| 1009 | } |
| 1010 | }); |
| 1011 | } |
| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1012 | } |