ChooseLockPassword.java revision 6d5c75f6cd928befb348fa283e53edc10716f151
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 com.android.internal.widget.LockPatternUtils; 20import com.android.internal.widget.PasswordEntryKeyboardHelper; 21import com.android.internal.widget.PasswordEntryKeyboardView; 22import com.android.settings.notification.RedactionInterstitial; 23 24import android.app.Activity; 25import android.app.Fragment; 26import android.app.admin.DevicePolicyManager; 27import android.content.Intent; 28import android.inputmethodservice.KeyboardView; 29import android.os.Bundle; 30import android.os.Handler; 31import android.os.Message; 32import android.text.Editable; 33import android.text.InputType; 34import android.text.Selection; 35import android.text.Spannable; 36import android.text.TextUtils; 37import android.text.TextWatcher; 38import android.view.KeyEvent; 39import android.view.LayoutInflater; 40import android.view.View; 41import android.view.ViewGroup; 42import android.view.View.OnClickListener; 43import android.view.inputmethod.EditorInfo; 44import android.widget.Button; 45import android.widget.TextView; 46import android.widget.TextView.OnEditorActionListener; 47 48public class ChooseLockPassword extends SettingsActivity { 49 public static final String PASSWORD_MIN_KEY = "lockscreen.password_min"; 50 public static final String PASSWORD_MAX_KEY = "lockscreen.password_max"; 51 public static final String PASSWORD_MIN_LETTERS_KEY = "lockscreen.password_min_letters"; 52 public static final String PASSWORD_MIN_LOWERCASE_KEY = "lockscreen.password_min_lowercase"; 53 public static final String PASSWORD_MIN_UPPERCASE_KEY = "lockscreen.password_min_uppercase"; 54 public static final String PASSWORD_MIN_NUMERIC_KEY = "lockscreen.password_min_numeric"; 55 public static final String PASSWORD_MIN_SYMBOLS_KEY = "lockscreen.password_min_symbols"; 56 public static final String PASSWORD_MIN_NONLETTER_KEY = "lockscreen.password_min_nonletter"; 57 58 @Override 59 public Intent getIntent() { 60 Intent modIntent = new Intent(super.getIntent()); 61 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, ChooseLockPasswordFragment.class.getName()); 62 return modIntent; 63 } 64 65 @Override 66 protected boolean isValidFragment(String fragmentName) { 67 if (ChooseLockPasswordFragment.class.getName().equals(fragmentName)) return true; 68 return false; 69 } 70 71 @Override 72 public void onCreate(Bundle savedInstanceState) { 73 // TODO: Fix on phones 74 // Disable IME on our window since we provide our own keyboard 75 //getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, 76 //WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 77 super.onCreate(savedInstanceState); 78 CharSequence msg = getText(R.string.lockpassword_choose_your_password_header); 79 setTitle(msg); 80 } 81 82 public static class ChooseLockPasswordFragment extends Fragment 83 implements OnClickListener, OnEditorActionListener, TextWatcher { 84 private static final String KEY_FIRST_PIN = "first_pin"; 85 private static final String KEY_UI_STAGE = "ui_stage"; 86 private TextView mPasswordEntry; 87 private int mPasswordMinLength = 4; 88 private int mPasswordMaxLength = 16; 89 private int mPasswordMinLetters = 0; 90 private int mPasswordMinUpperCase = 0; 91 private int mPasswordMinLowerCase = 0; 92 private int mPasswordMinSymbols = 0; 93 private int mPasswordMinNumeric = 0; 94 private int mPasswordMinNonLetter = 0; 95 private LockPatternUtils mLockPatternUtils; 96 private int mRequestedQuality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; 97 private ChooseLockSettingsHelper mChooseLockSettingsHelper; 98 private Stage mUiStage = Stage.Introduction; 99 private boolean mDone = false; 100 private TextView mHeaderText; 101 private String mFirstPin; 102 private KeyboardView mKeyboardView; 103 private PasswordEntryKeyboardHelper mKeyboardHelper; 104 private boolean mIsAlphaMode; 105 private Button mCancelButton; 106 private Button mNextButton; 107 private static final int CONFIRM_EXISTING_REQUEST = 58; 108 static final int RESULT_FINISHED = RESULT_FIRST_USER; 109 private static final long ERROR_MESSAGE_TIMEOUT = 3000; 110 private static final int MSG_SHOW_ERROR = 1; 111 112 private Handler mHandler = new Handler() { 113 @Override 114 public void handleMessage(Message msg) { 115 if (msg.what == MSG_SHOW_ERROR) { 116 updateStage((Stage) msg.obj); 117 } 118 } 119 }; 120 121 /** 122 * Keep track internally of where the user is in choosing a pattern. 123 */ 124 protected enum Stage { 125 126 Introduction(R.string.lockpassword_choose_your_password_header, 127 R.string.lockpassword_choose_your_pin_header, 128 R.string.lockpassword_continue_label), 129 130 NeedToConfirm(R.string.lockpassword_confirm_your_password_header, 131 R.string.lockpassword_confirm_your_pin_header, 132 R.string.lockpassword_ok_label), 133 134 ConfirmWrong(R.string.lockpassword_confirm_passwords_dont_match, 135 R.string.lockpassword_confirm_pins_dont_match, 136 R.string.lockpassword_continue_label); 137 138 Stage(int hintInAlpha, int hintInNumeric, int nextButtonText) { 139 this.alphaHint = hintInAlpha; 140 this.numericHint = hintInNumeric; 141 this.buttonText = nextButtonText; 142 } 143 144 public final int alphaHint; 145 public final int numericHint; 146 public final int buttonText; 147 } 148 149 // required constructor for fragments 150 public ChooseLockPasswordFragment() { 151 152 } 153 154 @Override 155 public void onCreate(Bundle savedInstanceState) { 156 super.onCreate(savedInstanceState); 157 mLockPatternUtils = new LockPatternUtils(getActivity()); 158 Intent intent = getActivity().getIntent(); 159 if (!(getActivity() instanceof ChooseLockPassword)) { 160 throw new SecurityException("Fragment contained in wrong activity"); 161 } 162 mRequestedQuality = Math.max(intent.getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY, 163 mRequestedQuality), mLockPatternUtils.getRequestedPasswordQuality()); 164 mPasswordMinLength = Math.max( 165 intent.getIntExtra(PASSWORD_MIN_KEY, mPasswordMinLength), mLockPatternUtils 166 .getRequestedMinimumPasswordLength()); 167 mPasswordMaxLength = intent.getIntExtra(PASSWORD_MAX_KEY, mPasswordMaxLength); 168 mPasswordMinLetters = Math.max(intent.getIntExtra(PASSWORD_MIN_LETTERS_KEY, 169 mPasswordMinLetters), mLockPatternUtils.getRequestedPasswordMinimumLetters()); 170 mPasswordMinUpperCase = Math.max(intent.getIntExtra(PASSWORD_MIN_UPPERCASE_KEY, 171 mPasswordMinUpperCase), mLockPatternUtils.getRequestedPasswordMinimumUpperCase()); 172 mPasswordMinLowerCase = Math.max(intent.getIntExtra(PASSWORD_MIN_LOWERCASE_KEY, 173 mPasswordMinLowerCase), mLockPatternUtils.getRequestedPasswordMinimumLowerCase()); 174 mPasswordMinNumeric = Math.max(intent.getIntExtra(PASSWORD_MIN_NUMERIC_KEY, 175 mPasswordMinNumeric), mLockPatternUtils.getRequestedPasswordMinimumNumeric()); 176 mPasswordMinSymbols = Math.max(intent.getIntExtra(PASSWORD_MIN_SYMBOLS_KEY, 177 mPasswordMinSymbols), mLockPatternUtils.getRequestedPasswordMinimumSymbols()); 178 mPasswordMinNonLetter = Math.max(intent.getIntExtra(PASSWORD_MIN_NONLETTER_KEY, 179 mPasswordMinNonLetter), mLockPatternUtils.getRequestedPasswordMinimumNonLetter()); 180 181 mChooseLockSettingsHelper = new ChooseLockSettingsHelper(getActivity()); 182 } 183 184 @Override 185 public View onCreateView(LayoutInflater inflater, ViewGroup container, 186 Bundle savedInstanceState) { 187 188 View view = inflater.inflate(R.layout.choose_lock_password, null); 189 190 mCancelButton = (Button) view.findViewById(R.id.cancel_button); 191 mCancelButton.setOnClickListener(this); 192 mNextButton = (Button) view.findViewById(R.id.next_button); 193 mNextButton.setOnClickListener(this); 194 195 mIsAlphaMode = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC == mRequestedQuality 196 || DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC == mRequestedQuality 197 || DevicePolicyManager.PASSWORD_QUALITY_COMPLEX == mRequestedQuality; 198 mKeyboardView = (PasswordEntryKeyboardView) view.findViewById(R.id.keyboard); 199 mPasswordEntry = (TextView) view.findViewById(R.id.password_entry); 200 mPasswordEntry.setOnEditorActionListener(this); 201 mPasswordEntry.addTextChangedListener(this); 202 203 final Activity activity = getActivity(); 204 mKeyboardHelper = new PasswordEntryKeyboardHelper(activity, 205 mKeyboardView, mPasswordEntry); 206 mKeyboardHelper.setKeyboardMode(mIsAlphaMode ? 207 PasswordEntryKeyboardHelper.KEYBOARD_MODE_ALPHA 208 : PasswordEntryKeyboardHelper.KEYBOARD_MODE_NUMERIC); 209 210 mHeaderText = (TextView) view.findViewById(R.id.headerText); 211 mKeyboardView.requestFocus(); 212 213 int currentType = mPasswordEntry.getInputType(); 214 mPasswordEntry.setInputType(mIsAlphaMode ? currentType 215 : (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD)); 216 217 Intent intent = getActivity().getIntent(); 218 final boolean confirmCredentials = intent.getBooleanExtra("confirm_credentials", true); 219 if (savedInstanceState == null) { 220 updateStage(Stage.Introduction); 221 if (confirmCredentials) { 222 mChooseLockSettingsHelper.launchConfirmationActivity(CONFIRM_EXISTING_REQUEST, 223 null, null); 224 } 225 } else { 226 mFirstPin = savedInstanceState.getString(KEY_FIRST_PIN); 227 final String state = savedInstanceState.getString(KEY_UI_STAGE); 228 if (state != null) { 229 mUiStage = Stage.valueOf(state); 230 updateStage(mUiStage); 231 } 232 } 233 mDone = false; 234 if (activity instanceof SettingsActivity) { 235 final SettingsActivity sa = (SettingsActivity) activity; 236 int id = mIsAlphaMode ? R.string.lockpassword_choose_your_password_header 237 : R.string.lockpassword_choose_your_pin_header; 238 CharSequence title = getText(id); 239 sa.setTitle(title); 240 } 241 242 return view; 243 } 244 245 @Override 246 public void onResume() { 247 super.onResume(); 248 updateStage(mUiStage); 249 mKeyboardView.requestFocus(); 250 } 251 252 @Override 253 public void onPause() { 254 mHandler.removeMessages(MSG_SHOW_ERROR); 255 256 super.onPause(); 257 } 258 259 @Override 260 public void onSaveInstanceState(Bundle outState) { 261 super.onSaveInstanceState(outState); 262 outState.putString(KEY_UI_STAGE, mUiStage.name()); 263 outState.putString(KEY_FIRST_PIN, mFirstPin); 264 } 265 266 @Override 267 public void onActivityResult(int requestCode, int resultCode, 268 Intent data) { 269 super.onActivityResult(requestCode, resultCode, data); 270 switch (requestCode) { 271 case CONFIRM_EXISTING_REQUEST: 272 if (resultCode != Activity.RESULT_OK) { 273 getActivity().setResult(RESULT_FINISHED); 274 getActivity().finish(); 275 } 276 break; 277 } 278 } 279 280 protected void updateStage(Stage stage) { 281 final Stage previousStage = mUiStage; 282 mUiStage = stage; 283 updateUi(); 284 285 // If the stage changed, announce the header for accessibility. This 286 // is a no-op when accessibility is disabled. 287 if (previousStage != stage) { 288 mHeaderText.announceForAccessibility(mHeaderText.getText()); 289 } 290 } 291 292 /** 293 * Validates PIN and returns a message to display if PIN fails test. 294 * @param password the raw password the user typed in 295 * @return error message to show to user or null if password is OK 296 */ 297 private String validatePassword(String password) { 298 if (password.length() < mPasswordMinLength) { 299 return getString(mIsAlphaMode ? 300 R.string.lockpassword_password_too_short 301 : R.string.lockpassword_pin_too_short, mPasswordMinLength); 302 } 303 if (password.length() > mPasswordMaxLength) { 304 return getString(mIsAlphaMode ? 305 R.string.lockpassword_password_too_long 306 : R.string.lockpassword_pin_too_long, mPasswordMaxLength + 1); 307 } 308 int letters = 0; 309 int numbers = 0; 310 int lowercase = 0; 311 int symbols = 0; 312 int uppercase = 0; 313 int nonletter = 0; 314 for (int i = 0; i < password.length(); i++) { 315 char c = password.charAt(i); 316 // allow non control Latin-1 characters only 317 if (c < 32 || c > 127) { 318 return getString(R.string.lockpassword_illegal_character); 319 } 320 if (c >= '0' && c <= '9') { 321 numbers++; 322 nonletter++; 323 } else if (c >= 'A' && c <= 'Z') { 324 letters++; 325 uppercase++; 326 } else if (c >= 'a' && c <= 'z') { 327 letters++; 328 lowercase++; 329 } else { 330 symbols++; 331 nonletter++; 332 } 333 } 334 if (DevicePolicyManager.PASSWORD_QUALITY_NUMERIC == mRequestedQuality 335 || DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX == mRequestedQuality) { 336 if (letters > 0 || symbols > 0) { 337 // This shouldn't be possible unless user finds some way to bring up 338 // soft keyboard 339 return getString(R.string.lockpassword_pin_contains_non_digits); 340 } 341 // Check for repeated characters or sequences (e.g. '1234', '0000', '2468') 342 final int sequence = LockPatternUtils.maxLengthSequence(password); 343 if (DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX == mRequestedQuality 344 && sequence > LockPatternUtils.MAX_ALLOWED_SEQUENCE) { 345 return getString(R.string.lockpassword_pin_no_sequential_digits); 346 } 347 } else if (DevicePolicyManager.PASSWORD_QUALITY_COMPLEX == mRequestedQuality) { 348 if (letters < mPasswordMinLetters) { 349 return String.format(getResources().getQuantityString( 350 R.plurals.lockpassword_password_requires_letters, mPasswordMinLetters), 351 mPasswordMinLetters); 352 } else if (numbers < mPasswordMinNumeric) { 353 return String.format(getResources().getQuantityString( 354 R.plurals.lockpassword_password_requires_numeric, mPasswordMinNumeric), 355 mPasswordMinNumeric); 356 } else if (lowercase < mPasswordMinLowerCase) { 357 return String.format(getResources().getQuantityString( 358 R.plurals.lockpassword_password_requires_lowercase, mPasswordMinLowerCase), 359 mPasswordMinLowerCase); 360 } else if (uppercase < mPasswordMinUpperCase) { 361 return String.format(getResources().getQuantityString( 362 R.plurals.lockpassword_password_requires_uppercase, mPasswordMinUpperCase), 363 mPasswordMinUpperCase); 364 } else if (symbols < mPasswordMinSymbols) { 365 return String.format(getResources().getQuantityString( 366 R.plurals.lockpassword_password_requires_symbols, mPasswordMinSymbols), 367 mPasswordMinSymbols); 368 } else if (nonletter < mPasswordMinNonLetter) { 369 return String.format(getResources().getQuantityString( 370 R.plurals.lockpassword_password_requires_nonletter, mPasswordMinNonLetter), 371 mPasswordMinNonLetter); 372 } 373 } else { 374 final boolean alphabetic = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC 375 == mRequestedQuality; 376 final boolean alphanumeric = DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC 377 == mRequestedQuality; 378 if ((alphabetic || alphanumeric) && letters == 0) { 379 return getString(R.string.lockpassword_password_requires_alpha); 380 } 381 if (alphanumeric && numbers == 0) { 382 return getString(R.string.lockpassword_password_requires_digit); 383 } 384 } 385 if(mLockPatternUtils.checkPasswordHistory(password)) { 386 return getString(mIsAlphaMode ? R.string.lockpassword_password_recently_used 387 : R.string.lockpassword_pin_recently_used); 388 } 389 390 return null; 391 } 392 393 private void handleNext() { 394 if (mDone) return; 395 396 final String pin = mPasswordEntry.getText().toString(); 397 if (TextUtils.isEmpty(pin)) { 398 return; 399 } 400 String errorMsg = null; 401 if (mUiStage == Stage.Introduction) { 402 errorMsg = validatePassword(pin); 403 if (errorMsg == null) { 404 mFirstPin = pin; 405 mPasswordEntry.setText(""); 406 updateStage(Stage.NeedToConfirm); 407 } 408 } else if (mUiStage == Stage.NeedToConfirm) { 409 if (mFirstPin.equals(pin)) { 410 final boolean isFallback = getActivity().getIntent().getBooleanExtra( 411 LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, false); 412 mLockPatternUtils.clearLock(isFallback); 413 mLockPatternUtils.saveLockPassword(pin, mRequestedQuality, isFallback); 414 getActivity().setResult(RESULT_FINISHED); 415 getActivity().finish(); 416 mDone = true; 417 startActivity(RedactionInterstitial.createStartIntent(getActivity())); 418 } else { 419 CharSequence tmp = mPasswordEntry.getText(); 420 if (tmp != null) { 421 Selection.setSelection((Spannable) tmp, 0, tmp.length()); 422 } 423 updateStage(Stage.ConfirmWrong); 424 } 425 } 426 if (errorMsg != null) { 427 showError(errorMsg, mUiStage); 428 } 429 } 430 431 public void onClick(View v) { 432 switch (v.getId()) { 433 case R.id.next_button: 434 handleNext(); 435 break; 436 437 case R.id.cancel_button: 438 getActivity().finish(); 439 break; 440 } 441 } 442 443 private void showError(String msg, final Stage next) { 444 mHeaderText.setText(msg); 445 mHeaderText.announceForAccessibility(mHeaderText.getText()); 446 Message mesg = mHandler.obtainMessage(MSG_SHOW_ERROR, next); 447 mHandler.removeMessages(MSG_SHOW_ERROR); 448 mHandler.sendMessageDelayed(mesg, ERROR_MESSAGE_TIMEOUT); 449 } 450 451 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 452 // Check if this was the result of hitting the enter or "done" key 453 if (actionId == EditorInfo.IME_NULL 454 || actionId == EditorInfo.IME_ACTION_DONE 455 || actionId == EditorInfo.IME_ACTION_NEXT) { 456 handleNext(); 457 return true; 458 } 459 return false; 460 } 461 462 /** 463 * Update the hint based on current Stage and length of password entry 464 */ 465 private void updateUi() { 466 String password = mPasswordEntry.getText().toString(); 467 final int length = password.length(); 468 if (mUiStage == Stage.Introduction && length > 0) { 469 if (length < mPasswordMinLength) { 470 String msg = getString(mIsAlphaMode ? R.string.lockpassword_password_too_short 471 : R.string.lockpassword_pin_too_short, mPasswordMinLength); 472 mHeaderText.setText(msg); 473 mNextButton.setEnabled(false); 474 } else { 475 String error = validatePassword(password); 476 if (error != null) { 477 mHeaderText.setText(error); 478 mNextButton.setEnabled(false); 479 } else { 480 mHeaderText.setText(R.string.lockpassword_press_continue); 481 mNextButton.setEnabled(true); 482 } 483 } 484 } else { 485 mHeaderText.setText(mIsAlphaMode ? mUiStage.alphaHint : mUiStage.numericHint); 486 mNextButton.setEnabled(length > 0); 487 } 488 mNextButton.setText(mUiStage.buttonText); 489 } 490 491 public void afterTextChanged(Editable s) { 492 // Changing the text while error displayed resets to NeedToConfirm state 493 if (mUiStage == Stage.ConfirmWrong) { 494 mUiStage = Stage.NeedToConfirm; 495 } 496 updateUi(); 497 } 498 499 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 500 501 } 502 503 public void onTextChanged(CharSequence s, int start, int before, int count) { 504 505 } 506 } 507} 508