Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 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 | |
Roman Kalukiewicz | 3088c8f | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 20 | import android.net.NetworkTemplate; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 24 | import 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 Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 31 | * |
| 32 | * @hide |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 33 | */ |
Jeff Sharkey | 89a344f | 2016-02-29 16:34:46 -0700 | [diff] [blame] | 34 | public final class DataUsageRequest implements Parcelable { |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 35 | |
Antonio Cansado | 7a65363 | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 36 | public static final String PARCELABLE_KEY = "DataUsageRequest"; |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 37 | 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 Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 42 | */ |
| 43 | public final int requestId; |
| 44 | |
| 45 | /** |
Antonio Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 46 | * {@link NetworkTemplate} describing the network to monitor. |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 47 | */ |
Antonio Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 48 | public final NetworkTemplate template; |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Threshold in bytes to be notified on. |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 52 | */ |
| 53 | public final long thresholdInBytes; |
| 54 | |
Antonio Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 55 | public DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes) { |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 56 | this.requestId = requestId; |
Antonio Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 57 | this.template = template; |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 58 | 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 Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 69 | dest.writeParcelable(template, flags); |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 70 | dest.writeLong(thresholdInBytes); |
| 71 | } |
| 72 | |
Jeff Sharkey | 43cdbac | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 73 | public static final @android.annotation.NonNull Creator<DataUsageRequest> CREATOR = |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 74 | new Creator<DataUsageRequest>() { |
| 75 | @Override |
| 76 | public DataUsageRequest createFromParcel(Parcel in) { |
| 77 | int requestId = in.readInt(); |
Bernardo Rufino | 2ace102 | 2022-01-14 17:35:36 +0000 | [diff] [blame] | 78 | NetworkTemplate template = in.readParcelable(null, android.net.NetworkTemplate.class); |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 79 | long thresholdInBytes = in.readLong(); |
Antonio Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 80 | DataUsageRequest result = new DataUsageRequest(requestId, template, |
| 81 | thresholdInBytes); |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 82 | 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 Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 94 | + ", networkTemplate=" + template |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 95 | + ", thresholdInBytes=" + thresholdInBytes + " ]"; |
| 96 | } |
| 97 | |
| 98 | @Override |
Roman Kalukiewicz | 3088c8f | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 99 | public boolean equals(@Nullable Object obj) { |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 100 | if (obj instanceof DataUsageRequest == false) return false; |
| 101 | DataUsageRequest that = (DataUsageRequest) obj; |
| 102 | return that.requestId == this.requestId |
Antonio Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 103 | && Objects.equals(that.template, this.template) |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 104 | && that.thresholdInBytes == this.thresholdInBytes; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int hashCode() { |
Antonio Cansado | fe78ecb | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 109 | return Objects.hash(requestId, template, thresholdInBytes); |
Antonio Cansado | fb50210 | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } |