Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 87be5d5f authored by Junyu Lai's avatar Junyu Lai Committed by Frank
Browse files

[MS48.1] Remove INetworkStatsSession from ChartDataLoader

Replace with public APIs. Since NetworkStatsHistory is no longer
accessible, use List<Bucket> 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.
Change-Id: I5336ceef43193343d707724066e2da40f1ff0633
parent ded7c84c
Loading
Loading
Loading
Loading
+11 −5
Original line number Diff line number Diff line
@@ -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<NetworkStats.Bucket> network;

    public NetworkStatsHistory detail;
    public NetworkStatsHistory detailDefault;
    public NetworkStatsHistory detailForeground;
    // Collect the detail datausage history (foreground + Background).
    public List<NetworkStats.Bucket> detail;
    // Collect background datausage history.
    public List<NetworkStats.Bucket> detailDefault;
    // Collect foreground datausage history.
    public List<NetworkStats.Bucket> detailForeground;
}
+38 −32
Original line number Diff line number Diff line
@@ -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<ChartData> {
    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<ChartData> {
    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<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 {
        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<ChartData> {
            }

            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<ChartData> {
    }

    /**
     * Collect {@link NetworkStatsHistory} for the requested UID, combining with
     * an existing {@link NetworkStatsHistory} if provided.
     * Collect {@link List<NetworkStats.Bucket>} for the requested UID, combining with
     * an existing {@link List<NetworkStats.Bucket>} 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<NetworkStats.Bucket> collectHistoryForUid(
            NetworkTemplate template, int uid, int set, List<NetworkStats.Bucket> existing) {
        final List<NetworkStats.Bucket> 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;