| Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 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.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 java.util.Map; | 
|  | 30 |  | 
|  | 31 | /** | 
|  | 32 | * Class similar to the com.android.settings.EditPinPreference | 
|  | 33 | * class, with a couple of modifications, including a different layout | 
|  | 34 | * for the dialog. | 
|  | 35 | */ | 
|  | 36 | public class EditPinPreference extends EditTextPreference { | 
|  | 37 |  | 
|  | 38 | private boolean shouldHideButtons; | 
|  | 39 |  | 
|  | 40 | interface OnPinEnteredListener { | 
|  | 41 | void onPinEntered(EditPinPreference preference, boolean positiveResult); | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | private OnPinEnteredListener mPinListener; | 
|  | 45 |  | 
|  | 46 | public void setOnPinEnteredListener(OnPinEnteredListener listener) { | 
|  | 47 | mPinListener = listener; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | public EditPinPreference(Context context, AttributeSet attrs) { | 
|  | 51 | super(context, attrs); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | public EditPinPreference(Context context, AttributeSet attrs, int defStyle) { | 
|  | 55 | super(context, attrs, defStyle); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | /** | 
|  | 59 | * Overridden to setup the correct dialog layout, as well as setting up | 
|  | 60 | * other properties for the pin / puk entry field. | 
|  | 61 | */ | 
|  | 62 | @Override | 
|  | 63 | protected View onCreateDialogView() { | 
|  | 64 | // set the dialog layout | 
|  | 65 | setDialogLayoutResource(R.layout.pref_dialog_editpin); | 
|  | 66 |  | 
|  | 67 | View dialog = super.onCreateDialogView(); | 
|  | 68 |  | 
|  | 69 | getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | | 
|  | 70 | InputType.TYPE_NUMBER_VARIATION_PASSWORD); | 
|  | 71 |  | 
|  | 72 | return dialog; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | @Override | 
|  | 76 | protected void onBindDialogView(View view) { | 
|  | 77 | super.onBindDialogView(view); | 
|  | 78 |  | 
|  | 79 | // If the layout does not contain an edittext, hide the buttons. | 
|  | 80 | shouldHideButtons = (view.findViewById(android.R.id.edit) == null); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | @Override | 
|  | 84 | protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { | 
|  | 85 | super.onPrepareDialogBuilder(builder); | 
|  | 86 |  | 
|  | 87 | // hide the buttons if we need to. | 
|  | 88 | if (shouldHideButtons) { | 
|  | 89 | builder.setPositiveButton(null, this); | 
|  | 90 | builder.setNegativeButton(null, this); | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | @Override | 
|  | 95 | protected void onDialogClosed(boolean positiveResult) { | 
|  | 96 | super.onDialogClosed(positiveResult); | 
|  | 97 | if (mPinListener != null) { | 
|  | 98 | mPinListener.onPinEntered(this, positiveResult); | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | /** | 
|  | 103 | * Externally visible method to bring up the dialog to | 
|  | 104 | * for multi-step / multi-dialog requests (like changing | 
|  | 105 | * the SIM pin). | 
|  | 106 | */ | 
|  | 107 | public void showPinDialog() { | 
|  | 108 | showDialog(null); | 
|  | 109 | } | 
|  | 110 | } |