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