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