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 |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 109 | * {@link NetworkStats.Bucket#UID_ALL}, tag {@link NetworkStats.Bucket#TAG_NONE} |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 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 { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 125 | NetworkTemplate template; |
| 126 | try { |
| 127 | template = createTemplate(networkType, subscriberId); |
| 128 | } catch (IllegalArgumentException e) { |
| 129 | if (DBG) Log.e(TAG, "Cannot create template", e); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 130 | return null; |
| 131 | } |
| 132 | |
| 133 | Bucket bucket = null; |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 134 | NetworkStats stats = new NetworkStats(mContext, template, startTime, endTime); |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 135 | bucket = stats.getDeviceSummaryForNetwork(); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 136 | |
| 137 | stats.close(); |
| 138 | return bucket; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Query network usage statistics summaries. Result is summarised data usage for all uids |
| 143 | * 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] | 144 | * This means the bucket's start and end timestamp are going to be the same as the 'startTime' |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 145 | * and 'endTime' parameters, state is going to be {@link NetworkStats.Bucket#STATE_ALL} and uid |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 146 | * {@link NetworkStats.Bucket#UID_ALL}. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 147 | * |
| 148 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 149 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 150 | * etc. |
| 151 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 152 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 153 | * {@link java.lang.System#currentTimeMillis}. |
| 154 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 155 | * {@link java.lang.System#currentTimeMillis}. |
| 156 | * @return Bucket object or null if permissions are insufficient or error happened during |
| 157 | * statistics collection. |
| 158 | */ |
| 159 | public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 160 | long endTime) throws SecurityException, RemoteException { |
| 161 | NetworkTemplate template; |
| 162 | try { |
| 163 | template = createTemplate(networkType, subscriberId); |
| 164 | } catch (IllegalArgumentException e) { |
| 165 | if (DBG) Log.e(TAG, "Cannot create template", e); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 166 | return null; |
| 167 | } |
| 168 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 169 | NetworkStats stats; |
| 170 | stats = new NetworkStats(mContext, template, startTime, endTime); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 171 | stats.startSummaryEnumeration(); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 172 | |
| 173 | stats.close(); |
| 174 | return stats.getSummaryAggregate(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Query network usage statistics summaries. Result filtered to include only uids belonging to |
| 179 | * calling user. Result is aggregated over time, hence all buckets will have the same start and |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 180 | * end timestamps. Not aggregated over state or uid. This means buckets' start and end |
| 181 | * timestamps are going to be the same as the 'startTime' and 'endTime' parameters. |
| 182 | * State and uid are going to vary, and tag is going to be the same. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 183 | * |
| 184 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 185 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 186 | * etc. |
| 187 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 188 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 189 | * {@link java.lang.System#currentTimeMillis}. |
| 190 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 191 | * {@link java.lang.System#currentTimeMillis}. |
| 192 | * @return Statistics object or null if permissions are insufficient or error happened during |
| 193 | * statistics collection. |
| 194 | */ |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 195 | public NetworkStats querySummary(int networkType, String subscriberId, long startTime, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 196 | long endTime) throws SecurityException, RemoteException { |
| 197 | NetworkTemplate template; |
| 198 | try { |
| 199 | template = createTemplate(networkType, subscriberId); |
| 200 | } catch (IllegalArgumentException e) { |
| 201 | if (DBG) Log.e(TAG, "Cannot create template", e); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 202 | return null; |
| 203 | } |
| 204 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 205 | NetworkStats result; |
| 206 | result = new NetworkStats(mContext, template, startTime, endTime); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 207 | result.startSummaryEnumeration(); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 208 | |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | /** |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 213 | * Query network usage statistics details for a given uid. |
| 214 | * |
| 215 | * #see queryDetailsForUidTag(int, String, long, long, int, int) |
| 216 | */ |
| 217 | public NetworkStats queryDetailsForUid(int networkType, String subscriberId, |
| 218 | long startTime, long endTime, int uid) throws SecurityException, RemoteException { |
| 219 | return queryDetailsForUidTag(networkType, subscriberId, startTime, endTime, uid, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 220 | NetworkStats.Bucket.TAG_NONE); |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Query network usage statistics details for a given uid and tag. Only usable for uids |
| 225 | * belonging to calling user. Result is aggregated over state but not aggregated over time. |
| 226 | * This means buckets' start and end timestamps are going to be between 'startTime' and |
| 227 | * 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid the |
| 228 | * 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] | 229 | * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't |
| 230 | * interpolate across partial buckets. Since bucket length is in the order of hours, this |
| 231 | * 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] | 232 | * |
| 233 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 234 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 235 | * etc. |
| 236 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 237 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 238 | * {@link java.lang.System#currentTimeMillis}. |
| 239 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 240 | * {@link java.lang.System#currentTimeMillis}. |
| 241 | * @param uid UID of app |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 242 | * @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] | 243 | * @return Statistics object or null if an error happened during statistics collection. |
| 244 | * @throws SecurityException if permissions are insufficient to read network statistics. |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 245 | */ |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 246 | public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId, |
Antonio Cansado | 904237f | 2016-05-25 09:57:21 -0700 | [diff] [blame] | 247 | long startTime, long endTime, int uid, int tag) throws SecurityException { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 248 | NetworkTemplate template; |
Antonio Cansado | 904237f | 2016-05-25 09:57:21 -0700 | [diff] [blame] | 249 | template = createTemplate(networkType, subscriberId); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 250 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 251 | NetworkStats result; |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 252 | try { |
| 253 | result = new NetworkStats(mContext, template, startTime, endTime); |
| 254 | result.startHistoryEnumeration(uid, tag); |
| 255 | } catch (RemoteException e) { |
| 256 | Log.e(TAG, "Error while querying stats for uid=" + uid + " tag=" + tag, e); |
| 257 | return null; |
| 258 | } |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 259 | |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * 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] | 265 | * calling user. Result is aggregated over state but not aggregated over time or uid. This means |
| 266 | * 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] | 267 | * parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid will vary, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 268 | * tag {@link NetworkStats.Bucket#TAG_NONE} and roaming is going to be |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 269 | * {@link NetworkStats.Bucket#ROAMING_ALL}. |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 270 | * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't |
| 271 | * interpolate across partial buckets. Since bucket length is in the order of hours, this |
| 272 | * 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] | 273 | * |
| 274 | * @param networkType As defined in {@link ConnectivityManager}, e.g. |
| 275 | * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} |
| 276 | * etc. |
| 277 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 278 | * @param startTime Start of period. Defined in terms of "Unix time", see |
| 279 | * {@link java.lang.System#currentTimeMillis}. |
| 280 | * @param endTime End of period. Defined in terms of "Unix time", see |
| 281 | * {@link java.lang.System#currentTimeMillis}. |
| 282 | * @return Statistics object or null if permissions are insufficient or error happened during |
| 283 | * statistics collection. |
| 284 | */ |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 285 | public NetworkStats queryDetails(int networkType, String subscriberId, long startTime, |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 286 | long endTime) throws SecurityException, RemoteException { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 287 | NetworkTemplate template; |
| 288 | try { |
| 289 | template = createTemplate(networkType, subscriberId); |
| 290 | } catch (IllegalArgumentException e) { |
| 291 | if (DBG) Log.e(TAG, "Cannot create template", e); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 292 | return null; |
| 293 | } |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 294 | |
Zoltan Szatmary-Ban | 381483b | 2015-05-13 17:53:17 +0100 | [diff] [blame] | 295 | NetworkStats result; |
| 296 | result = new NetworkStats(mContext, template, startTime, endTime); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 297 | result.startUserUidEnumeration(); |
| 298 | return result; |
| 299 | } |
| 300 | |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 301 | /** |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 302 | * Registers to receive notifications about data usage on specified networks. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 303 | * |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 304 | * #see registerUsageCallback(int, String[], long, UsageCallback, Handler) |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 305 | */ |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 306 | public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes, |
| 307 | UsageCallback callback) { |
Antonio Cansado | f704837 | 2016-06-20 15:03:03 -0700 | [diff] [blame] | 308 | registerUsageCallback(networkType, subscriberId, thresholdBytes, callback, |
| 309 | null /* handler */); |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 313 | * Registers to receive notifications about data usage on specified networks. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 314 | * |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 315 | * <p>The callbacks will continue to be called as long as the process is live or |
| 316 | * {@link #unregisterUsageCallback} is called. |
| 317 | * |
| 318 | * @param networkType Type of network to monitor. Either |
| 319 | {@link ConnectivityManager#TYPE_MOBILE} or {@link ConnectivityManager#TYPE_WIFI}. |
| 320 | * @param subscriberId If applicable, the subscriber id of the network interface. |
| 321 | * @param thresholdBytes Threshold in bytes to be notified on. |
| 322 | * @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] | 323 | * has exceeded the specified threshold. |
| 324 | * @param handler to dispatch callback events through, otherwise if {@code null} it uses |
| 325 | * the calling thread. |
| 326 | */ |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 327 | public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes, |
| 328 | UsageCallback callback, @Nullable Handler handler) { |
| 329 | checkNotNull(callback, "UsageCallback cannot be null"); |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 330 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 331 | final Looper looper; |
| 332 | if (handler == null) { |
| 333 | looper = Looper.myLooper(); |
| 334 | } else { |
| 335 | looper = handler.getLooper(); |
| 336 | } |
| 337 | |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 338 | if (DBG) { |
| 339 | Log.d(TAG, "registerUsageCallback called with: {" |
| 340 | + " networkType=" + networkType |
| 341 | + " subscriberId=" + subscriberId |
| 342 | + " thresholdBytes=" + thresholdBytes |
| 343 | + " }"); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 344 | } |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 345 | |
| 346 | NetworkTemplate template = createTemplate(networkType, subscriberId); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 347 | DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 348 | template, thresholdBytes); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 349 | try { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 350 | CallbackHandler callbackHandler = new CallbackHandler(looper, networkType, |
| 351 | subscriberId, callback); |
| 352 | callback.request = mService.registerUsageCallback( |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 353 | mContext.getOpPackageName(), request, new Messenger(callbackHandler), |
| 354 | new Binder()); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 355 | if (DBG) Log.d(TAG, "registerUsageCallback returned " + callback.request); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 356 | |
| 357 | if (callback.request == null) { |
| 358 | Log.e(TAG, "Request from callback is null; should not happen"); |
| 359 | } |
| 360 | } catch (RemoteException e) { |
| 361 | if (DBG) Log.d(TAG, "Remote exception when registering callback"); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 362 | throw e.rethrowFromSystemServer(); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 363 | } |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Unregisters callbacks on data usage. |
| 368 | * |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 369 | * @param callback The {@link UsageCallback} used when registering. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 370 | */ |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 371 | public void unregisterUsageCallback(UsageCallback callback) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 372 | if (callback == null || callback.request == null |
| 373 | || callback.request.requestId == DataUsageRequest.REQUEST_ID_UNSET) { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 374 | throw new IllegalArgumentException("Invalid UsageCallback"); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 375 | } |
| 376 | try { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 377 | mService.unregisterUsageRequest(callback.request); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 378 | } catch (RemoteException e) { |
| 379 | if (DBG) Log.d(TAG, "Remote exception when unregistering callback"); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 380 | throw e.rethrowFromSystemServer(); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 381 | } |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 384 | /** |
| 385 | * Base class for usage callbacks. Should be extended by applications wanting notifications. |
| 386 | */ |
| 387 | public static abstract class UsageCallback { |
| 388 | |
| 389 | /** |
| 390 | * Called when data usage has reached the given threshold. |
| 391 | */ |
| 392 | public abstract void onThresholdReached(int networkType, String subscriberId); |
| 393 | |
| 394 | /** |
| 395 | * @hide used for internal bookkeeping |
| 396 | */ |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 397 | private DataUsageRequest request; |
| 398 | } |
| 399 | |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 400 | private static NetworkTemplate createTemplate(int networkType, String subscriberId) { |
| 401 | NetworkTemplate template = null; |
| 402 | switch (networkType) { |
| 403 | case ConnectivityManager.TYPE_MOBILE: { |
| 404 | template = NetworkTemplate.buildTemplateMobileAll(subscriberId); |
| 405 | } break; |
| 406 | case ConnectivityManager.TYPE_WIFI: { |
| 407 | template = NetworkTemplate.buildTemplateWifiWildcard(); |
| 408 | } break; |
| 409 | default: { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 410 | throw new IllegalArgumentException("Cannot create template for network type " |
| 411 | + networkType + ", subscriberId '" |
| 412 | + NetworkIdentity.scrubSubscriberId(subscriberId) + "'."); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | return template; |
| 416 | } |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 417 | |
| 418 | private static class CallbackHandler extends Handler { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 419 | private final int mNetworkType; |
| 420 | private final String mSubscriberId; |
| 421 | private UsageCallback mCallback; |
| 422 | |
| 423 | CallbackHandler(Looper looper, int networkType, String subscriberId, |
| 424 | UsageCallback callback) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 425 | super(looper); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 426 | mNetworkType = networkType; |
| 427 | mSubscriberId = subscriberId; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 428 | mCallback = callback; |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void handleMessage(Message message) { |
| 433 | DataUsageRequest request = |
| 434 | (DataUsageRequest) getObject(message, DataUsageRequest.PARCELABLE_KEY); |
| 435 | |
| 436 | switch (message.what) { |
| 437 | case CALLBACK_LIMIT_REACHED: { |
| 438 | if (mCallback != null) { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 439 | mCallback.onThresholdReached(mNetworkType, mSubscriberId); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 440 | } else { |
| 441 | Log.e(TAG, "limit reached with released callback for " + request); |
| 442 | } |
| 443 | break; |
| 444 | } |
| 445 | case CALLBACK_RELEASED: { |
| 446 | if (DBG) Log.d(TAG, "callback released for " + request); |
| 447 | mCallback = null; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | private static Object getObject(Message msg, String key) { |
| 454 | return msg.getData().getParcelable(key); |
| 455 | } |
| 456 | } |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 457 | } |