Merge "[MS49] Remove NetworkStats hidden Api usages"
diff --git a/src/com/android/settings/datausage/BillingCyclePreferenceController.java b/src/com/android/settings/datausage/BillingCyclePreferenceController.java
index 2f144cb..9592c5e 100644
--- a/src/com/android/settings/datausage/BillingCyclePreferenceController.java
+++ b/src/com/android/settings/datausage/BillingCyclePreferenceController.java
@@ -17,7 +17,6 @@
 package com.android.settings.datausage;
 
 import android.content.Context;
-import android.net.INetworkStatsService;
 import android.net.NetworkPolicyManager;
 import android.net.NetworkTemplate;
 import android.os.INetworkManagementService;
@@ -51,8 +50,6 @@
         TemplatePreference.NetworkServices services = new TemplatePreference.NetworkServices();
         services.mNetworkService = INetworkManagementService.Stub.asInterface(
                 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
-        services.mStatsService = INetworkStatsService.Stub.asInterface(
-                ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
         services.mPolicyManager = mContext.getSystemService(NetworkPolicyManager.class);
         services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager);
         services.mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
diff --git a/src/com/android/settings/datausage/ChartDataUsagePreference.java b/src/com/android/settings/datausage/ChartDataUsagePreference.java
index 0577e9c..12fb03b 100644
--- a/src/com/android/settings/datausage/ChartDataUsagePreference.java
+++ b/src/com/android/settings/datausage/ChartDataUsagePreference.java
@@ -17,13 +17,13 @@
 import android.content.Context;
 import android.content.res.Resources;
 import android.net.NetworkPolicy;
-import android.net.TrafficStats;
 import android.text.SpannableStringBuilder;
 import android.text.TextUtils;
 import android.text.format.DateUtils;
 import android.text.format.Formatter;
 import android.text.style.ForegroundColorSpan;
 import android.util.AttributeSet;
+import android.util.DataUnit;
 import android.util.SparseIntArray;
 
 import androidx.annotation.VisibleForTesting;
@@ -46,7 +46,7 @@
 
     // The resolution we show on the graph so that we can squash things down to ints.
     // Set to half a meg for now.
-    private static final long RESOLUTION = TrafficStats.MB_IN_BYTES / 2;
+    private static final long RESOLUTION = DataUnit.MEBIBYTES.toBytes(1) / 2;
 
     private final int mWarningColor;
     private final int mLimitColor;
diff --git a/src/com/android/settings/datausage/DataUsageBaseFragment.java b/src/com/android/settings/datausage/DataUsageBaseFragment.java
index 299ce43..eee3228 100644
--- a/src/com/android/settings/datausage/DataUsageBaseFragment.java
+++ b/src/com/android/settings/datausage/DataUsageBaseFragment.java
@@ -15,7 +15,6 @@
 package com.android.settings.datausage;
 
 import android.content.Context;
-import android.net.INetworkStatsService;
 import android.net.NetworkPolicy;
 import android.net.NetworkPolicyManager;
 import android.os.Bundle;
@@ -44,8 +43,6 @@
 
         services.mNetworkService = INetworkManagementService.Stub.asInterface(
                 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
-        services.mStatsService = INetworkStatsService.Stub.asInterface(
-                ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
         services.mPolicyManager = (NetworkPolicyManager) context
                 .getSystemService(Context.NETWORK_POLICY_SERVICE);
 
diff --git a/src/com/android/settings/datausage/DataUsageList.java b/src/com/android/settings/datausage/DataUsageList.java
index f72a006..200a888 100644
--- a/src/com/android/settings/datausage/DataUsageList.java
+++ b/src/com/android/settings/datausage/DataUsageList.java
@@ -14,11 +14,9 @@
 
 package com.android.settings.datausage;
 
+import static android.app.usage.NetworkStats.Bucket.UID_REMOVED;
+import static android.app.usage.NetworkStats.Bucket.UID_TETHERING;
 import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
-import static android.net.NetworkStatsHistory.FIELD_RX_BYTES;
-import static android.net.NetworkStatsHistory.FIELD_TX_BYTES;
-import static android.net.TrafficStats.UID_REMOVED;
-import static android.net.TrafficStats.UID_TETHERING;
 
 import android.app.Activity;
 import android.app.ActivityManager;
@@ -91,7 +89,6 @@
     private static final String KEY_APPS_GROUP = "apps_group";
     private static final String KEY_TEMPLATE = "template";
     private static final String KEY_APP = "app";
-    private static final String KEY_FIELDS = "fields";
 
     @VisibleForTesting
     static final int LOADER_CHART_DATA = 2;
@@ -304,7 +301,6 @@
         final Bundle args = new Bundle();
         args.putParcelable(KEY_TEMPLATE, template);
         args.putParcelable(KEY_APP, null);
-        args.putInt(KEY_FIELDS, FIELD_RX_BYTES | FIELD_TX_BYTES);
         return args;
     }
 
diff --git a/src/com/android/settings/datausage/DataUsagePreference.java b/src/com/android/settings/datausage/DataUsagePreference.java
index 644ea9b..0ed0ad3 100644
--- a/src/com/android/settings/datausage/DataUsagePreference.java
+++ b/src/com/android/settings/datausage/DataUsagePreference.java
@@ -53,7 +53,7 @@
         mTemplate = template;
         mSubId = subId;
         final DataUsageController controller = getDataUsageController();
-        if (mTemplate.isMatchRuleMobile()) {
+        if (mTemplate.getMatchRule() == NetworkTemplate.MATCH_MOBILE) {
             setTitle(R.string.app_cellular_data_usage);
         } else {
             final DataUsageController.DataUsageInfo usageInfo =
@@ -78,13 +78,14 @@
         final SubSettingLauncher launcher;
         args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate);
         args.putInt(DataUsageList.EXTRA_SUB_ID, mSubId);
-        args.putInt(DataUsageList.EXTRA_NETWORK_TYPE, mTemplate.isMatchRuleMobile()
-            ? ConnectivityManager.TYPE_MOBILE : ConnectivityManager.TYPE_WIFI);
+        args.putInt(DataUsageList.EXTRA_NETWORK_TYPE, mTemplate.getMatchRule()
+                == NetworkTemplate.MATCH_MOBILE
+                ? ConnectivityManager.TYPE_MOBILE : ConnectivityManager.TYPE_WIFI);
         launcher = new SubSettingLauncher(getContext())
             .setArguments(args)
             .setDestination(DataUsageList.class.getName())
             .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN);
-        if (mTemplate.isMatchRuleMobile()) {
+        if (mTemplate.getMatchRule() == NetworkTemplate.MATCH_MOBILE) {
             launcher.setTitleRes(R.string.app_cellular_data_usage);
         } else {
             launcher.setTitleRes(mTitleRes);
diff --git a/src/com/android/settings/datausage/TemplatePreference.java b/src/com/android/settings/datausage/TemplatePreference.java
index 158c3b6..43dda0a 100644
--- a/src/com/android/settings/datausage/TemplatePreference.java
+++ b/src/com/android/settings/datausage/TemplatePreference.java
@@ -14,7 +14,6 @@
 
 package com.android.settings.datausage;
 
-import android.net.INetworkStatsService;
 import android.net.NetworkPolicyManager;
 import android.net.NetworkTemplate;
 import android.os.INetworkManagementService;
@@ -30,7 +29,6 @@
 
     class NetworkServices {
         INetworkManagementService mNetworkService;
-        INetworkStatsService mStatsService;
         NetworkPolicyManager mPolicyManager;
         TelephonyManager mTelephonyManager;
         SubscriptionManager mSubscriptionManager;
diff --git a/src/com/android/settings/datausage/lib/DataUsageLib.java b/src/com/android/settings/datausage/lib/DataUsageLib.java
index 9f4c78f..1ca5eff 100644
--- a/src/com/android/settings/datausage/lib/DataUsageLib.java
+++ b/src/com/android/settings/datausage/lib/DataUsageLib.java
@@ -24,6 +24,8 @@
 import android.telephony.TelephonyManager;
 import android.util.Log;
 
+import androidx.annotation.NonNull;
+
 import com.android.internal.util.ArrayUtils;
 
 import java.util.List;
@@ -70,7 +72,24 @@
             return mobileTemplate;
         }
 
-        return NetworkTemplate.normalize(mobileTemplate, mergedSubscriberIds);
+        return normalizeMobileTemplate(mobileTemplate, mergedSubscriberIds);
+    }
+
+    private static NetworkTemplate normalizeMobileTemplate(
+            @NonNull NetworkTemplate template, @NonNull String[] mergedSet) {
+        if (template.getSubscriberIds().isEmpty()) return template;
+        // The input template should have at most 1 subscriberId.
+        final String subscriberId = template.getSubscriberIds().iterator().next();
+
+        if (Set.of(mergedSet).contains(subscriberId)) {
+            // Requested template subscriber is part of the merge group; return
+            // a template that matches all merged subscribers.
+            return new NetworkTemplate.Builder(template.getMatchRule())
+                    .setSubscriberIds(Set.of(mergedSet))
+                    .setMeteredness(template.getMeteredness()).build();
+        }
+
+        return template;
     }
 
     public static NetworkTemplate getMobileTemplateForSubId(
diff --git a/src/com/android/settings/deviceinfo/MigrateEstimateTask.java b/src/com/android/settings/deviceinfo/MigrateEstimateTask.java
index 03e2b04..4b22412 100644
--- a/src/com/android/settings/deviceinfo/MigrateEstimateTask.java
+++ b/src/com/android/settings/deviceinfo/MigrateEstimateTask.java
@@ -21,7 +21,6 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.UserInfo;
-import android.net.TrafficStats;
 import android.os.AsyncTask;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -29,6 +28,7 @@
 import android.os.storage.VolumeInfo;
 import android.text.format.DateUtils;
 import android.text.format.Formatter;
+import android.util.DataUnit;
 import android.util.Log;
 
 import java.io.IOException;
@@ -42,7 +42,7 @@
     /**
      * Assume roughly a Class 10 card.
      */
-    private static final long SPEED_ESTIMATE_BPS = 10 * TrafficStats.MB_IN_BYTES;
+    private static final long SPEED_ESTIMATE_BPS = DataUnit.MEBIBYTES.toBytes(10);
 
     private final Context mContext;
 
diff --git a/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java b/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
index fd9ae8c..ee0c9e7 100644
--- a/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
+++ b/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
@@ -24,12 +24,12 @@
 import android.content.pm.PackageManager;
 import android.content.res.TypedArray;
 import android.graphics.drawable.Drawable;
-import android.net.TrafficStats;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.storage.VolumeInfo;
+import android.util.DataUnit;
 import android.util.Log;
 import android.util.SparseArray;
 import android.widget.Toast;
@@ -389,7 +389,8 @@
                 attributedSize -= otherData.duplicateCodeSize;
             }
 
-            final long systemSize = Math.max(TrafficStats.GB_IN_BYTES, mUsedBytes - attributedSize);
+            final long systemSize = Math.max(DataUnit.GIBIBYTES.toBytes(1),
+                    mUsedBytes - attributedSize);
             mSystemPreference.setStorageSize(systemSize, mTotalSize);
         }
 
diff --git a/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java b/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
index c0cdb01..68a8a86 100644
--- a/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
@@ -18,7 +18,7 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
@@ -438,7 +438,7 @@
 
         assertThat(mFragment.mTemplate.getMatchRule())
                 .isEqualTo(NetworkTemplate.MATCH_WIFI);
-        assertNull(mFragment.mTemplate.getSubscriberId());
+        assertTrue(mFragment.mTemplate.getSubscriberIds().isEmpty());
         assertThat(mFragment.mTemplate.getNetworkId())
                 .isEqualTo(NetworkTemplate.WIFI_NETWORKID_ALL);
     }
diff --git a/tests/robotests/src/com/android/settings/network/telephony/DataUsagePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/DataUsagePreferenceControllerTest.java
index 43bda4e3..a3be60d 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/DataUsagePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/DataUsagePreferenceControllerTest.java
@@ -26,10 +26,10 @@
 import android.app.usage.NetworkStatsManager;
 import android.content.Context;
 import android.content.Intent;
-import android.net.TrafficStats;
 import android.provider.Settings;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
+import android.util.DataUnit;
 
 import androidx.preference.SwitchPreference;
 
@@ -128,7 +128,7 @@
     public void updateState_shouldUseIECUnit() {
         final DataUsageController.DataUsageInfo usageInfo =
                 new DataUsageController.DataUsageInfo();
-        usageInfo.usageLevel = TrafficStats.MB_IN_BYTES;
+        usageInfo.usageLevel = DataUnit.MEBIBYTES.toBytes(1);
         doReturn(usageInfo).when(mController).getDataUsageInfo(any());
 
         mController.updateState(mPreference);
diff --git a/tests/unit/src/com/android/settings/deviceinfo/storage/StorageAsyncLoaderTest.java b/tests/unit/src/com/android/settings/deviceinfo/storage/StorageAsyncLoaderTest.java
index 671da95..cad68e1 100644
--- a/tests/unit/src/com/android/settings/deviceinfo/storage/StorageAsyncLoaderTest.java
+++ b/tests/unit/src/com/android/settings/deviceinfo/storage/StorageAsyncLoaderTest.java
@@ -33,9 +33,9 @@
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.UserInfo;
 import android.content.res.Resources;
-import android.net.TrafficStats;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.util.DataUnit;
 import android.util.SparseArray;
 
 import androidx.test.core.app.ApplicationProvider;
@@ -62,7 +62,7 @@
     private static final String PACKAGE_NAME_1 = "com.blah.test";
     private static final String PACKAGE_NAME_2 = "com.blah.test2";
     private static final String PACKAGE_NAME_3 = "com.blah.test3";
-    private static final long DEFAULT_QUOTA = 64 * TrafficStats.MB_IN_BYTES;
+    private static final long DEFAULT_QUOTA = DataUnit.MEBIBYTES.toBytes(64);
 
     @Mock
     private StorageStatsSource mSource;