blob: 3e1e4abaa84c970dbed7fbf20e5820aef882ca1b [file] [log] [blame]
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001/*
2 * Copyright (C) 2018 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 */
16package android.view.contentcapture;
17
Felipe Leme7e735752019-03-01 16:27:24 -080018import android.annotation.IntDef;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080019import android.annotation.NonNull;
Adam He3d0409b2019-01-15 14:22:04 -080020import android.app.ActivityThread;
Felipe Leme044c63b2019-02-12 14:35:20 -080021import android.content.LocusId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080022import android.os.Parcel;
23import android.os.Parcelable;
Adam He3d0409b2019-01-15 14:22:04 -080024import android.util.IntArray;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080025
Adam He3d0409b2019-01-15 14:22:04 -080026import com.android.internal.util.Preconditions;
27
Felipe Leme7e735752019-03-01 16:27:24 -080028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
Adam He3d0409b2019-01-15 14:22:04 -080030import java.util.ArrayList;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080031import java.util.List;
32
33/**
34 * Class used by apps to request the Content Capture service to remove user-data associated with
35 * some context.
36 */
37public final class UserDataRemovalRequest implements Parcelable {
38
Felipe Leme7e735752019-03-01 16:27:24 -080039 /**
40 * When set, service should use the {@link LocusId#getId()} as prefix for the data to be
41 * removed.
42 */
43 public static final int FLAG_IS_PREFIX = 0x1;
44
45 /** @hide */
46 @IntDef(prefix = { "FLAG" }, flag = true, value = {
47 FLAG_IS_PREFIX
48 })
49 @Retention(RetentionPolicy.SOURCE)
50 @interface Flags {}
51
Adam He3d0409b2019-01-15 14:22:04 -080052 private final String mPackageName;
53
54 private final boolean mForEverything;
Felipe Leme044c63b2019-02-12 14:35:20 -080055 private ArrayList<LocusIdRequest> mLocusIdRequests;
Adam He3d0409b2019-01-15 14:22:04 -080056
57 private UserDataRemovalRequest(@NonNull Builder builder) {
58 mPackageName = ActivityThread.currentActivityThread().getApplication().getPackageName();
59 mForEverything = builder.mForEverything;
Felipe Leme044c63b2019-02-12 14:35:20 -080060 if (builder.mLocusIds != null) {
61 final int size = builder.mLocusIds.size();
62 mLocusIdRequests = new ArrayList<>(size);
Adam He3d0409b2019-01-15 14:22:04 -080063 for (int i = 0; i < size; i++) {
Felipe Leme044c63b2019-02-12 14:35:20 -080064 mLocusIdRequests.add(new LocusIdRequest(builder.mLocusIds.get(i),
Felipe Leme7e735752019-03-01 16:27:24 -080065 builder.mFlags.get(i)));
Adam He3d0409b2019-01-15 14:22:04 -080066 }
67 }
68 }
69
70 private UserDataRemovalRequest(@NonNull Parcel parcel) {
71 mPackageName = parcel.readString();
72 mForEverything = parcel.readBoolean();
73 if (!mForEverything) {
74 final int size = parcel.readInt();
Felipe Leme044c63b2019-02-12 14:35:20 -080075 mLocusIdRequests = new ArrayList<>(size);
Adam He3d0409b2019-01-15 14:22:04 -080076 for (int i = 0; i < size; i++) {
Felipe Leme044c63b2019-02-12 14:35:20 -080077 mLocusIdRequests.add(new LocusIdRequest((LocusId) parcel.readValue(null),
Felipe Leme7e735752019-03-01 16:27:24 -080078 parcel.readInt()));
Adam He3d0409b2019-01-15 14:22:04 -080079 }
80 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -080081 }
82
83 /**
84 * Gets the name of the app that's making the request.
Felipe Lemeaa5088e2018-12-10 14:53:58 -080085 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -080086 @NonNull
87 public String getPackageName() {
Adam He3d0409b2019-01-15 14:22:04 -080088 return mPackageName;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080089 }
90
91 /**
92 * Checks if app is requesting to remove all user data associated with its package.
Felipe Lemeaa5088e2018-12-10 14:53:58 -080093 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -080094 public boolean isForEverything() {
Adam He3d0409b2019-01-15 14:22:04 -080095 return mForEverything;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080096 }
97
98 /**
Felipe Leme044c63b2019-02-12 14:35:20 -080099 * Gets the list of {@code LousId}s the apps is requesting to remove.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800100 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800101 @NonNull
Felipe Leme044c63b2019-02-12 14:35:20 -0800102 public List<LocusIdRequest> getLocusIdRequests() {
103 return mLocusIdRequests;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800104 }
105
106 /**
107 * Builder for {@link UserDataRemovalRequest} objects.
108 */
109 public static final class Builder {
110
Adam He3d0409b2019-01-15 14:22:04 -0800111 private boolean mForEverything;
Felipe Leme044c63b2019-02-12 14:35:20 -0800112 private ArrayList<LocusId> mLocusIds;
Felipe Leme7e735752019-03-01 16:27:24 -0800113 private IntArray mFlags;
Adam He3d0409b2019-01-15 14:22:04 -0800114
115 private boolean mDestroyed;
116
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800117 /**
118 * Requests servive to remove all user data associated with the app's package.
119 *
120 * @return this builder
121 */
122 @NonNull
123 public Builder forEverything() {
Adam He3d0409b2019-01-15 14:22:04 -0800124 throwIfDestroyed();
Felipe Leme044c63b2019-02-12 14:35:20 -0800125 Preconditions.checkState(mLocusIds == null, "Already added LocusIds");
Adam He3d0409b2019-01-15 14:22:04 -0800126
127 mForEverything = true;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800128 return this;
129 }
130
131 /**
Felipe Leme044c63b2019-02-12 14:35:20 -0800132 * Request service to remove data associated with a given {@link LocusId}.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800133 *
Felipe Leme044c63b2019-02-12 14:35:20 -0800134 * @param locusId the {@link LocusId} being requested to be removed.
Felipe Leme7e735752019-03-01 16:27:24 -0800135 * @param flags either {@link UserDataRemovalRequest#FLAG_IS_PREFIX} or {@code 0}
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800136 *
137 * @return this builder
138 */
Felipe Lemece6877b2019-02-28 09:02:26 -0800139 @NonNull
Felipe Leme7e735752019-03-01 16:27:24 -0800140 public Builder addLocusId(@NonNull LocusId locusId, @Flags int flags) {
Adam He3d0409b2019-01-15 14:22:04 -0800141 throwIfDestroyed();
Felipe Leme044c63b2019-02-12 14:35:20 -0800142 Preconditions.checkState(!mForEverything, "Already is for everything");
143 Preconditions.checkNotNull(locusId);
Felipe Leme7e735752019-03-01 16:27:24 -0800144 // felipeal: check flags
Adam He3d0409b2019-01-15 14:22:04 -0800145
Felipe Leme044c63b2019-02-12 14:35:20 -0800146 if (mLocusIds == null) {
147 mLocusIds = new ArrayList<>();
Felipe Leme7e735752019-03-01 16:27:24 -0800148 mFlags = new IntArray();
Adam He3d0409b2019-01-15 14:22:04 -0800149 }
150
Felipe Leme044c63b2019-02-12 14:35:20 -0800151 mLocusIds.add(locusId);
Felipe Leme7e735752019-03-01 16:27:24 -0800152 mFlags.add(flags);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800153 return this;
154 }
155
156 /**
157 * Builds the {@link UserDataRemovalRequest}.
158 */
159 @NonNull
160 public UserDataRemovalRequest build() {
Adam He3d0409b2019-01-15 14:22:04 -0800161 throwIfDestroyed();
162
Felipe Leme7e735752019-03-01 16:27:24 -0800163 Preconditions.checkState(mForEverything || mLocusIds != null,
164 "must call either #forEverything() or add one #addLocusId()");
Adam He3d0409b2019-01-15 14:22:04 -0800165
166 mDestroyed = true;
167 return new UserDataRemovalRequest(this);
168 }
169
170 private void throwIfDestroyed() {
171 Preconditions.checkState(!mDestroyed, "Already destroyed!");
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800172 }
173 }
174
175 @Override
176 public int describeContents() {
177 return 0;
178 }
179
180 @Override
181 public void writeToParcel(Parcel parcel, int flags) {
Adam He3d0409b2019-01-15 14:22:04 -0800182 parcel.writeString(mPackageName);
183 parcel.writeBoolean(mForEverything);
184 if (!mForEverything) {
Felipe Leme044c63b2019-02-12 14:35:20 -0800185 final int size = mLocusIdRequests.size();
Adam He3d0409b2019-01-15 14:22:04 -0800186 parcel.writeInt(size);
187 for (int i = 0; i < size; i++) {
Felipe Leme044c63b2019-02-12 14:35:20 -0800188 final LocusIdRequest request = mLocusIdRequests.get(i);
189 parcel.writeValue(request.getLocusId());
Felipe Leme7e735752019-03-01 16:27:24 -0800190 parcel.writeInt(request.getFlags());
Adam He3d0409b2019-01-15 14:22:04 -0800191 }
192 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800193 }
194
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700195 public static final @android.annotation.NonNull Parcelable.Creator<UserDataRemovalRequest> CREATOR =
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800196 new Parcelable.Creator<UserDataRemovalRequest>() {
197
198 @Override
Felipe Lemece6877b2019-02-28 09:02:26 -0800199 @NonNull
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800200 public UserDataRemovalRequest createFromParcel(Parcel parcel) {
Adam He3d0409b2019-01-15 14:22:04 -0800201 return new UserDataRemovalRequest(parcel);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800202 }
203
204 @Override
Felipe Lemece6877b2019-02-28 09:02:26 -0800205 @NonNull
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800206 public UserDataRemovalRequest[] newArray(int size) {
207 return new UserDataRemovalRequest[size];
208 }
209 };
210
211 /**
Felipe Leme044c63b2019-02-12 14:35:20 -0800212 * Representation of a request to remove data associated with a {@link LocusId}.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800213 */
Felipe Leme044c63b2019-02-12 14:35:20 -0800214 public final class LocusIdRequest {
215 private final @NonNull LocusId mLocusId;
Felipe Leme7e735752019-03-01 16:27:24 -0800216 private final @Flags int mFlags;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800217
Felipe Leme7e735752019-03-01 16:27:24 -0800218 private LocusIdRequest(@NonNull LocusId locusId, @Flags int flags) {
Felipe Leme044c63b2019-02-12 14:35:20 -0800219 this.mLocusId = locusId;
Felipe Leme7e735752019-03-01 16:27:24 -0800220 this.mFlags = flags;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800221 }
222
223 /**
Felipe Leme044c63b2019-02-12 14:35:20 -0800224 * Gets the {@code LocusId} per se.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800225 */
226 @NonNull
Felipe Leme044c63b2019-02-12 14:35:20 -0800227 public LocusId getLocusId() {
228 return mLocusId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800229 }
230
231 /**
Felipe Leme7e735752019-03-01 16:27:24 -0800232 * Gets the flags associates with request.
233 *
234 * @return either {@link UserDataRemovalRequest#FLAG_IS_PREFIX} or {@code 0}.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800235 */
236 @NonNull
Felipe Leme7e735752019-03-01 16:27:24 -0800237 public @Flags int getFlags() {
238 return mFlags;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800239 }
240 }
241}