[WifiSettings] Submit Wi-Fi dialog with enter key

Add a OnKeyListener for hardware keyboards and an
OnEditorActionListener for on-screen IMEs to submit the Wi-Fi dialog
with the currently entered information.

Before committing the action, check for whether the dialog is
submittable -- that is checking whether all necessary information has
been entered. If not the enter key behaves the same as it is before
the change.

Bug: 22211604
Bug: 25357079
Change-Id: Ia332435bc96414db9c6835612bc4c7d29c7c3669
Original-Change-Id: Idede4233a7385d3bcd8fd6614948270280536bf1
[cernekee: backported from master]
diff --git a/src/com/android/settings/wifi/SavedAccessPointsWifiSettings.java b/src/com/android/settings/wifi/SavedAccessPointsWifiSettings.java
index 45aafdf..52cb509 100644
--- a/src/com/android/settings/wifi/SavedAccessPointsWifiSettings.java
+++ b/src/com/android/settings/wifi/SavedAccessPointsWifiSettings.java
@@ -18,7 +18,6 @@
 
 import android.app.Dialog;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.content.res.Resources;
 import android.net.wifi.WifiManager;
 import android.os.Bundle;
@@ -45,7 +44,7 @@
  * UI to manage saved networks/access points.
  */
 public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
-        implements DialogInterface.OnClickListener, Indexable {
+        implements Indexable, WifiDialog.WifiDialogListener {
     private static final String TAG = "SavedAccessPointsWifiSettings";
 
     private WifiDialog mDialog;
@@ -170,8 +169,8 @@
     }
 
     @Override
-    public void onClick(DialogInterface dialogInterface, int button) {
-        if (button == WifiDialog.BUTTON_FORGET && mSelectedAccessPoint != null) {
+    public void onForget(WifiDialog dialog) {
+        if (mSelectedAccessPoint != null) {
             mWifiManager.forget(mSelectedAccessPoint.getConfig().networkId, null);
             getPreferenceScreen().removePreference((Preference) mSelectedAccessPoint.getTag());
             mSelectedAccessPoint = null;
@@ -179,6 +178,11 @@
     }
 
     @Override
+    public void onSubmit(WifiDialog dialog) {
+        // Ignored
+    }
+
+    @Override
     public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
         if (preference instanceof AccessPointPreference) {
             showDialog((AccessPointPreference) preference, false);
diff --git a/src/com/android/settings/wifi/WifiConfigController.java b/src/com/android/settings/wifi/WifiConfigController.java
index 774c54b..69b1505 100644
--- a/src/com/android/settings/wifi/WifiConfigController.java
+++ b/src/com/android/settings/wifi/WifiConfigController.java
@@ -42,8 +42,10 @@
 import android.text.TextWatcher;
 import android.text.TextUtils;
 import android.util.Log;
+import android.view.KeyEvent;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.inputmethod.EditorInfo;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
@@ -68,7 +70,8 @@
  * share the logic for controlling buttons, text fields, etc.
  */
 public class WifiConfigController implements TextWatcher,
-       AdapterView.OnItemSelectedListener, OnCheckedChangeListener {
+        AdapterView.OnItemSelectedListener, OnCheckedChangeListener,
+        TextView.OnEditorActionListener, View.OnKeyListener{
     private static final String TAG = "WifiConfigController";
 
     private final WifiConfigUiBase mConfigUi;
@@ -344,27 +347,27 @@
         Button submit = mConfigUi.getSubmitButton();
         if (submit == null) return;
 
+        submit.setEnabled(isSubmittable());
+    }
+
+    boolean isSubmittable() {
         boolean enabled = false;
         boolean passwordInvalid = false;
 
         if (mPasswordView != null &&
-            ((mAccessPointSecurity == AccessPoint.SECURITY_WEP && mPasswordView.length() == 0) ||
-            (mAccessPointSecurity == AccessPoint.SECURITY_PSK && mPasswordView.length() < 8))) {
+                ((mAccessPointSecurity == AccessPoint.SECURITY_WEP && mPasswordView.length() == 0) ||
+                        (mAccessPointSecurity == AccessPoint.SECURITY_PSK && mPasswordView.length() < 8))) {
             passwordInvalid = true;
         }
 
         if ((mSsidView != null && mSsidView.length() == 0) ||
-            ((mAccessPoint == null || !mAccessPoint.isSaved()) &&
-            passwordInvalid)) {
+                ((mAccessPoint == null || !mAccessPoint.isSaved()) &&
+                        passwordInvalid)) {
             enabled = false;
         } else {
-            if (ipAndProxyFieldsAreValid()) {
-                enabled = true;
-            } else {
-                enabled = false;
-            }
+            enabled = ipAndProxyFieldsAreValid();
         }
-        submit.setEnabled(enabled);
+        return enabled;
     }
 
     /* package */ WifiConfiguration getConfig() {
@@ -627,6 +630,8 @@
         if (mPasswordView == null) {
             mPasswordView = (TextView) mView.findViewById(R.id.password);
             mPasswordView.addTextChangedListener(this);
+            mPasswordView.setOnEditorActionListener(this);
+            mPasswordView.setOnKeyListener(this);
             ((CheckBox) mView.findViewById(R.id.show_password))
                 .setOnCheckedChangeListener(this);
 
@@ -983,6 +988,28 @@
     }
 
     @Override
+    public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
+        if (textView == mPasswordView) {
+            if (id == EditorInfo.IME_ACTION_DONE && isSubmittable()) {
+                mConfigUi.dispatchSubmit();
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
+        if (view == mPasswordView) {
+            if (keyCode == KeyEvent.KEYCODE_ENTER && isSubmittable()) {
+                mConfigUi.dispatchSubmit();
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
     public void onCheckedChanged(CompoundButton view, boolean isChecked) {
         if (view.getId() == R.id.show_password) {
             int pos = mPasswordView.getSelectionEnd();
diff --git a/src/com/android/settings/wifi/WifiConfigUiBase.java b/src/com/android/settings/wifi/WifiConfigUiBase.java
index a0f67ab..d6ecf02 100644
--- a/src/com/android/settings/wifi/WifiConfigUiBase.java
+++ b/src/com/android/settings/wifi/WifiConfigUiBase.java
@@ -29,6 +29,8 @@
     public LayoutInflater getLayoutInflater();
     public boolean isEdit();
 
+    public void dispatchSubmit();
+
     public void setTitle(int id);
     public void setTitle(CharSequence title);
 
diff --git a/src/com/android/settings/wifi/WifiDialog.java b/src/com/android/settings/wifi/WifiDialog.java
index d052c95..42f93bd 100644
--- a/src/com/android/settings/wifi/WifiDialog.java
+++ b/src/com/android/settings/wifi/WifiDialog.java
@@ -26,13 +26,19 @@
 import android.view.View;
 import android.widget.Button;
 
-class WifiDialog extends AlertDialog implements WifiConfigUiBase {
-    static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
-    static final int BUTTON_FORGET = DialogInterface.BUTTON_NEUTRAL;
+class WifiDialog extends AlertDialog implements WifiConfigUiBase, DialogInterface.OnClickListener {
+
+    public interface WifiDialogListener {
+        void onForget(WifiDialog dialog);
+        void onSubmit(WifiDialog dialog);
+    }
+
+    private static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
+    private static final int BUTTON_FORGET = DialogInterface.BUTTON_NEUTRAL;
 
     private final boolean mEdit;
     private final boolean mModify;
-    private final DialogInterface.OnClickListener mListener;
+    private final WifiDialogListener mListener;
     private final AccessPoint mAccessPoint;
 
     private View mView;
@@ -40,7 +46,7 @@
     private boolean mHideSubmitButton;
     private boolean mHideForgetButton;
 
-    public WifiDialog(Context context, DialogInterface.OnClickListener listener,
+    public WifiDialog(Context context, WifiDialogListener listener,
             AccessPoint accessPoint, boolean edit, boolean modify,
             boolean hideSubmitButton, boolean hideForgetButton) {
         this(context, listener, accessPoint, edit, modify);
@@ -48,7 +54,7 @@
         mHideForgetButton = hideForgetButton;
     }
 
-    public WifiDialog(Context context, DialogInterface.OnClickListener listener,
+    public WifiDialog(Context context, WifiDialogListener listener,
             AccessPoint accessPoint, boolean edit, boolean modify) {
         super(context);
         mEdit = edit;
@@ -91,6 +97,28 @@
     }
 
     @Override
+    public void dispatchSubmit() {
+        if (mListener != null) {
+            mListener.onSubmit(this);
+        }
+        dismiss();
+    }
+
+    @Override
+    public void onClick(DialogInterface dialogInterface, int id) {
+        if (mListener != null) {
+            switch (id) {
+                case BUTTON_SUBMIT:
+                    mListener.onSubmit(this);
+                    break;
+                case BUTTON_FORGET:
+                    mListener.onForget(this);
+                    break;
+            }
+        }
+    }
+
+    @Override
     public boolean isEdit() {
         return mEdit;
     }
@@ -112,16 +140,16 @@
 
     @Override
     public void setSubmitButton(CharSequence text) {
-        setButton(BUTTON_SUBMIT, text, mListener);
+        setButton(BUTTON_SUBMIT, text, this);
     }
 
     @Override
     public void setForgetButton(CharSequence text) {
-        setButton(BUTTON_FORGET, text, mListener);
+        setButton(BUTTON_FORGET, text, this);
     }
 
     @Override
     public void setCancelButton(CharSequence text) {
-        setButton(BUTTON_NEGATIVE, text, mListener);
+        setButton(BUTTON_NEGATIVE, text, this);
     }
 }
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index 7eded24..8863d4e 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -16,19 +16,15 @@
 
 package com.android.settings.wifi;
 
-import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID;
 import static android.os.UserManager.DISALLOW_CONFIG_WIFI;
 
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.AppGlobals;
 import android.app.Dialog;
-import android.app.admin.DeviceAdminInfo;
 import android.app.admin.DevicePolicyManager;
-import android.app.admin.DevicePolicyManagerInternal;
 import android.content.ContentResolver;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.IPackageManager;
@@ -48,7 +44,6 @@
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.UserHandle;
-import android.os.UserManager;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
 import android.provider.Settings;
@@ -69,7 +64,6 @@
 import android.widget.Toast;
 
 import com.android.internal.logging.MetricsLogger;
-import com.android.server.LocalServices;
 import com.android.settings.LinkifyUtils;
 import com.android.settings.R;
 import com.android.settings.RestrictedSettingsFragment;
@@ -96,8 +90,8 @@
  * and menus.
  */
 public class WifiSettings extends RestrictedSettingsFragment
-        implements DialogInterface.OnClickListener, Indexable, WifiTracker.WifiListener,
-        AccessPointListener {
+        implements Indexable, WifiTracker.WifiListener, AccessPointListener,
+        WifiDialog.WifiDialogListener {
 
     private static final String TAG = "WifiSettings";
 
@@ -816,13 +810,14 @@
     }
 
     @Override
-    public void onClick(DialogInterface dialogInterface, int button) {
-        if (button == WifiDialog.BUTTON_FORGET && mSelectedAccessPoint != null) {
-            forget();
-        } else if (button == WifiDialog.BUTTON_SUBMIT) {
-            if (mDialog != null) {
-                submit(mDialog.getController());
-            }
+    public void onForget(WifiDialog dialog) {
+        forget();
+    }
+
+    @Override
+    public void onSubmit(WifiDialog dialog) {
+        if (mDialog != null) {
+            submit(mDialog.getController());
         }
     }
 
@@ -1020,5 +1015,4 @@
                 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0) != 0;
         return !isLockdownFeatureEnabled;
     }
-
 }