Delay showSoftInput() call.

We need to wait onFocusView() call back for actually showing
software keyboard.

Bug: 3053354
Change-Id: Idd946e3b8a372ddbabb8b36600c77290f77a06e6
diff --git a/src/com/android/settings/wifi/WifiConfigUiForSetupWizardXL.java b/src/com/android/settings/wifi/WifiConfigUiForSetupWizardXL.java
index 155b691..a29790f 100644
--- a/src/com/android/settings/wifi/WifiConfigUiForSetupWizardXL.java
+++ b/src/com/android/settings/wifi/WifiConfigUiForSetupWizardXL.java
@@ -20,9 +20,11 @@
 
 import android.app.Activity;
 import android.content.Context;
+import android.os.Handler;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.View.OnFocusChangeListener;
 import android.view.ViewGroup;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.Button;
@@ -32,7 +34,7 @@
  * Shows simplified UI for configuring a wifi network. Used only in SetupWizard for XLarge
  * screen.
  */
-public class WifiConfigUiForSetupWizardXL implements WifiConfigUiBase {
+public class WifiConfigUiForSetupWizardXL implements WifiConfigUiBase, OnFocusChangeListener {
     private static final String TAG = "SetupWizard";
 
     private Button mConnectButton;
@@ -44,6 +46,7 @@
     private WifiConfigController mController;
     private AccessPoint mAccessPoint;
     private boolean mEdit;
+    private Handler mHandler = new Handler();
 
     private LayoutInflater mInflater;
 
@@ -66,25 +69,18 @@
 
         mView = mInflater.inflate(R.layout.wifi_config_ui_for_setup_wizard, parent, false);
         mController = new WifiConfigController(this, mView, mAccessPoint, edit);
-        trySetFocusAndLaunchSoftInput(R.id.password);
-    }
 
-    private void trySetFocusAndLaunchSoftInput(int id) {
-        final View viewToBeFocused = mView.findViewById(id);
-        if (viewToBeFocused != null && viewToBeFocused.getVisibility() == View.VISIBLE) {
+        // Set Focus to password View.
+        final View viewToBeFocused = mView.findViewById(R.id.password);
+        if (viewToBeFocused != null && viewToBeFocused.getVisibility() == View.VISIBLE &&
+                viewToBeFocused instanceof EditText) {
+            // After acquiring the focus, we show software keyboard.
+            viewToBeFocused.setOnFocusChangeListener(this);
             final boolean requestFocusResult = viewToBeFocused.requestFocus();
-            Log.i(TAG, String.format("Focus request to %x %s.", id,
+            Log.i(TAG, String.format("Focus request  %s.",
                     (requestFocusResult ? "successful" : "failed")));
-            if (requestFocusResult && viewToBeFocused instanceof EditText) {
-                Log.i(TAG, String.format(
-                        "Focused View (%x) is EditText. Try to show software keyboard", id));
-                final InputMethodManager inputMethodManager = (InputMethodManager)
-                        mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
-                final boolean showSoftInputResult =
-                        inputMethodManager.showSoftInput(viewToBeFocused, 0);
-                if (!showSoftInputResult) {
-                    Log.w(TAG, "Failed to show software keyboard ");
-                }
+            if (!requestFocusResult) {
+                viewToBeFocused.setOnFocusChangeListener(null);
             }
         }
     }
@@ -164,4 +160,32 @@
     public void setTitle(CharSequence title) {
         Log.d(TAG, "Ignoring setTitle");
     }
+
+    private static class FocusRunnable implements Runnable {
+        final InputMethodManager mInputMethodManager;
+        final View mViewToBeFocused;
+        public FocusRunnable(Context context, View viewToBeFocused) {
+            mInputMethodManager = (InputMethodManager)
+                    context.getSystemService(Context.INPUT_METHOD_SERVICE);
+            mViewToBeFocused = viewToBeFocused;
+        }
+
+        @Override
+        public void run() {
+            mInputMethodManager.focusIn(mViewToBeFocused);
+            final boolean showSoftInputResult =
+                    mInputMethodManager.showSoftInput(mViewToBeFocused, 0);
+            if (!showSoftInputResult) {
+                Log.w(TAG, "Failed to show software keyboard ");
+            }
+        }
+    }
+
+    @Override
+    public void onFocusChange(View view, boolean hasFocus) {
+        view.setOnFocusChangeListener(null);
+        if (hasFocus) {
+            mHandler.post(new FocusRunnable(mActivity, view));
+        }
+    }
 }
\ No newline at end of file
diff --git a/src/com/android/settings/wifi/WifiSettingsForSetupWizardXL.java b/src/com/android/settings/wifi/WifiSettingsForSetupWizardXL.java
index 635f301..f59816e 100644
--- a/src/com/android/settings/wifi/WifiSettingsForSetupWizardXL.java
+++ b/src/com/android/settings/wifi/WifiSettingsForSetupWizardXL.java
@@ -111,7 +111,7 @@
         setContentView(R.layout.wifi_settings_for_setup_wizard_xl);
 
         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
-        
+
         mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
         // There's no button here enabling wifi network, so we need to enable it without
         // users' request.
@@ -188,6 +188,7 @@
     }
 
     private void hideSoftwareKeyboard() {
+        Log.i(TAG, "Hiding software keyboard.");
         final View focusedView = getCurrentFocus();
         if (focusedView != null) {
             mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);