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 23 import android.annotation.NonNull; 24 import android.app.usage.NetworkStats; 25 import android.app.usage.NetworkStatsManager; 26 import android.content.AsyncTaskLoader; 27 import android.content.Context; 28 import android.net.NetworkTemplate; 29 import android.os.Bundle; 30 import android.os.RemoteException; 31 32 import com.android.settingslib.AppItem; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 /** 38 * Framework loader is deprecated, use the compat version instead. 39 * 40 * @deprecated 41 */ 42 @Deprecated 43 public class ChartDataLoader extends AsyncTaskLoader<ChartData> { 44 private static final String KEY_TEMPLATE = "template"; 45 private static final String KEY_APP = "app"; 46 47 private final NetworkStatsManager mNetworkStatsManager; 48 private final Bundle mArgs; 49 buildArgs(NetworkTemplate template, AppItem app)50 public static Bundle buildArgs(NetworkTemplate template, AppItem app) { 51 final Bundle args = new Bundle(); 52 args.putParcelable(KEY_TEMPLATE, template); 53 args.putParcelable(KEY_APP, app); 54 return args; 55 } 56 ChartDataLoader(Context context, NetworkStatsManager statsManager, Bundle args)57 public ChartDataLoader(Context context, NetworkStatsManager statsManager, Bundle args) { 58 super(context); 59 mNetworkStatsManager = statsManager; 60 mArgs = args; 61 } 62 63 @Override onStartLoading()64 protected void onStartLoading() { 65 super.onStartLoading(); 66 forceLoad(); 67 } 68 69 @Override loadInBackground()70 public ChartData loadInBackground() { 71 final NetworkTemplate template = mArgs.getParcelable(KEY_TEMPLATE); 72 final AppItem app = mArgs.getParcelable(KEY_APP); 73 74 try { 75 return loadInBackground(template, app); 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 @NonNull convertToBuckets(@onNull NetworkStats stats)84 private List<NetworkStats.Bucket> convertToBuckets(@NonNull NetworkStats stats) { 85 final List<NetworkStats.Bucket> ret = new ArrayList<>(); 86 while (stats.hasNextBucket()) { 87 final NetworkStats.Bucket bucket = new NetworkStats.Bucket(); 88 stats.getNextBucket(bucket); 89 ret.add(bucket); 90 } 91 return ret; 92 } 93 loadInBackground(NetworkTemplate template, AppItem app)94 private ChartData loadInBackground(NetworkTemplate template, AppItem app) 95 throws RemoteException { 96 final ChartData data = new ChartData(); 97 data.network = convertToBuckets(mNetworkStatsManager.queryDetailsForDevice( 98 template, Long.MIN_VALUE, Long.MAX_VALUE)); 99 100 if (app != null) { 101 // load stats for current uid and template 102 final int size = app.uids.size(); 103 for (int i = 0; i < size; i++) { 104 final int uid = app.uids.keyAt(i); 105 data.detailDefault = collectHistoryForUid( 106 template, uid, SET_DEFAULT, data.detailDefault); 107 data.detailForeground = collectHistoryForUid( 108 template, uid, SET_FOREGROUND, data.detailForeground); 109 } 110 111 if (size > 0) { 112 data.detail = new ArrayList<>(); 113 data.detail.addAll(data.detailDefault); 114 data.detail.addAll(data.detailForeground); 115 } else { 116 data.detailDefault = new ArrayList<>(); 117 data.detailForeground = new ArrayList<>(); 118 data.detail = new ArrayList<>(); 119 } 120 } 121 122 return data; 123 } 124 125 @Override onStopLoading()126 protected void onStopLoading() { 127 super.onStopLoading(); 128 cancelLoad(); 129 } 130 131 @Override onReset()132 protected void onReset() { 133 super.onReset(); 134 cancelLoad(); 135 } 136 137 /** 138 * Collect {@link List<NetworkStats.Bucket>} for the requested UID, combining with 139 * an existing {@link List<NetworkStats.Bucket>} if provided. 140 */ collectHistoryForUid( NetworkTemplate template, int uid, int set, List<NetworkStats.Bucket> existing)141 private List<NetworkStats.Bucket> collectHistoryForUid( 142 NetworkTemplate template, int uid, int set, List<NetworkStats.Bucket> existing) { 143 final List<NetworkStats.Bucket> history = convertToBuckets( 144 mNetworkStatsManager.queryDetailsForUidTagState(template, 145 Long.MIN_VALUE, Long.MAX_VALUE, uid, TAG_NONE, set)); 146 147 if (existing != null) { 148 existing.addAll(history); 149 return existing; 150 } else { 151 return history; 152 } 153 } 154 } 155