1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.usage;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import com.android.internal.util.Preconditions;
26 
27 import java.util.Objects;
28 
29 /**
30  * CacheQuotaHint represents a triplet of a uid, the volume UUID it is stored upon, and
31  * its usage stats. When processed, it obtains a cache quota as defined by the system which
32  * allows apps to understand how much cache to use.
33  * {@hide}
34  */
35 @SystemApi
36 public final class CacheQuotaHint implements Parcelable {
37     public static final long QUOTA_NOT_SET = -1;
38     private final String mUuid;
39     private final int mUid;
40     private final UsageStats mUsageStats;
41     private final long mQuota;
42 
43     /**
44      * Create a new request.
45      * @param builder A builder for this object.
46      */
CacheQuotaHint(Builder builder)47     public CacheQuotaHint(Builder builder) {
48         this.mUuid = builder.mUuid;
49         this.mUid = builder.mUid;
50         this.mUsageStats = builder.mUsageStats;
51         this.mQuota = builder.mQuota;
52     }
53 
getVolumeUuid()54     public String getVolumeUuid() {
55         return mUuid;
56     }
57 
getUid()58     public int getUid() {
59         return mUid;
60     }
61 
getQuota()62     public long getQuota() {
63         return mQuota;
64     }
65 
getUsageStats()66     public UsageStats getUsageStats() {
67         return mUsageStats;
68     }
69 
70     @Override
writeToParcel(Parcel dest, int flags)71     public void writeToParcel(Parcel dest, int flags) {
72         dest.writeString(mUuid);
73         dest.writeInt(mUid);
74         dest.writeLong(mQuota);
75         dest.writeParcelable(mUsageStats, 0);
76     }
77 
78     @Override
describeContents()79     public int describeContents() {
80         return 0;
81     }
82 
83     @Override
equals(@ullable Object o)84     public boolean equals(@Nullable Object o) {
85         if (o instanceof CacheQuotaHint) {
86             final CacheQuotaHint other = (CacheQuotaHint) o;
87             return Objects.equals(mUuid, other.mUuid)
88                     && Objects.equals(mUsageStats, other.mUsageStats)
89                     && mUid == other.mUid && mQuota == other.mQuota;
90         }
91 
92         return false;
93     }
94 
95     @Override
hashCode()96     public int hashCode() {
97         return Objects.hash(this.mUuid, this.mUid, this.mUsageStats, this.mQuota);
98     }
99 
100     public static final class Builder {
101         private String mUuid;
102         private int mUid;
103         private UsageStats mUsageStats;
104         private long mQuota;
105 
Builder()106         public Builder() {
107         }
108 
Builder(CacheQuotaHint hint)109         public Builder(CacheQuotaHint hint) {
110             setVolumeUuid(hint.getVolumeUuid());
111             setUid(hint.getUid());
112             setUsageStats(hint.getUsageStats());
113             setQuota(hint.getQuota());
114         }
115 
setVolumeUuid(@ullable String uuid)116         public @NonNull Builder setVolumeUuid(@Nullable String uuid) {
117             mUuid = uuid;
118             return this;
119         }
120 
setUid(int uid)121         public @NonNull Builder setUid(int uid) {
122             Preconditions.checkArgumentNonnegative(uid, "Proposed uid was negative.");
123             mUid = uid;
124             return this;
125         }
126 
setUsageStats(@ullable UsageStats stats)127         public @NonNull Builder setUsageStats(@Nullable UsageStats stats) {
128             mUsageStats = stats;
129             return this;
130         }
131 
setQuota(long quota)132         public @NonNull Builder setQuota(long quota) {
133             Preconditions.checkArgument((quota >= QUOTA_NOT_SET));
134             mQuota = quota;
135             return this;
136         }
137 
build()138         public @NonNull CacheQuotaHint build() {
139             return new CacheQuotaHint(this);
140         }
141     }
142 
143     public static final @android.annotation.NonNull Parcelable.Creator<CacheQuotaHint> CREATOR =
144             new Creator<CacheQuotaHint>() {
145                 @Override
146                 public CacheQuotaHint createFromParcel(Parcel in) {
147                     final Builder builder = new Builder();
148                     return builder.setVolumeUuid(in.readString())
149                             .setUid(in.readInt())
150                             .setQuota(in.readLong())
151                             .setUsageStats(in.readParcelable(UsageStats.class.getClassLoader()))
152                             .build();
153                 }
154 
155                 @Override
156                 public CacheQuotaHint[] newArray(int size) {
157                     return new CacheQuotaHint[size];
158                 }
159             };
160 }
161