KeyguardSimPukView.java revision 87d57952af662a94637c1519b2189e22456af2db
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 = R.string.lockscreen_sim_unlock_progress_dialog_message; 95 updateSim(); 96 } else { 97 msg = R.string.kg_invalid_confirm_pin_hint; 98 } 99 } 100 mSimPinEntry.setText(null); 101 if (msg != 0) { 102 mSecurityMessageDisplay.setMessage(msg, true); 103 } 104 } 105 106 void reset() { 107 mPinText=""; 108 mPukText=""; 109 state = ENTER_PUK; 110 mSecurityMessageDisplay.setMessage(R.string.kg_puk_enter_puk_hint, true); 111 mSimPinEntry.requestFocus(); 112 } 113 } 114 115 public KeyguardSimPukView(Context context) { 116 this(context, null); 117 } 118 119 public KeyguardSimPukView(Context context, AttributeSet attrs) { 120 super(context, attrs); 121 mLockPatternUtils = new LockPatternUtils(getContext()); 122 } 123 124 public void setKeyguardCallback(KeyguardSecurityCallback callback) { 125 mCallback = callback; 126 mLockPatternUtils = new LockPatternUtils(getContext()); 127 } 128 129 @Override 130 protected void onFinishInflate() { 131 super.onFinishInflate(); 132 133 // We always set a dummy NavigationManager to avoid null checks 134 mSecurityMessageDisplay = new KeyguardNavigationManager(null); 135 136 mSimPinEntry = (TextView) findViewById(R.id.sim_pin_entry); 137 mSimPinEntry.setOnEditorActionListener(this); 138 mSimPinEntry.addTextChangedListener(this); 139 mDeleteButton = findViewById(R.id.delete_button); 140 mDeleteButton.setOnClickListener(this); 141 mKeyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard); 142 mKeyboardHelper = new PasswordEntryKeyboardHelper(mContext, mKeyboardView, this, false, 143 new int[] { 144 R.xml.kg_password_kbd_numeric, 145 com.android.internal.R.xml.password_kbd_qwerty, 146 com.android.internal.R.xml.password_kbd_qwerty_shifted, 147 com.android.internal.R.xml.password_kbd_symbols, 148 com.android.internal.R.xml.password_kbd_symbols_shift 149 }); 150 mKeyboardHelper.setKeyboardMode(PasswordEntryKeyboardHelper.KEYBOARD_MODE_NUMERIC); 151 mKeyboardHelper.setEnableHaptics(mLockPatternUtils.isTactileFeedbackEnabled()); 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 /** {@inheritDoc} */ 173 public void cleanUp() { 174 // dismiss the dialog. 175 if (mSimUnlockProgressDialog != null) { 176 mSimUnlockProgressDialog.dismiss(); 177 mSimUnlockProgressDialog = null; 178 } 179 } 180 181 /** 182 * Since the IPC can block, we want to run the request in a separate thread 183 * with a callback. 184 */ 185 private abstract class CheckSimPuk extends Thread { 186 187 private final String mPin, mPuk; 188 189 protected CheckSimPuk(String puk, String pin) { 190 mPuk = puk; 191 mPin = pin; 192 } 193 194 abstract void onSimLockChangedResponse(boolean success); 195 196 @Override 197 public void run() { 198 try { 199 final boolean result = ITelephony.Stub.asInterface(ServiceManager 200 .checkService("phone")).supplyPuk(mPuk, mPin); 201 202 post(new Runnable() { 203 public void run() { 204 onSimLockChangedResponse(result); 205 } 206 }); 207 } catch (RemoteException e) { 208 post(new Runnable() { 209 public void run() { 210 onSimLockChangedResponse(false); 211 } 212 }); 213 } 214 } 215 } 216 217 public void onClick(View v) { 218 if (v == mDeleteButton) { 219 mSimPinEntry.requestFocus(); 220 final Editable digits = mSimPinEntry.getEditableText(); 221 final int len = digits.length(); 222 if (len > 0) { 223 digits.delete(len-1, len); 224 } 225 } 226 mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS); 227 } 228 229 private Dialog getSimUnlockProgressDialog() { 230 if (mSimUnlockProgressDialog == null) { 231 mSimUnlockProgressDialog = new ProgressDialog(mContext); 232 mSimUnlockProgressDialog.setMessage(mContext.getString( 233 R.string.kg_sim_unlock_progress_dialog_message)); 234 mSimUnlockProgressDialog.setIndeterminate(true); 235 mSimUnlockProgressDialog.setCancelable(false); 236 if (!(mContext instanceof Activity)) { 237 mSimUnlockProgressDialog.getWindow().setType( 238 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); 239 } 240 } 241 return mSimUnlockProgressDialog; 242 } 243 244 private boolean checkPuk() { 245 // make sure the puk is at least 8 digits long. 246 if (mSimPinEntry.getText().length() >= 8) { 247 mPukText = mSimPinEntry.getText().toString(); 248 return true; 249 } 250 return false; 251 } 252 253 private boolean checkPin() { 254 // make sure the PIN is between 4 and 8 digits 255 int length = mSimPinEntry.getText().length(); 256 if (length >= 4 && length <= 8) { 257 mPinText = mSimPinEntry.getText().toString(); 258 return true; 259 } 260 return false; 261 } 262 263 public boolean confirmPin() { 264 return mPinText.equals(mSimPinEntry.getText().toString()); 265 } 266 267 private void updateSim() { 268 getSimUnlockProgressDialog().show(); 269 270 if (!mCheckInProgress) { 271 mCheckInProgress = true; 272 new CheckSimPuk(mPukText, mPinText) { 273 void onSimLockChangedResponse(final boolean success) { 274 post(new Runnable() { 275 public void run() { 276 if (mSimUnlockProgressDialog != null) { 277 mSimUnlockProgressDialog.hide(); 278 } 279 if (success) { 280 mCallback.dismiss(true); 281 } else { 282 mStateMachine.reset(); 283 mSecurityMessageDisplay.setMessage(R.string.kg_invalid_puk, true); 284 } 285 mCheckInProgress = false; 286 } 287 }); 288 } 289 }.start(); 290 } 291 } 292 293 @Override 294 public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { 295 // Check if this was the result of hitting the enter key 296 mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS); 297 if (event.getAction() == MotionEvent.ACTION_DOWN) { 298 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE 299 || actionId == EditorInfo.IME_ACTION_NEXT) { 300 mStateMachine.next(); 301 return true; 302 } 303 } 304 return false; 305 } 306 307 @Override 308 public void setLockPatternUtils(LockPatternUtils utils) { 309 mLockPatternUtils = utils; 310 } 311 312 @Override 313 public void reset() { 314 mStateMachine.reset(); 315 } 316 317 @Override 318 public KeyguardSecurityCallback getCallback() { 319 return mCallback; 320 } 321 322 @Override 323 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 324 if (mCallback != null) { 325 mCallback.userActivity(KeyguardViewManager.DIGIT_PRESS_WAKE_MILLIS); 326 } 327 } 328 329 @Override 330 public void onTextChanged(CharSequence s, int start, int before, int count) { 331 } 332 333 @Override 334 public void afterTextChanged(Editable s) { 335 } 336 337 @Override 338 public void setSecurityMessageDisplay(SecurityMessageDisplay display) { 339 mSecurityMessageDisplay = display; 340 reset(); 341 } 342} 343