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