Home | History | Annotate | Download | only in fdn
      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.text.method.DigitsKeyListener;
     24 import android.text.method.PasswordTransformationMethod;
     25 import android.util.AttributeSet;
     26 import android.view.View;
     27 import android.widget.EditText;
     28 
     29 import com.android.phone.R;
     30 
     31 import java.util.Map;
     32 
     33 /**
     34  * Class similar to the com.android.settings.EditPinPreference
     35  * class, with a couple of modifications, including a different layout
     36  * for the dialog.
     37  */
     38 public class EditPinPreference extends EditTextPreference {
     39 
     40     private boolean shouldHideButtons;
     41 
     42     interface OnPinEnteredListener {
     43         void onPinEntered(EditPinPreference preference, boolean positiveResult);
     44     }
     45 
     46     private OnPinEnteredListener mPinListener;
     47 
     48     public void setOnPinEnteredListener(OnPinEnteredListener listener) {
     49         mPinListener = listener;
     50     }
     51 
     52     public EditPinPreference(Context context, AttributeSet attrs) {
     53         super(context, attrs);
     54     }
     55 
     56     public EditPinPreference(Context context, AttributeSet attrs, int defStyle) {
     57         super(context, attrs, defStyle);
     58     }
     59 
     60     /**
     61      * Overridden to setup the correct dialog layout, as well as setting up
     62      * other properties for the pin / puk entry field.
     63      */
     64     @Override
     65     protected View onCreateDialogView() {
     66         // set the dialog layout
     67         setDialogLayoutResource(R.layout.pref_dialog_editpin);
     68 
     69         View dialog = super.onCreateDialogView();
     70 
     71         getEditText().setInputType(InputType.TYPE_CLASS_NUMBER |
     72             InputType.TYPE_NUMBER_VARIATION_PASSWORD);
     73 
     74         return dialog;
     75     }
     76 
     77     @Override
     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
     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
     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      */
    109     public void showPinDialog() {
    110         showDialog(null);
    111     }
    112 }
    113