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