KeyguardSecurityContainer.java revision 3a3d212a51974ccec6100c1e117225291edc83cc
15a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller/*
25a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * Copyright (C) 2014 The Android Open Source Project
35a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller *
45a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * Licensed under the Apache License, Version 2.0 (the "License");
55a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * you may not use this file except in compliance with the License.
65a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * You may obtain a copy of the License at
75a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller *
85a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller *      http://www.apache.org/licenses/LICENSE-2.0
95a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller *
105a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * Unless required by applicable law or agreed to in writing, software
115a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * distributed under the License is distributed on an "AS IS" BASIS,
125a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * See the License for the specific language governing permissions and
145a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller * limitations under the License.
155a8daad469c22b9b48fdf3ac478991e4ddefdf1eJim Miller */
165ecd81154fa039961f65bb4e36d18ac555b0d1d6Jim Millerpackage com.android.keyguard;
17838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller
187751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerimport android.app.Activity;
197751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerimport android.app.AlertDialog;
203a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasaniimport android.app.admin.DevicePolicyManager;
21838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Millerimport android.content.Context;
223a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasaniimport android.os.UserHandle;
233a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasaniimport android.os.UserManager;
24838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Millerimport android.util.AttributeSet;
257751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerimport android.util.Log;
267751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerimport android.util.Slog;
277751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerimport android.view.LayoutInflater;
28052999f3c94df2659e6e3e7730a2810980f718f7Chris Wrenimport android.view.View;
297751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerimport android.view.WindowManager;
30838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Millerimport android.widget.FrameLayout;
31838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller
327751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerimport com.android.internal.widget.LockPatternUtils;
337751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerimport com.android.keyguard.KeyguardSecurityModel.SecurityMode;
347751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
357751ff6cd079e59e3c1f2404198774cd371ea69fJim Millerpublic class KeyguardSecurityContainer extends FrameLayout implements KeyguardSecurityView {
365cf17879a31b7b78c09ec50b727f921840dcf783Jorim Jaggi    private static final boolean DEBUG = KeyguardConstants.DEBUG;
377751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private static final String TAG = "KeyguardSecurityView";
383a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani
393a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani    private static final int USER_TYPE_PRIMARY = 1;
403a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani    private static final int USER_TYPE_WORK_PROFILE = 2;
413a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani    private static final int USER_TYPE_SECONDARY_USER = 3;
423a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani
437751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private KeyguardSecurityModel mSecurityModel;
447751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private boolean mEnableFallback; // TODO: This should get the value from KeyguardPatternView
457751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private LockPatternUtils mLockPatternUtils;
467751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
477751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private KeyguardSecurityViewFlipper mSecurityViewFlipper;
487751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private boolean mIsVerifyUnlockOnly;
497751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private SecurityMode mCurrentSecuritySelection = SecurityMode.Invalid;
507751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private boolean mIsBouncing;
517751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private SecurityCallback mSecurityCallback;
527751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
53336be7f7a499eec1acd90ab04d3e47d2789c8168Adrian Roos    private final KeyguardUpdateMonitor mUpdateMonitor;
54336be7f7a499eec1acd90ab04d3e47d2789c8168Adrian Roos
557751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    // Used to notify the container when something interesting happens.
567751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public interface SecurityCallback {
57ba7d94b8f3c41fd5b933b185e7463a56589e04d2Jim Miller        public boolean dismiss(boolean authenticated);
58b690f0d5023fcf144f2701058d5a6f88d66cc97aJorim Jaggi        public void userActivity();
59ba7d94b8f3c41fd5b933b185e7463a56589e04d2Jim Miller        public void onSecurityModeChanged(SecurityMode securityMode, boolean needsInput);
607751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public void finish();
617751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
627751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
63838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller    public KeyguardSecurityContainer(Context context, AttributeSet attrs) {
64838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller        this(context, attrs, 0);
65838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller    }
66838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller
67838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller    public KeyguardSecurityContainer(Context context) {
68fe0f24cc92b04e03cac3f807859721f1ce7ef54aEsteban Talavera        this(context, null, 0);
69838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller    }
70838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller
71838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller    public KeyguardSecurityContainer(Context context, AttributeSet attrs, int defStyle) {
72838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller        super(context, attrs, defStyle);
737751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityModel = new KeyguardSecurityModel(context);
747751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mLockPatternUtils = new LockPatternUtils(context);
75336be7f7a499eec1acd90ab04d3e47d2789c8168Adrian Roos        mUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
767751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
777751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
787751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void setSecurityCallback(SecurityCallback callback) {
797751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityCallback = callback;
807751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
817751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
827751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    @Override
837751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void onResume(int reason) {
84f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        if (mCurrentSecuritySelection != SecurityMode.None) {
85f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi            getSecurityView(mCurrentSecuritySelection).onResume(reason);
86f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        }
877751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
887751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
897751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    @Override
907751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void onPause() {
91f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        if (mCurrentSecuritySelection != SecurityMode.None) {
92f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi            getSecurityView(mCurrentSecuritySelection).onPause();
93f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        }
947751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
957751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
96c14f829506a5273e8022b461db2d61038b18ac4bJorim Jaggi    public void startAppearAnimation() {
97f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        if (mCurrentSecuritySelection != SecurityMode.None) {
98f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi            getSecurityView(mCurrentSecuritySelection).startAppearAnimation();
99f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        }
100c14f829506a5273e8022b461db2d61038b18ac4bJorim Jaggi    }
101c14f829506a5273e8022b461db2d61038b18ac4bJorim Jaggi
10276a1623afc170a13923b68f3256057d8adeb7937Jorim Jaggi    public boolean startDisappearAnimation(Runnable onFinishRunnable) {
10376a1623afc170a13923b68f3256057d8adeb7937Jorim Jaggi        if (mCurrentSecuritySelection != SecurityMode.None) {
10476a1623afc170a13923b68f3256057d8adeb7937Jorim Jaggi            return getSecurityView(mCurrentSecuritySelection).startDisappearAnimation(
10576a1623afc170a13923b68f3256057d8adeb7937Jorim Jaggi                    onFinishRunnable);
10676a1623afc170a13923b68f3256057d8adeb7937Jorim Jaggi        }
10776a1623afc170a13923b68f3256057d8adeb7937Jorim Jaggi        return false;
10876a1623afc170a13923b68f3256057d8adeb7937Jorim Jaggi    }
10976a1623afc170a13923b68f3256057d8adeb7937Jorim Jaggi
1107751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    void updateSecurityViews(boolean isBouncing) {
1117751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        int children = mSecurityViewFlipper.getChildCount();
1127751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        for (int i = 0; i < children; i++) {
1137751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            updateSecurityView(mSecurityViewFlipper.getChildAt(i), isBouncing);
1147751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
1157751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
1167751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1177751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void announceCurrentSecurityMethod() {
1187751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        View v = (View) getSecurityView(mCurrentSecuritySelection);
1197751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (v != null) {
1207751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            v.announceForAccessibility(v.getContentDescription());
1217751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
1227751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
1237751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1247751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private KeyguardSecurityView getSecurityView(SecurityMode securityMode) {
1257751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int securityViewIdForMode = getSecurityViewIdForMode(securityMode);
1267751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        KeyguardSecurityView view = null;
1277751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int children = mSecurityViewFlipper.getChildCount();
1287751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        for (int child = 0; child < children; child++) {
1297751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (mSecurityViewFlipper.getChildAt(child).getId() == securityViewIdForMode) {
1307751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                view = ((KeyguardSecurityView)mSecurityViewFlipper.getChildAt(child));
1317751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                break;
1327751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
1337751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
1347751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        int layoutId = getLayoutIdFor(securityMode);
1357751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (view == null && layoutId != 0) {
1367751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            final LayoutInflater inflater = LayoutInflater.from(mContext);
1377751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (DEBUG) Log.v(TAG, "inflating id = " + layoutId);
1387751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            View v = inflater.inflate(layoutId, mSecurityViewFlipper, false);
1397751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            mSecurityViewFlipper.addView(v);
1407751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            updateSecurityView(v, mIsBouncing);
1417751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            view = (KeyguardSecurityView)v;
1427751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
1437751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1447751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        return view;
1457751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
1467751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1477751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private void updateSecurityView(View view, boolean isBouncing) {
1487751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mIsBouncing = isBouncing;
1497751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (view instanceof KeyguardSecurityView) {
1507751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            KeyguardSecurityView ksv = (KeyguardSecurityView) view;
1517751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            ksv.setKeyguardCallback(mCallback);
1527751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            ksv.setLockPatternUtils(mLockPatternUtils);
1537751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (isBouncing) {
1547751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                ksv.showBouncer(0);
1557751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            } else {
1567751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                ksv.hideBouncer(0);
1577751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
1587751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        } else {
1597751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            Log.w(TAG, "View " + view + " is not a KeyguardSecurityView");
1607751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
1617751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
1627751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1637751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    protected void onFinishInflate() {
1647751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityViewFlipper = (KeyguardSecurityViewFlipper) findViewById(R.id.view_flipper);
1657751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityViewFlipper.setLockPatternUtils(mLockPatternUtils);
1667751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
1677751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1687751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void setLockPatternUtils(LockPatternUtils utils) {
1697751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mLockPatternUtils = utils;
1707751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityModel.setLockPatternUtils(utils);
1717751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityViewFlipper.setLockPatternUtils(mLockPatternUtils);
1727751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
1737751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1747751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private void showDialog(String title, String message) {
1757751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final AlertDialog dialog = new AlertDialog.Builder(mContext)
1767751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            .setTitle(title)
1777751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            .setMessage(message)
1787751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            .setNeutralButton(R.string.ok, null)
1797751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            .create();
1807751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (!(mContext instanceof Activity)) {
1817751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
1827751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
1837751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        dialog.show();
1847751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
1857751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1867751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private void showTimeoutDialog() {
1877751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        int timeoutInSeconds = (int) LockPatternUtils.FAILED_ATTEMPT_TIMEOUT_MS / 1000;
1887751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        int messageId = 0;
1897751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
1907751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        switch (mSecurityModel.getSecurityMode()) {
1917751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Pattern:
1927751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                messageId = R.string.kg_too_many_failed_pattern_attempts_dialog_message;
1937751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                break;
1947751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case PIN:
1957751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                messageId = R.string.kg_too_many_failed_pin_attempts_dialog_message;
1967751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                break;
1977751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Password:
1987751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                messageId = R.string.kg_too_many_failed_password_attempts_dialog_message;
1997751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                break;
2007751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            // These don't have timeout dialogs.
2017751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Account:
2027751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Biometric:
2037751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Invalid:
2047751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case None:
2057751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case SimPin:
2067751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case SimPuk:
2077751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                break;
2087751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
2097751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
2107751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (messageId != 0) {
2117751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            final String message = mContext.getString(messageId,
2127751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    KeyguardUpdateMonitor.getInstance(mContext).getFailedUnlockAttempts(),
2137751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    timeoutInSeconds);
2147751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            showDialog(null, message);
2157751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
2167751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
2177751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
2183a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani    private void showAlmostAtWipeDialog(int attempts, int remaining, int userType) {
2193a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        String message = null;
2203a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        switch (userType) {
2213a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            case USER_TYPE_PRIMARY:
2223a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                message = mContext.getString(R.string.kg_failed_attempts_almost_at_wipe,
2233a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                        attempts, remaining);
2243a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                break;
2253a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            case USER_TYPE_SECONDARY_USER:
2263a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_user,
2273a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                        attempts, remaining);
2283a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                break;
2293a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            case USER_TYPE_WORK_PROFILE:
2303a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_profile,
2313a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                        attempts, remaining);
2323a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                break;
2333a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        }
2347751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        showDialog(null, message);
2357751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
2367751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
2373a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani    private void showWipeDialog(int attempts, int userType) {
2383a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        String message = null;
2393a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        switch (userType) {
2403a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            case USER_TYPE_PRIMARY:
2413a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                message = mContext.getString(R.string.kg_failed_attempts_now_wiping,
2423a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                        attempts);
2433a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                break;
2443a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            case USER_TYPE_SECONDARY_USER:
2453a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                message = mContext.getString(R.string.kg_failed_attempts_now_erasing_user,
2463a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                        attempts);
2473a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                break;
2483a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            case USER_TYPE_WORK_PROFILE:
2493a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                message = mContext.getString(R.string.kg_failed_attempts_now_erasing_profile,
2503a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                        attempts);
2513a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                break;
2523a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        }
2537751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        showDialog(null, message);
2547751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
2557751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
2567751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private void showAlmostAtAccountLoginDialog() {
2577751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int timeoutInSeconds = (int) LockPatternUtils.FAILED_ATTEMPT_TIMEOUT_MS / 1000;
2587751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int count = LockPatternUtils.FAILED_ATTEMPTS_BEFORE_RESET
2597751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                - LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT;
2607751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        String message = mContext.getString(R.string.kg_failed_attempts_almost_at_login,
2617751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                count, LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT, timeoutInSeconds);
2627751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        showDialog(null, message);
263c345146a3a321644f653fce0fb79f288a0221fb0Chris Wren    }
264c345146a3a321644f653fce0fb79f288a0221fb0Chris Wren
2657751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private void reportFailedUnlockAttempt() {
2667751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
2677751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int failedAttempts = monitor.getFailedUnlockAttempts() + 1; // +1 for this time
2687751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
2697751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (DEBUG) Log.d(TAG, "reportFailedPatternAttempt: #" + failedAttempts);
2707751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
2717751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        SecurityMode mode = mSecurityModel.getSecurityMode();
2727751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final boolean usingPattern = mode == KeyguardSecurityModel.SecurityMode.Pattern;
2733a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        final int currentUser = mLockPatternUtils.getCurrentUser();
2743a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        final DevicePolicyManager dpm = mLockPatternUtils.getDevicePolicyManager();
2753a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani        final int failedAttemptsBeforeWipe =
2763a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                dpm.getMaximumFailedPasswordsForWipe(null, currentUser);
2777751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
2787751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int failedAttemptWarning = LockPatternUtils.FAILED_ATTEMPTS_BEFORE_RESET
2797751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                - LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT;
2807751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
2817751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int remainingBeforeWipe = failedAttemptsBeforeWipe > 0 ?
2827751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                (failedAttemptsBeforeWipe - failedAttempts)
2837751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                : Integer.MAX_VALUE; // because DPM returns 0 if no restriction
2847751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        boolean showTimeout = false;
2857751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (remainingBeforeWipe < LockPatternUtils.FAILED_ATTEMPTS_BEFORE_WIPE_GRACE) {
286fe0f24cc92b04e03cac3f807859721f1ce7ef54aEsteban Talavera            // The user has installed a DevicePolicyManager that requests a user/profile to be wiped
287fe0f24cc92b04e03cac3f807859721f1ce7ef54aEsteban Talavera            // N attempts. Once we get below the grace period, we post this dialog every time as a
288fe0f24cc92b04e03cac3f807859721f1ce7ef54aEsteban Talavera            // clear warning until the deletion fires.
2893a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            // Check which profile has the strictest policy for failed password attempts
2903a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            final int expiringUser = dpm.getProfileWithMinimumFailedPasswordsForWipe(currentUser);
2913a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            int userType = USER_TYPE_PRIMARY;
2923a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            if (expiringUser == currentUser) {
2933a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                if (expiringUser != UserHandle.USER_OWNER) {
2943a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                    userType = USER_TYPE_SECONDARY_USER;
2953a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                }
2963a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            } else if (expiringUser != UserHandle.USER_NULL) {
2973a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                userType = USER_TYPE_WORK_PROFILE;
2983a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani            } // If USER_NULL, which shouldn't happen, leave it as USER_TYPE_PRIMARY
2997751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (remainingBeforeWipe > 0) {
3003a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                showAlmostAtWipeDialog(failedAttempts, remainingBeforeWipe, userType);
3017751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            } else {
3027751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                // Too many attempts. The device will be wiped shortly.
3033a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                Slog.i(TAG, "Too many unlock attempts; user " + expiringUser + " will be wiped!");
3043a3d212a51974ccec6100c1e117225291edc83ccAmith Yamasani                showWipeDialog(failedAttempts, userType);
3057751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
3067751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        } else {
3077751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            showTimeout =
3087751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                (failedAttempts % LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT) == 0;
3097751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (usingPattern && mEnableFallback) {
3107751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                if (failedAttempts == failedAttemptWarning) {
3117751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    showAlmostAtAccountLoginDialog();
3127751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    showTimeout = false; // don't show both dialogs
3137751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                } else if (failedAttempts >= LockPatternUtils.FAILED_ATTEMPTS_BEFORE_RESET) {
3147751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    mLockPatternUtils.setPermanentlyLocked(true);
3157751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    showSecurityScreen(SecurityMode.Account);
3167751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    // don't show timeout dialog because we show account unlock screen next
3177751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    showTimeout = false;
3187751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                }
3197751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
3207751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
3217751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        monitor.reportFailedUnlockAttempt();
3227751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mLockPatternUtils.reportFailedPasswordAttempt();
3237751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (showTimeout) {
3247751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            showTimeoutDialog();
3257751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
3267751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
3277751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
3287751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    /**
3297751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * Shows the primary security screen for the user. This will be either the multi-selector
3307751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * or the user's security method.
3317751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * @param turningOff true if the device is being turned off
3327751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     */
3337751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    void showPrimarySecurityScreen(boolean turningOff) {
3347751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        SecurityMode securityMode = mSecurityModel.getSecurityMode();
3357751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (DEBUG) Log.v(TAG, "showPrimarySecurityScreen(turningOff=" + turningOff + ")");
3367751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (!turningOff &&
3377751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                KeyguardUpdateMonitor.getInstance(mContext).isAlternateUnlockEnabled()) {
3387751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            // If we're not turning off, then allow biometric alternate.
3397751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            // We'll reload it when the device comes back on.
3407751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            securityMode = mSecurityModel.getAlternateFor(securityMode);
3417751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
3427751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        showSecurityScreen(securityMode);
3437751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
3447751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
3457751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    /**
3467751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * Shows the backup security screen for the current security mode.  This could be used for
3477751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * password recovery screens but is currently only used for pattern unlock to show the
3487751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * account unlock screen and biometric unlock to show the user's normal unlock.
3497751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     */
3507751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private void showBackupSecurityScreen() {
3517751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (DEBUG) Log.d(TAG, "showBackupSecurity()");
3527751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        SecurityMode backup = mSecurityModel.getBackupSecurityMode(mCurrentSecuritySelection);
3537751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        showSecurityScreen(backup);
3547751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
3557751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
356ba7d94b8f3c41fd5b933b185e7463a56589e04d2Jim Miller    /**
357ba7d94b8f3c41fd5b933b185e7463a56589e04d2Jim Miller     * Shows the next security screen if there is one.
358ba7d94b8f3c41fd5b933b185e7463a56589e04d2Jim Miller     * @param authenticated true if the user entered the correct authentication
359ba7d94b8f3c41fd5b933b185e7463a56589e04d2Jim Miller     * @param authenticated
360ba7d94b8f3c41fd5b933b185e7463a56589e04d2Jim Miller     * @return true if keyguard is done
361ba7d94b8f3c41fd5b933b185e7463a56589e04d2Jim Miller     */
3627751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    boolean showNextSecurityScreenOrFinish(boolean authenticated) {
3637751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (DEBUG) Log.d(TAG, "showNextSecurityScreenOrFinish(" + authenticated + ")");
3647751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        boolean finish = false;
365336be7f7a499eec1acd90ab04d3e47d2789c8168Adrian Roos        if (mUpdateMonitor.getUserHasTrust(mLockPatternUtils.getCurrentUser())) {
366336be7f7a499eec1acd90ab04d3e47d2789c8168Adrian Roos            finish = true;
367336be7f7a499eec1acd90ab04d3e47d2789c8168Adrian Roos        } else if (SecurityMode.None == mCurrentSecuritySelection) {
3687751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            SecurityMode securityMode = mSecurityModel.getSecurityMode();
3697751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            // Allow an alternate, such as biometric unlock
3707751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            securityMode = mSecurityModel.getAlternateFor(securityMode);
3717751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (SecurityMode.None == securityMode) {
3727751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                finish = true; // no security required
3737751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            } else {
3747751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                showSecurityScreen(securityMode); // switch to the alternate security view
3757751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
3767751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        } else if (authenticated) {
3777751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            switch (mCurrentSecuritySelection) {
3787751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                case Pattern:
3797751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                case Password:
3807751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                case PIN:
3817751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                case Account:
3827751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                case Biometric:
3837751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    finish = true;
3847751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    break;
3857751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
3867751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                case SimPin:
3877751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                case SimPuk:
3887751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    // Shortcut for SIM PIN/PUK to go to directly to user's security screen or home
3897751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    SecurityMode securityMode = mSecurityModel.getSecurityMode();
3907751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    if (securityMode != SecurityMode.None) {
3917751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                        showSecurityScreen(securityMode);
3927751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    } else {
3937751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                        finish = true;
3947751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    }
3957751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    break;
3967751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
3977751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                default:
3987751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    Log.v(TAG, "Bad security screen " + mCurrentSecuritySelection + ", fail safe");
3997751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    showPrimarySecurityScreen(false);
4007751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    break;
4017751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
4027751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        } else {
4037751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            showPrimarySecurityScreen(false);
4047751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
4057751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (finish) {
4067751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            mSecurityCallback.finish();
4077751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
4087751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        return finish;
4097751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
4107751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4117751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    /**
4127751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * Switches to the given security view unless it's already being shown, in which case
4137751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * this is a no-op.
4147751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     *
4157751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     * @param securityMode
4167751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller     */
4177751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private void showSecurityScreen(SecurityMode securityMode) {
4187751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (DEBUG) Log.d(TAG, "showSecurityScreen(" + securityMode + ")");
4197751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4207751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (securityMode == mCurrentSecuritySelection) return;
4217751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4227751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        KeyguardSecurityView oldView = getSecurityView(mCurrentSecuritySelection);
4237751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        KeyguardSecurityView newView = getSecurityView(securityMode);
4247751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4257751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        // Emulate Activity life cycle
4267751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        if (oldView != null) {
4277751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            oldView.onPause();
4287751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            oldView.setKeyguardCallback(mNullCallback); // ignore requests from old view
4297751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
430f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        if (securityMode != SecurityMode.None) {
431f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi            newView.onResume(KeyguardSecurityView.VIEW_REVEALED);
432f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi            newView.setKeyguardCallback(mCallback);
433f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        }
4347751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4357751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        // Find and show this child.
4367751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int childCount = mSecurityViewFlipper.getChildCount();
4377751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4387751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        final int securityViewIdForMode = getSecurityViewIdForMode(securityMode);
4397751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        for (int i = 0; i < childCount; i++) {
4407751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (mSecurityViewFlipper.getChildAt(i).getId() == securityViewIdForMode) {
4417751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                mSecurityViewFlipper.setDisplayedChild(i);
4427751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                break;
4437751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
4447751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
4457751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4467751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mCurrentSecuritySelection = securityMode;
447f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi        mSecurityCallback.onSecurityModeChanged(securityMode,
448f479792e05485a536c3fa68db9d8a71f34591b78Jorim Jaggi                securityMode != SecurityMode.None && newView.needsInput());
4497751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
4507751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4517751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private KeyguardSecurityViewFlipper getFlipper() {
452052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren        for (int i = 0; i < getChildCount(); i++) {
453052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren            View child = getChildAt(i);
454052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren            if (child instanceof KeyguardSecurityViewFlipper) {
455c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren                return (KeyguardSecurityViewFlipper) child;
456052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren            }
457052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren        }
458052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren        return null;
459052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren    }
460052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren
461c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren    public void showBouncer(int duration) {
462c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren        KeyguardSecurityViewFlipper flipper = getFlipper();
463c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren        if (flipper != null) {
464c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren            flipper.showBouncer(duration);
465c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren        }
466c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren    }
467c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren
468c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren    public void hideBouncer(int duration) {
469c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren        KeyguardSecurityViewFlipper flipper = getFlipper();
470c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren        if (flipper != null) {
471c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren            flipper.hideBouncer(duration);
472052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren        }
473052999f3c94df2659e6e3e7730a2810980f718f7Chris Wren    }
4747751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4757751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private KeyguardSecurityCallback mCallback = new KeyguardSecurityCallback() {
4767751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
477b690f0d5023fcf144f2701058d5a6f88d66cc97aJorim Jaggi        public void userActivity() {
4787751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (mSecurityCallback != null) {
479b690f0d5023fcf144f2701058d5a6f88d66cc97aJorim Jaggi                mSecurityCallback.userActivity();
4807751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
4817751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
4827751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4837751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public void dismiss(boolean authenticated) {
4847751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            mSecurityCallback.dismiss(authenticated);
4857751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
4867751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4877751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public boolean isVerifyUnlockOnly() {
4887751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            return mIsVerifyUnlockOnly;
4897751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
4907751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
4917751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public void reportUnlockAttempt(boolean success) {
4927751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
4937751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            if (success) {
4947751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                monitor.clearFailedUnlockAttempts();
4957751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                mLockPatternUtils.reportSuccessfulPasswordAttempt();
4967751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            } else {
4977751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                if (mCurrentSecuritySelection == SecurityMode.Biometric) {
4987751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    monitor.reportFailedBiometricUnlockAttempt();
4997751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                } else {
5007751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                    KeyguardSecurityContainer.this.reportFailedUnlockAttempt();
5017751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                }
5027751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            }
5037751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
5047751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5057751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        @Override
5067751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public void showBackupSecurity() {
5077751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            KeyguardSecurityContainer.this.showBackupSecurityScreen();
5087751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
5097751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5107751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    };
5117751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5127751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    // The following is used to ignore callbacks from SecurityViews that are no longer current
5137751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    // (e.g. face unlock). This avoids unwanted asynchronous events from messing with the
5147751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    // state for the current security method.
5157751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private KeyguardSecurityCallback mNullCallback = new KeyguardSecurityCallback() {
5167751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        @Override
517b690f0d5023fcf144f2701058d5a6f88d66cc97aJorim Jaggi        public void userActivity() { }
5187751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        @Override
5197751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public void showBackupSecurity() { }
5207751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        @Override
5217751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public void reportUnlockAttempt(boolean success) { }
5227751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        @Override
5237751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public boolean isVerifyUnlockOnly() { return false; }
5247751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        @Override
5257751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        public void dismiss(boolean securityVerified) { }
5267751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    };
5277751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5287751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private int getSecurityViewIdForMode(SecurityMode securityMode) {
5297751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        switch (securityMode) {
5307751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Pattern: return R.id.keyguard_pattern_view;
5317751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case PIN: return R.id.keyguard_pin_view;
5327751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Password: return R.id.keyguard_password_view;
5337751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Biometric: return R.id.keyguard_face_unlock_view;
5347751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Account: return R.id.keyguard_account_view;
5357751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case SimPin: return R.id.keyguard_sim_pin_view;
5367751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case SimPuk: return R.id.keyguard_sim_puk_view;
5377751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
5387751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        return 0;
5397751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5407751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5417751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    private int getLayoutIdFor(SecurityMode securityMode) {
5427751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        switch (securityMode) {
5437751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Pattern: return R.layout.keyguard_pattern_view;
5447751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case PIN: return R.layout.keyguard_pin_view;
5457751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Password: return R.layout.keyguard_password_view;
5467751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Biometric: return R.layout.keyguard_face_unlock_view;
5477751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case Account: return R.layout.keyguard_account_view;
5487751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case SimPin: return R.layout.keyguard_sim_pin_view;
5497751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            case SimPuk: return R.layout.keyguard_sim_puk_view;
5507751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller            default:
5517751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller                return 0;
5527751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        }
5537751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5547751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5557751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public SecurityMode getSecurityMode() {
5567751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        return mSecurityModel.getSecurityMode();
5577751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5587751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5597751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void verifyUnlock() {
5607751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mIsVerifyUnlockOnly = true;
5617751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        showSecurityScreen(getSecurityMode());
5627751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5637751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5647751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public SecurityMode getCurrentSecuritySelection() {
5657751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        return mCurrentSecuritySelection;
5667751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5677751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5687751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void dismiss(boolean authenticated) {
5697751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mCallback.dismiss(authenticated);
5707751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5717751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5727751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public boolean needsInput() {
5737751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        return mSecurityViewFlipper.needsInput();
5747751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5757751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5767751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    @Override
5777751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void setKeyguardCallback(KeyguardSecurityCallback callback) {
5787751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityViewFlipper.setKeyguardCallback(callback);
5797751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5807751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5817751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    @Override
5827751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void reset() {
5837751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityViewFlipper.reset();
5847751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5857751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5867751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    @Override
5877751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public KeyguardSecurityCallback getCallback() {
5887751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        return mSecurityViewFlipper.getCallback();
5897751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5907751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
5917751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    @Override
5927751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    public void showUsabilityHint() {
5937751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller        mSecurityViewFlipper.showUsabilityHint();
5947751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller    }
5957751ff6cd079e59e3c1f2404198774cd371ea69fJim Miller
596838906b165e4d3cb2c512b2db344aa50cb5d4751Jim Miller}
597c0ae9e67ebe6f1298800feaed1b43e867139a904Chris Wren
598