blob: f0c5805f4e77de20dd020851f07a62f75f2e5dc7 [file] [log] [blame]
Jim Millerdcb3d842012-08-23 19:18:12 -07001/*
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 */
Jim Millerdcb3d842012-08-23 19:18:12 -070016
Jim Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
18
Jim Miller5ecd8112013-01-09 18:50:26 -080019import android.content.Context;
Jim Millerdcb3d842012-08-23 19:18:12 -070020import android.app.Activity;
Wink Savilleb896b9f2013-10-23 15:44:26 -070021import android.app.AlertDialog;
Jim Millerdcb3d842012-08-23 19:18:12 -070022import android.app.Dialog;
23import android.app.ProgressDialog;
Jim Millerdcb3d842012-08-23 19:18:12 -070024import android.os.RemoteException;
25import android.os.ServiceManager;
Jim Millerdcb3d842012-08-23 19:18:12 -070026import android.util.AttributeSet;
Wink Savilleb896b9f2013-10-23 15:44:26 -070027import android.util.Log;
Jim Millerdcb3d842012-08-23 19:18:12 -070028import android.view.WindowManager;
Jim Millerdcb3d842012-08-23 19:18:12 -070029
Wink Savilleb896b9f2013-10-23 15:44:26 -070030import com.android.internal.telephony.ITelephony;
31import com.android.internal.telephony.PhoneConstants;
32
33
Daniel Sandler23d7a6e2012-11-02 00:21:07 -040034/**
35 * Displays a PIN pad for entering a PUK (Pin Unlock Kode) provided by a carrier.
36 */
Selim Cinek4e8b9ed2014-06-20 16:37:04 -070037public class KeyguardSimPukView extends KeyguardPinBasedInputView {
Wink Savilleb896b9f2013-10-23 15:44:26 -070038 private static final String LOG_TAG = "KeyguardSimPukView";
Jorim Jaggi5cf17872014-03-26 18:31:48 +010039 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller4fc2b012013-11-04 16:47:48 -080040 public static final String TAG = "KeyguardSimPukView";
Jim Millerdcb3d842012-08-23 19:18:12 -070041
42 private ProgressDialog mSimUnlockProgressDialog = null;
Jim Miller4fc2b012013-11-04 16:47:48 -080043 private CheckSimPuk mCheckSimPukThread;
Jim Miller3efe1062012-09-28 16:59:31 -070044 private String mPukText;
Jim Miller3efe1062012-09-28 16:59:31 -070045 private String mPinText;
46 private StateMachine mStateMachine = new StateMachine();
Wink Savilleb896b9f2013-10-23 15:44:26 -070047 private AlertDialog mRemainingAttemptsDialog;
Jim Miller3efe1062012-09-28 16:59:31 -070048
49 private class StateMachine {
50 final int ENTER_PUK = 0;
51 final int ENTER_PIN = 1;
52 final int CONFIRM_PIN = 2;
53 final int DONE = 3;
54 private int state = ENTER_PUK;
55
56 public void next() {
57 int msg = 0;
58 if (state == ENTER_PUK) {
59 if (checkPuk()) {
60 state = ENTER_PIN;
61 msg = R.string.kg_puk_enter_pin_hint;
62 } else {
63 msg = R.string.kg_invalid_sim_puk_hint;
64 }
65 } else if (state == ENTER_PIN) {
66 if (checkPin()) {
67 state = CONFIRM_PIN;
68 msg = R.string.kg_enter_confirm_pin_hint;
69 } else {
70 msg = R.string.kg_invalid_sim_pin_hint;
71 }
72 } else if (state == CONFIRM_PIN) {
73 if (confirmPin()) {
74 state = DONE;
Jim Miller5ecd8112013-01-09 18:50:26 -080075 msg = R.string.keyguard_sim_unlock_progress_dialog_message;
Jim Miller3efe1062012-09-28 16:59:31 -070076 updateSim();
77 } else {
Daniel Sandler23d7a6e2012-11-02 00:21:07 -040078 state = ENTER_PIN; // try again?
Jim Miller3efe1062012-09-28 16:59:31 -070079 msg = R.string.kg_invalid_confirm_pin_hint;
80 }
81 }
Selim Cinek4e8b9ed2014-06-20 16:37:04 -070082 resetPasswordText(true);
Jim Miller3efe1062012-09-28 16:59:31 -070083 if (msg != 0) {
Adam Cohen0a4f9002012-10-12 19:57:16 -070084 mSecurityMessageDisplay.setMessage(msg, true);
Jim Miller3efe1062012-09-28 16:59:31 -070085 }
86 }
87
88 void reset() {
89 mPinText="";
90 mPukText="";
91 state = ENTER_PUK;
Adam Cohen6dbf8612012-10-14 17:26:31 -070092 mSecurityMessageDisplay.setMessage(R.string.kg_puk_enter_puk_hint, true);
Daniel Sandler23d7a6e2012-11-02 00:21:07 -040093 mPasswordEntry.requestFocus();
Jim Miller3efe1062012-09-28 16:59:31 -070094 }
95 }
96
Wink Savilleb896b9f2013-10-23 15:44:26 -070097 private String getPukPasswordErrorMessage(int attemptsRemaining) {
98 String displayMessage;
99
100 if (attemptsRemaining == 0) {
101 displayMessage = getContext().getString(R.string.kg_password_wrong_puk_code_dead);
102 } else if (attemptsRemaining > 0) {
103 displayMessage = getContext().getResources()
104 .getQuantityString(R.plurals.kg_password_wrong_puk_code, attemptsRemaining,
105 attemptsRemaining);
106 } else {
107 displayMessage = getContext().getString(R.string.kg_password_puk_failed);
108 }
109 if (DEBUG) Log.d(LOG_TAG, "getPukPasswordErrorMessage:"
110 + " attemptsRemaining=" + attemptsRemaining + " displayMessage=" + displayMessage);
111 return displayMessage;
112 }
113
Jim Millerdcb3d842012-08-23 19:18:12 -0700114 public KeyguardSimPukView(Context context) {
115 this(context, null);
116 }
117
118 public KeyguardSimPukView(Context context, AttributeSet attrs) {
119 super(context, attrs);
Jim Millerdcb3d842012-08-23 19:18:12 -0700120 }
121
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400122 public void resetState() {
Selim Cinek4e8b9ed2014-06-20 16:37:04 -0700123 super.resetState();
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400124 mStateMachine.reset();
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400125 }
126
127 @Override
Jim Miller7d5e00a2013-10-11 22:45:57 -0700128 protected boolean shouldLockout(long deadline) {
129 // SIM PUK doesn't have a timed lockout
130 return false;
131 }
132
133 @Override
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400134 protected int getPasswordTextViewId() {
Jorim Jaggifa9189f2014-04-17 20:17:30 +0200135 return R.id.pukEntry;
Jim Millerdcb3d842012-08-23 19:18:12 -0700136 }
137
138 @Override
139 protected void onFinishInflate() {
140 super.onFinishInflate();
Jim Miller0b728242012-10-28 19:42:30 -0700141
Jim Miller0b728242012-10-28 19:42:30 -0700142 mSecurityMessageDisplay.setTimeout(0); // don't show ownerinfo/charging status by default
Jorim Jaggi40a0b382014-05-27 21:12:27 +0200143 if (mEcaView instanceof EmergencyCarrierArea) {
144 ((EmergencyCarrierArea) mEcaView).setCarrierTextVisible(true);
145 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700146 }
147
Adam Cohen6fb841f2012-10-24 13:15:38 -0700148 @Override
149 public void showUsabilityHint() {
150 }
151
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400152 @Override
153 public void onPause() {
Jim Millerdcb3d842012-08-23 19:18:12 -0700154 // dismiss the dialog.
155 if (mSimUnlockProgressDialog != null) {
156 mSimUnlockProgressDialog.dismiss();
157 mSimUnlockProgressDialog = null;
158 }
159 }
160
161 /**
162 * Since the IPC can block, we want to run the request in a separate thread
163 * with a callback.
164 */
165 private abstract class CheckSimPuk extends Thread {
166
167 private final String mPin, mPuk;
168
169 protected CheckSimPuk(String puk, String pin) {
170 mPuk = puk;
171 mPin = pin;
172 }
173
Wink Savilleb896b9f2013-10-23 15:44:26 -0700174 abstract void onSimLockChangedResponse(final int result, final int attemptsRemaining);
Jim Millerdcb3d842012-08-23 19:18:12 -0700175
176 @Override
177 public void run() {
178 try {
Jim Miller4fc2b012013-11-04 16:47:48 -0800179 Log.v(TAG, "call supplyPukReportResult()");
Wink Savilleb896b9f2013-10-23 15:44:26 -0700180 final int[] result = ITelephony.Stub.asInterface(ServiceManager
181 .checkService("phone")).supplyPukReportResult(mPuk, mPin);
Jim Miller4fc2b012013-11-04 16:47:48 -0800182 Log.v(TAG, "supplyPukReportResult returned: " + result[0] + " " + result[1]);
Jim Millerdcb3d842012-08-23 19:18:12 -0700183 post(new Runnable() {
184 public void run() {
Wink Savilleb896b9f2013-10-23 15:44:26 -0700185 onSimLockChangedResponse(result[0], result[1]);
Jim Millerdcb3d842012-08-23 19:18:12 -0700186 }
187 });
188 } catch (RemoteException e) {
Jim Miller4fc2b012013-11-04 16:47:48 -0800189 Log.e(TAG, "RemoteException for supplyPukReportResult:", e);
Jim Millerdcb3d842012-08-23 19:18:12 -0700190 post(new Runnable() {
191 public void run() {
Wink Savilleb896b9f2013-10-23 15:44:26 -0700192 onSimLockChangedResponse(PhoneConstants.PIN_GENERAL_FAILURE, -1);
Jim Millerdcb3d842012-08-23 19:18:12 -0700193 }
194 });
195 }
196 }
197 }
198
Jim Millerdcb3d842012-08-23 19:18:12 -0700199 private Dialog getSimUnlockProgressDialog() {
200 if (mSimUnlockProgressDialog == null) {
201 mSimUnlockProgressDialog = new ProgressDialog(mContext);
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400202 mSimUnlockProgressDialog.setMessage(
203 mContext.getString(R.string.kg_sim_unlock_progress_dialog_message));
Jim Millerdcb3d842012-08-23 19:18:12 -0700204 mSimUnlockProgressDialog.setIndeterminate(true);
205 mSimUnlockProgressDialog.setCancelable(false);
206 if (!(mContext instanceof Activity)) {
207 mSimUnlockProgressDialog.getWindow().setType(
208 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
209 }
210 }
211 return mSimUnlockProgressDialog;
212 }
213
Wink Savilleb896b9f2013-10-23 15:44:26 -0700214 private Dialog getPukRemainingAttemptsDialog(int remaining) {
215 String msg = getPukPasswordErrorMessage(remaining);
216 if (mRemainingAttemptsDialog == null) {
217 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
218 builder.setMessage(msg);
219 builder.setCancelable(false);
220 builder.setNeutralButton(R.string.ok, null);
221 mRemainingAttemptsDialog = builder.create();
222 mRemainingAttemptsDialog.getWindow().setType(
223 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
224 } else {
225 mRemainingAttemptsDialog.setMessage(msg);
226 }
227 return mRemainingAttemptsDialog;
228 }
229
Jim Miller3efe1062012-09-28 16:59:31 -0700230 private boolean checkPuk() {
Jim Millerdcb3d842012-08-23 19:18:12 -0700231 // make sure the puk is at least 8 digits long.
Rakesh Pallerlab8b7bd32014-01-08 15:49:34 +0530232 if (mPasswordEntry.getText().length() == 8) {
Selim Cinek4e8b9ed2014-06-20 16:37:04 -0700233 mPukText = mPasswordEntry.getText();
Jim Miller3efe1062012-09-28 16:59:31 -0700234 return true;
Jim Millerdcb3d842012-08-23 19:18:12 -0700235 }
Jim Miller3efe1062012-09-28 16:59:31 -0700236 return false;
237 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700238
Jim Miller3efe1062012-09-28 16:59:31 -0700239 private boolean checkPin() {
Jim Millerdcb3d842012-08-23 19:18:12 -0700240 // make sure the PIN is between 4 and 8 digits
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400241 int length = mPasswordEntry.getText().length();
Jim Miller3efe1062012-09-28 16:59:31 -0700242 if (length >= 4 && length <= 8) {
Selim Cinek4e8b9ed2014-06-20 16:37:04 -0700243 mPinText = mPasswordEntry.getText();
Jim Miller3efe1062012-09-28 16:59:31 -0700244 return true;
Jim Millerdcb3d842012-08-23 19:18:12 -0700245 }
Jim Miller3efe1062012-09-28 16:59:31 -0700246 return false;
247 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700248
Jim Miller3efe1062012-09-28 16:59:31 -0700249 public boolean confirmPin() {
Selim Cinek4e8b9ed2014-06-20 16:37:04 -0700250 return mPinText.equals(mPasswordEntry.getText());
Jim Miller3efe1062012-09-28 16:59:31 -0700251 }
252
253 private void updateSim() {
Jim Millerdcb3d842012-08-23 19:18:12 -0700254 getSimUnlockProgressDialog().show();
255
Jim Miller4fc2b012013-11-04 16:47:48 -0800256 if (mCheckSimPukThread == null) {
257 mCheckSimPukThread = new CheckSimPuk(mPukText, mPinText) {
Wink Savilleb896b9f2013-10-23 15:44:26 -0700258 void onSimLockChangedResponse(final int result, final int attemptsRemaining) {
Jim Miller3efe1062012-09-28 16:59:31 -0700259 post(new Runnable() {
Jim Miller4b09dd32012-09-04 14:27:25 -0700260 public void run() {
261 if (mSimUnlockProgressDialog != null) {
262 mSimUnlockProgressDialog.hide();
263 }
Wink Savilleb896b9f2013-10-23 15:44:26 -0700264 if (result == PhoneConstants.PIN_RESULT_SUCCESS) {
265 KeyguardUpdateMonitor.getInstance(getContext()).reportSimUnlocked();
Jim Miller4b09dd32012-09-04 14:27:25 -0700266 mCallback.dismiss(true);
267 } else {
Wink Savilleb896b9f2013-10-23 15:44:26 -0700268 if (result == PhoneConstants.PIN_PASSWORD_INCORRECT) {
269 if (attemptsRemaining <= 2) {
270 // this is getting critical - show dialog
271 getPukRemainingAttemptsDialog(attemptsRemaining).show();
272 } else {
273 // show message
274 mSecurityMessageDisplay.setMessage(
275 getPukPasswordErrorMessage(attemptsRemaining), true);
276 }
277 } else {
278 mSecurityMessageDisplay.setMessage(getContext().getString(
279 R.string.kg_password_puk_failed), true);
280 }
281 if (DEBUG) Log.d(LOG_TAG, "verifyPasswordAndUnlock "
282 + " UpdateSim.onSimCheckResponse: "
283 + " attemptsRemaining=" + attemptsRemaining);
Jim Miller3efe1062012-09-28 16:59:31 -0700284 mStateMachine.reset();
Jim Miller4b09dd32012-09-04 14:27:25 -0700285 }
Jim Miller4fc2b012013-11-04 16:47:48 -0800286 mCheckSimPukThread = null;
Jim Millerdcb3d842012-08-23 19:18:12 -0700287 }
Jim Miller4b09dd32012-09-04 14:27:25 -0700288 });
289 }
Jim Miller4fc2b012013-11-04 16:47:48 -0800290 };
291 mCheckSimPukThread.start();
Jim Miller4b09dd32012-09-04 14:27:25 -0700292 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700293 }
294
295 @Override
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400296 protected void verifyPasswordAndUnlock() {
297 mStateMachine.next();
Jim Miller9cf2c522012-10-04 22:02:29 -0700298 }
Jorim Jaggic14f8292014-05-27 02:25:45 +0200299
300 @Override
301 public void startAppearAnimation() {
302 // noop.
303 }
Jorim Jaggi76a16232014-08-08 17:00:47 +0200304
305 @Override
306 public boolean startDisappearAnimation(Runnable finishRunnable) {
307 return false;
308 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700309}
Daniel Sandler23d7a6e2012-11-02 00:21:07 -0400310
311