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