blob: 2e3aca46f904af30631eead0ac71ba0d8775d866 [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;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000037import android.util.Log;
38
39/**
40 * Provides access to network usage history and statistics. Usage data is collected in
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +010041 * discrete bins of time called 'Buckets'. See {@link NetworkStats.Bucket} for details.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000042 * <p />
43 * 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 -080044 * 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-Ban9c5dfa52015-02-23 17:20:20 +000047 * <h3>
48 * Summary queries
49 * </h3>
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +010050 * {@link #querySummaryForDevice} <p />
51 * {@link #querySummaryForUser} <p />
52 * {@link #querySummary} <p />
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000053 * These queries aggregate network usage across the whole interval. Therefore there will be only one
Jeff Davidsona6a78072016-01-11 16:02:17 -080054 * 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-Ban9c5dfa52015-02-23 17:20:20 +000056 * <h3>
57 * History queries
58 * </h3>
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +010059 * {@link #queryDetailsForUid} <p />
60 * {@link #queryDetails} <p />
Jeff Davidsona6a78072016-01-11 16:02:17 -080061 * 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-Ban9c5dfa52015-02-23 17:20:20 +000065 * <p />
Zoltan Szatmary-Ban3a8b3432016-01-21 10:44:37 +000066 * <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-Ban9c5dfa52015-02-23 17:20:20 +000072 * Profile owner apps are automatically granted permission to query data on the profile they manage
Jeff Davidson1efb1332015-12-09 18:04:50 -080073 * (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-Ban9c5dfa52015-02-23 17:20:20 +000082 */
83public class NetworkStatsManager {
Antonio Cansadocd42acd2016-02-17 13:03:38 -080084 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-Ban9c5dfa52015-02-23 17:20:20 +000091
92 private final Context mContext;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080093 private final INetworkStatsService mService;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000094
95 /**
96 * {@hide}
97 */
98 public NetworkStatsManager(Context context) {
99 mContext = context;
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800100 mService = INetworkStatsService.Stub.asInterface(
101 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000102 }
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800103
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000104 /**
105 * Query network usage statistics summaries. Result is summarised data usage for the whole
Antonio Cansado46c753672015-12-10 15:57:56 -0800106 * 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
109 * {@link NetworkStats.Bucket#UID_ALL}, tag {@link NetworkStats.Bucket#TAG_ALL}
110 * and roaming {@link NetworkStats.Bucket#ROAMING_ALL}.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000111 *
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 {
125 NetworkTemplate template = createTemplate(networkType, subscriberId);
126 if (template == null) {
127 return null;
128 }
129
130 Bucket bucket = null;
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100131 NetworkStats stats = new NetworkStats(mContext, template, startTime, endTime);
Zoltan Szatmary-Ban72027d22015-06-16 15:49:16 +0100132 bucket = stats.getDeviceSummaryForNetwork();
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000133
134 stats.close();
135 return bucket;
136 }
137
138 /**
Antonio Cansado46c753672015-12-10 15:57:56 -0800139 * Query network usage statistics summaries aggregated across tags.
140 *
141 * #see querySummaryForUser(int, String, long, long, boolean)
142 */
143 public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime,
144 long endTime) throws SecurityException, RemoteException {
145 return querySummaryForUser(networkType, subscriberId, startTime, endTime,
146 false /* includeTags */);
147 }
148
149 /**
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000150 * Query network usage statistics summaries. Result is summarised data usage for all uids
151 * belonging to calling user. Result is a single Bucket aggregated over time, state and uid.
Zoltan Szatmary-Ban72027d22015-06-16 15:49:16 +0100152 * This means the bucket's start and end timestamp are going to be the same as the 'startTime'
Antonio Cansado46c753672015-12-10 15:57:56 -0800153 * and 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL} and uid
154 * {@link NetworkStats.Bucket#UID_ALL}.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000155 *
156 * @param networkType As defined in {@link ConnectivityManager}, e.g.
157 * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
158 * etc.
159 * @param subscriberId If applicable, the subscriber id of the network interface.
160 * @param startTime Start of period. Defined in terms of "Unix time", see
161 * {@link java.lang.System#currentTimeMillis}.
162 * @param endTime End of period. Defined in terms of "Unix time", see
163 * {@link java.lang.System#currentTimeMillis}.
Antonio Cansado46c753672015-12-10 15:57:56 -0800164 * @param includeTags whether to include network tags. If {@code true}, tags will be returned
165 * and history retention may be shorter.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000166 * @return Bucket object or null if permissions are insufficient or error happened during
167 * statistics collection.
168 */
169 public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime,
Antonio Cansado46c753672015-12-10 15:57:56 -0800170 long endTime, boolean includeTags) throws SecurityException, RemoteException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000171 NetworkTemplate template = createTemplate(networkType, subscriberId);
172 if (template == null) {
173 return null;
174 }
175
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100176 NetworkStats stats;
177 stats = new NetworkStats(mContext, template, startTime, endTime);
Antonio Cansado46c753672015-12-10 15:57:56 -0800178 stats.startSummaryEnumeration(includeTags);
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000179
180 stats.close();
181 return stats.getSummaryAggregate();
182 }
183
184 /**
Antonio Cansado46c753672015-12-10 15:57:56 -0800185 * Query network usage statistics summaries aggregated across tags.
186 *
187 * #see querySummary(int, String, long, long, boolean)
188 */
189 public NetworkStats querySummary(int networkType, String subscriberId, long startTime,
190 long endTime) throws SecurityException, RemoteException {
191 return querySummary(networkType, subscriberId, startTime, endTime, false /* includeTags */);
192 }
193
194 /**
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000195 * Query network usage statistics summaries. Result filtered to include only uids belonging to
196 * calling user. Result is aggregated over time, hence all buckets will have the same start and
Antonio Cansado46c753672015-12-10 15:57:56 -0800197 * end timestamps. Not aggregated over state or uid or tag. This means buckets' start and end
198 * timestamps are going to be the same as the 'startTime' and 'endTime' parameters. State,
199 * uid and tag are going to vary.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000200 *
201 * @param networkType As defined in {@link ConnectivityManager}, e.g.
202 * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
203 * etc.
204 * @param subscriberId If applicable, the subscriber id of the network interface.
205 * @param startTime Start of period. Defined in terms of "Unix time", see
206 * {@link java.lang.System#currentTimeMillis}.
207 * @param endTime End of period. Defined in terms of "Unix time", see
208 * {@link java.lang.System#currentTimeMillis}.
Antonio Cansado46c753672015-12-10 15:57:56 -0800209 * @param includeTags whether to include network tags. If {@code true}, tags will be returned
210 * and history retention may be shorter.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000211 * @return Statistics object or null if permissions are insufficient or error happened during
212 * statistics collection.
213 */
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100214 public NetworkStats querySummary(int networkType, String subscriberId, long startTime,
Antonio Cansado46c753672015-12-10 15:57:56 -0800215 long endTime, boolean includeTags) throws SecurityException, RemoteException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000216 NetworkTemplate template = createTemplate(networkType, subscriberId);
217 if (template == null) {
218 return null;
219 }
220
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100221 NetworkStats result;
222 result = new NetworkStats(mContext, template, startTime, endTime);
Antonio Cansado46c753672015-12-10 15:57:56 -0800223 result.startSummaryEnumeration(includeTags);
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000224
225 return result;
226 }
227
228 /**
Antonio Cansado46c753672015-12-10 15:57:56 -0800229 * Query network usage statistics details for a given uid.
230 *
231 * #see queryDetailsForUidTag(int, String, long, long, int, int)
232 */
233 public NetworkStats queryDetailsForUid(int networkType, String subscriberId,
234 long startTime, long endTime, int uid) throws SecurityException, RemoteException {
235 return queryDetailsForUidTag(networkType, subscriberId, startTime, endTime, uid,
236 NetworkStats.Bucket.TAG_ALL);
237 }
238
239 /**
240 * Query network usage statistics details for a given uid and tag. Only usable for uids
241 * belonging to calling user. Result is aggregated over state but not aggregated over time.
242 * This means buckets' start and end timestamps are going to be between 'startTime' and
243 * 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid the
244 * same as the 'uid' parameter and tag the same as 'tag' parameter.
Zoltan Szatmary-Ban72027d22015-06-16 15:49:16 +0100245 * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't
246 * interpolate across partial buckets. Since bucket length is in the order of hours, this
247 * method cannot be used to measure data usage on a fine grained time scale.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000248 *
249 * @param networkType As defined in {@link ConnectivityManager}, e.g.
250 * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
251 * etc.
252 * @param subscriberId If applicable, the subscriber id of the network interface.
253 * @param startTime Start of period. Defined in terms of "Unix time", see
254 * {@link java.lang.System#currentTimeMillis}.
255 * @param endTime End of period. Defined in terms of "Unix time", see
256 * {@link java.lang.System#currentTimeMillis}.
257 * @param uid UID of app
Antonio Cansado46c753672015-12-10 15:57:56 -0800258 * @param tag TAG of interest. Use {@link NetworkStats.Bucket#TAG_ANY} for any tags, use
259 * {@link NetworkStats.Bucket#TAG_ALL} to aggregate over tags.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000260 * @return Statistics object or null if permissions are insufficient or error happened during
261 * statistics collection.
262 */
Antonio Cansado46c753672015-12-10 15:57:56 -0800263 public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId,
264 long startTime, long endTime, int uid, int tag) throws SecurityException,
265 RemoteException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000266 NetworkTemplate template = createTemplate(networkType, subscriberId);
267 if (template == null) {
268 return null;
269 }
270
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100271 NetworkStats result;
272 result = new NetworkStats(mContext, template, startTime, endTime);
Antonio Cansado46c753672015-12-10 15:57:56 -0800273 result.startHistoryEnumeration(uid, tag);
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000274
275 return result;
276 }
277
278 /**
279 * Query network usage statistics details. Result filtered to include only uids belonging to
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100280 * calling user. Result is aggregated over state but not aggregated over time or uid. This means
281 * buckets' start and end timestamps are going to be between 'startTime' and 'endTime'
Antonio Cansado46c753672015-12-10 15:57:56 -0800282 * parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid will vary,
283 * tag {@link NetworkStats.Bucket#TAG_ALL} and roaming is going to be
284 * {@link NetworkStats.Bucket#ROAMING_ALL}.
Zoltan Szatmary-Ban72027d22015-06-16 15:49:16 +0100285 * <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't
286 * interpolate across partial buckets. Since bucket length is in the order of hours, this
287 * method cannot be used to measure data usage on a fine grained time scale.
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000288 *
289 * @param networkType As defined in {@link ConnectivityManager}, e.g.
290 * {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
291 * etc.
292 * @param subscriberId If applicable, the subscriber id of the network interface.
293 * @param startTime Start of period. Defined in terms of "Unix time", see
294 * {@link java.lang.System#currentTimeMillis}.
295 * @param endTime End of period. Defined in terms of "Unix time", see
296 * {@link java.lang.System#currentTimeMillis}.
297 * @return Statistics object or null if permissions are insufficient or error happened during
298 * statistics collection.
299 */
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100300 public NetworkStats queryDetails(int networkType, String subscriberId, long startTime,
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000301 long endTime) throws SecurityException, RemoteException {
302 NetworkTemplate template = createTemplate(networkType, subscriberId);
303 if (template == null) {
304 return null;
305 }
Zoltan Szatmary-Ban381483b2015-05-13 17:53:17 +0100306 NetworkStats result;
307 result = new NetworkStats(mContext, template, startTime, endTime);
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000308 result.startUserUidEnumeration();
309 return result;
310 }
311
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800312 /**
313 * Registers to receive notifications about data usage on specified networks and uids.
314 * The callbacks will continue to be called as long as the process is live or
315 * {@link #unregisterDataUsageCallback} is called.
316 *
317 * @param policy {@link DataUsagePolicy} describing this request.
318 * @param callback The {@link DataUsageCallback} that the system will call when data usage
319 * has exceeded the specified threshold.
320 */
321 public void registerDataUsageCallback(DataUsagePolicy policy, DataUsageCallback callback) {
322 registerDataUsageCallback(policy, callback, null /* handler */);
323 }
324
325 /**
326 * Registers to receive notifications about data usage on specified networks and uids.
327 * The callbacks will continue to be called as long as the process is live or
328 * {@link #unregisterDataUsageCallback} is called.
329 *
330 * @param policy {@link DataUsagePolicy} describing this request.
331 * @param callback The {@link DataUsageCallback} that the system will call when data usage
332 * has exceeded the specified threshold.
333 * @param handler to dispatch callback events through, otherwise if {@code null} it uses
334 * the calling thread.
335 */
336 public void registerDataUsageCallback(DataUsagePolicy policy, DataUsageCallback callback,
337 @Nullable Handler handler) {
338 checkNotNull(policy, "DataUsagePolicy cannot be null");
339 checkNotNull(callback, "DataUsageCallback cannot be null");
340
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800341 final Looper looper;
342 if (handler == null) {
343 looper = Looper.myLooper();
344 } else {
345 looper = handler.getLooper();
346 }
347
348 if (DBG) Log.d(TAG, "registerDataUsageCallback called with " + policy);
349
350 NetworkTemplate[] templates;
351 if (policy.subscriberIds == null || policy.subscriberIds.length == 0) {
352 templates = new NetworkTemplate[1];
353 templates[0] = createTemplate(policy.networkType, null /* subscriberId */);
354 } else {
355 templates = new NetworkTemplate[policy.subscriberIds.length];
356 for (int i = 0; i < policy.subscriberIds.length; i++) {
357 templates[i] = createTemplate(policy.networkType, policy.subscriberIds[i]);
358 }
359 }
360 DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET,
361 templates, policy.uids, policy.thresholdInBytes);
362 try {
363 CallbackHandler callbackHandler = new CallbackHandler(looper, callback);
364 callback.request = mService.registerDataUsageCallback(
365 mContext.getOpPackageName(), request, new Messenger(callbackHandler),
366 new Binder());
367 if (DBG) Log.d(TAG, "registerDataUsageCallback returned " + callback.request);
368
369 if (callback.request == null) {
370 Log.e(TAG, "Request from callback is null; should not happen");
371 }
372 } catch (RemoteException e) {
373 if (DBG) Log.d(TAG, "Remote exception when registering callback");
374 }
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800375 }
376
377 /**
378 * Unregisters callbacks on data usage.
379 *
380 * @param callback The {@link DataUsageCallback} used when registering.
381 */
382 public void unregisterDataUsageCallback(DataUsageCallback callback) {
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800383 if (callback == null || callback.request == null
384 || callback.request.requestId == DataUsageRequest.REQUEST_ID_UNSET) {
385 throw new IllegalArgumentException("Invalid DataUsageCallback");
386 }
387 try {
388 mService.unregisterDataUsageRequest(callback.request);
389 } catch (RemoteException e) {
390 if (DBG) Log.d(TAG, "Remote exception when unregistering callback");
391 }
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800392 }
393
394 /**
395 * Base class for data usage callbacks. Should be extended by applications wanting
396 * notifications.
397 */
398 public static class DataUsageCallback {
399 /**
400 * Called when data usage has reached the given policy threshold.
401 */
402 public void onLimitReached() {}
403
404 private DataUsageRequest request;
405 }
406
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000407 private static NetworkTemplate createTemplate(int networkType, String subscriberId) {
408 NetworkTemplate template = null;
409 switch (networkType) {
410 case ConnectivityManager.TYPE_MOBILE: {
411 template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
412 } break;
413 case ConnectivityManager.TYPE_WIFI: {
414 template = NetworkTemplate.buildTemplateWifiWildcard();
415 } break;
416 default: {
417 Log.w(TAG, "Cannot create template for network type " + networkType
418 + ", subscriberId '" + NetworkIdentity.scrubSubscriberId(subscriberId) +
419 "'.");
420 }
421 }
422 return template;
423 }
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800424
425 private static class CallbackHandler extends Handler {
426 private DataUsageCallback mCallback;
427 CallbackHandler(Looper looper, DataUsageCallback callback) {
428 super(looper);
429 mCallback = callback;
430 }
431
432 @Override
433 public void handleMessage(Message message) {
434 DataUsageRequest request =
435 (DataUsageRequest) getObject(message, DataUsageRequest.PARCELABLE_KEY);
436
437 switch (message.what) {
438 case CALLBACK_LIMIT_REACHED: {
439 if (mCallback != null) {
440 mCallback.onLimitReached();
441 } else {
442 Log.e(TAG, "limit reached with released callback for " + request);
443 }
444 break;
445 }
446 case CALLBACK_RELEASED: {
447 if (DBG) Log.d(TAG, "callback released for " + request);
448 mCallback = null;
449 break;
450 }
451 }
452 }
453
454 private static Object getObject(Message msg, String key) {
455 return msg.getData().getParcelable(key);
456 }
457 }
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000458}