KeyguardFaceUnlockView.java revision dcb3d84b82cc2448d04e73359a716581bfb657db
1dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller/*
2dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * Copyright (C) 2012 The Android Open Source Project
3dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller *
4dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * Licensed under the Apache License, Version 2.0 (the "License");
5dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * you may not use this file except in compliance with the License.
6dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * You may obtain a copy of the License at
7dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller *
8dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller *      http://www.apache.org/licenses/LICENSE-2.0
9dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller *
10dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * Unless required by applicable law or agreed to in writing, software
11dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * distributed under the License is distributed on an "AS IS" BASIS,
12dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * See the License for the specific language governing permissions and
14dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller * limitations under the License.
15dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller */
16dcb3d84b82cc2448d04e73359a716581bfb657dbJim Millerpackage com.android.internal.policy.impl.keyguard;
17dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
18dcb3d84b82cc2448d04e73359a716581bfb657dbJim Millerimport android.content.Context;
19dcb3d84b82cc2448d04e73359a716581bfb657dbJim Millerimport android.util.AttributeSet;
20dcb3d84b82cc2448d04e73359a716581bfb657dbJim Millerimport android.widget.LinearLayout;
21dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
22dcb3d84b82cc2448d04e73359a716581bfb657dbJim Millerimport com.android.internal.widget.LockPatternUtils;
23dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
24dcb3d84b82cc2448d04e73359a716581bfb657dbJim Millerpublic class KeyguardFaceUnlockView extends LinearLayout implements KeyguardSecurityView {
25dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
26dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    // Long enough to stay visible while dialer comes up
27dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    // Short enough to not be visible if the user goes back immediately
28dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    private static final int BIOMETRIC_AREA_EMERGENCY_DIALER_TIMEOUT = 1000;
29dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    private KeyguardSecurityCallback mKeyguardSecurityCallback;
30dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    private LockPatternUtils mLockPatternUtils;
31dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
32dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public KeyguardFaceUnlockView(Context context) {
33dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller        this(context, null);
34dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
35dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
36dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public KeyguardFaceUnlockView(Context context, AttributeSet attrs) {
37dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller        super(context, attrs);
38dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
39dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
40dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    @Override
41dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public void setKeyguardCallback(KeyguardSecurityCallback callback) {
42dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller        mKeyguardSecurityCallback = callback;
43dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
44dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
45dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    @Override
46dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public void setLockPatternUtils(LockPatternUtils utils) {
47dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller        mLockPatternUtils = utils;
48dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
49dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
50dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    @Override
51dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public void reset() {
52dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
53dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
54dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
55dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    @Override
56dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public void onPause() {
57dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
58dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
59dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
60dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    @Override
61dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public void onResume() {
62dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
63dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
64dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
65dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    @Override
66dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public boolean needsInput() {
67dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller        return false;
68dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
69dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
70dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    @Override
71dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    public KeyguardSecurityCallback getCallback() {
72dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller        return mKeyguardSecurityCallback;
73dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    }
74dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
75dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    // TODO
76dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    public void onRefreshBatteryInfo(BatteryStatus status) {
77dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // When someone plugs in or unplugs the device, we hide the biometric sensor area and
78dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // suppress its startup for the next onScreenTurnedOn().  Since plugging/unplugging
79dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // causes the screen to turn on, the biometric unlock would start if it wasn't
80dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // suppressed.
81dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        //
82dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // However, if the biometric unlock is already running, we do not want to interrupt it.
83dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        final boolean pluggedIn = status.isPluggedIn();
84dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (mBiometricUnlock != null && mPluggedIn != pluggedIn
85dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                && !mBiometricUnlock.isRunning()) {
86dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            mBiometricUnlock.stop();
87dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            mBiometricUnlock.hide();
88dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            mSuppressBiometricUnlock = true;
89dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        }
90dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        mPluggedIn = pluggedIn;
91dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    }
92dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
93dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    // We need to stop the biometric unlock when a phone call comes in
94dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    @Override
95dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    public void onPhoneStateChanged(int phoneState) {
96dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (DEBUG) Log.d(TAG, "phone state: " + phoneState);
97dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (phoneState == TelephonyManager.CALL_STATE_RINGING) {
98dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            mSuppressBiometricUnlock = true;
99dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            mBiometricUnlock.stop();
100dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            mBiometricUnlock.hide();
101dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        }
102dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    }
103dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
104dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    @Override
105dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    public void onUserSwitched(int userId) {
106dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (mBiometricUnlock != null) {
107dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            mBiometricUnlock.stop();
108dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        }
109dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        mLockPatternUtils.setCurrentUser(userId);
110dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        updateScreen(getInitialMode(), true);
111dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    }
112dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
113dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    /**
114dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //     * This returns false if there is any condition that indicates that the biometric unlock should
115dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //     * not be used before the next time the unlock screen is recreated.  In other words, if this
116dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //     * returns false there is no need to even construct the biometric unlock.
117dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //     */
118dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    private boolean useBiometricUnlock() {
119dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        final ShowingMode unlockMode = getUnlockMode();
120dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        final boolean backupIsTimedOut = (mUpdateMonitor.getFailedAttempts() >=
121dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT);
122dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        return (mLockPatternUtils.usingBiometricWeak() &&
123dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                mLockPatternUtils.isBiometricWeakInstalled() &&
124dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                !mUpdateMonitor.getMaxBiometricUnlockAttemptsReached() &&
125dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                !backupIsTimedOut &&
126dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                (unlockMode == ShowingMode.Pattern || unlockMode == ShowingMode.Password));
127dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    }
128dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
129dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    private void initializeBiometricUnlockView(View view) {
130dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        boolean restartBiometricUnlock = false;
131dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //
132dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (mBiometricUnlock != null) {
133dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            restartBiometricUnlock = mBiometricUnlock.stop();
134dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        }
135dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //
136dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // Prevents biometric unlock from coming up immediately after a phone call or if there
137dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // is a dialog on top of lockscreen. It is only updated if the screen is off because if the
138dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // screen is on it's either because of an orientation change, or when it first boots.
139dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // In both those cases, we don't want to override the current value of
140dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // mSuppressBiometricUnlock and instead want to use the previous value.
141dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (!mScreenOn) {
142dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            mSuppressBiometricUnlock =
143dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                    mUpdateMonitor.getPhoneState() != TelephonyManager.CALL_STATE_IDLE
144dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                    || mHasDialog;
145dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        }
146dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //
147dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // If the biometric unlock is not being used, we don't bother constructing it.  Then we can
148dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        // simply check if it is null when deciding whether we should make calls to it.
149dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        mBiometricUnlock = null;
150dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (useBiometricUnlock()) {
151dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            // TODO: make faceLockAreaView a more general biometricUnlockView
152dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            // We will need to add our Face Unlock specific child views programmatically in
153dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            // initializeView rather than having them in the XML files.
154dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            View biometricUnlockView = view.findViewById(
155dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                    com.android.internal.R.id.faceLockAreaView);
156dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            if (biometricUnlockView != null) {
157dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                mBiometricUnlock = new FaceUnlock(mContext, mUpdateMonitor, mLockPatternUtils,
158dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                        mKeyguardScreenCallback);
159dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                mBiometricUnlock.initializeView(biometricUnlockView);
160dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //
161dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                // If this is being called because the screen turned off, we want to cover the
162dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                // backup lock so it is covered when the screen turns back on.
163dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                if (!mScreenOn) mBiometricUnlock.show(0);
164dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            } else {
165dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                Log.w(TAG, "Couldn't find biometric unlock view");
166dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            }
167dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        }
168dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //
169dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (mBiometricUnlock != null && restartBiometricUnlock) {
170dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            maybeStartBiometricUnlock();
171dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        }
172dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    }
173dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
174dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    /**
175dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //     * Starts the biometric unlock if it should be started based on a number of factors including
176dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //     * the mSuppressBiometricUnlock flag.  If it should not be started, it hides the biometric
177dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //     * unlock area.
178dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //     */
179dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //    private void maybeStartBiometricUnlock() {
180dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        if (mBiometricUnlock != null) {
181dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            final boolean backupIsTimedOut = (mUpdateMonitor.getFailedAttempts() >=
182dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                    LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT);
183dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            if (!mSuppressBiometricUnlock
184dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                    && mUpdateMonitor.getPhoneState() == TelephonyManager.CALL_STATE_IDLE
185dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                    && !mUpdateMonitor.getMaxBiometricUnlockAttemptsReached()
186dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                    && !backupIsTimedOut) {
187dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                mBiometricUnlock.start();
188dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            } else {
189dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //                mBiometricUnlock.hide();
190dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //            }
191dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //        }
192dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller    //}
193dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller
194dcb3d84b82cc2448d04e73359a716581bfb657dbJim Miller}
195