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