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