ChooseLockGeneric.java revision 524484426855f814ff1f3189fd5221dd630dbf8c
1/* 2 * Copyright (C) 2010 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 17package com.android.settings; 18 19import android.accessibilityservice.AccessibilityServiceInfo; 20import android.app.Activity; 21import android.app.PendingIntent; 22import android.app.admin.DevicePolicyManager; 23import android.content.Context; 24import android.content.Intent; 25import android.content.pm.UserInfo; 26import android.os.Bundle; 27import android.os.UserManager; 28import android.preference.Preference; 29import android.preference.PreferenceScreen; 30import android.security.KeyStore; 31import android.util.EventLog; 32import android.util.MutableBoolean; 33import android.view.LayoutInflater; 34import android.view.View; 35import android.view.ViewGroup; 36import android.view.accessibility.AccessibilityManager; 37import android.widget.ListView; 38 39import com.android.internal.widget.LockPatternUtils; 40 41import java.util.List; 42 43public class ChooseLockGeneric extends SettingsActivity { 44 45 @Override 46 public Intent getIntent() { 47 Intent modIntent = new Intent(super.getIntent()); 48 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, ChooseLockGenericFragment.class.getName()); 49 return modIntent; 50 } 51 52 @Override 53 protected boolean isValidFragment(String fragmentName) { 54 if (ChooseLockGenericFragment.class.getName().equals(fragmentName)) return true; 55 return false; 56 } 57 58 public static class InternalActivity extends ChooseLockGeneric { 59 } 60 61 public static class ChooseLockGenericFragment extends SettingsPreferenceFragment { 62 private static final int MIN_PASSWORD_LENGTH = 4; 63 private static final String KEY_UNLOCK_BACKUP_INFO = "unlock_backup_info"; 64 private static final String KEY_UNLOCK_SET_OFF = "unlock_set_off"; 65 private static final String KEY_UNLOCK_SET_NONE = "unlock_set_none"; 66 private static final String KEY_UNLOCK_SET_BIOMETRIC_WEAK = "unlock_set_biometric_weak"; 67 private static final String KEY_UNLOCK_SET_PIN = "unlock_set_pin"; 68 private static final String KEY_UNLOCK_SET_PASSWORD = "unlock_set_password"; 69 private static final String KEY_UNLOCK_SET_PATTERN = "unlock_set_pattern"; 70 private static final int CONFIRM_EXISTING_REQUEST = 100; 71 private static final int FALLBACK_REQUEST = 101; 72 private static final String PASSWORD_CONFIRMED = "password_confirmed"; 73 private static final String CONFIRM_CREDENTIALS = "confirm_credentials"; 74 private static final String WAITING_FOR_CONFIRMATION = "waiting_for_confirmation"; 75 private static final String FINISH_PENDING = "finish_pending"; 76 public static final String MINIMUM_QUALITY_KEY = "minimum_quality"; 77 78 private static final boolean ALWAY_SHOW_TUTORIAL = true; 79 80 private ChooseLockSettingsHelper mChooseLockSettingsHelper; 81 private DevicePolicyManager mDPM; 82 private KeyStore mKeyStore; 83 private boolean mPasswordConfirmed = false; 84 private boolean mWaitingForConfirmation = false; 85 private boolean mFinishPending = false; 86 87 @Override 88 public void onCreate(Bundle savedInstanceState) { 89 super.onCreate(savedInstanceState); 90 91 mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 92 mKeyStore = KeyStore.getInstance(); 93 mChooseLockSettingsHelper = new ChooseLockSettingsHelper(this.getActivity()); 94 95 // Defaults to needing to confirm credentials 96 final boolean confirmCredentials = getActivity().getIntent() 97 .getBooleanExtra(CONFIRM_CREDENTIALS, true); 98 if (getActivity() instanceof ChooseLockGeneric.InternalActivity) { 99 mPasswordConfirmed = !confirmCredentials; 100 } 101 102 if (savedInstanceState != null) { 103 mPasswordConfirmed = savedInstanceState.getBoolean(PASSWORD_CONFIRMED); 104 mWaitingForConfirmation = savedInstanceState.getBoolean(WAITING_FOR_CONFIRMATION); 105 mFinishPending = savedInstanceState.getBoolean(FINISH_PENDING); 106 } 107 108 if (mPasswordConfirmed) { 109 updatePreferencesOrFinish(); 110 } else if (!mWaitingForConfirmation) { 111 ChooseLockSettingsHelper helper = 112 new ChooseLockSettingsHelper(this.getActivity(), this); 113 if (!helper.launchConfirmationActivity(CONFIRM_EXISTING_REQUEST, null, null)) { 114 mPasswordConfirmed = true; // no password set, so no need to confirm 115 updatePreferencesOrFinish(); 116 } else { 117 mWaitingForConfirmation = true; 118 } 119 } 120 } 121 122 @Override 123 public void onResume() { 124 super.onResume(); 125 if (mFinishPending) { 126 mFinishPending = false; 127 finish(); 128 } 129 } 130 131 @Override 132 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 133 Preference preference) { 134 final String key = preference.getKey(); 135 boolean handled = true; 136 137 EventLog.writeEvent(EventLogTags.LOCK_SCREEN_TYPE, key); 138 139 if (KEY_UNLOCK_SET_OFF.equals(key)) { 140 updateUnlockMethodAndFinish( 141 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, true); 142 } else if (KEY_UNLOCK_SET_NONE.equals(key)) { 143 updateUnlockMethodAndFinish( 144 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, false); 145 } else if (KEY_UNLOCK_SET_BIOMETRIC_WEAK.equals(key)) { 146 updateUnlockMethodAndFinish( 147 DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK, false); 148 }else if (KEY_UNLOCK_SET_PATTERN.equals(key)) { 149 updateUnlockMethodAndFinish( 150 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, false); 151 } else if (KEY_UNLOCK_SET_PIN.equals(key)) { 152 updateUnlockMethodAndFinish( 153 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC, false); 154 } else if (KEY_UNLOCK_SET_PASSWORD.equals(key)) { 155 updateUnlockMethodAndFinish( 156 DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC, false); 157 } else { 158 handled = false; 159 } 160 return handled; 161 } 162 163 @Override 164 public View onCreateView(LayoutInflater inflater, ViewGroup container, 165 Bundle savedInstanceState) { 166 View v = super.onCreateView(inflater, container, savedInstanceState); 167 final boolean onlyShowFallback = getActivity().getIntent() 168 .getBooleanExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, false); 169 if (onlyShowFallback) { 170 View header = v.inflate(getActivity(), 171 R.layout.weak_biometric_fallback_header, null); 172 ((ListView) v.findViewById(android.R.id.list)).addHeaderView(header, null, false); 173 } 174 175 return v; 176 } 177 178 @Override 179 public void onActivityResult(int requestCode, int resultCode, Intent data) { 180 super.onActivityResult(requestCode, resultCode, data); 181 mWaitingForConfirmation = false; 182 if (requestCode == CONFIRM_EXISTING_REQUEST && resultCode == Activity.RESULT_OK) { 183 mPasswordConfirmed = true; 184 updatePreferencesOrFinish(); 185 } else if(requestCode == FALLBACK_REQUEST) { 186 mChooseLockSettingsHelper.utils().deleteTempGallery(); 187 getActivity().setResult(resultCode); 188 finish(); 189 } else { 190 getActivity().setResult(Activity.RESULT_CANCELED); 191 finish(); 192 } 193 } 194 195 @Override 196 public void onSaveInstanceState(Bundle outState) { 197 super.onSaveInstanceState(outState); 198 // Saved so we don't force user to re-enter their password if configuration changes 199 outState.putBoolean(PASSWORD_CONFIRMED, mPasswordConfirmed); 200 outState.putBoolean(WAITING_FOR_CONFIRMATION, mWaitingForConfirmation); 201 outState.putBoolean(FINISH_PENDING, mFinishPending); 202 } 203 204 private void updatePreferencesOrFinish() { 205 Intent intent = getActivity().getIntent(); 206 int quality = intent.getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY, -1); 207 if (quality == -1) { 208 // If caller didn't specify password quality, show UI and allow the user to choose. 209 quality = intent.getIntExtra(MINIMUM_QUALITY_KEY, -1); 210 MutableBoolean allowBiometric = new MutableBoolean(false); 211 quality = upgradeQuality(quality, allowBiometric); 212 final PreferenceScreen prefScreen = getPreferenceScreen(); 213 if (prefScreen != null) { 214 prefScreen.removeAll(); 215 } 216 addPreferencesFromResource(R.xml.security_settings_picker); 217 disableUnusablePreferences(quality, allowBiometric); 218 updatePreferenceSummaryIfNeeded(); 219 } else { 220 updateUnlockMethodAndFinish(quality, false); 221 } 222 } 223 224 /** increases the quality if necessary, and returns whether biometric is allowed */ 225 private int upgradeQuality(int quality, MutableBoolean allowBiometric) { 226 quality = upgradeQualityForDPM(quality); 227 quality = upgradeQualityForKeyStore(quality); 228 return quality; 229 } 230 231 private int upgradeQualityForDPM(int quality) { 232 // Compare min allowed password quality 233 int minQuality = mDPM.getPasswordQuality(null); 234 if (quality < minQuality) { 235 quality = minQuality; 236 } 237 return quality; 238 } 239 240 private int upgradeQualityForKeyStore(int quality) { 241 if (!mKeyStore.isEmpty()) { 242 if (quality < CredentialStorage.MIN_PASSWORD_QUALITY) { 243 quality = CredentialStorage.MIN_PASSWORD_QUALITY; 244 } 245 } 246 return quality; 247 } 248 249 /*** 250 * Disables preferences that are less secure than required quality. 251 * 252 * @param quality the requested quality. 253 */ 254 private void disableUnusablePreferences(final int quality, MutableBoolean allowBiometric) { 255 final PreferenceScreen entries = getPreferenceScreen(); 256 final boolean onlyShowFallback = getActivity().getIntent() 257 .getBooleanExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, false); 258 final boolean weakBiometricAvailable = 259 mChooseLockSettingsHelper.utils().isBiometricWeakInstalled(); 260 261 // if there are multiple users, disable "None" setting 262 UserManager mUm = (UserManager) getSystemService(Context.USER_SERVICE); 263 List<UserInfo> users = mUm.getUsers(true); 264 final boolean singleUser = users.size() == 1; 265 266 for (int i = entries.getPreferenceCount() - 1; i >= 0; --i) { 267 Preference pref = entries.getPreference(i); 268 if (pref instanceof PreferenceScreen) { 269 final String key = ((PreferenceScreen) pref).getKey(); 270 boolean enabled = true; 271 boolean visible = true; 272 if (KEY_UNLOCK_SET_OFF.equals(key)) { 273 enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 274 visible = singleUser; // don't show when there's more than 1 user 275 } else if (KEY_UNLOCK_SET_NONE.equals(key)) { 276 enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 277 } else if (KEY_UNLOCK_SET_BIOMETRIC_WEAK.equals(key)) { 278 enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK || 279 allowBiometric.value; 280 visible = weakBiometricAvailable; // If not available, then don't show it. 281 } else if (KEY_UNLOCK_SET_PATTERN.equals(key)) { 282 enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING; 283 } else if (KEY_UNLOCK_SET_PIN.equals(key)) { 284 enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; 285 } else if (KEY_UNLOCK_SET_PASSWORD.equals(key)) { 286 enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; 287 } 288 if (!visible || (onlyShowFallback && !allowedForFallback(key))) { 289 entries.removePreference(pref); 290 } else if (!enabled) { 291 pref.setSummary(R.string.unlock_set_unlock_disabled_summary); 292 pref.setEnabled(false); 293 } 294 } 295 } 296 } 297 298 private void updatePreferenceSummaryIfNeeded() { 299 if (AccessibilityManager.getInstance(getActivity()).getEnabledAccessibilityServiceList( 300 AccessibilityServiceInfo.FEEDBACK_ALL_MASK).isEmpty()) { 301 return; 302 } 303 304 CharSequence summary = getString(R.string.secure_lock_encryption_warning); 305 306 PreferenceScreen screen = getPreferenceScreen(); 307 final int preferenceCount = screen.getPreferenceCount(); 308 for (int i = 0; i < preferenceCount; i++) { 309 Preference preference = screen.getPreference(i); 310 switch (preference.getKey()) { 311 case KEY_UNLOCK_SET_PATTERN: 312 case KEY_UNLOCK_SET_PIN: 313 case KEY_UNLOCK_SET_PASSWORD: { 314 preference.setSummary(summary); 315 } break; 316 } 317 } 318 } 319 320 /** 321 * Check whether the key is allowed for fallback (e.g. bio sensor). Returns true if it's 322 * supported as a backup. 323 * 324 * @param key 325 * @return true if allowed 326 */ 327 private boolean allowedForFallback(String key) { 328 return KEY_UNLOCK_BACKUP_INFO.equals(key) || 329 KEY_UNLOCK_SET_PATTERN.equals(key) || KEY_UNLOCK_SET_PIN.equals(key); 330 } 331 332 private Intent getBiometricSensorIntent() { 333 Intent fallBackIntent = new Intent().setClass(getActivity(), 334 ChooseLockGeneric.InternalActivity.class); 335 fallBackIntent.putExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, true); 336 fallBackIntent.putExtra(CONFIRM_CREDENTIALS, false); 337 fallBackIntent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, 338 R.string.backup_lock_settings_picker_title); 339 340 boolean showTutorial = ALWAY_SHOW_TUTORIAL || 341 !mChooseLockSettingsHelper.utils().isBiometricWeakEverChosen(); 342 Intent intent = new Intent(); 343 intent.setClassName("com.android.facelock", "com.android.facelock.SetupIntro"); 344 intent.putExtra("showTutorial", showTutorial); 345 PendingIntent pending = PendingIntent.getActivity(getActivity(), 0, fallBackIntent, 0); 346 intent.putExtra("PendingIntent", pending); 347 return intent; 348 } 349 350 /** 351 * Invokes an activity to change the user's pattern, password or PIN based on given quality 352 * and minimum quality specified by DevicePolicyManager. If quality is 353 * {@link DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED}, password is cleared. 354 * 355 * @param quality the desired quality. Ignored if DevicePolicyManager requires more security 356 * @param disabled whether or not to show LockScreen at all. Only meaningful when quality is 357 * {@link DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED} 358 */ 359 void updateUnlockMethodAndFinish(int quality, boolean disabled) { 360 // Sanity check. We should never get here without confirming user's existing password. 361 if (!mPasswordConfirmed) { 362 throw new IllegalStateException("Tried to update password without confirming it"); 363 } 364 365 final boolean isFallback = getActivity().getIntent() 366 .getBooleanExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, false); 367 368 quality = upgradeQuality(quality, null); 369 370 if (quality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) { 371 int minLength = mDPM.getPasswordMinimumLength(null); 372 if (minLength < MIN_PASSWORD_LENGTH) { 373 minLength = MIN_PASSWORD_LENGTH; 374 } 375 final int maxLength = mDPM.getPasswordMaximumLength(quality); 376 Intent intent = new Intent().setClass(getActivity(), ChooseLockPassword.class); 377 intent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, quality); 378 intent.putExtra(ChooseLockPassword.PASSWORD_MIN_KEY, minLength); 379 intent.putExtra(ChooseLockPassword.PASSWORD_MAX_KEY, maxLength); 380 intent.putExtra(CONFIRM_CREDENTIALS, false); 381 intent.putExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, 382 isFallback); 383 if (isFallback) { 384 startActivityForResult(intent, FALLBACK_REQUEST); 385 return; 386 } else { 387 mFinishPending = true; 388 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 389 startActivity(intent); 390 } 391 } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) { 392 Intent intent = new Intent(getActivity(), ChooseLockPattern.class); 393 intent.putExtra("key_lock_method", "pattern"); 394 intent.putExtra(CONFIRM_CREDENTIALS, false); 395 intent.putExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, 396 isFallback); 397 if (isFallback) { 398 startActivityForResult(intent, FALLBACK_REQUEST); 399 return; 400 } else { 401 mFinishPending = true; 402 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 403 startActivity(intent); 404 } 405 } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) { 406 Intent intent = getBiometricSensorIntent(); 407 mFinishPending = true; 408 startActivity(intent); 409 } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { 410 mChooseLockSettingsHelper.utils().clearLock(false); 411 mChooseLockSettingsHelper.utils().setLockScreenDisabled(disabled); 412 getActivity().setResult(Activity.RESULT_OK); 413 finish(); 414 } else { 415 finish(); 416 } 417 } 418 419 @Override 420 protected int getHelpResource() { 421 return R.string.help_url_choose_lockscreen; 422 } 423 424 } 425} 426