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