[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
Change-Id: Idede4233a7385d3bcd8fd6614948270280536bf1
This commit is contained in:
Maurice Lam
2015-08-27 16:06:55 -07:00
parent e3be8eb3e5
commit 2662df8433
5 changed files with 95 additions and 40 deletions

View File

@@ -18,7 +18,6 @@ package com.android.settings.wifi;
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 @@ import java.util.List;
* 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,14 +169,19 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
}
@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;
}
}
@Override
public void onSubmit(WifiDialog dialog) {
// Ignored
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
if (preference instanceof AccessPointPreference) {

View File

@@ -42,8 +42,10 @@ import android.text.InputType;
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 @@ import java.util.Iterator;
* 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,32 +347,32 @@ public class WifiConfigController implements TextWatcher,
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();
}
if (mEapCaCertSpinner != null &&
mView.findViewById(R.id.l_ca_cert).getVisibility() != View.GONE &&
((String)mEapCaCertSpinner.getSelectedItem()).equals(unspecifiedCert)) {
enabled = false;
}
submit.setEnabled(enabled);
return enabled;
}
/* package */ WifiConfiguration getConfig() {
@@ -632,6 +635,8 @@ public class WifiConfigController implements TextWatcher,
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);
@@ -988,6 +993,28 @@ public class WifiConfigController implements TextWatcher,
// work done in afterTextChanged
}
@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) {

View File

@@ -29,6 +29,8 @@ public interface WifiConfigUiBase {
public LayoutInflater getLayoutInflater();
public boolean isEdit();
public void dispatchSubmit();
public void setTitle(int id);
public void setTitle(CharSequence title);

View File

@@ -26,13 +26,19 @@ import android.os.Bundle;
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 @@ class WifiDialog extends AlertDialog implements WifiConfigUiBase {
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 @@ class WifiDialog extends AlertDialog implements WifiConfigUiBase {
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;
@@ -90,6 +96,28 @@ class WifiDialog extends AlertDialog implements WifiConfigUiBase {
mController.updatePassword();
}
@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 @@ class WifiDialog extends AlertDialog implements WifiConfigUiBase {
@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);
}
}

View File

@@ -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.HandlerThread;
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.TextView.BufferType;
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 @@ import java.util.List;
* 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 @@ public class WifiSettings extends RestrictedSettingsFragment
}
@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 @@ public class WifiSettings extends RestrictedSettingsFragment
Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0) != 0;
return !isLockdownFeatureEnabled;
}
}