Fixing bugs in vehicular networking ref app

Fixing issues in the reference app where a network specifier wasn't
being set on update as well as correctly displaying all network IP
addresses vs the first one.

Bug: 236294399
Bug: 236174962
Test: manually tested in ref app
Change-Id: If8c931cb8c2a38095da11f3f8d464b789c958e70
diff --git a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/ConfigurationUpdater.java b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/ConfigurationUpdater.java
index bdf780f..6ab3322 100644
--- a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/ConfigurationUpdater.java
+++ b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/ConfigurationUpdater.java
@@ -20,6 +20,7 @@
 import android.content.pm.PackageManager;
 import android.net.EthernetManager;
 import android.net.EthernetNetworkManagementException;
+import android.net.EthernetNetworkSpecifier;
 import android.net.EthernetNetworkUpdateRequest;
 import android.net.IpConfiguration;
 import android.net.LinkAddress;
@@ -55,7 +56,8 @@
 
         EthernetNetworkUpdateRequest request = new EthernetNetworkUpdateRequest.Builder()
                 .setIpConfiguration(getIpConfiguration(ipConfigurationText))
-                .setNetworkCapabilities(getCapabilities(networkCapabilitiesText, packageNames))
+                .setNetworkCapabilities(getCapabilities(
+                        interfaceName, networkCapabilitiesText, packageNames))
                 .build();
 
         mEthernetManager.updateConfiguration(interfaceName, request,
@@ -72,7 +74,7 @@
     }
 
     @Nullable
-    private NetworkCapabilities getCapabilities(String networkCapabilitiesText,
+    private NetworkCapabilities getCapabilities(String iface, String networkCapabilitiesText,
             String packageNames) throws PackageManager.NameNotFoundException {
         // TODO: Allow for setting package names without capabilities. In this case, the existing
         //  capabilities should be used.
@@ -82,7 +84,8 @@
 
         NetworkCapabilities.Builder networkCapabilitiesBuilder =
                 NetworkCapabilities.Builder.withoutDefaultCapabilities()
-                        .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET);
+                        .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
+                        .setNetworkSpecifier(new EthernetNetworkSpecifier(iface));
 
         for (int capability : getCapabilitiesList(networkCapabilitiesText)) {
             networkCapabilitiesBuilder.addCapability(capability);
diff --git a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/CurrentEthernetNetworksViewModel.java b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/CurrentEthernetNetworksViewModel.java
index 1cf5d5a..1d62a60 100644
--- a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/CurrentEthernetNetworksViewModel.java
+++ b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/CurrentEthernetNetworksViewModel.java
@@ -18,6 +18,7 @@
 
 import android.app.Application;
 import android.net.ConnectivityManager;
+import android.net.LinkAddress;
 import android.net.LinkProperties;
 import android.net.Network;
 import android.net.NetworkCapabilities;
@@ -102,8 +103,10 @@
                 sb.append(linkProperties.getInterfaceName());
 
                 sb.append("\n\t");
-                sb.append("ip: ");
-                sb.append(linkProperties.getLinkAddresses().get(0).getAddress().getHostAddress());
+                sb.append("ip addresses: ");
+                for (LinkAddress address : linkProperties.getLinkAddresses()) {
+                    sb.append("\n\t\t").append(address.getAddress().getHostAddress());
+                }
             }
 
             if (networkCapabilities != null) {
diff --git a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/MainActivity.java b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/MainActivity.java
index a40042b..a26f3a9 100644
--- a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/MainActivity.java
+++ b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/MainActivity.java
@@ -184,14 +184,16 @@
             validateInterfaceName(getEnableDisableConnectIface());
             NetworkRequest request =
                     new NetworkRequest.Builder()
+                            .clearCapabilities()
                             .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
                             .setNetworkSpecifier(
-                            new EthernetNetworkSpecifier(getEnableDisableConnectIface())).build();
+                                    new EthernetNetworkSpecifier(getEnableDisableConnectIface()))
+                            .build();
             mConnectivityManager.requestNetwork(request,
                     new InterfaceConnectorCallback(),
                     new Handler(Looper.getMainLooper()),
                     Math.toIntExact(REQUEST_NETWORK_TIMEOUT.toMillis()));
-        } catch (IllegalArgumentException e) {
+        } catch (SecurityException | IllegalArgumentException e) {
             showOperationResultDialog(e.getLocalizedMessage(), /* isSuccess= */ false);
         }
     }
@@ -221,8 +223,8 @@
         public void onAvailable(Network network) {
             super.onAvailable(network);
 
-            try {
-                network.bindSocket(new Socket());
+            try (Socket socket = new Socket()) {
+                network.bindSocket(socket);
             } catch (IOException e) {
                 showOperationResultDialog(e.getLocalizedMessage(), /* isSuccess= */ false);
                 return;