blob: 0ac8f7e794dcaa15340b50da19a9629e06f83f70 [file] [log] [blame]
Antonio Cansadoba8288d2015-12-02 08:42:54 -08001/**
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
17package android.net;
18
19import android.net.NetworkTemplate;
20import android.os.Parcel;
21import android.os.Parcelable;
22
Antonio Cansadoba8288d2015-12-02 08:42:54 -080023import 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.
Antonio Cansado6965c182016-03-30 11:37:18 -070030 *
31 * @hide
Antonio Cansadoba8288d2015-12-02 08:42:54 -080032 */
Jeff Sharkey50d1c042016-02-29 16:34:46 -070033public final class DataUsageRequest implements Parcelable {
Antonio Cansadoba8288d2015-12-02 08:42:54 -080034
Antonio Cansadocd42acd2016-02-17 13:03:38 -080035 public static final String PARCELABLE_KEY = "DataUsageRequest";
Antonio Cansadoba8288d2015-12-02 08:42:54 -080036 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.
Antonio Cansadoba8288d2015-12-02 08:42:54 -080041 */
42 public final int requestId;
43
44 /**
Antonio Cansado6965c182016-03-30 11:37:18 -070045 * {@link NetworkTemplate} describing the network to monitor.
Antonio Cansadoba8288d2015-12-02 08:42:54 -080046 */
Antonio Cansado6965c182016-03-30 11:37:18 -070047 public final NetworkTemplate template;
Antonio Cansadoba8288d2015-12-02 08:42:54 -080048
49 /**
50 * Threshold in bytes to be notified on.
Antonio Cansadoba8288d2015-12-02 08:42:54 -080051 */
52 public final long thresholdInBytes;
53
Antonio Cansado6965c182016-03-30 11:37:18 -070054 public DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes) {
Antonio Cansadoba8288d2015-12-02 08:42:54 -080055 this.requestId = requestId;
Antonio Cansado6965c182016-03-30 11:37:18 -070056 this.template = template;
Antonio Cansadoba8288d2015-12-02 08:42:54 -080057 this.thresholdInBytes = thresholdInBytes;
58 }
59
60 @Override
61 public int describeContents() {
62 return 0;
63 }
64
65 @Override
66 public void writeToParcel(Parcel dest, int flags) {
67 dest.writeInt(requestId);
Antonio Cansado6965c182016-03-30 11:37:18 -070068 dest.writeParcelable(template, flags);
Antonio Cansadoba8288d2015-12-02 08:42:54 -080069 dest.writeLong(thresholdInBytes);
70 }
71
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070072 public static final @android.annotation.NonNull Creator<DataUsageRequest> CREATOR =
Antonio Cansadoba8288d2015-12-02 08:42:54 -080073 new Creator<DataUsageRequest>() {
74 @Override
75 public DataUsageRequest createFromParcel(Parcel in) {
76 int requestId = in.readInt();
Antonio Cansado6965c182016-03-30 11:37:18 -070077 NetworkTemplate template = in.readParcelable(null);
Antonio Cansadoba8288d2015-12-02 08:42:54 -080078 long thresholdInBytes = in.readLong();
Antonio Cansado6965c182016-03-30 11:37:18 -070079 DataUsageRequest result = new DataUsageRequest(requestId, template,
80 thresholdInBytes);
Antonio Cansadoba8288d2015-12-02 08:42:54 -080081 return result;
82 }
83
84 @Override
85 public DataUsageRequest[] newArray(int size) {
86 return new DataUsageRequest[size];
87 }
88 };
89
90 @Override
91 public String toString() {
92 return "DataUsageRequest [ requestId=" + requestId
Antonio Cansado6965c182016-03-30 11:37:18 -070093 + ", networkTemplate=" + template
Antonio Cansadoba8288d2015-12-02 08:42:54 -080094 + ", thresholdInBytes=" + thresholdInBytes + " ]";
95 }
96
97 @Override
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
Antonio Cansado6965c182016-03-30 11:37:18 -0700102 && Objects.equals(that.template, this.template)
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800103 && that.thresholdInBytes == this.thresholdInBytes;
104 }
105
106 @Override
107 public int hashCode() {
Antonio Cansado6965c182016-03-30 11:37:18 -0700108 return Objects.hash(requestId, template, thresholdInBytes);
Antonio Cansadoba8288d2015-12-02 08:42:54 -0800109 }
110
111}