summaryrefslogtreecommitdiff
path: root/core/java/android/os/storage/CrateInfo.java
blob: 418d39e0c495dd896656ffd244f9010ecf03c974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os.storage;

import android.annotation.CurrentTimeMillisLong;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.app.usage.StorageStatsManager;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.text.TextUtils;

import com.android.internal.util.Preconditions;

import java.util.UUID;

/**
 * The CrateInfo describe the crate information.
 * <p>
 *      It describe the following items.
 *      <ul>
 *          <li>The crate id that is the name of the child directory in
 *          {@link Context#getCrateDir(String)}</li>
 *          <li>Label to provide human readable text for the users.</li>
 *          <li>Expiration information. When the crate is expired and the run .</li>
 *
 *      </ul>for the directory
 * </p>
 * @hide
 */
@TestApi
public final class CrateInfo implements Parcelable {
    private static final String TAG = "CrateInfo";

    /**
     * The following member fields whose value are set by apps and retrieved by system_server.
     */
    private CharSequence mLabel;
    @CurrentTimeMillisLong
    private long mExpiration;

    /**
     * The following member fields whose value are retrieved by installd.
     * <p>{@link android.app.usage.StorageStatsManager#queryCratesForUser(UUID, UserHandle)} query
     * all of crates for the specified UserHandle. That means the return crate list whose elements
     * may have the same userId but different package name. Each crate needs the information to tell
     * the caller from where package comes.
     * </p>
     */
    private int mUid;

    /**
     * The following member fields whose value are retrieved by installd.
     * <p>Both {@link StorageStatsManager#queryCratesForUid(UUID, int)} and
     * {@link android.app.usage.StorageStatsManager#queryCratesForUser(UUID, UserHandle)} query
     * all of crates for the specified uid or userId. That means the return crate list whose
     * elements may have the same uid or userId but different package name. Each crate needs the
     * information to tell the caller from where package comes.
     * </p>
     */
    @Nullable
    private String mPackageName;

    /**
     * The following member fields whose value are retrieved by system_server.
     * <p>
     *     The child directories in {@link Context#getCrateDir(String)} are crates. Each directories
     *     is a crate. The folder name is the crate id.
     * </p><p>
     *     Can't apply check if the path is validated or not because it need pass through the
     *     parcel.
     * </p>
     */
    @Nullable
    private String mId;

    private CrateInfo() {
        mExpiration = 0;
    }

    /**
     * To create the crateInfo by passing validated label.
     * @param label a display name for the crate
     * @param expiration It's positive integer. if current time is larger than the expiration, the
     *                  files under this crate will be considered to be deleted. Default value is 0.
     * @throws IllegalArgumentException cause IllegalArgumentException when label is null
     *      or empty string
     */
    public CrateInfo(@NonNull CharSequence label, @CurrentTimeMillisLong long expiration) {
        Preconditions.checkStringNotEmpty(label,
                "Label should not be either null or empty string");
        Preconditions.checkArgumentNonnegative(expiration,
                "Expiration should be non negative number");

        mLabel = label;
        mExpiration = expiration;
    }

    /**
     * To create the crateInfo by passing validated label.
     * @param label a display name for the crate
     * @throws IllegalArgumentException cause IllegalArgumentException when label is null
     *      or empty string
     */
    public CrateInfo(@NonNull CharSequence label) {
        this(label, 0);
    }

    /**
     * To get the meaningful text of the crate for the users.
     * @return the meaningful text
     */
    @NonNull
    public CharSequence getLabel() {
        if (TextUtils.isEmpty(mLabel)) {
            return mId;
        }
        return mLabel;
    }


    /**
     * To return the expiration time.
     * <p>
     *     If the current time is larger than expiration time, the crate files are considered to be
     *     deleted.
     * </p>
     * @return the expiration time
     */
    @CurrentTimeMillisLong
    public long getExpirationMillis() {
        return mExpiration;
    }

    /**
     * To set the expiration time.
     * @param expiration the expiration time
     * @hide
     */
    public void setExpiration(@CurrentTimeMillisLong long expiration) {
        Preconditions.checkArgumentNonnegative(expiration);
        mExpiration = expiration;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    /**
     * To compare with crateinfo when selves' mId is validated.
     * <p>The validated crateinfo.mId must be validated the following items.
     * <ul>
     *     <li>mId is not null</li>
     *     <li>mId is not empty string</li>
     * </ul>
     * </p>
     * @param   obj   the reference object with which to compare.
     * @return true when selves's mId is validated and equal to crateinfo.mId.
     */
    @Override
    public boolean equals(@Nullable Object obj) {
        if (obj == null) {
            return false;
        }

        if (obj instanceof CrateInfo) {
            CrateInfo crateInfo = (CrateInfo) obj;
            if (!TextUtils.isEmpty(mId)
                    && TextUtils.equals(mId, crateInfo.mId)) {
                return true;
            }
        }

        return super.equals(obj);
    }



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@Nullable Parcel dest, int flags) {
        if (dest == null) {
            return;
        }

        dest.writeCharSequence(mLabel);
        dest.writeLong(mExpiration);

        dest.writeInt(mUid);
        dest.writeString(mPackageName);
        dest.writeString(mId);
    }

    /**
     * To read the data from parcel.
     * <p>
     *     It's called by StorageStatsService.
     * </p>
     * @hide
     */
    public void readFromParcel(@Nullable Parcel in) {
        if (in == null) {
            return;
        }

        mLabel = in.readCharSequence();
        mExpiration = in.readLong();

        mUid = in.readInt();
        mPackageName = in.readString();
        mId = in.readString();
    }

    @NonNull
    public static final Creator<CrateInfo> CREATOR = new Creator<CrateInfo>() {
        @NonNull
        @Override
        public CrateInfo createFromParcel(@NonNull Parcel in) {
            CrateInfo crateInfo = new CrateInfo();
            crateInfo.readFromParcel(in);
            return crateInfo;
        }

        @NonNull
        @Override
        public CrateInfo[] newArray(int size) {
            return new CrateInfo[size];
        }
    };

    /**
     * To copy the information from service into crateinfo.
     * <p>
     * This function is called in system_server. The copied information includes
     *     <ul>
     *         <li>uid</li>
     *         <li>package name</li>
     *         <li>crate id</li>
     *     </ul>
     * </p>
     * @param uid the uid that the crate belong to
     * @param packageName the package name that the crate belong to
     * @param id the crate dir
     * @return the CrateInfo instance
     * @hide
     */
    @TestApi
    @Nullable
    public static CrateInfo copyFrom(int uid, @Nullable String packageName, @Nullable String id) {
        if (!UserHandle.isApp(uid) || TextUtils.isEmpty(packageName) || TextUtils.isEmpty(id)) {
            return null;
        }

        CrateInfo crateInfo = new CrateInfo(id /* default label = id */, 0);
        crateInfo.mUid = uid;
        crateInfo.mPackageName = packageName;
        crateInfo.mId = id;
        return crateInfo;
    }
}