blob: da5aa61f1f2131ac74f97fc92f2b3bd29725d9b9 [file] [log] [blame]
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +00001/**
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
6 * of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17package android.app.usage;
18
Antonio Cansadoba8288d2015-12-02 08:42:54 -080019import static com.android.internal.util.Preconditions.checkNotNull;
20
21import android.annotation.Nullable;
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +010022import android.app.usage.NetworkStats.Bucket;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000023import android.content.Context;
24import android.net.ConnectivityManager;
Antonio Cansadoba8288d2015-12-02 08:42:54 -080025import android.net.DataUsageRequest;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000026import android.net.NetworkIdentity;
27import android.net.NetworkTemplate;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080028import android.net.INetworkStatsService;
29import android.os.Binder;
Jeff Davidson1efb1332015-12-09 18:04:50 -080030import android.os.Build;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080031import android.os.Message;
32import android.os.Messenger;
Antonio Cansadoba8288d2015-12-02 08:42:54 -080033import android.os.Handler;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080034import android.os.Looper;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000035import android.os.RemoteException;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080036import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060037import android.os.ServiceManager.ServiceNotFoundException;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000038import android.util.Log;
39
40/**
41 * Provides access to network usage history and statistics. Usage data is collected in
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +010042 * discrete bins of time called 'Buckets'. See {@link NetworkStats.Bucket} for details.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000043 * <p />
44 * Queries can define a time interval in the form of start and end timestamps (Long.MIN_VALUE and
Jeff Davidson1efb1332015-12-09 18:04:50 -080045 * 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-Ban9c5dfa52015-02-23 17:20:20 +000048 * <h3>
49 * Summary queries
50 * </h3>
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +010051 * {@link #querySummaryForDevice} <p />
52 * {@link #querySummaryForUser} <p />
53 * {@link #querySummary} <p />
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000054 * These queries aggregate network usage across the whole interval. Therefore there will be only one
Stephen Chen25147872016-10-21 12:44:26 -070055 * 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-Ban9c5dfa52015-02-23 17:20:20 +000057 * <h3>
58 * History queries
59 * </h3>
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +010060 * {@link #queryDetailsForUid} <p />
61 * {@link #queryDetails} <p />
Stephen Chen25147872016-10-21 12:44:26 -070062 * 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 Davidsona6a78072016-01-11 16:02:17 -080066 * {@link NetworkStats.Bucket#ROAMING_ALL}.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000067 * <p />
Zoltan Szatmary-Ban3a8b3432016-01-21 10:44:37 +000068 * <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-Ban9c5dfa52015-02-23 17:20:20 +000074 * Profile owner apps are automatically granted permission to query data on the profile they manage
Jeff Davidson1efb1332015-12-09 18:04:50 -080075 * (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-Ban9c5dfa52015-02-23 17:20:20 +000084 */
85public class NetworkStatsManager {
Antonio Cansadocd42acd2016-02-17 13:03:38 -080086 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-Ban9c5dfa52015-02-23 17:20:20 +000093
94 private final Context mContext;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080095 private final INetworkStatsService mService;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000096
97 /**
98 * {@hide}
99 */
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600100 public NetworkStatsManager(Context context) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000101 mContext = context;
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800102 mService = INetworkStatsService.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600103 ServiceManager.getServiceOrThrow(Context.NETWORK_STATS_SERVICE));
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000104 }
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800105
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000106 /**
107 * Query network usage statistics summaries. Result is summarised data usage for the whole
Stephen Chen25147872016-10-21 12:44:26 -0700108 * 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 Cansado46c753672015-12-10 15:57:56 -0800113 * and roaming {@link NetworkStats.Bucket#ROAMING_ALL}.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000114 *
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 Cansado6965c182016-03-30 11:37:18 -0700128 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-Ban9c5dfa52015-02-23 17:20:20 +0000133 return null;
134 }
135
136 Bucket bucket = null;
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100137 NetworkStats stats = new NetworkStats(mContext, template, startTime, endTime);
Zoltan Szatmary-Ban72027d22015-06-16 15:49:16 +0100138 bucket = stats.getDeviceSummaryForNetwork();
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000139
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-Ban72027d22015-06-16 15:49:16 +0100147 * This means the bucket's start and end timestamp are going to be the same as the 'startTime'
Stephen Chen25147872016-10-21 12:44:26 -0700148 * 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-Ban9c5dfa52015-02-23 17:20:20 +0000152 *
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 Cansado6965c182016-03-30 11:37:18 -0700165 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-Ban9c5dfa52015-02-23 17:20:20 +0000171 return null;
172 }
173
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100174 NetworkStats stats;
175 stats = new NetworkStats(mContext, template, startTime, endTime);
Antonio Cansado6965c182016-03-30 11:37:18 -0700176 stats.startSummaryEnumeration();
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000177
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 Chen25147872016-10-21 12:44:26 -0700185 * 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-Ban9c5dfa52015-02-23 17:20:20 +0000189 *
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-Ban381483b2015-05-13 17:53:17 +0100201 public NetworkStats querySummary(int networkType, String subscriberId, long startTime,
Antonio Cansado6965c182016-03-30 11:37:18 -0700202 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-Ban9c5dfa52015-02-23 17:20:20 +0000208 return null;
209 }
210
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100211 NetworkStats result;
212 result = new NetworkStats(mContext, template, startTime, endTime);
Antonio Cansado6965c182016-03-30 11:37:18 -0700213 result.startSummaryEnumeration();
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000214
215 return result;
216 }
217
218 /**
Antonio Cansado46c753672015-12-10 15:57:56 -0800219 * 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 Cansado6965c182016-03-30 11:37:18 -0700226 NetworkStats.Bucket.TAG_NONE);
Antonio Cansado46c753672015-12-10 15:57:56 -0800227 }
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-Ban72027d22015-06-16 15:49:16 +0100235 * <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-Ban9c5dfa52015-02-23 17:20:20 +0000238 *
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 Cansado6965c182016-03-30 11:37:18 -0700248 * @param tag TAG of interest. Use {@link NetworkStats.Bucket#TAG_NONE} for no tags.
Antonio Cansado904237f2016-05-25 09:57:21 -0700249 * @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-Ban9c5dfa52015-02-23 17:20:20 +0000251 */
Antonio Cansado46c753672015-12-10 15:57:56 -0800252 public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId,
Antonio Cansado904237f2016-05-25 09:57:21 -0700253 long startTime, long endTime, int uid, int tag) throws SecurityException {
Antonio Cansado6965c182016-03-30 11:37:18 -0700254 NetworkTemplate template;
Antonio Cansado904237f2016-05-25 09:57:21 -0700255 template = createTemplate(networkType, subscriberId);
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000256
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100257 NetworkStats result;
Antonio Cansado6965c182016-03-30 11:37:18 -0700258 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-Ban9c5dfa52015-02-23 17:20:20 +0000265
266 return result;
267 }
268
269 /**
270 * Query network usage statistics details. Result filtered to include only uids belonging to
Stephen Chen25147872016-10-21 12:44:26 -0700271 * 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 Cansado46c753672015-12-10 15:57:56 -0800277 * {@link NetworkStats.Bucket#ROAMING_ALL}.
Zoltan Szatmary-Ban72027d22015-06-16 15:49:16 +0100278 * <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-Ban9c5dfa52015-02-23 17:20:20 +0000281 *
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-Ban381483b2015-05-13 17:53:17 +0100293 public NetworkStats queryDetails(int networkType, String subscriberId, long startTime,
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000294 long endTime) throws SecurityException, RemoteException {
Antonio Cansado6965c182016-03-30 11:37:18 -0700295 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-Ban9c5dfa52015-02-23 17:20:20 +0000300 return null;
301 }
Antonio Cansado6965c182016-03-30 11:37:18 -0700302
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100303 NetworkStats result;
304 result = new NetworkStats(mContext, template, startTime, endTime);
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000305 result.startUserUidEnumeration();
306 return result;
307 }
308
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800309 /**
Antonio Cansado6965c182016-03-30 11:37:18 -0700310 * Registers to receive notifications about data usage on specified networks.
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800311 *
Antonio Cansado6965c182016-03-30 11:37:18 -0700312 * #see registerUsageCallback(int, String[], long, UsageCallback, Handler)
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800313 */
Antonio Cansado6965c182016-03-30 11:37:18 -0700314 public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes,
315 UsageCallback callback) {
Antonio Cansadof7048372016-06-20 15:03:03 -0700316 registerUsageCallback(networkType, subscriberId, thresholdBytes, callback,
317 null /* handler */);
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800318 }
319
320 /**
Antonio Cansado6965c182016-03-30 11:37:18 -0700321 * Registers to receive notifications about data usage on specified networks.
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800322 *
Antonio Cansado6965c182016-03-30 11:37:18 -0700323 * <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 Cansadoba8288d2015-12-02 08:42:54 -0800331 * 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 Cansado6965c182016-03-30 11:37:18 -0700335 public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes,
336 UsageCallback callback, @Nullable Handler handler) {
337 checkNotNull(callback, "UsageCallback cannot be null");
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800338
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800339 final Looper looper;
340 if (handler == null) {
341 looper = Looper.myLooper();
342 } else {
343 looper = handler.getLooper();
344 }
345
Antonio Cansado6965c182016-03-30 11:37:18 -0700346 if (DBG) {
347 Log.d(TAG, "registerUsageCallback called with: {"
348 + " networkType=" + networkType
349 + " subscriberId=" + subscriberId
350 + " thresholdBytes=" + thresholdBytes
351 + " }");
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800352 }
Antonio Cansado6965c182016-03-30 11:37:18 -0700353
354 NetworkTemplate template = createTemplate(networkType, subscriberId);
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800355 DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET,
Antonio Cansado6965c182016-03-30 11:37:18 -0700356 template, thresholdBytes);
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800357 try {
Antonio Cansado6965c182016-03-30 11:37:18 -0700358 CallbackHandler callbackHandler = new CallbackHandler(looper, networkType,
359 subscriberId, callback);
360 callback.request = mService.registerUsageCallback(
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800361 mContext.getOpPackageName(), request, new Messenger(callbackHandler),
362 new Binder());
Antonio Cansado6965c182016-03-30 11:37:18 -0700363 if (DBG) Log.d(TAG, "registerUsageCallback returned " + callback.request);
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800364
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 Cansado6965c182016-03-30 11:37:18 -0700370 throw e.rethrowFromSystemServer();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800371 }
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800372 }
373
374 /**
375 * Unregisters callbacks on data usage.
376 *
Antonio Cansado6965c182016-03-30 11:37:18 -0700377 * @param callback The {@link UsageCallback} used when registering.
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800378 */
Antonio Cansado6965c182016-03-30 11:37:18 -0700379 public void unregisterUsageCallback(UsageCallback callback) {
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800380 if (callback == null || callback.request == null
381 || callback.request.requestId == DataUsageRequest.REQUEST_ID_UNSET) {
Antonio Cansado6965c182016-03-30 11:37:18 -0700382 throw new IllegalArgumentException("Invalid UsageCallback");
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800383 }
384 try {
Antonio Cansado6965c182016-03-30 11:37:18 -0700385 mService.unregisterUsageRequest(callback.request);
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800386 } catch (RemoteException e) {
387 if (DBG) Log.d(TAG, "Remote exception when unregistering callback");
Antonio Cansado6965c182016-03-30 11:37:18 -0700388 throw e.rethrowFromSystemServer();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800389 }
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800390 }
391
Antonio Cansado6965c182016-03-30 11:37:18 -0700392 /**
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 Cansadoba8288d2015-12-02 08:42:54 -0800405 private DataUsageRequest request;
406 }
407
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000408 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 Cansado6965c182016-03-30 11:37:18 -0700418 throw new IllegalArgumentException("Cannot create template for network type "
419 + networkType + ", subscriberId '"
420 + NetworkIdentity.scrubSubscriberId(subscriberId) + "'.");
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000421 }
422 }
423 return template;
424 }
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800425
426 private static class CallbackHandler extends Handler {
Antonio Cansado6965c182016-03-30 11:37:18 -0700427 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 Cansadocd42acd2016-02-17 13:03:38 -0800433 super(looper);
Antonio Cansado6965c182016-03-30 11:37:18 -0700434 mNetworkType = networkType;
435 mSubscriberId = subscriberId;
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800436 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 Cansado6965c182016-03-30 11:37:18 -0700447 mCallback.onThresholdReached(mNetworkType, mSubscriberId);
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800448 } 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-Ban9c5dfa52015-02-23 17:20:20 +0000465}