From 96496231c7b5bd3fa013512f727d157fc8a9f320 Mon Sep 17 00:00:00 2001 From: Wayne Ma Date: Tue, 23 May 2023 16:23:41 +0800 Subject: [PATCH] Add buildNetworkTemplateForUpstreamType() helper method The method handles different types of UpstreamType and constructs the corresponding NetworkTemplate object. Bug: 282861165 Test: atest TetheringMetricsTest Change-Id: I1b7091e6da2712df6b6319e545465b409259f743 --- .../tethering/metrics/TetheringMetrics.java | 49 +++++++++++++++++++ .../metrics/TetheringMetricsTest.java | 36 ++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/Tethering/src/com/android/networkstack/tethering/metrics/TetheringMetrics.java b/Tethering/src/com/android/networkstack/tethering/metrics/TetheringMetrics.java index 136dfb1da7..ac4d8b1048 100644 --- a/Tethering/src/com/android/networkstack/tethering/metrics/TetheringMetrics.java +++ b/Tethering/src/com/android/networkstack/tethering/metrics/TetheringMetrics.java @@ -22,6 +22,12 @@ import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET; import static android.net.NetworkCapabilities.TRANSPORT_LOWPAN; import static android.net.NetworkCapabilities.TRANSPORT_WIFI; import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE; +import static android.net.NetworkStats.DEFAULT_NETWORK_YES; +import static android.net.NetworkStats.METERED_YES; +import static android.net.NetworkTemplate.MATCH_BLUETOOTH; +import static android.net.NetworkTemplate.MATCH_ETHERNET; +import static android.net.NetworkTemplate.MATCH_MOBILE; +import static android.net.NetworkTemplate.MATCH_WIFI; import static android.net.TetheringManager.TETHERING_BLUETOOTH; import static android.net.TetheringManager.TETHERING_ETHERNET; import static android.net.TetheringManager.TETHERING_NCM; @@ -48,6 +54,7 @@ import static android.net.TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; import android.annotation.Nullable; import android.content.Context; import android.net.NetworkCapabilities; +import android.net.NetworkTemplate; import android.stats.connectivity.DownstreamType; import android.stats.connectivity.ErrorCode; import android.stats.connectivity.UpstreamType; @@ -419,4 +426,46 @@ public class TetheringMetrics { } return false; } + + /** + * Build NetworkTemplate for the given upstream type. + * + *

NetworkTemplate.Builder API was introduced in Android T. + * + * @param type the upstream type + * @return A NetworkTemplate object with a corresponding match rule or null if tethering + * metrics' data usage cannot be collected for a given upstream type. + */ + @Nullable + public static NetworkTemplate buildNetworkTemplateForUpstreamType(@NonNull UpstreamType type) { + if (!isUsageSupportedForUpstreamType(type)) return null; + + switch (type) { + case UT_CELLULAR: + // TODO: Handle the DUN connection, which is not a default network. + return new NetworkTemplate.Builder(MATCH_MOBILE) + .setMeteredness(METERED_YES) + .setDefaultNetworkStatus(DEFAULT_NETWORK_YES) + .build(); + case UT_WIFI: + return new NetworkTemplate.Builder(MATCH_WIFI) + .setMeteredness(METERED_YES) + .setDefaultNetworkStatus(DEFAULT_NETWORK_YES) + .build(); + case UT_BLUETOOTH: + return new NetworkTemplate.Builder(MATCH_BLUETOOTH) + .setMeteredness(METERED_YES) + .setDefaultNetworkStatus(DEFAULT_NETWORK_YES) + .build(); + case UT_ETHERNET: + return new NetworkTemplate.Builder(MATCH_ETHERNET) + .setMeteredness(METERED_YES) + .setDefaultNetworkStatus(DEFAULT_NETWORK_YES) + .build(); + default: + Log.e(TAG, "Unsupported UpstreamType: " + type.name()); + break; + } + return null; + } } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/metrics/TetheringMetricsTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/metrics/TetheringMetricsTest.java index 7cef9cbf0c..fbc2893803 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/metrics/TetheringMetricsTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/metrics/TetheringMetricsTest.java @@ -22,6 +22,10 @@ import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET; import static android.net.NetworkCapabilities.TRANSPORT_LOWPAN; import static android.net.NetworkCapabilities.TRANSPORT_WIFI; import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE; +import static android.net.NetworkTemplate.MATCH_BLUETOOTH; +import static android.net.NetworkTemplate.MATCH_ETHERNET; +import static android.net.NetworkTemplate.MATCH_MOBILE; +import static android.net.NetworkTemplate.MATCH_WIFI; import static android.net.TetheringManager.TETHERING_BLUETOOTH; import static android.net.TetheringManager.TETHERING_ETHERNET; import static android.net.TetheringManager.TETHERING_NCM; @@ -47,11 +51,14 @@ import static android.net.TetheringManager.TETHER_ERROR_UNSUPPORTED; import static android.net.TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; import android.content.Context; import android.net.NetworkCapabilities; +import android.net.NetworkTemplate; +import android.os.Build; import android.stats.connectivity.DownstreamType; import android.stats.connectivity.ErrorCode; import android.stats.connectivity.UpstreamType; @@ -62,8 +69,11 @@ import androidx.test.runner.AndroidJUnit4; import com.android.networkstack.tethering.UpstreamNetworkState; import com.android.networkstack.tethering.metrics.TetheringMetrics.Dependencies; +import com.android.testutils.DevSdkIgnoreRule; +import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -72,12 +82,15 @@ import org.mockito.MockitoAnnotations; @RunWith(AndroidJUnit4.class) @SmallTest public final class TetheringMetricsTest { + @Rule public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); + private static final String TEST_CALLER_PKG = "com.test.caller.pkg"; private static final String SETTINGS_PKG = "com.android.settings"; private static final String SYSTEMUI_PKG = "com.android.systemui"; private static final String GMS_PKG = "com.google.android.gms"; private static final long TEST_START_TIME = 1670395936033L; private static final long SECOND_IN_MILLIS = 1_000L; + private static final int MATCH_NONE = -1; @Mock private Context mContext; @Mock private Dependencies mDeps; @@ -392,4 +405,27 @@ public final class TetheringMetricsTest { runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_LOWPAN, false /* isSupported */); runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_UNKNOWN, false /* isSupported */); } + + private void runBuildNetworkTemplateForUpstreamType(final UpstreamType upstreamType, + final int matchRule) { + final NetworkTemplate template = + TetheringMetrics.buildNetworkTemplateForUpstreamType(upstreamType); + if (matchRule == MATCH_NONE) { + assertNull(template); + } else { + assertEquals(matchRule, template.getMatchRule()); + } + } + + @Test + @IgnoreUpTo(Build.VERSION_CODES.S_V2) + public void testBuildNetworkTemplateForUpstreamType() { + runBuildNetworkTemplateForUpstreamType(UpstreamType.UT_CELLULAR, MATCH_MOBILE); + runBuildNetworkTemplateForUpstreamType(UpstreamType.UT_WIFI, MATCH_WIFI); + runBuildNetworkTemplateForUpstreamType(UpstreamType.UT_BLUETOOTH, MATCH_BLUETOOTH); + runBuildNetworkTemplateForUpstreamType(UpstreamType.UT_ETHERNET, MATCH_ETHERNET); + runBuildNetworkTemplateForUpstreamType(UpstreamType.UT_WIFI_AWARE, MATCH_NONE); + runBuildNetworkTemplateForUpstreamType(UpstreamType.UT_LOWPAN, MATCH_NONE); + runBuildNetworkTemplateForUpstreamType(UpstreamType.UT_UNKNOWN, MATCH_NONE); + } }