Jim Miller | 5a8daad | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | */ |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 16 | package com.android.keyguard; |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 17 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 18 | import android.app.Activity; |
| 19 | import android.app.AlertDialog; |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 20 | import android.app.admin.DevicePolicyManager; |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 22 | import android.os.UserHandle; |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | import android.util.Slog; |
| 26 | import android.view.LayoutInflater; |
Chris Wren | 052999f | 2012-11-02 14:36:56 -0400 | [diff] [blame] | 27 | import android.view.View; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 28 | import android.view.WindowManager; |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 29 | import android.widget.FrameLayout; |
| 30 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 31 | import com.android.internal.widget.LockPatternUtils; |
| 32 | import com.android.keyguard.KeyguardSecurityModel.SecurityMode; |
| 33 | |
| 34 | public class KeyguardSecurityContainer extends FrameLayout implements KeyguardSecurityView { |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 35 | private static final boolean DEBUG = KeyguardConstants.DEBUG; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 36 | private static final String TAG = "KeyguardSecurityView"; |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 37 | |
| 38 | private static final int USER_TYPE_PRIMARY = 1; |
| 39 | private static final int USER_TYPE_WORK_PROFILE = 2; |
| 40 | private static final int USER_TYPE_SECONDARY_USER = 3; |
| 41 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 42 | private KeyguardSecurityModel mSecurityModel; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 43 | private LockPatternUtils mLockPatternUtils; |
| 44 | |
| 45 | private KeyguardSecurityViewFlipper mSecurityViewFlipper; |
| 46 | private boolean mIsVerifyUnlockOnly; |
| 47 | private SecurityMode mCurrentSecuritySelection = SecurityMode.Invalid; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 48 | private SecurityCallback mSecurityCallback; |
| 49 | |
Adrian Roos | 336be7f | 2014-05-19 17:11:18 +0200 | [diff] [blame] | 50 | private final KeyguardUpdateMonitor mUpdateMonitor; |
| 51 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 52 | // Used to notify the container when something interesting happens. |
| 53 | public interface SecurityCallback { |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 54 | public boolean dismiss(boolean authenticated); |
Jorim Jaggi | b690f0d | 2014-07-03 23:25:44 +0200 | [diff] [blame] | 55 | public void userActivity(); |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 56 | public void onSecurityModeChanged(SecurityMode securityMode, boolean needsInput); |
Jorim Jaggi | 25b4d4b | 2015-08-11 15:54:06 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * @param strongAuth wheher the user has authenticated with strong authentication like |
| 60 | * pattern, password or PIN but not by trust agents or fingerprint |
| 61 | */ |
| 62 | public void finish(boolean strongAuth); |
Andrew Lee | 72b46d4 | 2015-01-30 13:23:21 -0800 | [diff] [blame] | 63 | public void reset(); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 66 | public KeyguardSecurityContainer(Context context, AttributeSet attrs) { |
| 67 | this(context, attrs, 0); |
| 68 | } |
| 69 | |
| 70 | public KeyguardSecurityContainer(Context context) { |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 71 | this(context, null, 0); |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public KeyguardSecurityContainer(Context context, AttributeSet attrs, int defStyle) { |
| 75 | super(context, attrs, defStyle); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 76 | mSecurityModel = new KeyguardSecurityModel(context); |
| 77 | mLockPatternUtils = new LockPatternUtils(context); |
Adrian Roos | 336be7f | 2014-05-19 17:11:18 +0200 | [diff] [blame] | 78 | mUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext); |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 81 | public void setSecurityCallback(SecurityCallback callback) { |
| 82 | mSecurityCallback = callback; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onResume(int reason) { |
Jorim Jaggi | f479792 | 2014-08-04 22:49:41 +0200 | [diff] [blame] | 87 | if (mCurrentSecuritySelection != SecurityMode.None) { |
| 88 | getSecurityView(mCurrentSecuritySelection).onResume(reason); |
| 89 | } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onPause() { |
Jorim Jaggi | f479792 | 2014-08-04 22:49:41 +0200 | [diff] [blame] | 94 | if (mCurrentSecuritySelection != SecurityMode.None) { |
| 95 | getSecurityView(mCurrentSecuritySelection).onPause(); |
| 96 | } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Jorim Jaggi | c14f829 | 2014-05-27 02:25:45 +0200 | [diff] [blame] | 99 | public void startAppearAnimation() { |
Jorim Jaggi | f479792 | 2014-08-04 22:49:41 +0200 | [diff] [blame] | 100 | if (mCurrentSecuritySelection != SecurityMode.None) { |
| 101 | getSecurityView(mCurrentSecuritySelection).startAppearAnimation(); |
| 102 | } |
Jorim Jaggi | c14f829 | 2014-05-27 02:25:45 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Jorim Jaggi | 76a1623 | 2014-08-08 17:00:47 +0200 | [diff] [blame] | 105 | public boolean startDisappearAnimation(Runnable onFinishRunnable) { |
| 106 | if (mCurrentSecuritySelection != SecurityMode.None) { |
| 107 | return getSecurityView(mCurrentSecuritySelection).startDisappearAnimation( |
| 108 | onFinishRunnable); |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 113 | public void announceCurrentSecurityMethod() { |
| 114 | View v = (View) getSecurityView(mCurrentSecuritySelection); |
| 115 | if (v != null) { |
| 116 | v.announceForAccessibility(v.getContentDescription()); |
| 117 | } |
| 118 | } |
| 119 | |
Jorim Jaggi | c1dff8c | 2015-02-02 14:45:39 +0100 | [diff] [blame] | 120 | public CharSequence getCurrentSecurityModeContentDescription() { |
| 121 | View v = (View) getSecurityView(mCurrentSecuritySelection); |
| 122 | if (v != null) { |
| 123 | return v.getContentDescription(); |
| 124 | } |
| 125 | return ""; |
| 126 | } |
| 127 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 128 | private KeyguardSecurityView getSecurityView(SecurityMode securityMode) { |
| 129 | final int securityViewIdForMode = getSecurityViewIdForMode(securityMode); |
| 130 | KeyguardSecurityView view = null; |
| 131 | final int children = mSecurityViewFlipper.getChildCount(); |
| 132 | for (int child = 0; child < children; child++) { |
| 133 | if (mSecurityViewFlipper.getChildAt(child).getId() == securityViewIdForMode) { |
| 134 | view = ((KeyguardSecurityView)mSecurityViewFlipper.getChildAt(child)); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | int layoutId = getLayoutIdFor(securityMode); |
| 139 | if (view == null && layoutId != 0) { |
| 140 | final LayoutInflater inflater = LayoutInflater.from(mContext); |
| 141 | if (DEBUG) Log.v(TAG, "inflating id = " + layoutId); |
| 142 | View v = inflater.inflate(layoutId, mSecurityViewFlipper, false); |
| 143 | mSecurityViewFlipper.addView(v); |
Jorim Jaggi | 6b88cdf | 2014-12-22 20:56:50 +0100 | [diff] [blame] | 144 | updateSecurityView(v); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 145 | view = (KeyguardSecurityView)v; |
| 146 | } |
| 147 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 148 | return view; |
| 149 | } |
| 150 | |
Jorim Jaggi | 6b88cdf | 2014-12-22 20:56:50 +0100 | [diff] [blame] | 151 | private void updateSecurityView(View view) { |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 152 | if (view instanceof KeyguardSecurityView) { |
| 153 | KeyguardSecurityView ksv = (KeyguardSecurityView) view; |
| 154 | ksv.setKeyguardCallback(mCallback); |
| 155 | ksv.setLockPatternUtils(mLockPatternUtils); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 156 | } else { |
| 157 | Log.w(TAG, "View " + view + " is not a KeyguardSecurityView"); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | protected void onFinishInflate() { |
| 162 | mSecurityViewFlipper = (KeyguardSecurityViewFlipper) findViewById(R.id.view_flipper); |
| 163 | mSecurityViewFlipper.setLockPatternUtils(mLockPatternUtils); |
| 164 | } |
| 165 | |
| 166 | public void setLockPatternUtils(LockPatternUtils utils) { |
| 167 | mLockPatternUtils = utils; |
| 168 | mSecurityModel.setLockPatternUtils(utils); |
| 169 | mSecurityViewFlipper.setLockPatternUtils(mLockPatternUtils); |
| 170 | } |
| 171 | |
| 172 | private void showDialog(String title, String message) { |
| 173 | final AlertDialog dialog = new AlertDialog.Builder(mContext) |
| 174 | .setTitle(title) |
| 175 | .setMessage(message) |
| 176 | .setNeutralButton(R.string.ok, null) |
| 177 | .create(); |
| 178 | if (!(mContext instanceof Activity)) { |
| 179 | dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
| 180 | } |
| 181 | dialog.show(); |
| 182 | } |
| 183 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 184 | private void showTimeoutDialog(int timeoutMs) { |
| 185 | int timeoutInSeconds = (int) timeoutMs / 1000; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 186 | int messageId = 0; |
| 187 | |
| 188 | switch (mSecurityModel.getSecurityMode()) { |
| 189 | case Pattern: |
| 190 | messageId = R.string.kg_too_many_failed_pattern_attempts_dialog_message; |
| 191 | break; |
| 192 | case PIN: |
| 193 | messageId = R.string.kg_too_many_failed_pin_attempts_dialog_message; |
| 194 | break; |
| 195 | case Password: |
| 196 | messageId = R.string.kg_too_many_failed_password_attempts_dialog_message; |
| 197 | break; |
| 198 | // These don't have timeout dialogs. |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 199 | case Invalid: |
| 200 | case None: |
| 201 | case SimPin: |
| 202 | case SimPuk: |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | if (messageId != 0) { |
| 207 | final String message = mContext.getString(messageId, |
| 208 | KeyguardUpdateMonitor.getInstance(mContext).getFailedUnlockAttempts(), |
| 209 | timeoutInSeconds); |
| 210 | showDialog(null, message); |
| 211 | } |
| 212 | } |
| 213 | |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 214 | private void showAlmostAtWipeDialog(int attempts, int remaining, int userType) { |
| 215 | String message = null; |
| 216 | switch (userType) { |
| 217 | case USER_TYPE_PRIMARY: |
| 218 | message = mContext.getString(R.string.kg_failed_attempts_almost_at_wipe, |
| 219 | attempts, remaining); |
| 220 | break; |
| 221 | case USER_TYPE_SECONDARY_USER: |
| 222 | message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_user, |
| 223 | attempts, remaining); |
| 224 | break; |
| 225 | case USER_TYPE_WORK_PROFILE: |
| 226 | message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_profile, |
| 227 | attempts, remaining); |
| 228 | break; |
| 229 | } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 230 | showDialog(null, message); |
| 231 | } |
| 232 | |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 233 | private void showWipeDialog(int attempts, int userType) { |
| 234 | String message = null; |
| 235 | switch (userType) { |
| 236 | case USER_TYPE_PRIMARY: |
| 237 | message = mContext.getString(R.string.kg_failed_attempts_now_wiping, |
| 238 | attempts); |
| 239 | break; |
| 240 | case USER_TYPE_SECONDARY_USER: |
| 241 | message = mContext.getString(R.string.kg_failed_attempts_now_erasing_user, |
| 242 | attempts); |
| 243 | break; |
| 244 | case USER_TYPE_WORK_PROFILE: |
| 245 | message = mContext.getString(R.string.kg_failed_attempts_now_erasing_profile, |
| 246 | attempts); |
| 247 | break; |
| 248 | } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 249 | showDialog(null, message); |
| 250 | } |
| 251 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 252 | private void reportFailedUnlockAttempt(int timeoutMs) { |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 253 | final KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext); |
| 254 | final int failedAttempts = monitor.getFailedUnlockAttempts() + 1; // +1 for this time |
| 255 | |
| 256 | if (DEBUG) Log.d(TAG, "reportFailedPatternAttempt: #" + failedAttempts); |
| 257 | |
Adrian Roos | d6aa6cb | 2015-04-16 19:31:29 -0700 | [diff] [blame] | 258 | final int currentUser = KeyguardUpdateMonitor.getCurrentUser(); |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 259 | final DevicePolicyManager dpm = mLockPatternUtils.getDevicePolicyManager(); |
| 260 | final int failedAttemptsBeforeWipe = |
| 261 | dpm.getMaximumFailedPasswordsForWipe(null, currentUser); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 262 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 263 | final int remainingBeforeWipe = failedAttemptsBeforeWipe > 0 ? |
| 264 | (failedAttemptsBeforeWipe - failedAttempts) |
| 265 | : Integer.MAX_VALUE; // because DPM returns 0 if no restriction |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 266 | if (remainingBeforeWipe < LockPatternUtils.FAILED_ATTEMPTS_BEFORE_WIPE_GRACE) { |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 267 | // The user has installed a DevicePolicyManager that requests a user/profile to be wiped |
| 268 | // N attempts. Once we get below the grace period, we post this dialog every time as a |
| 269 | // clear warning until the deletion fires. |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 270 | // Check which profile has the strictest policy for failed password attempts |
| 271 | final int expiringUser = dpm.getProfileWithMinimumFailedPasswordsForWipe(currentUser); |
| 272 | int userType = USER_TYPE_PRIMARY; |
| 273 | if (expiringUser == currentUser) { |
Xiaohui Chen | cc791bc | 2015-08-26 14:54:34 -0700 | [diff] [blame] | 274 | // TODO: http://b/23522538 |
| 275 | if (expiringUser != UserHandle.USER_SYSTEM) { |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 276 | userType = USER_TYPE_SECONDARY_USER; |
| 277 | } |
| 278 | } else if (expiringUser != UserHandle.USER_NULL) { |
| 279 | userType = USER_TYPE_WORK_PROFILE; |
| 280 | } // If USER_NULL, which shouldn't happen, leave it as USER_TYPE_PRIMARY |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 281 | if (remainingBeforeWipe > 0) { |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 282 | showAlmostAtWipeDialog(failedAttempts, remainingBeforeWipe, userType); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 283 | } else { |
| 284 | // Too many attempts. The device will be wiped shortly. |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 285 | Slog.i(TAG, "Too many unlock attempts; user " + expiringUser + " will be wiped!"); |
| 286 | showWipeDialog(failedAttempts, userType); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 287 | } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 288 | } |
Jorim Jaggi | 25b4d4b | 2015-08-11 15:54:06 -0700 | [diff] [blame] | 289 | monitor.reportFailedStrongAuthUnlockAttempt(); |
Adrian Roos | d6aa6cb | 2015-04-16 19:31:29 -0700 | [diff] [blame] | 290 | mLockPatternUtils.reportFailedPasswordAttempt(KeyguardUpdateMonitor.getCurrentUser()); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 291 | if (timeoutMs > 0) { |
| 292 | showTimeoutDialog(timeoutMs); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Shows the primary security screen for the user. This will be either the multi-selector |
| 298 | * or the user's security method. |
| 299 | * @param turningOff true if the device is being turned off |
| 300 | */ |
| 301 | void showPrimarySecurityScreen(boolean turningOff) { |
| 302 | SecurityMode securityMode = mSecurityModel.getSecurityMode(); |
| 303 | if (DEBUG) Log.v(TAG, "showPrimarySecurityScreen(turningOff=" + turningOff + ")"); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 304 | showSecurityScreen(securityMode); |
| 305 | } |
| 306 | |
| 307 | /** |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 308 | * Shows the next security screen if there is one. |
| 309 | * @param authenticated true if the user entered the correct authentication |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 310 | * @return true if keyguard is done |
| 311 | */ |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 312 | boolean showNextSecurityScreenOrFinish(boolean authenticated) { |
| 313 | if (DEBUG) Log.d(TAG, "showNextSecurityScreenOrFinish(" + authenticated + ")"); |
| 314 | boolean finish = false; |
Jorim Jaggi | 25b4d4b | 2015-08-11 15:54:06 -0700 | [diff] [blame] | 315 | boolean strongAuth = false; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 316 | if (mUpdateMonitor.getUserCanSkipBouncer( |
| 317 | KeyguardUpdateMonitor.getCurrentUser())) { |
Adrian Roos | 336be7f | 2014-05-19 17:11:18 +0200 | [diff] [blame] | 318 | finish = true; |
| 319 | } else if (SecurityMode.None == mCurrentSecuritySelection) { |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 320 | SecurityMode securityMode = mSecurityModel.getSecurityMode(); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 321 | if (SecurityMode.None == securityMode) { |
| 322 | finish = true; // no security required |
| 323 | } else { |
| 324 | showSecurityScreen(securityMode); // switch to the alternate security view |
| 325 | } |
| 326 | } else if (authenticated) { |
| 327 | switch (mCurrentSecuritySelection) { |
| 328 | case Pattern: |
| 329 | case Password: |
| 330 | case PIN: |
Jorim Jaggi | 25b4d4b | 2015-08-11 15:54:06 -0700 | [diff] [blame] | 331 | strongAuth = true; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 332 | finish = true; |
| 333 | break; |
| 334 | |
| 335 | case SimPin: |
| 336 | case SimPuk: |
| 337 | // Shortcut for SIM PIN/PUK to go to directly to user's security screen or home |
| 338 | SecurityMode securityMode = mSecurityModel.getSecurityMode(); |
Selim Cinek | 245273e | 2015-06-24 13:17:56 -0400 | [diff] [blame] | 339 | if (securityMode != SecurityMode.None |
| 340 | || !mLockPatternUtils.isLockScreenDisabled( |
| 341 | KeyguardUpdateMonitor.getCurrentUser())) { |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 342 | showSecurityScreen(securityMode); |
| 343 | } else { |
| 344 | finish = true; |
| 345 | } |
| 346 | break; |
| 347 | |
| 348 | default: |
| 349 | Log.v(TAG, "Bad security screen " + mCurrentSecuritySelection + ", fail safe"); |
| 350 | showPrimarySecurityScreen(false); |
| 351 | break; |
| 352 | } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 353 | } |
| 354 | if (finish) { |
Jorim Jaggi | 25b4d4b | 2015-08-11 15:54:06 -0700 | [diff] [blame] | 355 | mSecurityCallback.finish(strongAuth); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 356 | } |
| 357 | return finish; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Switches to the given security view unless it's already being shown, in which case |
| 362 | * this is a no-op. |
| 363 | * |
| 364 | * @param securityMode |
| 365 | */ |
| 366 | private void showSecurityScreen(SecurityMode securityMode) { |
| 367 | if (DEBUG) Log.d(TAG, "showSecurityScreen(" + securityMode + ")"); |
| 368 | |
| 369 | if (securityMode == mCurrentSecuritySelection) return; |
| 370 | |
| 371 | KeyguardSecurityView oldView = getSecurityView(mCurrentSecuritySelection); |
| 372 | KeyguardSecurityView newView = getSecurityView(securityMode); |
| 373 | |
| 374 | // Emulate Activity life cycle |
| 375 | if (oldView != null) { |
| 376 | oldView.onPause(); |
| 377 | oldView.setKeyguardCallback(mNullCallback); // ignore requests from old view |
| 378 | } |
Jorim Jaggi | f479792 | 2014-08-04 22:49:41 +0200 | [diff] [blame] | 379 | if (securityMode != SecurityMode.None) { |
| 380 | newView.onResume(KeyguardSecurityView.VIEW_REVEALED); |
| 381 | newView.setKeyguardCallback(mCallback); |
| 382 | } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 383 | |
| 384 | // Find and show this child. |
| 385 | final int childCount = mSecurityViewFlipper.getChildCount(); |
| 386 | |
| 387 | final int securityViewIdForMode = getSecurityViewIdForMode(securityMode); |
| 388 | for (int i = 0; i < childCount; i++) { |
| 389 | if (mSecurityViewFlipper.getChildAt(i).getId() == securityViewIdForMode) { |
| 390 | mSecurityViewFlipper.setDisplayedChild(i); |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | mCurrentSecuritySelection = securityMode; |
Jorim Jaggi | f479792 | 2014-08-04 22:49:41 +0200 | [diff] [blame] | 396 | mSecurityCallback.onSecurityModeChanged(securityMode, |
| 397 | securityMode != SecurityMode.None && newView.needsInput()); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | private KeyguardSecurityViewFlipper getFlipper() { |
Chris Wren | 052999f | 2012-11-02 14:36:56 -0400 | [diff] [blame] | 401 | for (int i = 0; i < getChildCount(); i++) { |
| 402 | View child = getChildAt(i); |
| 403 | if (child instanceof KeyguardSecurityViewFlipper) { |
Chris Wren | c0ae9e6 | 2012-11-05 13:16:31 -0500 | [diff] [blame] | 404 | return (KeyguardSecurityViewFlipper) child; |
Chris Wren | 052999f | 2012-11-02 14:36:56 -0400 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | return null; |
| 408 | } |
| 409 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 410 | private KeyguardSecurityCallback mCallback = new KeyguardSecurityCallback() { |
Jorim Jaggi | b690f0d | 2014-07-03 23:25:44 +0200 | [diff] [blame] | 411 | public void userActivity() { |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 412 | if (mSecurityCallback != null) { |
Jorim Jaggi | b690f0d | 2014-07-03 23:25:44 +0200 | [diff] [blame] | 413 | mSecurityCallback.userActivity(); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | public void dismiss(boolean authenticated) { |
| 418 | mSecurityCallback.dismiss(authenticated); |
| 419 | } |
| 420 | |
| 421 | public boolean isVerifyUnlockOnly() { |
| 422 | return mIsVerifyUnlockOnly; |
| 423 | } |
| 424 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 425 | public void reportUnlockAttempt(boolean success, int timeoutMs) { |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 426 | KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext); |
| 427 | if (success) { |
| 428 | monitor.clearFailedUnlockAttempts(); |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 429 | mLockPatternUtils.reportSuccessfulPasswordAttempt( |
Adrian Roos | d6aa6cb | 2015-04-16 19:31:29 -0700 | [diff] [blame] | 430 | KeyguardUpdateMonitor.getCurrentUser()); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 431 | } else { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 432 | KeyguardSecurityContainer.this.reportFailedUnlockAttempt(timeoutMs); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Andrew Lee | 72b46d4 | 2015-01-30 13:23:21 -0800 | [diff] [blame] | 436 | public void reset() { |
| 437 | mSecurityCallback.reset(); |
| 438 | } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | // The following is used to ignore callbacks from SecurityViews that are no longer current |
| 442 | // (e.g. face unlock). This avoids unwanted asynchronous events from messing with the |
| 443 | // state for the current security method. |
| 444 | private KeyguardSecurityCallback mNullCallback = new KeyguardSecurityCallback() { |
| 445 | @Override |
Jorim Jaggi | b690f0d | 2014-07-03 23:25:44 +0200 | [diff] [blame] | 446 | public void userActivity() { } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 447 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 448 | public void reportUnlockAttempt(boolean success, int timeoutMs) { } |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 449 | @Override |
| 450 | public boolean isVerifyUnlockOnly() { return false; } |
| 451 | @Override |
| 452 | public void dismiss(boolean securityVerified) { } |
Andrew Lee | 72b46d4 | 2015-01-30 13:23:21 -0800 | [diff] [blame] | 453 | @Override |
| 454 | public void reset() {} |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | private int getSecurityViewIdForMode(SecurityMode securityMode) { |
| 458 | switch (securityMode) { |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 459 | case Pattern: return R.id.keyguard_pattern_view; |
| 460 | case PIN: return R.id.keyguard_pin_view; |
| 461 | case Password: return R.id.keyguard_password_view; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 462 | case SimPin: return R.id.keyguard_sim_pin_view; |
| 463 | case SimPuk: return R.id.keyguard_sim_puk_view; |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | private int getLayoutIdFor(SecurityMode securityMode) { |
| 469 | switch (securityMode) { |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 470 | case Pattern: return R.layout.keyguard_pattern_view; |
| 471 | case PIN: return R.layout.keyguard_pin_view; |
| 472 | case Password: return R.layout.keyguard_password_view; |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 473 | case SimPin: return R.layout.keyguard_sim_pin_view; |
| 474 | case SimPuk: return R.layout.keyguard_sim_puk_view; |
| 475 | default: |
| 476 | return 0; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | public SecurityMode getSecurityMode() { |
| 481 | return mSecurityModel.getSecurityMode(); |
| 482 | } |
| 483 | |
Jorim Jaggi | 95e89ca | 2014-11-24 20:12:50 +0100 | [diff] [blame] | 484 | public SecurityMode getCurrentSecurityMode() { |
| 485 | return mCurrentSecuritySelection; |
| 486 | } |
| 487 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 488 | public void verifyUnlock() { |
| 489 | mIsVerifyUnlockOnly = true; |
| 490 | showSecurityScreen(getSecurityMode()); |
| 491 | } |
| 492 | |
| 493 | public SecurityMode getCurrentSecuritySelection() { |
| 494 | return mCurrentSecuritySelection; |
| 495 | } |
| 496 | |
| 497 | public void dismiss(boolean authenticated) { |
| 498 | mCallback.dismiss(authenticated); |
| 499 | } |
| 500 | |
| 501 | public boolean needsInput() { |
| 502 | return mSecurityViewFlipper.needsInput(); |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public void setKeyguardCallback(KeyguardSecurityCallback callback) { |
| 507 | mSecurityViewFlipper.setKeyguardCallback(callback); |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public void reset() { |
| 512 | mSecurityViewFlipper.reset(); |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public KeyguardSecurityCallback getCallback() { |
| 517 | return mSecurityViewFlipper.getCallback(); |
| 518 | } |
| 519 | |
| 520 | @Override |
Selim Cinek | 3122fa8 | 2015-06-18 01:38:59 -0700 | [diff] [blame] | 521 | public void showPromptReason(int reason) { |
| 522 | if (mCurrentSecuritySelection != SecurityMode.None) { |
| 523 | getSecurityView(mCurrentSecuritySelection).showPromptReason(reason); |
| 524 | } |
| 525 | } |
| 526 | |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 527 | |
| 528 | public void showMessage(String message, int color) { |
| 529 | if (mCurrentSecuritySelection != SecurityMode.None) { |
| 530 | getSecurityView(mCurrentSecuritySelection).showMessage(message, color); |
| 531 | } |
| 532 | } |
| 533 | |
Selim Cinek | 3122fa8 | 2015-06-18 01:38:59 -0700 | [diff] [blame] | 534 | @Override |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 535 | public void showUsabilityHint() { |
| 536 | mSecurityViewFlipper.showUsabilityHint(); |
| 537 | } |
| 538 | |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 539 | } |
Chris Wren | c0ae9e6 | 2012-11-05 13:16:31 -0500 | [diff] [blame] | 540 | |