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 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.app.Dialog; |
| 22 | import android.app.StatusBarManager; |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.content.res.Resources; |
| 28 | import android.media.AudioManager; |
| 29 | import android.media.ToneGenerator; |
| 30 | import android.net.Uri; |
| 31 | import android.os.Bundle; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 32 | import android.os.PersistableBundle; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 33 | import android.provider.Settings; |
Tyler Gunn | 4d45d1c | 2014-09-12 22:17:53 -0700 | [diff] [blame] | 34 | import android.telecom.PhoneAccount; |
Jonathan Basseri | 3649bdb | 2015-04-30 22:39:11 -0700 | [diff] [blame] | 35 | import android.telephony.CarrierConfigManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 36 | import android.telephony.PhoneNumberUtils; |
Santos Cordon | e137eed | 2015-06-23 15:34:47 -0700 | [diff] [blame] | 37 | import android.telephony.SubscriptionManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 38 | import android.text.Editable; |
| 39 | import android.text.TextUtils; |
| 40 | import android.text.TextWatcher; |
| 41 | import android.text.method.DialerKeyListener; |
Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 42 | import android.text.style.TtsSpan; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 43 | import android.util.Log; |
Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 44 | import android.view.HapticFeedbackConstants; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 45 | import android.view.KeyEvent; |
Andrew Lee | d5631e8 | 2014-10-08 16:03:58 -0700 | [diff] [blame] | 46 | import android.view.MenuItem; |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 47 | import android.view.MotionEvent; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 48 | import android.view.View; |
| 49 | import android.view.WindowManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 50 | import android.widget.EditText; |
| 51 | |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 52 | import com.android.phone.common.dialpad.DialpadKeyButton; |
Sai Cheemalapati | 14462b6 | 2014-06-18 13:53:56 -0700 | [diff] [blame] | 53 | import com.android.phone.common.util.ViewUtil; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | /** |
| 57 | * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls. |
| 58 | * |
| 59 | * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer |
| 60 | * activity from apps/Contacts) that: |
| 61 | * 1. Allows ONLY emergency calls to be dialed |
| 62 | * 2. Disallows voicemail functionality |
| 63 | * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this |
| 64 | * activity to stay in front of the keyguard. |
| 65 | * |
| 66 | * TODO: Even though this is an ultra-simplified version of the normal |
| 67 | * dialer, there's still lots of code duplication between this class and |
| 68 | * the TwelveKeyDialer class from apps/Contacts. Could the common code be |
| 69 | * moved into a shared base class that would live in the framework? |
| 70 | * Or could we figure out some way to move *this* class into apps/Contacts |
| 71 | * also? |
| 72 | */ |
| 73 | public class EmergencyDialer extends Activity implements View.OnClickListener, |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 74 | View.OnLongClickListener, View.OnKeyListener, TextWatcher, |
| 75 | DialpadKeyButton.OnPressedListener { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 76 | // Keys used with onSaveInstanceState(). |
| 77 | private static final String LAST_NUMBER = "lastNumber"; |
| 78 | |
| 79 | // Intent action for this activity. |
| 80 | public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL"; |
| 81 | |
| 82 | // List of dialer button IDs. |
| 83 | private static final int[] DIALER_KEYS = new int[] { |
| 84 | R.id.one, R.id.two, R.id.three, |
| 85 | R.id.four, R.id.five, R.id.six, |
| 86 | R.id.seven, R.id.eight, R.id.nine, |
| 87 | R.id.star, R.id.zero, R.id.pound }; |
| 88 | |
| 89 | // Debug constants. |
| 90 | private static final boolean DBG = false; |
| 91 | private static final String LOG_TAG = "EmergencyDialer"; |
| 92 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 93 | private StatusBarManager mStatusBarManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 94 | |
| 95 | /** The length of DTMF tones in milliseconds */ |
| 96 | private static final int TONE_LENGTH_MS = 150; |
| 97 | |
| 98 | /** The DTMF tone volume relative to other sounds in the stream */ |
| 99 | private static final int TONE_RELATIVE_VOLUME = 80; |
| 100 | |
| 101 | /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */ |
| 102 | private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF; |
| 103 | |
| 104 | private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0; |
| 105 | |
Santos Cordon | fc30981 | 2013-08-20 18:33:16 -0700 | [diff] [blame] | 106 | // private static final int USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR = 15000; // millis |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 107 | |
| 108 | EditText mDigits; |
| 109 | private View mDialButton; |
| 110 | private View mDelete; |
| 111 | |
| 112 | private ToneGenerator mToneGenerator; |
| 113 | private Object mToneGeneratorLock = new Object(); |
| 114 | |
| 115 | // determines if we want to playback local DTMF tones. |
| 116 | private boolean mDTMFToneEnabled; |
| 117 | |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 118 | private EmergencyActionGroup mEmergencyActionGroup; |
| 119 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 120 | // close activity when screen turns off |
| 121 | private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 122 | @Override |
| 123 | public void onReceive(Context context, Intent intent) { |
| 124 | if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { |
Adrian Roos | 061c723 | 2015-04-21 12:37:07 -0700 | [diff] [blame] | 125 | finishAndRemoveTask(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | private String mLastNumber; // last number we tried to dial. Used to restore error dialog. |
| 131 | |
| 132 | @Override |
| 133 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 134 | // Do nothing |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onTextChanged(CharSequence input, int start, int before, int changeCount) { |
| 139 | // Do nothing |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void afterTextChanged(Editable input) { |
| 144 | // Check for special sequences, in particular the "**04" or "**05" |
| 145 | // sequences that allow you to enter PIN or PUK-related codes. |
| 146 | // |
| 147 | // But note we *don't* allow most other special sequences here, |
| 148 | // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"), |
| 149 | // since those shouldn't be available if the device is locked. |
| 150 | // |
| 151 | // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice() |
| 152 | // here, not the regular handleChars() method. |
| 153 | if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) { |
| 154 | // A special sequence was entered, clear the digits |
| 155 | mDigits.getText().clear(); |
| 156 | } |
| 157 | |
| 158 | updateDialAndDeleteButtonStateEnabledAttr(); |
Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 159 | updateTtsSpans(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected void onCreate(Bundle icicle) { |
| 164 | super.onCreate(icicle); |
| 165 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 166 | mStatusBarManager = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 167 | |
| 168 | // Allow this activity to be displayed in front of the keyguard / lockscreen. |
| 169 | WindowManager.LayoutParams lp = getWindow().getAttributes(); |
| 170 | lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; |
Santos Cordon | fc30981 | 2013-08-20 18:33:16 -0700 | [diff] [blame] | 171 | |
| 172 | // When no proximity sensor is available, use a shorter timeout. |
Christine Chen | 07fae16 | 2013-09-19 15:05:56 -0700 | [diff] [blame] | 173 | // TODO: Do we enable this for non proximity devices any more? |
Santos Cordon | fc30981 | 2013-08-20 18:33:16 -0700 | [diff] [blame] | 174 | // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR; |
| 175 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 176 | getWindow().setAttributes(lp); |
| 177 | |
| 178 | setContentView(R.layout.emergency_dialer); |
| 179 | |
| 180 | mDigits = (EditText) findViewById(R.id.digits); |
| 181 | mDigits.setKeyListener(DialerKeyListener.getInstance()); |
| 182 | mDigits.setOnClickListener(this); |
| 183 | mDigits.setOnKeyListener(this); |
| 184 | mDigits.setLongClickable(false); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 185 | maybeAddNumberFormatting(); |
| 186 | |
| 187 | // Check for the presence of the keypad |
| 188 | View view = findViewById(R.id.one); |
| 189 | if (view != null) { |
| 190 | setupKeypad(); |
| 191 | } |
| 192 | |
| 193 | mDelete = findViewById(R.id.deleteButton); |
| 194 | mDelete.setOnClickListener(this); |
| 195 | mDelete.setOnLongClickListener(this); |
| 196 | |
Sai Cheemalapati | 14462b6 | 2014-06-18 13:53:56 -0700 | [diff] [blame] | 197 | mDialButton = findViewById(R.id.floating_action_button); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 198 | |
| 199 | // Check whether we should show the onscreen "Dial" button and co. |
Jonathan Basseri | e619a4c | 2015-06-26 14:52:26 -0700 | [diff] [blame] | 200 | // Read carrier config through the public API because PhoneGlobals is not available when we |
| 201 | // run as a secondary user. |
| 202 | CarrierConfigManager configMgr = |
| 203 | (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE); |
| 204 | PersistableBundle carrierConfig = |
Shishir Agrawal | d3480e0 | 2016-01-25 13:05:49 -0800 | [diff] [blame^] | 205 | configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId()); |
Jonathan Basseri | 9504c6b | 2015-06-04 14:23:32 -0700 | [diff] [blame] | 206 | if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 207 | mDialButton.setOnClickListener(this); |
| 208 | } else { |
| 209 | mDialButton.setVisibility(View.GONE); |
| 210 | } |
Adrian Roos | c9fdb6c | 2015-05-25 15:10:21 -0700 | [diff] [blame] | 211 | ViewUtil.setupFloatingActionButton(mDialButton, getResources()); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 212 | |
| 213 | if (icicle != null) { |
| 214 | super.onRestoreInstanceState(icicle); |
| 215 | } |
| 216 | |
| 217 | // Extract phone number from intent |
| 218 | Uri data = getIntent().getData(); |
Jay Shrauner | 137458b | 2014-09-05 14:27:25 -0700 | [diff] [blame] | 219 | if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 220 | String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this); |
| 221 | if (number != null) { |
| 222 | mDigits.setText(number); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // if the mToneGenerator creation fails, just continue without it. It is |
| 227 | // a local audio signal, and is not as important as the dtmf tone itself. |
| 228 | synchronized (mToneGeneratorLock) { |
| 229 | if (mToneGenerator == null) { |
| 230 | try { |
| 231 | mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME); |
| 232 | } catch (RuntimeException e) { |
| 233 | Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e); |
| 234 | mToneGenerator = null; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | final IntentFilter intentFilter = new IntentFilter(); |
| 240 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 241 | registerReceiver(mBroadcastReceiver, intentFilter); |
| 242 | |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 243 | mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
| 247 | protected void onDestroy() { |
| 248 | super.onDestroy(); |
| 249 | synchronized (mToneGeneratorLock) { |
| 250 | if (mToneGenerator != null) { |
| 251 | mToneGenerator.release(); |
| 252 | mToneGenerator = null; |
| 253 | } |
| 254 | } |
| 255 | unregisterReceiver(mBroadcastReceiver); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | protected void onRestoreInstanceState(Bundle icicle) { |
| 260 | mLastNumber = icicle.getString(LAST_NUMBER); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | protected void onSaveInstanceState(Bundle outState) { |
| 265 | super.onSaveInstanceState(outState); |
| 266 | outState.putString(LAST_NUMBER, mLastNumber); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Explicitly turn off number formatting, since it gets in the way of the emergency |
| 271 | * number detector |
| 272 | */ |
| 273 | protected void maybeAddNumberFormatting() { |
| 274 | // Do nothing. |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | protected void onPostCreate(Bundle savedInstanceState) { |
| 279 | super.onPostCreate(savedInstanceState); |
| 280 | |
| 281 | // This can't be done in onCreate(), since the auto-restoring of the digits |
| 282 | // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState() |
| 283 | // is called. This method will be called every time the activity is created, and |
| 284 | // will always happen after onRestoreSavedInstanceState(). |
| 285 | mDigits.addTextChangedListener(this); |
| 286 | } |
| 287 | |
| 288 | private void setupKeypad() { |
| 289 | // Setup the listeners for the buttons |
| 290 | for (int id : DIALER_KEYS) { |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 291 | final DialpadKeyButton key = (DialpadKeyButton) findViewById(id); |
| 292 | key.setOnPressedListener(this); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | View view = findViewById(R.id.zero); |
| 296 | view.setOnLongClickListener(this); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * handle key events |
| 301 | */ |
| 302 | @Override |
| 303 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 304 | switch (keyCode) { |
| 305 | // Happen when there's a "Call" hard button. |
| 306 | case KeyEvent.KEYCODE_CALL: { |
| 307 | if (TextUtils.isEmpty(mDigits.getText().toString())) { |
| 308 | // if we are adding a call from the InCallScreen and the phone |
| 309 | // number entered is empty, we just close the dialer to expose |
| 310 | // the InCallScreen under it. |
| 311 | finish(); |
| 312 | } else { |
| 313 | // otherwise, we place the call. |
| 314 | placeCall(); |
| 315 | } |
| 316 | return true; |
| 317 | } |
| 318 | } |
| 319 | return super.onKeyDown(keyCode, event); |
| 320 | } |
| 321 | |
| 322 | private void keyPressed(int keyCode) { |
Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 323 | mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 324 | KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); |
| 325 | mDigits.onKeyDown(keyCode, event); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public boolean onKey(View view, int keyCode, KeyEvent event) { |
| 330 | switch (view.getId()) { |
| 331 | case R.id.digits: |
| 332 | // Happen when "Done" button of the IME is pressed. This can happen when this |
| 333 | // Activity is forced into landscape mode due to a desk dock. |
| 334 | if (keyCode == KeyEvent.KEYCODE_ENTER |
| 335 | && event.getAction() == KeyEvent.ACTION_UP) { |
| 336 | placeCall(); |
| 337 | return true; |
| 338 | } |
| 339 | break; |
| 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | @Override |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 345 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 346 | mEmergencyActionGroup.onPreTouchEvent(ev); |
| 347 | boolean handled = super.dispatchTouchEvent(ev); |
| 348 | mEmergencyActionGroup.onPostTouchEvent(ev); |
| 349 | return handled; |
| 350 | } |
| 351 | |
| 352 | @Override |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 353 | public void onClick(View view) { |
| 354 | switch (view.getId()) { |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 355 | case R.id.deleteButton: { |
| 356 | keyPressed(KeyEvent.KEYCODE_DEL); |
| 357 | return; |
| 358 | } |
| 359 | case R.id.floating_action_button: { |
Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 360 | view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 361 | placeCall(); |
| 362 | return; |
| 363 | } |
| 364 | case R.id.digits: { |
| 365 | if (mDigits.length() != 0) { |
| 366 | mDigits.setCursorVisible(true); |
| 367 | } |
| 368 | return; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public void onPressed(View view, boolean pressed) { |
| 375 | if (!pressed) { |
| 376 | return; |
| 377 | } |
| 378 | switch (view.getId()) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 379 | case R.id.one: { |
| 380 | playTone(ToneGenerator.TONE_DTMF_1); |
| 381 | keyPressed(KeyEvent.KEYCODE_1); |
| 382 | return; |
| 383 | } |
| 384 | case R.id.two: { |
| 385 | playTone(ToneGenerator.TONE_DTMF_2); |
| 386 | keyPressed(KeyEvent.KEYCODE_2); |
| 387 | return; |
| 388 | } |
| 389 | case R.id.three: { |
| 390 | playTone(ToneGenerator.TONE_DTMF_3); |
| 391 | keyPressed(KeyEvent.KEYCODE_3); |
| 392 | return; |
| 393 | } |
| 394 | case R.id.four: { |
| 395 | playTone(ToneGenerator.TONE_DTMF_4); |
| 396 | keyPressed(KeyEvent.KEYCODE_4); |
| 397 | return; |
| 398 | } |
| 399 | case R.id.five: { |
| 400 | playTone(ToneGenerator.TONE_DTMF_5); |
| 401 | keyPressed(KeyEvent.KEYCODE_5); |
| 402 | return; |
| 403 | } |
| 404 | case R.id.six: { |
| 405 | playTone(ToneGenerator.TONE_DTMF_6); |
| 406 | keyPressed(KeyEvent.KEYCODE_6); |
| 407 | return; |
| 408 | } |
| 409 | case R.id.seven: { |
| 410 | playTone(ToneGenerator.TONE_DTMF_7); |
| 411 | keyPressed(KeyEvent.KEYCODE_7); |
| 412 | return; |
| 413 | } |
| 414 | case R.id.eight: { |
| 415 | playTone(ToneGenerator.TONE_DTMF_8); |
| 416 | keyPressed(KeyEvent.KEYCODE_8); |
| 417 | return; |
| 418 | } |
| 419 | case R.id.nine: { |
| 420 | playTone(ToneGenerator.TONE_DTMF_9); |
| 421 | keyPressed(KeyEvent.KEYCODE_9); |
| 422 | return; |
| 423 | } |
| 424 | case R.id.zero: { |
| 425 | playTone(ToneGenerator.TONE_DTMF_0); |
| 426 | keyPressed(KeyEvent.KEYCODE_0); |
| 427 | return; |
| 428 | } |
| 429 | case R.id.pound: { |
| 430 | playTone(ToneGenerator.TONE_DTMF_P); |
| 431 | keyPressed(KeyEvent.KEYCODE_POUND); |
| 432 | return; |
| 433 | } |
| 434 | case R.id.star: { |
| 435 | playTone(ToneGenerator.TONE_DTMF_S); |
| 436 | keyPressed(KeyEvent.KEYCODE_STAR); |
| 437 | return; |
| 438 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 443 | * called for long touch events |
| 444 | */ |
| 445 | @Override |
| 446 | public boolean onLongClick(View view) { |
| 447 | int id = view.getId(); |
| 448 | switch (id) { |
| 449 | case R.id.deleteButton: { |
| 450 | mDigits.getText().clear(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 451 | return true; |
| 452 | } |
| 453 | case R.id.zero: { |
Yorke Lee | 9131166 | 2014-10-24 14:50:45 -0700 | [diff] [blame] | 454 | removePreviousDigitIfPossible(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 455 | keyPressed(KeyEvent.KEYCODE_PLUS); |
| 456 | return true; |
| 457 | } |
| 458 | } |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | protected void onResume() { |
| 464 | super.onResume(); |
| 465 | |
| 466 | // retrieve the DTMF tone play back setting. |
| 467 | mDTMFToneEnabled = Settings.System.getInt(getContentResolver(), |
| 468 | Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1; |
| 469 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 470 | // if the mToneGenerator creation fails, just continue without it. It is |
| 471 | // a local audio signal, and is not as important as the dtmf tone itself. |
| 472 | synchronized (mToneGeneratorLock) { |
| 473 | if (mToneGenerator == null) { |
| 474 | try { |
| 475 | mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF, |
| 476 | TONE_RELATIVE_VOLUME); |
| 477 | } catch (RuntimeException e) { |
| 478 | Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e); |
| 479 | mToneGenerator = null; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Disable the status bar and set the poke lock timeout to medium. |
| 485 | // There is no need to do anything with the wake lock. |
| 486 | if (DBG) Log.d(LOG_TAG, "disabling status bar, set to long timeout"); |
| 487 | mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND); |
| 488 | |
| 489 | updateDialAndDeleteButtonStateEnabledAttr(); |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public void onPause() { |
| 494 | // Reenable the status bar and set the poke lock timeout to default. |
| 495 | // There is no need to do anything with the wake lock. |
| 496 | if (DBG) Log.d(LOG_TAG, "reenabling status bar and closing the dialer"); |
| 497 | mStatusBarManager.disable(StatusBarManager.DISABLE_NONE); |
| 498 | |
| 499 | super.onPause(); |
| 500 | |
| 501 | synchronized (mToneGeneratorLock) { |
| 502 | if (mToneGenerator != null) { |
| 503 | mToneGenerator.release(); |
| 504 | mToneGenerator = null; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * place the call, but check to make sure it is a viable number. |
| 511 | */ |
| 512 | private void placeCall() { |
| 513 | mLastNumber = mDigits.getText().toString(); |
Yorke Lee | 36bb254 | 2014-06-05 08:09:52 -0700 | [diff] [blame] | 514 | if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 515 | if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber); |
| 516 | |
| 517 | // place the call if it is a valid number |
| 518 | if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) { |
| 519 | // There is no number entered. |
| 520 | playTone(ToneGenerator.TONE_PROP_NACK); |
| 521 | return; |
| 522 | } |
| 523 | Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY); |
Jay Shrauner | 137458b | 2014-09-05 14:27:25 -0700 | [diff] [blame] | 524 | intent.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null)); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 525 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 526 | startActivity(intent); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 527 | } else { |
| 528 | if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber); |
| 529 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 530 | showDialog(BAD_EMERGENCY_NUMBER_DIALOG); |
| 531 | } |
Yorke Lee | 9b34151 | 2014-10-17 11:36:41 -0700 | [diff] [blame] | 532 | mDigits.getText().delete(0, mDigits.getText().length()); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Plays the specified tone for TONE_LENGTH_MS milliseconds. |
| 537 | * |
| 538 | * The tone is played locally, using the audio stream for phone calls. |
| 539 | * Tones are played only if the "Audible touch tones" user preference |
| 540 | * is checked, and are NOT played if the device is in silent mode. |
| 541 | * |
| 542 | * @param tone a tone code from {@link ToneGenerator} |
| 543 | */ |
| 544 | void playTone(int tone) { |
| 545 | // if local tone playback is disabled, just return. |
| 546 | if (!mDTMFToneEnabled) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | // Also do nothing if the phone is in silent mode. |
| 551 | // We need to re-check the ringer mode for *every* playTone() |
| 552 | // call, rather than keeping a local flag that's updated in |
| 553 | // onResume(), since it's possible to toggle silent mode without |
| 554 | // leaving the current activity (via the ENDCALL-longpress menu.) |
| 555 | AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
| 556 | int ringerMode = audioManager.getRingerMode(); |
| 557 | if ((ringerMode == AudioManager.RINGER_MODE_SILENT) |
| 558 | || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) { |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | synchronized (mToneGeneratorLock) { |
| 563 | if (mToneGenerator == null) { |
| 564 | Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | // Start the new tone (will stop any playing tone) |
| 569 | mToneGenerator.startTone(tone, TONE_LENGTH_MS); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | private CharSequence createErrorMessage(String number) { |
| 574 | if (!TextUtils.isEmpty(number)) { |
| 575 | return getString(R.string.dial_emergency_error, mLastNumber); |
| 576 | } else { |
| 577 | return getText(R.string.dial_emergency_empty_error).toString(); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | protected Dialog onCreateDialog(int id) { |
| 583 | AlertDialog dialog = null; |
| 584 | if (id == BAD_EMERGENCY_NUMBER_DIALOG) { |
| 585 | // construct dialog |
| 586 | dialog = new AlertDialog.Builder(this) |
| 587 | .setTitle(getText(R.string.emergency_enable_radio_dialog_title)) |
| 588 | .setMessage(createErrorMessage(mLastNumber)) |
| 589 | .setPositiveButton(R.string.ok, null) |
| 590 | .setCancelable(true).create(); |
| 591 | |
| 592 | // blur stuff behind the dialog |
| 593 | dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
Yorke Lee | c30f00c | 2014-07-31 16:09:05 -0700 | [diff] [blame] | 594 | dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 595 | } |
| 596 | return dialog; |
| 597 | } |
| 598 | |
| 599 | @Override |
| 600 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 601 | super.onPrepareDialog(id, dialog); |
| 602 | if (id == BAD_EMERGENCY_NUMBER_DIALOG) { |
| 603 | AlertDialog alert = (AlertDialog) dialog; |
| 604 | alert.setMessage(createErrorMessage(mLastNumber)); |
| 605 | } |
| 606 | } |
| 607 | |
Andrew Lee | d5631e8 | 2014-10-08 16:03:58 -0700 | [diff] [blame] | 608 | @Override |
| 609 | public boolean onOptionsItemSelected(MenuItem item) { |
| 610 | final int itemId = item.getItemId(); |
| 611 | if (itemId == android.R.id.home) { |
| 612 | onBackPressed(); |
| 613 | return true; |
| 614 | } |
| 615 | return super.onOptionsItemSelected(item); |
| 616 | } |
| 617 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 618 | /** |
| 619 | * Update the enabledness of the "Dial" and "Backspace" buttons if applicable. |
| 620 | */ |
| 621 | private void updateDialAndDeleteButtonStateEnabledAttr() { |
| 622 | final boolean notEmpty = mDigits.length() != 0; |
| 623 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 624 | mDelete.setEnabled(notEmpty); |
| 625 | } |
Yorke Lee | 9131166 | 2014-10-24 14:50:45 -0700 | [diff] [blame] | 626 | |
| 627 | /** |
| 628 | * Remove the digit just before the current position. Used by various long pressed callbacks |
| 629 | * to remove the digit that was populated as a result of the short click. |
| 630 | */ |
| 631 | private void removePreviousDigitIfPossible() { |
| 632 | final int currentPosition = mDigits.getSelectionStart(); |
| 633 | if (currentPosition > 0) { |
| 634 | mDigits.setSelection(currentPosition); |
| 635 | mDigits.getText().delete(currentPosition - 1, currentPosition); |
| 636 | } |
| 637 | } |
Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 638 | |
| 639 | /** |
| 640 | * Update the text-to-speech annotations in the edit field. |
| 641 | */ |
| 642 | private void updateTtsSpans() { |
| 643 | for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) { |
| 644 | mDigits.getText().removeSpan(o); |
| 645 | } |
| 646 | PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length()); |
| 647 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 648 | } |