Do not show soft keyboard when locked out

- Remove stateVisible for ConfirmLockPassword activity so that
  soft keyboard does not auto show up when the activity starts;
- Programatically show/hide soft keyboard when ConfirmLockPassword
  activity window gains focus;

Bug:20542149
Change-Id: I3faa20312d2d25752bd35b70dc8ed785e7687f0a
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 6fd534c..d8d1736 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -1407,7 +1407,7 @@
             android:theme="@style/Theme.ConfirmDeviceCredentials"/>
 
         <activity android:name="ConfirmLockPassword"
-            android:windowSoftInputMode="stateVisible|adjustResize"
+            android:windowSoftInputMode="adjustResize"
             android:theme="@style/Theme.ConfirmDeviceCredentials"/>
 
         <activity android:name=".fingerprint.FingerprintSettings" android:exported="false"/>
@@ -1424,7 +1424,7 @@
         <!-- Note this must not be exported since it returns the password in the intent -->
         <activity android:name="ConfirmLockPassword$InternalActivity"
             android:exported="false"
-            android:windowSoftInputMode="stateVisible|adjustResize"
+            android:windowSoftInputMode="adjustResize"
             android:theme="@style/Theme.ConfirmDeviceCredentials"/>
 
         <activity android:name="SetupChooseLockGeneric"
diff --git a/src/com/android/settings/ConfirmLockPassword.java b/src/com/android/settings/ConfirmLockPassword.java
index c1d8adb..8ae2824 100644
--- a/src/com/android/settings/ConfirmLockPassword.java
+++ b/src/com/android/settings/ConfirmLockPassword.java
@@ -16,7 +16,6 @@
 
 package com.android.settings;
 
-import android.annotation.Nullable;
 import android.os.UserHandle;
 import android.text.TextUtils;
 import com.android.internal.logging.MetricsLogger;
@@ -24,7 +23,9 @@
 import com.android.internal.widget.LockPatternUtils;
 import com.android.internal.widget.TextViewInputDisabler;
 
+import android.app.Fragment;
 import android.app.admin.DevicePolicyManager;
+import android.content.Context;
 import android.content.Intent;
 import android.os.AsyncTask;
 import android.os.Bundle;
@@ -35,11 +36,11 @@
 import android.text.InputType;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
-import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputMethodManager;
 import android.widget.TextView;
 import android.widget.TextView.OnEditorActionListener;
 
@@ -61,6 +62,15 @@
         return false;
     }
 
+    @Override
+    public void onWindowFocusChanged(boolean hasFocus) {
+        super.onWindowFocusChanged(hasFocus);
+        Fragment fragment = getFragmentManager().findFragmentById(R.id.main_content);
+        if (fragment != null && fragment instanceof ConfirmLockPasswordFragment) {
+            ((ConfirmLockPasswordFragment)fragment).onWindowFocusChanged(hasFocus);
+        }
+    }
+
     public static class ConfirmLockPasswordFragment extends ConfirmDeviceCredentialBaseFragment
             implements OnClickListener, OnEditorActionListener {
         private static final String KEY_NUM_WRONG_CONFIRM_ATTEMPTS
@@ -77,6 +87,7 @@
         private int mNumWrongConfirmAttempts;
         private CountDownTimer mCountdownTimer;
         private boolean mIsAlpha;
+        private InputMethodManager mImm;
 
         // required constructor for fragments
         public ConfirmLockPasswordFragment() {
@@ -111,6 +122,9 @@
                     || DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC == storedQuality
                     || DevicePolicyManager.PASSWORD_QUALITY_COMPLEX == storedQuality;
 
+            mImm = (InputMethodManager) getActivity().getSystemService(
+                    Context.INPUT_METHOD_SERVICE);
+
             Intent intent = getActivity().getIntent();
             if (intent != null) {
                 CharSequence headerMessage = intent.getCharSequenceExtra(
@@ -172,7 +186,7 @@
             if (deadline != 0) {
                 handleAttemptLockout(deadline);
             } else {
-                mPasswordEntryInputDisabler.setInputEnabled(true);
+                resetState();
             }
         }
 
@@ -189,6 +203,31 @@
             getActivity().finish();
         }
 
+        private void resetState() {
+            mPasswordEntry.setEnabled(true);
+            mPasswordEntryInputDisabler.setInputEnabled(true);
+            mImm.showSoftInput(mPasswordEntry, InputMethodManager.SHOW_IMPLICIT);
+        }
+
+        public void onWindowFocusChanged(boolean hasFocus) {
+            if (!hasFocus) {
+                return;
+            }
+            // Post to let window focus logic to finish to allow soft input show/hide properly.
+            mPasswordEntry.post(new Runnable() {
+                @Override
+                public void run() {
+                    if (mPasswordEntry.isEnabled()) {
+                        resetState();
+                        return;
+                    }
+
+                    mImm.hideSoftInputFromWindow(mPasswordEntry.getWindowToken(),
+                            InputMethodManager.HIDE_IMPLICIT_ONLY);
+                }
+            });
+        }
+
         private void handleNext() {
             mPasswordEntryInputDisabler.setInputEnabled(false);
             if (mPendingLockCheck != null) {
@@ -279,7 +318,7 @@
 
         private void handleAttemptLockout(long elapsedRealtimeDeadline) {
             long elapsedRealtime = SystemClock.elapsedRealtime();
-            mPasswordEntryInputDisabler.setInputEnabled(false);
+            mPasswordEntry.setEnabled(false);
             mCountdownTimer = new CountDownTimer(
                     elapsedRealtimeDeadline - elapsedRealtime,
                     LockPatternUtils.FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS) {
@@ -294,7 +333,7 @@
 
                 @Override
                 public void onFinish() {
-                    mPasswordEntryInputDisabler.setInputEnabled(true);
+                    resetState();
                     mErrorTextView.setText("");
                     mNumWrongConfirmAttempts = 0;
                 }