PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/policy/src/com/android/internal/policy/impl/SimPukUnlockScreen.java

https://github.com/Kali-/android_frameworks_base
Java | 427 lines | 327 code | 60 blank | 40 comment | 78 complexity | 3ac8f858e7ada6e9e0ab661e317aee74 MD5 | raw file
  1. /*
  2. * Copyright (C) 2008 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. package com.android.internal.policy.impl;
  17. import android.app.Dialog;
  18. import android.app.ProgressDialog;
  19. import android.content.Context;
  20. import android.content.res.Configuration;
  21. import android.os.RemoteException;
  22. import android.os.ServiceManager;
  23. import com.android.internal.telephony.ITelephony;
  24. import com.android.internal.widget.LockPatternUtils;
  25. import android.text.Editable;
  26. import android.util.Log;
  27. import android.view.KeyEvent;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.view.WindowManager;
  31. import android.widget.Button;
  32. import android.widget.LinearLayout;
  33. import android.widget.TextView;
  34. import com.android.internal.R;
  35. /**
  36. * Displays a dialer like interface to unlock the SIM PUK.
  37. */
  38. public class SimPukUnlockScreen extends LinearLayout implements KeyguardScreen,
  39. View.OnClickListener, View.OnFocusChangeListener {
  40. private static final int DIGIT_PRESS_WAKE_MILLIS = 5000;
  41. private final KeyguardUpdateMonitor mUpdateMonitor;
  42. private final KeyguardScreenCallback mCallback;
  43. private KeyguardStatusViewManager mKeyguardStatusViewManager;
  44. private TextView mHeaderText;
  45. private TextView mPukText;
  46. private TextView mPinText;
  47. private TextView mFocusedEntry;
  48. private View mOkButton;
  49. private View mDelPukButton;
  50. private View mDelPinButton;
  51. private ProgressDialog mSimUnlockProgressDialog = null;
  52. private LockPatternUtils mLockPatternUtils;
  53. private int mCreationOrientation;
  54. private int mKeyboardHidden;
  55. private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  56. public SimPukUnlockScreen(Context context, Configuration configuration,
  57. KeyguardUpdateMonitor updateMonitor, KeyguardScreenCallback callback,
  58. LockPatternUtils lockpatternutils) {
  59. super(context);
  60. mUpdateMonitor = updateMonitor;
  61. mCallback = callback;;
  62. mCreationOrientation = configuration.orientation;
  63. mKeyboardHidden = configuration.hardKeyboardHidden;
  64. mLockPatternUtils = lockpatternutils;
  65. LayoutInflater inflater = LayoutInflater.from(context);
  66. if (mKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
  67. inflater.inflate(
  68. R.layout.keyguard_screen_sim_puk_landscape, this, true);
  69. } else {
  70. inflater.inflate(
  71. R.layout.keyguard_screen_sim_puk_portrait, this, true);
  72. new TouchInput();
  73. }
  74. mHeaderText = (TextView) findViewById(R.id.headerText);
  75. mPukText = (TextView) findViewById(R.id.pukDisplay);
  76. mPinText = (TextView) findViewById(R.id.pinDisplay);
  77. mDelPukButton = findViewById(R.id.pukDel);
  78. mDelPinButton = findViewById(R.id.pinDel);
  79. mOkButton = findViewById(R.id.ok);
  80. mDelPinButton.setOnClickListener(this);
  81. mDelPukButton.setOnClickListener(this);
  82. mOkButton.setOnClickListener(this);
  83. mHeaderText.setText(R.string.keyguard_password_enter_puk_code);
  84. // To make marquee work
  85. mHeaderText.setSelected(true);
  86. mKeyguardStatusViewManager = new KeyguardStatusViewManager(this, updateMonitor,
  87. lockpatternutils, callback, true);
  88. mPinText.setFocusableInTouchMode(true);
  89. mPinText.setOnFocusChangeListener(this);
  90. mPukText.setFocusableInTouchMode(true);
  91. mPukText.setOnFocusChangeListener(this);
  92. }
  93. /** {@inheritDoc} */
  94. public boolean needsInput() {
  95. return false;
  96. }
  97. /** {@inheritDoc} */
  98. public void onPause() {
  99. mKeyguardStatusViewManager.onPause();
  100. }
  101. /** {@inheritDoc} */
  102. public void onResume() {
  103. // start fresh
  104. mHeaderText.setText(R.string.keyguard_password_enter_puk_code);
  105. mKeyguardStatusViewManager.onResume();
  106. }
  107. /** {@inheritDoc} */
  108. public void cleanUp() {
  109. // dismiss the dialog.
  110. if (mSimUnlockProgressDialog != null) {
  111. mSimUnlockProgressDialog.dismiss();
  112. mSimUnlockProgressDialog = null;
  113. }
  114. mUpdateMonitor.removeCallback(this);
  115. }
  116. /**
  117. * Since the IPC can block, we want to run the request in a separate thread
  118. * with a callback.
  119. */
  120. private abstract class CheckSimPuk extends Thread {
  121. private final String mPin, mPuk;
  122. protected CheckSimPuk(String puk, String pin) {
  123. mPuk = puk;
  124. mPin = pin;
  125. }
  126. abstract void onSimLockChangedResponse(boolean success);
  127. @Override
  128. public void run() {
  129. try {
  130. final boolean result = ITelephony.Stub.asInterface(ServiceManager
  131. .checkService("phone")).supplyPuk(mPuk, mPin);
  132. post(new Runnable() {
  133. public void run() {
  134. onSimLockChangedResponse(result);
  135. }
  136. });
  137. } catch (RemoteException e) {
  138. post(new Runnable() {
  139. public void run() {
  140. onSimLockChangedResponse(false);
  141. }
  142. });
  143. }
  144. }
  145. }
  146. public void onClick(View v) {
  147. if (v == mDelPukButton) {
  148. if (mFocusedEntry != mPukText)
  149. mPukText.requestFocus();
  150. final Editable digits = mPukText.getEditableText();
  151. final int len = digits.length();
  152. if (len > 0) {
  153. digits.delete(len-1, len);
  154. }
  155. } else if (v == mDelPinButton) {
  156. if (mFocusedEntry != mPinText)
  157. mPinText.requestFocus();
  158. final Editable digits = mPinText.getEditableText();
  159. final int len = digits.length();
  160. if (len > 0) {
  161. digits.delete(len-1, len);
  162. }
  163. } else if (v == mOkButton) {
  164. checkPuk();
  165. }
  166. mCallback.pokeWakelock(DIGIT_PRESS_WAKE_MILLIS);
  167. }
  168. @Override
  169. public void onFocusChange(View v, boolean hasFocus) {
  170. if (hasFocus)
  171. mFocusedEntry = (TextView)v;
  172. }
  173. private Dialog getSimUnlockProgressDialog() {
  174. if (mSimUnlockProgressDialog == null) {
  175. mSimUnlockProgressDialog = new ProgressDialog(mContext);
  176. mSimUnlockProgressDialog.setMessage(
  177. mContext.getString(R.string.lockscreen_sim_unlock_progress_dialog_message));
  178. mSimUnlockProgressDialog.setIndeterminate(true);
  179. mSimUnlockProgressDialog.setCancelable(false);
  180. mSimUnlockProgressDialog.getWindow().setType(
  181. WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
  182. }
  183. return mSimUnlockProgressDialog;
  184. }
  185. private void checkPuk() {
  186. // make sure that the puk is at least 8 digits long.
  187. if (mPukText.getText().length() < 8) {
  188. // otherwise, display a message to the user, and don't submit.
  189. mHeaderText.setText(R.string.invalidPuk);
  190. mPukText.setText("");
  191. return;
  192. }
  193. if (mPinText.getText().length() < 4
  194. || mPinText.getText().length() > 8) {
  195. // otherwise, display a message to the user, and don't submit.
  196. mHeaderText.setText(R.string.invalidPin);
  197. mPinText.setText("");
  198. return;
  199. }
  200. getSimUnlockProgressDialog().show();
  201. new CheckSimPuk(mPukText.getText().toString(),
  202. mPinText.getText().toString()) {
  203. void onSimLockChangedResponse(final boolean success) {
  204. mPinText.post(new Runnable() {
  205. public void run() {
  206. if (mSimUnlockProgressDialog != null) {
  207. mSimUnlockProgressDialog.hide();
  208. }
  209. if (success) {
  210. // before closing the keyguard, report back that
  211. // the sim is unlocked so it knows right away
  212. mUpdateMonitor.reportSimUnlocked();
  213. mCallback.goToUnlockScreen();
  214. } else {
  215. mHeaderText.setText(R.string.badPuk);
  216. mPukText.setText("");
  217. mPinText.setText("");
  218. }
  219. }
  220. });
  221. }
  222. }.start();
  223. }
  224. public boolean onKeyDown(int keyCode, KeyEvent event) {
  225. if (keyCode == KeyEvent.KEYCODE_BACK) {
  226. mCallback.goToLockScreen();
  227. return true;
  228. }
  229. final char match = event.getMatch(DIGITS);
  230. if (match != 0) {
  231. reportDigit(match - '0');
  232. return true;
  233. }
  234. if (keyCode == KeyEvent.KEYCODE_DEL) {
  235. mFocusedEntry.onKeyDown(keyCode, event);
  236. final Editable digits = mFocusedEntry.getEditableText();
  237. final int len = digits.length();
  238. if (len > 0) {
  239. digits.delete(len-1, len);
  240. }
  241. mCallback.pokeWakelock(DIGIT_PRESS_WAKE_MILLIS);
  242. return true;
  243. }
  244. if (keyCode == KeyEvent.KEYCODE_ENTER) {
  245. checkPuk();
  246. return true;
  247. }
  248. return false;
  249. }
  250. private void reportDigit(int digit) {
  251. mFocusedEntry.append(Integer.toString(digit));
  252. }
  253. void updateConfiguration() {
  254. Configuration newConfig = getResources().getConfiguration();
  255. if (newConfig.orientation != mCreationOrientation) {
  256. mCallback.recreateMe(newConfig);
  257. } else if (newConfig.hardKeyboardHidden != mKeyboardHidden) {
  258. mKeyboardHidden = newConfig.hardKeyboardHidden;
  259. final boolean isKeyboardOpen =
  260. (mKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO);
  261. if (mUpdateMonitor.isKeyguardBypassEnabled() && isKeyboardOpen) {
  262. mCallback.goToUnlockScreen();
  263. }
  264. }
  265. }
  266. @Override
  267. protected void onAttachedToWindow() {
  268. super.onAttachedToWindow();
  269. updateConfiguration();
  270. }
  271. /** {@inheritDoc} */
  272. @Override
  273. protected void onConfigurationChanged(Configuration newConfig) {
  274. super.onConfigurationChanged(newConfig);
  275. updateConfiguration();
  276. }
  277. /**
  278. * Helper class to handle input from touch dialer. Only relevant when
  279. * the keyboard is shut.
  280. */
  281. private class TouchInput implements View.OnClickListener {
  282. private TextView mZero;
  283. private TextView mOne;
  284. private TextView mTwo;
  285. private TextView mThree;
  286. private TextView mFour;
  287. private TextView mFive;
  288. private TextView mSix;
  289. private TextView mSeven;
  290. private TextView mEight;
  291. private TextView mNine;
  292. private TextView mCancelButton;
  293. private TouchInput() {
  294. mZero = (TextView) findViewById(R.id.zero);
  295. mOne = (TextView) findViewById(R.id.one);
  296. mTwo = (TextView) findViewById(R.id.two);
  297. mThree = (TextView) findViewById(R.id.three);
  298. mFour = (TextView) findViewById(R.id.four);
  299. mFive = (TextView) findViewById(R.id.five);
  300. mSix = (TextView) findViewById(R.id.six);
  301. mSeven = (TextView) findViewById(R.id.seven);
  302. mEight = (TextView) findViewById(R.id.eight);
  303. mNine = (TextView) findViewById(R.id.nine);
  304. mCancelButton = (TextView) findViewById(R.id.cancel);
  305. mZero.setText("0");
  306. mOne.setText("1");
  307. mTwo.setText("2");
  308. mThree.setText("3");
  309. mFour.setText("4");
  310. mFive.setText("5");
  311. mSix.setText("6");
  312. mSeven.setText("7");
  313. mEight.setText("8");
  314. mNine.setText("9");
  315. mZero.setOnClickListener(this);
  316. mOne.setOnClickListener(this);
  317. mTwo.setOnClickListener(this);
  318. mThree.setOnClickListener(this);
  319. mFour.setOnClickListener(this);
  320. mFive.setOnClickListener(this);
  321. mSix.setOnClickListener(this);
  322. mSeven.setOnClickListener(this);
  323. mEight.setOnClickListener(this);
  324. mNine.setOnClickListener(this);
  325. mCancelButton.setOnClickListener(this);
  326. }
  327. public void onClick(View v) {
  328. if (v == mCancelButton) {
  329. // clear the PIN/PUK entry fields if the user cancels
  330. mPinText.setText("");
  331. mPukText.setText("");
  332. mCallback.goToLockScreen();
  333. return;
  334. }
  335. final int digit = checkDigit(v);
  336. if (digit >= 0) {
  337. mCallback.pokeWakelock(DIGIT_PRESS_WAKE_MILLIS);
  338. reportDigit(digit);
  339. }
  340. }
  341. private int checkDigit(View v) {
  342. int digit = -1;
  343. if (v == mZero) {
  344. digit = 0;
  345. } else if (v == mOne) {
  346. digit = 1;
  347. } else if (v == mTwo) {
  348. digit = 2;
  349. } else if (v == mThree) {
  350. digit = 3;
  351. } else if (v == mFour) {
  352. digit = 4;
  353. } else if (v == mFive) {
  354. digit = 5;
  355. } else if (v == mSix) {
  356. digit = 6;
  357. } else if (v == mSeven) {
  358. digit = 7;
  359. } else if (v == mEight) {
  360. digit = 8;
  361. } else if (v == mNine) {
  362. digit = 9;
  363. }
  364. return digit;
  365. }
  366. }
  367. }