KeyguardFaceUnlockView.java revision 94c7b14b1a4f53157997939505b70ef056ca90d8
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.os.PowerManager;
20import android.telephony.TelephonyManager;
21import android.util.AttributeSet;
22import android.util.Log;
23import android.view.View;
24import android.widget.ImageButton;
25import android.widget.LinearLayout;
26
27import com.android.internal.R;
28
29import com.android.internal.widget.LockPatternUtils;
30
31public class KeyguardFaceUnlockView extends LinearLayout implements KeyguardSecurityView {
32
33    private static final String TAG = "FULKeyguardFaceUnlockView";
34    private static final boolean DEBUG = false;
35    private KeyguardSecurityCallback mKeyguardSecurityCallback;
36    private LockPatternUtils mLockPatternUtils;
37    private BiometricSensorUnlock mBiometricUnlock;
38    private SecurityMessageDisplay mSecurityMessageDisplay;
39    private View mFaceUnlockAreaView;
40    private ImageButton mCancelButton;
41
42    public KeyguardFaceUnlockView(Context context) {
43        this(context, null);
44    }
45
46    public KeyguardFaceUnlockView(Context context, AttributeSet attrs) {
47        super(context, attrs);
48    }
49
50    @Override
51    protected void onFinishInflate() {
52        super.onFinishInflate();
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 onDetachedFromWindow() {
76        if (DEBUG) Log.d(TAG, "onDetachedFromWindow()");
77        if (mBiometricUnlock != null) {
78            mBiometricUnlock.stopAndShowBackup();
79        }
80    }
81
82    @Override
83    public void onPause() {
84        if (DEBUG) Log.d(TAG, "onPause()");
85        if (mBiometricUnlock != null) {
86            mBiometricUnlock.stopAndShowBackup();
87        }
88        KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mUpdateCallback);
89    }
90
91    @Override
92    public void onResume() {
93        if (DEBUG) Log.d(TAG, "onResume()");
94        maybeStartBiometricUnlock();
95        KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateCallback);
96    }
97
98    @Override
99    public boolean needsInput() {
100        return false;
101    }
102
103    @Override
104    public KeyguardSecurityCallback getCallback() {
105        return mKeyguardSecurityCallback;
106    }
107
108    @Override
109    protected void onLayout(boolean changed, int l, int t, int r, int b) {
110        super.onLayout(changed, l, t, r, b);
111        mBiometricUnlock.initializeView(mFaceUnlockAreaView);
112    }
113
114    private void initializeBiometricUnlockView() {
115        mFaceUnlockAreaView = findViewById(R.id.face_unlock_area_view);
116        if (mFaceUnlockAreaView != null) {
117            mBiometricUnlock = new FaceUnlock(mContext);
118
119            mCancelButton = (ImageButton) findViewById(R.id.face_unlock_cancel_button);
120            mCancelButton.setOnClickListener(new OnClickListener() {
121                @Override
122                public void onClick(View v) {
123                    mBiometricUnlock.stopAndShowBackup();
124                }
125            });
126        } else {
127            Log.w(TAG, "Couldn't find biometric unlock view");
128        }
129    }
130
131    /**
132     * Starts the biometric unlock if it should be started based on a number of factors including
133     * the mSuppressBiometricUnlock flag.  If it should not be started, it hides the biometric
134     * unlock area.
135     */
136    private void maybeStartBiometricUnlock() {
137        if (DEBUG) Log.d(TAG, "maybeStartBiometricUnlock()");
138        if (mBiometricUnlock != null) {
139            KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
140            final boolean backupIsTimedOut = (
141                    monitor.getFailedUnlockAttempts() >=
142                    LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT);
143            PowerManager powerManager = (PowerManager) mContext.getSystemService(
144                    Context.POWER_SERVICE);
145            // TODO: Some of these conditions are handled in KeyguardSecurityModel and may not be
146            // necessary here.
147            if (monitor.getPhoneState() != TelephonyManager.CALL_STATE_RINGING
148                    && !monitor.getMaxBiometricUnlockAttemptsReached()
149                    && !backupIsTimedOut
150                    && powerManager.isScreenOn()) {
151                mBiometricUnlock.start();
152            } else {
153                mBiometricUnlock.stopAndShowBackup();
154            }
155        }
156    }
157
158    KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
159        // We need to stop the biometric unlock when a phone call comes in
160        @Override
161        public void onPhoneStateChanged(int phoneState) {
162            if (DEBUG) Log.d(TAG, "onPhoneStateChanged(" + phoneState + ")");
163            if (phoneState == TelephonyManager.CALL_STATE_RINGING) {
164                mBiometricUnlock.stopAndShowBackup();
165            }
166        }
167
168        @Override
169        public void onUserSwitched(int userId) {
170            if (DEBUG) Log.d(TAG, "onUserSwitched(" + userId + ")");
171            if (mBiometricUnlock != null) {
172                mBiometricUnlock.stop();
173            }
174            // No longer required; static value set by KeyguardViewMediator
175            // mLockPatternUtils.setCurrentUser(userId);
176        }
177    };
178
179    @Override
180    public void setSecurityMessageDisplay(SecurityMessageDisplay display) {
181        mSecurityMessageDisplay = display;
182    }
183}
184