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; |
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. |
| 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 |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 66 | * {@link NetworkStats.Bucket#ROAMING_ALL}. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 67 | * <p /> |
Zoltan Szatmary-Ban | 3a8b343 | 2016-01-21 10:44:37 +0000 | [diff] [blame] | 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 /> |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 74 | * 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] | 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. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 84 | */ |
| 85 | public class NetworkStatsManager { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 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; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 93 | |
| 94 | private final Context mContext; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 95 | private final INetworkStatsService mService; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * {@hide} |
| 99 | */ |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 100 | public NetworkStatsManager(Context context) throws ServiceNotFoundException { |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 101 | mContext = context; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 102 | mService = INetworkStatsService.Stub.asInterface( |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 103 | ServiceManager.getServiceOrThrow(Context.NETWORK_STATS_SERVICE)); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 104 | } |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 105 | |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 106 | /** |
| 107 | * 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^] | 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}, |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 113 | * and roaming {@link NetworkStats.Bucket#ROAMING_ALL}. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 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 { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 128 | NetworkTemplate template; |
| 129 | try { |
| 130 | template = createTemplate(networkType, subscriberId); |
| 131 | } catch (IllegalArgumentException e) { |
| 132 | if (DBG) Log.e(TAG, "Cannot create template", e); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 133 | return null; |
| 134 | } |
| 135 | |
| 136 | Bucket bucket = null; |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 137 | NetworkStats stats = new NetworkStats(mContext, template, startTime, endTime); |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 138 | bucket = stats.getDeviceSummaryForNetwork(); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 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. |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 147 | * 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^] | 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}. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 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, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 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); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 171 | return null; |
| 172 | } |
| 173 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 174 | NetworkStats stats; |
| 175 | stats = new NetworkStats(mContext, template, startTime, endTime); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 176 | stats.startSummaryEnumeration(); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 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 |
Stephen Chen | 2514787 | 2016-10-21 12:44:26 -0700 | [diff] [blame^] | 185 | * end timestamps. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, |
| 186 | * uid {@link NetworkStats.Bucket#UID_ALL}, tag {@link NetworkStats.Bucket#TAG_NONE}, |
| 187 | * metered {@link NetworkStats.Bucket#METERED_ALL}, and roaming |
| 188 | * {@link NetworkStats.Bucket#ROAMING_ALL}. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 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 | */ |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 201 | public NetworkStats querySummary(int networkType, String subscriberId, long startTime, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 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); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 208 | return null; |
| 209 | } |
| 210 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 211 | NetworkStats result; |
| 212 | result = new NetworkStats(mContext, template, startTime, endTime); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 213 | result.startSummaryEnumeration(); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 214 | |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | /** |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 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, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 226 | NetworkStats.Bucket.TAG_NONE); |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 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. |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 235 | * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't |
| 236 | * interpolate across partial buckets. Since bucket length is in the order of hours, this |
| 237 | * 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] | 238 | * |
| 239 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 240 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 241 | * etc. |
| 242 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 243 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 244 | * {@link java.lang.System#currentTimeMillis}. |
| 245 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 246 | * {@link java.lang.System#currentTimeMillis}. |
| 247 | * @param uid UID of app |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 248 | * @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] | 249 | * @return Statistics object or null if an error happened during statistics collection. |
| 250 | * @throws SecurityException if permissions are insufficient to read network statistics. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 251 | */ |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 252 | public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId, |
Antonio Cansado | 904237f | 2016-05-25 09:57:21 -0700 | [diff] [blame] | 253 | long startTime, long endTime, int uid, int tag) throws SecurityException { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 254 | NetworkTemplate template; |
Antonio Cansado | 904237f | 2016-05-25 09:57:21 -0700 | [diff] [blame] | 255 | template = createTemplate(networkType, subscriberId); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 256 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 257 | NetworkStats result; |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 258 | try { |
| 259 | result = new NetworkStats(mContext, template, startTime, endTime); |
| 260 | result.startHistoryEnumeration(uid, tag); |
| 261 | } catch (RemoteException e) { |
| 262 | Log.e(TAG, "Error while querying stats for uid=" + uid + " tag=" + tag, e); |
| 263 | return null; |
| 264 | } |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 265 | |
| 266 | return result; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * 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^] | 271 | * calling user. Result is aggregated over state but not aggregated over time, uid, tag, |
| 272 | * metered, nor roaming. This means buckets' start and end timestamps are going to be between |
| 273 | * 'startTime' and 'endTime' parameters. State is going to be |
| 274 | * {@link NetworkStats.Bucket#STATE_ALL}, uid will vary, |
| 275 | * tag {@link NetworkStats.Bucket#TAG_NONE}, metered is going to be |
| 276 | * {@link NetworkStats.Bucket#METERED_ALL}, and roaming is going to be |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 277 | * {@link NetworkStats.Bucket#ROAMING_ALL}. |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 278 | * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't |
| 279 | * interpolate across partial buckets. Since bucket length is in the order of hours, this |
| 280 | * 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] | 281 | * |
| 282 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 283 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 284 | * etc. |
| 285 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 286 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 287 | * {@link java.lang.System#currentTimeMillis}. |
| 288 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 289 | * {@link java.lang.System#currentTimeMillis}. |
| 290 | * @return Statistics object or null if permissions are insufficient or error happened during |
| 291 | * statistics collection. |
| 292 | */ |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 293 | public NetworkStats queryDetails(int networkType, String subscriberId, long startTime, |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 294 | long endTime) throws SecurityException, RemoteException { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 295 | NetworkTemplate template; |
| 296 | try { |
| 297 | template = createTemplate(networkType, subscriberId); |
| 298 | } catch (IllegalArgumentException e) { |
| 299 | if (DBG) Log.e(TAG, "Cannot create template", e); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 300 | return null; |
| 301 | } |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 302 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 303 | NetworkStats result; |
| 304 | result = new NetworkStats(mContext, template, startTime, endTime); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 305 | result.startUserUidEnumeration(); |
| 306 | return result; |
| 307 | } |
| 308 | |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 309 | /** |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 310 | * Registers to receive notifications about data usage on specified networks. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 311 | * |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 312 | * #see registerUsageCallback(int, String[], long, UsageCallback, Handler) |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 313 | */ |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 314 | public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes, |
| 315 | UsageCallback callback) { |
Antonio Cansado | f704837 | 2016-06-20 15:03:03 -0700 | [diff] [blame] | 316 | registerUsageCallback(networkType, subscriberId, thresholdBytes, callback, |
| 317 | null /* handler */); |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 321 | * Registers to receive notifications about data usage on specified networks. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 322 | * |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 323 | * <p>The callbacks will continue to be called as long as the process is live or |
| 324 | * {@link #unregisterUsageCallback} is called. |
| 325 | * |
| 326 | * @param networkType Type of network to monitor. Either |
| 327 | {@link ConnectivityManager#TYPE_MOBILE} or {@link ConnectivityManager#TYPE_WIFI}. |
| 328 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 329 | * @param thresholdBytes Threshold in bytes to be notified on. |
| 330 | * @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] | 331 | * has exceeded the specified threshold. |
| 332 | * @param handler to dispatch callback events through, otherwise if {@code null} it uses |
| 333 | * the calling thread. |
| 334 | */ |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 335 | public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes, |
| 336 | UsageCallback callback, @Nullable Handler handler) { |
| 337 | checkNotNull(callback, "UsageCallback cannot be null"); |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 338 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 339 | final Looper looper; |
| 340 | if (handler == null) { |
| 341 | looper = Looper.myLooper(); |
| 342 | } else { |
| 343 | looper = handler.getLooper(); |
| 344 | } |
| 345 | |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 346 | if (DBG) { |
| 347 | Log.d(TAG, "registerUsageCallback called with: {" |
| 348 | + " networkType=" + networkType |
| 349 | + " subscriberId=" + subscriberId |
| 350 | + " thresholdBytes=" + thresholdBytes |
| 351 | + " }"); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 352 | } |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 353 | |
| 354 | NetworkTemplate template = createTemplate(networkType, subscriberId); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 355 | DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 356 | template, thresholdBytes); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 357 | try { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 358 | CallbackHandler callbackHandler = new CallbackHandler(looper, networkType, |
| 359 | subscriberId, callback); |
| 360 | callback.request = mService.registerUsageCallback( |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 361 | mContext.getOpPackageName(), request, new Messenger(callbackHandler), |
| 362 | new Binder()); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 363 | if (DBG) Log.d(TAG, "registerUsageCallback returned " + callback.request); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 364 | |
| 365 | if (callback.request == null) { |
| 366 | Log.e(TAG, "Request from callback is null; should not happen"); |
| 367 | } |
| 368 | } catch (RemoteException e) { |
| 369 | if (DBG) Log.d(TAG, "Remote exception when registering callback"); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 370 | throw e.rethrowFromSystemServer(); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 371 | } |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Unregisters callbacks on data usage. |
| 376 | * |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 377 | * @param callback The {@link UsageCallback} used when registering. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 378 | */ |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 379 | public void unregisterUsageCallback(UsageCallback callback) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 380 | if (callback == null || callback.request == null |
| 381 | || callback.request.requestId == DataUsageRequest.REQUEST_ID_UNSET) { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 382 | throw new IllegalArgumentException("Invalid UsageCallback"); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 383 | } |
| 384 | try { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 385 | mService.unregisterUsageRequest(callback.request); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 386 | } catch (RemoteException e) { |
| 387 | if (DBG) Log.d(TAG, "Remote exception when unregistering callback"); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 388 | throw e.rethrowFromSystemServer(); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 389 | } |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 392 | /** |
| 393 | * Base class for usage callbacks. Should be extended by applications wanting notifications. |
| 394 | */ |
| 395 | public static abstract class UsageCallback { |
| 396 | |
| 397 | /** |
| 398 | * Called when data usage has reached the given threshold. |
| 399 | */ |
| 400 | public abstract void onThresholdReached(int networkType, String subscriberId); |
| 401 | |
| 402 | /** |
| 403 | * @hide used for internal bookkeeping |
| 404 | */ |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 405 | private DataUsageRequest request; |
| 406 | } |
| 407 | |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 408 | private static NetworkTemplate createTemplate(int networkType, String subscriberId) { |
| 409 | NetworkTemplate template = null; |
| 410 | switch (networkType) { |
| 411 | case ConnectivityManager.TYPE_MOBILE: { |
| 412 | template = NetworkTemplate.buildTemplateMobileAll(subscriberId); |
| 413 | } break; |
| 414 | case ConnectivityManager.TYPE_WIFI: { |
| 415 | template = NetworkTemplate.buildTemplateWifiWildcard(); |
| 416 | } break; |
| 417 | default: { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 418 | throw new IllegalArgumentException("Cannot create template for network type " |
| 419 | + networkType + ", subscriberId '" |
| 420 | + NetworkIdentity.scrubSubscriberId(subscriberId) + "'."); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | return template; |
| 424 | } |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 425 | |
| 426 | private static class CallbackHandler extends Handler { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 427 | private final int mNetworkType; |
| 428 | private final String mSubscriberId; |
| 429 | private UsageCallback mCallback; |
| 430 | |
| 431 | CallbackHandler(Looper looper, int networkType, String subscriberId, |
| 432 | UsageCallback callback) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 433 | super(looper); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 434 | mNetworkType = networkType; |
| 435 | mSubscriberId = subscriberId; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 436 | mCallback = callback; |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public void handleMessage(Message message) { |
| 441 | DataUsageRequest request = |
| 442 | (DataUsageRequest) getObject(message, DataUsageRequest.PARCELABLE_KEY); |
| 443 | |
| 444 | switch (message.what) { |
| 445 | case CALLBACK_LIMIT_REACHED: { |
| 446 | if (mCallback != null) { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 447 | mCallback.onThresholdReached(mNetworkType, mSubscriberId); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 448 | } else { |
| 449 | Log.e(TAG, "limit reached with released callback for " + request); |
| 450 | } |
| 451 | break; |
| 452 | } |
| 453 | case CALLBACK_RELEASED: { |
| 454 | if (DBG) Log.d(TAG, "callback released for " + request); |
| 455 | mCallback = null; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | private static Object getObject(Message msg, String key) { |
| 462 | return msg.getData().getParcelable(key); |
| 463 | } |
| 464 | } |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 465 | } |