blob: f0ff46522d15045affd747c29ec1aeeba6587edf [file] [log] [blame]
Antonio Cansadofb502102015-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
Roman Kalukiewicz3088c8f2020-10-14 15:59:06 -070019import android.annotation.Nullable;
Antonio Cansadofb502102015-12-02 08:42:54 -080020import android.net.NetworkTemplate;
21import android.os.Parcel;
22import android.os.Parcelable;
23
Antonio Cansadofb502102015-12-02 08:42:54 -080024import java.util.Objects;
25
26/**
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.
Antonio Cansadofe78ecb2016-03-30 11:37:18 -070031 *
32 * @hide
Antonio Cansadofb502102015-12-02 08:42:54 -080033 */
Jeff Sharkey89a344f2016-02-29 16:34:46 -070034public final class DataUsageRequest implements Parcelable {
Antonio Cansadofb502102015-12-02 08:42:54 -080035
Antonio Cansado7a653632016-02-17 13:03:38 -080036 public static final String PARCELABLE_KEY = "DataUsageRequest";
Antonio Cansadofb502102015-12-02 08:42:54 -080037 public static final int REQUEST_ID_UNSET = 0;
38
39 /**
40 * Identifies the request. {@link DataUsageRequest}s should only be constructed by
41 * the Framework and it is used internally to identify the request.
Antonio Cansadofb502102015-12-02 08:42:54 -080042 */
43 public final int requestId;
44
45 /**
Antonio Cansadofe78ecb2016-03-30 11:37:18 -070046 * {@link NetworkTemplate} describing the network to monitor.
Antonio Cansadofb502102015-12-02 08:42:54 -080047 */
Antonio Cansadofe78ecb2016-03-30 11:37:18 -070048 public final NetworkTemplate template;
Antonio Cansadofb502102015-12-02 08:42:54 -080049
50 /**
51 * Threshold in bytes to be notified on.
Antonio Cansadofb502102015-12-02 08:42:54 -080052 */
53 public final long thresholdInBytes;
54
Antonio Cansadofe78ecb2016-03-30 11:37:18 -070055 public DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes) {
Antonio Cansadofb502102015-12-02 08:42:54 -080056 this.requestId = requestId;
Antonio Cansadofe78ecb2016-03-30 11:37:18 -070057 this.template = template;
Antonio Cansadofb502102015-12-02 08:42:54 -080058 this.thresholdInBytes = thresholdInBytes;
59 }
60
61 @Override
62 public int describeContents() {
63 return 0;
64 }
65
66 @Override
67 public void writeToParcel(Parcel dest, int flags) {
68 dest.writeInt(requestId);
Antonio Cansadofe78ecb2016-03-30 11:37:18 -070069 dest.writeParcelable(template, flags);
Antonio Cansadofb502102015-12-02 08:42:54 -080070 dest.writeLong(thresholdInBytes);
71 }
72
Jeff Sharkey43cdbac2019-02-28 12:06:45 -070073 public static final @android.annotation.NonNull Creator<DataUsageRequest> CREATOR =
Antonio Cansadofb502102015-12-02 08:42:54 -080074 new Creator<DataUsageRequest>() {
75 @Override
76 public DataUsageRequest createFromParcel(Parcel in) {
77 int requestId = in.readInt();
Bernardo Rufino2ace1022022-01-14 17:35:36 +000078 NetworkTemplate template = in.readParcelable(null, android.net.NetworkTemplate.class);
Antonio Cansadofb502102015-12-02 08:42:54 -080079 long thresholdInBytes = in.readLong();
Antonio Cansadofe78ecb2016-03-30 11:37:18 -070080 DataUsageRequest result = new DataUsageRequest(requestId, template,
81 thresholdInBytes);
Antonio Cansadofb502102015-12-02 08:42:54 -080082 return result;
83 }
84
85 @Override
86 public DataUsageRequest[] newArray(int size) {
87 return new DataUsageRequest[size];
88 }
89 };
90
91 @Override
92 public String toString() {
93 return "DataUsageRequest [ requestId=" + requestId
Antonio Cansadofe78ecb2016-03-30 11:37:18 -070094 + ", networkTemplate=" + template
Antonio Cansadofb502102015-12-02 08:42:54 -080095 + ", thresholdInBytes=" + thresholdInBytes + " ]";
96 }
97
98 @Override
Roman Kalukiewicz3088c8f2020-10-14 15:59:06 -070099 public boolean equals(@Nullable Object obj) {
Antonio Cansadofb502102015-12-02 08:42:54 -0800100 if (obj instanceof DataUsageRequest == false) return false;
101 DataUsageRequest that = (DataUsageRequest) obj;
102 return that.requestId == this.requestId
Antonio Cansadofe78ecb2016-03-30 11:37:18 -0700103 && Objects.equals(that.template, this.template)
Antonio Cansadofb502102015-12-02 08:42:54 -0800104 && that.thresholdInBytes == this.thresholdInBytes;
105 }
106
107 @Override
108 public int hashCode() {
Antonio Cansadofe78ecb2016-03-30 11:37:18 -0700109 return Objects.hash(requestId, template, thresholdInBytes);
Antonio Cansadofb502102015-12-02 08:42:54 -0800110 }
111
112}