From 04f1941be612f7ce78f8689c6a5fcf0855ebba79 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Mon, 10 Jan 2022 10:35:59 +0000 Subject: [PATCH] [MS48.1] Remove INetworkStatsSession from ChartDataLoader Replace with public APIs. Since NetworkStatsHistory is no longer accessible, use List as an alternative structure for furthur manipulating. Test: atest clockwork-settings-robotests make RunSettingsRoboTests -j40 make RunSettingsLibRoboTests ROBOTEST_FILTER=DataUsageControllerTest make RunSettingsLibRoboTests ROBOTEST_FILTER=NetworkCycleChartDataLoaderTest make RunSettingsLibRoboTests ROBOTEST_FILTER=NetworkCycleDataForUidLoaderTest make RunSettingsLibRoboTests ROBOTEST_FILTER=NetworkCycleDataLoaderTest make RunSettingsLibRoboTests ROBOTEST_FILTER=DataUsageUtilsTest Bug: 204830222 Ignore-AOSP-First: Related API conflict, need master first. (cherry-picked from ag/16681840) Change-Id: I5336ceef43193343d707724066e2da40f1ff0633 Merged-In: I5336ceef43193343d707724066e2da40f1ff0633 --- .../android/settingslib/net/ChartData.java | 16 +++-- .../settingslib/net/ChartDataLoader.java | 70 ++++++++++--------- 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/net/ChartData.java b/packages/SettingsLib/src/com/android/settingslib/net/ChartData.java index e30aac575c679..a69c59c6da6d5 100644 --- a/packages/SettingsLib/src/com/android/settingslib/net/ChartData.java +++ b/packages/SettingsLib/src/com/android/settingslib/net/ChartData.java @@ -16,12 +16,18 @@ package com.android.settingslib.net; -import android.net.NetworkStatsHistory; +import android.app.usage.NetworkStats; + +import java.util.List; public class ChartData { - public NetworkStatsHistory network; + // Collect the data usage history of the network from the given {@link NetworkTemplate}. + public List network; - public NetworkStatsHistory detail; - public NetworkStatsHistory detailDefault; - public NetworkStatsHistory detailForeground; + // Collect the detail datausage history (foreground + Background). + public List detail; + // Collect background datausage history. + public List detailDefault; + // Collect foreground datausage history. + public List detailForeground; } diff --git a/packages/SettingsLib/src/com/android/settingslib/net/ChartDataLoader.java b/packages/SettingsLib/src/com/android/settingslib/net/ChartDataLoader.java index 60d22a0d58032..573922f9013c8 100644 --- a/packages/SettingsLib/src/com/android/settingslib/net/ChartDataLoader.java +++ b/packages/SettingsLib/src/com/android/settingslib/net/ChartDataLoader.java @@ -19,20 +19,21 @@ package com.android.settingslib.net; import static android.net.NetworkStats.SET_DEFAULT; import static android.net.NetworkStats.SET_FOREGROUND; import static android.net.NetworkStats.TAG_NONE; -import static android.net.NetworkStatsHistory.FIELD_RX_BYTES; -import static android.net.NetworkStatsHistory.FIELD_TX_BYTES; -import static android.text.format.DateUtils.HOUR_IN_MILLIS; +import android.annotation.NonNull; +import android.app.usage.NetworkStats; +import android.app.usage.NetworkStatsManager; import android.content.AsyncTaskLoader; import android.content.Context; -import android.net.INetworkStatsSession; -import android.net.NetworkStatsHistory; import android.net.NetworkTemplate; import android.os.Bundle; import android.os.RemoteException; import com.android.settingslib.AppItem; +import java.util.ArrayList; +import java.util.List; + /** * Framework loader is deprecated, use the compat version instead. * @@ -42,26 +43,20 @@ import com.android.settingslib.AppItem; public class ChartDataLoader extends AsyncTaskLoader { private static final String KEY_TEMPLATE = "template"; private static final String KEY_APP = "app"; - private static final String KEY_FIELDS = "fields"; - private final INetworkStatsSession mSession; + private final NetworkStatsManager mNetworkStatsManager; private final Bundle mArgs; public static Bundle buildArgs(NetworkTemplate template, AppItem app) { - return buildArgs(template, app, FIELD_RX_BYTES | FIELD_TX_BYTES); - } - - public static Bundle buildArgs(NetworkTemplate template, AppItem app, int fields) { final Bundle args = new Bundle(); args.putParcelable(KEY_TEMPLATE, template); args.putParcelable(KEY_APP, app); - args.putInt(KEY_FIELDS, fields); return args; } - public ChartDataLoader(Context context, INetworkStatsSession session, Bundle args) { + public ChartDataLoader(Context context, NetworkStatsManager statsManager, Bundle args) { super(context); - mSession = session; + mNetworkStatsManager = statsManager; mArgs = args; } @@ -75,10 +70,9 @@ public class ChartDataLoader extends AsyncTaskLoader { public ChartData loadInBackground() { final NetworkTemplate template = mArgs.getParcelable(KEY_TEMPLATE); final AppItem app = mArgs.getParcelable(KEY_APP); - final int fields = mArgs.getInt(KEY_FIELDS); try { - return loadInBackground(template, app, fields); + return loadInBackground(template, app); } catch (RemoteException e) { // since we can't do much without history, and we don't want to // leave with half-baked UI, we bail hard. @@ -86,10 +80,22 @@ public class ChartDataLoader extends AsyncTaskLoader { } } - private ChartData loadInBackground(NetworkTemplate template, AppItem app, int fields) + @NonNull + private List convertToBuckets(@NonNull NetworkStats stats) { + final List ret = new ArrayList<>(); + while (stats.hasNextBucket()) { + final NetworkStats.Bucket bucket = new NetworkStats.Bucket(); + stats.getNextBucket(bucket); + ret.add(bucket); + } + return ret; + } + + private ChartData loadInBackground(NetworkTemplate template, AppItem app) throws RemoteException { final ChartData data = new ChartData(); - data.network = mSession.getHistoryForNetwork(template, fields); + data.network = convertToBuckets(mNetworkStatsManager.queryDetailsForDevice( + template, Long.MIN_VALUE, Long.MAX_VALUE)); if (app != null) { // load stats for current uid and template @@ -103,13 +109,13 @@ public class ChartDataLoader extends AsyncTaskLoader { } if (size > 0) { - data.detail = new NetworkStatsHistory(data.detailForeground.getBucketDuration()); - data.detail.recordEntireHistory(data.detailDefault); - data.detail.recordEntireHistory(data.detailForeground); + data.detail = new ArrayList<>(); + data.detail.addAll(data.detailDefault); + data.detail.addAll(data.detailForeground); } else { - data.detailDefault = new NetworkStatsHistory(HOUR_IN_MILLIS); - data.detailForeground = new NetworkStatsHistory(HOUR_IN_MILLIS); - data.detail = new NetworkStatsHistory(HOUR_IN_MILLIS); + data.detailDefault = new ArrayList<>(); + data.detailForeground = new ArrayList<>(); + data.detail = new ArrayList<>(); } } @@ -129,17 +135,17 @@ public class ChartDataLoader extends AsyncTaskLoader { } /** - * Collect {@link NetworkStatsHistory} for the requested UID, combining with - * an existing {@link NetworkStatsHistory} if provided. + * Collect {@link List} for the requested UID, combining with + * an existing {@link List} if provided. */ - private NetworkStatsHistory collectHistoryForUid( - NetworkTemplate template, int uid, int set, NetworkStatsHistory existing) - throws RemoteException { - final NetworkStatsHistory history = mSession.getHistoryForUid( - template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES); + private List collectHistoryForUid( + NetworkTemplate template, int uid, int set, List existing) { + final List history = convertToBuckets( + mNetworkStatsManager.queryDetailsForUidTagState(template, + Long.MIN_VALUE, Long.MAX_VALUE, uid, TAG_NONE, set)); if (existing != null) { - existing.recordEntireHistory(history); + existing.addAll(history); return existing; } else { return history;