Fix UI for WifiSettings for XL screen

Change-Id: I874e1e8d86e8b219414f6bf2ff945ea4f9ab7a6c
diff --git a/res/layout-xlarge/wifi_settings_for_setup_wizard_xl.xml b/res/layout-xlarge/wifi_settings_for_setup_wizard_xl.xml
index a6a8e8a..6aa825a 100644
--- a/res/layout-xlarge/wifi_settings_for_setup_wizard_xl.xml
+++ b/res/layout-xlarge/wifi_settings_for_setup_wizard_xl.xml
@@ -93,12 +93,6 @@
         android:layout_alignParentRight="true"
         android:layout_alignParentBottom="true">
 
-        <Button android:id="@+id/wifi_setup_connect"
-                android:layout_width="260dip"
-                android:layout_height="60dip"
-                android:textSize="24dip"
-                android:text="@string/wifi_connect"
-                android:visibility="gone" />
         <Button android:id="@+id/wifi_setup_forget"
                 android:layout_width="260dip"
                 android:layout_height="60dip"
@@ -106,6 +100,14 @@
                 android:text="@string/wifi_forget"
                 android:visibility="gone" />
 
+        <Button android:id="@+id/wifi_setup_connect"
+                android:layout_width="260dip"
+                android:layout_height="60dip"
+                android:layout_marginTop="20dip"
+                android:textSize="24dip"
+                android:text="@string/wifi_connect"
+                android:visibility="gone" />
+
         <Button android:id="@+id/wifi_setup_add_network"
                 android:layout_width="260dip"
                 android:layout_height="60dip"
@@ -134,7 +136,6 @@
             android:layout_height="60dip"
             android:layout_above="@+id/wifi_setup_cancel"
             android:layout_alignLeft="@+id/wifi_setup_cancel"
-            android:layout_marginBottom="30dip"
             android:textSize="24dip"
             android:text="@string/wifi_detail"
             android:visibility="gone" />
@@ -142,6 +143,7 @@
     <Button android:id="@+id/wifi_setup_cancel"
             android:layout_width="260dip"
             android:layout_height="60dip"
+            android:layout_marginTop="20dip"
             android:layout_alignParentBottom="true"
             android:layout_toLeftOf="@id/right_buttons"
             android:textSize="24dip"
diff --git a/res/layout/wifi_config_preference2.xml b/res/layout/wifi_config_preference2.xml
index 29cb585..bbda2f9 100644
--- a/res/layout/wifi_config_preference2.xml
+++ b/res/layout/wifi_config_preference2.xml
@@ -56,7 +56,32 @@
                   android:layout_height="wrap_content"
                   android:text="@string/wifi_show_password" />
     </LinearLayout>
+    <LinearLayout android:id="@+id/type"
+                 android:layout_width="fill_parent"
+                 android:layout_height="wrap_content"
+                 android:orientation="vertical"
+                 android:visibility="gone">
 
+       <TextView android:layout_width="fill_parent"
+                android:layout_height="wrap_content"
+                 android:text="@string/wifi_ssid" />
+
+       <EditText android:id="@+id/ssid"
+                 android:layout_width="fill_parent"
+                 android:layout_height="wrap_content"
+                 android:singleLine="true"
+                 android:inputType="textNoSuggestions" />
+
+       <TextView android:layout_width="fill_parent"
+                 android:layout_height="wrap_content"
+                 android:text="@string/wifi_security" />
+
+       <Spinner android:id="@+id/security"
+                android:layout_width="fill_parent"
+                android:layout_height="wrap_content"
+                android:prompt="@string/wifi_security"
+                android:entries="@array/wifi_security" />
+    </LinearLayout> <!-- android:id="@+id/type" -->
 
     <!-- All the views below are "gone".
          We want them as data storage, not as UI components. -->
@@ -109,33 +134,6 @@
                   android:inputType="textPassword" />
     </LinearLayout> -->
 
-    <LinearLayout android:id="@+id/type"
-                 android:layout_width="fill_parent"
-                 android:layout_height="wrap_content"
-                 android:orientation="vertical"
-                 android:visibility="gone">
-
-       <TextView android:layout_width="fill_parent"
-                android:layout_height="wrap_content"
-                 android:text="@string/wifi_ssid" />
-
-       <EditText android:id="@+id/ssid"
-                 android:layout_width="fill_parent"
-                 android:layout_height="wrap_content"
-                 android:singleLine="true"
-                 android:inputType="textNoSuggestions" />
-
-       <TextView android:layout_width="fill_parent"
-                 android:layout_height="wrap_content"
-                 android:text="@string/wifi_security" />
-
-       <Spinner android:id="@+id/security"
-                android:layout_width="fill_parent"
-                android:layout_height="wrap_content"
-                android:prompt="@string/wifi_security"
-                android:entries="@array/wifi_security" />
-    </LinearLayout> <!-- android:id="@+id/type" -->
-
     <!--  android:id="@+id/security_fields" -->
     <LinearLayout
                   android:layout_width="fill_parent"
diff --git a/src/com/android/settings/wifi/WifiConfigPreference.java b/src/com/android/settings/wifi/WifiConfigPreference.java
index c495481..0e7af51 100644
--- a/src/com/android/settings/wifi/WifiConfigPreference.java
+++ b/src/com/android/settings/wifi/WifiConfigPreference.java
@@ -21,16 +21,21 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.preference.Preference;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.inputmethod.InputMethodManager;
 import android.widget.Button;
+import android.widget.EditText;
 
 /**
  * Preference letting users modify a setting for Wifi network. This work as an alternative UI
  * for {@link WifiDialog} without shouwing popup Dialog.
  */
 public class WifiConfigPreference extends Preference implements WifiConfigUiBase {
+    private static final String TAG = "WifiConfigPreference";
+
     private WifiSettings mWifiSettings;
     private View mView;
     private final DialogInterface.OnClickListener mListener;
@@ -38,6 +43,9 @@
     private AccessPoint mAccessPoint;
     private boolean mEdit;
 
+    // Stores an View id to be focused. Used when view isn't available while setFocus() is called.
+    private int mFocusId = -1;
+
     private LayoutInflater mInflater;
 
     public WifiConfigPreference(WifiSettings wifiSettings,
@@ -57,12 +65,16 @@
     @Override
     protected View onCreateView(ViewGroup parent) {
         // Called every time the list is created.
-        if (mView != null) {
-            // TODO: we need to re-forcus something.
-            return mView;
+        if (mView == null) {
+            mView = mInflater.inflate(getLayoutResource(), parent, false);
+            mController = new WifiConfigController(this, mView, mAccessPoint, mEdit, mListener);
         }
-        mView = mInflater.inflate(getLayoutResource(), parent, false);
-        mController = new WifiConfigController(this, mView, mAccessPoint, mEdit, mListener);
+
+        if (mFocusId >= 0) {
+            trySetFocusAndLaunchSoftInput(mFocusId);
+            mFocusId = -1;
+        }
+
         return mView;
     }
 
@@ -71,6 +83,31 @@
         return mController;
     }
 
+    public void setFocus(int id) {
+        if (mView != null) {
+            trySetFocusAndLaunchSoftInput(id);
+            mFocusId = -1;
+        } else {
+            mFocusId = id;
+        }
+    }
+
+    private void trySetFocusAndLaunchSoftInput(int id) {
+        final View viewToBeFocused = mView.findViewById(id);
+        if (viewToBeFocused != null && viewToBeFocused.getVisibility() == View.VISIBLE) {
+            viewToBeFocused.requestFocus();
+            // TODO: doesn't work.
+            if (viewToBeFocused instanceof EditText) {
+                Log.d(TAG, "Focused View is EditText. We try showing the software keyboard");
+                // viewToBeFocused.performClick();
+                final InputMethodManager inputMethodManager =
+                        (InputMethodManager)
+                                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+                inputMethodManager.showSoftInput(viewToBeFocused, 0);
+            }
+        }
+    }
+
     public View findViewById(int id) {
         return mView.findViewById(id);
     }
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index efafd59..bc1f09d 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -47,7 +47,6 @@
 import android.provider.Settings.Secure;
 import android.security.Credentials;
 import android.security.KeyStore;
-import android.util.Log;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.Menu;
@@ -639,6 +638,10 @@
             forget();
         } else if (button == WifiDialog.BUTTON_SUBMIT) {
             submit();
+            final Activity activity = getActivity();
+            if (activity instanceof WifiSettingsForSetupWizardXL) {
+                ((WifiSettingsForSetupWizardXL)activity).onConnectButtonPressed();
+            }
         }
     }
 
@@ -740,6 +743,11 @@
     /* package */ void onAddNetworkPressed() {
         mSelectedAccessPoint = null;
         showConfigUi(null, true);
+
+        // Set focus to the EditText the user needs to configure.
+        if (mConfigPreference != null) {
+            mConfigPreference.setFocus(R.id.ssid);
+        }
     }
 
     /* package */ int getAccessPointsCount() {
diff --git a/src/com/android/settings/wifi/WifiSettingsForSetupWizardXL.java b/src/com/android/settings/wifi/WifiSettingsForSetupWizardXL.java
index fde5d90..1d11986 100644
--- a/src/com/android/settings/wifi/WifiSettingsForSetupWizardXL.java
+++ b/src/com/android/settings/wifi/WifiSettingsForSetupWizardXL.java
@@ -23,6 +23,7 @@
 import android.net.NetworkInfo.DetailedState;
 import android.os.Bundle;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.Window;
@@ -129,14 +130,7 @@
             break;
         case R.id.wifi_setup_connect:
             mWifiSettings.submit();
-
-            // updateConnectionState() isn't called soon after the user's "connect" action,
-            // and the user still sees "not connected" message for a while, which looks strange.
-            // We instead manually show "connecting" message before the system gets actual
-            // "connecting" message from Wi-Fi module.
-            showConnectingStatus();
-            mShowingConnectingMessageManually = true;
-            mIgnoringWifiNotificationCount = 2;
+            onConnectButtonPressed();
             break;
         case R.id.wifi_setup_forget:
             mWifiSettings.forget();
@@ -186,7 +180,10 @@
             mProgressBar.setProgress(2);
             mProgressText.setText(Summary.get(this, state));
             mStatusText.setText(R.string.wifi_setup_status_proceed_to_next);
-            enableButtons();
+            // We don't want "Add network" button here. User can press it after pressing
+            // "Refresh" button.
+            ((Button)findViewById(R.id.wifi_setup_refresh_list)).setEnabled(true);
+            ((Button)findViewById(R.id.wifi_setup_skip_or_next)).setEnabled(true);
 
             if (mIgnoringWifiNotificationCount > 0) {
                 // The network is already available before doing anything. We avoid skip this
@@ -219,7 +216,6 @@
                 mShowingConnectingMessageManually = false;
                 mProgressBar.setIndeterminate(false);
                 mProgressBar.setProgress(0);
-                mStatusText.setText(R.string.wifi_setup_status_select_network);
                 mProgressText.setText(getString(R.string.wifi_setup_not_connected));
                 enableButtons();
             }
@@ -254,7 +250,21 @@
     public void onRefreshAccessPoints() {
         mIgnoringWifiNotificationCount = 5;
         mProgressBar.setIndeterminate(true);
+        ((Button)findViewById(R.id.wifi_setup_add_network)).setEnabled(false);
+        ((Button)findViewById(R.id.wifi_setup_refresh_list)).setEnabled(false);
         mProgressText.setText(Summary.get(this, DetailedState.SCANNING));
         mStatusText.setText(R.string.wifi_setup_status_scanning);
     }
+
+    /* package */ void onConnectButtonPressed() {
+        // updateConnectionState() isn't called soon after the user's "connect" action,
+        // and the user still sees "not connected" message for a while, which looks strange.
+        // We instead manually show "connecting" message before the system gets actual
+        // "connecting" message from Wi-Fi module.
+        showConnectingStatus();
+        ((Button)findViewById(R.id.wifi_setup_add_network)).setEnabled(false);
+        ((Button)findViewById(R.id.wifi_setup_refresh_list)).setEnabled(false);
+        mShowingConnectingMessageManually = true;
+        mIgnoringWifiNotificationCount = 2;
+    }
 }