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