1 /**
2  * Copyright (C) 2016 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.net;
18 
19 import android.net.NetworkTemplate;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.Objects;
24 
25 /**
26  * Defines a request to register a callbacks. Used to be notified on data usage via
27  * {@link android.app.usage.NetworkStatsManager#registerDataUsageCallback}.
28  * If no {@code uid}s are set, callbacks are restricted to device-owners,
29  * carrier-privileged apps, or system apps.
30  *
31  * @hide
32  */
33 public final class DataUsageRequest implements Parcelable {
34 
35     public static final String PARCELABLE_KEY = "DataUsageRequest";
36     public static final int REQUEST_ID_UNSET = 0;
37 
38     /**
39      * Identifies the request.  {@link DataUsageRequest}s should only be constructed by
40      * the Framework and it is used internally to identify the request.
41      */
42     public final int requestId;
43 
44     /**
45      * {@link NetworkTemplate} describing the network to monitor.
46      */
47     public final NetworkTemplate template;
48 
49     /**
50      * Threshold in bytes to be notified on.
51      */
52     public final long thresholdInBytes;
53 
DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes)54     public DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes) {
55         this.requestId = requestId;
56         this.template = template;
57         this.thresholdInBytes = thresholdInBytes;
58     }
59 
60     @Override
describeContents()61     public int describeContents() {
62         return 0;
63     }
64 
65     @Override
writeToParcel(Parcel dest, int flags)66     public void writeToParcel(Parcel dest, int flags) {
67         dest.writeInt(requestId);
68         dest.writeParcelable(template, flags);
69         dest.writeLong(thresholdInBytes);
70     }
71 
72     public static final @android.annotation.NonNull Creator<DataUsageRequest> CREATOR =
73             new Creator<DataUsageRequest>() {
74                 @Override
75                 public DataUsageRequest createFromParcel(Parcel in) {
76                     int requestId = in.readInt();
77                     NetworkTemplate template = in.readParcelable(null);
78                     long thresholdInBytes = in.readLong();
79                     DataUsageRequest result = new DataUsageRequest(requestId, template,
80                             thresholdInBytes);
81                     return result;
82                 }
83 
84                 @Override
85                 public DataUsageRequest[] newArray(int size) {
86                     return new DataUsageRequest[size];
87                 }
88             };
89 
90     @Override
toString()91     public String toString() {
92         return "DataUsageRequest [ requestId=" + requestId
93                 + ", networkTemplate=" + template
94                 + ", thresholdInBytes=" + thresholdInBytes + " ]";
95     }
96 
97     @Override
equals(Object obj)98     public boolean equals(Object obj) {
99         if (obj instanceof DataUsageRequest == false) return false;
100         DataUsageRequest that = (DataUsageRequest) obj;
101         return that.requestId == this.requestId
102                 && Objects.equals(that.template, this.template)
103                 && that.thresholdInBytes == this.thresholdInBytes;
104     }
105 
106     @Override
hashCode()107     public int hashCode() {
108         return Objects.hash(requestId, template, thresholdInBytes);
109    }
110 
111 }
112