KeyguardSimPinView.java revision b896b9f74225d61af67c2661f44eceadb9e22013
1/*
2 * Copyright (C) 2012 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
17package com.android.keyguard;
18
19import com.android.internal.telephony.ITelephony;
20import com.android.internal.telephony.PhoneConstants;
21
22import android.content.Context;
23import android.content.DialogInterface;
24import android.app.Activity;
25import android.app.AlertDialog;
26import android.app.AlertDialog.Builder;
27import android.app.Dialog;
28import android.app.ProgressDialog;
29import android.os.Message;
30import android.os.RemoteException;
31import android.os.ServiceManager;
32import android.text.Editable;
33import android.text.InputType;
34import android.text.TextWatcher;
35import android.text.method.DigitsKeyListener;
36import android.util.AttributeSet;
37import android.view.View;
38import android.util.Log;
39import android.view.WindowManager;
40import android.widget.TextView.OnEditorActionListener;
41
42/**
43 * Displays a PIN pad for unlocking.
44 */
45public class KeyguardSimPinView extends KeyguardAbsKeyInputView
46        implements KeyguardSecurityView, OnEditorActionListener, TextWatcher {
47    private static final String LOG_TAG = "KeyguardSimPinView";
48    private static final boolean DEBUG = KeyguardViewMediator.DEBUG;
49
50    private ProgressDialog mSimUnlockProgressDialog = null;
51    private volatile boolean mSimCheckInProgress;
52
53    private AlertDialog mRemainingAttemptsDialog;
54
55    public KeyguardSimPinView(Context context) {
56        this(context, null);
57    }
58
59    public KeyguardSimPinView(Context context, AttributeSet attrs) {
60        super(context, attrs);
61    }
62
63    public void resetState() {
64        mSecurityMessageDisplay.setMessage(R.string.kg_sim_pin_instructions, true);
65        mPasswordEntry.setEnabled(true);
66    }
67
68    private String getPinPasswordErrorMessage(int attemptsRemaining) {
69        String displayMessage;
70
71        if (attemptsRemaining == 0) {
72            displayMessage = getContext().getString(R.string.kg_password_wrong_pin_code_pukked);
73        } else if (attemptsRemaining > 0) {
74            displayMessage = getContext().getResources()
75                    .getQuantityString(R.plurals.kg_password_wrong_pin_code, attemptsRemaining,
76                            attemptsRemaining);
77        } else {
78            displayMessage = getContext().getString(R.string.kg_password_pin_failed);
79        }
80        if (DEBUG) Log.d(LOG_TAG, "getPinPasswordErrorMessage:"
81                + " attemptsRemaining=" + attemptsRemaining + " displayMessage=" + displayMessage);
82        return displayMessage;
83    }
84
85    @Override
86    protected boolean shouldLockout(long deadline) {
87        // SIM PIN doesn't have a timed lockout
88        return false;
89    }
90
91    @Override
92    protected int getPasswordTextViewId() {
93        return R.id.pinEntry;
94    }
95
96    @Override
97    protected void onFinishInflate() {
98        super.onFinishInflate();
99
100        final View ok = findViewById(R.id.key_enter);
101        if (ok != null) {
102            ok.setOnClickListener(new View.OnClickListener() {
103                @Override
104                public void onClick(View v) {
105                    doHapticKeyClick();
106                    verifyPasswordAndUnlock();
107                }
108            });
109        }
110
111        // The delete button is of the PIN keyboard itself in some (e.g. tablet) layouts,
112        // not a separate view
113        View pinDelete = findViewById(R.id.delete_button);
114        if (pinDelete != null) {
115            pinDelete.setVisibility(View.VISIBLE);
116            pinDelete.setOnClickListener(new OnClickListener() {
117                public void onClick(View v) {
118                    CharSequence str = mPasswordEntry.getText();
119                    if (str.length() > 0) {
120                        mPasswordEntry.setText(str.subSequence(0, str.length()-1));
121                    }
122                    doHapticKeyClick();
123                }
124            });
125            pinDelete.setOnLongClickListener(new View.OnLongClickListener() {
126                public boolean onLongClick(View v) {
127                    mPasswordEntry.setText("");
128                    doHapticKeyClick();
129                    return true;
130                }
131            });
132        }
133
134        mPasswordEntry.setKeyListener(DigitsKeyListener.getInstance());
135        mPasswordEntry.setInputType(InputType.TYPE_CLASS_NUMBER
136                | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
137
138        mPasswordEntry.requestFocus();
139
140        mSecurityMessageDisplay.setTimeout(0); // don't show ownerinfo/charging status by default
141    }
142
143    @Override
144    public void showUsabilityHint() {
145    }
146
147    @Override
148    public void onPause() {
149        // dismiss the dialog.
150        if (mSimUnlockProgressDialog != null) {
151            mSimUnlockProgressDialog.dismiss();
152            mSimUnlockProgressDialog = null;
153        }
154    }
155
156    /**
157     * Since the IPC can block, we want to run the request in a separate thread
158     * with a callback.
159     */
160    private abstract class CheckSimPin extends Thread {
161        private final String mPin;
162
163        protected CheckSimPin(String pin) {
164            mPin = pin;
165        }
166
167        abstract void onSimCheckResponse(final int result, final int attemptsRemaining);
168
169        @Override
170        public void run() {
171            try {
172                final int[] result = ITelephony.Stub.asInterface(ServiceManager
173                        .checkService("phone")).supplyPinReportResult(mPin);
174                post(new Runnable() {
175                    public void run() {
176                        onSimCheckResponse(result[0], result[1]);
177                    }
178                });
179            } catch (RemoteException e) {
180                post(new Runnable() {
181                    public void run() {
182                        onSimCheckResponse(PhoneConstants.PIN_GENERAL_FAILURE, -1);
183                    }
184                });
185            }
186        }
187    }
188
189    private Dialog getSimUnlockProgressDialog() {
190        if (mSimUnlockProgressDialog == null) {
191            mSimUnlockProgressDialog = new ProgressDialog(mContext);
192            mSimUnlockProgressDialog.setMessage(
193                    mContext.getString(R.string.kg_sim_unlock_progress_dialog_message));
194            mSimUnlockProgressDialog.setIndeterminate(true);
195            mSimUnlockProgressDialog.setCancelable(false);
196            mSimUnlockProgressDialog.getWindow().setType(
197                    WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
198        }
199        return mSimUnlockProgressDialog;
200    }
201
202    private Dialog getSimRemainingAttemptsDialog(int remaining) {
203        String msg = getPinPasswordErrorMessage(remaining);
204        if (mRemainingAttemptsDialog == null) {
205            Builder builder = new AlertDialog.Builder(mContext);
206            builder.setMessage(msg);
207            builder.setCancelable(false);
208            builder.setNeutralButton(R.string.ok, null);
209            mRemainingAttemptsDialog = builder.create();
210            mRemainingAttemptsDialog.getWindow().setType(
211                    WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
212        } else {
213            mRemainingAttemptsDialog.setMessage(msg);
214        }
215        return mRemainingAttemptsDialog;
216    }
217
218    @Override
219    protected void verifyPasswordAndUnlock() {
220        String entry = mPasswordEntry.getText().toString();
221
222        if (entry.length() < 4) {
223            // otherwise, display a message to the user, and don't submit.
224            mSecurityMessageDisplay.setMessage(R.string.kg_invalid_sim_pin_hint, true);
225            mPasswordEntry.setText("");
226            mCallback.userActivity(0);
227            return;
228        }
229
230        getSimUnlockProgressDialog().show();
231
232        if (!mSimCheckInProgress) {
233            mSimCheckInProgress = true; // there should be only one
234            new CheckSimPin(mPasswordEntry.getText().toString()) {
235                void onSimCheckResponse(final int result, final int attemptsRemaining) {
236                    post(new Runnable() {
237                        public void run() {
238                            if (mSimUnlockProgressDialog != null) {
239                                mSimUnlockProgressDialog.hide();
240                            }
241                            if (result == PhoneConstants.PIN_RESULT_SUCCESS) {
242                                KeyguardUpdateMonitor.getInstance(getContext()).reportSimUnlocked();
243                                mCallback.dismiss(true);
244                            } else {
245                                if (result == PhoneConstants.PIN_PASSWORD_INCORRECT) {
246                                    if (attemptsRemaining <= 2) {
247                                        // this is getting critical - show dialog
248                                        getSimRemainingAttemptsDialog(attemptsRemaining).show();
249                                    } else {
250                                        // show message
251                                        mSecurityMessageDisplay.setMessage(
252                                                getPinPasswordErrorMessage(attemptsRemaining), true);
253                                    }
254                                } else {
255                                    // "PIN operation failed!" - no idea what this was and no way to
256                                    // find out. :/
257                                    mSecurityMessageDisplay.setMessage(getContext().getString(
258                                            R.string.kg_password_pin_failed), true);
259                                }
260                                if (DEBUG) Log.d(LOG_TAG, "verifyPasswordAndUnlock "
261                                        + " CheckSimPin.onSimCheckResponse: " + result
262                                        + " attemptsRemaining=" + attemptsRemaining);
263                                mPasswordEntry.setText("");
264                            }
265                            mCallback.userActivity(0);
266                            mSimCheckInProgress = false;
267                        }
268                    });
269                }
270            }.start();
271        }
272    }
273}
274
275