Use separate settings for host and port.

IPv6 uses ':' in addresses so it's a bad thing to use as a separator.
Rather than find another separator we'll just store them separately.
bug:2700664

Change-Id: Ia6c40891ab9f3f44edd65d8ff7a2a1f7a24cf06e
This commit is contained in:
Robert Greenwalt
2010-10-13 14:07:03 -07:00
parent b5381c60a2
commit 52322a9173
2 changed files with 36 additions and 48 deletions

View File

@@ -26,7 +26,9 @@ import android.app.admin.DevicePolicyManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Proxy; import android.net.Proxy;
import android.net.ProxyProperties;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings; import android.provider.Settings;
import android.text.Selection; import android.text.Selection;
@@ -42,6 +44,7 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import java.net.InetSocketAddress;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -84,7 +87,7 @@ public class ProxySelector extends Fragment implements DialogCreatable {
mView = inflater.inflate(R.layout.proxy, container, false); mView = inflater.inflate(R.layout.proxy, container, false);
initView(mView); initView(mView);
// TODO: Populate based on connection status // TODO: Populate based on connection status
populateFields(false); populateFields();
return mView; return mView;
} }
@@ -152,21 +155,20 @@ public class ProxySelector extends Fragment implements DialogCreatable {
mDefaultButton.setOnClickListener(mDefaultHandler); mDefaultButton.setOnClickListener(mDefaultHandler);
} }
void populateFields(boolean useDefault) { void populateFields() {
final Activity activity = getActivity(); final Activity activity = getActivity();
String hostname = null; String hostname = "";
int port = -1; int port = -1;
String exclList = null; String exclList = "";
if (useDefault) {
// Use the default proxy settings provided by the carrier
hostname = Proxy.getDefaultHost();
port = Proxy.getDefaultPort();
} else {
// Use the last setting given by the user // Use the last setting given by the user
ContentResolver res = getActivity().getContentResolver(); ConnectivityManager cm =
hostname = Proxy.getHost(activity); (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
port = Proxy.getPort(activity);
exclList = Settings.Secure.getString(res, Settings.Secure.HTTP_PROXY_EXCLUSION_LIST); ProxyProperties proxy = cm.getGlobalProxy();
if (proxy != null) {
hostname = proxy.getHost();
port = proxy.getPort();
exclList = proxy.getExclusionList();
} }
if (hostname == null) { if (hostname == null) {
@@ -234,7 +236,7 @@ public class ProxySelector extends Fragment implements DialogCreatable {
String hostname = mHostnameField.getText().toString().trim(); String hostname = mHostnameField.getText().toString().trim();
String portStr = mPortField.getText().toString().trim(); String portStr = mPortField.getText().toString().trim();
String exclList = mExclusionListField.getText().toString().trim(); String exclList = mExclusionListField.getText().toString().trim();
int port = -1; int port = 0;
int result = validate(hostname, portStr, exclList); int result = validate(hostname, portStr, exclList);
if (result > 0) { if (result > 0) {
@@ -246,36 +248,21 @@ public class ProxySelector extends Fragment implements DialogCreatable {
try { try {
port = Integer.parseInt(portStr); port = Integer.parseInt(portStr);
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
// should never happen - caught by validate above
return false; return false;
} }
} }
ProxyProperties p = new ProxyProperties(hostname, port, exclList);
// FIXME: The best solution would be to make a better UI that would // FIXME: The best solution would be to make a better UI that would
// disable editing of the text boxes if the user chooses to use the // disable editing of the text boxes if the user chooses to use the
// default settings. i.e. checking a box to always use the default // default settings. i.e. checking a box to always use the default
// carrier. http:/b/issue?id=756480 // carrier. http:/b/issue?id=756480
// FIXME: This currently will not work if the default host is blank and
// the user has cleared the input boxes in order to not use a proxy.
// This is a UI problem and can be solved with some better form
// controls.
// FIXME: If the user types in a proxy that matches the default, should // FIXME: If the user types in a proxy that matches the default, should
// we keep that setting? Can be fixed with a new UI. // we keep that setting? Can be fixed with a new UI.
ContentResolver res = getActivity().getContentResolver(); ConnectivityManager cm =
if (hostname.equals(Proxy.getDefaultHost()) (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
&& port == Proxy.getDefaultPort()) {
// If the user hit the default button and didn't change any of
// the input boxes, treat it as if the user has not specified a
// proxy.
hostname = null;
}
if (!TextUtils.isEmpty(hostname)) {
hostname += ':' + portStr;
}
Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY, hostname);
Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY_EXCLUSION_LIST, exclList);
getActivity().sendBroadcast(new Intent(Proxy.PROXY_CHANGE_ACTION));
cm.setGlobalProxy(p);
return true; return true;
} }
@@ -298,7 +285,7 @@ public class ProxySelector extends Fragment implements DialogCreatable {
OnClickListener mDefaultHandler = new OnClickListener() { OnClickListener mDefaultHandler = new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
// TODO: populate based on connection status // TODO: populate based on connection status
populateFields(false); populateFields();
} }
}; };

View File

@@ -354,14 +354,18 @@ public class WifiConfigController implements TextWatcher,
if (config.proxySettings == ProxySettings.STATIC) { if (config.proxySettings == ProxySettings.STATIC) {
String host = mProxyHostView.getText().toString(); String host = mProxyHostView.getText().toString();
String port = mProxyPortView.getText().toString(); String portStr = mProxyPortView.getText().toString();
String exclusionList = mProxyExclusionListView.getText().toString(); String exclusionList = mProxyExclusionListView.getText().toString();
int result = ProxySelector.validate(host, port, exclusionList); int port = 0;
int result = 0;
try {
port = Integer.parseInt(portStr);
result = ProxySelector.validate(host, portStr, exclusionList);
} catch (NumberFormatException e) {
result = R.string.proxy_error_invalid_port;
}
if (result == 0) { if (result == 0) {
ProxyProperties proxyProperties= new ProxyProperties(); ProxyProperties proxyProperties= new ProxyProperties(host, port, exclusionList);
proxyProperties.setSocketAddress(
InetSocketAddress.createUnresolved(host, Integer.parseInt(port)));
proxyProperties.setExclusionList(exclusionList);
config.linkProperties.setHttpProxy(proxyProperties); config.linkProperties.setHttpProxy(proxyProperties);
} else { } else {
Toast.makeText(mConfigUi.getContext(), result, Toast.LENGTH_LONG).show(); Toast.makeText(mConfigUi.getContext(), result, Toast.LENGTH_LONG).show();
@@ -572,14 +576,11 @@ public class WifiConfigController implements TextWatcher,
if (config != null) { if (config != null) {
ProxyProperties proxyProperties = config.linkProperties.getHttpProxy(); ProxyProperties proxyProperties = config.linkProperties.getHttpProxy();
if (proxyProperties != null) { if (proxyProperties != null) {
InetSocketAddress sockAddr = proxyProperties.getSocketAddress(); mProxyHostView.setText(proxyProperties.getHost());
if (sockAddr != null) { mProxyPortView.setText(Integer.toString(proxyProperties.getPort()));
mProxyHostView.setText(sockAddr.getHostName());
mProxyPortView.setText(Integer.toString(sockAddr.getPort()));
mProxyExclusionListView.setText(proxyProperties.getExclusionList()); mProxyExclusionListView.setText(proxyProperties.getExclusionList());
} }
} }
}
} else { } else {
mView.findViewById(R.id.proxy_fields).setVisibility(View.GONE); mView.findViewById(R.id.proxy_fields).setVisibility(View.GONE);
} }