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     /**
     43      * Interface definition for a callback to be invoked when the PIN is entered.
     44      */
     45     public interface OnPinEnteredListener {
     46         /**
     47          * Called when the dialog of {@link #EditPinPreference} is dismissed.
     48          *
     49          * @param preference the specified {@link #EditPinPreference}
     50          * @param positiveResult Whether the positive button was clicked (true), or
     51          *                       the negative button was clicked or the dialog was canceled (false).
     52          */
     53         void onPinEntered(EditPinPreference preference, boolean positiveResult);
     54     }
     55 
     56     private OnPinEnteredListener mPinListener;
     57 
     58     public void setOnPinEnteredListener(OnPinEnteredListener listener) {
     59         mPinListener = listener;
     60     }
     61 
     62     public EditPinPreference(Context context, AttributeSet attrs) {
     63         super(context, attrs);
     64     }
     65 
     66     public EditPinPreference(Context context, AttributeSet attrs, int defStyle) {
     67         super(context, attrs, defStyle);
     68     }
     69 
     70     /**
     71      * Overridden to setup the correct dialog layout, as well as setting up
     72      * other properties for the pin / puk entry field.
     73      */
     74     @Override
     75     protected View onCreateDialogView() {
     76         // set the dialog layout
     77         setDialogLayoutResource(R.layout.pref_dialog_editpin);
     78 
     79         View dialog = super.onCreateDialogView();
     80 
     81         getEditText().setInputType(InputType.TYPE_CLASS_NUMBER |
     82             InputType.TYPE_NUMBER_VARIATION_PASSWORD);
     83 
     84         return dialog;
     85     }
     86 
     87     @Override
     88     protected void onBindDialogView(View view) {
     89         super.onBindDialogView(view);
     90 
     91         // If the layout does not contain an edittext, hide the buttons.
     92         shouldHideButtons = (view.findViewById(android.R.id.edit) == null);
     93     }
     94 
     95     @Override
     96     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
     97         super.onPrepareDialogBuilder(builder);
     98 
     99         // hide the buttons if we need to.
    100         if (shouldHideButtons) {
    101             builder.setPositiveButton(null, this);
    102             builder.setNegativeButton(null, this);
    103         }
    104     }
    105 
    106     @Override
    107     protected void onDialogClosed(boolean positiveResult) {
    108         super.onDialogClosed(positiveResult);
    109         if (mPinListener != null) {
    110             mPinListener.onPinEntered(this, positiveResult);
    111         }
    112     }
    113 
    114     /**
    115      * Externally visible method to bring up the dialog to
    116      * for multi-step / multi-dialog requests (like changing
    117      * the SIM pin).
    118      */
    119     public void showPinDialog() {
    120         showDialog(null);
    121     }
    122 }
    123