KeyguardFaceUnlockView.java revision 000464ac012471d301c6e48a8228291519915e17
1/* 2 * Copyright (C) 2012 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 */ 16package com.android.internal.policy.impl.keyguard; 17 18import android.content.Context; 19import android.telephony.TelephonyManager; 20import android.util.AttributeSet; 21import android.util.Log; 22import android.view.View; 23import android.widget.LinearLayout; 24 25import com.android.internal.R; 26import com.android.internal.policy.impl.keyguard.BiometricSensorUnlock; 27import com.android.internal.policy.impl.keyguard.FaceUnlock; 28import com.android.internal.widget.LockPatternUtils; 29 30public class KeyguardFaceUnlockView extends LinearLayout implements KeyguardSecurityView { 31 32 private static final String TAG = "KeyguardFaceUnlockView"; 33 // Long enough to stay visible while dialer comes up 34 // Short enough to not be visible if the user goes back immediately 35 private KeyguardSecurityCallback mKeyguardSecurityCallback; 36 private LockPatternUtils mLockPatternUtils; 37 private BiometricSensorUnlock mBiometricUnlock; 38 private KeyguardNavigationManager mNavigationManager; 39 private View mFaceUnlockAreaView; 40 41 public KeyguardFaceUnlockView(Context context) { 42 this(context, null); 43 } 44 45 public KeyguardFaceUnlockView(Context context, AttributeSet attrs) { 46 super(context, attrs); 47 } 48 49 @Override 50 protected void onFinishInflate() { 51 super.onFinishInflate(); 52 mNavigationManager = new KeyguardNavigationManager(this); 53 54 initializeBiometricUnlockView(); 55 } 56 57 @Override 58 public void setKeyguardCallback(KeyguardSecurityCallback callback) { 59 mKeyguardSecurityCallback = callback; 60 // TODO: formalize this in the interface or factor it out 61 ((FaceUnlock)mBiometricUnlock).setKeyguardCallback(callback); 62 } 63 64 @Override 65 public void setLockPatternUtils(LockPatternUtils utils) { 66 mLockPatternUtils = utils; 67 } 68 69 @Override 70 public void reset() { 71 72 } 73 74 @Override 75 public void onPause() { 76 if (mBiometricUnlock != null) { 77 mBiometricUnlock.hide(); 78 mBiometricUnlock.stop(); 79 } 80 KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mUpdateCallback); 81 } 82 83 @Override 84 public void onResume() { 85 maybeStartBiometricUnlock(); 86 mBiometricUnlock.show(0); 87 KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateCallback); 88 } 89 90 @Override 91 public boolean needsInput() { 92 return false; 93 } 94 95 @Override 96 public KeyguardSecurityCallback getCallback() { 97 return mKeyguardSecurityCallback; 98 } 99 100 @Override 101 protected void onLayout(boolean changed, int l, int t, int r, int b) { 102 super.onLayout(changed, l, t, r, b); 103 mBiometricUnlock.initializeView(mFaceUnlockAreaView); 104 } 105 106 private void initializeBiometricUnlockView() { 107 mFaceUnlockAreaView = findViewById(R.id.face_unlock_area_view); 108 if (mFaceUnlockAreaView != null) { 109 mBiometricUnlock = new FaceUnlock(mContext); 110 } else { 111 Log.w(TAG, "Couldn't find biometric unlock view"); 112 } 113 } 114 115 /** 116 * Starts the biometric unlock if it should be started based on a number of factors including 117 * the mSuppressBiometricUnlock flag. If it should not be started, it hides the biometric 118 * unlock area. 119 */ 120 private void maybeStartBiometricUnlock() { 121 if (mBiometricUnlock != null) { 122 KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext); 123 final boolean backupIsTimedOut = ( 124 monitor.getFailedUnlockAttempts() >= 125 LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT); 126 if (monitor.getPhoneState() == TelephonyManager.CALL_STATE_IDLE 127 && !monitor.getMaxBiometricUnlockAttemptsReached() 128 && !backupIsTimedOut) { 129 mBiometricUnlock.start(); 130 } else { 131 mBiometricUnlock.hide(); 132 } 133 } 134 } 135 136 KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() { 137 // We need to stop the biometric unlock when a phone call comes in 138 @Override 139 public void onPhoneStateChanged(int phoneState) { 140 if (phoneState == TelephonyManager.CALL_STATE_RINGING) { 141 mBiometricUnlock.stop(); 142 mBiometricUnlock.hide(); 143 } 144 } 145 146 @Override 147 public void onUserSwitched(int userId) { 148 if (mBiometricUnlock != null) { 149 mBiometricUnlock.stop(); 150 } 151 mLockPatternUtils.setCurrentUser(userId); 152 } 153 }; 154 155} 156