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 android.content.Context; 20import android.content.res.ColorStateList; 21import android.content.res.Resources; 22import android.app.Activity; 23import android.app.AlertDialog; 24import android.app.Dialog; 25import android.app.ProgressDialog; 26import android.graphics.Color; 27import android.os.RemoteException; 28import android.os.ServiceManager; 29import android.telephony.SubscriptionInfo; 30import android.telephony.SubscriptionManager; 31import android.telephony.TelephonyManager; 32import android.util.AttributeSet; 33import android.util.Log; 34import android.view.WindowManager; 35import android.widget.ImageView; 36 37import com.android.internal.telephony.ITelephony; 38import com.android.internal.telephony.IccCardConstants; 39import com.android.internal.telephony.PhoneConstants; 40import com.android.internal.telephony.IccCardConstants.State; 41 42 43/** 44 * Displays a PIN pad for entering a PUK (Pin Unlock Kode) provided by a carrier. 45 */ 46public class KeyguardSimPukView extends KeyguardPinBasedInputView { 47 private static final String LOG_TAG = "KeyguardSimPukView"; 48 private static final boolean DEBUG = KeyguardConstants.DEBUG; 49 public static final String TAG = "KeyguardSimPukView"; 50 51 private ProgressDialog mSimUnlockProgressDialog = null; 52 private CheckSimPuk mCheckSimPukThread; 53 private String mPukText; 54 private String mPinText; 55 private StateMachine mStateMachine = new StateMachine(); 56 private AlertDialog mRemainingAttemptsDialog; 57 private int mSubId; 58 private ImageView mSimImageView; 59 60 KeyguardUpdateMonitorCallback mUpdateMonitorCallback = new KeyguardUpdateMonitorCallback() { 61 @Override 62 public void onSimStateChanged(int subId, int slotId, State simState) { 63 if (DEBUG) Log.v(TAG, "onSimStateChanged(subId=" + subId + ",state=" + simState + ")"); 64 resetState(); 65 }; 66 }; 67 68 public KeyguardSimPukView(Context context) { 69 this(context, null); 70 } 71 72 public KeyguardSimPukView(Context context, AttributeSet attrs) { 73 super(context, attrs); 74 } 75 76 private class StateMachine { 77 final int ENTER_PUK = 0; 78 final int ENTER_PIN = 1; 79 final int CONFIRM_PIN = 2; 80 final int DONE = 3; 81 private int state = ENTER_PUK; 82 83 public void next() { 84 int msg = 0; 85 if (state == ENTER_PUK) { 86 if (checkPuk()) { 87 state = ENTER_PIN; 88 msg = R.string.kg_puk_enter_pin_hint; 89 } else { 90 msg = R.string.kg_invalid_sim_puk_hint; 91 } 92 } else if (state == ENTER_PIN) { 93 if (checkPin()) { 94 state = CONFIRM_PIN; 95 msg = R.string.kg_enter_confirm_pin_hint; 96 } else { 97 msg = R.string.kg_invalid_sim_pin_hint; 98 } 99 } else if (state == CONFIRM_PIN) { 100 if (confirmPin()) { 101 state = DONE; 102 msg = R.string.keyguard_sim_unlock_progress_dialog_message; 103 updateSim(); 104 } else { 105 state = ENTER_PIN; // try again? 106 msg = R.string.kg_invalid_confirm_pin_hint; 107 } 108 } 109 resetPasswordText(true); 110 if (msg != 0) { 111 mSecurityMessageDisplay.setMessage(msg, true); 112 } 113 } 114 115 void reset() { 116 mPinText=""; 117 mPukText=""; 118 state = ENTER_PUK; 119 KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext); 120 mSubId = monitor.getNextSubIdForState(IccCardConstants.State.PUK_REQUIRED); 121 if (SubscriptionManager.isValidSubscriptionId(mSubId)) { 122 int count = TelephonyManager.getDefault().getSimCount(); 123 Resources rez = getResources(); 124 final String msg; 125 int color = Color.WHITE; 126 if (count < 2) { 127 msg = rez.getString(R.string.kg_puk_enter_puk_hint); 128 } else { 129 SubscriptionInfo info = monitor.getSubscriptionInfoForSubId(mSubId); 130 CharSequence displayName = info != null ? info.getDisplayName() : ""; 131 msg = rez.getString(R.string.kg_puk_enter_puk_hint_multi, displayName); 132 if (info != null) { 133 color = info.getIconTint(); 134 } 135 } 136 mSecurityMessageDisplay.setMessage(msg, true); 137 mSimImageView.setImageTintList(ColorStateList.valueOf(color)); 138 } 139 mPasswordEntry.requestFocus(); 140 } 141 } 142 143 @Override 144 protected int getPromtReasonStringRes(int reason) { 145 // No message on SIM Puk 146 return 0; 147 } 148 149 private String getPukPasswordErrorMessage(int attemptsRemaining) { 150 String displayMessage; 151 152 if (attemptsRemaining == 0) { 153 displayMessage = getContext().getString(R.string.kg_password_wrong_puk_code_dead); 154 } else if (attemptsRemaining > 0) { 155 displayMessage = getContext().getResources() 156 .getQuantityString(R.plurals.kg_password_wrong_puk_code, attemptsRemaining, 157 attemptsRemaining); 158 } else { 159 displayMessage = getContext().getString(R.string.kg_password_puk_failed); 160 } 161 if (DEBUG) Log.d(LOG_TAG, "getPukPasswordErrorMessage:" 162 + " attemptsRemaining=" + attemptsRemaining + " displayMessage=" + displayMessage); 163 return displayMessage; 164 } 165 166 public void resetState() { 167 super.resetState(); 168 mStateMachine.reset(); 169 } 170 171 @Override 172 protected boolean shouldLockout(long deadline) { 173 // SIM PUK doesn't have a timed lockout 174 return false; 175 } 176 177 @Override 178 protected int getPasswordTextViewId() { 179 return R.id.pukEntry; 180 } 181 182 @Override 183 protected void onFinishInflate() { 184 super.onFinishInflate(); 185 186 mSecurityMessageDisplay.setTimeout(0); // don't show ownerinfo/charging status by default 187 if (mEcaView instanceof EmergencyCarrierArea) { 188 ((EmergencyCarrierArea) mEcaView).setCarrierTextVisible(true); 189 } 190 mSimImageView = (ImageView) findViewById(R.id.keyguard_sim); 191 } 192 193 @Override 194 protected void onAttachedToWindow() { 195 super.onAttachedToWindow(); 196 KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateMonitorCallback); 197 } 198 199 @Override 200 protected void onDetachedFromWindow() { 201 super.onDetachedFromWindow(); 202 KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mUpdateMonitorCallback); 203 } 204 205 @Override 206 public void showUsabilityHint() { 207 } 208 209 @Override 210 public void onPause() { 211 // dismiss the dialog. 212 if (mSimUnlockProgressDialog != null) { 213 mSimUnlockProgressDialog.dismiss(); 214 mSimUnlockProgressDialog = null; 215 } 216 } 217 218 /** 219 * Since the IPC can block, we want to run the request in a separate thread 220 * with a callback. 221 */ 222 private abstract class CheckSimPuk extends Thread { 223 224 private final String mPin, mPuk; 225 private final int mSubId; 226 227 protected CheckSimPuk(String puk, String pin, int subId) { 228 mPuk = puk; 229 mPin = pin; 230 mSubId = subId; 231 } 232 233 abstract void onSimLockChangedResponse(final int result, final int attemptsRemaining); 234 235 @Override 236 public void run() { 237 try { 238 if (DEBUG) Log.v(TAG, "call supplyPukReportResult()"); 239 final int[] result = ITelephony.Stub.asInterface(ServiceManager 240 .checkService("phone")).supplyPukReportResultForSubscriber(mSubId, mPuk, mPin); 241 if (DEBUG) { 242 Log.v(TAG, "supplyPukReportResult returned: " + result[0] + " " + result[1]); 243 } 244 post(new Runnable() { 245 public void run() { 246 onSimLockChangedResponse(result[0], result[1]); 247 } 248 }); 249 } catch (RemoteException e) { 250 Log.e(TAG, "RemoteException for supplyPukReportResult:", e); 251 post(new Runnable() { 252 public void run() { 253 onSimLockChangedResponse(PhoneConstants.PIN_GENERAL_FAILURE, -1); 254 } 255 }); 256 } 257 } 258 } 259 260 private Dialog getSimUnlockProgressDialog() { 261 if (mSimUnlockProgressDialog == null) { 262 mSimUnlockProgressDialog = new ProgressDialog(mContext); 263 mSimUnlockProgressDialog.setMessage( 264 mContext.getString(R.string.kg_sim_unlock_progress_dialog_message)); 265 mSimUnlockProgressDialog.setIndeterminate(true); 266 mSimUnlockProgressDialog.setCancelable(false); 267 if (!(mContext instanceof Activity)) { 268 mSimUnlockProgressDialog.getWindow().setType( 269 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); 270 } 271 } 272 return mSimUnlockProgressDialog; 273 } 274 275 private Dialog getPukRemainingAttemptsDialog(int remaining) { 276 String msg = getPukPasswordErrorMessage(remaining); 277 if (mRemainingAttemptsDialog == null) { 278 AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 279 builder.setMessage(msg); 280 builder.setCancelable(false); 281 builder.setNeutralButton(R.string.ok, null); 282 mRemainingAttemptsDialog = builder.create(); 283 mRemainingAttemptsDialog.getWindow().setType( 284 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); 285 } else { 286 mRemainingAttemptsDialog.setMessage(msg); 287 } 288 return mRemainingAttemptsDialog; 289 } 290 291 private boolean checkPuk() { 292 // make sure the puk is at least 8 digits long. 293 if (mPasswordEntry.getText().length() == 8) { 294 mPukText = mPasswordEntry.getText(); 295 return true; 296 } 297 return false; 298 } 299 300 private boolean checkPin() { 301 // make sure the PIN is between 4 and 8 digits 302 int length = mPasswordEntry.getText().length(); 303 if (length >= 4 && length <= 8) { 304 mPinText = mPasswordEntry.getText(); 305 return true; 306 } 307 return false; 308 } 309 310 public boolean confirmPin() { 311 return mPinText.equals(mPasswordEntry.getText()); 312 } 313 314 private void updateSim() { 315 getSimUnlockProgressDialog().show(); 316 317 if (mCheckSimPukThread == null) { 318 mCheckSimPukThread = new CheckSimPuk(mPukText, mPinText, mSubId) { 319 void onSimLockChangedResponse(final int result, final int attemptsRemaining) { 320 post(new Runnable() { 321 public void run() { 322 if (mSimUnlockProgressDialog != null) { 323 mSimUnlockProgressDialog.hide(); 324 } 325 resetPasswordText(true /* animate */); 326 if (result == PhoneConstants.PIN_RESULT_SUCCESS) { 327 KeyguardUpdateMonitor.getInstance(getContext()) 328 .reportSimUnlocked(mSubId); 329 mCallback.dismiss(true); 330 } else { 331 if (result == PhoneConstants.PIN_PASSWORD_INCORRECT) { 332 if (attemptsRemaining <= 2) { 333 // this is getting critical - show dialog 334 getPukRemainingAttemptsDialog(attemptsRemaining).show(); 335 } else { 336 // show message 337 mSecurityMessageDisplay.setMessage( 338 getPukPasswordErrorMessage(attemptsRemaining), true); 339 } 340 } else { 341 mSecurityMessageDisplay.setMessage(getContext().getString( 342 R.string.kg_password_puk_failed), true); 343 } 344 if (DEBUG) Log.d(LOG_TAG, "verifyPasswordAndUnlock " 345 + " UpdateSim.onSimCheckResponse: " 346 + " attemptsRemaining=" + attemptsRemaining); 347 mStateMachine.reset(); 348 } 349 mCheckSimPukThread = null; 350 } 351 }); 352 } 353 }; 354 mCheckSimPukThread.start(); 355 } 356 } 357 358 @Override 359 protected void verifyPasswordAndUnlock() { 360 mStateMachine.next(); 361 } 362 363 @Override 364 public void startAppearAnimation() { 365 // noop. 366 } 367 368 @Override 369 public boolean startDisappearAnimation(Runnable finishRunnable) { 370 return false; 371 } 372} 373 374 375