Merge "[MS50] Remove NetworkTemplate#buildTemplate* usages"
diff --git a/src/com/android/settings/SettingsDumpService.java b/src/com/android/settings/SettingsDumpService.java
index 7e37e32..38987f5 100644
--- a/src/com/android/settings/SettingsDumpService.java
+++ b/src/com/android/settings/SettingsDumpService.java
@@ -35,6 +35,7 @@
 import androidx.annotation.VisibleForTesting;
 
 import com.android.settings.applications.ProcStatsData;
+import com.android.settings.datausage.lib.DataUsageLib;
 import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService;
 import com.android.settingslib.net.DataUsageController;
 
@@ -109,14 +110,8 @@
         if (telephonyManager.isDataCapable()) {
             JSONArray array = new JSONArray();
             for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) {
-                telephonyManager = telephonyManager
-                        .createForSubscriptionId(info.getSubscriptionId());
-                String subscriberId = telephonyManager.getSubscriberId();
-                // The null subscriberId means that no any mobile/carrier network will be matched.
-                // Using old API: buildTemplateMobileAll for the null subscriberId to avoid NPE.
-                NetworkTemplate template = subscriberId != null
-                        ? NetworkTemplate.buildTemplateCarrierMetered(subscriberId)
-                        : NetworkTemplate.buildTemplateMobileAll(subscriberId);
+                NetworkTemplate template = DataUsageLib.getMobileTemplateForSubId(
+                        telephonyManager, info.getSubscriptionId());
                 final JSONObject usage = dumpDataUsage(template, controller);
                 usage.put("subId", info.getSubscriptionId());
                 array.put(usage);
@@ -125,12 +120,12 @@
         }
         if (packageManager.hasSystemFeature(FEATURE_WIFI)) {
             obj.put("wifi", dumpDataUsage(
-                    NetworkTemplate.buildTemplateWifi(
-                    NetworkTemplate.WIFI_NETWORKID_ALL, null /* subscriberId */), controller));
+                    new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build(), controller));
         }
 
         if (packageManager.hasSystemFeature(FEATURE_ETHERNET)) {
-            obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
+            obj.put("ethernet", dumpDataUsage(new NetworkTemplate.Builder(
+                    NetworkTemplate.MATCH_ETHERNET).build(), controller));
         }
         return obj;
     }
diff --git a/src/com/android/settings/applications/appinfo/AppDataUsagePreferenceController.java b/src/com/android/settings/applications/appinfo/AppDataUsagePreferenceController.java
index f321d72..6a254cb 100644
--- a/src/com/android/settings/applications/appinfo/AppDataUsagePreferenceController.java
+++ b/src/com/android/settings/applications/appinfo/AppDataUsagePreferenceController.java
@@ -17,6 +17,7 @@
 package com.android.settings.applications.appinfo;
 
 import android.content.Context;
+import android.net.NetworkStats;
 import android.net.NetworkTemplate;
 import android.os.Bundle;
 import android.text.format.DateUtils;
@@ -136,13 +137,13 @@
 
     private static NetworkTemplate getTemplate(Context context) {
         if (DataUsageUtils.hasReadyMobileRadio(context)) {
-            return NetworkTemplate.buildTemplateMobileWildcard();
+            return new NetworkTemplate.Builder(NetworkTemplate.MATCH_MOBILE).setMeteredness(
+                    NetworkStats.METERED_YES).build();
         }
         if (DataUsageUtils.hasWifiRadio(context)) {
-            return NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                    null /* subscriberId */);
+            return new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build();
         }
-        return NetworkTemplate.buildTemplateEthernet();
+        return new NetworkTemplate.Builder(NetworkTemplate.MATCH_ETHERNET).build();
     }
 
     @VisibleForTesting
diff --git a/src/com/android/settings/datausage/DataUsageSummary.java b/src/com/android/settings/datausage/DataUsageSummary.java
index a4396a2..abcc45f 100644
--- a/src/com/android/settings/datausage/DataUsageSummary.java
+++ b/src/com/android/settings/datausage/DataUsageSummary.java
@@ -183,15 +183,15 @@
     void addWifiSection() {
         TemplatePreferenceCategory category = (TemplatePreferenceCategory)
                 inflatePreferences(R.xml.data_usage_wifi);
-        category.setTemplate(
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */), 0, services);
+        category.setTemplate(new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build(),
+                0, services);
     }
 
     private void addEthernetSection() {
         TemplatePreferenceCategory category = (TemplatePreferenceCategory)
                 inflatePreferences(R.xml.data_usage_ethernet);
-        category.setTemplate(NetworkTemplate.buildTemplateEthernet(), 0, services);
+        category.setTemplate(new NetworkTemplate.Builder(NetworkTemplate.MATCH_ETHERNET).build(),
+                0, services);
     }
 
     private Preference inflatePreferences(int resId) {
diff --git a/src/com/android/settings/datausage/DataUsageSummaryPreference.java b/src/com/android/settings/datausage/DataUsageSummaryPreference.java
index 260a6da..b6e533b 100644
--- a/src/com/android/settings/datausage/DataUsageSummaryPreference.java
+++ b/src/com/android/settings/datausage/DataUsageSummaryPreference.java
@@ -225,8 +225,7 @@
     static void launchWifiDataUsage(Context context) {
         final Bundle args = new Bundle(1);
         args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE,
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */));
+                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
         args.putInt(DataUsageList.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_WIFI);
         final SubSettingLauncher launcher = new SubSettingLauncher(context)
                 .setArguments(args)
@@ -369,8 +368,7 @@
     protected long getHistoricalUsageLevel() {
         final DataUsageController controller = new DataUsageController(getContext());
         return controller.getHistoricalUsageLevel(
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */));
+                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
     }
 
     @VisibleForTesting
diff --git a/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java b/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
index ac1e1ad..1b3b47a 100644
--- a/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
+++ b/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
@@ -138,8 +138,7 @@
             mDefaultTemplate = DataUsageLib.getMobileTemplate(context, subscriptionId);
         } else if (DataUsageUtils.hasWifiRadio(context)) {
             mDataUsageTemplate = R.string.wifi_data_template;
-            mDefaultTemplate = NetworkTemplate.buildTemplateWifi(
-                    NetworkTemplate.WIFI_NETWORKID_ALL, null /* subscriberId */);
+            mDefaultTemplate = new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build();
         } else {
             mDataUsageTemplate = R.string.ethernet_data_template;
             mDefaultTemplate = DataUsageUtils.getDefaultTemplate(context, subscriptionId);
diff --git a/src/com/android/settings/datausage/DataUsageUtils.java b/src/com/android/settings/datausage/DataUsageUtils.java
index fae53ec..5227284 100644
--- a/src/com/android/settings/datausage/DataUsageUtils.java
+++ b/src/com/android/settings/datausage/DataUsageUtils.java
@@ -184,10 +184,9 @@
         if (SubscriptionManager.isValidSubscriptionId(defaultSubId) && hasMobileData(context)) {
             return DataUsageLib.getMobileTemplate(context, defaultSubId);
         } else if (hasWifiRadio(context)) {
-            return NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                    null /* subscriberId */);
+            return new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build();
         } else {
-            return NetworkTemplate.buildTemplateEthernet();
+            return new NetworkTemplate.Builder(NetworkTemplate.MATCH_ETHERNET).build();
         }
     }
 
diff --git a/src/com/android/settings/datausage/WifiDataUsageSummaryPreferenceController.java b/src/com/android/settings/datausage/WifiDataUsageSummaryPreferenceController.java
index 0551fc2..9d3054e 100644
--- a/src/com/android/settings/datausage/WifiDataUsageSummaryPreferenceController.java
+++ b/src/com/android/settings/datausage/WifiDataUsageSummaryPreferenceController.java
@@ -26,6 +26,8 @@
 import com.android.settingslib.core.lifecycle.Lifecycle;
 import com.android.settingslib.net.DataUsageController;
 
+import java.util.Set;
+
 /**
  * The controller displays a data usage chart for the specified Wi-Fi network.
  */
@@ -52,7 +54,8 @@
         final DataUsageSummaryPreference mPreference = (DataUsageSummaryPreference) preference;
         // TODO(b/126299427): Currently gets data usage of whole Wi-Fi networks, but should get
         //  specified one.
-        final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(mNetworkId);
+        final NetworkTemplate template = new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI)
+                .setWifiNetworkKeys(Set.of(mNetworkId)).build();
         final DataUsageController.DataUsageInfo info = mDataUsageController.getDataUsageInfo(
                 template);
         mDataInfoController.updateDataLimit(info, mPolicyEditor.getPolicy(template));
diff --git a/src/com/android/settings/datausage/lib/DataUsageLib.java b/src/com/android/settings/datausage/lib/DataUsageLib.java
index 830f1ca..9f4c78f 100644
--- a/src/com/android/settings/datausage/lib/DataUsageLib.java
+++ b/src/com/android/settings/datausage/lib/DataUsageLib.java
@@ -17,6 +17,7 @@
 package com.android.settings.datausage.lib;
 
 import android.content.Context;
+import android.net.NetworkStats;
 import android.net.NetworkTemplate;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
@@ -26,6 +27,7 @@
 import com.android.internal.util.ArrayUtils;
 
 import java.util.List;
+import java.util.Set;
 
 /**
  * Lib class for data usage
@@ -71,12 +73,17 @@
         return NetworkTemplate.normalize(mobileTemplate, mergedSubscriberIds);
     }
 
-    private static NetworkTemplate getMobileTemplateForSubId(
+    public static NetworkTemplate getMobileTemplateForSubId(
             TelephonyManager telephonyManager, int subId) {
-        // The null subscriberId means that no any mobile/carrier network will be matched.
-        // Using old API: buildTemplateMobileAll for the null subscriberId to avoid NPE.
+        // Create template that matches any mobile network when the subscriberId is null.
         String subscriberId = telephonyManager.getSubscriberId(subId);
-        return subscriberId != null ? NetworkTemplate.buildTemplateCarrierMetered(subscriberId)
-                : NetworkTemplate.buildTemplateMobileAll(subscriberId);
+        return subscriberId != null
+                ? new NetworkTemplate.Builder(NetworkTemplate.MATCH_CARRIER)
+                .setSubscriberIds(Set.of(subscriberId))
+                .setMeteredness(NetworkStats.METERED_YES)
+                .build()
+                : new NetworkTemplate.Builder(NetworkTemplate.MATCH_MOBILE)
+                        .setMeteredness(NetworkStats.METERED_YES)
+                        .build();
     }
 }
diff --git a/src/com/android/settings/network/NetworkProviderSettings.java b/src/com/android/settings/network/NetworkProviderSettings.java
index fccd874..1e660b0 100644
--- a/src/com/android/settings/network/NetworkProviderSettings.java
+++ b/src/com/android/settings/network/NetworkProviderSettings.java
@@ -302,9 +302,8 @@
         mAddWifiNetworkPreference = new AddWifiNetworkPreference(getPrefContext());
         mDataUsagePreference = findPreference(PREF_KEY_DATA_USAGE);
         mDataUsagePreference.setVisible(DataUsageUtils.hasWifiRadio(getContext()));
-        mDataUsagePreference.setTemplate(
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */), 0 /*subId*/, null /*service*/);
+        mDataUsagePreference.setTemplate(new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI)
+                        .build(), 0 /*subId*/, null /*service*/);
         mResetInternetPreference = findPreference(PREF_KEY_RESET_INTERNET);
         if (mResetInternetPreference != null) {
             mResetInternetPreference.setVisible(false);
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index c4b4d59..0c3b1ae 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -265,9 +265,8 @@
         mStatusMessagePreference = findPreference(PREF_KEY_STATUS_MESSAGE);
         mDataUsagePreference = findPreference(PREF_KEY_DATA_USAGE);
         mDataUsagePreference.setVisible(DataUsageUtils.hasWifiRadio(getContext()));
-        mDataUsagePreference.setTemplate(
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */), 0 /*subId*/, null /*service*/);
+        mDataUsagePreference.setTemplate(new NetworkTemplate.Builder(
+                NetworkTemplate.MATCH_WIFI).build(), 0 /*subId*/, null /*service*/);
     }
 
     @Override
diff --git a/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java b/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
index f75b7f0..c0cdb01 100644
--- a/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
@@ -310,8 +310,7 @@
         ReflectionHelpers.setField(mFragment, "mContext", context);
         ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
         ReflectionHelpers.setField(mFragment, "mTemplate",
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */));
+                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
         final long end = System.currentTimeMillis();
         final long start = end - (DateUtils.WEEK_IN_MILLIS * 4);
 
@@ -336,8 +335,7 @@
         ReflectionHelpers.setField(mFragment, "mContext", context);
         ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
         ReflectionHelpers.setField(mFragment, "mTemplate",
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */));
+                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
         final long end = System.currentTimeMillis();
         final long start = end - (DateUtils.WEEK_IN_MILLIS * 4);
 
@@ -368,8 +366,7 @@
         ReflectionHelpers.setField(mFragment, "mCycles", testCycles);
         ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
         ReflectionHelpers.setField(mFragment, "mTemplate",
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */));
+                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
 
         final NetworkCycleDataForUidLoader loader = (NetworkCycleDataForUidLoader)
                 mFragment.mUidDataCallbacks.onCreateLoader(0 /* id */, Bundle.EMPTY /* args */);
diff --git a/tests/robotests/src/com/android/settings/datausage/DataUsagePreferenceTest.java b/tests/robotests/src/com/android/settings/datausage/DataUsagePreferenceTest.java
index ff8ef93..02f683a 100644
--- a/tests/robotests/src/com/android/settings/datausage/DataUsagePreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/DataUsagePreferenceTest.java
@@ -24,6 +24,7 @@
 
 import android.content.Context;
 import android.content.Intent;
+import android.net.NetworkStats;
 import android.net.NetworkTemplate;
 
 import com.android.settingslib.net.DataUsageController;
@@ -58,8 +59,9 @@
     public void setTemplate_noDataUsage_shouldDisablePreference() {
         doReturn(0L).when(mController).getHistoricalUsageLevel(any(NetworkTemplate.class));
 
-        mPreference.setTemplate(
-                NetworkTemplate.buildTemplateMobileWildcard(), 5 /* subId */, null /* services */);
+        mPreference.setTemplate(new NetworkTemplate.Builder(NetworkTemplate.MATCH_MOBILE)
+                .setMeteredness(NetworkStats.METERED_YES).build(),
+                5 /* subId */, null /* services */);
 
         verify(mPreference).setEnabled(false);
         verify(mPreference).setIntent(null);
@@ -69,8 +71,9 @@
     public void setTemplate_hasDataUsage_shouldNotDisablePreference() {
         doReturn(200L).when(mController).getHistoricalUsageLevel(any(NetworkTemplate.class));
 
-        mPreference.setTemplate(
-                NetworkTemplate.buildTemplateMobileWildcard(), 5 /* subId */, null /* services */);
+        mPreference.setTemplate(new NetworkTemplate.Builder(NetworkTemplate.MATCH_MOBILE)
+                .setMeteredness(NetworkStats.METERED_YES).build(),
+                5 /* subId */, null /* services */);
 
         verify(mPreference, never()).setEnabled(false);
         verify(mPreference).setIntent(any(Intent.class));
diff --git a/tests/unit/src/com/android/settings/datausage/DataUsageInfoControllerTest.java b/tests/unit/src/com/android/settings/datausage/DataUsageInfoControllerTest.java
index d770aba..b80e9d2 100644
--- a/tests/unit/src/com/android/settings/datausage/DataUsageInfoControllerTest.java
+++ b/tests/unit/src/com/android/settings/datausage/DataUsageInfoControllerTest.java
@@ -101,8 +101,7 @@
     }
 
     private NetworkPolicy getDefaultNetworkPolicy() {
-        NetworkTemplate template = NetworkTemplate.buildTemplateWifi(
-                NetworkTemplate.WIFI_NETWORKID_ALL, null /* subscriberId */);
+        NetworkTemplate template = new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build();
         int cycleDay  = -1;
         String cycleTimezone = "UTC";
         long warningBytes = -1;
diff --git a/tests/unit/src/com/android/settings/datausage/DataUsageSummaryPreferenceTest.java b/tests/unit/src/com/android/settings/datausage/DataUsageSummaryPreferenceTest.java
index 7d814b5..b27591e 100644
--- a/tests/unit/src/com/android/settings/datausage/DataUsageSummaryPreferenceTest.java
+++ b/tests/unit/src/com/android/settings/datausage/DataUsageSummaryPreferenceTest.java
@@ -531,13 +531,11 @@
 
         final Bundle expect = new Bundle(1);
         expect.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE,
-                NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
-                null /* subscriberId */));
+                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
         final Bundle actual = startedIntent
                 .getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
         assertThat((NetworkTemplate) actual.getParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE))
-                .isEqualTo(NetworkTemplate.buildTemplateWifi(
-                NetworkTemplate.WIFI_NETWORKID_ALL, null /* subscriberId */));
+                .isEqualTo(new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
 
         assertThat(startedIntent.getIntExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, 0))
                 .isEqualTo(ResourcesUtils.getResourcesId(mContext, "string", "wifi_data_usage"));