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