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