The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | */ |
| 16 | |
| 17 | package com.android.internal.widget; |
| 18 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 20 | import android.app.admin.DevicePolicyManager; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 21 | import android.app.trust.IStrongAuthTracker; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 22 | import android.app.trust.TrustManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 23 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 26 | import android.content.pm.UserInfo; |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 27 | import android.os.AsyncTask; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 29 | import android.os.IBinder; |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 30 | import android.os.Looper; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 31 | import android.os.Message; |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 32 | import android.os.RemoteException; |
| 33 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.SystemClock; |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 35 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 36 | import android.os.UserHandle; |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 37 | import android.os.UserManager; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 38 | import android.os.storage.IMountService; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 39 | import android.os.storage.StorageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
| 42 | import android.util.Log; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 43 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 45 | import com.google.android.collect.Lists; |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 46 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 47 | import java.lang.annotation.Retention; |
| 48 | import java.lang.annotation.RetentionPolicy; |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 49 | import java.nio.charset.StandardCharsets; |
Brian Carlstrom | 929a1c2 | 2011-02-01 21:54:09 -0800 | [diff] [blame] | 50 | import java.security.MessageDigest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import java.security.NoSuchAlgorithmException; |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 52 | import java.security.SecureRandom; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 53 | import java.util.ArrayList; |
| 54 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import java.util.List; |
| 56 | |
Adrian Roos | c2e0168 | 2015-01-15 23:20:20 +0100 | [diff] [blame] | 57 | import libcore.util.HexEncoding; |
| 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | /** |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 60 | * Utilities for the lock pattern and its settings. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | */ |
| 62 | public class LockPatternUtils { |
| 63 | |
| 64 | private static final String TAG = "LockPatternUtils"; |
Brian Colonna | a0ee004 | 2014-07-16 13:50:47 -0400 | [diff] [blame] | 65 | private static final boolean DEBUG = false; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | /** |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 68 | * The key to identify when the lock pattern enabled flag is being acccessed for legacy reasons. |
| 69 | */ |
| 70 | public static final String LEGACY_LOCK_PATTERN_ENABLED = "legacy_lock_pattern_enabled"; |
| 71 | |
| 72 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | * The number of incorrect attempts before which we fall back on an alternative |
| 74 | * method of verifying the user, and resetting their lock pattern. |
| 75 | */ |
| 76 | public static final int FAILED_ATTEMPTS_BEFORE_RESET = 20; |
| 77 | |
| 78 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * The interval of the countdown for showing progress of the lockout. |
| 80 | */ |
| 81 | public static final long FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS = 1000L; |
| 82 | |
Jim Miller | 4f36995 | 2011-08-19 18:29:22 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * This dictates when we start telling the user that continued failed attempts will wipe |
| 86 | * their device. |
| 87 | */ |
| 88 | public static final int FAILED_ATTEMPTS_BEFORE_WIPE_GRACE = 5; |
| 89 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | /** |
| 91 | * The minimum number of dots in a valid pattern. |
| 92 | */ |
| 93 | public static final int MIN_LOCK_PATTERN_SIZE = 4; |
| 94 | |
| 95 | /** |
Adrian Roos | f80e66ce9 | 2015-01-15 23:27:24 +0100 | [diff] [blame] | 96 | * The minimum size of a valid password. |
| 97 | */ |
| 98 | public static final int MIN_LOCK_PASSWORD_SIZE = 4; |
| 99 | |
| 100 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | * The minimum number of dots the user must include in a wrong pattern |
| 102 | * attempt for it to be counted against the counts that affect |
| 103 | * {@link #FAILED_ATTEMPTS_BEFORE_TIMEOUT} and {@link #FAILED_ATTEMPTS_BEFORE_RESET} |
| 104 | */ |
Jim Miller | 4f36995 | 2011-08-19 18:29:22 -0700 | [diff] [blame] | 105 | public static final int MIN_PATTERN_REGISTER_FAIL = MIN_LOCK_PATTERN_SIZE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 107 | @Deprecated |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 108 | public final static String LOCKOUT_PERMANENT_KEY = "lockscreen.lockedoutpermanently"; |
| 109 | public final static String LOCKOUT_ATTEMPT_DEADLINE = "lockscreen.lockoutattemptdeadline"; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 110 | public final static String LOCKOUT_ATTEMPT_TIMEOUT_MS = "lockscreen.lockoutattempttimeoutmss"; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 111 | public final static String PATTERN_EVER_CHOSEN_KEY = "lockscreen.patterneverchosen"; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 112 | public final static String PASSWORD_TYPE_KEY = "lockscreen.password_type"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 113 | @Deprecated |
| 114 | public final static String PASSWORD_TYPE_ALTERNATE_KEY = "lockscreen.password_type_alternate"; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 115 | public final static String LOCK_PASSWORD_SALT_KEY = "lockscreen.password_salt"; |
| 116 | public final static String DISABLE_LOCKSCREEN_KEY = "lockscreen.disabled"; |
| 117 | public final static String LOCKSCREEN_OPTIONS = "lockscreen.options"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 118 | @Deprecated |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 119 | public final static String LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK |
| 120 | = "lockscreen.biometric_weak_fallback"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 121 | @Deprecated |
Danielle Millett | 7a07219 | 2011-10-03 17:36:01 -0400 | [diff] [blame] | 122 | public final static String BIOMETRIC_WEAK_EVER_CHOSEN_KEY |
| 123 | = "lockscreen.biometricweakeverchosen"; |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 124 | public final static String LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS |
| 125 | = "lockscreen.power_button_instantly_locks"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 126 | @Deprecated |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 127 | public final static String LOCKSCREEN_WIDGETS_ENABLED = "lockscreen.widgets_enabled"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 129 | public final static String PASSWORD_HISTORY_KEY = "lockscreen.passwordhistory"; |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 130 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 131 | private static final String LOCK_SCREEN_OWNER_INFO = Settings.Secure.LOCK_SCREEN_OWNER_INFO; |
| 132 | private static final String LOCK_SCREEN_OWNER_INFO_ENABLED = |
| 133 | Settings.Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 134 | |
Andrei Stingaceanu | 6644cd9 | 2015-11-10 13:03:31 +0000 | [diff] [blame] | 135 | private static final String LOCK_SCREEN_DEVICE_OWNER_INFO = "lockscreen.device_owner_info"; |
| 136 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 137 | private static final String ENABLED_TRUST_AGENTS = "lockscreen.enabledtrustagents"; |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 138 | private static final String IS_TRUST_USUALLY_MANAGED = "lockscreen.istrustusuallymanaged"; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 139 | |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 140 | private static final String SEPARATE_PROFILE_CHALLENGE_KEY = "lockscreen.profilechallenge"; |
| 141 | |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 142 | // Maximum allowed number of repeated or ordered characters in a sequence before we'll |
| 143 | // consider it a complex PIN/password. |
| 144 | public static final int MAX_ALLOWED_SEQUENCE = 3; |
| 145 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 146 | private final Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | private final ContentResolver mContentResolver; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 148 | private DevicePolicyManager mDevicePolicyManager; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 149 | private ILockSettings mLockSettingsService; |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 150 | private UserManager mUserManager; |
Jim Miller | ee82f8f | 2012-10-01 16:26:18 -0700 | [diff] [blame] | 151 | |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 152 | /** |
| 153 | * Use {@link TrustManager#isTrustUsuallyManaged(int)}. |
| 154 | * |
| 155 | * This returns the lazily-peristed value and should only be used by TrustManagerService. |
| 156 | */ |
| 157 | public boolean isTrustUsuallyManaged(int userId) { |
| 158 | if (!(mLockSettingsService instanceof ILockSettings.Stub)) { |
| 159 | throw new IllegalStateException("May only be called by TrustManagerService. " |
| 160 | + "Use TrustManager.isTrustUsuallyManaged()"); |
| 161 | } |
| 162 | try { |
| 163 | return getLockSettings().getBoolean(IS_TRUST_USUALLY_MANAGED, false, userId); |
| 164 | } catch (RemoteException e) { |
| 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public void setTrustUsuallyManaged(boolean managed, int userId) { |
| 170 | try { |
| 171 | getLockSettings().setBoolean(IS_TRUST_USUALLY_MANAGED, managed, userId); |
| 172 | } catch (RemoteException e) { |
| 173 | // System dead. |
| 174 | } |
| 175 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 176 | |
| 177 | public static final class RequestThrottledException extends Exception { |
| 178 | private int mTimeoutMs; |
| 179 | public RequestThrottledException(int timeoutMs) { |
| 180 | mTimeoutMs = timeoutMs; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return The amount of time in ms before another request may |
| 185 | * be executed |
| 186 | */ |
| 187 | public int getTimeoutMs() { |
| 188 | return mTimeoutMs; |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 193 | public DevicePolicyManager getDevicePolicyManager() { |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 194 | if (mDevicePolicyManager == null) { |
| 195 | mDevicePolicyManager = |
| 196 | (DevicePolicyManager)mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 197 | if (mDevicePolicyManager == null) { |
| 198 | Log.e(TAG, "Can't get DevicePolicyManagerService: is it running?", |
| 199 | new IllegalStateException("Stack trace:")); |
| 200 | } |
| 201 | } |
| 202 | return mDevicePolicyManager; |
| 203 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 204 | |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 205 | private UserManager getUserManager() { |
| 206 | if (mUserManager == null) { |
| 207 | mUserManager = UserManager.get(mContext); |
| 208 | } |
| 209 | return mUserManager; |
| 210 | } |
| 211 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 212 | private TrustManager getTrustManager() { |
| 213 | TrustManager trust = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE); |
| 214 | if (trust == null) { |
| 215 | Log.e(TAG, "Can't get TrustManagerService: is it running?", |
| 216 | new IllegalStateException("Stack trace:")); |
| 217 | } |
| 218 | return trust; |
| 219 | } |
| 220 | |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 221 | public LockPatternUtils(Context context) { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 222 | mContext = context; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 223 | mContentResolver = context.getContentResolver(); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 224 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 225 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 226 | private ILockSettings getLockSettings() { |
| 227 | if (mLockSettingsService == null) { |
Adrian Roos | 450ce9f | 2014-10-29 14:30:37 +0100 | [diff] [blame] | 228 | ILockSettings service = ILockSettings.Stub.asInterface( |
| 229 | ServiceManager.getService("lock_settings")); |
Adrian Roos | 138b833 | 2014-11-11 13:51:07 +0100 | [diff] [blame] | 230 | mLockSettingsService = service; |
Brad Fitzpatrick | 9088100 | 2010-08-23 18:30:08 -0700 | [diff] [blame] | 231 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 232 | return mLockSettingsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 235 | public int getRequestedMinimumPasswordLength(int userId) { |
| 236 | return getDevicePolicyManager().getPasswordMinimumLength(null, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 239 | /** |
| 240 | * Gets the device policy password mode. If the mode is non-specific, returns |
| 241 | * MODE_PATTERN which allows the user to choose anything. |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 242 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 243 | public int getRequestedPasswordQuality(int userId) { |
| 244 | return getDevicePolicyManager().getPasswordQuality(null, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | private int getRequestedPasswordHistoryLength(int userId) { |
| 248 | return getDevicePolicyManager().getPasswordHistoryLength(null, userId); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 251 | public int getRequestedPasswordMinimumLetters(int userId) { |
| 252 | return getDevicePolicyManager().getPasswordMinimumLetters(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 255 | public int getRequestedPasswordMinimumUpperCase(int userId) { |
| 256 | return getDevicePolicyManager().getPasswordMinimumUpperCase(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 259 | public int getRequestedPasswordMinimumLowerCase(int userId) { |
| 260 | return getDevicePolicyManager().getPasswordMinimumLowerCase(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 263 | public int getRequestedPasswordMinimumNumeric(int userId) { |
| 264 | return getDevicePolicyManager().getPasswordMinimumNumeric(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 267 | public int getRequestedPasswordMinimumSymbols(int userId) { |
| 268 | return getDevicePolicyManager().getPasswordMinimumSymbols(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 271 | public int getRequestedPasswordMinimumNonLetter(int userId) { |
| 272 | return getDevicePolicyManager().getPasswordMinimumNonLetter(null, userId); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 273 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 274 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 275 | public void reportFailedPasswordAttempt(int userId) { |
Adrian Roos | 4f994eb | 2014-07-23 15:45:05 +0200 | [diff] [blame] | 276 | getDevicePolicyManager().reportFailedPasswordAttempt(userId); |
| 277 | getTrustManager().reportUnlockAttempt(false /* authenticated */, userId); |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 278 | requireStrongAuth(StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 281 | public void reportSuccessfulPasswordAttempt(int userId) { |
| 282 | getDevicePolicyManager().reportSuccessfulPasswordAttempt(userId); |
| 283 | getTrustManager().reportUnlockAttempt(true /* authenticated */, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Clara Bayarri | 51e41ad | 2016-02-11 17:48:53 +0000 | [diff] [blame] | 286 | public int getCurrentFailedPasswordAttempts(int userId) { |
| 287 | return getDevicePolicyManager().getCurrentFailedPasswordAttempts(userId); |
| 288 | } |
| 289 | |
| 290 | public int getMaximumFailedPasswordsForWipe(int userId) { |
| 291 | return getDevicePolicyManager().getMaximumFailedPasswordsForWipe( |
| 292 | null /* componentName */, userId); |
| 293 | } |
| 294 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | /** |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 296 | * Check to see if a pattern matches the saved pattern. |
| 297 | * If pattern matches, return an opaque attestation that the challenge |
| 298 | * was verified. |
| 299 | * |
| 300 | * @param pattern The pattern to check. |
| 301 | * @param challenge The challenge to verify against the pattern |
| 302 | * @return the attestation that the challenge was verified, or null. |
| 303 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 304 | public byte[] verifyPattern(List<LockPatternView.Cell> pattern, long challenge, int userId) |
| 305 | throws RequestThrottledException { |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 306 | throwIfCalledOnMainThread(); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 307 | try { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 308 | VerifyCredentialResponse response = |
| 309 | getLockSettings().verifyPattern(patternToString(pattern), challenge, userId); |
| 310 | if (response == null) { |
| 311 | // Shouldn't happen |
| 312 | return null; |
| 313 | } |
| 314 | |
| 315 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 316 | return response.getPayload(); |
| 317 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 318 | throw new RequestThrottledException(response.getTimeout()); |
| 319 | } else { |
| 320 | return null; |
| 321 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 322 | } catch (RemoteException re) { |
| 323 | return null; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | * Check to see if a pattern matches the saved pattern. If no pattern exists, |
| 329 | * always returns true. |
| 330 | * @param pattern The pattern to check. |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 331 | * @return Whether the pattern matches the stored one. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 333 | public boolean checkPattern(List<LockPatternView.Cell> pattern, int userId) |
| 334 | throws RequestThrottledException { |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 335 | throwIfCalledOnMainThread(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | try { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 337 | VerifyCredentialResponse response = |
| 338 | getLockSettings().checkPattern(patternToString(pattern), userId); |
| 339 | |
| 340 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 341 | return true; |
| 342 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 343 | throw new RequestThrottledException(response.getTimeout()); |
| 344 | } else { |
| 345 | return false; |
| 346 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 347 | } catch (RemoteException re) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | return true; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 353 | * Check to see if a password matches the saved password. |
| 354 | * If password matches, return an opaque attestation that the challenge |
| 355 | * was verified. |
| 356 | * |
| 357 | * @param password The password to check. |
| 358 | * @param challenge The challenge to verify against the password |
| 359 | * @return the attestation that the challenge was verified, or null. |
| 360 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 361 | public byte[] verifyPassword(String password, long challenge, int userId) |
| 362 | throws RequestThrottledException { |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 363 | throwIfCalledOnMainThread(); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 364 | try { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 365 | VerifyCredentialResponse response = |
| 366 | getLockSettings().verifyPassword(password, challenge, userId); |
| 367 | |
| 368 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 369 | return response.getPayload(); |
| 370 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 371 | throw new RequestThrottledException(response.getTimeout()); |
| 372 | } else { |
| 373 | return null; |
| 374 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 375 | } catch (RemoteException re) { |
| 376 | return null; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /** |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 381 | * Check to see if a password matches the saved password. If no password exists, |
| 382 | * always returns true. |
| 383 | * @param password The password to check. |
| 384 | * @return Whether the password matches the stored one. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 386 | public boolean checkPassword(String password, int userId) throws RequestThrottledException { |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 387 | throwIfCalledOnMainThread(); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 388 | try { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 389 | VerifyCredentialResponse response = |
| 390 | getLockSettings().checkPassword(password, userId); |
| 391 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 392 | return true; |
| 393 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 394 | throw new RequestThrottledException(response.getTimeout()); |
| 395 | } else { |
| 396 | return false; |
| 397 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 398 | } catch (RemoteException re) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 404 | * Check to see if vold already has the password. |
| 405 | * Note that this also clears vold's copy of the password. |
| 406 | * @return Whether the vold password matches or not. |
| 407 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 408 | public boolean checkVoldPassword(int userId) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 409 | try { |
| 410 | return getLockSettings().checkVoldPassword(userId); |
| 411 | } catch (RemoteException re) { |
| 412 | return false; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 417 | * Check to see if a password matches any of the passwords stored in the |
| 418 | * password history. |
| 419 | * |
| 420 | * @param password The password to check. |
| 421 | * @return Whether the password matches any in the history. |
| 422 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 423 | public boolean checkPasswordHistory(String password, int userId) { |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 424 | String passwordHashString = new String( |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 425 | passwordToHash(password, userId), StandardCharsets.UTF_8); |
| 426 | String passwordHistory = getString(PASSWORD_HISTORY_KEY, userId); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 427 | if (passwordHistory == null) { |
| 428 | return false; |
| 429 | } |
| 430 | // Password History may be too long... |
| 431 | int passwordHashLength = passwordHashString.length(); |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 432 | int passwordHistoryLength = getRequestedPasswordHistoryLength(userId); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 433 | if(passwordHistoryLength == 0) { |
| 434 | return false; |
| 435 | } |
| 436 | int neededPasswordHistoryLength = passwordHashLength * passwordHistoryLength |
| 437 | + passwordHistoryLength - 1; |
| 438 | if (passwordHistory.length() > neededPasswordHistoryLength) { |
| 439 | passwordHistory = passwordHistory.substring(0, neededPasswordHistoryLength); |
| 440 | } |
| 441 | return passwordHistory.contains(passwordHashString); |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 445 | * Check to see if the user has stored a lock pattern. |
| 446 | * @return Whether a saved pattern exists. |
| 447 | */ |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 448 | private boolean savedPatternExists(int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 449 | try { |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 450 | return getLockSettings().havePattern(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 451 | } catch (RemoteException re) { |
| 452 | return false; |
| 453 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Check to see if the user has stored a lock pattern. |
| 458 | * @return Whether a saved pattern exists. |
| 459 | */ |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 460 | private boolean savedPasswordExists(int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 461 | try { |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 462 | return getLockSettings().havePassword(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 463 | } catch (RemoteException re) { |
| 464 | return false; |
| 465 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 469 | * Return true if the user has ever chosen a pattern. This is true even if the pattern is |
| 470 | * currently cleared. |
| 471 | * |
| 472 | * @return True if the user has ever chosen a pattern. |
| 473 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 474 | public boolean isPatternEverChosen(int userId) { |
| 475 | return getBoolean(PATTERN_EVER_CHOSEN_KEY, false, userId); |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Used by device policy manager to validate the current password |
| 480 | * information it has. |
| 481 | */ |
| 482 | public int getActivePasswordQuality(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 483 | int quality = getKeyguardStoredPasswordQuality(userId); |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 484 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 485 | if (isLockPasswordEnabled(quality, userId)) { |
| 486 | // Quality is a password and a password exists. Return the quality. |
| 487 | return quality; |
| 488 | } |
| 489 | |
| 490 | if (isLockPatternEnabled(quality, userId)) { |
| 491 | // Quality is a pattern and a pattern exists. Return the quality. |
| 492 | return quality; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 493 | } |
Danielle Millett | c8fb532 | 2011-10-04 12:18:51 -0400 | [diff] [blame] | 494 | |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 495 | return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 496 | } |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 497 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 498 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 499 | * Clear any lock pattern or password. |
| 500 | */ |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 501 | public void clearLock(int userHandle) { |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 502 | setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userHandle); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 503 | |
| 504 | try { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 505 | getLockSettings().setLockPassword(null, null, userHandle); |
| 506 | getLockSettings().setLockPattern(null, null, userHandle); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 507 | } catch (RemoteException e) { |
| 508 | // well, we tried... |
| 509 | } |
| 510 | |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 511 | if (userHandle == UserHandle.USER_SYSTEM) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 512 | // Set the encryption password to default. |
| 513 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null); |
Robin Lee | d39113e | 2016-01-22 15:44:49 +0000 | [diff] [blame] | 514 | setCredentialRequiredToDecrypt(false); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | getDevicePolicyManager().setActivePasswordState( |
| 518 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, 0, 0, 0, 0, 0, 0, 0, userHandle); |
| 519 | |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 520 | onAfterChangingPassword(userHandle); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 521 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 522 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 523 | /** |
Benjamin Franz | 51ed794 | 2015-04-07 16:34:56 +0100 | [diff] [blame] | 524 | * Disable showing lock screen at all for a given user. |
| 525 | * This is only meaningful if pattern, pin or password are not set. |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 526 | * |
Benjamin Franz | 51ed794 | 2015-04-07 16:34:56 +0100 | [diff] [blame] | 527 | * @param disable Disables lock screen when true |
| 528 | * @param userId User ID of the user this has effect on |
| 529 | */ |
| 530 | public void setLockScreenDisabled(boolean disable, int userId) { |
| 531 | setBoolean(DISABLE_LOCKSCREEN_KEY, disable, userId); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Determine if LockScreen is disabled for the current user. This is used to decide whether |
| 536 | * LockScreen is shown after reboot or after screen timeout / short press on power. |
| 537 | * |
| 538 | * @return true if lock screen is disabled |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 539 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 540 | public boolean isLockScreenDisabled(int userId) { |
| 541 | return !isSecure(userId) && |
| 542 | getBoolean(DISABLE_LOCKSCREEN_KEY, false, userId); |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /** |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 546 | * Save a lock pattern. |
| 547 | * @param pattern The new pattern to save. |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 548 | * @param userId the user whose pattern is to be saved. |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 549 | */ |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 550 | public void saveLockPattern(List<LockPatternView.Cell> pattern, int userId) { |
| 551 | this.saveLockPattern(pattern, null, userId); |
| 552 | } |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 553 | /** |
| 554 | * Save a lock pattern. |
| 555 | * @param pattern The new pattern to save. |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 556 | * @param savedPattern The previously saved pattern, converted to String format |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 557 | * @param userId the user whose pattern is to be saved. |
| 558 | */ |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 559 | public void saveLockPattern(List<LockPatternView.Cell> pattern, String savedPattern, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | try { |
Adrian Roos | f80e66ce9 | 2015-01-15 23:27:24 +0100 | [diff] [blame] | 561 | if (pattern == null || pattern.size() < MIN_LOCK_PATTERN_SIZE) { |
| 562 | throw new IllegalArgumentException("pattern must not be null and at least " |
| 563 | + MIN_LOCK_PATTERN_SIZE + " dots long."); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 564 | } |
| 565 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 566 | getLockSettings().setLockPattern(patternToString(pattern), savedPattern, userId); |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 567 | DevicePolicyManager dpm = getDevicePolicyManager(); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 568 | |
| 569 | // Update the device encryption password. |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 570 | if (userId == UserHandle.USER_SYSTEM |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 571 | && LockPatternUtils.isDeviceEncryptionEnabled()) { |
Andrei Kapishnikov | 4eb6a36 | 2015-04-02 15:21:20 -0400 | [diff] [blame] | 572 | if (!shouldEncryptWithCredentials(true)) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 573 | clearEncryptionPassword(); |
| 574 | } else { |
| 575 | String stringPattern = patternToString(pattern); |
| 576 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_PATTERN, stringPattern); |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 577 | } |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 578 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 579 | |
| 580 | setBoolean(PATTERN_EVER_CHOSEN_KEY, true, userId); |
| 581 | |
| 582 | setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, userId); |
| 583 | dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, |
| 584 | pattern.size(), 0, 0, 0, 0, 0, 0, userId); |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 585 | onAfterChangingPassword(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 586 | } catch (RemoteException re) { |
| 587 | Log.e(TAG, "Couldn't save lock pattern " + re); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 591 | private void updateCryptoUserInfo(int userId) { |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 592 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 593 | return; |
| 594 | } |
| 595 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 596 | final String ownerInfo = isOwnerInfoEnabled(userId) ? getOwnerInfo(userId) : ""; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 597 | |
| 598 | IBinder service = ServiceManager.getService("mount"); |
| 599 | if (service == null) { |
| 600 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 605 | try { |
| 606 | Log.d(TAG, "Setting owner info"); |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 607 | mountService.setField(StorageManager.OWNER_INFO_KEY, ownerInfo); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 608 | } catch (RemoteException e) { |
| 609 | Log.e(TAG, "Error changing user info", e); |
| 610 | } |
| 611 | } |
| 612 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 613 | public void setOwnerInfo(String info, int userId) { |
| 614 | setString(LOCK_SCREEN_OWNER_INFO, info, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 615 | updateCryptoUserInfo(userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 618 | public void setOwnerInfoEnabled(boolean enabled, int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 619 | setBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, enabled, userId); |
| 620 | updateCryptoUserInfo(userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | public String getOwnerInfo(int userId) { |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 624 | return getString(LOCK_SCREEN_OWNER_INFO, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 627 | public boolean isOwnerInfoEnabled(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 628 | return getBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, false, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 629 | } |
| 630 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | /** |
Andrei Stingaceanu | 6644cd9 | 2015-11-10 13:03:31 +0000 | [diff] [blame] | 632 | * Sets the device owner information. If the information is {@code null} or empty then the |
| 633 | * device owner info is cleared. |
| 634 | * |
| 635 | * @param info Device owner information which will be displayed instead of the user |
| 636 | * owner info. |
| 637 | */ |
| 638 | public void setDeviceOwnerInfo(String info) { |
| 639 | if (info != null && info.isEmpty()) { |
| 640 | info = null; |
| 641 | } |
| 642 | |
| 643 | setString(LOCK_SCREEN_DEVICE_OWNER_INFO, info, UserHandle.USER_SYSTEM); |
| 644 | } |
| 645 | |
| 646 | public String getDeviceOwnerInfo() { |
| 647 | return getString(LOCK_SCREEN_DEVICE_OWNER_INFO, UserHandle.USER_SYSTEM); |
| 648 | } |
| 649 | |
| 650 | public boolean isDeviceOwnerInfoEnabled() { |
| 651 | return getDeviceOwnerInfo() != null; |
| 652 | } |
| 653 | |
| 654 | /** |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 655 | * Compute the password quality from the given password string. |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 656 | */ |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 657 | static public int computePasswordQuality(String password) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 658 | boolean hasDigit = false; |
| 659 | boolean hasNonDigit = false; |
| 660 | final int len = password.length(); |
| 661 | for (int i = 0; i < len; i++) { |
| 662 | if (Character.isDigit(password.charAt(i))) { |
| 663 | hasDigit = true; |
| 664 | } else { |
| 665 | hasNonDigit = true; |
| 666 | } |
| 667 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 668 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 669 | if (hasNonDigit && hasDigit) { |
| 670 | return DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 671 | } |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 672 | if (hasNonDigit) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 673 | return DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 674 | } |
| 675 | if (hasDigit) { |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 676 | return maxLengthSequence(password) > MAX_ALLOWED_SEQUENCE |
| 677 | ? DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
| 678 | : DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 679 | } |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 680 | return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 681 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 682 | |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 683 | private static int categoryChar(char c) { |
| 684 | if ('a' <= c && c <= 'z') return 0; |
| 685 | if ('A' <= c && c <= 'Z') return 1; |
| 686 | if ('0' <= c && c <= '9') return 2; |
| 687 | return 3; |
| 688 | } |
| 689 | |
| 690 | private static int maxDiffCategory(int category) { |
| 691 | if (category == 0 || category == 1) return 1; |
| 692 | else if (category == 2) return 10; |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * Returns the maximum length of a sequential characters. A sequence is defined as |
| 698 | * monotonically increasing characters with a constant interval or the same character repeated. |
| 699 | * |
| 700 | * For example: |
| 701 | * maxLengthSequence("1234") == 4 |
| 702 | * maxLengthSequence("1234abc") == 4 |
| 703 | * maxLengthSequence("aabc") == 3 |
| 704 | * maxLengthSequence("qwertyuio") == 1 |
| 705 | * maxLengthSequence("@ABC") == 3 |
| 706 | * maxLengthSequence(";;;;") == 4 (anything that repeats) |
| 707 | * maxLengthSequence(":;<=>") == 1 (ordered, but not composed of alphas or digits) |
| 708 | * |
| 709 | * @param string the pass |
| 710 | * @return the number of sequential letters or digits |
| 711 | */ |
| 712 | public static int maxLengthSequence(String string) { |
| 713 | if (string.length() == 0) return 0; |
| 714 | char previousChar = string.charAt(0); |
| 715 | int category = categoryChar(previousChar); //current category of the sequence |
| 716 | int diff = 0; //difference between two consecutive characters |
| 717 | boolean hasDiff = false; //if we are currently targeting a sequence |
| 718 | int maxLength = 0; //maximum length of a sequence already found |
| 719 | int startSequence = 0; //where the current sequence started |
| 720 | for (int current = 1; current < string.length(); current++) { |
| 721 | char currentChar = string.charAt(current); |
| 722 | int categoryCurrent = categoryChar(currentChar); |
| 723 | int currentDiff = (int) currentChar - (int) previousChar; |
| 724 | if (categoryCurrent != category || Math.abs(currentDiff) > maxDiffCategory(category)) { |
| 725 | maxLength = Math.max(maxLength, current - startSequence); |
| 726 | startSequence = current; |
| 727 | hasDiff = false; |
| 728 | category = categoryCurrent; |
| 729 | } |
| 730 | else { |
| 731 | if(hasDiff && currentDiff != diff) { |
| 732 | maxLength = Math.max(maxLength, current - startSequence); |
| 733 | startSequence = current - 1; |
| 734 | } |
| 735 | diff = currentDiff; |
| 736 | hasDiff = true; |
| 737 | } |
| 738 | previousChar = currentChar; |
| 739 | } |
| 740 | maxLength = Math.max(maxLength, string.length() - startSequence); |
| 741 | return maxLength; |
| 742 | } |
| 743 | |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 744 | /** Update the encryption password if it is enabled **/ |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 745 | private void updateEncryptionPassword(final int type, final String password) { |
Amith Yamasani | cd410ba | 2014-10-17 11:16:58 -0700 | [diff] [blame] | 746 | if (!isDeviceEncryptionEnabled()) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 747 | return; |
| 748 | } |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 749 | final IBinder service = ServiceManager.getService("mount"); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 750 | if (service == null) { |
| 751 | Log.e(TAG, "Could not find the mount service to update the encryption password"); |
| 752 | return; |
| 753 | } |
| 754 | |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 755 | new AsyncTask<Void, Void, Void>() { |
| 756 | @Override |
| 757 | protected Void doInBackground(Void... dummy) { |
| 758 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 759 | try { |
| 760 | mountService.changeEncryptionPassword(type, password); |
| 761 | } catch (RemoteException e) { |
| 762 | Log.e(TAG, "Error changing encryption password", e); |
| 763 | } |
| 764 | return null; |
| 765 | } |
| 766 | }.execute(); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 767 | } |
| 768 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 769 | /** |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 770 | * Save a lock password. Does not ensure that the password is as good |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 771 | * as the requested mode, but will adjust the mode to be as good as the |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 772 | * password. |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 773 | * @param password The password to save |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 774 | * @param savedPassword The previously saved lock password, or null if none |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 775 | * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 776 | * @param userHandle The userId of the user to change the password for |
| 777 | */ |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 778 | public void saveLockPassword(String password, String savedPassword, int quality, |
| 779 | int userHandle) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 780 | try { |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 781 | DevicePolicyManager dpm = getDevicePolicyManager(); |
Adrian Roos | f80e66ce9 | 2015-01-15 23:27:24 +0100 | [diff] [blame] | 782 | if (password == null || password.length() < MIN_LOCK_PASSWORD_SIZE) { |
| 783 | throw new IllegalArgumentException("password must not be null and at least " |
| 784 | + "of length " + MIN_LOCK_PASSWORD_SIZE); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 785 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 786 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 787 | getLockSettings().setLockPassword(password, savedPassword, userHandle); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 788 | int computedQuality = computePasswordQuality(password); |
| 789 | |
| 790 | // Update the device encryption password. |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 791 | if (userHandle == UserHandle.USER_SYSTEM |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 792 | && LockPatternUtils.isDeviceEncryptionEnabled()) { |
Andrei Kapishnikov | 4eb6a36 | 2015-04-02 15:21:20 -0400 | [diff] [blame] | 793 | if (!shouldEncryptWithCredentials(true)) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 794 | clearEncryptionPassword(); |
| 795 | } else { |
| 796 | boolean numeric = computedQuality |
| 797 | == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; |
| 798 | boolean numericComplex = computedQuality |
| 799 | == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; |
| 800 | int type = numeric || numericComplex ? StorageManager.CRYPT_TYPE_PIN |
| 801 | : StorageManager.CRYPT_TYPE_PASSWORD; |
| 802 | updateEncryptionPassword(type, password); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | setLong(PASSWORD_TYPE_KEY, Math.max(quality, computedQuality), userHandle); |
| 807 | if (computedQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 808 | int letters = 0; |
| 809 | int uppercase = 0; |
| 810 | int lowercase = 0; |
| 811 | int numbers = 0; |
| 812 | int symbols = 0; |
| 813 | int nonletter = 0; |
| 814 | for (int i = 0; i < password.length(); i++) { |
| 815 | char c = password.charAt(i); |
| 816 | if (c >= 'A' && c <= 'Z') { |
| 817 | letters++; |
| 818 | uppercase++; |
| 819 | } else if (c >= 'a' && c <= 'z') { |
| 820 | letters++; |
| 821 | lowercase++; |
| 822 | } else if (c >= '0' && c <= '9') { |
| 823 | numbers++; |
| 824 | nonletter++; |
| 825 | } else { |
| 826 | symbols++; |
| 827 | nonletter++; |
| 828 | } |
| 829 | } |
| 830 | dpm.setActivePasswordState(Math.max(quality, computedQuality), |
| 831 | password.length(), letters, uppercase, lowercase, |
| 832 | numbers, symbols, nonletter, userHandle); |
| 833 | } else { |
| 834 | // The password is not anything. |
| 835 | dpm.setActivePasswordState( |
| 836 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 837 | 0, 0, 0, 0, 0, 0, 0, userHandle); |
| 838 | } |
| 839 | |
| 840 | // Add the password to the password history. We assume all |
| 841 | // password hashes have the same length for simplicity of implementation. |
| 842 | String passwordHistory = getString(PASSWORD_HISTORY_KEY, userHandle); |
| 843 | if (passwordHistory == null) { |
| 844 | passwordHistory = ""; |
| 845 | } |
| 846 | int passwordHistoryLength = getRequestedPasswordHistoryLength(userHandle); |
| 847 | if (passwordHistoryLength == 0) { |
| 848 | passwordHistory = ""; |
| 849 | } else { |
| 850 | byte[] hash = passwordToHash(password, userHandle); |
| 851 | passwordHistory = new String(hash, StandardCharsets.UTF_8) + "," + passwordHistory; |
| 852 | // Cut it to contain passwordHistoryLength hashes |
| 853 | // and passwordHistoryLength -1 commas. |
| 854 | passwordHistory = passwordHistory.substring(0, Math.min(hash.length |
| 855 | * passwordHistoryLength + passwordHistoryLength - 1, passwordHistory |
| 856 | .length())); |
| 857 | } |
| 858 | setString(PASSWORD_HISTORY_KEY, passwordHistory, userHandle); |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 859 | onAfterChangingPassword(userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 860 | } catch (RemoteException re) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 861 | // Cant do much |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 862 | Log.e(TAG, "Unable to save lock password " + re); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 866 | /** |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 867 | * Gets whether the device is encrypted. |
| 868 | * |
| 869 | * @return Whether the device is encrypted. |
| 870 | */ |
| 871 | public static boolean isDeviceEncrypted() { |
| 872 | IMountService mountService = IMountService.Stub.asInterface( |
| 873 | ServiceManager.getService("mount")); |
| 874 | try { |
| 875 | return mountService.getEncryptionState() != IMountService.ENCRYPTION_STATE_NONE |
| 876 | && mountService.getPasswordType() != StorageManager.CRYPT_TYPE_DEFAULT; |
| 877 | } catch (RemoteException re) { |
| 878 | Log.e(TAG, "Error getting encryption state", re); |
| 879 | } |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | /** |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 884 | * Determine if the device supports encryption, even if it's set to default. This |
| 885 | * differs from isDeviceEncrypted() in that it returns true even if the device is |
| 886 | * encrypted with the default password. |
| 887 | * @return true if device encryption is enabled |
| 888 | */ |
| 889 | public static boolean isDeviceEncryptionEnabled() { |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 890 | return StorageManager.isEncrypted(); |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /** |
Paul Lawrence | 5c21c70 | 2016-01-29 13:26:19 -0800 | [diff] [blame] | 894 | * Determine if the device is file encrypted |
| 895 | * @return true if device is file encrypted |
| 896 | */ |
| 897 | public static boolean isFileEncryptionEnabled() { |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 898 | return StorageManager.isFileEncryptedNativeOrEmulated(); |
Paul Lawrence | 5c21c70 | 2016-01-29 13:26:19 -0800 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /** |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 902 | * Clears the encryption password. |
| 903 | */ |
| 904 | public void clearEncryptionPassword() { |
| 905 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null); |
| 906 | } |
| 907 | |
| 908 | /** |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 909 | * Retrieves the quality mode for {@param userHandle}. |
| 910 | * {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
| 911 | * |
| 912 | * @return stored password quality |
| 913 | */ |
| 914 | public int getKeyguardStoredPasswordQuality(int userHandle) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 915 | return (int) getLong(PASSWORD_TYPE_KEY, |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 916 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userHandle); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 919 | /** |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 920 | * Enables/disables the Separate Profile Challenge for this {@param userHandle}. This is a no-op |
| 921 | * for user handles that do not belong to a managed profile. |
| 922 | */ |
| 923 | public void setSeparateProfileChallengeEnabled(int userHandle, boolean enabled) { |
| 924 | UserInfo info = getUserManager().getUserInfo(userHandle); |
| 925 | if (info.isManagedProfile()) { |
| 926 | setBoolean(SEPARATE_PROFILE_CHALLENGE_KEY, enabled, userHandle); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * Retrieves whether the Separate Profile Challenge is enabled for this {@param userHandle}. |
| 932 | */ |
| 933 | public boolean isSeparateProfileChallengeEnabled(int userHandle) { |
| 934 | UserInfo info = getUserManager().getUserInfo(userHandle); |
Clara Bayarri | 8d35de8 | 2016-01-12 17:29:29 +0000 | [diff] [blame] | 935 | if (info == null || !info.isManagedProfile()) { |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 936 | return false; |
| 937 | } |
| 938 | return getBoolean(SEPARATE_PROFILE_CHALLENGE_KEY, false, userHandle); |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * Retrieves whether the current DPM allows use of the Profile Challenge. |
| 943 | */ |
| 944 | public boolean isSeparateProfileChallengeAllowed(int userHandle) { |
| 945 | UserInfo info = getUserManager().getUserInfo(userHandle); |
Clara Bayarri | 8d35de8 | 2016-01-12 17:29:29 +0000 | [diff] [blame] | 946 | if (info == null || !info.isManagedProfile()) { |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 947 | return false; |
| 948 | } |
| 949 | return getDevicePolicyManager().isSeparateProfileChallengeAllowed(userHandle); |
| 950 | } |
| 951 | |
| 952 | /** |
Clara Bayarri | d769391 | 2016-01-22 17:26:31 +0000 | [diff] [blame] | 953 | * Retrieves whether the current profile and device locks can be unified. |
| 954 | */ |
| 955 | public boolean isSeparateProfileChallengeAllowedToUnify(int userHandle) { |
| 956 | return getDevicePolicyManager().isProfileActivePasswordSufficientForParent(userHandle); |
| 957 | } |
| 958 | |
| 959 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | * Deserialize a pattern. |
| 961 | * @param string The pattern serialized with {@link #patternToString} |
| 962 | * @return The pattern. |
| 963 | */ |
| 964 | public static List<LockPatternView.Cell> stringToPattern(String string) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 965 | if (string == null) { |
| 966 | return null; |
| 967 | } |
| 968 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | List<LockPatternView.Cell> result = Lists.newArrayList(); |
| 970 | |
| 971 | final byte[] bytes = string.getBytes(); |
| 972 | for (int i = 0; i < bytes.length; i++) { |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 973 | byte b = (byte) (bytes[i] - '1'); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | result.add(LockPatternView.Cell.of(b / 3, b % 3)); |
| 975 | } |
| 976 | return result; |
| 977 | } |
| 978 | |
| 979 | /** |
| 980 | * Serialize a pattern. |
| 981 | * @param pattern The pattern. |
| 982 | * @return The pattern in string form. |
| 983 | */ |
| 984 | public static String patternToString(List<LockPatternView.Cell> pattern) { |
| 985 | if (pattern == null) { |
| 986 | return ""; |
| 987 | } |
| 988 | final int patternSize = pattern.size(); |
| 989 | |
| 990 | byte[] res = new byte[patternSize]; |
| 991 | for (int i = 0; i < patternSize; i++) { |
| 992 | LockPatternView.Cell cell = pattern.get(i); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 993 | res[i] = (byte) (cell.getRow() * 3 + cell.getColumn() + '1'); |
| 994 | } |
| 995 | return new String(res); |
| 996 | } |
| 997 | |
| 998 | public static String patternStringToBaseZero(String pattern) { |
| 999 | if (pattern == null) { |
| 1000 | return ""; |
| 1001 | } |
| 1002 | final int patternSize = pattern.length(); |
| 1003 | |
| 1004 | byte[] res = new byte[patternSize]; |
| 1005 | final byte[] bytes = pattern.getBytes(); |
| 1006 | for (int i = 0; i < patternSize; i++) { |
| 1007 | res[i] = (byte) (bytes[i] - '1'); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | } |
| 1009 | return new String(res); |
| 1010 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1011 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | /* |
| 1013 | * Generate an SHA-1 hash for the pattern. Not the most secure, but it is |
| 1014 | * at least a second level of protection. First level is that the file |
| 1015 | * is in a location only readable by the system process. |
| 1016 | * @param pattern the gesture pattern. |
| 1017 | * @return the hash of the pattern in a byte array. |
| 1018 | */ |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1019 | public static byte[] patternToHash(List<LockPatternView.Cell> pattern) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | if (pattern == null) { |
| 1021 | return null; |
| 1022 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1023 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | final int patternSize = pattern.size(); |
| 1025 | byte[] res = new byte[patternSize]; |
| 1026 | for (int i = 0; i < patternSize; i++) { |
| 1027 | LockPatternView.Cell cell = pattern.get(i); |
| 1028 | res[i] = (byte) (cell.getRow() * 3 + cell.getColumn()); |
| 1029 | } |
| 1030 | try { |
| 1031 | MessageDigest md = MessageDigest.getInstance("SHA-1"); |
| 1032 | byte[] hash = md.digest(res); |
| 1033 | return hash; |
| 1034 | } catch (NoSuchAlgorithmException nsa) { |
| 1035 | return res; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1039 | private String getSalt(int userId) { |
| 1040 | long salt = getLong(LOCK_PASSWORD_SALT_KEY, 0, userId); |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 1041 | if (salt == 0) { |
| 1042 | try { |
| 1043 | salt = SecureRandom.getInstance("SHA1PRNG").nextLong(); |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1044 | setLong(LOCK_PASSWORD_SALT_KEY, salt, userId); |
| 1045 | Log.v(TAG, "Initialized lock password salt for user: " + userId); |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 1046 | } catch (NoSuchAlgorithmException e) { |
| 1047 | // Throw an exception rather than storing a password we'll never be able to recover |
| 1048 | throw new IllegalStateException("Couldn't get SecureRandom number", e); |
| 1049 | } |
| 1050 | } |
| 1051 | return Long.toHexString(salt); |
| 1052 | } |
| 1053 | |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1054 | /* |
| 1055 | * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash. |
| 1056 | * Not the most secure, but it is at least a second level of protection. First level is that |
| 1057 | * the file is in a location only readable by the system process. |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1058 | * |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1059 | * @param password the gesture pattern. |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1060 | * |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1061 | * @return the hash of the pattern in a byte array. |
| 1062 | */ |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1063 | public byte[] passwordToHash(String password, int userId) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1064 | if (password == null) { |
| 1065 | return null; |
| 1066 | } |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1067 | |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1068 | try { |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1069 | byte[] saltedPassword = (password + getSalt(userId)).getBytes(); |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1070 | byte[] sha1 = MessageDigest.getInstance("SHA-1").digest(saltedPassword); |
| 1071 | byte[] md5 = MessageDigest.getInstance("MD5").digest(saltedPassword); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1072 | |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1073 | byte[] combined = new byte[sha1.length + md5.length]; |
| 1074 | System.arraycopy(sha1, 0, combined, 0, sha1.length); |
| 1075 | System.arraycopy(md5, 0, combined, sha1.length, md5.length); |
| 1076 | |
| 1077 | final char[] hexEncoded = HexEncoding.encode(combined); |
| 1078 | return new String(hexEncoded).getBytes(StandardCharsets.UTF_8); |
| 1079 | } catch (NoSuchAlgorithmException e) { |
| 1080 | throw new AssertionError("Missing digest algorithm: ", e); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1081 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /** |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1085 | * @param userId the user for which to report the value |
| 1086 | * @return Whether the lock screen is secured. |
| 1087 | */ |
| 1088 | public boolean isSecure(int userId) { |
| 1089 | int mode = getKeyguardStoredPasswordQuality(userId); |
| 1090 | return isLockPatternEnabled(mode, userId) || isLockPasswordEnabled(mode, userId); |
| 1091 | } |
| 1092 | |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 1093 | public boolean isLockPasswordEnabled(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1094 | return isLockPasswordEnabled(getKeyguardStoredPasswordQuality(userId), userId); |
| 1095 | } |
| 1096 | |
| 1097 | private boolean isLockPasswordEnabled(int mode, int userId) { |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1098 | final boolean passwordEnabled = mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC |
| 1099 | || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 1100 | || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1101 | || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC |
| 1102 | || mode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1103 | return passwordEnabled && savedPasswordExists(userId); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | /** |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 1107 | * @return Whether the lock pattern is enabled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | */ |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 1109 | public boolean isLockPatternEnabled(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1110 | return isLockPatternEnabled(getKeyguardStoredPasswordQuality(userId), userId); |
Danielle Millett | 925a7d8 | 2012-03-19 18:02:20 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 1113 | @Deprecated |
| 1114 | public boolean isLegacyLockPatternEnabled(int userId) { |
| 1115 | // Note: this value should default to {@code true} to avoid any reset that might result. |
| 1116 | // We must use a special key to read this value, since it will by default return the value |
| 1117 | // based on the new logic. |
| 1118 | return getBoolean(LEGACY_LOCK_PATTERN_ENABLED, true, userId); |
| 1119 | } |
| 1120 | |
| 1121 | @Deprecated |
| 1122 | public void setLegacyLockPatternEnabled(int userId) { |
| 1123 | setBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, true, userId); |
| 1124 | } |
| 1125 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1126 | private boolean isLockPatternEnabled(int mode, int userId) { |
| 1127 | return mode == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING |
| 1128 | && savedPatternExists(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * @return Whether the visible pattern is enabled. |
| 1133 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1134 | public boolean isVisiblePatternEnabled(int userId) { |
| 1135 | return getBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * Set whether the visible pattern is enabled. |
| 1140 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1141 | public void setVisiblePatternEnabled(boolean enabled, int userId) { |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 1142 | setBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, enabled, userId); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1143 | |
| 1144 | // Update for crypto if owner |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 1145 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1146 | return; |
| 1147 | } |
| 1148 | |
| 1149 | IBinder service = ServiceManager.getService("mount"); |
| 1150 | if (service == null) { |
| 1151 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 1156 | try { |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 1157 | mountService.setField(StorageManager.PATTERN_VISIBLE_KEY, enabled ? "1" : "0"); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1158 | } catch (RemoteException e) { |
| 1159 | Log.e(TAG, "Error changing pattern visible state", e); |
| 1160 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | /** |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1164 | * Set whether the visible password is enabled for cryptkeeper screen. |
| 1165 | */ |
| 1166 | public void setVisiblePasswordEnabled(boolean enabled, int userId) { |
| 1167 | // Update for crypto if owner |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 1168 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | IBinder service = ServiceManager.getService("mount"); |
| 1173 | if (service == null) { |
| 1174 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 1179 | try { |
| 1180 | mountService.setField(StorageManager.PASSWORD_VISIBLE_KEY, enabled ? "1" : "0"); |
| 1181 | } catch (RemoteException e) { |
| 1182 | Log.e(TAG, "Error changing password visible state", e); |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | * @return Whether tactile feedback for the pattern is enabled. |
| 1188 | */ |
| 1189 | public boolean isTactileFeedbackEnabled() { |
Jeff Sharkey | 5ed9d68 | 2012-10-10 14:28:27 -0700 | [diff] [blame] | 1190 | return Settings.System.getIntForUser(mContentResolver, |
| 1191 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 1, UserHandle.USER_CURRENT) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * Set and store the lockout deadline, meaning the user can't attempt his/her unlock |
| 1196 | * pattern until the deadline has passed. |
| 1197 | * @return the chosen deadline. |
| 1198 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1199 | public long setLockoutAttemptDeadline(int userId, int timeoutMs) { |
| 1200 | final long deadline = SystemClock.elapsedRealtime() + timeoutMs; |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1201 | setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline, userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1202 | setLong(LOCKOUT_ATTEMPT_TIMEOUT_MS, timeoutMs, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | return deadline; |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * @return The elapsed time in millis in the future when the user is allowed to |
| 1208 | * attempt to enter his/her lock pattern, or 0 if the user is welcome to |
| 1209 | * enter a pattern. |
| 1210 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1211 | public long getLockoutAttemptDeadline(int userId) { |
Andres Morales | a4e2337 | 2015-09-08 13:17:37 -0700 | [diff] [blame] | 1212 | long deadline = getLong(LOCKOUT_ATTEMPT_DEADLINE, 0L, userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1213 | final long timeoutMs = getLong(LOCKOUT_ATTEMPT_TIMEOUT_MS, 0L, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | final long now = SystemClock.elapsedRealtime(); |
Jorim Jaggi | e3e6d56 | 2015-09-28 13:57:37 -0700 | [diff] [blame] | 1215 | if (deadline < now && deadline != 0) { |
Andres Morales | a4e2337 | 2015-09-08 13:17:37 -0700 | [diff] [blame] | 1216 | // timeout expired |
| 1217 | setLong(LOCKOUT_ATTEMPT_DEADLINE, 0, userId); |
| 1218 | setLong(LOCKOUT_ATTEMPT_TIMEOUT_MS, 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | return 0L; |
| 1220 | } |
Andres Morales | a4e2337 | 2015-09-08 13:17:37 -0700 | [diff] [blame] | 1221 | |
| 1222 | if (deadline > (now + timeoutMs)) { |
| 1223 | // device was rebooted, set new deadline |
| 1224 | deadline = now + timeoutMs; |
| 1225 | setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline, userId); |
| 1226 | } |
| 1227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | return deadline; |
| 1229 | } |
| 1230 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1231 | private boolean getBoolean(String secureSettingKey, boolean defaultValue, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1232 | try { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1233 | return getLockSettings().getBoolean(secureSettingKey, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1234 | } catch (RemoteException re) { |
| 1235 | return defaultValue; |
| 1236 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1239 | private void setBoolean(String secureSettingKey, boolean enabled, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1240 | try { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1241 | getLockSettings().setBoolean(secureSettingKey, enabled, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1242 | } catch (RemoteException re) { |
| 1243 | // What can we do? |
| 1244 | Log.e(TAG, "Couldn't write boolean " + secureSettingKey + re); |
| 1245 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1248 | private long getLong(String secureSettingKey, long defaultValue, int userHandle) { |
| 1249 | try { |
| 1250 | return getLockSettings().getLong(secureSettingKey, defaultValue, userHandle); |
| 1251 | } catch (RemoteException re) { |
| 1252 | return defaultValue; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1256 | private void setLong(String secureSettingKey, long value, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1257 | try { |
Kenny Guy | 0cf1370 | 2013-11-29 16:33:05 +0000 | [diff] [blame] | 1258 | getLockSettings().setLong(secureSettingKey, value, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1259 | } catch (RemoteException re) { |
| 1260 | // What can we do? |
| 1261 | Log.e(TAG, "Couldn't write long " + secureSettingKey + re); |
| 1262 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1265 | private String getString(String secureSettingKey, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1266 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1267 | return getLockSettings().getString(secureSettingKey, null, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1268 | } catch (RemoteException re) { |
| 1269 | return null; |
| 1270 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1273 | private void setString(String secureSettingKey, String value, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1274 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1275 | getLockSettings().setString(secureSettingKey, value, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1276 | } catch (RemoteException re) { |
| 1277 | // What can we do? |
| 1278 | Log.e(TAG, "Couldn't write string " + secureSettingKey + re); |
| 1279 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1282 | public void setPowerButtonInstantlyLocks(boolean enabled, int userId) { |
| 1283 | setBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, enabled, userId); |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1286 | public boolean getPowerButtonInstantlyLocks(int userId) { |
| 1287 | return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true, userId); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | public void setEnabledTrustAgents(Collection<ComponentName> activeTrustAgents, int userId) { |
| 1291 | StringBuilder sb = new StringBuilder(); |
| 1292 | for (ComponentName cn : activeTrustAgents) { |
| 1293 | if (sb.length() > 0) { |
| 1294 | sb.append(','); |
| 1295 | } |
| 1296 | sb.append(cn.flattenToShortString()); |
| 1297 | } |
| 1298 | setString(ENABLED_TRUST_AGENTS, sb.toString(), userId); |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1299 | getTrustManager().reportEnabledTrustAgentsChanged(userId); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | public List<ComponentName> getEnabledTrustAgents(int userId) { |
| 1303 | String serialized = getString(ENABLED_TRUST_AGENTS, userId); |
| 1304 | if (TextUtils.isEmpty(serialized)) { |
| 1305 | return null; |
| 1306 | } |
| 1307 | String[] split = serialized.split(","); |
| 1308 | ArrayList<ComponentName> activeTrustAgents = new ArrayList<ComponentName>(split.length); |
| 1309 | for (String s : split) { |
| 1310 | if (!TextUtils.isEmpty(s)) { |
| 1311 | activeTrustAgents.add(ComponentName.unflattenFromString(s)); |
| 1312 | } |
| 1313 | } |
| 1314 | return activeTrustAgents; |
| 1315 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1316 | |
| 1317 | /** |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1318 | * Disable trust until credentials have been entered for user {@param userId}. |
| 1319 | * |
| 1320 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 1321 | * |
| 1322 | * @param userId either an explicit user id or {@link android.os.UserHandle#USER_ALL} |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1323 | */ |
| 1324 | public void requireCredentialEntry(int userId) { |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1325 | requireStrongAuth(StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST, userId); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /** |
| 1329 | * Requests strong authentication for user {@param userId}. |
| 1330 | * |
| 1331 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 1332 | * |
| 1333 | * @param strongAuthReason a combination of {@link StrongAuthTracker.StrongAuthFlags} indicating |
| 1334 | * the reason for and the strength of the requested authentication. |
| 1335 | * @param userId either an explicit user id or {@link android.os.UserHandle#USER_ALL} |
| 1336 | */ |
| 1337 | public void requireStrongAuth(@StrongAuthTracker.StrongAuthFlags int strongAuthReason, |
| 1338 | int userId) { |
| 1339 | try { |
| 1340 | getLockSettings().requireStrongAuth(strongAuthReason, userId); |
| 1341 | } catch (RemoteException e) { |
| 1342 | Log.e(TAG, "Error while requesting strong auth: " + e); |
| 1343 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1344 | } |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 1345 | |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 1346 | private void onAfterChangingPassword(int userHandle) { |
| 1347 | getTrustManager().reportEnabledTrustAgentsChanged(userHandle); |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 1348 | } |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1349 | |
| 1350 | public boolean isCredentialRequiredToDecrypt(boolean defaultValue) { |
| 1351 | final int value = Settings.Global.getInt(mContentResolver, |
| 1352 | Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, -1); |
| 1353 | return value == -1 ? defaultValue : (value != 0); |
| 1354 | } |
| 1355 | |
| 1356 | public void setCredentialRequiredToDecrypt(boolean required) { |
Robin Lee | d39113e | 2016-01-22 15:44:49 +0000 | [diff] [blame] | 1357 | if (!(getUserManager().isSystemUser() || getUserManager().isPrimaryUser())) { |
| 1358 | throw new IllegalStateException( |
| 1359 | "Only the system or primary user may call setCredentialRequiredForDecrypt()"); |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1360 | } |
Paul Lawrence | 688af6c | 2015-05-15 11:26:17 -0700 | [diff] [blame] | 1361 | |
| 1362 | if (isDeviceEncryptionEnabled()){ |
| 1363 | Settings.Global.putInt(mContext.getContentResolver(), |
| 1364 | Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, required ? 1 : 0); |
| 1365 | } |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1366 | } |
Andrei Kapishnikov | 4eb6a36 | 2015-04-02 15:21:20 -0400 | [diff] [blame] | 1367 | |
| 1368 | private boolean isDoNotAskCredentialsOnBootSet() { |
| 1369 | return mDevicePolicyManager.getDoNotAskCredentialsOnBoot(); |
| 1370 | } |
| 1371 | |
| 1372 | private boolean shouldEncryptWithCredentials(boolean defaultValue) { |
| 1373 | return isCredentialRequiredToDecrypt(defaultValue) && !isDoNotAskCredentialsOnBootSet(); |
| 1374 | } |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 1375 | |
| 1376 | private void throwIfCalledOnMainThread() { |
| 1377 | if (Looper.getMainLooper().isCurrentThread()) { |
| 1378 | throw new IllegalStateException("should not be called from the main thread."); |
| 1379 | } |
| 1380 | } |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1381 | |
| 1382 | public void registerStrongAuthTracker(final StrongAuthTracker strongAuthTracker) { |
| 1383 | try { |
| 1384 | getLockSettings().registerStrongAuthTracker(strongAuthTracker.mStub); |
| 1385 | } catch (RemoteException e) { |
| 1386 | throw new RuntimeException("Could not register StrongAuthTracker"); |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | public void unregisterStrongAuthTracker(final StrongAuthTracker strongAuthTracker) { |
| 1391 | try { |
| 1392 | getLockSettings().unregisterStrongAuthTracker(strongAuthTracker.mStub); |
| 1393 | } catch (RemoteException e) { |
| 1394 | Log.e(TAG, "Could not unregister StrongAuthTracker", e); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | /** |
| 1399 | * Tracks the global strong authentication state. |
| 1400 | */ |
| 1401 | public static class StrongAuthTracker { |
| 1402 | |
| 1403 | @IntDef(flag = true, |
| 1404 | value = { STRONG_AUTH_NOT_REQUIRED, |
| 1405 | STRONG_AUTH_REQUIRED_AFTER_BOOT, |
| 1406 | STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW, |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1407 | SOME_AUTH_REQUIRED_AFTER_USER_REQUEST}) |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1408 | @Retention(RetentionPolicy.SOURCE) |
| 1409 | public @interface StrongAuthFlags {} |
| 1410 | |
| 1411 | /** |
| 1412 | * Strong authentication is not required. |
| 1413 | */ |
| 1414 | public static final int STRONG_AUTH_NOT_REQUIRED = 0x0; |
| 1415 | |
| 1416 | /** |
| 1417 | * Strong authentication is required because the user has not authenticated since boot. |
| 1418 | */ |
| 1419 | public static final int STRONG_AUTH_REQUIRED_AFTER_BOOT = 0x1; |
| 1420 | |
| 1421 | /** |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1422 | * Strong authentication is required because a device admin has requested it. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1423 | */ |
| 1424 | public static final int STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW = 0x2; |
| 1425 | |
| 1426 | /** |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1427 | * Some authentication is required because the user has temporarily disabled trust. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1428 | */ |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1429 | public static final int SOME_AUTH_REQUIRED_AFTER_USER_REQUEST = 0x4; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1430 | |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 1431 | /** |
| 1432 | * Strong authentication is required because the user has been locked out after too many |
| 1433 | * attempts. |
| 1434 | */ |
| 1435 | public static final int STRONG_AUTH_REQUIRED_AFTER_LOCKOUT = 0x8; |
| 1436 | |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1437 | /** |
| 1438 | * Some authentication is required because the user has entered a wrong credential. |
| 1439 | */ |
| 1440 | public static final int SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL = 0x10; |
| 1441 | |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1442 | private static final int ALLOWING_FINGERPRINT = STRONG_AUTH_NOT_REQUIRED |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1443 | | SOME_AUTH_REQUIRED_AFTER_USER_REQUEST |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1444 | | SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1445 | |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1446 | private final SparseIntArray mStrongAuthRequiredForUser = new SparseIntArray(); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1447 | private final H mHandler; |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1448 | private final int mDefaultStrongAuthFlags; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1449 | |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1450 | public StrongAuthTracker(Context context) { |
| 1451 | this(context, Looper.myLooper()); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * @param looper the looper on whose thread calls to {@link #onStrongAuthRequiredChanged} |
| 1456 | * will be scheduled. |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1457 | * @param context the current {@link Context} |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1458 | */ |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1459 | public StrongAuthTracker(Context context, Looper looper) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1460 | mHandler = new H(looper); |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1461 | mDefaultStrongAuthFlags = getDefaultFlags(context); |
| 1462 | } |
| 1463 | |
| 1464 | public static @StrongAuthFlags int getDefaultFlags(Context context) { |
| 1465 | boolean strongAuthRequired = context.getResources().getBoolean( |
| 1466 | com.android.internal.R.bool.config_strongAuthRequiredOnBoot); |
| 1467 | return strongAuthRequired ? STRONG_AUTH_REQUIRED_AFTER_BOOT : STRONG_AUTH_NOT_REQUIRED; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * Returns {@link #STRONG_AUTH_NOT_REQUIRED} if strong authentication is not required, |
| 1472 | * otherwise returns a combination of {@link StrongAuthFlags} indicating why strong |
| 1473 | * authentication is required. |
| 1474 | * |
| 1475 | * @param userId the user for whom the state is queried. |
| 1476 | */ |
| 1477 | public @StrongAuthFlags int getStrongAuthForUser(int userId) { |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1478 | return mStrongAuthRequiredForUser.get(userId, mDefaultStrongAuthFlags); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * @return true if unlocking with trust alone is allowed for {@param userId} by the current |
| 1483 | * strong authentication requirements. |
| 1484 | */ |
| 1485 | public boolean isTrustAllowedForUser(int userId) { |
| 1486 | return getStrongAuthForUser(userId) == STRONG_AUTH_NOT_REQUIRED; |
| 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * @return true if unlocking with fingerprint alone is allowed for {@param userId} by the |
| 1491 | * current strong authentication requirements. |
| 1492 | */ |
| 1493 | public boolean isFingerprintAllowedForUser(int userId) { |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1494 | return (getStrongAuthForUser(userId) & ~ALLOWING_FINGERPRINT) == 0; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * Called when the strong authentication requirements for {@param userId} changed. |
| 1499 | */ |
| 1500 | public void onStrongAuthRequiredChanged(int userId) { |
| 1501 | } |
| 1502 | |
| 1503 | void handleStrongAuthRequiredChanged(@StrongAuthFlags int strongAuthFlags, |
| 1504 | int userId) { |
| 1505 | |
| 1506 | int oldValue = getStrongAuthForUser(userId); |
| 1507 | if (strongAuthFlags != oldValue) { |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1508 | if (strongAuthFlags == mDefaultStrongAuthFlags) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1509 | mStrongAuthRequiredForUser.delete(userId); |
| 1510 | } else { |
| 1511 | mStrongAuthRequiredForUser.put(userId, strongAuthFlags); |
| 1512 | } |
| 1513 | onStrongAuthRequiredChanged(userId); |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | |
| 1518 | final IStrongAuthTracker.Stub mStub = new IStrongAuthTracker.Stub() { |
| 1519 | @Override |
| 1520 | public void onStrongAuthRequiredChanged(@StrongAuthFlags int strongAuthFlags, |
| 1521 | int userId) { |
| 1522 | mHandler.obtainMessage(H.MSG_ON_STRONG_AUTH_REQUIRED_CHANGED, |
| 1523 | strongAuthFlags, userId).sendToTarget(); |
| 1524 | } |
| 1525 | }; |
| 1526 | |
| 1527 | private class H extends Handler { |
| 1528 | static final int MSG_ON_STRONG_AUTH_REQUIRED_CHANGED = 1; |
| 1529 | |
| 1530 | public H(Looper looper) { |
| 1531 | super(looper); |
| 1532 | } |
| 1533 | |
| 1534 | @Override |
| 1535 | public void handleMessage(Message msg) { |
| 1536 | switch (msg.what) { |
| 1537 | case MSG_ON_STRONG_AUTH_REQUIRED_CHANGED: |
| 1538 | handleStrongAuthRequiredChanged(msg.arg1, msg.arg2); |
| 1539 | break; |
| 1540 | } |
| 1541 | } |
| 1542 | }; |
| 1543 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1544 | } |