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 android.content.AsyncTaskLoader;
20import android.content.Context;
21import android.net.INetworkStatsSession;
22import android.net.NetworkStats;
23import android.net.NetworkTemplate;
24import android.os.Bundle;
25import android.os.RemoteException;
26
27public class SummaryForAllUidLoader extends AsyncTaskLoader<NetworkStats> {
28    private static final String KEY_TEMPLATE = "template";
29    private static final String KEY_START = "start";
30    private static final String KEY_END = "end";
31
32    private final INetworkStatsSession mSession;
33    private final Bundle mArgs;
34
35    public static Bundle buildArgs(NetworkTemplate template, long start, long end) {
36        final Bundle args = new Bundle();
37        args.putParcelable(KEY_TEMPLATE, template);
38        args.putLong(KEY_START, start);
39        args.putLong(KEY_END, end);
40        return args;
41    }
42
43    public SummaryForAllUidLoader(Context context, INetworkStatsSession session, Bundle args) {
44        super(context);
45        mSession = session;
46        mArgs = args;
47    }
48
49    @Override
50    protected void onStartLoading() {
51        super.onStartLoading();
52        forceLoad();
53    }
54
55    @Override
56    public NetworkStats loadInBackground() {
57        final NetworkTemplate template = mArgs.getParcelable(KEY_TEMPLATE);
58        final long start = mArgs.getLong(KEY_START);
59        final long end = mArgs.getLong(KEY_END);
60
61        try {
62            return mSession.getSummaryForAllUid(template, start, end, false);
63        } catch (RemoteException e) {
64            return null;
65        }
66    }
67
68    @Override
69    protected void onStopLoading() {
70        super.onStopLoading();
71        cancelLoad();
72    }
73
74    @Override
75    protected void onReset() {
76        super.onReset();
77        cancelLoad();
78    }
79}
80