Debian Codegraph @

core/java/android/os/storage/CrateInfo.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2019 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.os.storage;
18
19 import android.annotation.CurrentTimeMillisLong;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.TestApi;
23 import android.app.usage.StorageStatsManager;
24 import android.content.Context;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 import android.os.UserHandle;
28 import android.text.TextUtils;
29
30 import com.android.internal.util.Preconditions;
31
32 import java.util.UUID;
33
34 /**
35 * The CrateInfo describe the crate information.
36 * <p>
37 * It describe the following items.
38 * <ul>
39 * <li>The crate id that is the name of the child directory in
40 * {@link Context#getCrateDir(String)}</li>
41 * <li>Label to provide human readable text for the users.</li>
42 * <li>Expiration information. When the crate is expired and the run .</li>
43 *
44 * </ul>for the directory
45 * </p>
46 * @hide
47 */
48 @TestApi
49 public final class CrateInfo implements Parcelable {
50 private static final String TAG = "CrateInfo";
51
52 /**
53 * The following member fields whose value are set by apps and retrieved by system_server.
54 */
55 private CharSequence mLabel;
56 @CurrentTimeMillisLong
57 private long mExpiration;
58
59 /**
60 * The following member fields whose value are retrieved by installd.
61 * <p>{@link android.app.usage.StorageStatsManager#queryCratesForUser(UUID, UserHandle)} query
62 * all of crates for the specified UserHandle. That means the return crate list whose elements
63 * may have the same userId but different package name. Each crate needs the information to tell
64 * the caller from where package comes.
65 * </p>
66 */
67 private int mUid;
68
69 /**
70 * The following member fields whose value are retrieved by installd.
71 * <p>Both {@link StorageStatsManager#queryCratesForUid(UUID, int)} and
72 * {@link android.app.usage.StorageStatsManager#queryCratesForUser(UUID, UserHandle)} query
73 * all of crates for the specified uid or userId. That means the return crate list whose
74 * elements may have the same uid or userId but different package name. Each crate needs the
75 * information to tell the caller from where package comes.
76 * </p>
77 */
78 @Nullable
79 private String mPackageName;
80
81 /**
82 * The following member fields whose value are retrieved by system_server.
83 * <p>
84 * The child directories in {@link Context#getCrateDir(String)} are crates. Each directories
85 * is a crate. The folder name is the crate id.
86 * </p><p>
87 * Can't apply check if the path is validated or not because it need pass through the
88 * parcel.
89 * </p>
90 */
91 @Nullable
92 private String mId;
93
94 private CrateInfo() {
95 mExpiration = 0;
96 }
97
98 /**
99 * To create the crateInfo by passing validated label.
100 * @param label a display name for the crate
101 * @param expiration It's positive integer. if current time is larger than the expiration, the
102 * files under this crate will be considered to be deleted. Default value is 0.
103 * @throws IllegalArgumentException cause IllegalArgumentException when label is null
104 * or empty string
105 */
106 public CrateInfo(@NonNull CharSequence label, @CurrentTimeMillisLong long expiration) {
107 Preconditions.checkStringNotEmpty(label,
108 "Label should not be either null or empty string");
109 Preconditions.checkArgumentNonnegative(expiration,
110 "Expiration should be non negative number");
111
112 mLabel = label;
113 mExpiration = expiration;
114 }
115
116 /**
117 * To create the crateInfo by passing validated label.
118 * @param label a display name for the crate
119 * @throws IllegalArgumentException cause IllegalArgumentException when label is null
120 * or empty string
121 */
122 public CrateInfo(@NonNull CharSequence label) {
123 this(label, 0);
124 }
125
126 /**
127 * To get the meaningful text of the crate for the users.
128 * @return the meaningful text
129 */
130 @NonNull
131 public CharSequence getLabel() {
132 if (TextUtils.isEmpty(mLabel)) {
133 return mId;
134 }
135 return mLabel;
136 }
137
138
139 /**
140 * To return the expiration time.
141 * <p>
142 * If the current time is larger than expiration time, the crate files are considered to be
143 * deleted.
144 * </p>
145 * @return the expiration time
146 */
147 @CurrentTimeMillisLong
148 public long getExpirationMillis() {
149 return mExpiration;
150 }
151
152 /**
153 * To set the expiration time.
154 * @param expiration the expiration time
155 * @hide
156 */
157 public void setExpiration(@CurrentTimeMillisLong long expiration) {
158 Preconditions.checkArgumentNonnegative(expiration);
159 mExpiration = expiration;
160 }
161
162 @Override
163 public int hashCode() {
164 return super.hashCode();
165 }
166
167 /**
168 * To compare with crateinfo when selves' mId is validated.
169 * <p>The validated crateinfo.mId must be validated the following items.
170 * <ul>
171 * <li>mId is not null</li>
172 * <li>mId is not empty string</li>
173 * </ul>
174 * </p>
175 * @param obj the reference object with which to compare.
176 * @return true when selves's mId is validated and equal to crateinfo.mId.
177 */
178 @Override
179 public boolean equals(@Nullable Object obj) {
180 if (obj == null) {
181 return false;
182 }
183
184 if (obj instanceof CrateInfo) {
185 CrateInfo crateInfo = (CrateInfo) obj;
186 if (!TextUtils.isEmpty(mId)
187 && TextUtils.equals(mId, crateInfo.mId)) {
188 return true;
189 }
190 }
191
192 return super.equals(obj);
193 }
194
195
196
197 @Override
198 public int describeContents() {
199 return 0;
200 }
201
202 @Override
203 public void writeToParcel(@Nullable Parcel dest, int flags) {
204 if (dest == null) {
205 return;
206 }
207
208 dest.writeCharSequence(mLabel);
209 dest.writeLong(mExpiration);
210
211 dest.writeInt(mUid);
212 dest.writeString(mPackageName);
213 dest.writeString(mId);
214 }
215
216 /**
217 * To read the data from parcel.
218 * <p>
219 * It's called by StorageStatsService.
220 * </p>
221 * @hide
222 */
223 public void readFromParcel(@Nullable Parcel in) {
224 if (in == null) {
225 return;
226 }
227
228 mLabel = in.readCharSequence();
229 mExpiration = in.readLong();
230
231 mUid = in.readInt();
232 mPackageName = in.readString();
233 mId = in.readString();
234 }
235
236 @NonNull
237 public static final Creator<CrateInfo> CREATOR = new Creator<CrateInfo>() {
238 @NonNull
239 @Override
240 public CrateInfo createFromParcel(@NonNull Parcel in) {
241 CrateInfo crateInfo = new CrateInfo();
242 crateInfo.readFromParcel(in);
243 return crateInfo;
244 }
245
246 @NonNull
247 @Override
248 public CrateInfo[] newArray(int size) {
249 return new CrateInfo[size];
250 }
251 };
252
253 /**
254 * To copy the information from service into crateinfo.
255 * <p>
256 * This function is called in system_server. The copied information includes
257 * <ul>
258 * <li>uid</li>
259 * <li>package name</li>
260 * <li>crate id</li>
261 * </ul>
262 * </p>
263 * @param uid the uid that the crate belong to
264 * @param packageName the package name that the crate belong to
265 * @param id the crate dir
266 * @return the CrateInfo instance
267 * @hide
268 */
269 @TestApi
270 @Nullable
271 public static CrateInfo copyFrom(int uid, @Nullable String packageName, @Nullable String id) {
272 if (!UserHandle.isApp(uid) || TextUtils.isEmpty(packageName) || TextUtils.isEmpty(id)) {
273 return null;
274 }
275
276 CrateInfo crateInfo = new CrateInfo(id /* default label = id */, 0);
277 crateInfo.mUid = uid;
278 crateInfo.mPackageName = packageName;
279 crateInfo.mId = id;
280 return crateInfo;
281 }
282 }