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