blob: 865a7c4cd96df4e91d1ce17b5c8a146e62ad371b [file] [log] [blame]
Jim Millerdcb3d842012-08-23 19:18:12 -07001/*
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
Jim Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
Jim Millerdcb3d842012-08-23 19:18:12 -070018
Daniel Sandler53149e62012-11-01 22:00:07 -040019import com.android.internal.telephony.ITelephony;
20
21import android.content.Context;
Jim Millerdcb3d842012-08-23 19:18:12 -070022import android.app.Activity;
23import android.app.Dialog;
24import android.app.ProgressDialog;
Jim Millerdcb3d842012-08-23 19:18:12 -070025import android.os.RemoteException;
26import android.os.ServiceManager;
Jim Miller9cf2c522012-10-04 22:02:29 -070027import android.text.Editable;
Daniel Sandler53149e62012-11-01 22:00:07 -040028import android.text.InputType;
Jim Miller9cf2c522012-10-04 22:02:29 -070029import android.text.TextWatcher;
Daniel Sandler53149e62012-11-01 22:00:07 -040030import android.text.method.DigitsKeyListener;
Jim Millerdcb3d842012-08-23 19:18:12 -070031import android.util.AttributeSet;
Jim Millerdcb3d842012-08-23 19:18:12 -070032import android.view.View;
33import android.view.WindowManager;
Jim Millerdcb3d842012-08-23 19:18:12 -070034import android.widget.TextView.OnEditorActionListener;
35
36/**
Daniel Sandler53149e62012-11-01 22:00:07 -040037 * Displays a PIN pad for unlocking.
Jim Millerdcb3d842012-08-23 19:18:12 -070038 */
Daniel Sandler53149e62012-11-01 22:00:07 -040039public class KeyguardSimPinView extends KeyguardAbsKeyInputView
Jim Millerd6c48842012-11-01 21:40:47 -070040 implements KeyguardSecurityView, OnEditorActionListener, TextWatcher {
Jim Millerdcb3d842012-08-23 19:18:12 -070041
Jim Millerd6c48842012-11-01 21:40:47 -070042 private ProgressDialog mSimUnlockProgressDialog = null;
43 private volatile boolean mSimCheckInProgress;
44
45 public KeyguardSimPinView(Context context) {
Jim Millerdcb3d842012-08-23 19:18:12 -070046 this(context, null);
47 }
48
49 public KeyguardSimPinView(Context context, AttributeSet attrs) {
50 super(context, attrs);
Jim Millerdcb3d842012-08-23 19:18:12 -070051 }
52
Daniel Sandler53149e62012-11-01 22:00:07 -040053 public void resetState() {
54 mSecurityMessageDisplay.setMessage(R.string.kg_sim_pin_instructions, true);
55 mPasswordEntry.setEnabled(true);
56 }
57
58 @Override
59 protected int getPasswordTextViewId() {
60 return R.id.pinEntry;
Jim Millerdcb3d842012-08-23 19:18:12 -070061 }
62
63 @Override
64 protected void onFinishInflate() {
65 super.onFinishInflate();
66
Daniel Sandler53149e62012-11-01 22:00:07 -040067 final View ok = findViewById(R.id.key_enter);
68 if (ok != null) {
69 ok.setOnClickListener(new View.OnClickListener() {
70 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -070071 public void onClick(View v) {
Daniel Sandler53149e62012-11-01 22:00:07 -040072 doHapticKeyClick();
73 verifyPasswordAndUnlock();
Jim Millerdcb3d842012-08-23 19:18:12 -070074 }
75 });
76 }
Jim Miller0b728242012-10-28 19:42:30 -070077
Daniel Sandler53149e62012-11-01 22:00:07 -040078 // The delete button is of the PIN keyboard itself in some (e.g. tablet) layouts,
79 // not a separate view
80 View pinDelete = findViewById(R.id.delete_button);
81 if (pinDelete != null) {
82 pinDelete.setVisibility(View.VISIBLE);
83 pinDelete.setOnClickListener(new OnClickListener() {
84 public void onClick(View v) {
85 CharSequence str = mPasswordEntry.getText();
86 if (str.length() > 0) {
87 mPasswordEntry.setText(str.subSequence(0, str.length()-1));
88 }
89 doHapticKeyClick();
90 }
91 });
92 pinDelete.setOnLongClickListener(new View.OnLongClickListener() {
93 public boolean onLongClick(View v) {
94 mPasswordEntry.setText("");
95 doHapticKeyClick();
96 return true;
97 }
98 });
99 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700100
Daniel Sandler53149e62012-11-01 22:00:07 -0400101 mPasswordEntry.setKeyListener(DigitsKeyListener.getInstance());
102 mPasswordEntry.setInputType(InputType.TYPE_CLASS_NUMBER
103 | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
Jim Millerdcb3d842012-08-23 19:18:12 -0700104
Daniel Sandler53149e62012-11-01 22:00:07 -0400105 mPasswordEntry.requestFocus();
Jim Millerdcb3d842012-08-23 19:18:12 -0700106 }
107
Adam Cohen6fb841f2012-10-24 13:15:38 -0700108 @Override
109 public void showUsabilityHint() {
110 }
111
Daniel Sandler53149e62012-11-01 22:00:07 -0400112 @Override
113 public void onPause() {
Jim Millerdcb3d842012-08-23 19:18:12 -0700114 // dismiss the dialog.
115 if (mSimUnlockProgressDialog != null) {
116 mSimUnlockProgressDialog.dismiss();
117 mSimUnlockProgressDialog = null;
118 }
119 }
120
121 /**
122 * Since the IPC can block, we want to run the request in a separate thread
123 * with a callback.
124 */
125 private abstract class CheckSimPin extends Thread {
126 private final String mPin;
127
128 protected CheckSimPin(String pin) {
129 mPin = pin;
130 }
131
Jim Miller4b09dd32012-09-04 14:27:25 -0700132 abstract void onSimCheckResponse(boolean success);
Jim Millerdcb3d842012-08-23 19:18:12 -0700133
134 @Override
135 public void run() {
136 try {
137 final boolean result = ITelephony.Stub.asInterface(ServiceManager
138 .checkService("phone")).supplyPin(mPin);
139 post(new Runnable() {
140 public void run() {
Jim Miller4b09dd32012-09-04 14:27:25 -0700141 onSimCheckResponse(result);
Jim Millerdcb3d842012-08-23 19:18:12 -0700142 }
143 });
144 } catch (RemoteException e) {
145 post(new Runnable() {
146 public void run() {
Jim Miller4b09dd32012-09-04 14:27:25 -0700147 onSimCheckResponse(false);
Jim Millerdcb3d842012-08-23 19:18:12 -0700148 }
149 });
150 }
151 }
152 }
153
Jim Millerdcb3d842012-08-23 19:18:12 -0700154 private Dialog getSimUnlockProgressDialog() {
155 if (mSimUnlockProgressDialog == null) {
156 mSimUnlockProgressDialog = new ProgressDialog(mContext);
157 mSimUnlockProgressDialog.setMessage(
158 mContext.getString(R.string.kg_sim_unlock_progress_dialog_message));
159 mSimUnlockProgressDialog.setIndeterminate(true);
160 mSimUnlockProgressDialog.setCancelable(false);
161 if (!(mContext instanceof Activity)) {
162 mSimUnlockProgressDialog.getWindow().setType(
163 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
164 }
165 }
166 return mSimUnlockProgressDialog;
167 }
168
Daniel Sandler53149e62012-11-01 22:00:07 -0400169 @Override
170 protected void verifyPasswordAndUnlock() {
171 String entry = mPasswordEntry.getText().toString();
172
173 if (entry.length() < 4) {
Jim Millerdcb3d842012-08-23 19:18:12 -0700174 // otherwise, display a message to the user, and don't submit.
Adam Cohen0a4f9002012-10-12 19:57:16 -0700175 mSecurityMessageDisplay.setMessage(R.string.kg_invalid_sim_pin_hint, true);
Daniel Sandler53149e62012-11-01 22:00:07 -0400176 mPasswordEntry.setText("");
Jim Millerdcb3d842012-08-23 19:18:12 -0700177 mCallback.userActivity(0);
178 return;
179 }
180
181 getSimUnlockProgressDialog().show();
182
Jim Miller4b09dd32012-09-04 14:27:25 -0700183 if (!mSimCheckInProgress) {
184 mSimCheckInProgress = true; // there should be only one
Daniel Sandler53149e62012-11-01 22:00:07 -0400185 new CheckSimPin(mPasswordEntry.getText().toString()) {
Jim Miller4b09dd32012-09-04 14:27:25 -0700186 void onSimCheckResponse(final boolean success) {
187 post(new Runnable() {
188 public void run() {
189 if (mSimUnlockProgressDialog != null) {
190 mSimUnlockProgressDialog.hide();
191 }
192 if (success) {
193 // before closing the keyguard, report back that the sim is unlocked
194 // so it knows right away.
195 KeyguardUpdateMonitor.getInstance(getContext()).reportSimUnlocked();
196 mCallback.dismiss(true);
197 } else {
Adam Cohen0a4f9002012-10-12 19:57:16 -0700198 mSecurityMessageDisplay.setMessage
199 (R.string.kg_password_wrong_pin_code, true);
Daniel Sandler53149e62012-11-01 22:00:07 -0400200 mPasswordEntry.setText("");
Jim Miller4b09dd32012-09-04 14:27:25 -0700201 }
202 mCallback.userActivity(0);
203 mSimCheckInProgress = false;
Jim Millerdcb3d842012-08-23 19:18:12 -0700204 }
Jim Miller4b09dd32012-09-04 14:27:25 -0700205 });
206 }
207 }.start();
208 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700209 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700210}
Daniel Sandler53149e62012-11-01 22:00:07 -0400211