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