From b16da1a9cafebbbb0f33f733a74d8213611e4935 Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Wed, 17 Mar 2021 17:25:41 +0800 Subject: [PATCH] Replace the way to access StaticIpConfiguration StaticIpConfiguration is a part of incoming connectivity mainline module. The hidden variable is not accessible for module outside. The caller should use getter and builder to access it. Replace the usage with the formal way. Test: make RunSettingsRoboTests ROBOTEST_FILTER=\ com.android.settings.wifi.WifiConfigControllerTest Test: make RunSettingsRoboTests ROBOTEST_FILTER=\ com.android.settings.wifi.WifiConfigController2Test Change-Id: I2ccd427ba19a4ec7df863c38ee0b34cf0be87fa5 --- .../settings/wifi/WifiConfigController.java | 124 +++++++++------- .../settings/wifi/WifiConfigController2.java | 137 ++++++++++-------- 2 files changed, 146 insertions(+), 115 deletions(-) diff --git a/src/com/android/settings/wifi/WifiConfigController.java b/src/com/android/settings/wifi/WifiConfigController.java index 6e96ae2fefa..80866689238 100644 --- a/src/com/android/settings/wifi/WifiConfigController.java +++ b/src/com/android/settings/wifi/WifiConfigController.java @@ -327,9 +327,9 @@ public class WifiConfigController implements TextWatcher, // 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); @@ -915,67 +915,81 @@ public class WifiConfigController implements TextWatcher, 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 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 467f32e60fe..0ba95432df8 100644 --- a/src/com/android/settings/wifi/WifiConfigController2.java +++ b/src/com/android/settings/wifi/WifiConfigController2.java @@ -317,9 +317,9 @@ public class WifiConfigController2 implements TextWatcher, // 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); @@ -898,66 +898,83 @@ public class WifiConfigController2 implements TextWatcher, 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 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 +1384,18 @@ public class WifiConfigController2 implements TextWatcher, 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 dnsIterator = staticConfig.dnsServers.iterator(); + Iterator dnsIterator = staticConfig.getDnsServers().iterator(); if (dnsIterator.hasNext()) { mDns1View.setText(dnsIterator.next().getHostAddress()); }