1/*
2 * Copyright (C) 2008 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 android.app.Dialog;
20import android.content.Context;
21import android.preference.EditTextPreference;
22import android.text.InputType;
23import android.util.AttributeSet;
24import android.view.View;
25import android.widget.EditText;
26
27/**
28 * TODO: Add a soft dialpad for PIN entry.
29 */
30class EditPinPreference extends EditTextPreference {
31
32    interface OnPinEnteredListener {
33        void onPinEntered(EditPinPreference preference, boolean positiveResult);
34    }
35
36    private OnPinEnteredListener mPinListener;
37
38    public EditPinPreference(Context context, AttributeSet attrs) {
39        super(context, attrs);
40    }
41
42    public EditPinPreference(Context context, AttributeSet attrs, int defStyle) {
43        super(context, attrs, defStyle);
44    }
45
46    public void setOnPinEnteredListener(OnPinEnteredListener listener) {
47        mPinListener = listener;
48    }
49
50    @Override
51    protected void onBindDialogView(View view) {
52        super.onBindDialogView(view);
53
54        final EditText editText = getEditText();
55
56        if (editText != null) {
57            editText.setInputType(InputType.TYPE_CLASS_NUMBER |
58                InputType.TYPE_NUMBER_VARIATION_PASSWORD);
59        }
60    }
61
62    public boolean isDialogOpen() {
63        Dialog dialog = getDialog();
64        return dialog != null && dialog.isShowing();
65    }
66
67    @Override
68    protected void onDialogClosed(boolean positiveResult) {
69        super.onDialogClosed(positiveResult);
70        if (mPinListener != null) {
71            mPinListener.onPinEntered(this, positiveResult);
72        }
73    }
74
75    public void showPinDialog() {
76        Dialog dialog = getDialog();
77        if (dialog == null || !dialog.isShowing()) {
78            showDialog(null);
79        }
80    }
81}
82