ChooseLockGeneric.java revision 57d7fa545b792f2fa97cc6aad0e9f466ba415ffe
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.app.Activity; 20import android.app.PendingIntent; 21import android.app.admin.DevicePolicyManager; 22import android.content.Context; 23import android.content.Intent; 24import android.content.pm.UserInfo; 25import android.os.Bundle; 26import android.os.Process; 27import android.os.UserHandle; 28import android.os.UserManager; 29import android.preference.Preference; 30import android.preference.PreferenceScreen; 31import android.security.KeyStore; 32import android.util.EventLog; 33import android.util.MutableBoolean; 34import android.view.LayoutInflater; 35import android.view.View; 36import android.view.ViewGroup; 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 modIntent.putExtra(EXTRA_NO_HEADERS, true); 50 return modIntent; 51 } 52 53 @Override 54 protected boolean isValidFragment(String fragmentName) { 55 if (ChooseLockGenericFragment.class.getName().equals(fragmentName)) return true; 56 return false; 57 } 58 59 public static class InternalActivity extends ChooseLockGeneric { 60 } 61 62 public static class ChooseLockGenericFragment extends SettingsPreferenceFragment { 63 private static final int MIN_PASSWORD_LENGTH = 4; 64 private static final String KEY_UNLOCK_BACKUP_INFO = "unlock_backup_info"; 65 private static final String KEY_UNLOCK_SET_OFF = "unlock_set_off"; 66 private static final String KEY_UNLOCK_SET_NONE = "unlock_set_none"; 67 private static final String KEY_UNLOCK_SET_BIOMETRIC_WEAK = "unlock_set_biometric_weak"; 68 private static final String KEY_UNLOCK_SET_PIN = "unlock_set_pin"; 69 private static final String KEY_UNLOCK_SET_PASSWORD = "unlock_set_password"; 70 private static final String KEY_UNLOCK_SET_PATTERN = "unlock_set_pattern"; 71 private static final int CONFIRM_EXISTING_REQUEST = 100; 72 private static final int FALLBACK_REQUEST = 101; 73 private static final String PASSWORD_CONFIRMED = "password_confirmed"; 74 private static final String CONFIRM_CREDENTIALS = "confirm_credentials"; 75 private static final String WAITING_FOR_CONFIRMATION = "waiting_for_confirmation"; 76 private static final String FINISH_PENDING = "finish_pending"; 77 public static final String MINIMUM_QUALITY_KEY = "minimum_quality"; 78 79 private static final boolean ALWAY_SHOW_TUTORIAL = true; 80 81 private ChooseLockSettingsHelper mChooseLockSettingsHelper; 82 private DevicePolicyManager mDPM; 83 private KeyStore mKeyStore; 84 private boolean mPasswordConfirmed = false; 85 private boolean mWaitingForConfirmation = false; 86 private boolean mFinishPending = false; 87 88 @Override 89 public void onCreate(Bundle savedInstanceState) { 90 super.onCreate(savedInstanceState); 91 92 mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 93 mKeyStore = KeyStore.getInstance(); 94 mChooseLockSettingsHelper = new ChooseLockSettingsHelper(this.getActivity()); 95 96 // Defaults to needing to confirm credentials 97 final boolean confirmCredentials = getActivity().getIntent() 98 .getBooleanExtra(CONFIRM_CREDENTIALS, true); 99 if (getActivity() instanceof ChooseLockGeneric.InternalActivity) { 100 mPasswordConfirmed = !confirmCredentials; 101 } 102 103 if (savedInstanceState != null) { 104 mPasswordConfirmed = savedInstanceState.getBoolean(PASSWORD_CONFIRMED); 105 mWaitingForConfirmation = savedInstanceState.getBoolean(WAITING_FOR_CONFIRMATION); 106 mFinishPending = savedInstanceState.getBoolean(FINISH_PENDING); 107 } 108 109 if (mPasswordConfirmed) { 110 updatePreferencesOrFinish(); 111 } else if (!mWaitingForConfirmation) { 112 ChooseLockSettingsHelper helper = 113 new ChooseLockSettingsHelper(this.getActivity(), this); 114 if (!helper.launchConfirmationActivity(CONFIRM_EXISTING_REQUEST, null, null)) { 115 mPasswordConfirmed = true; // no password set, so no need to confirm 116 updatePreferencesOrFinish(); 117 } else { 118 mWaitingForConfirmation = true; 119 } 120 } 121 } 122 123 @Override 124 public void onResume() { 125 super.onResume(); 126 if (mFinishPending) { 127 mFinishPending = false; 128 finish(); 129 } 130 } 131 132 @Override 133 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 134 Preference preference) { 135 final String key = preference.getKey(); 136 boolean handled = true; 137 138 EventLog.writeEvent(EventLogTags.LOCK_SCREEN_TYPE, key); 139 140 if (KEY_UNLOCK_SET_OFF.equals(key)) { 141 updateUnlockMethodAndFinish( 142 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, true); 143 } else if (KEY_UNLOCK_SET_NONE.equals(key)) { 144 updateUnlockMethodAndFinish( 145 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, false); 146 } else if (KEY_UNLOCK_SET_BIOMETRIC_WEAK.equals(key)) { 147 updateUnlockMethodAndFinish( 148 DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK, false); 149 }else if (KEY_UNLOCK_SET_PATTERN.equals(key)) { 150 updateUnlockMethodAndFinish( 151 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, false); 152 } else if (KEY_UNLOCK_SET_PIN.equals(key)) { 153 updateUnlockMethodAndFinish( 154 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC, false); 155 } else if (KEY_UNLOCK_SET_PASSWORD.equals(key)) { 156 updateUnlockMethodAndFinish( 157 DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC, false); 158 } else { 159 handled = false; 160 } 161 return handled; 162 } 163 164 @Override 165 public View onCreateView(LayoutInflater inflater, ViewGroup container, 166 Bundle savedInstanceState) { 167 View v = super.onCreateView(inflater, container, savedInstanceState); 168 final boolean onlyShowFallback = getActivity().getIntent() 169 .getBooleanExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, false); 170 if (onlyShowFallback) { 171 View header = v.inflate(getActivity(), 172 R.layout.weak_biometric_fallback_header, null); 173 ((ListView) v.findViewById(android.R.id.list)).addHeaderView(header, null, false); 174 } 175 176 return v; 177 } 178 179 @Override 180 public void onActivityResult(int requestCode, int resultCode, Intent data) { 181 super.onActivityResult(requestCode, resultCode, data); 182 mWaitingForConfirmation = false; 183 if (requestCode == CONFIRM_EXISTING_REQUEST && resultCode == Activity.RESULT_OK) { 184 mPasswordConfirmed = true; 185 updatePreferencesOrFinish(); 186 } else if(requestCode == FALLBACK_REQUEST) { 187 mChooseLockSettingsHelper.utils().deleteTempGallery(); 188 getActivity().setResult(resultCode); 189 finish(); 190 } else { 191 getActivity().setResult(Activity.RESULT_CANCELED); 192 finish(); 193 } 194 } 195 196 @Override 197 public void onSaveInstanceState(Bundle outState) { 198 super.onSaveInstanceState(outState); 199 // Saved so we don't force user to re-enter their password if configuration changes 200 outState.putBoolean(PASSWORD_CONFIRMED, mPasswordConfirmed); 201 outState.putBoolean(WAITING_FOR_CONFIRMATION, mWaitingForConfirmation); 202 outState.putBoolean(FINISH_PENDING, mFinishPending); 203 } 204 205 private void updatePreferencesOrFinish() { 206 Intent intent = getActivity().getIntent(); 207 int quality = intent.getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY, -1); 208 if (quality == -1) { 209 // If caller didn't specify password quality, show UI and allow the user to choose. 210 quality = intent.getIntExtra(MINIMUM_QUALITY_KEY, -1); 211 MutableBoolean allowBiometric = new MutableBoolean(false); 212 quality = upgradeQuality(quality, allowBiometric); 213 final PreferenceScreen prefScreen = getPreferenceScreen(); 214 if (prefScreen != null) { 215 prefScreen.removeAll(); 216 } 217 addPreferencesFromResource(R.xml.security_settings_picker); 218 disableUnusablePreferences(quality, allowBiometric); 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; 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 /** 299 * Check whether the key is allowed for fallback (e.g. bio sensor). Returns true if it's 300 * supported as a backup. 301 * 302 * @param key 303 * @return true if allowed 304 */ 305 private boolean allowedForFallback(String key) { 306 return KEY_UNLOCK_BACKUP_INFO.equals(key) || 307 KEY_UNLOCK_SET_PATTERN.equals(key) || KEY_UNLOCK_SET_PIN.equals(key); 308 } 309 310 private Intent getBiometricSensorIntent() { 311 Intent fallBackIntent = new Intent().setClass(getActivity(), 312 ChooseLockGeneric.InternalActivity.class); 313 fallBackIntent.putExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, true); 314 fallBackIntent.putExtra(CONFIRM_CREDENTIALS, false); 315 fallBackIntent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, 316 R.string.backup_lock_settings_picker_title); 317 318 boolean showTutorial = ALWAY_SHOW_TUTORIAL || 319 !mChooseLockSettingsHelper.utils().isBiometricWeakEverChosen(); 320 Intent intent = new Intent(); 321 intent.setClassName("com.android.facelock", "com.android.facelock.SetupIntro"); 322 intent.putExtra("showTutorial", showTutorial); 323 PendingIntent pending = PendingIntent.getActivity(getActivity(), 0, fallBackIntent, 0); 324 intent.putExtra("PendingIntent", pending); 325 return intent; 326 } 327 328 /** 329 * Invokes an activity to change the user's pattern, password or PIN based on given quality 330 * and minimum quality specified by DevicePolicyManager. If quality is 331 * {@link DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED}, password is cleared. 332 * 333 * @param quality the desired quality. Ignored if DevicePolicyManager requires more security 334 * @param disabled whether or not to show LockScreen at all. Only meaningful when quality is 335 * {@link DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED} 336 */ 337 void updateUnlockMethodAndFinish(int quality, boolean disabled) { 338 // Sanity check. We should never get here without confirming user's existing password. 339 if (!mPasswordConfirmed) { 340 throw new IllegalStateException("Tried to update password without confirming it"); 341 } 342 343 final boolean isFallback = getActivity().getIntent() 344 .getBooleanExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, false); 345 346 quality = upgradeQuality(quality, null); 347 348 if (quality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) { 349 int minLength = mDPM.getPasswordMinimumLength(null); 350 if (minLength < MIN_PASSWORD_LENGTH) { 351 minLength = MIN_PASSWORD_LENGTH; 352 } 353 final int maxLength = mDPM.getPasswordMaximumLength(quality); 354 Intent intent = new Intent().setClass(getActivity(), ChooseLockPassword.class); 355 intent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, quality); 356 intent.putExtra(ChooseLockPassword.PASSWORD_MIN_KEY, minLength); 357 intent.putExtra(ChooseLockPassword.PASSWORD_MAX_KEY, maxLength); 358 intent.putExtra(CONFIRM_CREDENTIALS, false); 359 intent.putExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, 360 isFallback); 361 if (isFallback) { 362 startActivityForResult(intent, FALLBACK_REQUEST); 363 return; 364 } else { 365 mFinishPending = true; 366 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 367 startActivity(intent); 368 } 369 } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) { 370 Intent intent = new Intent(getActivity(), ChooseLockPattern.class); 371 intent.putExtra("key_lock_method", "pattern"); 372 intent.putExtra(CONFIRM_CREDENTIALS, false); 373 intent.putExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, 374 isFallback); 375 if (isFallback) { 376 startActivityForResult(intent, FALLBACK_REQUEST); 377 return; 378 } else { 379 mFinishPending = true; 380 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 381 startActivity(intent); 382 } 383 } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) { 384 Intent intent = getBiometricSensorIntent(); 385 mFinishPending = true; 386 startActivity(intent); 387 } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { 388 mChooseLockSettingsHelper.utils().clearLock(false); 389 mChooseLockSettingsHelper.utils().setLockScreenDisabled(disabled); 390 getActivity().setResult(Activity.RESULT_OK); 391 finish(); 392 } else { 393 finish(); 394 } 395 } 396 397 @Override 398 protected int getHelpResource() { 399 return R.string.help_url_choose_lockscreen; 400 } 401 402 } 403} 404