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