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 | |
Jim Miller | 78c48f6 | 2013-03-01 15:28:33 -0800 | [diff] [blame] | 19 | import android.Manifest; |
Amith Yamasani | 4b4b954 | 2012-09-14 13:36:29 -0700 | [diff] [blame] | 20 | import android.app.ActivityManagerNative; |
Adrian Roos | edad7b2 | 2014-07-13 14:38:22 +0200 | [diff] [blame] | 21 | import android.app.AlarmManager; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 22 | import android.app.admin.DevicePolicyManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 23 | import android.app.trust.TrustManager; |
Jim Miller | 5111726 | 2012-11-04 17:58:09 -0800 | [diff] [blame] | 24 | import android.appwidget.AppWidgetManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 25 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Steven Ross | 329979c | 2011-09-28 11:42:56 -0400 | [diff] [blame] | 28 | import android.content.Intent; |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
Adam Lesinski | 61d9409 | 2014-09-15 15:27:19 -0700 | [diff] [blame] | 30 | import android.content.pm.UserInfo; |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 31 | import android.os.AsyncTask; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 32 | import android.os.IBinder; |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 33 | import android.os.RemoteException; |
| 34 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.SystemClock; |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 36 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 37 | import android.os.UserHandle; |
Adam Lesinski | 61d9409 | 2014-09-15 15:27:19 -0700 | [diff] [blame] | 38 | import android.os.UserManager; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 39 | import android.os.storage.IMountService; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 40 | import android.os.storage.StorageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.provider.Settings; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 42 | import android.telecom.TelecomManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.text.TextUtils; |
| 44 | import android.util.Log; |
Jim Miller | 5111726 | 2012-11-04 17:58:09 -0800 | [diff] [blame] | 45 | import android.view.IWindowManager; |
John Wang | 0f7b3f8 | 2011-05-31 11:20:55 -0700 | [diff] [blame] | 46 | import android.view.View; |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 47 | import android.widget.Button; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 49 | import com.android.internal.R; |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 50 | import com.google.android.collect.Lists; |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 51 | |
Brian Carlstrom | 929a1c2 | 2011-02-01 21:54:09 -0800 | [diff] [blame] | 52 | import java.security.MessageDigest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import java.security.NoSuchAlgorithmException; |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 54 | import java.security.SecureRandom; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 55 | import java.util.ArrayList; |
| 56 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import java.util.List; |
| 58 | |
| 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 | /** |
| 68 | * The maximum number of incorrect attempts before the user is prevented |
| 69 | * from trying again for {@link #FAILED_ATTEMPT_TIMEOUT_MS}. |
| 70 | */ |
| 71 | public static final int FAILED_ATTEMPTS_BEFORE_TIMEOUT = 5; |
| 72 | |
| 73 | /** |
| 74 | * The number of incorrect attempts before which we fall back on an alternative |
| 75 | * method of verifying the user, and resetting their lock pattern. |
| 76 | */ |
| 77 | public static final int FAILED_ATTEMPTS_BEFORE_RESET = 20; |
| 78 | |
| 79 | /** |
| 80 | * How long the user is prevented from trying again after entering the |
| 81 | * wrong pattern too many times. |
| 82 | */ |
| 83 | public static final long FAILED_ATTEMPT_TIMEOUT_MS = 30000L; |
| 84 | |
| 85 | /** |
| 86 | * The interval of the countdown for showing progress of the lockout. |
| 87 | */ |
| 88 | public static final long FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS = 1000L; |
| 89 | |
Jim Miller | 4f36995 | 2011-08-19 18:29:22 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * This dictates when we start telling the user that continued failed attempts will wipe |
| 93 | * their device. |
| 94 | */ |
| 95 | public static final int FAILED_ATTEMPTS_BEFORE_WIPE_GRACE = 5; |
| 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | /** |
| 98 | * The minimum number of dots in a valid pattern. |
| 99 | */ |
| 100 | public static final int MIN_LOCK_PATTERN_SIZE = 4; |
| 101 | |
| 102 | /** |
| 103 | * The minimum number of dots the user must include in a wrong pattern |
| 104 | * attempt for it to be counted against the counts that affect |
| 105 | * {@link #FAILED_ATTEMPTS_BEFORE_TIMEOUT} and {@link #FAILED_ATTEMPTS_BEFORE_RESET} |
| 106 | */ |
Jim Miller | 4f36995 | 2011-08-19 18:29:22 -0700 | [diff] [blame] | 107 | 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] | 108 | |
Danielle Millett | 925a7d8 | 2012-03-19 18:02:20 -0400 | [diff] [blame] | 109 | /** |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 110 | * Tells the keyguard to show the user switcher when the keyguard is created. |
| 111 | */ |
| 112 | public static final String KEYGUARD_SHOW_USER_SWITCHER = "showuserswitcher"; |
| 113 | |
| 114 | /** |
| 115 | * Tells the keyguard to show the security challenge when the keyguard is created. |
| 116 | */ |
| 117 | public static final String KEYGUARD_SHOW_SECURITY_CHALLENGE = "showsecuritychallenge"; |
| 118 | |
| 119 | /** |
Michael Jurka | 76017ca | 2012-11-06 16:21:09 -0800 | [diff] [blame] | 120 | * Tells the keyguard to show the widget with the specified id when the keyguard is created. |
| 121 | */ |
| 122 | public static final String KEYGUARD_SHOW_APPWIDGET = "showappwidget"; |
| 123 | |
| 124 | /** |
Danielle Millett | 925a7d8 | 2012-03-19 18:02:20 -0400 | [diff] [blame] | 125 | * The bit in LOCK_BIOMETRIC_WEAK_FLAGS to be used to indicate whether liveliness should |
| 126 | * be used |
| 127 | */ |
| 128 | public static final int FLAG_BIOMETRIC_WEAK_LIVELINESS = 0x1; |
| 129 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Pseudo-appwidget id we use to represent the default clock status widget |
| 132 | */ |
| 133 | public static final int ID_DEFAULT_STATUS_WIDGET = -2; |
| 134 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 135 | public final static String LOCKOUT_PERMANENT_KEY = "lockscreen.lockedoutpermanently"; |
| 136 | public final static String LOCKOUT_ATTEMPT_DEADLINE = "lockscreen.lockoutattemptdeadline"; |
| 137 | public final static String PATTERN_EVER_CHOSEN_KEY = "lockscreen.patterneverchosen"; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 138 | public final static String PASSWORD_TYPE_KEY = "lockscreen.password_type"; |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 139 | public static final String PASSWORD_TYPE_ALTERNATE_KEY = "lockscreen.password_type_alternate"; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 140 | public final static String LOCK_PASSWORD_SALT_KEY = "lockscreen.password_salt"; |
| 141 | public final static String DISABLE_LOCKSCREEN_KEY = "lockscreen.disabled"; |
| 142 | public final static String LOCKSCREEN_OPTIONS = "lockscreen.options"; |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 143 | public final static String LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK |
| 144 | = "lockscreen.biometric_weak_fallback"; |
Danielle Millett | 7a07219 | 2011-10-03 17:36:01 -0400 | [diff] [blame] | 145 | public final static String BIOMETRIC_WEAK_EVER_CHOSEN_KEY |
| 146 | = "lockscreen.biometricweakeverchosen"; |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 147 | public final static String LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS |
| 148 | = "lockscreen.power_button_instantly_locks"; |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 149 | 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] | 150 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 151 | public final static String PASSWORD_HISTORY_KEY = "lockscreen.passwordhistory"; |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 152 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 153 | private static final String LOCK_SCREEN_OWNER_INFO = Settings.Secure.LOCK_SCREEN_OWNER_INFO; |
| 154 | private static final String LOCK_SCREEN_OWNER_INFO_ENABLED = |
| 155 | Settings.Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 156 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 157 | private static final String ENABLED_TRUST_AGENTS = "lockscreen.enabledtrustagents"; |
| 158 | |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 159 | // Maximum allowed number of repeated or ordered characters in a sequence before we'll |
| 160 | // consider it a complex PIN/password. |
| 161 | public static final int MAX_ALLOWED_SEQUENCE = 3; |
| 162 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 163 | private final Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | private final ContentResolver mContentResolver; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 165 | private DevicePolicyManager mDevicePolicyManager; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 166 | private ILockSettings mLockSettingsService; |
Jim Miller | ee82f8f | 2012-10-01 16:26:18 -0700 | [diff] [blame] | 167 | |
Jim Miller | 78c48f6 | 2013-03-01 15:28:33 -0800 | [diff] [blame] | 168 | private final boolean mMultiUserMode; |
| 169 | |
Jim Miller | ee82f8f | 2012-10-01 16:26:18 -0700 | [diff] [blame] | 170 | // The current user is set by KeyguardViewMediator and shared by all LockPatternUtils. |
| 171 | private static volatile int sCurrentUserId = UserHandle.USER_NULL; |
Dianne Hackborn | de4c26f | 2011-07-17 13:42:47 -0700 | [diff] [blame] | 172 | |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 173 | public DevicePolicyManager getDevicePolicyManager() { |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 174 | if (mDevicePolicyManager == null) { |
| 175 | mDevicePolicyManager = |
| 176 | (DevicePolicyManager)mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 177 | if (mDevicePolicyManager == null) { |
| 178 | Log.e(TAG, "Can't get DevicePolicyManagerService: is it running?", |
| 179 | new IllegalStateException("Stack trace:")); |
| 180 | } |
| 181 | } |
| 182 | return mDevicePolicyManager; |
| 183 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 184 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 185 | private TrustManager getTrustManager() { |
| 186 | TrustManager trust = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE); |
| 187 | if (trust == null) { |
| 188 | Log.e(TAG, "Can't get TrustManagerService: is it running?", |
| 189 | new IllegalStateException("Stack trace:")); |
| 190 | } |
| 191 | return trust; |
| 192 | } |
| 193 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | /** |
| 195 | * @param contentResolver Used to look up and save settings. |
| 196 | */ |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 197 | public LockPatternUtils(Context context) { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 198 | mContext = context; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 199 | mContentResolver = context.getContentResolver(); |
Jim Miller | 78c48f6 | 2013-03-01 15:28:33 -0800 | [diff] [blame] | 200 | |
| 201 | // If this is being called by the system or by an application like keyguard that |
| 202 | // has permision INTERACT_ACROSS_USERS, then LockPatternUtils will operate in multi-user |
| 203 | // mode where calls are for the current user rather than the user of the calling process. |
| 204 | mMultiUserMode = context.checkCallingOrSelfPermission( |
| 205 | Manifest.permission.INTERACT_ACROSS_USERS_FULL) == PackageManager.PERMISSION_GRANTED; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 206 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 207 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 208 | private ILockSettings getLockSettings() { |
| 209 | if (mLockSettingsService == null) { |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 210 | mLockSettingsService = LockPatternUtilsCache.getInstance( |
| 211 | ILockSettings.Stub.asInterface(ServiceManager.getService("lock_settings"))); |
Brad Fitzpatrick | 9088100 | 2010-08-23 18:30:08 -0700 | [diff] [blame] | 212 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 213 | return mLockSettingsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 216 | public int getRequestedMinimumPasswordLength() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 217 | return getDevicePolicyManager().getPasswordMinimumLength(null, getCurrentOrCallingUserId()); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 220 | /** |
| 221 | * Gets the device policy password mode. If the mode is non-specific, returns |
| 222 | * MODE_PATTERN which allows the user to choose anything. |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 223 | */ |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 224 | public int getRequestedPasswordQuality() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 225 | return getDevicePolicyManager().getPasswordQuality(null, getCurrentOrCallingUserId()); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 228 | public int getRequestedPasswordHistoryLength() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 229 | return getDevicePolicyManager().getPasswordHistoryLength(null, getCurrentOrCallingUserId()); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 232 | public int getRequestedPasswordMinimumLetters() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 233 | return getDevicePolicyManager().getPasswordMinimumLetters(null, |
| 234 | getCurrentOrCallingUserId()); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | public int getRequestedPasswordMinimumUpperCase() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 238 | return getDevicePolicyManager().getPasswordMinimumUpperCase(null, |
| 239 | getCurrentOrCallingUserId()); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | public int getRequestedPasswordMinimumLowerCase() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 243 | return getDevicePolicyManager().getPasswordMinimumLowerCase(null, |
| 244 | getCurrentOrCallingUserId()); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | public int getRequestedPasswordMinimumNumeric() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 248 | return getDevicePolicyManager().getPasswordMinimumNumeric(null, |
| 249 | getCurrentOrCallingUserId()); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | public int getRequestedPasswordMinimumSymbols() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 253 | return getDevicePolicyManager().getPasswordMinimumSymbols(null, |
| 254 | getCurrentOrCallingUserId()); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 257 | public int getRequestedPasswordMinimumNonLetter() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 258 | return getDevicePolicyManager().getPasswordMinimumNonLetter(null, |
| 259 | getCurrentOrCallingUserId()); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 260 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 261 | |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 262 | public void reportFailedPasswordAttempt() { |
Adrian Roos | 4f994eb | 2014-07-23 15:45:05 +0200 | [diff] [blame] | 263 | int userId = getCurrentOrCallingUserId(); |
| 264 | getDevicePolicyManager().reportFailedPasswordAttempt(userId); |
| 265 | getTrustManager().reportUnlockAttempt(false /* authenticated */, userId); |
| 266 | getTrustManager().reportRequireCredentialEntry(userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | public void reportSuccessfulPasswordAttempt() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 270 | getDevicePolicyManager().reportSuccessfulPasswordAttempt(getCurrentOrCallingUserId()); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 271 | getTrustManager().reportUnlockAttempt(true /* authenticated */, |
| 272 | getCurrentOrCallingUserId()); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 275 | public void setCurrentUser(int userId) { |
Jim Miller | ee82f8f | 2012-10-01 16:26:18 -0700 | [diff] [blame] | 276 | sCurrentUserId = userId; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Amith Yamasani | f882f1a | 2012-04-10 15:13:39 -0700 | [diff] [blame] | 279 | public int getCurrentUser() { |
Jim Miller | ee82f8f | 2012-10-01 16:26:18 -0700 | [diff] [blame] | 280 | if (sCurrentUserId != UserHandle.USER_NULL) { |
Jim Miller | 25645d8 | 2012-09-21 14:47:54 -0700 | [diff] [blame] | 281 | // Someone is regularly updating using setCurrentUser() use that value. |
Jim Miller | ee82f8f | 2012-10-01 16:26:18 -0700 | [diff] [blame] | 282 | return sCurrentUserId; |
Jim Miller | 25645d8 | 2012-09-21 14:47:54 -0700 | [diff] [blame] | 283 | } |
| 284 | try { |
| 285 | return ActivityManagerNative.getDefault().getCurrentUser().id; |
| 286 | } catch (RemoteException re) { |
| 287 | return UserHandle.USER_OWNER; |
Amith Yamasani | f882f1a | 2012-04-10 15:13:39 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 291 | public void removeUser(int userId) { |
Jim Miller | 25645d8 | 2012-09-21 14:47:54 -0700 | [diff] [blame] | 292 | try { |
| 293 | getLockSettings().removeUser(userId); |
| 294 | } catch (RemoteException re) { |
| 295 | Log.e(TAG, "Couldn't remove lock settings for user " + userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | private int getCurrentOrCallingUserId() { |
Jim Miller | 78c48f6 | 2013-03-01 15:28:33 -0800 | [diff] [blame] | 300 | if (mMultiUserMode) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 301 | // TODO: This is a little inefficient. See if all users of this are able to |
| 302 | // handle USER_CURRENT and pass that instead. |
| 303 | return getCurrentUser(); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 304 | } else { |
Jim Miller | 78c48f6 | 2013-03-01 15:28:33 -0800 | [diff] [blame] | 305 | return UserHandle.getCallingUserId(); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | /** |
| 310 | * Check to see if a pattern matches the saved pattern. If no pattern exists, |
| 311 | * always returns true. |
| 312 | * @param pattern The pattern to check. |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 313 | * @return Whether the pattern matches the stored one. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | */ |
| 315 | public boolean checkPattern(List<LockPatternView.Cell> pattern) { |
Kenny Root | df8bfe0 | 2012-09-21 15:00:25 -0700 | [diff] [blame] | 316 | final int userId = getCurrentOrCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | try { |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 318 | return getLockSettings().checkPattern(patternToString(pattern), userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 319 | } catch (RemoteException re) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | return true; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 325 | * Check to see if a password matches the saved password. If no password exists, |
| 326 | * always returns true. |
| 327 | * @param password The password to check. |
| 328 | * @return Whether the password matches the stored one. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | */ |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 330 | public boolean checkPassword(String password) { |
Kenny Root | df8bfe0 | 2012-09-21 15:00:25 -0700 | [diff] [blame] | 331 | final int userId = getCurrentOrCallingUserId(); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 332 | try { |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 333 | return getLockSettings().checkPassword(password, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 334 | } catch (RemoteException re) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 340 | * Check to see if vold already has the password. |
| 341 | * Note that this also clears vold's copy of the password. |
| 342 | * @return Whether the vold password matches or not. |
| 343 | */ |
| 344 | public boolean checkVoldPassword() { |
| 345 | final int userId = getCurrentOrCallingUserId(); |
| 346 | try { |
| 347 | return getLockSettings().checkVoldPassword(userId); |
| 348 | } catch (RemoteException re) { |
| 349 | return false; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 354 | * Check to see if a password matches any of the passwords stored in the |
| 355 | * password history. |
| 356 | * |
| 357 | * @param password The password to check. |
| 358 | * @return Whether the password matches any in the history. |
| 359 | */ |
| 360 | public boolean checkPasswordHistory(String password) { |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 361 | String passwordHashString = new String( |
| 362 | passwordToHash(password, getCurrentOrCallingUserId())); |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 363 | String passwordHistory = getString(PASSWORD_HISTORY_KEY); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 364 | if (passwordHistory == null) { |
| 365 | return false; |
| 366 | } |
| 367 | // Password History may be too long... |
| 368 | int passwordHashLength = passwordHashString.length(); |
| 369 | int passwordHistoryLength = getRequestedPasswordHistoryLength(); |
| 370 | if(passwordHistoryLength == 0) { |
| 371 | return false; |
| 372 | } |
| 373 | int neededPasswordHistoryLength = passwordHashLength * passwordHistoryLength |
| 374 | + passwordHistoryLength - 1; |
| 375 | if (passwordHistory.length() > neededPasswordHistoryLength) { |
| 376 | passwordHistory = passwordHistory.substring(0, neededPasswordHistoryLength); |
| 377 | } |
| 378 | return passwordHistory.contains(passwordHashString); |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 382 | * Check to see if the user has stored a lock pattern. |
| 383 | * @return Whether a saved pattern exists. |
| 384 | */ |
| 385 | public boolean savedPatternExists() { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 386 | try { |
| 387 | return getLockSettings().havePattern(getCurrentOrCallingUserId()); |
| 388 | } catch (RemoteException re) { |
| 389 | return false; |
| 390 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Check to see if the user has stored a lock pattern. |
| 395 | * @return Whether a saved pattern exists. |
| 396 | */ |
| 397 | public boolean savedPasswordExists() { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 398 | try { |
| 399 | return getLockSettings().havePassword(getCurrentOrCallingUserId()); |
| 400 | } catch (RemoteException re) { |
| 401 | return false; |
| 402 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 406 | * Return true if the user has ever chosen a pattern. This is true even if the pattern is |
| 407 | * currently cleared. |
| 408 | * |
| 409 | * @return True if the user has ever chosen a pattern. |
| 410 | */ |
| 411 | public boolean isPatternEverChosen() { |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 412 | return getBoolean(PATTERN_EVER_CHOSEN_KEY, false); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
Danielle Millett | 7a07219 | 2011-10-03 17:36:01 -0400 | [diff] [blame] | 416 | * Return true if the user has ever chosen biometric weak. This is true even if biometric |
| 417 | * weak is not current set. |
| 418 | * |
| 419 | * @return True if the user has ever chosen biometric weak. |
| 420 | */ |
| 421 | public boolean isBiometricWeakEverChosen() { |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 422 | return getBoolean(BIOMETRIC_WEAK_EVER_CHOSEN_KEY, false); |
Danielle Millett | 7a07219 | 2011-10-03 17:36:01 -0400 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 426 | * Used by device policy manager to validate the current password |
| 427 | * information it has. |
| 428 | */ |
| 429 | public int getActivePasswordQuality() { |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 430 | int activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Danielle Millett | c8fb532 | 2011-10-04 12:18:51 -0400 | [diff] [blame] | 431 | // Note we don't want to use getKeyguardStoredPasswordQuality() because we want this to |
| 432 | // return biometric_weak if that is being used instead of the backup |
| 433 | int quality = |
| 434 | (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); |
| 435 | switch (quality) { |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 436 | case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 437 | if (isLockPatternEnabled()) { |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 438 | activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_SOMETHING; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 439 | } |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 440 | break; |
Danielle Millett | c8fb532 | 2011-10-04 12:18:51 -0400 | [diff] [blame] | 441 | case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK: |
| 442 | if (isBiometricWeakInstalled()) { |
| 443 | activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK; |
| 444 | } |
| 445 | break; |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 446 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 447 | if (isLockPasswordEnabled()) { |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 448 | activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 449 | } |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 450 | break; |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 451 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX: |
| 452 | if (isLockPasswordEnabled()) { |
| 453 | activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; |
| 454 | } |
| 455 | break; |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 456 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 457 | if (isLockPasswordEnabled()) { |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 458 | activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 459 | } |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 460 | break; |
| 461 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC: |
| 462 | if (isLockPasswordEnabled()) { |
| 463 | activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC; |
| 464 | } |
| 465 | break; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 466 | case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX: |
| 467 | if (isLockPasswordEnabled()) { |
| 468 | activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; |
| 469 | } |
| 470 | break; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 471 | } |
Danielle Millett | c8fb532 | 2011-10-04 12:18:51 -0400 | [diff] [blame] | 472 | |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 473 | return activePasswordQuality; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 474 | } |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 475 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 476 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 477 | * Clear any lock pattern or password. |
| 478 | */ |
Steven Ross | 329979c | 2011-09-28 11:42:56 -0400 | [diff] [blame] | 479 | public void clearLock(boolean isFallback) { |
| 480 | if(!isFallback) deleteGallery(); |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 481 | saveLockPassword(null, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 482 | setLockPatternEnabled(false); |
| 483 | saveLockPattern(null); |
Jim Miller | 93708af | 2012-01-25 18:26:12 -0800 | [diff] [blame] | 484 | setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 485 | setLong(PASSWORD_TYPE_ALTERNATE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 486 | onAfterChangingPassword(); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 487 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 488 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 489 | /** |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 490 | * Disable showing lock screen at all when the DevicePolicyManager allows it. |
| 491 | * This is only meaningful if pattern, pin or password are not set. |
| 492 | * |
| 493 | * @param disable Disables lock screen when true |
| 494 | */ |
| 495 | public void setLockScreenDisabled(boolean disable) { |
| 496 | setLong(DISABLE_LOCKSCREEN_KEY, disable ? 1 : 0); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Determine if LockScreen can be disabled. This is used, for example, to tell if we should |
| 501 | * show LockScreen or go straight to the home screen. |
| 502 | * |
| 503 | * @return true if lock screen is can be disabled |
| 504 | */ |
| 505 | public boolean isLockScreenDisabled() { |
Adam Lesinski | 61d9409 | 2014-09-15 15:27:19 -0700 | [diff] [blame] | 506 | if (!isSecure() && getLong(DISABLE_LOCKSCREEN_KEY, 0) != 0) { |
| 507 | // Check if the number of switchable users forces the lockscreen. |
| 508 | final List<UserInfo> users = UserManager.get(mContext).getUsers(true); |
| 509 | final int userCount = users.size(); |
| 510 | int switchableUsers = 0; |
| 511 | for (int i = 0; i < userCount; i++) { |
| 512 | if (users.get(i).supportsSwitchTo()) { |
| 513 | switchableUsers++; |
| 514 | } |
| 515 | } |
| 516 | return switchableUsers < 2; |
| 517 | } |
| 518 | return false; |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /** |
Steven Ross | 3553c29 | 2011-09-30 15:48:40 -0400 | [diff] [blame] | 522 | * Calls back SetupFaceLock to delete the temporary gallery file |
Steven Ross | 329979c | 2011-09-28 11:42:56 -0400 | [diff] [blame] | 523 | */ |
| 524 | public void deleteTempGallery() { |
Danielle Millett | 574e49e | 2012-03-27 16:17:10 -0400 | [diff] [blame] | 525 | Intent intent = new Intent().setAction("com.android.facelock.DELETE_GALLERY"); |
Steven Ross | 3553c29 | 2011-09-30 15:48:40 -0400 | [diff] [blame] | 526 | intent.putExtra("deleteTempGallery", true); |
Danielle Millett | 574e49e | 2012-03-27 16:17:10 -0400 | [diff] [blame] | 527 | mContext.sendBroadcast(intent); |
Steven Ross | 329979c | 2011-09-28 11:42:56 -0400 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Calls back SetupFaceLock to delete the gallery file when the lock type is changed |
| 532 | */ |
| 533 | void deleteGallery() { |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 534 | if(usingBiometricWeak()) { |
Danielle Millett | 574e49e | 2012-03-27 16:17:10 -0400 | [diff] [blame] | 535 | Intent intent = new Intent().setAction("com.android.facelock.DELETE_GALLERY"); |
Steven Ross | 329979c | 2011-09-28 11:42:56 -0400 | [diff] [blame] | 536 | intent.putExtra("deleteGallery", true); |
Danielle Millett | 574e49e | 2012-03-27 16:17:10 -0400 | [diff] [blame] | 537 | mContext.sendBroadcast(intent); |
Steven Ross | 329979c | 2011-09-28 11:42:56 -0400 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | /** |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 542 | * Save a lock pattern. |
| 543 | * @param pattern The new pattern to save. |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 544 | */ |
| 545 | public void saveLockPattern(List<LockPatternView.Cell> pattern) { |
| 546 | this.saveLockPattern(pattern, false); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Save a lock pattern. |
| 551 | * @param pattern The new pattern to save. |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 552 | * @param isFallback Specifies if this is a fallback to biometric weak |
| 553 | */ |
| 554 | public void saveLockPattern(List<LockPatternView.Cell> pattern, boolean isFallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | try { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 556 | int userId = getCurrentOrCallingUserId(); |
| 557 | getLockSettings().setLockPattern(patternToString(pattern), userId); |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 558 | DevicePolicyManager dpm = getDevicePolicyManager(); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 559 | if (pattern != null) { |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 560 | // Update the device encryption password. |
| 561 | if (userId == UserHandle.USER_OWNER |
| 562 | && LockPatternUtils.isDeviceEncryptionEnabled()) { |
| 563 | final ContentResolver cr = mContext.getContentResolver(); |
| 564 | final boolean required = Settings.Global.getInt(cr, |
| 565 | Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, 1) == 1 ? true : false; |
| 566 | if (!required) { |
| 567 | clearEncryptionPassword(); |
| 568 | } else { |
| 569 | String stringPattern = patternToString(pattern); |
| 570 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_PATTERN, stringPattern); |
| 571 | } |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 572 | } |
| 573 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 574 | setBoolean(PATTERN_EVER_CHOSEN_KEY, true); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 575 | if (!isFallback) { |
Steven Ross | 329979c | 2011-09-28 11:42:56 -0400 | [diff] [blame] | 576 | deleteGallery(); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 577 | setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 578 | dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 579 | pattern.size(), 0, 0, 0, 0, 0, 0, userId); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 580 | } else { |
| 581 | setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK); |
| 582 | setLong(PASSWORD_TYPE_ALTERNATE_KEY, |
| 583 | DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); |
Danielle Millett | 044a0a7 | 2011-11-07 15:42:12 -0500 | [diff] [blame] | 584 | finishBiometricWeak(); |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 585 | dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK, |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 586 | 0, 0, 0, 0, 0, 0, 0, userId); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 587 | } |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 588 | } else { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 589 | dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, 0, 0, |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 590 | 0, 0, 0, 0, 0, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 591 | } |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 592 | onAfterChangingPassword(); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 593 | } catch (RemoteException re) { |
| 594 | Log.e(TAG, "Couldn't save lock pattern " + re); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 598 | private void updateCryptoUserInfo() { |
| 599 | int userId = getCurrentOrCallingUserId(); |
| 600 | if (userId != UserHandle.USER_OWNER) { |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | final String ownerInfo = isOwnerInfoEnabled() ? getOwnerInfo(userId) : ""; |
| 605 | |
| 606 | IBinder service = ServiceManager.getService("mount"); |
| 607 | if (service == null) { |
| 608 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 613 | try { |
| 614 | Log.d(TAG, "Setting owner info"); |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 615 | mountService.setField(StorageManager.OWNER_INFO_KEY, ownerInfo); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 616 | } catch (RemoteException e) { |
| 617 | Log.e(TAG, "Error changing user info", e); |
| 618 | } |
| 619 | } |
| 620 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 621 | public void setOwnerInfo(String info, int userId) { |
| 622 | setString(LOCK_SCREEN_OWNER_INFO, info, userId); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 623 | updateCryptoUserInfo(); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | public void setOwnerInfoEnabled(boolean enabled) { |
| 627 | setBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, enabled); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 628 | updateCryptoUserInfo(); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | public String getOwnerInfo(int userId) { |
| 632 | return getString(LOCK_SCREEN_OWNER_INFO); |
| 633 | } |
| 634 | |
| 635 | public boolean isOwnerInfoEnabled() { |
| 636 | return getBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, false); |
| 637 | } |
| 638 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | /** |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 640 | * Compute the password quality from the given password string. |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 641 | */ |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 642 | static public int computePasswordQuality(String password) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 643 | boolean hasDigit = false; |
| 644 | boolean hasNonDigit = false; |
| 645 | final int len = password.length(); |
| 646 | for (int i = 0; i < len; i++) { |
| 647 | if (Character.isDigit(password.charAt(i))) { |
| 648 | hasDigit = true; |
| 649 | } else { |
| 650 | hasNonDigit = true; |
| 651 | } |
| 652 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 653 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 654 | if (hasNonDigit && hasDigit) { |
| 655 | return DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 656 | } |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 657 | if (hasNonDigit) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 658 | return DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 659 | } |
| 660 | if (hasDigit) { |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 661 | return maxLengthSequence(password) > MAX_ALLOWED_SEQUENCE |
| 662 | ? DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
| 663 | : DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 664 | } |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 665 | return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 666 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 667 | |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 668 | private static int categoryChar(char c) { |
| 669 | if ('a' <= c && c <= 'z') return 0; |
| 670 | if ('A' <= c && c <= 'Z') return 1; |
| 671 | if ('0' <= c && c <= '9') return 2; |
| 672 | return 3; |
| 673 | } |
| 674 | |
| 675 | private static int maxDiffCategory(int category) { |
| 676 | if (category == 0 || category == 1) return 1; |
| 677 | else if (category == 2) return 10; |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Returns the maximum length of a sequential characters. A sequence is defined as |
| 683 | * monotonically increasing characters with a constant interval or the same character repeated. |
| 684 | * |
| 685 | * For example: |
| 686 | * maxLengthSequence("1234") == 4 |
| 687 | * maxLengthSequence("1234abc") == 4 |
| 688 | * maxLengthSequence("aabc") == 3 |
| 689 | * maxLengthSequence("qwertyuio") == 1 |
| 690 | * maxLengthSequence("@ABC") == 3 |
| 691 | * maxLengthSequence(";;;;") == 4 (anything that repeats) |
| 692 | * maxLengthSequence(":;<=>") == 1 (ordered, but not composed of alphas or digits) |
| 693 | * |
| 694 | * @param string the pass |
| 695 | * @return the number of sequential letters or digits |
| 696 | */ |
| 697 | public static int maxLengthSequence(String string) { |
| 698 | if (string.length() == 0) return 0; |
| 699 | char previousChar = string.charAt(0); |
| 700 | int category = categoryChar(previousChar); //current category of the sequence |
| 701 | int diff = 0; //difference between two consecutive characters |
| 702 | boolean hasDiff = false; //if we are currently targeting a sequence |
| 703 | int maxLength = 0; //maximum length of a sequence already found |
| 704 | int startSequence = 0; //where the current sequence started |
| 705 | for (int current = 1; current < string.length(); current++) { |
| 706 | char currentChar = string.charAt(current); |
| 707 | int categoryCurrent = categoryChar(currentChar); |
| 708 | int currentDiff = (int) currentChar - (int) previousChar; |
| 709 | if (categoryCurrent != category || Math.abs(currentDiff) > maxDiffCategory(category)) { |
| 710 | maxLength = Math.max(maxLength, current - startSequence); |
| 711 | startSequence = current; |
| 712 | hasDiff = false; |
| 713 | category = categoryCurrent; |
| 714 | } |
| 715 | else { |
| 716 | if(hasDiff && currentDiff != diff) { |
| 717 | maxLength = Math.max(maxLength, current - startSequence); |
| 718 | startSequence = current - 1; |
| 719 | } |
| 720 | diff = currentDiff; |
| 721 | hasDiff = true; |
| 722 | } |
| 723 | previousChar = currentChar; |
| 724 | } |
| 725 | maxLength = Math.max(maxLength, string.length() - startSequence); |
| 726 | return maxLength; |
| 727 | } |
| 728 | |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 729 | /** Update the encryption password if it is enabled **/ |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 730 | private void updateEncryptionPassword(final int type, final String password) { |
Amith Yamasani | cd410ba | 2014-10-17 11:16:58 -0700 | [diff] [blame^] | 731 | if (!isDeviceEncryptionEnabled()) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 732 | return; |
| 733 | } |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 734 | final IBinder service = ServiceManager.getService("mount"); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 735 | if (service == null) { |
| 736 | Log.e(TAG, "Could not find the mount service to update the encryption password"); |
| 737 | return; |
| 738 | } |
| 739 | |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 740 | new AsyncTask<Void, Void, Void>() { |
| 741 | @Override |
| 742 | protected Void doInBackground(Void... dummy) { |
| 743 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 744 | try { |
| 745 | mountService.changeEncryptionPassword(type, password); |
| 746 | } catch (RemoteException e) { |
| 747 | Log.e(TAG, "Error changing encryption password", e); |
| 748 | } |
| 749 | return null; |
| 750 | } |
| 751 | }.execute(); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 752 | } |
| 753 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 754 | /** |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 755 | * 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] | 756 | * as the requested mode, but will adjust the mode to be as good as the |
| 757 | * pattern. |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 758 | * @param password The password to save |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 759 | * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 760 | */ |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 761 | public void saveLockPassword(String password, int quality) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 762 | this.saveLockPassword(password, quality, false, getCurrentOrCallingUserId()); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Save a lock password. Does not ensure that the password is as good |
| 767 | * as the requested mode, but will adjust the mode to be as good as the |
| 768 | * pattern. |
| 769 | * @param password The password to save |
| 770 | * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
| 771 | * @param isFallback Specifies if this is a fallback to biometric weak |
| 772 | */ |
| 773 | public void saveLockPassword(String password, int quality, boolean isFallback) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 774 | saveLockPassword(password, quality, isFallback, getCurrentOrCallingUserId()); |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * Save a lock password. Does not ensure that the password is as good |
| 779 | * as the requested mode, but will adjust the mode to be as good as the |
| 780 | * pattern. |
| 781 | * @param password The password to save |
| 782 | * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
| 783 | * @param isFallback Specifies if this is a fallback to biometric weak |
| 784 | * @param userHandle The userId of the user to change the password for |
| 785 | */ |
| 786 | public void saveLockPassword(String password, int quality, boolean isFallback, int userHandle) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 787 | try { |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 788 | DevicePolicyManager dpm = getDevicePolicyManager(); |
Paul Lawrence | ba45bcb | 2014-07-02 15:38:35 -0700 | [diff] [blame] | 789 | if (!TextUtils.isEmpty(password)) { |
Adrian Roos | ab7dae5 | 2014-09-25 18:07:13 +0200 | [diff] [blame] | 790 | getLockSettings().setLockPassword(password, userHandle); |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 791 | int computedQuality = computePasswordQuality(password); |
| 792 | |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 793 | // Update the device encryption password. |
| 794 | if (userHandle == UserHandle.USER_OWNER |
| 795 | && LockPatternUtils.isDeviceEncryptionEnabled()) { |
| 796 | final ContentResolver cr = mContext.getContentResolver(); |
| 797 | final boolean required = Settings.Global.getInt(cr, |
| 798 | Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, 1) == 1 ? true : false; |
| 799 | if (!required) { |
| 800 | clearEncryptionPassword(); |
| 801 | } else { |
| 802 | boolean numeric = computedQuality |
| 803 | == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; |
| 804 | boolean numericComplex = computedQuality |
| 805 | == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; |
| 806 | int type = numeric || numericComplex ? StorageManager.CRYPT_TYPE_PIN |
| 807 | : StorageManager.CRYPT_TYPE_PASSWORD; |
| 808 | updateEncryptionPassword(type, password); |
| 809 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 810 | } |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 811 | |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 812 | if (!isFallback) { |
Steven Ross | 329979c | 2011-09-28 11:42:56 -0400 | [diff] [blame] | 813 | deleteGallery(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 814 | setLong(PASSWORD_TYPE_KEY, Math.max(quality, computedQuality), userHandle); |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 815 | if (computedQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 816 | int letters = 0; |
| 817 | int uppercase = 0; |
| 818 | int lowercase = 0; |
| 819 | int numbers = 0; |
| 820 | int symbols = 0; |
| 821 | int nonletter = 0; |
| 822 | for (int i = 0; i < password.length(); i++) { |
| 823 | char c = password.charAt(i); |
| 824 | if (c >= 'A' && c <= 'Z') { |
| 825 | letters++; |
| 826 | uppercase++; |
| 827 | } else if (c >= 'a' && c <= 'z') { |
| 828 | letters++; |
| 829 | lowercase++; |
| 830 | } else if (c >= '0' && c <= '9') { |
| 831 | numbers++; |
| 832 | nonletter++; |
| 833 | } else { |
| 834 | symbols++; |
| 835 | nonletter++; |
| 836 | } |
| 837 | } |
| 838 | dpm.setActivePasswordState(Math.max(quality, computedQuality), |
| 839 | password.length(), letters, uppercase, lowercase, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 840 | numbers, symbols, nonletter, userHandle); |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 841 | } else { |
| 842 | // The password is not anything. |
| 843 | dpm.setActivePasswordState( |
| 844 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 845 | 0, 0, 0, 0, 0, 0, 0, userHandle); |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 846 | } |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 847 | } else { |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 848 | // Case where it's a fallback for biometric weak |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 849 | setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK, |
| 850 | userHandle); |
| 851 | setLong(PASSWORD_TYPE_ALTERNATE_KEY, Math.max(quality, computedQuality), |
| 852 | userHandle); |
Danielle Millett | 044a0a7 | 2011-11-07 15:42:12 -0500 | [diff] [blame] | 853 | finishBiometricWeak(); |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 854 | dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 855 | 0, 0, 0, 0, 0, 0, 0, userHandle); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 856 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 857 | // Add the password to the password history. We assume all |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 858 | // password hashes have the same length for simplicity of implementation. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 859 | String passwordHistory = getString(PASSWORD_HISTORY_KEY, userHandle); |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 860 | if (passwordHistory == null) { |
| 861 | passwordHistory = new String(); |
| 862 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 863 | int passwordHistoryLength = getRequestedPasswordHistoryLength(); |
| 864 | if (passwordHistoryLength == 0) { |
| 865 | passwordHistory = ""; |
| 866 | } else { |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 867 | byte[] hash = passwordToHash(password, userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 868 | passwordHistory = new String(hash) + "," + passwordHistory; |
| 869 | // Cut it to contain passwordHistoryLength hashes |
| 870 | // and passwordHistoryLength -1 commas. |
| 871 | passwordHistory = passwordHistory.substring(0, Math.min(hash.length |
| 872 | * passwordHistoryLength + passwordHistoryLength - 1, passwordHistory |
| 873 | .length())); |
| 874 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 875 | setString(PASSWORD_HISTORY_KEY, passwordHistory, userHandle); |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 876 | } else { |
Paul Lawrence | ba45bcb | 2014-07-02 15:38:35 -0700 | [diff] [blame] | 877 | // Empty password |
Adrian Roos | ab7dae5 | 2014-09-25 18:07:13 +0200 | [diff] [blame] | 878 | getLockSettings().setLockPassword(null, userHandle); |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 879 | if (userHandle == UserHandle.USER_OWNER) { |
Paul Lawrence | ba45bcb | 2014-07-02 15:38:35 -0700 | [diff] [blame] | 880 | // Set the encryption password to default. |
| 881 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null); |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 884 | dpm.setActivePasswordState( |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 885 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, 0, 0, 0, 0, 0, 0, 0, |
| 886 | userHandle); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 887 | } |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 888 | onAfterChangingPassword(); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 889 | } catch (RemoteException re) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 890 | // Cant do much |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 891 | Log.e(TAG, "Unable to save lock password " + re); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 895 | /** |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 896 | * Gets whether the device is encrypted. |
| 897 | * |
| 898 | * @return Whether the device is encrypted. |
| 899 | */ |
| 900 | public static boolean isDeviceEncrypted() { |
| 901 | IMountService mountService = IMountService.Stub.asInterface( |
| 902 | ServiceManager.getService("mount")); |
| 903 | try { |
| 904 | return mountService.getEncryptionState() != IMountService.ENCRYPTION_STATE_NONE |
| 905 | && mountService.getPasswordType() != StorageManager.CRYPT_TYPE_DEFAULT; |
| 906 | } catch (RemoteException re) { |
| 907 | Log.e(TAG, "Error getting encryption state", re); |
| 908 | } |
| 909 | return true; |
| 910 | } |
| 911 | |
| 912 | /** |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 913 | * Determine if the device supports encryption, even if it's set to default. This |
| 914 | * differs from isDeviceEncrypted() in that it returns true even if the device is |
| 915 | * encrypted with the default password. |
| 916 | * @return true if device encryption is enabled |
| 917 | */ |
| 918 | public static boolean isDeviceEncryptionEnabled() { |
| 919 | final String status = SystemProperties.get("ro.crypto.state", "unsupported"); |
| 920 | return "encrypted".equalsIgnoreCase(status); |
| 921 | } |
| 922 | |
| 923 | /** |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 924 | * Clears the encryption password. |
| 925 | */ |
| 926 | public void clearEncryptionPassword() { |
| 927 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null); |
| 928 | } |
| 929 | |
| 930 | /** |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 931 | * Retrieves the quality mode we're in. |
| 932 | * {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
| 933 | * |
| 934 | * @return stored password quality |
| 935 | */ |
| 936 | public int getKeyguardStoredPasswordQuality() { |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 937 | return getKeyguardStoredPasswordQuality(getCurrentOrCallingUserId()); |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * Retrieves the quality mode for {@param userHandle}. |
| 942 | * {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
| 943 | * |
| 944 | * @return stored password quality |
| 945 | */ |
| 946 | public int getKeyguardStoredPasswordQuality(int userHandle) { |
| 947 | int quality = (int) getLong(PASSWORD_TYPE_KEY, |
| 948 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userHandle); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 949 | // If the user has chosen to use weak biometric sensor, then return the backup locking |
| 950 | // method and treat biometric as a special case. |
| 951 | if (quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) { |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 952 | quality = (int) getLong(PASSWORD_TYPE_ALTERNATE_KEY, |
| 953 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userHandle); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 954 | } |
| 955 | return quality; |
| 956 | } |
| 957 | |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 958 | /** |
| 959 | * @return true if the lockscreen method is set to biometric weak |
| 960 | */ |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 961 | public boolean usingBiometricWeak() { |
| 962 | int quality = |
Jim Miller | 4312073 | 2014-04-14 16:56:08 -0700 | [diff] [blame] | 963 | (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 964 | return quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | * Deserialize a pattern. |
| 969 | * @param string The pattern serialized with {@link #patternToString} |
| 970 | * @return The pattern. |
| 971 | */ |
| 972 | public static List<LockPatternView.Cell> stringToPattern(String string) { |
| 973 | List<LockPatternView.Cell> result = Lists.newArrayList(); |
| 974 | |
| 975 | final byte[] bytes = string.getBytes(); |
| 976 | for (int i = 0; i < bytes.length; i++) { |
| 977 | byte b = bytes[i]; |
| 978 | result.add(LockPatternView.Cell.of(b / 3, b % 3)); |
| 979 | } |
| 980 | return result; |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Serialize a pattern. |
| 985 | * @param pattern The pattern. |
| 986 | * @return The pattern in string form. |
| 987 | */ |
| 988 | public static String patternToString(List<LockPatternView.Cell> pattern) { |
| 989 | if (pattern == null) { |
| 990 | return ""; |
| 991 | } |
| 992 | final int patternSize = pattern.size(); |
| 993 | |
| 994 | byte[] res = new byte[patternSize]; |
| 995 | for (int i = 0; i < patternSize; i++) { |
| 996 | LockPatternView.Cell cell = pattern.get(i); |
| 997 | res[i] = (byte) (cell.getRow() * 3 + cell.getColumn()); |
| 998 | } |
| 999 | return new String(res); |
| 1000 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1001 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | /* |
| 1003 | * Generate an SHA-1 hash for the pattern. Not the most secure, but it is |
| 1004 | * at least a second level of protection. First level is that the file |
| 1005 | * is in a location only readable by the system process. |
| 1006 | * @param pattern the gesture pattern. |
| 1007 | * @return the hash of the pattern in a byte array. |
| 1008 | */ |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1009 | public static byte[] patternToHash(List<LockPatternView.Cell> pattern) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | if (pattern == null) { |
| 1011 | return null; |
| 1012 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1013 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | final int patternSize = pattern.size(); |
| 1015 | byte[] res = new byte[patternSize]; |
| 1016 | for (int i = 0; i < patternSize; i++) { |
| 1017 | LockPatternView.Cell cell = pattern.get(i); |
| 1018 | res[i] = (byte) (cell.getRow() * 3 + cell.getColumn()); |
| 1019 | } |
| 1020 | try { |
| 1021 | MessageDigest md = MessageDigest.getInstance("SHA-1"); |
| 1022 | byte[] hash = md.digest(res); |
| 1023 | return hash; |
| 1024 | } catch (NoSuchAlgorithmException nsa) { |
| 1025 | return res; |
| 1026 | } |
| 1027 | } |
| 1028 | |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1029 | private String getSalt(int userId) { |
| 1030 | long salt = getLong(LOCK_PASSWORD_SALT_KEY, 0, userId); |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 1031 | if (salt == 0) { |
| 1032 | try { |
| 1033 | salt = SecureRandom.getInstance("SHA1PRNG").nextLong(); |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1034 | setLong(LOCK_PASSWORD_SALT_KEY, salt, userId); |
| 1035 | Log.v(TAG, "Initialized lock password salt for user: " + userId); |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 1036 | } catch (NoSuchAlgorithmException e) { |
| 1037 | // Throw an exception rather than storing a password we'll never be able to recover |
| 1038 | throw new IllegalStateException("Couldn't get SecureRandom number", e); |
| 1039 | } |
| 1040 | } |
| 1041 | return Long.toHexString(salt); |
| 1042 | } |
| 1043 | |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1044 | /* |
| 1045 | * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash. |
| 1046 | * Not the most secure, but it is at least a second level of protection. First level is that |
| 1047 | * the file is in a location only readable by the system process. |
| 1048 | * @param password the gesture pattern. |
| 1049 | * @return the hash of the pattern in a byte array. |
| 1050 | */ |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1051 | public byte[] passwordToHash(String password, int userId) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1052 | if (password == null) { |
| 1053 | return null; |
| 1054 | } |
| 1055 | String algo = null; |
| 1056 | byte[] hashed = null; |
| 1057 | try { |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1058 | byte[] saltedPassword = (password + getSalt(userId)).getBytes(); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1059 | byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword); |
| 1060 | byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword); |
| 1061 | hashed = (toHex(sha1) + toHex(md5)).getBytes(); |
| 1062 | } catch (NoSuchAlgorithmException e) { |
| 1063 | Log.w(TAG, "Failed to encode string because of missing algorithm: " + algo); |
| 1064 | } |
| 1065 | return hashed; |
| 1066 | } |
| 1067 | |
| 1068 | private static String toHex(byte[] ary) { |
| 1069 | final String hex = "0123456789ABCDEF"; |
| 1070 | String ret = ""; |
| 1071 | for (int i = 0; i < ary.length; i++) { |
| 1072 | ret += hex.charAt((ary[i] >> 4) & 0xf); |
| 1073 | ret += hex.charAt(ary[i] & 0xf); |
| 1074 | } |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | /** |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1079 | * @return Whether the lock password is enabled, or if it is set as a backup for biometric weak |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1080 | */ |
| 1081 | public boolean isLockPasswordEnabled() { |
| 1082 | long mode = getLong(PASSWORD_TYPE_KEY, 0); |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1083 | long backupMode = getLong(PASSWORD_TYPE_ALTERNATE_KEY, 0); |
| 1084 | final boolean passwordEnabled = mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC |
| 1085 | || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 1086 | || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1087 | || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC |
| 1088 | || mode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; |
| 1089 | final boolean backupEnabled = backupMode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC |
| 1090 | || backupMode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 1091 | || backupMode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1092 | || backupMode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC |
| 1093 | || backupMode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; |
| 1094 | |
| 1095 | return savedPasswordExists() && (passwordEnabled || |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 1096 | (usingBiometricWeak() && backupEnabled)); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | /** |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1100 | * @return Whether the lock pattern is enabled, or if it is set as a backup for biometric weak |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | */ |
| 1102 | public boolean isLockPatternEnabled() { |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1103 | final boolean backupEnabled = |
Jim Miller | 4312073 | 2014-04-14 16:56:08 -0700 | [diff] [blame] | 1104 | getLong(PASSWORD_TYPE_ALTERNATE_KEY, |
| 1105 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) |
| 1106 | == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING; |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1107 | |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 1108 | return getBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, false) |
Jim Miller | 4312073 | 2014-04-14 16:56:08 -0700 | [diff] [blame] | 1109 | && (getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1110 | == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING || |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 1111 | (usingBiometricWeak() && backupEnabled)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /** |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 1115 | * @return Whether biometric weak lock is installed and that the front facing camera exists |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 1116 | */ |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 1117 | public boolean isBiometricWeakInstalled() { |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 1118 | // Check that it's installed |
| 1119 | PackageManager pm = mContext.getPackageManager(); |
| 1120 | try { |
| 1121 | pm.getPackageInfo("com.android.facelock", PackageManager.GET_ACTIVITIES); |
| 1122 | } catch (PackageManager.NameNotFoundException e) { |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
| 1126 | // Check that the camera is enabled |
| 1127 | if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) { |
| 1128 | return false; |
| 1129 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1130 | if (getDevicePolicyManager().getCameraDisabled(null, getCurrentOrCallingUserId())) { |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 1131 | return false; |
| 1132 | } |
| 1133 | |
Brian Colonna | a0ee004 | 2014-07-16 13:50:47 -0400 | [diff] [blame] | 1134 | // TODO: If we decide not to proceed with Face Unlock as a trustlet, this must be changed |
| 1135 | // back to returning true. If we become certain that Face Unlock will be a trustlet, this |
| 1136 | // entire function and a lot of other code can be removed. |
| 1137 | if (DEBUG) Log.d(TAG, "Forcing isBiometricWeakInstalled() to return false to disable it"); |
| 1138 | return false; |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /** |
Danielle Millett | 925a7d8 | 2012-03-19 18:02:20 -0400 | [diff] [blame] | 1142 | * Set whether biometric weak liveliness is enabled. |
| 1143 | */ |
| 1144 | public void setBiometricWeakLivelinessEnabled(boolean enabled) { |
| 1145 | long currentFlag = getLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, 0L); |
| 1146 | long newFlag; |
| 1147 | if (enabled) { |
| 1148 | newFlag = currentFlag | FLAG_BIOMETRIC_WEAK_LIVELINESS; |
| 1149 | } else { |
| 1150 | newFlag = currentFlag & ~FLAG_BIOMETRIC_WEAK_LIVELINESS; |
| 1151 | } |
| 1152 | setLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, newFlag); |
| 1153 | } |
| 1154 | |
| 1155 | /** |
| 1156 | * @return Whether the biometric weak liveliness is enabled. |
| 1157 | */ |
| 1158 | public boolean isBiometricWeakLivelinessEnabled() { |
| 1159 | long currentFlag = getLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, 0L); |
| 1160 | return ((currentFlag & FLAG_BIOMETRIC_WEAK_LIVELINESS) != 0); |
| 1161 | } |
| 1162 | |
| 1163 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | * Set whether the lock pattern is enabled. |
| 1165 | */ |
| 1166 | public void setLockPatternEnabled(boolean enabled) { |
Amith Yamasani | 156c435 | 2010-03-05 17:10:03 -0800 | [diff] [blame] | 1167 | setBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, enabled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * @return Whether the visible pattern is enabled. |
| 1172 | */ |
| 1173 | public boolean isVisiblePatternEnabled() { |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 1174 | return getBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * Set whether the visible pattern is enabled. |
| 1179 | */ |
| 1180 | public void setVisiblePatternEnabled(boolean enabled) { |
Amith Yamasani | 156c435 | 2010-03-05 17:10:03 -0800 | [diff] [blame] | 1181 | setBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, enabled); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1182 | |
| 1183 | // Update for crypto if owner |
| 1184 | int userId = getCurrentOrCallingUserId(); |
| 1185 | if (userId != UserHandle.USER_OWNER) { |
| 1186 | return; |
| 1187 | } |
| 1188 | |
| 1189 | IBinder service = ServiceManager.getService("mount"); |
| 1190 | if (service == null) { |
| 1191 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 1196 | try { |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 1197 | mountService.setField(StorageManager.PATTERN_VISIBLE_KEY, enabled ? "1" : "0"); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1198 | } catch (RemoteException e) { |
| 1199 | Log.e(TAG, "Error changing pattern visible state", e); |
| 1200 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /** |
| 1204 | * @return Whether tactile feedback for the pattern is enabled. |
| 1205 | */ |
| 1206 | public boolean isTactileFeedbackEnabled() { |
Jeff Sharkey | 5ed9d68 | 2012-10-10 14:28:27 -0700 | [diff] [blame] | 1207 | return Settings.System.getIntForUser(mContentResolver, |
| 1208 | 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] | 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Set and store the lockout deadline, meaning the user can't attempt his/her unlock |
| 1213 | * pattern until the deadline has passed. |
| 1214 | * @return the chosen deadline. |
| 1215 | */ |
| 1216 | public long setLockoutAttemptDeadline() { |
| 1217 | final long deadline = SystemClock.elapsedRealtime() + FAILED_ATTEMPT_TIMEOUT_MS; |
| 1218 | setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline); |
| 1219 | return deadline; |
| 1220 | } |
| 1221 | |
| 1222 | /** |
| 1223 | * @return The elapsed time in millis in the future when the user is allowed to |
| 1224 | * attempt to enter his/her lock pattern, or 0 if the user is welcome to |
| 1225 | * enter a pattern. |
| 1226 | */ |
| 1227 | public long getLockoutAttemptDeadline() { |
| 1228 | final long deadline = getLong(LOCKOUT_ATTEMPT_DEADLINE, 0L); |
| 1229 | final long now = SystemClock.elapsedRealtime(); |
| 1230 | if (deadline < now || deadline > (now + FAILED_ATTEMPT_TIMEOUT_MS)) { |
| 1231 | return 0L; |
| 1232 | } |
| 1233 | return deadline; |
| 1234 | } |
| 1235 | |
| 1236 | /** |
| 1237 | * @return Whether the user is permanently locked out until they verify their |
| 1238 | * credentials. Occurs after {@link #FAILED_ATTEMPTS_BEFORE_RESET} failed |
| 1239 | * attempts. |
| 1240 | */ |
| 1241 | public boolean isPermanentlyLocked() { |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 1242 | return getBoolean(LOCKOUT_PERMANENT_KEY, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | /** |
| 1246 | * Set the state of whether the device is permanently locked, meaning the user |
Karl Rosaen | 678771b | 2009-08-21 14:00:26 -0700 | [diff] [blame] | 1247 | * must authenticate via other means. |
| 1248 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | * @param locked Whether the user is permanently locked out until they verify their |
| 1250 | * credentials. Occurs after {@link #FAILED_ATTEMPTS_BEFORE_RESET} failed |
| 1251 | * attempts. |
| 1252 | */ |
| 1253 | public void setPermanentlyLocked(boolean locked) { |
| 1254 | setBoolean(LOCKOUT_PERMANENT_KEY, locked); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
John Wang | 0f7b3f8 | 2011-05-31 11:20:55 -0700 | [diff] [blame] | 1257 | public boolean isEmergencyCallCapable() { |
| 1258 | return mContext.getResources().getBoolean( |
| 1259 | com.android.internal.R.bool.config_voice_capable); |
| 1260 | } |
| 1261 | |
| 1262 | public boolean isPukUnlockScreenEnable() { |
| 1263 | return mContext.getResources().getBoolean( |
| 1264 | com.android.internal.R.bool.config_enable_puk_unlock_screen); |
| 1265 | } |
| 1266 | |
Jim Miller | 1f56edc | 2011-11-07 19:00:48 -0800 | [diff] [blame] | 1267 | public boolean isEmergencyCallEnabledWhileSimLocked() { |
| 1268 | return mContext.getResources().getBoolean( |
| 1269 | com.android.internal.R.bool.config_enable_emergency_call_while_sim_locked); |
| 1270 | } |
| 1271 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1272 | /** |
| 1273 | * @return A formatted string of the next alarm (for showing on the lock screen), |
| 1274 | * or null if there is no next alarm. |
| 1275 | */ |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1276 | public AlarmManager.AlarmClockInfo getNextAlarm() { |
Adrian Roos | edad7b2 | 2014-07-13 14:38:22 +0200 | [diff] [blame] | 1277 | AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 1278 | return alarmManager.getNextAlarmClock(UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1281 | private boolean getBoolean(String secureSettingKey, boolean defaultValue, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1282 | try { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1283 | return getLockSettings().getBoolean(secureSettingKey, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1284 | } catch (RemoteException re) { |
| 1285 | return defaultValue; |
| 1286 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1289 | private boolean getBoolean(String secureSettingKey, boolean defaultValue) { |
| 1290 | return getBoolean(secureSettingKey, defaultValue, getCurrentOrCallingUserId()); |
| 1291 | } |
| 1292 | |
| 1293 | private void setBoolean(String secureSettingKey, boolean enabled, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1294 | try { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1295 | getLockSettings().setBoolean(secureSettingKey, enabled, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1296 | } catch (RemoteException re) { |
| 1297 | // What can we do? |
| 1298 | Log.e(TAG, "Couldn't write boolean " + secureSettingKey + re); |
| 1299 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1302 | private void setBoolean(String secureSettingKey, boolean enabled) { |
| 1303 | setBoolean(secureSettingKey, enabled, getCurrentOrCallingUserId()); |
| 1304 | } |
| 1305 | |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1306 | public int[] getAppWidgets() { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1307 | return getAppWidgets(UserHandle.USER_CURRENT); |
| 1308 | } |
| 1309 | |
| 1310 | private int[] getAppWidgets(int userId) { |
Amith Yamasani | 8fd96ec | 2012-09-21 17:48:49 -0700 | [diff] [blame] | 1311 | String appWidgetIdString = Settings.Secure.getStringForUser( |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1312 | mContentResolver, Settings.Secure.LOCK_SCREEN_APPWIDGET_IDS, userId); |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1313 | String delims = ","; |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1314 | if (appWidgetIdString != null && appWidgetIdString.length() > 0) { |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1315 | String[] appWidgetStringIds = appWidgetIdString.split(delims); |
| 1316 | int[] appWidgetIds = new int[appWidgetStringIds.length]; |
| 1317 | for (int i = 0; i < appWidgetStringIds.length; i++) { |
| 1318 | String appWidget = appWidgetStringIds[i]; |
| 1319 | try { |
| 1320 | appWidgetIds[i] = Integer.decode(appWidget); |
| 1321 | } catch (NumberFormatException e) { |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1322 | Log.d(TAG, "Error when parsing widget id " + appWidget); |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1323 | return null; |
| 1324 | } |
| 1325 | } |
| 1326 | return appWidgetIds; |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 1327 | } |
Michael Jurka | 67a871d | 2012-11-01 18:26:01 -0700 | [diff] [blame] | 1328 | return new int[0]; |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1331 | private static String combineStrings(int[] list, String separator) { |
| 1332 | int listLength = list.length; |
| 1333 | |
| 1334 | switch (listLength) { |
| 1335 | case 0: { |
| 1336 | return ""; |
| 1337 | } |
| 1338 | case 1: { |
| 1339 | return Integer.toString(list[0]); |
| 1340 | } |
Michael Jurka | 20c41d5 | 2012-09-20 19:01:06 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1343 | int strLength = 0; |
| 1344 | int separatorLength = separator.length(); |
| 1345 | |
| 1346 | String[] stringList = new String[list.length]; |
| 1347 | for (int i = 0; i < listLength; i++) { |
| 1348 | stringList[i] = Integer.toString(list[i]); |
| 1349 | strLength += stringList[i].length(); |
| 1350 | if (i < listLength - 1) { |
| 1351 | strLength += separatorLength; |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | StringBuilder sb = new StringBuilder(strLength); |
| 1356 | |
| 1357 | for (int i = 0; i < listLength; i++) { |
| 1358 | sb.append(list[i]); |
| 1359 | if (i < listLength - 1) { |
| 1360 | sb.append(separator); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | return sb.toString(); |
| 1365 | } |
| 1366 | |
Jim Miller | 5111726 | 2012-11-04 17:58:09 -0800 | [diff] [blame] | 1367 | // appwidget used when appwidgets are disabled (we make an exception for |
| 1368 | // default clock widget) |
| 1369 | public void writeFallbackAppWidgetId(int appWidgetId) { |
| 1370 | Settings.Secure.putIntForUser(mContentResolver, |
| 1371 | Settings.Secure.LOCK_SCREEN_FALLBACK_APPWIDGET_ID, |
| 1372 | appWidgetId, |
| 1373 | UserHandle.USER_CURRENT); |
| 1374 | } |
| 1375 | |
| 1376 | // appwidget used when appwidgets are disabled (we make an exception for |
| 1377 | // default clock widget) |
| 1378 | public int getFallbackAppWidgetId() { |
| 1379 | return Settings.Secure.getIntForUser( |
| 1380 | mContentResolver, |
| 1381 | Settings.Secure.LOCK_SCREEN_FALLBACK_APPWIDGET_ID, |
| 1382 | AppWidgetManager.INVALID_APPWIDGET_ID, |
| 1383 | UserHandle.USER_CURRENT); |
| 1384 | } |
| 1385 | |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1386 | private void writeAppWidgets(int[] appWidgetIds) { |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1387 | Settings.Secure.putStringForUser(mContentResolver, |
| 1388 | Settings.Secure.LOCK_SCREEN_APPWIDGET_IDS, |
| 1389 | combineStrings(appWidgetIds, ","), |
| 1390 | UserHandle.USER_CURRENT); |
| 1391 | } |
| 1392 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1393 | // TODO: log an error if this returns false |
| 1394 | public boolean addAppWidget(int widgetId, int index) { |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1395 | int[] widgets = getAppWidgets(); |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1396 | if (widgets == null) { |
| 1397 | return false; |
| 1398 | } |
Michael Jurka | 229dd8b | 2012-10-26 16:06:12 -0700 | [diff] [blame] | 1399 | if (index < 0 || index > widgets.length) { |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1402 | int[] newWidgets = new int[widgets.length + 1]; |
| 1403 | for (int i = 0, j = 0; i < newWidgets.length; i++) { |
| 1404 | if (index == i) { |
| 1405 | newWidgets[i] = widgetId; |
| 1406 | i++; |
| 1407 | } |
| 1408 | if (i < newWidgets.length) { |
| 1409 | newWidgets[i] = widgets[j]; |
| 1410 | j++; |
| 1411 | } |
| 1412 | } |
| 1413 | writeAppWidgets(newWidgets); |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1414 | return true; |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1417 | public boolean removeAppWidget(int widgetId) { |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1418 | int[] widgets = getAppWidgets(); |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1419 | |
Jim Miller | a976860 | 2012-11-06 22:17:25 -0800 | [diff] [blame] | 1420 | if (widgets.length == 0) { |
| 1421 | return false; |
| 1422 | } |
| 1423 | |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1424 | int[] newWidgets = new int[widgets.length - 1]; |
| 1425 | for (int i = 0, j = 0; i < widgets.length; i++) { |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1426 | if (widgets[i] == widgetId) { |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1427 | // continue... |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1428 | } else if (j >= newWidgets.length) { |
| 1429 | // we couldn't find the widget |
| 1430 | return false; |
Michael Jurka | aa2859a | 2012-10-24 12:46:49 -0700 | [diff] [blame] | 1431 | } else { |
| 1432 | newWidgets[j] = widgets[i]; |
| 1433 | j++; |
| 1434 | } |
| 1435 | } |
| 1436 | writeAppWidgets(newWidgets); |
| 1437 | return true; |
Michael Jurka | 20c41d5 | 2012-09-20 19:01:06 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1440 | private long getLong(String secureSettingKey, long defaultValue, int userHandle) { |
| 1441 | try { |
| 1442 | return getLockSettings().getLong(secureSettingKey, defaultValue, userHandle); |
| 1443 | } catch (RemoteException re) { |
| 1444 | return defaultValue; |
| 1445 | } |
| 1446 | } |
| 1447 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1448 | private long getLong(String secureSettingKey, long defaultValue) { |
| 1449 | try { |
| 1450 | return getLockSettings().getLong(secureSettingKey, defaultValue, |
| 1451 | getCurrentOrCallingUserId()); |
| 1452 | } catch (RemoteException re) { |
| 1453 | return defaultValue; |
| 1454 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1455 | } |
| 1456 | |
Amith Yamasani | 156c435 | 2010-03-05 17:10:03 -0800 | [diff] [blame] | 1457 | private void setLong(String secureSettingKey, long value) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1458 | setLong(secureSettingKey, value, getCurrentOrCallingUserId()); |
| 1459 | } |
| 1460 | |
| 1461 | private void setLong(String secureSettingKey, long value, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1462 | try { |
Kenny Guy | 0cf1370 | 2013-11-29 16:33:05 +0000 | [diff] [blame] | 1463 | getLockSettings().setLong(secureSettingKey, value, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1464 | } catch (RemoteException re) { |
| 1465 | // What can we do? |
| 1466 | Log.e(TAG, "Couldn't write long " + secureSettingKey + re); |
| 1467 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1470 | private String getString(String secureSettingKey) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1471 | return getString(secureSettingKey, getCurrentOrCallingUserId()); |
| 1472 | } |
| 1473 | |
| 1474 | private String getString(String secureSettingKey, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1475 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1476 | return getLockSettings().getString(secureSettingKey, null, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1477 | } catch (RemoteException re) { |
| 1478 | return null; |
| 1479 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1482 | private void setString(String secureSettingKey, String value, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1483 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1484 | getLockSettings().setString(secureSettingKey, value, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1485 | } catch (RemoteException re) { |
| 1486 | // What can we do? |
| 1487 | Log.e(TAG, "Couldn't write string " + secureSettingKey + re); |
| 1488 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1491 | public boolean isSecure() { |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 1492 | long mode = getKeyguardStoredPasswordQuality(); |
| 1493 | final boolean isPattern = mode == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING; |
| 1494 | final boolean isPassword = mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 1495 | || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 1496 | || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1497 | || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC |
| 1498 | || mode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 1499 | final boolean secure = isPattern && isLockPatternEnabled() && savedPatternExists() |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1500 | || isPassword && savedPasswordExists(); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1501 | return secure; |
| 1502 | } |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1503 | |
| 1504 | /** |
John Wang | 0f7b3f8 | 2011-05-31 11:20:55 -0700 | [diff] [blame] | 1505 | * Sets the emergency button visibility based on isEmergencyCallCapable(). |
| 1506 | * |
| 1507 | * If the emergency button is visible, sets the text on the emergency button |
| 1508 | * to indicate what action will be taken. |
| 1509 | * |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1510 | * If there's currently a call in progress, the button will take them to the call |
Santos Cordon | 0bae09f | 2014-07-07 14:53:10 -0700 | [diff] [blame] | 1511 | * @param button The button to update |
| 1512 | * @param shown Indicates whether the given screen wants the emergency button to show at all |
| 1513 | * @param showIcon Indicates whether to show a phone icon for the button. |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1514 | */ |
Santos Cordon | 0bae09f | 2014-07-07 14:53:10 -0700 | [diff] [blame] | 1515 | public void updateEmergencyCallButtonState(Button button, boolean shown, boolean showIcon) { |
Jim Miller | 1f56edc | 2011-11-07 19:00:48 -0800 | [diff] [blame] | 1516 | if (isEmergencyCallCapable() && shown) { |
John Wang | 0f7b3f8 | 2011-05-31 11:20:55 -0700 | [diff] [blame] | 1517 | button.setVisibility(View.VISIBLE); |
| 1518 | } else { |
| 1519 | button.setVisibility(View.GONE); |
| 1520 | return; |
| 1521 | } |
| 1522 | |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1523 | int textId; |
Yorke Lee | cc5179f | 2014-09-03 15:01:18 -0700 | [diff] [blame] | 1524 | if (isInCall()) { |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1525 | // show "return to call" text and show phone icon |
| 1526 | textId = R.string.lockscreen_return_to_call; |
Jim Miller | 109f1fd | 2012-09-19 20:44:16 -0700 | [diff] [blame] | 1527 | int phoneCallIcon = showIcon ? R.drawable.stat_sys_phone_call : 0; |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1528 | button.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0); |
| 1529 | } else { |
| 1530 | textId = R.string.lockscreen_emergency_call; |
Jim Miller | 109f1fd | 2012-09-19 20:44:16 -0700 | [diff] [blame] | 1531 | int emergencyIcon = showIcon ? R.drawable.ic_emergency : 0; |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1532 | button.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0); |
| 1533 | } |
Jim Miller | e38c8e2 | 2013-09-24 15:54:04 -0700 | [diff] [blame] | 1534 | button.setText(textId); |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | /** |
| 1538 | * Resumes a call in progress. Typically launched from the EmergencyCall button |
| 1539 | * on various lockscreens. |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1540 | */ |
Santos Cordon | 3534ede | 2014-05-29 13:07:10 -0700 | [diff] [blame] | 1541 | public void resumeCall() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 1542 | getTelecommManager().showInCallScreen(false); |
Santos Cordon | 0bae09f | 2014-07-07 14:53:10 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Yorke Lee | cc5179f | 2014-09-03 15:01:18 -0700 | [diff] [blame] | 1545 | /** |
| 1546 | * @return {@code true} if there is a call currently in progress, {@code false} otherwise. |
| 1547 | */ |
| 1548 | public boolean isInCall() { |
| 1549 | return getTelecommManager().isInCall(); |
| 1550 | } |
| 1551 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 1552 | private TelecomManager getTelecommManager() { |
| 1553 | return (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 1554 | } |
Danielle Millett | 044a0a7 | 2011-11-07 15:42:12 -0500 | [diff] [blame] | 1555 | |
| 1556 | private void finishBiometricWeak() { |
| 1557 | setBoolean(BIOMETRIC_WEAK_EVER_CHOSEN_KEY, true); |
| 1558 | |
| 1559 | // Launch intent to show final screen, this also |
| 1560 | // moves the temporary gallery to the actual gallery |
| 1561 | Intent intent = new Intent(); |
| 1562 | intent.setClassName("com.android.facelock", |
| 1563 | "com.android.facelock.SetupEndScreen"); |
| 1564 | mContext.startActivity(intent); |
| 1565 | } |
| 1566 | |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 1567 | public void setPowerButtonInstantlyLocks(boolean enabled) { |
| 1568 | setBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, enabled); |
| 1569 | } |
| 1570 | |
| 1571 | public boolean getPowerButtonInstantlyLocks() { |
| 1572 | return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true); |
| 1573 | } |
Jim Miller | a976860 | 2012-11-06 22:17:25 -0800 | [diff] [blame] | 1574 | |
Jim Miller | 5111726 | 2012-11-04 17:58:09 -0800 | [diff] [blame] | 1575 | public static boolean isSafeModeEnabled() { |
| 1576 | try { |
| 1577 | return IWindowManager.Stub.asInterface( |
| 1578 | ServiceManager.getService("window")).isSafeModeEnabled(); |
| 1579 | } catch (RemoteException e) { |
| 1580 | // Shouldn't happen! |
| 1581 | } |
| 1582 | return false; |
| 1583 | } |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 1584 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1585 | /** |
| 1586 | * Determine whether the user has selected any non-system widgets in keyguard |
| 1587 | * |
| 1588 | * @return true if widgets have been selected |
| 1589 | */ |
| 1590 | public boolean hasWidgetsEnabledInKeyguard(int userid) { |
| 1591 | int widgets[] = getAppWidgets(userid); |
| 1592 | for (int i = 0; i < widgets.length; i++) { |
| 1593 | if (widgets[i] > 0) { |
| 1594 | return true; |
| 1595 | } |
| 1596 | } |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
| 1600 | public boolean getWidgetsEnabled() { |
| 1601 | return getWidgetsEnabled(getCurrentOrCallingUserId()); |
| 1602 | } |
| 1603 | |
| 1604 | public boolean getWidgetsEnabled(int userId) { |
| 1605 | return getBoolean(LOCKSCREEN_WIDGETS_ENABLED, false, userId); |
| 1606 | } |
| 1607 | |
| 1608 | public void setWidgetsEnabled(boolean enabled) { |
| 1609 | setWidgetsEnabled(enabled, getCurrentOrCallingUserId()); |
| 1610 | } |
| 1611 | |
| 1612 | public void setWidgetsEnabled(boolean enabled, int userId) { |
| 1613 | setBoolean(LOCKSCREEN_WIDGETS_ENABLED, enabled, userId); |
| 1614 | } |
| 1615 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1616 | public void setEnabledTrustAgents(Collection<ComponentName> activeTrustAgents) { |
| 1617 | setEnabledTrustAgents(activeTrustAgents, getCurrentOrCallingUserId()); |
| 1618 | } |
| 1619 | |
| 1620 | public List<ComponentName> getEnabledTrustAgents() { |
| 1621 | return getEnabledTrustAgents(getCurrentOrCallingUserId()); |
| 1622 | } |
| 1623 | |
| 1624 | public void setEnabledTrustAgents(Collection<ComponentName> activeTrustAgents, int userId) { |
| 1625 | StringBuilder sb = new StringBuilder(); |
| 1626 | for (ComponentName cn : activeTrustAgents) { |
| 1627 | if (sb.length() > 0) { |
| 1628 | sb.append(','); |
| 1629 | } |
| 1630 | sb.append(cn.flattenToShortString()); |
| 1631 | } |
| 1632 | setString(ENABLED_TRUST_AGENTS, sb.toString(), userId); |
| 1633 | getTrustManager().reportEnabledTrustAgentsChanged(getCurrentOrCallingUserId()); |
| 1634 | } |
| 1635 | |
| 1636 | public List<ComponentName> getEnabledTrustAgents(int userId) { |
| 1637 | String serialized = getString(ENABLED_TRUST_AGENTS, userId); |
| 1638 | if (TextUtils.isEmpty(serialized)) { |
| 1639 | return null; |
| 1640 | } |
| 1641 | String[] split = serialized.split(","); |
| 1642 | ArrayList<ComponentName> activeTrustAgents = new ArrayList<ComponentName>(split.length); |
| 1643 | for (String s : split) { |
| 1644 | if (!TextUtils.isEmpty(s)) { |
| 1645 | activeTrustAgents.add(ComponentName.unflattenFromString(s)); |
| 1646 | } |
| 1647 | } |
| 1648 | return activeTrustAgents; |
| 1649 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1650 | |
| 1651 | /** |
| 1652 | * @see android.app.trust.TrustManager#reportRequireCredentialEntry(int) |
| 1653 | */ |
| 1654 | public void requireCredentialEntry(int userId) { |
| 1655 | getTrustManager().reportRequireCredentialEntry(userId); |
| 1656 | } |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 1657 | |
| 1658 | private void onAfterChangingPassword() { |
| 1659 | getTrustManager().reportEnabledTrustAgentsChanged(getCurrentOrCallingUserId()); |
| 1660 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1661 | } |