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 | |
| 23 | import java.util.Arrays; |
| 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. |
| 31 | */ |
| 32 | public class DataUsageRequest implements Parcelable { |
| 33 | |
| 34 | /** |
| 35 | * @hide |
| 36 | */ |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 37 | public static final String PARCELABLE_KEY = "DataUsageRequest"; |
| 38 | |
| 39 | /** |
| 40 | * @hide |
| 41 | */ |
Antonio Cansado | ba8288d | 2015-12-02 08:42:54 -0800 | [diff] [blame] | 42 | public static final int REQUEST_ID_UNSET = 0; |
| 43 | |
| 44 | /** |
| 45 | * Identifies the request. {@link DataUsageRequest}s should only be constructed by |
| 46 | * the Framework and it is used internally to identify the request. |
| 47 | * @hide |
| 48 | */ |
| 49 | public final int requestId; |
| 50 | |
| 51 | /** |
| 52 | * Set of {@link NetworkTemplate}s describing the networks to monitor. |
| 53 | * @hide |
| 54 | */ |
| 55 | public final NetworkTemplate[] templates; |
| 56 | |
| 57 | /** |
| 58 | * Set of UIDs of which to monitor data usage. |
| 59 | * |
| 60 | * <p>If not {@code null}, the caller will be notified when any of the uids exceed |
| 61 | * the given threshold. If {@code null} all uids for which the calling process has access |
| 62 | * to stats will be monitored. |
| 63 | * @hide |
| 64 | */ |
| 65 | public final int[] uids; |
| 66 | |
| 67 | /** |
| 68 | * Threshold in bytes to be notified on. |
| 69 | * @hide |
| 70 | */ |
| 71 | public final long thresholdInBytes; |
| 72 | |
| 73 | /** |
| 74 | * @hide |
| 75 | */ |
| 76 | public DataUsageRequest(int requestId, NetworkTemplate[] templates, int[] uids, |
| 77 | long thresholdInBytes) { |
| 78 | this.requestId = requestId; |
| 79 | this.templates = templates; |
| 80 | this.uids = uids; |
| 81 | this.thresholdInBytes = thresholdInBytes; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int describeContents() { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void writeToParcel(Parcel dest, int flags) { |
| 91 | dest.writeInt(requestId); |
| 92 | dest.writeTypedArray(templates, flags); |
| 93 | dest.writeIntArray(uids); |
| 94 | dest.writeLong(thresholdInBytes); |
| 95 | } |
| 96 | |
| 97 | public static final Creator<DataUsageRequest> CREATOR = |
| 98 | new Creator<DataUsageRequest>() { |
| 99 | @Override |
| 100 | public DataUsageRequest createFromParcel(Parcel in) { |
| 101 | int requestId = in.readInt(); |
| 102 | NetworkTemplate[] templates = in.createTypedArray(NetworkTemplate.CREATOR); |
| 103 | int[] uids = in.createIntArray(); |
| 104 | long thresholdInBytes = in.readLong(); |
| 105 | DataUsageRequest result = new DataUsageRequest(requestId, |
| 106 | templates, uids, thresholdInBytes); |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public DataUsageRequest[] newArray(int size) { |
| 112 | return new DataUsageRequest[size]; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | @Override |
| 117 | public String toString() { |
| 118 | return "DataUsageRequest [ requestId=" + requestId |
| 119 | + ", networkTemplates=" + Arrays.toString(templates) |
| 120 | + ", uids=" + Arrays.toString(uids) |
| 121 | + ", thresholdInBytes=" + thresholdInBytes + " ]"; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public boolean equals(Object obj) { |
| 126 | if (obj instanceof DataUsageRequest == false) return false; |
| 127 | DataUsageRequest that = (DataUsageRequest) obj; |
| 128 | return that.requestId == this.requestId |
| 129 | && Arrays.deepEquals(that.templates, this.templates) |
| 130 | && Arrays.equals(that.uids, this.uids) |
| 131 | && that.thresholdInBytes == this.thresholdInBytes; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public int hashCode() { |
| 136 | // Start with a non-zero constant. |
| 137 | int result = 17; |
| 138 | |
| 139 | // Include a hash for each field. |
| 140 | result = 31 * result + requestId; |
| 141 | result = 31 * result + Arrays.deepHashCode(templates); |
| 142 | result = 31 * result + Arrays.hashCode(uids); |
| 143 | result = 31 * result + (int) (thresholdInBytes ^ (thresholdInBytes >>> 32)); |
| 144 | |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | } |