ConfirmLockPassword.java revision 74b1e0a7330af195621ad0965748bee66c46f4b6
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.os.Bundle; 28import android.os.Handler; 29import android.preference.PreferenceActivity; 30import android.text.InputType; 31import android.view.KeyEvent; 32import android.view.LayoutInflater; 33import android.view.View; 34import android.view.View.OnClickListener; 35import android.view.ViewGroup; 36import android.view.inputmethod.EditorInfo; 37import android.widget.TextView; 38import android.widget.TextView.OnEditorActionListener; 39 40public class ConfirmLockPassword extends PreferenceActivity { 41 42 @Override 43 public Intent getIntent() { 44 Intent modIntent = new Intent(super.getIntent()); 45 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, ConfirmLockPasswordFragment.class.getName()); 46 modIntent.putExtra(EXTRA_NO_HEADERS, true); 47 return modIntent; 48 } 49 50 @Override 51 public void onCreate(Bundle savedInstanceState) { 52 // Disable IME on our window since we provide our own keyboard 53 //getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, 54 //WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 55 super.onCreate(savedInstanceState); 56 CharSequence msg = getText(R.string.lockpassword_confirm_your_password_header); 57 showBreadCrumbs(msg, msg); 58 } 59 60 public static class ConfirmLockPasswordFragment extends Fragment implements OnClickListener, 61 OnEditorActionListener { 62 private static final long ERROR_MESSAGE_TIMEOUT = 3000; 63 private TextView mPasswordEntry; 64 private LockPatternUtils mLockPatternUtils; 65 private TextView mHeaderText; 66 private Handler mHandler = new Handler(); 67 private PasswordEntryKeyboardHelper mKeyboardHelper; 68 private PasswordEntryKeyboardView mKeyboardView; 69 70 71 // required constructor for fragments 72 public ConfirmLockPasswordFragment() { 73 74 } 75 76 @Override 77 public void onCreate(Bundle savedInstanceState) { 78 super.onCreate(savedInstanceState); 79 mLockPatternUtils = new LockPatternUtils(getActivity()); 80 } 81 82 @Override 83 public View onCreateView(LayoutInflater inflater, ViewGroup container, 84 Bundle savedInstanceState) { 85 final int storedQuality = mLockPatternUtils.getKeyguardStoredPasswordQuality(); 86 View view = inflater.inflate(R.layout.confirm_lock_password, null); 87 // Disable IME on our window since we provide our own keyboard 88 89 view.findViewById(R.id.cancel_button).setOnClickListener(this); 90 view.findViewById(R.id.next_button).setOnClickListener(this); 91 mPasswordEntry = (TextView) view.findViewById(R.id.password_entry); 92 mPasswordEntry.setOnEditorActionListener(this); 93 mKeyboardView = (PasswordEntryKeyboardView) view.findViewById(R.id.keyboard); 94 mHeaderText = (TextView) view.findViewById(R.id.headerText); 95 final boolean isAlpha = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC == storedQuality 96 || DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC == storedQuality 97 || DevicePolicyManager.PASSWORD_QUALITY_COMPLEX == storedQuality; 98 mHeaderText.setText(isAlpha ? R.string.lockpassword_confirm_your_password_header 99 : R.string.lockpassword_confirm_your_pin_header); 100 101 final Activity activity = getActivity(); 102 mKeyboardHelper = new PasswordEntryKeyboardHelper(activity, 103 mKeyboardView, mPasswordEntry); 104 mKeyboardHelper.setKeyboardMode(isAlpha ? 105 PasswordEntryKeyboardHelper.KEYBOARD_MODE_ALPHA 106 : PasswordEntryKeyboardHelper.KEYBOARD_MODE_NUMERIC); 107 mKeyboardView.requestFocus(); 108 109 int currentType = mPasswordEntry.getInputType(); 110 mPasswordEntry.setInputType(isAlpha ? currentType 111 : (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD)); 112 113 // Update the breadcrumb (title) if this is embedded in a PreferenceActivity 114 if (activity instanceof PreferenceActivity) { 115 final PreferenceActivity preferenceActivity = (PreferenceActivity) activity; 116 int id = isAlpha ? R.string.lockpassword_confirm_your_password_header 117 : R.string.lockpassword_confirm_your_pin_header; 118 CharSequence title = getText(id); 119 preferenceActivity.showBreadCrumbs(title, title); 120 } 121 122 return view; 123 } 124 125 @Override 126 public void onPause() { 127 super.onPause(); 128 mKeyboardView.requestFocus(); 129 } 130 131 @Override 132 public void onResume() { 133 // TODO Auto-generated method stub 134 super.onResume(); 135 mKeyboardView.requestFocus(); 136 } 137 138 private void handleNext() { 139 final String pin = mPasswordEntry.getText().toString(); 140 if (mLockPatternUtils.checkPassword(pin)) { 141 142 Intent intent = new Intent(); 143 intent.putExtra("password", pin); 144 145 getActivity().setResult(RESULT_OK, intent); 146 getActivity().finish(); 147 } else { 148 showError(R.string.lockpattern_need_to_unlock_wrong); 149 } 150 } 151 152 public void onClick(View v) { 153 switch (v.getId()) { 154 case R.id.next_button: 155 handleNext(); 156 break; 157 158 case R.id.cancel_button: 159 getActivity().setResult(RESULT_CANCELED); 160 getActivity().finish(); 161 break; 162 } 163 } 164 165 private void showError(int msg) { 166 mHeaderText.setText(msg); 167 mPasswordEntry.setText(null); 168 mHandler.postDelayed(new Runnable() { 169 public void run() { 170 mHeaderText.setText(R.string.lockpassword_confirm_your_password_header); 171 } 172 }, ERROR_MESSAGE_TIMEOUT); 173 } 174 175 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 176 // Check if this was the result of hitting the enter or "done" key 177 if (actionId == EditorInfo.IME_NULL 178 || actionId == EditorInfo.IME_ACTION_DONE 179 || actionId == EditorInfo.IME_ACTION_NEXT) { 180 handleNext(); 181 return true; 182 } 183 return false; 184 } 185 } 186} 187