KeyguardSimPukView.java revision 838906b165e4d3cb2c512b2db344aa50cb5d4751
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 */
16package com.android.internal.policy.impl.keyguard;
17
18import android.app.Activity;
19import android.app.Dialog;
20import android.app.ProgressDialog;
21import android.content.Context;
22import android.graphics.Rect;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25import android.text.Editable;
26import android.text.TextWatcher;
27import android.util.AttributeSet;
28import android.view.KeyEvent;
29import android.view.MotionEvent;
30import android.view.View;
31import android.view.WindowManager;
32import android.view.inputmethod.EditorInfo;
33import android.widget.LinearLayout;
34import android.widget.TextView;
35import android.widget.TextView.OnEditorActionListener;
36
37import com.android.internal.telephony.ITelephony;
38import com.android.internal.widget.LockPatternUtils;
39import com.android.internal.widget.PasswordEntryKeyboardHelper;
40import com.android.internal.widget.PasswordEntryKeyboardView;
41import com.android.internal.R;
42
43public class KeyguardSimPukView extends LinearLayout implements View.OnClickListener,
44    KeyguardSecurityView, OnEditorActionListener, TextWatcher {
45
46    private View mDeleteButton;
47
48    private ProgressDialog mSimUnlockProgressDialog = null;
49    private KeyguardSecurityCallback mCallback;
50
51    private SecurityMessageDisplay mSecurityMessageDisplay;
52
53    private PasswordEntryKeyboardView mKeyboardView;
54
55    private PasswordEntryKeyboardHelper mKeyboardHelper;
56
57    private LockPatternUtils mLockPatternUtils;
58
59    private volatile boolean mCheckInProgress;
60
61    private TextView mSimPinEntry;
62
63    private String mPukText;
64
65    private String mPinText;
66    private StateMachine mStateMachine = new StateMachine();
67
68    private class StateMachine {
69        final int ENTER_PUK = 0;
70        final int ENTER_PIN = 1;
71        final int CONFIRM_PIN = 2;
72        final int DONE = 3;
73        private int state = ENTER_PUK;
74
75        public void next() {
76            int msg = 0;
77            if (state == ENTER_PUK) {
78                if (checkPuk()) {
79                    state = ENTER_PIN;
80                    msg = R.string.kg_puk_enter_pin_hint;
81                } else {
82                    msg = R.string.kg_invalid_sim_puk_hint;
83                }
84            } else if (state == ENTER_PIN) {
85                if (checkPin()) {
86                    state = CONFIRM_PIN;
87                    msg = R.string.kg_enter_confirm_pin_hint;
88                } else {
89                    msg = R.string.kg_invalid_sim_pin_hint;
90                }
91            } else if (state == CONFIRM_PIN) {
92                if (confirmPin()) {
93                    state = DONE;
94                    msg =
95                        com.android.internal.R.string.lockscreen_sim_unlock_progress_dialog_message;
96                    updateSim();
97                } else {
98                    msg = R.string.kg_invalid_confirm_pin_hint;
99                }
100            }
101            mSimPinEntry.setText(null);
102            if (msg != 0) {
103                mSecurityMessageDisplay.setMessage(msg, true);
104            }
105        }
106
107        void reset() {
108            mPinText="";
109            mPukText="";
110            state = ENTER_PUK;
111            mSecurityMessageDisplay.setMessage(R.string.kg_puk_enter_puk_hint, true);
112            mSimPinEntry.requestFocus();
113        }
114    }
115
116    public KeyguardSimPukView(Context context) {
117        this(context, null);
118    }
119
120    public KeyguardSimPukView(Context context, AttributeSet attrs) {
121        super(context, attrs);
122        mLockPatternUtils = new LockPatternUtils(getContext());
123    }
124
125    public void setKeyguardCallback(KeyguardSecurityCallback callback) {
126        mCallback = callback;
127        mLockPatternUtils = new LockPatternUtils(getContext());
128    }
129
130    @Override
131    protected void onFinishInflate() {
132        super.onFinishInflate();
133
134        // We always set a dummy NavigationManager to avoid null checks
135        mSecurityMessageDisplay = new KeyguardNavigationManager(null);
136
137        mSimPinEntry = (TextView) findViewById(R.id.sim_pin_entry);
138        mSimPinEntry.setOnEditorActionListener(this);
139        mSimPinEntry.addTextChangedListener(this);
140        mDeleteButton = findViewById(R.id.delete_button);
141        mDeleteButton.setOnClickListener(this);
142        mKeyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard);
143        mKeyboardHelper = new PasswordEntryKeyboardHelper(mContext, mKeyboardView, this, false,
144                new int[] {
145                R.xml.kg_password_kbd_numeric,
146                com.android.internal.R.xml.password_kbd_qwerty,
147                com.android.internal.R.xml.password_kbd_qwerty_shifted,
148                com.android.internal.R.xml.password_kbd_symbols,
149                com.android.internal.R.xml.password_kbd_symbols_shift
150                });
151        mKeyboardHelper.setKeyboardMode(PasswordEntryKeyboardHelper.KEYBOARD_MODE_NUMERIC);
152        mKeyboardHelper.setEnableHaptics(mLockPatternUtils.isTactileFeedbackEnabled());
153        reset();
154    }
155
156    @Override
157    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
158        return mSimPinEntry.requestFocus(direction, previouslyFocusedRect);
159    }
160
161    public boolean needsInput() {
162        return false; // This view provides its own keypad
163    }
164
165    public void onPause() {
166
167    }
168
169    public void onResume() {
170        reset();
171    }
172
173    /** {@inheritDoc} */
174    public void cleanUp() {
175        // dismiss the dialog.
176        if (mSimUnlockProgressDialog != null) {
177            mSimUnlockProgressDialog.dismiss();
178            mSimUnlockProgressDialog = null;
179        }
180    }
181
182    /**
183     * Since the IPC can block, we want to run the request in a separate thread
184     * with a callback.
185     */
186    private abstract class CheckSimPuk extends Thread {
187
188        private final String mPin, mPuk;
189
190        protected CheckSimPuk(String puk, String pin) {
191            mPuk = puk;
192            mPin = pin;
193        }
194
195        abstract void onSimLockChangedResponse(boolean success);
196
197        @Override
198        public void run() {
199            try {
200                final boolean result = ITelephony.Stub.asInterface(ServiceManager
201                        .checkService("phone")).supplyPuk(mPuk, mPin);
202
203                post(new Runnable() {
204                    public void run() {
205                        onSimLockChangedResponse(result);
206                    }
207                });
208            } catch (RemoteException e) {
209                post(new Runnable() {
210                    public void run() {
211                        onSimLockChangedResponse(false);
212                    }
213                });
214            }
215        }
216    }
217
218    public void onClick(View v) {
219        if (v == mDeleteButton) {
220            mSimPinEntry.requestFocus();
221            final Editable digits = mSimPinEntry.getEditableText();
222            final int len = digits.length();
223            if (len > 0) {
224                digits.delete(len-1, len);
225            }
226        }
227        mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS);
228    }
229
230    private Dialog getSimUnlockProgressDialog() {
231        if (mSimUnlockProgressDialog == null) {
232            mSimUnlockProgressDialog = new ProgressDialog(mContext);
233            mSimUnlockProgressDialog.setMessage(mContext.getString(
234                    R.string.kg_sim_unlock_progress_dialog_message));
235            mSimUnlockProgressDialog.setIndeterminate(true);
236            mSimUnlockProgressDialog.setCancelable(false);
237            if (!(mContext instanceof Activity)) {
238                mSimUnlockProgressDialog.getWindow().setType(
239                        WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
240            }
241        }
242        return mSimUnlockProgressDialog;
243    }
244
245    private boolean checkPuk() {
246        // make sure the puk is at least 8 digits long.
247        if (mSimPinEntry.getText().length() >= 8) {
248            mPukText = mSimPinEntry.getText().toString();
249            return true;
250        }
251        return false;
252    }
253
254    private boolean checkPin() {
255        // make sure the PIN is between 4 and 8 digits
256        int length = mSimPinEntry.getText().length();
257        if (length >= 4 && length <= 8) {
258            mPinText = mSimPinEntry.getText().toString();
259            return true;
260        }
261        return false;
262    }
263
264    public boolean confirmPin() {
265        return mPinText.equals(mSimPinEntry.getText().toString());
266    }
267
268    private void updateSim() {
269        getSimUnlockProgressDialog().show();
270
271        if (!mCheckInProgress) {
272            mCheckInProgress = true;
273            new CheckSimPuk(mPukText, mPinText) {
274                void onSimLockChangedResponse(final boolean success) {
275                    post(new Runnable() {
276                        public void run() {
277                            if (mSimUnlockProgressDialog != null) {
278                                mSimUnlockProgressDialog.hide();
279                            }
280                            if (success) {
281                                mCallback.dismiss(true);
282                            } else {
283                                mStateMachine.reset();
284                                mSecurityMessageDisplay.setMessage(R.string.kg_invalid_puk, true);
285                            }
286                            mCheckInProgress = false;
287                        }
288                    });
289                }
290            }.start();
291        }
292    }
293
294    @Override
295    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
296        // Check if this was the result of hitting the enter key
297        mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS);
298        if (event.getAction() == MotionEvent.ACTION_DOWN) {
299            if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE
300                || actionId == EditorInfo.IME_ACTION_NEXT) {
301                mStateMachine.next();
302                return true;
303            }
304        }
305        return false;
306    }
307
308    @Override
309    public void setLockPatternUtils(LockPatternUtils utils) {
310        mLockPatternUtils = utils;
311    }
312
313    @Override
314    public void reset() {
315        mStateMachine.reset();
316    }
317
318    @Override
319    public KeyguardSecurityCallback getCallback() {
320        return mCallback;
321    }
322
323    @Override
324    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
325        if (mCallback != null) {
326            mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS);
327        }
328    }
329
330    @Override
331    public void onTextChanged(CharSequence s, int start, int before, int count) {
332    }
333
334    @Override
335    public void afterTextChanged(Editable s) {
336    }
337
338    @Override
339    public void setSecurityMessageDisplay(SecurityMessageDisplay display) {
340        mSecurityMessageDisplay = display;
341        reset();
342    }
343}
344