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.annotation.Nullable; 20 import android.net.NetworkTemplate; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 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 * @hide 33 */ 34 public final class DataUsageRequest implements Parcelable { 35 36 public static final String PARCELABLE_KEY = "DataUsageRequest"; 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. 42 */ 43 public final int requestId; 44 45 /** 46 * {@link NetworkTemplate} describing the network to monitor. 47 */ 48 public final NetworkTemplate template; 49 50 /** 51 * Threshold in bytes to be notified on. 52 */ 53 public final long thresholdInBytes; 54 DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes)55 public DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes) { 56 this.requestId = requestId; 57 this.template = template; 58 this.thresholdInBytes = thresholdInBytes; 59 } 60 61 @Override describeContents()62 public int describeContents() { 63 return 0; 64 } 65 66 @Override writeToParcel(Parcel dest, int flags)67 public void writeToParcel(Parcel dest, int flags) { 68 dest.writeInt(requestId); 69 dest.writeParcelable(template, flags); 70 dest.writeLong(thresholdInBytes); 71 } 72 73 public static final @android.annotation.NonNull Creator<DataUsageRequest> CREATOR = 74 new Creator<DataUsageRequest>() { 75 @Override 76 public DataUsageRequest createFromParcel(Parcel in) { 77 int requestId = in.readInt(); 78 NetworkTemplate template = in.readParcelable(null); 79 long thresholdInBytes = in.readLong(); 80 DataUsageRequest result = new DataUsageRequest(requestId, template, 81 thresholdInBytes); 82 return result; 83 } 84 85 @Override 86 public DataUsageRequest[] newArray(int size) { 87 return new DataUsageRequest[size]; 88 } 89 }; 90 91 @Override toString()92 public String toString() { 93 return "DataUsageRequest [ requestId=" + requestId 94 + ", networkTemplate=" + template 95 + ", thresholdInBytes=" + thresholdInBytes + " ]"; 96 } 97 98 @Override equals(@ullable Object obj)99 public boolean equals(@Nullable Object obj) { 100 if (obj instanceof DataUsageRequest == false) return false; 101 DataUsageRequest that = (DataUsageRequest) obj; 102 return that.requestId == this.requestId 103 && Objects.equals(that.template, this.template) 104 && that.thresholdInBytes == this.thresholdInBytes; 105 } 106 107 @Override hashCode()108 public int hashCode() { 109 return Objects.hash(requestId, template, thresholdInBytes); 110 } 111 112 } 113