EmergencyButton.java revision 5cf17879a31b7b78c09ec50b727f921840dcf783
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.keyguard;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.PowerManager;
22import android.os.SystemClock;
23import android.os.UserHandle;
24import android.telephony.TelephonyManager;
25import android.util.AttributeSet;
26import android.view.View;
27import android.widget.Button;
28
29import com.android.internal.telephony.IccCardConstants.State;
30import com.android.internal.widget.LockPatternUtils;
31
32/**
33 * This class implements a smart emergency button that updates itself based
34 * on telephony state.  When the phone is idle, it is an emergency call button.
35 * When there's a call in progress, it presents an appropriate message and
36 * allows the user to return to the call.
37 */
38public class EmergencyButton extends Button {
39
40    private static final int EMERGENCY_CALL_TIMEOUT = 10000; // screen timeout after starting e.d.
41    private static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
42
43    KeyguardUpdateMonitorCallback mInfoCallback = new KeyguardUpdateMonitorCallback() {
44
45        @Override
46        public void onSimStateChanged(State simState) {
47            int phoneState = KeyguardUpdateMonitor.getInstance(mContext).getPhoneState();
48            updateEmergencyCallButton(simState, phoneState);
49        }
50
51        @Override
52        public void onPhoneStateChanged(int phoneState) {
53            State simState = KeyguardUpdateMonitor.getInstance(mContext).getSimState();
54            updateEmergencyCallButton(simState, phoneState);
55        }
56    };
57    private LockPatternUtils mLockPatternUtils;
58    private PowerManager mPowerManager;
59
60    public EmergencyButton(Context context) {
61        this(context, null);
62    }
63
64    public EmergencyButton(Context context, AttributeSet attrs) {
65        super(context, attrs);
66    }
67
68    @Override
69    protected void onAttachedToWindow() {
70        super.onAttachedToWindow();
71        KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mInfoCallback);
72    }
73
74    @Override
75    protected void onDetachedFromWindow() {
76        super.onDetachedFromWindow();
77        KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mInfoCallback);
78    }
79
80    @Override
81    protected void onFinishInflate() {
82        super.onFinishInflate();
83        mLockPatternUtils = new LockPatternUtils(mContext);
84        mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
85        setOnClickListener(new OnClickListener() {
86            public void onClick(View v) {
87                takeEmergencyCallAction();
88            }
89        });
90        int phoneState = KeyguardUpdateMonitor.getInstance(mContext).getPhoneState();
91        State simState = KeyguardUpdateMonitor.getInstance(mContext).getSimState();
92        updateEmergencyCallButton(simState, phoneState);
93    }
94
95    /**
96     * Shows the emergency dialer or returns the user to the existing call.
97     */
98    public void takeEmergencyCallAction() {
99        // TODO: implement a shorter timeout once new PowerManager API is ready.
100        // should be the equivalent to the old userActivity(EMERGENCY_CALL_TIMEOUT)
101        mPowerManager.userActivity(SystemClock.uptimeMillis(), true);
102        if (TelephonyManager.getDefault().getCallState()
103                == TelephonyManager.CALL_STATE_OFFHOOK) {
104            mLockPatternUtils.resumeCall();
105        } else {
106            final boolean bypassHandler = true;
107            KeyguardUpdateMonitor.getInstance(mContext).reportEmergencyCallAction(bypassHandler);
108            Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
109            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
110                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
111            getContext().startActivityAsUser(intent,
112                    new UserHandle(mLockPatternUtils.getCurrentUser()));
113        }
114    }
115
116    private void updateEmergencyCallButton(State simState, int phoneState) {
117        boolean enabled = false;
118        if (phoneState == TelephonyManager.CALL_STATE_OFFHOOK) {
119            enabled = true; // always show "return to call" if phone is off-hook
120        } else if (mLockPatternUtils.isEmergencyCallCapable()) {
121            boolean simLocked = KeyguardUpdateMonitor.getInstance(mContext).isSimLocked();
122            if (simLocked) {
123                // Some countries can't handle emergency calls while SIM is locked.
124                enabled = mLockPatternUtils.isEmergencyCallEnabledWhileSimLocked();
125            } else {
126                // True if we need to show a secure screen (pin/pattern/SIM pin/SIM puk);
127                // hides emergency button on "Slide" screen if device is not secure.
128                enabled = mLockPatternUtils.isSecure();
129            }
130        }
131        mLockPatternUtils.updateEmergencyCallButtonState(this, phoneState, enabled, false);
132    }
133
134}
135