Migrate buildTemplate* to use NetworkTemplate.Builder

Use new Builder API which was introduced in Android T.

Bug: 238843364
Test: FrameworksNetTests
      dumpsys netstats --checkin and check the output
Change-Id: Ieb0a2ee04056a1cedb71274ea710ff5153e2d68c
This commit is contained in:
Aaron Huang
2022-07-13 18:35:14 +08:00
parent 1ba1febf2b
commit 1c14fa9b08
4 changed files with 74 additions and 75 deletions

View File

@@ -28,6 +28,10 @@ import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
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.TrafficStats.UID_REMOVED;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
@@ -774,10 +778,11 @@ public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.W
/** @hide */
public void dumpCheckin(PrintWriter pw, long start, long end) {
dumpCheckin(pw, start, end, NetworkTemplate.buildTemplateMobileWildcard(), "cell");
dumpCheckin(pw, start, end, NetworkTemplate.buildTemplateWifiWildcard(), "wifi");
dumpCheckin(pw, start, end, NetworkTemplate.buildTemplateEthernet(), "eth");
dumpCheckin(pw, start, end, NetworkTemplate.buildTemplateBluetooth(), "bt");
dumpCheckin(pw, start, end, new NetworkTemplate.Builder(MATCH_MOBILE)
.setMeteredness(METERED_YES).build(), "cell");
dumpCheckin(pw, start, end, new NetworkTemplate.Builder(MATCH_WIFI).build(), "wifi");
dumpCheckin(pw, start, end, new NetworkTemplate.Builder(MATCH_ETHERNET).build(), "eth");
dumpCheckin(pw, start, end, new NetworkTemplate.Builder(MATCH_BLUETOOTH).build(), "bt");
}
/**

View File

@@ -31,6 +31,7 @@ import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.IFACE_VT;
import static android.net.NetworkStats.INTERFACES_ALL;
import static android.net.NetworkStats.METERED_ALL;
import static android.net.NetworkStats.METERED_YES;
import static android.net.NetworkStats.ROAMING_ALL;
import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.SET_DEFAULT;
@@ -41,8 +42,8 @@ import static android.net.NetworkStats.TAG_ALL;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
import static android.net.NetworkStatsHistory.FIELD_ALL;
import static android.net.NetworkTemplate.buildTemplateMobileWildcard;
import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
import static android.net.NetworkTemplate.MATCH_MOBILE;
import static android.net.NetworkTemplate.MATCH_WIFI;
import static android.net.TrafficStats.KB_IN_BYTES;
import static android.net.TrafficStats.MB_IN_BYTES;
import static android.net.TrafficStats.UID_TETHERING;
@@ -2359,7 +2360,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
NetworkStats.Entry uidTotal;
// collect mobile sample
template = buildTemplateMobileWildcard();
template = new NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES).build();
devTotal = mDevRecorder.getTotalSinceBootLocked(template);
xtTotal = mXtRecorder.getTotalSinceBootLocked(template);
uidTotal = mUidRecorder.getTotalSinceBootLocked(template);
@@ -2371,7 +2372,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
currentTime);
// collect wifi sample
template = buildTemplateWifiWildcard();
template = new NetworkTemplate.Builder(MATCH_WIFI).build();
devTotal = mDevRecorder.getTotalSinceBootLocked(template);
xtTotal = mXtRecorder.getTotalSinceBootLocked(template);
uidTotal = mUidRecorder.getTotalSinceBootLocked(template);

View File

@@ -21,11 +21,12 @@ import static android.net.NetworkIdentity.OEM_NONE;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
import static android.net.NetworkStats.METERED_NO;
import static android.net.NetworkStats.METERED_YES;
import static android.net.NetworkStats.ROAMING_NO;
import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
import static android.net.NetworkTemplate.MATCH_MOBILE;
import static android.net.NetworkTemplate.MATCH_WIFI;
import static android.net.TrafficStats.MB_IN_BYTES;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
@@ -67,6 +68,7 @@ import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Set;
/**
* Tests for {@link NetworkStatsObservers}.
@@ -84,10 +86,13 @@ public class NetworkStatsObserversTest {
private static final int SUBID_1 = 1;
private static final String TEST_SSID = "AndroidAP";
private static NetworkTemplate sTemplateWifi = buildTemplateWifiWildcard();
private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
private static NetworkTemplate sTemplateWifi = new NetworkTemplate.Builder(MATCH_WIFI).build();
private static NetworkTemplate sTemplateImsi1 = new NetworkTemplate.Builder(MATCH_MOBILE)
.setSubscriberIds(Set.of(IMSI_1))
.setMeteredness(METERED_YES).build();
private static NetworkTemplate sTemplateImsi2 = new NetworkTemplate.Builder(MATCH_MOBILE)
.setSubscriberIds(Set.of(IMSI_2))
.setMeteredness(METERED_YES).build();
private static final int PID_SYSTEM = 1234;
private static final int PID_RED = 1235;
private static final int PID_BLUE = 1236;

View File

@@ -46,14 +46,10 @@ import static android.net.NetworkStats.TAG_ALL;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
import static android.net.NetworkStatsHistory.FIELD_ALL;
import static android.net.NetworkTemplate.MATCH_MOBILE_WILDCARD;
import static android.net.NetworkTemplate.NETWORK_TYPE_ALL;
import static android.net.NetworkTemplate.MATCH_MOBILE;
import static android.net.NetworkTemplate.MATCH_WIFI;
import static android.net.NetworkTemplate.OEM_MANAGED_NO;
import static android.net.NetworkTemplate.OEM_MANAGED_YES;
import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.net.NetworkTemplate.buildTemplateMobileWithRatType;
import static android.net.NetworkTemplate.buildTemplateWifi;
import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
import static android.net.TrafficStats.MB_IN_BYTES;
import static android.net.TrafficStats.UID_REMOVED;
import static android.net.TrafficStats.UID_TETHERING;
@@ -65,7 +61,6 @@ import static android.text.format.DateUtils.HOUR_IN_MILLIS;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
import static com.android.net.module.util.NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_EXACT;
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
import static com.android.server.net.NetworkStatsService.NETSTATS_IMPORT_ATTEMPTS_COUNTER_NAME;
import static com.android.server.net.NetworkStatsService.NETSTATS_IMPORT_FALLBACKS_COUNTER_NAME;
@@ -192,11 +187,14 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
private static final String IMSI_2 = "310260";
private static final String TEST_WIFI_NETWORK_KEY = "WifiNetworkKey";
private static NetworkTemplate sTemplateWifi = buildTemplateWifi(TEST_WIFI_NETWORK_KEY);
private static NetworkTemplate sTemplateCarrierWifi1 =
buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL, IMSI_1);
private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
private static NetworkTemplate sTemplateWifi = new NetworkTemplate.Builder(MATCH_WIFI)
.setWifiNetworkKeys(Set.of(TEST_WIFI_NETWORK_KEY)).build();
private static NetworkTemplate sTemplateCarrierWifi1 = new NetworkTemplate.Builder(MATCH_WIFI)
.setSubscriberIds(Set.of(IMSI_1)).build();
private static NetworkTemplate sTemplateImsi1 = new NetworkTemplate.Builder(MATCH_MOBILE)
.setMeteredness(METERED_YES).setSubscriberIds(Set.of(IMSI_1)).build();
private static NetworkTemplate sTemplateImsi2 = new NetworkTemplate.Builder(MATCH_MOBILE)
.setMeteredness(METERED_YES).setSubscriberIds(Set.of(IMSI_2)).build();
private static final Network WIFI_NETWORK = new Network(100);
private static final Network MOBILE_NETWORK = new Network(101);
@@ -833,15 +831,15 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
@Test
public void testMobileStatsByRatType() throws Exception {
final NetworkTemplate template3g =
buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_UMTS,
METERED_YES);
final NetworkTemplate template4g =
buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_LTE,
METERED_YES);
final NetworkTemplate template5g =
buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_NR,
METERED_YES);
final NetworkTemplate template3g = new NetworkTemplate.Builder(MATCH_MOBILE)
.setRatType(TelephonyManager.NETWORK_TYPE_UMTS)
.setMeteredness(METERED_YES).build();
final NetworkTemplate template4g = new NetworkTemplate.Builder(MATCH_MOBILE)
.setRatType(TelephonyManager.NETWORK_TYPE_LTE)
.setMeteredness(METERED_YES).build();
final NetworkTemplate template5g = new NetworkTemplate.Builder(MATCH_MOBILE)
.setRatType(TelephonyManager.NETWORK_TYPE_NR)
.setMeteredness(METERED_YES).build();
final NetworkStateSnapshot[] states =
new NetworkStateSnapshot[]{buildMobileState(IMSI_1)};
@@ -912,12 +910,13 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
@Test
public void testMobileStatsMeteredness() throws Exception {
// Create metered 5g template.
final NetworkTemplate templateMetered5g =
buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_NR,
METERED_YES);
final NetworkTemplate templateMetered5g = new NetworkTemplate.Builder(MATCH_MOBILE)
.setRatType(TelephonyManager.NETWORK_TYPE_NR)
.setMeteredness(METERED_YES).build();
// Create non-metered 5g template
final NetworkTemplate templateNonMetered5g =
buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_NR, METERED_NO);
final NetworkTemplate templateNonMetered5g = new NetworkTemplate.Builder(MATCH_MOBILE)
.setRatType(TelephonyManager.NETWORK_TYPE_NR)
.setMeteredness(METERED_NO).build();
expectDefaultSettings();
expectNetworkStatsSummary(buildEmptyStats());
@@ -950,33 +949,20 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
@Test
public void testMobileStatsOemManaged() throws Exception {
final NetworkTemplate templateOemPaid = new NetworkTemplate(MATCH_MOBILE_WILDCARD,
/*subscriberId=*/null, /*matchSubscriberIds=*/null,
/*matchWifiNetworkKeys=*/new String[0], METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_PAID, SUBSCRIBER_ID_MATCH_RULE_EXACT);
final NetworkTemplate templateOemPaid = new NetworkTemplate.Builder(MATCH_MOBILE)
.setOemManaged(OEM_PAID).build();
final NetworkTemplate templateOemPrivate = new NetworkTemplate(MATCH_MOBILE_WILDCARD,
/*subscriberId=*/null, /*matchSubscriberIds=*/null,
/*matchWifiNetworkKeys=*/new String[0], METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_PRIVATE, SUBSCRIBER_ID_MATCH_RULE_EXACT);
final NetworkTemplate templateOemPrivate = new NetworkTemplate.Builder(MATCH_MOBILE)
.setOemManaged(OEM_PRIVATE).build();
final NetworkTemplate templateOemAll = new NetworkTemplate(MATCH_MOBILE_WILDCARD,
/*subscriberId=*/null, /*matchSubscriberIds=*/null,
/*matchWifiNetworkKeys=*/new String[0], METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_PAID | OEM_PRIVATE,
SUBSCRIBER_ID_MATCH_RULE_EXACT);
final NetworkTemplate templateOemAll = new NetworkTemplate.Builder(MATCH_MOBILE)
.setOemManaged(OEM_PAID | OEM_PRIVATE).build();
final NetworkTemplate templateOemYes = new NetworkTemplate(MATCH_MOBILE_WILDCARD,
/*subscriberId=*/null, /*matchSubscriberIds=*/null,
/*matchWifiNetworkKeys=*/new String[0], METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_YES,
SUBSCRIBER_ID_MATCH_RULE_EXACT);
final NetworkTemplate templateOemYes = new NetworkTemplate.Builder(MATCH_MOBILE)
.setOemManaged(OEM_MANAGED_YES).build();
final NetworkTemplate templateOemNone = new NetworkTemplate(MATCH_MOBILE_WILDCARD,
/*subscriberId=*/null, /*matchSubscriberIds=*/null,
/*matchWifiNetworkKeys=*/new String[0], METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_NO,
SUBSCRIBER_ID_MATCH_RULE_EXACT);
final NetworkTemplate templateOemNone = new NetworkTemplate.Builder(MATCH_MOBILE)
.setOemManaged(OEM_MANAGED_NO).build();
// OEM_PAID network comes online.
NetworkStateSnapshot[] states = new NetworkStateSnapshot[]{
@@ -1156,7 +1142,9 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
final ZonedDateTime end =
ZonedDateTime.ofInstant(mClock.instant(), ZoneId.systemDefault());
final ZonedDateTime start = end.truncatedTo(ChronoUnit.HOURS);
NetworkStats stats = mSession.getSummaryForNetwork(buildTemplateWifi(TEST_WIFI_NETWORK_KEY),
NetworkStats stats = mSession.getSummaryForNetwork(
new NetworkTemplate.Builder(MATCH_WIFI)
.setWifiNetworkKeys(Set.of(TEST_WIFI_NETWORK_KEY)).build(),
start.toInstant().toEpochMilli(), end.toInstant().toEpochMilli());
assertEquals(1, stats.size());
assertValues(stats, IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
@@ -1668,14 +1656,14 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
public void testDynamicWatchForNetworkRatTypeChanges() throws Exception {
// Build 3G template, type unknown template to get stats while network type is unknown
// and type all template to get the sum of all network type stats.
final NetworkTemplate template3g =
buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_UMTS,
METERED_YES);
final NetworkTemplate templateUnknown =
buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_UNKNOWN,
METERED_YES);
final NetworkTemplate templateAll =
buildTemplateMobileWithRatType(null, NETWORK_TYPE_ALL, METERED_YES);
final NetworkTemplate template3g = new NetworkTemplate.Builder(MATCH_MOBILE)
.setRatType(TelephonyManager.NETWORK_TYPE_UMTS)
.setMeteredness(METERED_YES).build();
final NetworkTemplate templateUnknown = new NetworkTemplate.Builder(MATCH_MOBILE)
.setRatType(TelephonyManager.NETWORK_TYPE_UNKNOWN)
.setMeteredness(METERED_YES).build();
final NetworkTemplate templateAll = new NetworkTemplate.Builder(MATCH_MOBILE)
.setMeteredness(METERED_YES).build();
final NetworkStateSnapshot[] states =
new NetworkStateSnapshot[]{buildMobileState(IMSI_1)};
@@ -1772,8 +1760,8 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
forcePollAndWaitForIdle();
// Verify mobile summary is not changed by the operation count.
final NetworkTemplate templateMobile =
buildTemplateMobileWithRatType(null, NETWORK_TYPE_ALL, METERED_YES);
final NetworkTemplate templateMobile = new NetworkTemplate.Builder(MATCH_MOBILE)
.setMeteredness(METERED_YES).build();
final NetworkStats statsMobile = mSession.getSummaryForAllUid(
templateMobile, Long.MIN_VALUE, Long.MAX_VALUE, true);
assertValues(statsMobile, IFACE_ALL, UID_RED, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
@@ -1784,7 +1772,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
// Verify the operation count is blamed onto the default network.
// TODO: Blame onto the default network is not very reasonable. Consider blame onto the
// network that generates the traffic.
final NetworkTemplate templateWifi = buildTemplateWifiWildcard();
final NetworkTemplate templateWifi = new NetworkTemplate.Builder(MATCH_WIFI).build();
final NetworkStats statsWifi = mSession.getSummaryForAllUid(
templateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
assertValues(statsWifi, IFACE_ALL, UID_RED, SET_ALL, 0xF00D, METERED_ALL, ROAMING_ALL,