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