summaryrefslogtreecommitdiff
path: root/packages/SettingsLib/src/com/android/settingslib/net/NetworkCycleChartDataLoader.java
blob: 787dc55e60f4c8c85dacfcbaeb66ecb5d5a5a11b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * 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<>();
    }

    @Override
    void recordUsage(long start, long end) {
        try {
            final NetworkStats.Bucket bucket = mNetworkStatsManager.querySummaryForDevice(
                    mNetworkTemplate, start, end);
            final long total = bucket == null ? 0L : bucket.getRxBytes() + bucket.getTxBytes();
            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.Bucket bucket = mNetworkStatsManager.querySummaryForDevice(
                        mNetworkTemplate, bucketStart, bucketEnd);
                if (bucket != null) {
                    usage = bucket.getRxBytes() + bucket.getTxBytes();
                }
            } 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);
        }

    }

}