1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.net;
18
19import static android.net.NetworkStats.SET_DEFAULT;
20import static android.net.NetworkStats.SET_FOREGROUND;
21import static android.net.NetworkStats.TAG_NONE;
22import static android.net.NetworkStatsHistory.FIELD_RX_BYTES;
23import static android.net.NetworkStatsHistory.FIELD_TX_BYTES;
24
25import android.content.AsyncTaskLoader;
26import android.content.Context;
27import android.net.INetworkStatsService;
28import android.net.NetworkStatsHistory;
29import android.net.NetworkTemplate;
30import android.os.Bundle;
31import android.os.RemoteException;
32
33/**
34 * Loader for historical chart data for both network and UID details.
35 */
36public class ChartDataLoader extends AsyncTaskLoader<ChartData> {
37    private static final String KEY_TEMPLATE = "template";
38    private static final String KEY_UIDS = "uids";
39    private static final String KEY_FIELDS = "fields";
40
41    private final INetworkStatsService mStatsService;
42    private final Bundle mArgs;
43
44    public static Bundle buildArgs(NetworkTemplate template, int[] uids) {
45        return buildArgs(template, uids, FIELD_RX_BYTES | FIELD_TX_BYTES);
46    }
47
48    public static Bundle buildArgs(NetworkTemplate template, int[] uids, int fields) {
49        final Bundle args = new Bundle();
50        args.putParcelable(KEY_TEMPLATE, template);
51        args.putIntArray(KEY_UIDS, uids);
52        args.putInt(KEY_FIELDS, fields);
53        return args;
54    }
55
56    public ChartDataLoader(Context context, INetworkStatsService statsService, Bundle args) {
57        super(context);
58        mStatsService = statsService;
59        mArgs = args;
60    }
61
62    @Override
63    protected void onStartLoading() {
64        super.onStartLoading();
65        forceLoad();
66    }
67
68    @Override
69    public ChartData loadInBackground() {
70        final NetworkTemplate template = mArgs.getParcelable(KEY_TEMPLATE);
71        final int[] uids = mArgs.getIntArray(KEY_UIDS);
72        final int fields = mArgs.getInt(KEY_FIELDS);
73
74        try {
75            return loadInBackground(template, uids, fields);
76        } catch (RemoteException e) {
77            // since we can't do much without history, and we don't want to
78            // leave with half-baked UI, we bail hard.
79            throw new RuntimeException("problem reading network stats", e);
80        }
81    }
82
83    private ChartData loadInBackground(NetworkTemplate template, int[] uids, int fields)
84            throws RemoteException {
85        final ChartData data = new ChartData();
86        data.network = mStatsService.getHistoryForNetwork(template, fields);
87
88        if (uids != null) {
89            data.detailDefault = null;
90            data.detailForeground = null;
91
92            // load stats for current uid and template
93            for (int uid : uids) {
94                data.detailDefault = collectHistoryForUid(
95                        template, uid, SET_DEFAULT, data.detailDefault);
96                data.detailForeground = collectHistoryForUid(
97                        template, uid, SET_FOREGROUND, data.detailForeground);
98            }
99
100            data.detail = new NetworkStatsHistory(data.detailForeground.getBucketDuration());
101            data.detail.recordEntireHistory(data.detailDefault);
102            data.detail.recordEntireHistory(data.detailForeground);
103        }
104
105        return data;
106    }
107
108    @Override
109    protected void onStopLoading() {
110        super.onStopLoading();
111        cancelLoad();
112    }
113
114    @Override
115    protected void onReset() {
116        super.onReset();
117        cancelLoad();
118    }
119
120    /**
121     * Collect {@link NetworkStatsHistory} for the requested UID, combining with
122     * an existing {@link NetworkStatsHistory} if provided.
123     */
124    private NetworkStatsHistory collectHistoryForUid(
125            NetworkTemplate template, int uid, int set, NetworkStatsHistory existing)
126            throws RemoteException {
127        final NetworkStatsHistory history = mStatsService.getHistoryForUid(
128                template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
129
130        if (existing != null) {
131            existing.recordEntireHistory(history);
132            return existing;
133        } else {
134            return history;
135        }
136    }
137}
138