Home | History | Annotate | Download | only in net
      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 
     17 package com.android.settingslib.net;
     18 
     19 import static android.net.NetworkStats.SET_DEFAULT;
     20 import static android.net.NetworkStats.SET_FOREGROUND;
     21 import static android.net.NetworkStats.TAG_NONE;
     22 import static android.net.NetworkStatsHistory.FIELD_RX_BYTES;
     23 import static android.net.NetworkStatsHistory.FIELD_TX_BYTES;
     24 import static android.text.format.DateUtils.HOUR_IN_MILLIS;
     25 
     26 import android.content.AsyncTaskLoader;
     27 import android.content.Context;
     28 import android.net.INetworkStatsSession;
     29 import android.net.NetworkStatsHistory;
     30 import android.net.NetworkTemplate;
     31 import android.os.Bundle;
     32 import android.os.RemoteException;
     33 
     34 import com.android.settingslib.AppItem;
     35 
     36 /**
     37  * Framework loader is deprecated, use the compat version instead.
     38  *
     39  * @deprecated
     40  */
     41 @Deprecated
     42 public class ChartDataLoader extends AsyncTaskLoader<ChartData> {
     43     private static final String KEY_TEMPLATE = "template";
     44     private static final String KEY_APP = "app";
     45     private static final String KEY_FIELDS = "fields";
     46 
     47     private final INetworkStatsSession mSession;
     48     private final Bundle mArgs;
     49 
     50     public static Bundle buildArgs(NetworkTemplate template, AppItem app) {
     51         return buildArgs(template, app, FIELD_RX_BYTES | FIELD_TX_BYTES);
     52     }
     53 
     54     public static Bundle buildArgs(NetworkTemplate template, AppItem app, int fields) {
     55         final Bundle args = new Bundle();
     56         args.putParcelable(KEY_TEMPLATE, template);
     57         args.putParcelable(KEY_APP, app);
     58         args.putInt(KEY_FIELDS, fields);
     59         return args;
     60     }
     61 
     62     public ChartDataLoader(Context context, INetworkStatsSession session, Bundle args) {
     63         super(context);
     64         mSession = session;
     65         mArgs = args;
     66     }
     67 
     68     @Override
     69     protected void onStartLoading() {
     70         super.onStartLoading();
     71         forceLoad();
     72     }
     73 
     74     @Override
     75     public ChartData loadInBackground() {
     76         final NetworkTemplate template = mArgs.getParcelable(KEY_TEMPLATE);
     77         final AppItem app = mArgs.getParcelable(KEY_APP);
     78         final int fields = mArgs.getInt(KEY_FIELDS);
     79 
     80         try {
     81             return loadInBackground(template, app, fields);
     82         } catch (RemoteException e) {
     83             // since we can't do much without history, and we don't want to
     84             // leave with half-baked UI, we bail hard.
     85             throw new RuntimeException("problem reading network stats", e);
     86         }
     87     }
     88 
     89     private ChartData loadInBackground(NetworkTemplate template, AppItem app, int fields)
     90             throws RemoteException {
     91         final ChartData data = new ChartData();
     92         data.network = mSession.getHistoryForNetwork(template, fields);
     93 
     94         if (app != null) {
     95             // load stats for current uid and template
     96             final int size = app.uids.size();
     97             for (int i = 0; i < size; i++) {
     98                 final int uid = app.uids.keyAt(i);
     99                 data.detailDefault = collectHistoryForUid(
    100                         template, uid, SET_DEFAULT, data.detailDefault);
    101                 data.detailForeground = collectHistoryForUid(
    102                         template, uid, SET_FOREGROUND, data.detailForeground);
    103             }
    104 
    105             if (size > 0) {
    106                 data.detail = new NetworkStatsHistory(data.detailForeground.getBucketDuration());
    107                 data.detail.recordEntireHistory(data.detailDefault);
    108                 data.detail.recordEntireHistory(data.detailForeground);
    109             } else {
    110                 data.detailDefault = new NetworkStatsHistory(HOUR_IN_MILLIS);
    111                 data.detailForeground = new NetworkStatsHistory(HOUR_IN_MILLIS);
    112                 data.detail = new NetworkStatsHistory(HOUR_IN_MILLIS);
    113             }
    114         }
    115 
    116         return data;
    117     }
    118 
    119     @Override
    120     protected void onStopLoading() {
    121         super.onStopLoading();
    122         cancelLoad();
    123     }
    124 
    125     @Override
    126     protected void onReset() {
    127         super.onReset();
    128         cancelLoad();
    129     }
    130 
    131     /**
    132      * Collect {@link NetworkStatsHistory} for the requested UID, combining with
    133      * an existing {@link NetworkStatsHistory} if provided.
    134      */
    135     private NetworkStatsHistory collectHistoryForUid(
    136             NetworkTemplate template, int uid, int set, NetworkStatsHistory existing)
    137             throws RemoteException {
    138         final NetworkStatsHistory history = mSession.getHistoryForUid(
    139                 template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
    140 
    141         if (existing != null) {
    142             existing.recordEntireHistory(history);
    143             return existing;
    144         } else {
    145             return history;
    146         }
    147     }
    148 }
    149