• 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.settings.fdn;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.preference.EditTextPreference;
22 import android.text.InputType;
23 import android.util.AttributeSet;
24 import android.view.View;
25 
26 /**
27  * Class similar to the com.android.settings.EditPinPreference
28  * class, with a couple of modifications, including a different layout
29  * for the dialog.
30  */
31 public class EditPinPreference extends EditTextPreference {
32 
33     private boolean shouldHideButtons;
34 
35     /**
36      * Interface definition for a callback to be invoked when the PIN is entered.
37      */
38     public interface OnPinEnteredListener {
39         /**
40          * Called when the dialog of {@link #EditPinPreference} is dismissed.
41          *
42          * @param preference the specified {@link #EditPinPreference}
43          * @param positiveResult Whether the positive button was clicked (true), or
44          *                       the negative button was clicked or the dialog was canceled (false).
45          */
onPinEntered(EditPinPreference preference, boolean positiveResult)46         void onPinEntered(EditPinPreference preference, boolean positiveResult);
47     }
48 
49     private OnPinEnteredListener mPinListener;
50 
setOnPinEnteredListener(OnPinEnteredListener listener)51     public void setOnPinEnteredListener(OnPinEnteredListener listener) {
52         mPinListener = listener;
53     }
54 
EditPinPreference(Context context, AttributeSet attrs)55     public EditPinPreference(Context context, AttributeSet attrs) {
56         super(context, attrs);
57     }
58 
EditPinPreference(Context context, AttributeSet attrs, int defStyle)59     public EditPinPreference(Context context, AttributeSet attrs, int defStyle) {
60         super(context, attrs, defStyle);
61     }
62 
63     @Override
onBindDialogView(View view)64     protected void onBindDialogView(View view) {
65         super.onBindDialogView(view);
66 
67         getEditText().setInputType(InputType.TYPE_CLASS_NUMBER
68                 | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
69         // If the layout does not contain an edittext, hide the buttons.
70         shouldHideButtons = (view.findViewById(android.R.id.edit) == null);
71     }
72 
73     @Override
onPrepareDialogBuilder(AlertDialog.Builder builder)74     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
75         super.onPrepareDialogBuilder(builder);
76 
77         // hide the buttons if we need to.
78         if (shouldHideButtons) {
79             builder.setPositiveButton(null, this);
80             builder.setNegativeButton(null, this);
81         }
82     }
83 
84     @Override
onDialogClosed(boolean positiveResult)85     protected void onDialogClosed(boolean positiveResult) {
86         super.onDialogClosed(positiveResult);
87         if (mPinListener != null) {
88             mPinListener.onPinEntered(this, positiveResult);
89         }
90     }
91 
92     /**
93      * Externally visible method to bring up the dialog to
94      * for multi-step / multi-dialog requests (like changing
95      * the SIM pin).
96      */
showPinDialog()97     public void showPinDialog() {
98         showDialog(null);
99     }
100 }
101