• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.phone;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.preference.EditTextPreference;
22 import android.text.method.DigitsKeyListener;
23 import android.text.method.PasswordTransformationMethod;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.widget.EditText;
27 
28 import java.util.Map;
29 
30 /**
31  * Class similar to the com.android.settings.EditPinPreference
32  * class, with a couple of modifications, including a different layout
33  * for the dialog.
34  */
35 public class EditPinPreference extends EditTextPreference {
36 
37     private boolean shouldHideButtons;
38 
39     interface OnPinEnteredListener {
onPinEntered(EditPinPreference preference, boolean positiveResult)40         void onPinEntered(EditPinPreference preference, boolean positiveResult);
41     }
42 
43     private OnPinEnteredListener mPinListener;
44 
setOnPinEnteredListener(OnPinEnteredListener listener)45     public void setOnPinEnteredListener(OnPinEnteredListener listener) {
46         mPinListener = listener;
47     }
48 
EditPinPreference(Context context, AttributeSet attrs)49     public EditPinPreference(Context context, AttributeSet attrs) {
50         super(context, attrs);
51     }
52 
EditPinPreference(Context context, AttributeSet attrs, int defStyle)53     public EditPinPreference(Context context, AttributeSet attrs, int defStyle) {
54         super(context, attrs, defStyle);
55     }
56 
57     /**
58      * Overridden to setup the correct dialog layout, as well as setting up
59      * other properties for the pin / puk entry field.
60      */
61     @Override
onCreateDialogView()62     protected View onCreateDialogView() {
63         // set the dialog layout
64         setDialogLayoutResource(R.layout.pref_dialog_editpin);
65 
66         View dialog = super.onCreateDialogView();
67 
68         // set the transformation method and the key listener to ensure
69         // correct input and presentation of the pin / puk.
70         final EditText textfield = getEditText();
71         textfield.setTransformationMethod(PasswordTransformationMethod.getInstance());
72         textfield.setKeyListener(DigitsKeyListener.getInstance());
73 
74         return dialog;
75     }
76 
77     @Override
onBindDialogView(View view)78     protected void onBindDialogView(View view) {
79         super.onBindDialogView(view);
80 
81         // If the layout does not contain an edittext, hide the buttons.
82         shouldHideButtons = (view.findViewById(android.R.id.edit) == null);
83     }
84 
85     @Override
onPrepareDialogBuilder(AlertDialog.Builder builder)86     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
87         super.onPrepareDialogBuilder(builder);
88 
89         // hide the buttons if we need to.
90         if (shouldHideButtons) {
91             builder.setPositiveButton(null, this);
92             builder.setNegativeButton(null, this);
93         }
94     }
95 
96     @Override
onDialogClosed(boolean positiveResult)97     protected void onDialogClosed(boolean positiveResult) {
98         super.onDialogClosed(positiveResult);
99         if (mPinListener != null) {
100             mPinListener.onPinEntered(this, positiveResult);
101         }
102     }
103 
104     /**
105      * Externally visible method to bring up the dialog to
106      * for multi-step / multi-dialog requests (like changing
107      * the SIM pin).
108      */
showPinDialog()109     public void showPinDialog() {
110         showDialog(null);
111     }
112 }
113