ConfirmLockPassword.java revision 2f9dbcb49174d85218dd18e3b2097d3b9fc789c7
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.os.Bundle;
25import android.os.Handler;
26import android.text.Editable;
27import android.view.View;
28import android.view.WindowManager;
29import android.view.View.OnClickListener;
30import android.widget.Button;
31import android.widget.TextView;
32
33public class ConfirmLockPassword extends Activity implements OnClickListener {
34    private static final long ERROR_MESSAGE_TIMEOUT = 3000;
35    private TextView mPasswordEntry;
36    private LockPatternUtils mLockPatternUtils;
37    private TextView mHeaderText;
38    private Handler mHandler = new Handler();
39    private PasswordEntryKeyboardHelper mKeyboardHelper;
40    private PasswordEntryKeyboardView mKeyboardView;
41
42    @Override
43    protected void onCreate(Bundle savedInstanceState) {
44        super.onCreate(savedInstanceState);
45        mLockPatternUtils = new LockPatternUtils(this);
46        initViews();
47    }
48
49    private void initViews() {
50        int mode = mLockPatternUtils.getPasswordMode();
51        setContentView(R.layout.confirm_lock_password);
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
56        findViewById(R.id.cancel_button).setOnClickListener(this);
57        findViewById(R.id.next_button).setOnClickListener(this);
58        mPasswordEntry = (TextView) findViewById(R.id.password_entry);
59        mKeyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard);
60        mHeaderText = (TextView) findViewById(R.id.headerText);
61        mHeaderText.setText(R.string.lockpassword_confirm_your_password_header);
62        final boolean isAlpha =
63                LockPatternUtils.MODE_PASSWORD == mLockPatternUtils.getPasswordMode();
64        mKeyboardHelper = new PasswordEntryKeyboardHelper(this, mKeyboardView, mPasswordEntry);
65        mKeyboardHelper.setKeyboardMode(isAlpha ? PasswordEntryKeyboardHelper.KEYBOARD_MODE_ALPHA
66                : PasswordEntryKeyboardHelper.KEYBOARD_MODE_NUMERIC);
67        mKeyboardView.requestFocus();
68    }
69
70    @Override
71    protected void onPause() {
72        super.onPause();
73        mKeyboardView.requestFocus();
74    }
75
76    @Override
77    protected void onResume() {
78        // TODO Auto-generated method stub
79        super.onResume();
80        mKeyboardView.requestFocus();
81    }
82
83    public void onClick(View v) {
84        switch (v.getId()) {
85            case R.id.next_button:
86                {
87                    final String pin = mPasswordEntry.getText().toString();
88                    if (mLockPatternUtils.checkPassword(pin)) {
89                        setResult(RESULT_OK);
90                        finish();
91                    } else {
92                        showError(R.string.lockpattern_need_to_unlock_wrong);
93                    }
94                }
95                break;
96
97            case R.id.cancel_button:
98                setResult(RESULT_CANCELED);
99                finish();
100                break;
101        }
102    }
103
104    private void showError(int msg) {
105        mHeaderText.setText(msg);
106        mPasswordEntry.setText(null);
107        mHandler.postDelayed(new Runnable() {
108            public void run() {
109                mHeaderText.setText(R.string.lockpassword_confirm_your_password_header);
110            }
111        }, ERROR_MESSAGE_TIMEOUT);
112    }
113}
114