Antonio Cansado | ba8288d | 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 | |
| 19 | import android.net.NetworkTemplate; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 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. |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 30 | * |
| 31 | * @hide |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 32 | */ |
Jeff Sharkey | 50d1c04 | 2016-02-29 16:34:46 -0700 | [diff] [blame] | 33 | public final class DataUsageRequest implements Parcelable { |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 34 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 35 | public static final String PARCELABLE_KEY = "DataUsageRequest"; |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 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. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 41 | */ |
| 42 | public final int requestId; |
| 43 | |
| 44 | /** |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 45 | * {@link NetworkTemplate} describing the network to monitor. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 46 | */ |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 47 | public final NetworkTemplate template; |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Threshold in bytes to be notified on. |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 51 | */ |
| 52 | public final long thresholdInBytes; |
| 53 | |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 54 | public DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes) { |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 55 | this.requestId = requestId; |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 56 | this.template = template; |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 57 | 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 Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 68 | dest.writeParcelable(template, flags); |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 69 | dest.writeLong(thresholdInBytes); |
| 70 | } |
| 71 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 72 | public static final @android.annotation.NonNull Creator<DataUsageRequest> CREATOR = |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 73 | new Creator<DataUsageRequest>() { |
| 74 | @Override |
| 75 | public DataUsageRequest createFromParcel(Parcel in) { |
| 76 | int requestId = in.readInt(); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 77 | NetworkTemplate template = in.readParcelable(null); |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 78 | long thresholdInBytes = in.readLong(); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 79 | DataUsageRequest result = new DataUsageRequest(requestId, template, |
| 80 | thresholdInBytes); |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 81 | 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 Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 93 | + ", networkTemplate=" + template |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 94 | + ", 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 Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 102 | && Objects.equals(that.template, this.template) |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 103 | && that.thresholdInBytes == this.thresholdInBytes; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 108 | return Objects.hash(requestId, template, thresholdInBytes); |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | } |