KeyguardSimPukView.java revision 0b728244dc87b4a453f2191c2cb37a86e91aee0a
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 mSimPinEntry = (TextView) findViewById(R.id.sim_pin_entry); 134 mSimPinEntry.setOnEditorActionListener(this); 135 mSimPinEntry.addTextChangedListener(this); 136 mDeleteButton = findViewById(R.id.delete_button); 137 mDeleteButton.setOnClickListener(this); 138 mKeyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard); 139 mKeyboardHelper = new PasswordEntryKeyboardHelper(mContext, mKeyboardView, this, false, 140 new int[] { 141 R.xml.kg_password_kbd_numeric, 142 com.android.internal.R.xml.password_kbd_qwerty, 143 com.android.internal.R.xml.password_kbd_qwerty_shifted, 144 com.android.internal.R.xml.password_kbd_symbols, 145 com.android.internal.R.xml.password_kbd_symbols_shift 146 }); 147 mKeyboardHelper.setKeyboardMode(PasswordEntryKeyboardHelper.KEYBOARD_MODE_NUMERIC); 148 mKeyboardHelper.setEnableHaptics(mLockPatternUtils.isTactileFeedbackEnabled()); 149 150 mSecurityMessageDisplay = new KeyguardMessageArea.Helper(this); 151 mSecurityMessageDisplay.setTimeout(0); // don't show ownerinfo/charging status by default 152 reset(); 153 } 154 155 @Override 156 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 157 return mSimPinEntry.requestFocus(direction, previouslyFocusedRect); 158 } 159 160 public boolean needsInput() { 161 return false; // This view provides its own keypad 162 } 163 164 public void onPause() { 165 166 } 167 168 public void onResume() { 169 reset(); 170 } 171 172 @Override 173 public void showUsabilityHint() { 174 } 175 176 /** {@inheritDoc} */ 177 public void cleanUp() { 178 // dismiss the dialog. 179 if (mSimUnlockProgressDialog != null) { 180 mSimUnlockProgressDialog.dismiss(); 181 mSimUnlockProgressDialog = null; 182 } 183 } 184 185 /** 186 * Since the IPC can block, we want to run the request in a separate thread 187 * with a callback. 188 */ 189 private abstract class CheckSimPuk extends Thread { 190 191 private final String mPin, mPuk; 192 193 protected CheckSimPuk(String puk, String pin) { 194 mPuk = puk; 195 mPin = pin; 196 } 197 198 abstract void onSimLockChangedResponse(boolean success); 199 200 @Override 201 public void run() { 202 try { 203 final boolean result = ITelephony.Stub.asInterface(ServiceManager 204 .checkService("phone")).supplyPuk(mPuk, mPin); 205 206 post(new Runnable() { 207 public void run() { 208 onSimLockChangedResponse(result); 209 } 210 }); 211 } catch (RemoteException e) { 212 post(new Runnable() { 213 public void run() { 214 onSimLockChangedResponse(false); 215 } 216 }); 217 } 218 } 219 } 220 221 public void onClick(View v) { 222 if (v == mDeleteButton) { 223 mSimPinEntry.requestFocus(); 224 final Editable digits = mSimPinEntry.getEditableText(); 225 final int len = digits.length(); 226 if (len > 0) { 227 digits.delete(len-1, len); 228 } 229 } 230 mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS); 231 } 232 233 private Dialog getSimUnlockProgressDialog() { 234 if (mSimUnlockProgressDialog == null) { 235 mSimUnlockProgressDialog = new ProgressDialog(mContext); 236 mSimUnlockProgressDialog.setMessage(mContext.getString( 237 R.string.kg_sim_unlock_progress_dialog_message)); 238 mSimUnlockProgressDialog.setIndeterminate(true); 239 mSimUnlockProgressDialog.setCancelable(false); 240 if (!(mContext instanceof Activity)) { 241 mSimUnlockProgressDialog.getWindow().setType( 242 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); 243 } 244 } 245 return mSimUnlockProgressDialog; 246 } 247 248 private boolean checkPuk() { 249 // make sure the puk is at least 8 digits long. 250 if (mSimPinEntry.getText().length() >= 8) { 251 mPukText = mSimPinEntry.getText().toString(); 252 return true; 253 } 254 return false; 255 } 256 257 private boolean checkPin() { 258 // make sure the PIN is between 4 and 8 digits 259 int length = mSimPinEntry.getText().length(); 260 if (length >= 4 && length <= 8) { 261 mPinText = mSimPinEntry.getText().toString(); 262 return true; 263 } 264 return false; 265 } 266 267 public boolean confirmPin() { 268 return mPinText.equals(mSimPinEntry.getText().toString()); 269 } 270 271 private void updateSim() { 272 getSimUnlockProgressDialog().show(); 273 274 if (!mCheckInProgress) { 275 mCheckInProgress = true; 276 new CheckSimPuk(mPukText, mPinText) { 277 void onSimLockChangedResponse(final boolean success) { 278 post(new Runnable() { 279 public void run() { 280 if (mSimUnlockProgressDialog != null) { 281 mSimUnlockProgressDialog.hide(); 282 } 283 if (success) { 284 mCallback.dismiss(true); 285 } else { 286 mStateMachine.reset(); 287 mSecurityMessageDisplay.setMessage(R.string.kg_invalid_puk, true); 288 } 289 mCheckInProgress = false; 290 } 291 }); 292 } 293 }.start(); 294 } 295 } 296 297 @Override 298 public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { 299 // Check if this was the result of hitting the enter key 300 mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS); 301 if (event.getAction() == MotionEvent.ACTION_DOWN) { 302 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE 303 || actionId == EditorInfo.IME_ACTION_NEXT) { 304 mStateMachine.next(); 305 return true; 306 } 307 } 308 return false; 309 } 310 311 @Override 312 public void setLockPatternUtils(LockPatternUtils utils) { 313 mLockPatternUtils = utils; 314 } 315 316 @Override 317 public void reset() { 318 mStateMachine.reset(); 319 } 320 321 @Override 322 public KeyguardSecurityCallback getCallback() { 323 return mCallback; 324 } 325 326 @Override 327 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 328 if (mCallback != null) { 329 mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS); 330 } 331 } 332 333 @Override 334 public void onTextChanged(CharSequence s, int start, int before, int count) { 335 } 336 337 @Override 338 public void afterTextChanged(Editable s) { 339 } 340} 341