Merge "Replace hidden CM#isNetworkSupported() usage"
diff --git a/src/com/android/settings/ProxySelector.java b/src/com/android/settings/ProxySelector.java
index a685841..57d3c6a 100644
--- a/src/com/android/settings/ProxySelector.java
+++ b/src/com/android/settings/ProxySelector.java
@@ -44,6 +44,8 @@
 import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
 import com.android.settings.core.InstrumentedFragment;
 
+import java.util.Arrays;
+
 public class ProxySelector extends InstrumentedFragment implements DialogCreatable {
     private static final String TAG = "ProxySelector";
 
@@ -229,7 +231,9 @@
                 return false;
             }
         }
-        ProxyInfo p = new ProxyInfo(hostname, port, exclList);
+
+        ProxyInfo p = ProxyInfo.buildDirectProxy(
+                hostname, port, Arrays.asList(exclList.split(",")));
         // 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
diff --git a/src/com/android/settings/network/PrivateDnsModeDialogPreference.java b/src/com/android/settings/network/PrivateDnsModeDialogPreference.java
index ea29a1d..822aad0 100644
--- a/src/com/android/settings/network/PrivateDnsModeDialogPreference.java
+++ b/src/com/android/settings/network/PrivateDnsModeDialogPreference.java
@@ -160,7 +160,7 @@
         final Context context = getContext();
         final ContentResolver contentResolver = context.getContentResolver();
 
-        mMode = ConnectivityManager.getPrivateDnsMode(contentResolver);
+        mMode = ConnectivityManager.getPrivateDnsMode(context);
 
         mEditText = view.findViewById(R.id.private_dns_mode_provider_hostname);
         mEditText.addTextChangedListener(this);
diff --git a/src/com/android/settings/network/PrivateDnsPreferenceController.java b/src/com/android/settings/network/PrivateDnsPreferenceController.java
index 3136040..4aa92f4 100644
--- a/src/com/android/settings/network/PrivateDnsPreferenceController.java
+++ b/src/com/android/settings/network/PrivateDnsPreferenceController.java
@@ -118,7 +118,7 @@
     public CharSequence getSummary() {
         final Resources res = mContext.getResources();
         final ContentResolver cr = mContext.getContentResolver();
-        final String mode = ConnectivityManager.getPrivateDnsMode(cr);
+        final String mode = ConnectivityManager.getPrivateDnsMode(mContext);
         final LinkProperties lp = mLatestLinkProperties;
         final List<InetAddress> dnses = (lp == null) ? null : lp.getValidatedPrivateDnsServers();
         final boolean dnsesResolved = !ArrayUtils.isEmpty(dnses);
diff --git a/src/com/android/settings/vpn2/ConfigDialog.java b/src/com/android/settings/vpn2/ConfigDialog.java
index 42bc67d..03e3613 100644
--- a/src/com/android/settings/vpn2/ConfigDialog.java
+++ b/src/com/android/settings/vpn2/ConfigDialog.java
@@ -592,7 +592,7 @@
             // 0 is a last resort default, but the interface validates that the proxy port is
             // present and non-zero.
             int port = proxyPort.isEmpty() ? 0 : Integer.parseInt(proxyPort);
-            profile.proxy = new ProxyInfo(proxyHost, port, null);
+            profile.proxy = ProxyInfo.buildDirectProxy(proxyHost, port);
         } else {
             profile.proxy = null;
         }
diff --git a/src/com/android/settings/wifi/WifiConfigController.java b/src/com/android/settings/wifi/WifiConfigController.java
index 6e96ae2..f6fda6d 100644
--- a/src/com/android/settings/wifi/WifiConfigController.java
+++ b/src/com/android/settings/wifi/WifiConfigController.java
@@ -83,6 +83,7 @@
 import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
@@ -327,9 +328,9 @@
                     // Display IP address.
                     StaticIpConfiguration staticConfig = config.getIpConfiguration()
                             .getStaticIpConfiguration();
-                    if (staticConfig != null && staticConfig.ipAddress != null) {
+                    if (staticConfig != null && staticConfig.getIpAddress() != null) {
                         addRow(group, R.string.wifi_ip_address,
-                                staticConfig.ipAddress.getAddress().getHostAddress());
+                                staticConfig.getIpAddress().getAddress().getHostAddress());
                     }
                 } else {
                     mIpSettingsSpinner.setSelection(DHCP);
@@ -878,7 +879,8 @@
                 result = R.string.proxy_error_invalid_port;
             }
             if (result == 0) {
-                mHttpProxy = new ProxyInfo(host, port, exclusionList);
+                mHttpProxy = ProxyInfo.buildDirectProxy(
+                        host, port, Arrays.asList(exclusionList.split(",")));
             } else {
                 return false;
             }
@@ -892,7 +894,7 @@
             if (uri == null) {
                 return false;
             }
-            mHttpProxy = new ProxyInfo(uri);
+            mHttpProxy = ProxyInfo.buildPacProxy(uri);
         }
         return true;
     }
@@ -915,67 +917,81 @@
         if (inetAddr == null || inetAddr.equals(Inet4Address.ANY)) {
             return R.string.wifi_ip_settings_invalid_ip_address;
         }
-
-        int networkPrefixLength = -1;
+        // Copy all fields into the builder first and set desired value later with builder.
+        final StaticIpConfiguration.Builder staticIPBuilder = new StaticIpConfiguration.Builder()
+                .setDnsServers(staticIpConfiguration.getDnsServers())
+                .setDomains(staticIpConfiguration.getDomains())
+                .setGateway(staticIpConfiguration.getGateway())
+                .setIpAddress(staticIpConfiguration.getIpAddress());
         try {
-            networkPrefixLength = Integer.parseInt(mNetworkPrefixLengthView.getText().toString());
-            if (networkPrefixLength < 0 || networkPrefixLength > 32) {
-                return R.string.wifi_ip_settings_invalid_network_prefix_length;
-            }
-            staticIpConfiguration.ipAddress = new LinkAddress(inetAddr, networkPrefixLength);
-        } catch (NumberFormatException e) {
-            // Set the hint as default after user types in ip address
-            mNetworkPrefixLengthView.setText(mConfigUi.getContext().getString(
-                    R.string.wifi_network_prefix_length_hint));
-        } catch (IllegalArgumentException e) {
-            return R.string.wifi_ip_settings_invalid_ip_address;
-        }
-
-        String gateway = mGatewayView.getText().toString();
-        if (TextUtils.isEmpty(gateway)) {
+            int networkPrefixLength = -1;
             try {
-                //Extract a default gateway from IP address
-                InetAddress netPart = NetUtils.getNetworkPart(inetAddr, networkPrefixLength);
-                byte[] addr = netPart.getAddress();
-                addr[addr.length - 1] = 1;
-                mGatewayView.setText(InetAddress.getByAddress(addr).getHostAddress());
-            } catch (RuntimeException ee) {
-            } catch (java.net.UnknownHostException u) {
+                networkPrefixLength = Integer.parseInt(
+                        mNetworkPrefixLengthView.getText().toString());
+                if (networkPrefixLength < 0 || networkPrefixLength > 32) {
+                    return R.string.wifi_ip_settings_invalid_network_prefix_length;
+                }
+                staticIPBuilder.setIpAddress(new LinkAddress(inetAddr, networkPrefixLength));
+            } catch (NumberFormatException e) {
+                // Set the hint as default after user types in ip address
+                mNetworkPrefixLengthView.setText(mConfigUi.getContext().getString(
+                        R.string.wifi_network_prefix_length_hint));
+            } catch (IllegalArgumentException e) {
+                return R.string.wifi_ip_settings_invalid_ip_address;
             }
-        } else {
-            InetAddress gatewayAddr = getIPv4Address(gateway);
-            if (gatewayAddr == null) {
-                return R.string.wifi_ip_settings_invalid_gateway;
-            }
-            if (gatewayAddr.isMulticastAddress()) {
-                return R.string.wifi_ip_settings_invalid_gateway;
-            }
-            staticIpConfiguration.gateway = gatewayAddr;
-        }
 
-        String dns = mDns1View.getText().toString();
-        InetAddress dnsAddr = null;
-
-        if (TextUtils.isEmpty(dns)) {
-            //If everything else is valid, provide hint as a default option
-            mDns1View.setText(mConfigUi.getContext().getString(R.string.wifi_dns1_hint));
-        } else {
-            dnsAddr = getIPv4Address(dns);
-            if (dnsAddr == null) {
-                return R.string.wifi_ip_settings_invalid_dns;
+            String gateway = mGatewayView.getText().toString();
+            if (TextUtils.isEmpty(gateway)) {
+                try {
+                    //Extract a default gateway from IP address
+                    InetAddress netPart = NetUtils.getNetworkPart(inetAddr, networkPrefixLength);
+                    byte[] addr = netPart.getAddress();
+                    addr[addr.length - 1] = 1;
+                    mGatewayView.setText(InetAddress.getByAddress(addr).getHostAddress());
+                } catch (RuntimeException ee) {
+                } catch (java.net.UnknownHostException u) {
+                }
+            } else {
+                InetAddress gatewayAddr = getIPv4Address(gateway);
+                if (gatewayAddr == null) {
+                    return R.string.wifi_ip_settings_invalid_gateway;
+                }
+                if (gatewayAddr.isMulticastAddress()) {
+                    return R.string.wifi_ip_settings_invalid_gateway;
+                }
+                staticIPBuilder.setGateway(gatewayAddr);
             }
-            staticIpConfiguration.dnsServers.add(dnsAddr);
-        }
 
-        if (mDns2View.length() > 0) {
-            dns = mDns2View.getText().toString();
-            dnsAddr = getIPv4Address(dns);
-            if (dnsAddr == null) {
-                return R.string.wifi_ip_settings_invalid_dns;
+            String dns = mDns1View.getText().toString();
+            InetAddress dnsAddr = null;
+            final ArrayList<InetAddress> dnsServers = new ArrayList<>();
+
+            if (TextUtils.isEmpty(dns)) {
+                //If everything else is valid, provide hint as a default option
+                mDns1View.setText(mConfigUi.getContext().getString(R.string.wifi_dns1_hint));
+            } else {
+                dnsAddr = getIPv4Address(dns);
+                if (dnsAddr == null) {
+                    return R.string.wifi_ip_settings_invalid_dns;
+                }
+                dnsServers.add(dnsAddr);
             }
-            staticIpConfiguration.dnsServers.add(dnsAddr);
+
+            if (mDns2View.length() > 0) {
+                dns = mDns2View.getText().toString();
+                dnsAddr = getIPv4Address(dns);
+                if (dnsAddr == null) {
+                    return R.string.wifi_ip_settings_invalid_dns;
+                }
+                dnsServers.add(dnsAddr);
+            }
+            staticIPBuilder.setDnsServers(dnsServers);
+            return 0;
+        } finally {
+            // Caller of this method may rely on staticIpConfiguration, so build the final result
+            // at the end of the method.
+            staticIpConfiguration = staticIPBuilder.build();
         }
-        return 0;
     }
 
     private void showSecurityFields(boolean refreshEapMethods, boolean refreshCertificates) {
diff --git a/src/com/android/settings/wifi/WifiConfigController2.java b/src/com/android/settings/wifi/WifiConfigController2.java
index 467f32e..06fbc88 100644
--- a/src/com/android/settings/wifi/WifiConfigController2.java
+++ b/src/com/android/settings/wifi/WifiConfigController2.java
@@ -83,6 +83,7 @@
 import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
@@ -317,9 +318,9 @@
                     // Display IP address.
                     StaticIpConfiguration staticConfig = config.getIpConfiguration()
                             .getStaticIpConfiguration();
-                    if (staticConfig != null && staticConfig.ipAddress != null) {
+                    if (staticConfig != null && staticConfig.getIpAddress() != null) {
                         addRow(group, R.string.wifi_ip_address,
-                                staticConfig.ipAddress.getAddress().getHostAddress());
+                                staticConfig.getIpAddress().getAddress().getHostAddress());
                     }
                 } else {
                     mIpSettingsSpinner.setSelection(DHCP);
@@ -860,7 +861,8 @@
                 result = R.string.proxy_error_invalid_port;
             }
             if (result == 0) {
-                mHttpProxy = new ProxyInfo(host, port, exclusionList);
+                mHttpProxy = ProxyInfo.buildDirectProxy(
+                        host, port, Arrays.asList(exclusionList.split(",")));
             } else {
                 return false;
             }
@@ -874,7 +876,7 @@
             if (uri == null) {
                 return false;
             }
-            mHttpProxy = new ProxyInfo(uri);
+            mHttpProxy = ProxyInfo.buildPacProxy(uri);
         }
         return true;
     }
@@ -898,66 +900,83 @@
             return R.string.wifi_ip_settings_invalid_ip_address;
         }
 
-        int networkPrefixLength = -1;
+        // Copy all fields into the builder first and set desired value later with builder.
+        final StaticIpConfiguration.Builder staticIPBuilder = new StaticIpConfiguration.Builder()
+                .setDnsServers(staticIpConfiguration.getDnsServers())
+                .setDomains(staticIpConfiguration.getDomains())
+                .setGateway(staticIpConfiguration.getGateway())
+                .setIpAddress(staticIpConfiguration.getIpAddress());
         try {
-            networkPrefixLength = Integer.parseInt(mNetworkPrefixLengthView.getText().toString());
-            if (networkPrefixLength < 0 || networkPrefixLength > 32) {
-                return R.string.wifi_ip_settings_invalid_network_prefix_length;
-            }
-            staticIpConfiguration.ipAddress = new LinkAddress(inetAddr, networkPrefixLength);
-        } catch (NumberFormatException e) {
-            // Set the hint as default after user types in ip address
-            mNetworkPrefixLengthView.setText(mConfigUi.getContext().getString(
-                    R.string.wifi_network_prefix_length_hint));
-        } catch (IllegalArgumentException e) {
-            return R.string.wifi_ip_settings_invalid_ip_address;
-        }
-
-        String gateway = mGatewayView.getText().toString();
-        if (TextUtils.isEmpty(gateway)) {
+            int networkPrefixLength = -1;
             try {
-                //Extract a default gateway from IP address
-                InetAddress netPart = NetUtils.getNetworkPart(inetAddr, networkPrefixLength);
-                byte[] addr = netPart.getAddress();
-                addr[addr.length - 1] = 1;
-                mGatewayView.setText(InetAddress.getByAddress(addr).getHostAddress());
-            } catch (RuntimeException ee) {
-            } catch (java.net.UnknownHostException u) {
+                networkPrefixLength =
+                        Integer.parseInt(mNetworkPrefixLengthView.getText().toString());
+                if (networkPrefixLength < 0 || networkPrefixLength > 32) {
+                    return R.string.wifi_ip_settings_invalid_network_prefix_length;
+                }
+                staticIPBuilder.setIpAddress(new LinkAddress(inetAddr, networkPrefixLength));
+            } catch (NumberFormatException e) {
+                // Set the hint as default after user types in ip address
+                mNetworkPrefixLengthView.setText(mConfigUi.getContext().getString(
+                        R.string.wifi_network_prefix_length_hint));
+            } catch (IllegalArgumentException e) {
+                return R.string.wifi_ip_settings_invalid_ip_address;
             }
-        } else {
-            InetAddress gatewayAddr = getIPv4Address(gateway);
-            if (gatewayAddr == null) {
-                return R.string.wifi_ip_settings_invalid_gateway;
-            }
-            if (gatewayAddr.isMulticastAddress()) {
-                return R.string.wifi_ip_settings_invalid_gateway;
-            }
-            staticIpConfiguration.gateway = gatewayAddr;
-        }
 
-        String dns = mDns1View.getText().toString();
-        InetAddress dnsAddr = null;
-
-        if (TextUtils.isEmpty(dns)) {
-            //If everything else is valid, provide hint as a default option
-            mDns1View.setText(mConfigUi.getContext().getString(R.string.wifi_dns1_hint));
-        } else {
-            dnsAddr = getIPv4Address(dns);
-            if (dnsAddr == null) {
-                return R.string.wifi_ip_settings_invalid_dns;
+            String gateway = mGatewayView.getText().toString();
+            if (TextUtils.isEmpty(gateway)) {
+                try {
+                    //Extract a default gateway from IP address
+                    InetAddress netPart = NetUtils.getNetworkPart(inetAddr, networkPrefixLength);
+                    byte[] addr = netPart.getAddress();
+                    addr[addr.length - 1] = 1;
+                    mGatewayView.setText(InetAddress.getByAddress(addr).getHostAddress());
+                } catch (RuntimeException ee) {
+                } catch (java.net.UnknownHostException u) {
+                }
+            } else {
+                InetAddress gatewayAddr = getIPv4Address(gateway);
+                if (gatewayAddr == null) {
+                    return R.string.wifi_ip_settings_invalid_gateway;
+                }
+                if (gatewayAddr.isMulticastAddress()) {
+                    return R.string.wifi_ip_settings_invalid_gateway;
+                }
+                staticIPBuilder.setGateway(gatewayAddr);
             }
-            staticIpConfiguration.dnsServers.add(dnsAddr);
-        }
 
-        if (mDns2View.length() > 0) {
-            dns = mDns2View.getText().toString();
-            dnsAddr = getIPv4Address(dns);
-            if (dnsAddr == null) {
-                return R.string.wifi_ip_settings_invalid_dns;
+            String dns = mDns1View.getText().toString();
+            InetAddress dnsAddr = null;
+            final ArrayList<InetAddress> dnsServers = new ArrayList<>();
+
+            if (TextUtils.isEmpty(dns)) {
+                //If everything else is valid, provide hint as a default option
+                mDns1View.setText(mConfigUi.getContext().getString(R.string.wifi_dns1_hint));
+            } else {
+                dnsAddr = getIPv4Address(dns);
+                if (dnsAddr == null) {
+                    return R.string.wifi_ip_settings_invalid_dns;
+                }
+                dnsServers.add(dnsAddr);
+                staticIpConfiguration.dnsServers.add(dnsAddr);
             }
-            staticIpConfiguration.dnsServers.add(dnsAddr);
+
+            if (mDns2View.length() > 0) {
+                dns = mDns2View.getText().toString();
+                dnsAddr = getIPv4Address(dns);
+                if (dnsAddr == null) {
+                    return R.string.wifi_ip_settings_invalid_dns;
+                }
+                dnsServers.add(dnsAddr);
+                staticIpConfiguration.dnsServers.add(dnsAddr);
+            }
+            staticIPBuilder.setDnsServers(dnsServers);
+            return 0;
+        } finally {
+            // Caller of this method may rely on staticIpConfiguration, so build the final result
+            // at the end of the method.
+            staticIpConfiguration = staticIPBuilder.build();
         }
-        return 0;
     }
 
     private void showSecurityFields(boolean refreshEapMethods, boolean refreshCertificates) {
@@ -1367,18 +1386,18 @@
                 StaticIpConfiguration staticConfig = config.getIpConfiguration()
                         .getStaticIpConfiguration();
                 if (staticConfig != null) {
-                    if (staticConfig.ipAddress != null) {
+                    if (staticConfig.getIpAddress() != null) {
                         mIpAddressView.setText(
-                                staticConfig.ipAddress.getAddress().getHostAddress());
-                        mNetworkPrefixLengthView.setText(Integer.toString(staticConfig.ipAddress
-                                .getPrefixLength()));
+                                staticConfig.getIpAddress().getAddress().getHostAddress());
+                        mNetworkPrefixLengthView.setText(Integer.toString(
+                                staticConfig.getIpAddress().getPrefixLength()));
                     }
 
                     if (staticConfig.gateway != null) {
-                        mGatewayView.setText(staticConfig.gateway.getHostAddress());
+                        mGatewayView.setText(staticConfig.getGateway().getHostAddress());
                     }
 
-                    Iterator<InetAddress> dnsIterator = staticConfig.dnsServers.iterator();
+                    Iterator<InetAddress> dnsIterator = staticConfig.getDnsServers().iterator();
                     if (dnsIterator.hasNext()) {
                         mDns1View.setText(dnsIterator.next().getHostAddress());
                     }