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