Avoid dialog dismiss until IP and proxy is valid

Accepting an invalid configuration can lead to
unexpected behavior.

Validate all fields before dismissing dialog. A further
improvement to this can be highlighting the faulty field
to the user by a color change.

Bug: 3462049
Change-Id: I0d0a6eae1d62b16fbb822261c4536c4108a5866d
This commit is contained in:
Irfan Sheriff
2011-03-03 11:39:00 -08:00
parent 216a58c882
commit 5640ccff74

View File

@@ -47,10 +47,10 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.android.settings.ProxySelector; import com.android.settings.ProxySelector;
import com.android.settings.R; import com.android.settings.R;
@@ -258,16 +258,27 @@ public class WifiConfigController implements TextWatcher,
group.addView(row); group.addView(row);
} }
/* show submit button if the password is valid */ /* show submit button if password, ip and proxy settings are valid */
private void enableSubmitIfAppropriate() { private void enableSubmitIfAppropriate() {
Button submit = mConfigUi.getSubmitButton();
if (submit == null) return;
boolean enabled = false;
if ((mSsidView != null && mSsidView.length() == 0) || if ((mSsidView != null && mSsidView.length() == 0) ||
((mAccessPoint == null || mAccessPoint.networkId == INVALID_NETWORK_ID) && ((mAccessPoint == null || mAccessPoint.networkId == INVALID_NETWORK_ID) &&
((mAccessPointSecurity == AccessPoint.SECURITY_WEP && mPasswordView.length() == 0) || ((mAccessPointSecurity == AccessPoint.SECURITY_WEP && mPasswordView.length() == 0) ||
(mAccessPointSecurity == AccessPoint.SECURITY_PSK && mPasswordView.length() < 8)))) { (mAccessPointSecurity == AccessPoint.SECURITY_PSK && mPasswordView.length() < 8)))) {
mConfigUi.getSubmitButton().setEnabled(false); enabled = false;
} else { } else {
mConfigUi.getSubmitButton().setEnabled(true); enabled = true;
} }
if (ipAndProxyFieldsAreValid()) {
enabled = true;
} else {
enabled = false;
}
submit.setEnabled(enabled);
} }
/* package */ WifiConfiguration getConfig() { /* package */ WifiConfiguration getConfig() {
@@ -352,8 +363,6 @@ public class WifiConfigController implements TextWatcher,
return null; return null;
} }
validateAndFetchIpAndProxyFields();
config.proxySettings = mProxySettings; config.proxySettings = mProxySettings;
config.ipAssignment = mIpAssignment; config.ipAssignment = mIpAssignment;
config.linkProperties = new LinkProperties(mLinkProperties); config.linkProperties = new LinkProperties(mLinkProperties);
@@ -361,20 +370,16 @@ public class WifiConfigController implements TextWatcher,
return config; return config;
} }
private void validateAndFetchIpAndProxyFields() { private boolean ipAndProxyFieldsAreValid() {
mLinkProperties.clear(); mLinkProperties.clear();
mIpAssignment = (mIpSettingsSpinner != null && mIpAssignment = (mIpSettingsSpinner != null &&
mIpSettingsSpinner.getSelectedItemPosition() == STATIC_IP) ? mIpSettingsSpinner.getSelectedItemPosition() == STATIC_IP) ?
IpAssignment.STATIC : IpAssignment.DHCP; IpAssignment.STATIC : IpAssignment.DHCP;
if (mIpAssignment == IpAssignment.STATIC) { if (mIpAssignment == IpAssignment.STATIC) {
//TODO: A better way to do this is to not dismiss the
//dialog as long as one of the fields is invalid
int result = validateIpConfigFields(mLinkProperties); int result = validateIpConfigFields(mLinkProperties);
if (result != 0) { if (result != 0) {
mLinkProperties.clear(); return false;
Toast.makeText(mConfigUi.getContext(), result, Toast.LENGTH_LONG).show();
mIpAssignment = IpAssignment.UNASSIGNED;
} }
} }
@@ -398,10 +403,10 @@ public class WifiConfigController implements TextWatcher,
ProxyProperties proxyProperties= new ProxyProperties(host, port, exclusionList); ProxyProperties proxyProperties= new ProxyProperties(host, port, exclusionList);
mLinkProperties.setHttpProxy(proxyProperties); mLinkProperties.setHttpProxy(proxyProperties);
} else { } else {
Toast.makeText(mConfigUi.getContext(), result, Toast.LENGTH_LONG).show(); return false;
mProxySettings = ProxySettings.UNASSIGNED;
} }
} }
return true;
} }
private int validateIpConfigFields(LinkProperties linkProperties) { private int validateIpConfigFields(LinkProperties linkProperties) {
@@ -478,8 +483,6 @@ public class WifiConfigController implements TextWatcher,
config.pin = ((TextView) mView.findViewById(R.id.wps_pin)).getText().toString(); config.pin = ((TextView) mView.findViewById(R.id.wps_pin)).getText().toString();
config.BSSID = (mAccessPoint != null) ? mAccessPoint.bssid : null; config.BSSID = (mAccessPoint != null) ? mAccessPoint.bssid : null;
validateAndFetchIpAndProxyFields();
config.proxySettings = mProxySettings; config.proxySettings = mProxySettings;
config.ipAssignment = mIpAssignment; config.ipAssignment = mIpAssignment;
config.linkProperties = new LinkProperties(mLinkProperties); config.linkProperties = new LinkProperties(mLinkProperties);
@@ -581,11 +584,16 @@ public class WifiConfigController implements TextWatcher,
mView.findViewById(R.id.staticip).setVisibility(View.VISIBLE); mView.findViewById(R.id.staticip).setVisibility(View.VISIBLE);
if (mIpAddressView == null) { if (mIpAddressView == null) {
mIpAddressView = (TextView) mView.findViewById(R.id.ipaddress); mIpAddressView = (TextView) mView.findViewById(R.id.ipaddress);
mIpAddressView.addTextChangedListener(this);
mGatewayView = (TextView) mView.findViewById(R.id.gateway); mGatewayView = (TextView) mView.findViewById(R.id.gateway);
mGatewayView.addTextChangedListener(this);
mNetworkPrefixLengthView = (TextView) mView.findViewById( mNetworkPrefixLengthView = (TextView) mView.findViewById(
R.id.network_prefix_length); R.id.network_prefix_length);
mNetworkPrefixLengthView.addTextChangedListener(this);
mDns1View = (TextView) mView.findViewById(R.id.dns1); mDns1View = (TextView) mView.findViewById(R.id.dns1);
mDns1View.addTextChangedListener(this);
mDns2View = (TextView) mView.findViewById(R.id.dns2); mDns2View = (TextView) mView.findViewById(R.id.dns2);
mDns2View.addTextChangedListener(this);
} }
if (config != null) { if (config != null) {
LinkProperties linkProperties = config.linkProperties; LinkProperties linkProperties = config.linkProperties;
@@ -628,8 +636,11 @@ public class WifiConfigController implements TextWatcher,
mView.findViewById(R.id.proxy_fields).setVisibility(View.VISIBLE); mView.findViewById(R.id.proxy_fields).setVisibility(View.VISIBLE);
if (mProxyHostView == null) { if (mProxyHostView == null) {
mProxyHostView = (TextView) mView.findViewById(R.id.proxy_hostname); mProxyHostView = (TextView) mView.findViewById(R.id.proxy_hostname);
mProxyHostView.addTextChangedListener(this);
mProxyPortView = (TextView) mView.findViewById(R.id.proxy_port); mProxyPortView = (TextView) mView.findViewById(R.id.proxy_port);
mProxyPortView.addTextChangedListener(this);
mProxyExclusionListView = (TextView) mView.findViewById(R.id.proxy_exclusionlist); mProxyExclusionListView = (TextView) mView.findViewById(R.id.proxy_exclusionlist);
mProxyExclusionListView.addTextChangedListener(this);
} }
if (config != null) { if (config != null) {
ProxyProperties proxyProperties = config.linkProperties.getHttpProxy(); ProxyProperties proxyProperties = config.linkProperties.getHttpProxy();
@@ -721,8 +732,10 @@ public class WifiConfigController implements TextWatcher,
showNetworkSetupFields(); showNetworkSetupFields();
} else if (parent == mProxySettingsSpinner) { } else if (parent == mProxySettingsSpinner) {
showProxyFields(); showProxyFields();
enableSubmitIfAppropriate();
} else { } else {
showIpConfigFields(); showIpConfigFields();
enableSubmitIfAppropriate();
} }
} }