Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app.usage; |
| 18 | |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 20 | |
| 21 | import android.annotation.Nullable; |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 22 | import android.app.usage.NetworkStats.Bucket; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.net.ConnectivityManager; |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 25 | import android.net.DataUsageRequest; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 26 | import android.net.NetworkIdentity; |
| 27 | import android.net.NetworkTemplate; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 28 | import android.net.INetworkStatsService; |
| 29 | import android.os.Binder; |
Jeff Davidson | 1efb133 | 2015-12-09 18:04:50 -0800 | [diff] [blame] | 30 | import android.os.Build; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 31 | import android.os.Message; |
| 32 | import android.os.Messenger; |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 33 | import android.os.Handler; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 34 | import android.os.Looper; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 35 | import android.os.RemoteException; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 36 | import android.os.ServiceManager; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | |
| 39 | /** |
| 40 | * Provides access to network usage history and statistics. Usage data is collected in |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 41 | * discrete bins of time called 'Buckets'. See {@link NetworkStats.Bucket} for details. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 42 | * <p /> |
| 43 | * Queries can define a time interval in the form of start and end timestamps (Long.MIN_VALUE and |
Jeff Davidson | 1efb133 | 2015-12-09 18:04:50 -0800 | [diff] [blame] | 44 | * Long.MAX_VALUE can be used to simulate open ended intervals). By default, apps can only obtain |
| 45 | * data about themselves. See the below note for special cases in which apps can obtain data about |
| 46 | * other applications. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 47 | * <h3> |
| 48 | * Summary queries |
| 49 | * </h3> |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 50 | * {@link #querySummaryForDevice} <p /> |
| 51 | * {@link #querySummaryForUser} <p /> |
| 52 | * {@link #querySummary} <p /> |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 53 | * These queries aggregate network usage across the whole interval. Therefore there will be only one |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 54 | * bucket for a particular key and state and roaming combination. In case of the user-wide and |
| 55 | * device-wide summaries a single bucket containing the totalised network usage is returned. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 56 | * <h3> |
| 57 | * History queries |
| 58 | * </h3> |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 59 | * {@link #queryDetailsForUid} <p /> |
| 60 | * {@link #queryDetails} <p /> |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 61 | * These queries do not aggregate over time but do aggregate over state and roaming. Therefore there |
| 62 | * can be multiple buckets for a particular key but all Bucket's state is going to be |
| 63 | * {@link NetworkStats.Bucket#STATE_ALL} and all Bucket's roaming is going to be |
| 64 | * {@link NetworkStats.Bucket#ROAMING_ALL}. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 65 | * <p /> |
Zoltan Szatmary-Ban | 3a8b343 | 2016-01-21 10:44:37 +0000 | [diff] [blame] | 66 | * <b>NOTE:</b> Calling {@link #querySummaryForDevice} or accessing stats for apps other than the |
| 67 | * calling app requires the permission {@link android.Manifest.permission#PACKAGE_USAGE_STATS}, |
| 68 | * which is a system-level permission and will not be granted to third-party apps. However, |
| 69 | * declaring the permission implies intention to use the API and the user of the device can grant |
| 70 | * permission through the Settings application. |
| 71 | * <p /> |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 72 | * Profile owner apps are automatically granted permission to query data on the profile they manage |
Jeff Davidson | 1efb133 | 2015-12-09 18:04:50 -0800 | [diff] [blame] | 73 | * (that is, for any query except {@link #querySummaryForDevice}). Device owner apps and carrier- |
| 74 | * privileged apps likewise get access to usage data for all users on the device. |
| 75 | * <p /> |
| 76 | * In addition to tethering usage, usage by removed users and apps, and usage by the system |
| 77 | * is also included in the results for callers with one of these higher levels of access. |
| 78 | * <p /> |
| 79 | * <b>NOTE:</b> Prior to API level {@value Build.VERSION_CODES#N}, all calls to these APIs required |
| 80 | * the above permission, even to access an app's own data usage, and carrier-privileged apps were |
| 81 | * not included. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 82 | */ |
| 83 | public class NetworkStatsManager { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 84 | private static final String TAG = "NetworkStatsManager"; |
| 85 | private static final boolean DBG = false; |
| 86 | |
| 87 | /** @hide */ |
| 88 | public static final int CALLBACK_LIMIT_REACHED = 0; |
| 89 | /** @hide */ |
| 90 | public static final int CALLBACK_RELEASED = 1; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 91 | |
| 92 | private final Context mContext; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 93 | private final INetworkStatsService mService; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * {@hide} |
| 97 | */ |
| 98 | public NetworkStatsManager(Context context) { |
| 99 | mContext = context; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 100 | mService = INetworkStatsService.Stub.asInterface( |
| 101 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 102 | } |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 103 | |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Query network usage statistics summaries. Result is summarised data usage for the whole |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 106 | * device. Result is a single Bucket aggregated over time, state, uid, tag and roaming. This |
| 107 | * means the bucket's start and end timestamp are going to be the same as the 'startTime' and |
| 108 | * 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid |
| 109 | * {@link NetworkStats.Bucket#UID_ALL}, tag {@link NetworkStats.Bucket#TAG_ALL} |
| 110 | * and roaming {@link NetworkStats.Bucket#ROAMING_ALL}. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 111 | * |
| 112 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 113 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 114 | * etc. |
| 115 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 116 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 117 | * {@link java.lang.System#currentTimeMillis}. |
| 118 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 119 | * {@link java.lang.System#currentTimeMillis}. |
| 120 | * @return Bucket object or null if permissions are insufficient or error happened during |
| 121 | * statistics collection. |
| 122 | */ |
| 123 | public Bucket querySummaryForDevice(int networkType, String subscriberId, |
| 124 | long startTime, long endTime) throws SecurityException, RemoteException { |
| 125 | NetworkTemplate template = createTemplate(networkType, subscriberId); |
| 126 | if (template == null) { |
| 127 | return null; |
| 128 | } |
| 129 | |
| 130 | Bucket bucket = null; |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 131 | NetworkStats stats = new NetworkStats(mContext, template, startTime, endTime); |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 132 | bucket = stats.getDeviceSummaryForNetwork(); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 133 | |
| 134 | stats.close(); |
| 135 | return bucket; |
| 136 | } |
| 137 | |
| 138 | /** |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 139 | * Query network usage statistics summaries aggregated across tags. |
| 140 | * |
| 141 | * #see querySummaryForUser(int, String, long, long, boolean) |
| 142 | */ |
| 143 | public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime, |
| 144 | long endTime) throws SecurityException, RemoteException { |
| 145 | return querySummaryForUser(networkType, subscriberId, startTime, endTime, |
| 146 | false /* includeTags */); |
| 147 | } |
| 148 | |
| 149 | /** |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 150 | * Query network usage statistics summaries. Result is summarised data usage for all uids |
| 151 | * belonging to calling user. Result is a single Bucket aggregated over time, state and uid. |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 152 | * This means the bucket's start and end timestamp are going to be the same as the 'startTime' |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 153 | * and 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL} and uid |
| 154 | * {@link NetworkStats.Bucket#UID_ALL}. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 155 | * |
| 156 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 157 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 158 | * etc. |
| 159 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 160 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 161 | * {@link java.lang.System#currentTimeMillis}. |
| 162 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 163 | * {@link java.lang.System#currentTimeMillis}. |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 164 | * @param includeTags whether to include network tags. If {@code true}, tags will be returned |
| 165 | * and history retention may be shorter. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 166 | * @return Bucket object or null if permissions are insufficient or error happened during |
| 167 | * statistics collection. |
| 168 | */ |
| 169 | public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime, |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 170 | long endTime, boolean includeTags) throws SecurityException, RemoteException { |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 171 | NetworkTemplate template = createTemplate(networkType, subscriberId); |
| 172 | if (template == null) { |
| 173 | return null; |
| 174 | } |
| 175 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 176 | NetworkStats stats; |
| 177 | stats = new NetworkStats(mContext, template, startTime, endTime); |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 178 | stats.startSummaryEnumeration(includeTags); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 179 | |
| 180 | stats.close(); |
| 181 | return stats.getSummaryAggregate(); |
| 182 | } |
| 183 | |
| 184 | /** |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 185 | * Query network usage statistics summaries aggregated across tags. |
| 186 | * |
| 187 | * #see querySummary(int, String, long, long, boolean) |
| 188 | */ |
| 189 | public NetworkStats querySummary(int networkType, String subscriberId, long startTime, |
| 190 | long endTime) throws SecurityException, RemoteException { |
| 191 | return querySummary(networkType, subscriberId, startTime, endTime, false /* includeTags */); |
| 192 | } |
| 193 | |
| 194 | /** |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 195 | * Query network usage statistics summaries. Result filtered to include only uids belonging to |
| 196 | * calling user. Result is aggregated over time, hence all buckets will have the same start and |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 197 | * end timestamps. Not aggregated over state or uid or tag. This means buckets' start and end |
| 198 | * timestamps are going to be the same as the 'startTime' and 'endTime' parameters. State, |
| 199 | * uid and tag are going to vary. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 200 | * |
| 201 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 202 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 203 | * etc. |
| 204 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 205 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 206 | * {@link java.lang.System#currentTimeMillis}. |
| 207 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 208 | * {@link java.lang.System#currentTimeMillis}. |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 209 | * @param includeTags whether to include network tags. If {@code true}, tags will be returned |
| 210 | * and history retention may be shorter. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 211 | * @return Statistics object or null if permissions are insufficient or error happened during |
| 212 | * statistics collection. |
| 213 | */ |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 214 | public NetworkStats querySummary(int networkType, String subscriberId, long startTime, |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 215 | long endTime, boolean includeTags) throws SecurityException, RemoteException { |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 216 | NetworkTemplate template = createTemplate(networkType, subscriberId); |
| 217 | if (template == null) { |
| 218 | return null; |
| 219 | } |
| 220 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 221 | NetworkStats result; |
| 222 | result = new NetworkStats(mContext, template, startTime, endTime); |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 223 | result.startSummaryEnumeration(includeTags); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 224 | |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | /** |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 229 | * Query network usage statistics details for a given uid. |
| 230 | * |
| 231 | * #see queryDetailsForUidTag(int, String, long, long, int, int) |
| 232 | */ |
| 233 | public NetworkStats queryDetailsForUid(int networkType, String subscriberId, |
| 234 | long startTime, long endTime, int uid) throws SecurityException, RemoteException { |
| 235 | return queryDetailsForUidTag(networkType, subscriberId, startTime, endTime, uid, |
| 236 | NetworkStats.Bucket.TAG_ALL); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Query network usage statistics details for a given uid and tag. Only usable for uids |
| 241 | * belonging to calling user. Result is aggregated over state but not aggregated over time. |
| 242 | * This means buckets' start and end timestamps are going to be between 'startTime' and |
| 243 | * 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid the |
| 244 | * same as the 'uid' parameter and tag the same as 'tag' parameter. |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 245 | * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't |
| 246 | * interpolate across partial buckets. Since bucket length is in the order of hours, this |
| 247 | * method cannot be used to measure data usage on a fine grained time scale. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 248 | * |
| 249 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 250 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 251 | * etc. |
| 252 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 253 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 254 | * {@link java.lang.System#currentTimeMillis}. |
| 255 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 256 | * {@link java.lang.System#currentTimeMillis}. |
| 257 | * @param uid UID of app |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 258 | * @param tag TAG of interest. Use {@link NetworkStats.Bucket#TAG_ANY} for any tags, use |
| 259 | * {@link NetworkStats.Bucket#TAG_ALL} to aggregate over tags. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 260 | * @return Statistics object or null if permissions are insufficient or error happened during |
| 261 | * statistics collection. |
| 262 | */ |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 263 | public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId, |
| 264 | long startTime, long endTime, int uid, int tag) throws SecurityException, |
| 265 | RemoteException { |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 266 | NetworkTemplate template = createTemplate(networkType, subscriberId); |
| 267 | if (template == null) { |
| 268 | return null; |
| 269 | } |
| 270 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 271 | NetworkStats result; |
| 272 | result = new NetworkStats(mContext, template, startTime, endTime); |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 273 | result.startHistoryEnumeration(uid, tag); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 274 | |
| 275 | return result; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Query network usage statistics details. Result filtered to include only uids belonging to |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 280 | * calling user. Result is aggregated over state but not aggregated over time or uid. This means |
| 281 | * buckets' start and end timestamps are going to be between 'startTime' and 'endTime' |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 282 | * parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid will vary, |
| 283 | * tag {@link NetworkStats.Bucket#TAG_ALL} and roaming is going to be |
| 284 | * {@link NetworkStats.Bucket#ROAMING_ALL}. |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 285 | * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't |
| 286 | * interpolate across partial buckets. Since bucket length is in the order of hours, this |
| 287 | * method cannot be used to measure data usage on a fine grained time scale. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 288 | * |
| 289 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 290 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 291 | * etc. |
| 292 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 293 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 294 | * {@link java.lang.System#currentTimeMillis}. |
| 295 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 296 | * {@link java.lang.System#currentTimeMillis}. |
| 297 | * @return Statistics object or null if permissions are insufficient or error happened during |
| 298 | * statistics collection. |
| 299 | */ |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 300 | public NetworkStats queryDetails(int networkType, String subscriberId, long startTime, |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 301 | long endTime) throws SecurityException, RemoteException { |
| 302 | NetworkTemplate template = createTemplate(networkType, subscriberId); |
| 303 | if (template == null) { |
| 304 | return null; |
| 305 | } |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 306 | NetworkStats result; |
| 307 | result = new NetworkStats(mContext, template, startTime, endTime); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 308 | result.startUserUidEnumeration(); |
| 309 | return result; |
| 310 | } |
| 311 | |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 312 | /** |
| 313 | * Registers to receive notifications about data usage on specified networks and uids. |
| 314 | * The callbacks will continue to be called as long as the process is live or |
| 315 | * {@link #unregisterDataUsageCallback} is called. |
| 316 | * |
| 317 | * @param policy {@link DataUsagePolicy} describing this request. |
| 318 | * @param callback The {@link DataUsageCallback} that the system will call when data usage |
| 319 | * has exceeded the specified threshold. |
| 320 | */ |
| 321 | public void registerDataUsageCallback(DataUsagePolicy policy, DataUsageCallback callback) { |
| 322 | registerDataUsageCallback(policy, callback, null /* handler */); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Registers to receive notifications about data usage on specified networks and uids. |
| 327 | * The callbacks will continue to be called as long as the process is live or |
| 328 | * {@link #unregisterDataUsageCallback} is called. |
| 329 | * |
| 330 | * @param policy {@link DataUsagePolicy} describing this request. |
| 331 | * @param callback The {@link DataUsageCallback} that the system will call when data usage |
| 332 | * has exceeded the specified threshold. |
| 333 | * @param handler to dispatch callback events through, otherwise if {@code null} it uses |
| 334 | * the calling thread. |
| 335 | */ |
| 336 | public void registerDataUsageCallback(DataUsagePolicy policy, DataUsageCallback callback, |
| 337 | @Nullable Handler handler) { |
| 338 | checkNotNull(policy, "DataUsagePolicy cannot be null"); |
| 339 | checkNotNull(callback, "DataUsageCallback cannot be null"); |
| 340 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 341 | final Looper looper; |
| 342 | if (handler == null) { |
| 343 | looper = Looper.myLooper(); |
| 344 | } else { |
| 345 | looper = handler.getLooper(); |
| 346 | } |
| 347 | |
| 348 | if (DBG) Log.d(TAG, "registerDataUsageCallback called with " + policy); |
| 349 | |
| 350 | NetworkTemplate[] templates; |
| 351 | if (policy.subscriberIds == null || policy.subscriberIds.length == 0) { |
| 352 | templates = new NetworkTemplate[1]; |
| 353 | templates[0] = createTemplate(policy.networkType, null /* subscriberId */); |
| 354 | } else { |
| 355 | templates = new NetworkTemplate[policy.subscriberIds.length]; |
| 356 | for (int i = 0; i < policy.subscriberIds.length; i++) { |
| 357 | templates[i] = createTemplate(policy.networkType, policy.subscriberIds[i]); |
| 358 | } |
| 359 | } |
| 360 | DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, |
| 361 | templates, policy.uids, policy.thresholdInBytes); |
| 362 | try { |
| 363 | CallbackHandler callbackHandler = new CallbackHandler(looper, callback); |
| 364 | callback.request = mService.registerDataUsageCallback( |
| 365 | mContext.getOpPackageName(), request, new Messenger(callbackHandler), |
| 366 | new Binder()); |
| 367 | if (DBG) Log.d(TAG, "registerDataUsageCallback returned " + callback.request); |
| 368 | |
| 369 | if (callback.request == null) { |
| 370 | Log.e(TAG, "Request from callback is null; should not happen"); |
| 371 | } |
| 372 | } catch (RemoteException e) { |
| 373 | if (DBG) Log.d(TAG, "Remote exception when registering callback"); |
| 374 | } |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Unregisters callbacks on data usage. |
| 379 | * |
| 380 | * @param callback The {@link DataUsageCallback} used when registering. |
| 381 | */ |
| 382 | public void unregisterDataUsageCallback(DataUsageCallback callback) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 383 | if (callback == null || callback.request == null |
| 384 | || callback.request.requestId == DataUsageRequest.REQUEST_ID_UNSET) { |
| 385 | throw new IllegalArgumentException("Invalid DataUsageCallback"); |
| 386 | } |
| 387 | try { |
| 388 | mService.unregisterDataUsageRequest(callback.request); |
| 389 | } catch (RemoteException e) { |
| 390 | if (DBG) Log.d(TAG, "Remote exception when unregistering callback"); |
| 391 | } |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Base class for data usage callbacks. Should be extended by applications wanting |
| 396 | * notifications. |
| 397 | */ |
| 398 | public static class DataUsageCallback { |
| 399 | /** |
| 400 | * Called when data usage has reached the given policy threshold. |
| 401 | */ |
| 402 | public void onLimitReached() {} |
| 403 | |
| 404 | private DataUsageRequest request; |
| 405 | } |
| 406 | |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 407 | private static NetworkTemplate createTemplate(int networkType, String subscriberId) { |
| 408 | NetworkTemplate template = null; |
| 409 | switch (networkType) { |
| 410 | case ConnectivityManager.TYPE_MOBILE: { |
| 411 | template = NetworkTemplate.buildTemplateMobileAll(subscriberId); |
| 412 | } break; |
| 413 | case ConnectivityManager.TYPE_WIFI: { |
| 414 | template = NetworkTemplate.buildTemplateWifiWildcard(); |
| 415 | } break; |
| 416 | default: { |
| 417 | Log.w(TAG, "Cannot create template for network type " + networkType |
| 418 | + ", subscriberId '" + NetworkIdentity.scrubSubscriberId(subscriberId) + |
| 419 | "'."); |
| 420 | } |
| 421 | } |
| 422 | return template; |
| 423 | } |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame^] | 424 | |
| 425 | private static class CallbackHandler extends Handler { |
| 426 | private DataUsageCallback mCallback; |
| 427 | CallbackHandler(Looper looper, DataUsageCallback callback) { |
| 428 | super(looper); |
| 429 | mCallback = callback; |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void handleMessage(Message message) { |
| 434 | DataUsageRequest request = |
| 435 | (DataUsageRequest) getObject(message, DataUsageRequest.PARCELABLE_KEY); |
| 436 | |
| 437 | switch (message.what) { |
| 438 | case CALLBACK_LIMIT_REACHED: { |
| 439 | if (mCallback != null) { |
| 440 | mCallback.onLimitReached(); |
| 441 | } else { |
| 442 | Log.e(TAG, "limit reached with released callback for " + request); |
| 443 | } |
| 444 | break; |
| 445 | } |
| 446 | case CALLBACK_RELEASED: { |
| 447 | if (DBG) Log.d(TAG, "callback released for " + request); |
| 448 | mCallback = null; |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | private static Object getObject(Message msg, String key) { |
| 455 | return msg.getData().getParcelable(key); |
| 456 | } |
| 457 | } |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 458 | } |