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
diff --git a/src/com/android/settings/ProxySelector.java b/src/com/android/settings/ProxySelector.java
index 344f710..bdb32e3 100644
--- a/src/com/android/settings/ProxySelector.java
+++ b/src/com/android/settings/ProxySelector.java
@@ -26,7 +26,9 @@
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
+import android.net.ConnectivityManager;
 import android.net.Proxy;
+import android.net.ProxyProperties;
 import android.os.Bundle;
 import android.provider.Settings;
 import android.text.Selection;
@@ -42,6 +44,7 @@
 import android.widget.EditText;
 import android.widget.TextView;
 
+import java.net.InetSocketAddress;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -84,7 +87,7 @@
         mView = inflater.inflate(R.layout.proxy, container, false);
         initView(mView);
         // TODO: Populate based on connection status
-        populateFields(false);
+        populateFields();
         return mView;
     }
 
@@ -152,21 +155,20 @@
         mDefaultButton.setOnClickListener(mDefaultHandler);
     }
 
-    void populateFields(boolean useDefault) {
+    void populateFields() {
         final Activity activity = getActivity();
-        String hostname = null;
+        String hostname = "";
         int port = -1;
-        String exclList = null;
-        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
-            ContentResolver res = getActivity().getContentResolver();
-            hostname = Proxy.getHost(activity);
-            port = Proxy.getPort(activity);
-            exclList = Settings.Secure.getString(res, Settings.Secure.HTTP_PROXY_EXCLUSION_LIST);
+        String exclList = "";
+        // Use the last setting given by the user
+        ConnectivityManager cm =
+                (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
+
+        ProxyProperties proxy = cm.getGlobalProxy();
+        if (proxy != null) {
+            hostname = proxy.getHost();
+            port = proxy.getPort();
+            exclList = proxy.getExclusionList();
         }
 
         if (hostname == null) {
@@ -234,7 +236,7 @@
         String hostname = mHostnameField.getText().toString().trim();
         String portStr = mPortField.getText().toString().trim();
         String exclList = mExclusionListField.getText().toString().trim();
-        int port = -1;
+        int port = 0;
 
         int result = validate(hostname, portStr, exclList);
         if (result > 0) {
@@ -246,36 +248,21 @@
             try {
                 port = Integer.parseInt(portStr);
             } catch (NumberFormatException ex) {
+                // should never happen - caught by validate above
                 return false;
             }
         }
-
+        ProxyProperties p = new ProxyProperties(hostname, port, exclList);
         // 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
         // default settings. i.e. checking a box to always use the default
         // 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
         // we keep that setting? Can be fixed with a new UI.
-        ContentResolver res = getActivity().getContentResolver();
-        if (hostname.equals(Proxy.getDefaultHost())
-                && 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;
-        }
+        ConnectivityManager cm =
+                (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
 
-        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;
     }
 
@@ -298,7 +285,7 @@
     OnClickListener mDefaultHandler = new OnClickListener() {
             public void onClick(View v) {
                 // TODO: populate based on connection status
-                populateFields(false);
+                populateFields();
             }
         };
 
diff --git a/src/com/android/settings/wifi/WifiConfigController.java b/src/com/android/settings/wifi/WifiConfigController.java
index 6d62224..d1c89dd 100644
--- a/src/com/android/settings/wifi/WifiConfigController.java
+++ b/src/com/android/settings/wifi/WifiConfigController.java
@@ -354,14 +354,18 @@
 
         if (config.proxySettings == ProxySettings.STATIC) {
             String host = mProxyHostView.getText().toString();
-            String port = mProxyPortView.getText().toString();
+            String portStr = mProxyPortView.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) {
-                ProxyProperties proxyProperties= new ProxyProperties();
-                proxyProperties.setSocketAddress(
-                        InetSocketAddress.createUnresolved(host, Integer.parseInt(port)));
-                proxyProperties.setExclusionList(exclusionList);
+                ProxyProperties proxyProperties= new ProxyProperties(host, port, exclusionList);
                 config.linkProperties.setHttpProxy(proxyProperties);
             } else {
                 Toast.makeText(mConfigUi.getContext(), result, Toast.LENGTH_LONG).show();
@@ -572,12 +576,9 @@
             if (config != null) {
                 ProxyProperties proxyProperties = config.linkProperties.getHttpProxy();
                 if (proxyProperties != null) {
-                    InetSocketAddress sockAddr = proxyProperties.getSocketAddress();
-                    if (sockAddr != null) {
-                        mProxyHostView.setText(sockAddr.getHostName());
-                        mProxyPortView.setText(Integer.toString(sockAddr.getPort()));
-                        mProxyExclusionListView.setText(proxyProperties.getExclusionList());
-                    }
+                    mProxyHostView.setText(proxyProperties.getHost());
+                    mProxyPortView.setText(Integer.toString(proxyProperties.getPort()));
+                    mProxyExclusionListView.setText(proxyProperties.getExclusionList());
                 }
             }
         } else {