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

Commit e43477a2 authored by Doris Ling's avatar Doris Ling
Browse files

Add loader for net cycle data for specific Uid.

- Add the loader to retrive usage data for a particular uid.
- restructure the NetworkCycleData and the loader:
  - NetworkCycleData now becomes the base class of the cycle data. It
  only contains the cycle start/end and the total usage.
  - Add NetworkCycleChartData which has the bucketized usage for
  plotting the usage chart.
  - Add NetworkCycleDataForUid which shows detail usage for a specific
  app.

Bug: 111751694
Test: make RunSettingsLibRoboTests
Change-Id: Ifd95445730429373dc45aaceae37eb0d79b4377c
parent 9c1c9c2a
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ import com.android.settingslib.AppItem;
/**
 * Loader for historical chart data for both network and UID details.
 *
 * Deprecated in favor of {@link NetworkCycleDataLoader}
 * Deprecated in favor of {@link NetworkCycleChartDataLoader} and
 * {@link NetworkCycleDataForUidLoader}
 *
 * @deprecated
 */
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.net;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Usage data in a billing cycle with bucketized data for plotting the usage chart.
 */
public class NetworkCycleChartData extends NetworkCycleData {
    public static final long BUCKET_DURATION_MS = TimeUnit.DAYS.toMillis(1);

    private List<NetworkCycleData> mUsageBuckets;

    private NetworkCycleChartData() {
    }

    public List<NetworkCycleData> getUsageBuckets() {
        return mUsageBuckets;
    }

    public static class Builder extends NetworkCycleData.Builder {
        private NetworkCycleChartData mObject = new NetworkCycleChartData();

        public Builder setUsageBuckets(List<NetworkCycleData> buckets) {
            getObject().mUsageBuckets = buckets;
            return this;
        }

        @Override
        protected NetworkCycleChartData getObject() {
            return mObject;
        }

        @Override
        public NetworkCycleChartData build() {
            return getObject();
        }
    }

}
+106 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.net;

import android.app.usage.NetworkStats;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Loader for network data usage history. It returns a list of usage data per billing cycle with
 * bucketized usages.
 */
public class NetworkCycleChartDataLoader
        extends NetworkCycleDataLoader<List<NetworkCycleChartData>> {

    private static final String TAG = "NetworkCycleChartLoader";

    private final List<NetworkCycleChartData> mData;

    private NetworkCycleChartDataLoader(Builder builder) {
        super(builder);
        mData = new ArrayList<NetworkCycleChartData>();
    }

    @Override
    void recordUsage(long start, long end) {
        try {
            final NetworkStats stats = mNetworkStatsManager.querySummary(
                mNetworkType, mSubId, start, end);
            final long total = getTotalUsage(stats);
            if (total > 0L) {
                final NetworkCycleChartData.Builder builder = new NetworkCycleChartData.Builder();
                builder.setUsageBuckets(getUsageBuckets(start, end))
                    .setStartTime(start)
                    .setEndTime(end)
                    .setTotalUsage(total);
                mData.add(builder.build());
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Exception querying network detail.", e);
        }
    }

    @Override
    List<NetworkCycleChartData> getCycleUsage() {
        return mData;
    }

    public static Builder<?> builder(Context context) {
        return new Builder<NetworkCycleChartDataLoader>(context) {
            @Override
            public NetworkCycleChartDataLoader build() {
                return new NetworkCycleChartDataLoader(this);
            }
        };
    }

    private List<NetworkCycleData> getUsageBuckets(long start, long end) {
        final List<NetworkCycleData> data = new ArrayList<>();
        long bucketStart = start;
        long bucketEnd = start + NetworkCycleChartData.BUCKET_DURATION_MS;
        while (bucketEnd <= end) {
            long usage = 0L;
            try {
                final NetworkStats stats = mNetworkStatsManager.querySummary(
                    mNetworkType, mSubId, bucketStart, bucketEnd);
                usage = getTotalUsage(stats);
            } catch (RemoteException e) {
                Log.e(TAG, "Exception querying network detail.", e);
            }
            data.add(new NetworkCycleData.Builder()
                .setStartTime(bucketStart).setEndTime(bucketEnd).setTotalUsage(usage).build());
            bucketStart = bucketEnd;
            bucketEnd += NetworkCycleChartData.BUCKET_DURATION_MS;
        }
        return data;
    }

    public static abstract class Builder<T extends NetworkCycleChartDataLoader>
            extends NetworkCycleDataLoader.Builder<T> {

        public Builder(Context context) {
            super(context);
        }

    }

}
+26 −25
Original line number Diff line number Diff line
@@ -16,54 +16,55 @@

package com.android.settingslib.net;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Data structure representing usage data in a billing cycle.
 * Base data structure representing usage data in a billing cycle.
 */
public class NetworkCycleData {
    public static final long BUCKET_DURATION_MS = TimeUnit.DAYS.toMillis(1);
    public long startTime;
    public long endTime;
    public long totalUsage;
    public List<NetworkCycleData> usageBuckets;

    private NetworkCycleData(Builder builder) {
        startTime = builder.mStart;
        endTime = builder.mEnd;
        totalUsage = builder.mTotalUsage;
        usageBuckets = builder.mUsageBuckets;
    private long mStartTime;
    private long mEndTime;
    private long mTotalUsage;

    protected NetworkCycleData() {
    }

    public long getStartTime() {
        return mStartTime;
    }

    public long getEndTime() {
        return mEndTime;
    }

    public long getTotalUsage() {
        return mTotalUsage;
    }

    public static class Builder {
        private long mStart;
        private long mEnd;
        private long mTotalUsage;
        private List<NetworkCycleData> mUsageBuckets;

        private NetworkCycleData mObject = new NetworkCycleData();

        public Builder setStartTime(long start) {
            mStart = start;
            getObject().mStartTime = start;
            return this;
        }

        public Builder setEndTime(long end) {
            mEnd = end;
            getObject().mEndTime = end;
            return this;
        }

        public Builder setTotalUsage(long total) {
            mTotalUsage = total;
            getObject().mTotalUsage = total;
            return this;
        }

        public Builder setUsageBuckets(List<NetworkCycleData> buckets) {
            mUsageBuckets = buckets;
            return this;
        protected NetworkCycleData getObject() {
            return mObject;
        }

        public NetworkCycleData build() {
            return new NetworkCycleData(this);
            return getObject();
        }
    }
}
+65 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.net;

import java.util.concurrent.TimeUnit;

/**
 * Usage data in a billing cycle for a specific Uid.
 */
public class NetworkCycleDataForUid extends NetworkCycleData {

    private long mBackgroudUsage;
    private long mForegroudUsage;

    private NetworkCycleDataForUid() {
    }

    public long getBackgroudUsage() {
        return mBackgroudUsage;
    }

    public long getForegroudUsage() {
        return mForegroudUsage;
    }

    public static class Builder extends NetworkCycleData.Builder {

        private NetworkCycleDataForUid mObject = new NetworkCycleDataForUid();

        public Builder setBackgroundUsage(long backgroundUsage) {
            getObject().mBackgroudUsage = backgroundUsage;
            return this;
        }

        public Builder setForegroundUsage(long foregroundUsage) {
            getObject().mForegroudUsage = foregroundUsage;
            return this;
        }

        @Override
        public NetworkCycleDataForUid getObject() {
            return mObject;
        }

        @Override
        public NetworkCycleDataForUid build() {
            return getObject();
        }
    }

}
Loading