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