Merge "[MS48.1] Remove INetworkStatsSession from ChartDataLoader"

This commit is contained in:
Frank Li
2022-01-27 08:47:55 +00:00
committed by Gerrit Code Review
2 changed files with 49 additions and 37 deletions

View File

@@ -16,12 +16,18 @@
package com.android.settingslib.net; package com.android.settingslib.net;
import android.net.NetworkStatsHistory; import android.app.usage.NetworkStats;
import java.util.List;
public class ChartData { public class ChartData {
public NetworkStatsHistory network; // Collect the data usage history of the network from the given {@link NetworkTemplate}.
public List<NetworkStats.Bucket> network;
public NetworkStatsHistory detail; // Collect the detail datausage history (foreground + Background).
public NetworkStatsHistory detailDefault; public List<NetworkStats.Bucket> detail;
public NetworkStatsHistory detailForeground; // Collect background datausage history.
public List<NetworkStats.Bucket> detailDefault;
// Collect foreground datausage history.
public List<NetworkStats.Bucket> detailForeground;
} }

View File

@@ -19,20 +19,21 @@ package com.android.settingslib.net;
import static android.net.NetworkStats.SET_DEFAULT; import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.SET_FOREGROUND; import static android.net.NetworkStats.SET_FOREGROUND;
import static android.net.NetworkStats.TAG_NONE; 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.AsyncTaskLoader;
import android.content.Context; import android.content.Context;
import android.net.INetworkStatsSession;
import android.net.NetworkStatsHistory;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import android.os.Bundle; import android.os.Bundle;
import android.os.RemoteException; import android.os.RemoteException;
import com.android.settingslib.AppItem; import com.android.settingslib.AppItem;
import java.util.ArrayList;
import java.util.List;
/** /**
* Framework loader is deprecated, use the compat version instead. * Framework loader is deprecated, use the compat version instead.
* *
@@ -42,26 +43,20 @@ import com.android.settingslib.AppItem;
public class ChartDataLoader extends AsyncTaskLoader<ChartData> { public class ChartDataLoader extends AsyncTaskLoader<ChartData> {
private static final String KEY_TEMPLATE = "template"; private static final String KEY_TEMPLATE = "template";
private static final String KEY_APP = "app"; 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; private final Bundle mArgs;
public static Bundle buildArgs(NetworkTemplate template, AppItem app) { 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(); final Bundle args = new Bundle();
args.putParcelable(KEY_TEMPLATE, template); args.putParcelable(KEY_TEMPLATE, template);
args.putParcelable(KEY_APP, app); args.putParcelable(KEY_APP, app);
args.putInt(KEY_FIELDS, fields);
return args; return args;
} }
public ChartDataLoader(Context context, INetworkStatsSession session, Bundle args) { public ChartDataLoader(Context context, NetworkStatsManager statsManager, Bundle args) {
super(context); super(context);
mSession = session; mNetworkStatsManager = statsManager;
mArgs = args; mArgs = args;
} }
@@ -75,10 +70,9 @@ public class ChartDataLoader extends AsyncTaskLoader<ChartData> {
public ChartData loadInBackground() { public ChartData loadInBackground() {
final NetworkTemplate template = mArgs.getParcelable(KEY_TEMPLATE); final NetworkTemplate template = mArgs.getParcelable(KEY_TEMPLATE);
final AppItem app = mArgs.getParcelable(KEY_APP); final AppItem app = mArgs.getParcelable(KEY_APP);
final int fields = mArgs.getInt(KEY_FIELDS);
try { try {
return loadInBackground(template, app, fields); return loadInBackground(template, app);
} catch (RemoteException e) { } catch (RemoteException e) {
// since we can't do much without history, and we don't want to // since we can't do much without history, and we don't want to
// leave with half-baked UI, we bail hard. // leave with half-baked UI, we bail hard.
@@ -86,10 +80,22 @@ public class ChartDataLoader extends AsyncTaskLoader<ChartData> {
} }
} }
private ChartData loadInBackground(NetworkTemplate template, AppItem app, int fields) @NonNull
private List<NetworkStats.Bucket> convertToBuckets(@NonNull NetworkStats stats) {
final List<NetworkStats.Bucket> 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 { throws RemoteException {
final ChartData data = new ChartData(); 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) { if (app != null) {
// load stats for current uid and template // load stats for current uid and template
@@ -103,13 +109,13 @@ public class ChartDataLoader extends AsyncTaskLoader<ChartData> {
} }
if (size > 0) { if (size > 0) {
data.detail = new NetworkStatsHistory(data.detailForeground.getBucketDuration()); data.detail = new ArrayList<>();
data.detail.recordEntireHistory(data.detailDefault); data.detail.addAll(data.detailDefault);
data.detail.recordEntireHistory(data.detailForeground); data.detail.addAll(data.detailForeground);
} else { } else {
data.detailDefault = new NetworkStatsHistory(HOUR_IN_MILLIS); data.detailDefault = new ArrayList<>();
data.detailForeground = new NetworkStatsHistory(HOUR_IN_MILLIS); data.detailForeground = new ArrayList<>();
data.detail = new NetworkStatsHistory(HOUR_IN_MILLIS); data.detail = new ArrayList<>();
} }
} }
@@ -129,17 +135,17 @@ public class ChartDataLoader extends AsyncTaskLoader<ChartData> {
} }
/** /**
* Collect {@link NetworkStatsHistory} for the requested UID, combining with * Collect {@link List<NetworkStats.Bucket>} for the requested UID, combining with
* an existing {@link NetworkStatsHistory} if provided. * an existing {@link List<NetworkStats.Bucket>} if provided.
*/ */
private NetworkStatsHistory collectHistoryForUid( private List<NetworkStats.Bucket> collectHistoryForUid(
NetworkTemplate template, int uid, int set, NetworkStatsHistory existing) NetworkTemplate template, int uid, int set, List<NetworkStats.Bucket> existing) {
throws RemoteException { final List<NetworkStats.Bucket> history = convertToBuckets(
final NetworkStatsHistory history = mSession.getHistoryForUid( mNetworkStatsManager.queryDetailsForUidTagState(template,
template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES); Long.MIN_VALUE, Long.MAX_VALUE, uid, TAG_NONE, set));
if (existing != null) { if (existing != null) {
existing.recordEntireHistory(history); existing.addAll(history);
return existing; return existing;
} else { } else {
return history; return history;