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