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