From 37a6ef1cd6afb56aac9530daf2128c0f850185f3 Mon Sep 17 00:00:00 2001 From: Benedict Wong Date: Tue, 15 Feb 2022 22:09:46 +0000 Subject: [PATCH] Add type string to VCN NetworkAgentConfig This change populates the VCN type string used in NetworkInfo. While the NetworkInfo API has long been deprecated, some apps apparently still use this API. Even more unusually, some apps use the NetworkInfo's type strings, and perform string comparisons on the type string, as opposed to the type integer/enums to determine network types. This change ensures maximum backward compatibility. Bug: 219287843 Test: atest FrameworksVcnTests Change-Id: I059dbc997f97482f7355fdc29d464d5452933ce3 --- .../android/server/vcn/VcnGatewayConnection.java | 15 +++++++++++++++ .../VcnGatewayConnectionConnectedStateTest.java | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/vcn/VcnGatewayConnection.java b/services/core/java/com/android/server/vcn/VcnGatewayConnection.java index 8b80b4a0b21ec..597f7f284730d 100644 --- a/services/core/java/com/android/server/vcn/VcnGatewayConnection.java +++ b/services/core/java/com/android/server/vcn/VcnGatewayConnection.java @@ -78,6 +78,7 @@ import android.os.PowerManager.WakeLock; import android.os.Process; import android.os.SystemClock; import android.provider.Settings; +import android.telephony.TelephonyManager; import android.util.ArraySet; import android.util.Slog; @@ -163,6 +164,14 @@ import java.util.function.Consumer; public class VcnGatewayConnection extends StateMachine { private static final String TAG = VcnGatewayConnection.class.getSimpleName(); + // Matches DataConnection.NETWORK_TYPE private constant, and magic string from + // ConnectivityManager#getNetworkTypeName() + @VisibleForTesting(visibility = Visibility.PRIVATE) + static final String NETWORK_INFO_NETWORK_TYPE_STRING = "MOBILE"; + + @VisibleForTesting(visibility = Visibility.PRIVATE) + static final String NETWORK_INFO_EXTRA_INFO = "VCN"; + @VisibleForTesting(visibility = Visibility.PRIVATE) static final InetAddress DUMMY_ADDR = InetAddresses.parseNumericAddress("192.0.2.0"); @@ -1631,6 +1640,12 @@ public class VcnGatewayConnection extends StateMachine { final NetworkAgentConfig nac = new NetworkAgentConfig.Builder() .setLegacyType(ConnectivityManager.TYPE_MOBILE) + .setLegacyTypeName(NETWORK_INFO_NETWORK_TYPE_STRING) + .setLegacySubType(TelephonyManager.NETWORK_TYPE_UNKNOWN) + .setLegacySubTypeName( + TelephonyManager.getNetworkTypeName( + TelephonyManager.NETWORK_TYPE_UNKNOWN)) + .setLegacyExtraInfo(NETWORK_INFO_EXTRA_INFO) .build(); final VcnNetworkAgent agent = diff --git a/tests/vcn/java/com/android/server/vcn/VcnGatewayConnectionConnectedStateTest.java b/tests/vcn/java/com/android/server/vcn/VcnGatewayConnectionConnectedStateTest.java index e547400fff73f..4cfa93b4ecf9e 100644 --- a/tests/vcn/java/com/android/server/vcn/VcnGatewayConnectionConnectedStateTest.java +++ b/tests/vcn/java/com/android/server/vcn/VcnGatewayConnectionConnectedStateTest.java @@ -307,7 +307,10 @@ public class VcnGatewayConnectionConnectedStateTest extends VcnGatewayConnection ncCaptor.capture(), lpCaptor.capture(), any(), - argThat(nac -> nac.getLegacyType() == ConnectivityManager.TYPE_MOBILE), + // Subtype integer/name and extras do not have getters; cannot be tested. + argThat(nac -> nac.getLegacyType() == ConnectivityManager.TYPE_MOBILE + && nac.getLegacyTypeName().equals( + VcnGatewayConnection.NETWORK_INFO_NETWORK_TYPE_STRING)), any(), any(), any());