2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
19 import android.net.NetworkTemplate;
20 import android.os.Parcel;
21 import android.os.Parcelable;
23 import java.util.Arrays;
24 import java.util.Objects;
27 * Defines a request to register a callbacks. Used to be notified on data usage via
28 * {@link android.app.usage.NetworkStatsManager#registerDataUsageCallback}.
29 * If no {@code uid}s are set, callbacks are restricted to device-owners,
30 * carrier-privileged apps, or system apps.
32 public final class DataUsageRequest implements Parcelable {
37 public static final String PARCELABLE_KEY = "DataUsageRequest";
42 public static final int REQUEST_ID_UNSET = 0;
45 * Identifies the request. {@link DataUsageRequest}s should only be constructed by
46 * the Framework and it is used internally to identify the request.
49 public final int requestId;
52 * Set of {@link NetworkTemplate}s describing the networks to monitor.
55 public final NetworkTemplate[] templates;
58 * Set of UIDs of which to monitor data usage.
60 * <p>If not {@code null}, the caller will be notified when any of the uids exceed
61 * the given threshold. If {@code null} all uids for which the calling process has access
62 * to stats will be monitored.
65 public final int[] uids;
68 * Threshold in bytes to be notified on.
71 public final long thresholdInBytes;
76 public DataUsageRequest(int requestId, NetworkTemplate[] templates, int[] uids,
77 long thresholdInBytes) {
78 this.requestId = requestId;
79 this.templates = templates;
81 this.thresholdInBytes = thresholdInBytes;
85 public int describeContents() {
90 public void writeToParcel(Parcel dest, int flags) {
91 dest.writeInt(requestId);
92 dest.writeTypedArray(templates, flags);
93 dest.writeIntArray(uids);
94 dest.writeLong(thresholdInBytes);
97 public static final Creator<DataUsageRequest> CREATOR =
98 new Creator<DataUsageRequest>() {
100 public DataUsageRequest createFromParcel(Parcel in) {
101 int requestId = in.readInt();
102 NetworkTemplate[] templates = in.createTypedArray(NetworkTemplate.CREATOR);
103 int[] uids = in.createIntArray();
104 long thresholdInBytes = in.readLong();
105 DataUsageRequest result = new DataUsageRequest(requestId,
106 templates, uids, thresholdInBytes);
111 public DataUsageRequest[] newArray(int size) {
112 return new DataUsageRequest[size];
117 public String toString() {
118 return "DataUsageRequest [ requestId=" + requestId
119 + ", networkTemplates=" + Arrays.toString(templates)
120 + ", uids=" + Arrays.toString(uids)
121 + ", thresholdInBytes=" + thresholdInBytes + " ]";
125 public boolean equals(Object obj) {
126 if (obj instanceof DataUsageRequest == false) return false;
127 DataUsageRequest that = (DataUsageRequest) obj;
128 return that.requestId == this.requestId
129 && Arrays.deepEquals(that.templates, this.templates)
130 && Arrays.equals(that.uids, this.uids)
131 && that.thresholdInBytes == this.thresholdInBytes;
135 public int hashCode() {
136 // Start with a non-zero constant.
139 // Include a hash for each field.
140 result = 31 * result + requestId;
141 result = 31 * result + Arrays.deepHashCode(templates);
142 result = 31 * result + Arrays.hashCode(uids);
143 result = 31 * result + (int) (thresholdInBytes ^ (thresholdInBytes >>> 32));