2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.keyguard;
19 import android.content.Context;
20 import android.graphics.Rect;
21 import android.text.Editable;
22 import android.text.InputType;
23 import android.text.TextUtils;
24 import android.text.TextWatcher;
25 import android.text.method.TextKeyListener;
26 import android.util.AttributeSet;
27 import android.view.KeyEvent;
28 import android.view.View;
29 import android.view.animation.AnimationUtils;
30 import android.view.animation.Interpolator;
31 import android.view.inputmethod.EditorInfo;
32 import android.view.inputmethod.InputMethodInfo;
33 import android.view.inputmethod.InputMethodManager;
34 import android.view.inputmethod.InputMethodSubtype;
35 import android.widget.TextView;
36 import android.widget.TextView.OnEditorActionListener;
38 import com.android.internal.widget.TextViewInputDisabler;
40 import java.util.List;
42 * Displays an alphanumeric (latin-1) key entry for the user to enter
45 public class KeyguardPasswordView extends KeyguardAbsKeyInputView
46 implements KeyguardSecurityView, OnEditorActionListener, TextWatcher {
48 private final boolean mShowImeAtScreenOn;
49 private final int mDisappearYTranslation;
51 // A delay constant to be used in a workaround for the situation where InputMethodManagerService
52 // is not switched to the new user yet.
53 // TODO: Remove this by ensuring such a race condition never happens.
54 private static final int DELAY_MILLIS_TO_REEVALUATE_IME_SWITCH_ICON = 500; // 500ms
56 InputMethodManager mImm;
57 private TextView mPasswordEntry;
58 private TextViewInputDisabler mPasswordEntryDisabler;
59 private View mSwitchImeButton;
61 private Interpolator mLinearOutSlowInInterpolator;
62 private Interpolator mFastOutLinearInInterpolator;
64 public KeyguardPasswordView(Context context) {
68 public KeyguardPasswordView(Context context, AttributeSet attrs) {
69 super(context, attrs);
70 mShowImeAtScreenOn = context.getResources().
71 getBoolean(R.bool.kg_show_ime_at_screen_on);
72 mDisappearYTranslation = getResources().getDimensionPixelSize(
73 R.dimen.disappear_y_translation);
74 mLinearOutSlowInInterpolator = AnimationUtils.loadInterpolator(
75 context, android.R.interpolator.linear_out_slow_in);
76 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(
77 context, android.R.interpolator.fast_out_linear_in);
81 protected void resetState() {
82 mSecurityMessageDisplay.setMessage(R.string.kg_password_instructions, false);
83 final boolean wasDisabled = mPasswordEntry.isEnabled();
84 setPasswordEntryEnabled(true);
85 setPasswordEntryInputEnabled(true);
87 mImm.showSoftInput(mPasswordEntry, InputMethodManager.SHOW_IMPLICIT);
92 protected int getPasswordTextViewId() {
93 return R.id.passwordEntry;
97 public boolean needsInput() {
102 public void onResume(final int reason) {
103 super.onResume(reason);
105 // Wait a bit to focus the field so the focusable flag on the window is already set then.
106 post(new Runnable() {
109 if (isShown() && mPasswordEntry.isEnabled()) {
110 mPasswordEntry.requestFocus();
111 if (reason != KeyguardSecurityView.SCREEN_ON || mShowImeAtScreenOn) {
112 mImm.showSoftInput(mPasswordEntry, InputMethodManager.SHOW_IMPLICIT);
120 protected int getPromtReasonStringRes(int reason) {
122 case PROMPT_REASON_RESTART:
123 return R.string.kg_prompt_reason_restart_password;
124 case PROMPT_REASON_TIMEOUT:
125 return R.string.kg_prompt_reason_timeout_password;
126 case PROMPT_REASON_DEVICE_ADMIN:
127 return R.string.kg_prompt_reason_device_admin;
128 case PROMPT_REASON_USER_REQUEST:
129 return R.string.kg_prompt_reason_user_request;
130 case PROMPT_REASON_NONE:
133 return R.string.kg_prompt_reason_timeout_password;
138 public void onPause() {
140 mImm.hideSoftInputFromWindow(getWindowToken(), 0);
144 public void reset() {
146 mPasswordEntry.requestFocus();
149 private void updateSwitchImeButton() {
150 // If there's more than one IME, enable the IME switcher button
151 final boolean wasVisible = mSwitchImeButton.getVisibility() == View.VISIBLE;
152 final boolean shouldBeVisible = hasMultipleEnabledIMEsOrSubtypes(mImm, false);
153 if (wasVisible != shouldBeVisible) {
154 mSwitchImeButton.setVisibility(shouldBeVisible ? View.VISIBLE : View.GONE);
157 // TODO: Check if we still need this hack.
158 // If no icon is visible, reset the start margin on the password field so the text is
160 if (mSwitchImeButton.getVisibility() != View.VISIBLE) {
161 android.view.ViewGroup.LayoutParams params = mPasswordEntry.getLayoutParams();
162 if (params instanceof MarginLayoutParams) {
163 final MarginLayoutParams mlp = (MarginLayoutParams) params;
164 mlp.setMarginStart(0);
165 mPasswordEntry.setLayoutParams(params);
171 protected void onFinishInflate() {
172 super.onFinishInflate();
174 mImm = (InputMethodManager) getContext().getSystemService(
175 Context.INPUT_METHOD_SERVICE);
177 mPasswordEntry = (TextView) findViewById(getPasswordTextViewId());
178 mPasswordEntryDisabler = new TextViewInputDisabler(mPasswordEntry);
179 mPasswordEntry.setKeyListener(TextKeyListener.getInstance());
180 mPasswordEntry.setInputType(InputType.TYPE_CLASS_TEXT
181 | InputType.TYPE_TEXT_VARIATION_PASSWORD);
182 mPasswordEntry.setOnEditorActionListener(this);
183 mPasswordEntry.addTextChangedListener(this);
185 // Poke the wakelock any time the text is selected or modified
186 mPasswordEntry.setOnClickListener(new OnClickListener() {
188 public void onClick(View v) {
189 mCallback.userActivity();
193 // Set selected property on so the view can send accessibility events.
194 mPasswordEntry.setSelected(true);
196 mPasswordEntry.requestFocus();
198 mSwitchImeButton = findViewById(R.id.switch_ime_button);
199 mSwitchImeButton.setOnClickListener(new OnClickListener() {
201 public void onClick(View v) {
202 mCallback.userActivity(); // Leave the screen on a bit longer
203 // Do not show auxiliary subtypes in password lock screen.
204 mImm.showInputMethodPicker(false /* showAuxiliarySubtypes */);
208 // If there's more than one IME, enable the IME switcher button
209 updateSwitchImeButton();
211 // When we the current user is switching, InputMethodManagerService sometimes has not
212 // switched internal state yet here. As a quick workaround, we check the keyboard state
214 // TODO: Remove this workaround by ensuring such a race condition never happens.
215 postDelayed(new Runnable() {
218 updateSwitchImeButton();
220 }, DELAY_MILLIS_TO_REEVALUATE_IME_SWITCH_ICON);
224 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
225 // send focus to the password field
226 return mPasswordEntry.requestFocus(direction, previouslyFocusedRect);
230 protected void resetPasswordText(boolean animate, boolean announce) {
231 mPasswordEntry.setText("");
235 protected String getPasswordText() {
236 return mPasswordEntry.getText().toString();
240 protected void setPasswordEntryEnabled(boolean enabled) {
241 mPasswordEntry.setEnabled(enabled);
245 protected void setPasswordEntryInputEnabled(boolean enabled) {
246 mPasswordEntryDisabler.setInputEnabled(enabled);
250 * Method adapted from com.android.inputmethod.latin.Utils
252 * @param imm The input method manager
253 * @param shouldIncludeAuxiliarySubtypes
254 * @return true if we have multiple IMEs to choose from
256 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
257 final boolean shouldIncludeAuxiliarySubtypes) {
258 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
260 // Number of the filtered IMEs
261 int filteredImisCount = 0;
263 for (InputMethodInfo imi : enabledImis) {
264 // We can return true immediately after we find two or more filtered IMEs.
265 if (filteredImisCount > 1) return true;
266 final List<InputMethodSubtype> subtypes =
267 imm.getEnabledInputMethodSubtypeList(imi, true);
268 // IMEs that have no subtypes should be counted.
269 if (subtypes.isEmpty()) {
275 for (InputMethodSubtype subtype : subtypes) {
276 if (subtype.isAuxiliary()) {
280 final int nonAuxCount = subtypes.size() - auxCount;
282 // IMEs that have one or more non-auxiliary subtypes should be counted.
283 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
284 // subtypes should be counted as well.
285 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
291 return filteredImisCount > 1
292 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
293 // input method subtype (The current IME should be LatinIME.)
294 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
298 public void showUsabilityHint() {
302 public int getWrongPasswordStringId() {
303 return R.string.kg_wrong_password;
307 public void startAppearAnimation() {
314 .setInterpolator(mLinearOutSlowInInterpolator);
318 public boolean startDisappearAnimation(Runnable finishRunnable) {
321 .translationY(mDisappearYTranslation)
322 .setInterpolator(mFastOutLinearInInterpolator)
324 .withEndAction(finishRunnable);
329 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
330 if (mCallback != null) {
331 mCallback.userActivity();
336 public void onTextChanged(CharSequence s, int start, int before, int count) {
340 public void afterTextChanged(Editable s) {
341 // Poor man's user edit detection, assuming empty text is programmatic and everything else
343 if (!TextUtils.isEmpty(s)) {
349 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
350 // Check if this was the result of hitting the enter key
351 final boolean isSoftImeEvent = event == null
352 && (actionId == EditorInfo.IME_NULL
353 || actionId == EditorInfo.IME_ACTION_DONE
354 || actionId == EditorInfo.IME_ACTION_NEXT);
355 final boolean isKeyboardEnterKey = event != null
356 && KeyEvent.isConfirmKey(event.getKeyCode())
357 && event.getAction() == KeyEvent.ACTION_DOWN;
358 if (isSoftImeEvent || isKeyboardEnterKey) {
359 verifyPasswordAndUnlock();