blob: 7d66af92e9f678a5bc6de8a8336b027e4dbf3937 [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
18import android.annotation.NonNull;
Adam He3d0409b2019-01-15 14:22:04 -080019import android.app.ActivityThread;
Felipe Leme044c63b2019-02-12 14:35:20 -080020import android.content.LocusId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080021import android.os.Parcel;
22import android.os.Parcelable;
Adam He3d0409b2019-01-15 14:22:04 -080023import android.util.IntArray;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080024
Adam He3d0409b2019-01-15 14:22:04 -080025import com.android.internal.util.Preconditions;
26
27import java.util.ArrayList;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080028import java.util.List;
29
30/**
31 * Class used by apps to request the Content Capture service to remove user-data associated with
32 * some context.
33 */
34public final class UserDataRemovalRequest implements Parcelable {
35
Adam He3d0409b2019-01-15 14:22:04 -080036 private final String mPackageName;
37
38 private final boolean mForEverything;
Felipe Leme044c63b2019-02-12 14:35:20 -080039 private ArrayList<LocusIdRequest> mLocusIdRequests;
Adam He3d0409b2019-01-15 14:22:04 -080040
41 private UserDataRemovalRequest(@NonNull Builder builder) {
42 mPackageName = ActivityThread.currentActivityThread().getApplication().getPackageName();
43 mForEverything = builder.mForEverything;
Felipe Leme044c63b2019-02-12 14:35:20 -080044 if (builder.mLocusIds != null) {
45 final int size = builder.mLocusIds.size();
46 mLocusIdRequests = new ArrayList<>(size);
Adam He3d0409b2019-01-15 14:22:04 -080047 for (int i = 0; i < size; i++) {
Felipe Leme044c63b2019-02-12 14:35:20 -080048 mLocusIdRequests.add(new LocusIdRequest(builder.mLocusIds.get(i),
Adam He3d0409b2019-01-15 14:22:04 -080049 builder.mRecursive.get(i) == 1));
50 }
51 }
52 }
53
54 private UserDataRemovalRequest(@NonNull Parcel parcel) {
55 mPackageName = parcel.readString();
56 mForEverything = parcel.readBoolean();
57 if (!mForEverything) {
58 final int size = parcel.readInt();
Felipe Leme044c63b2019-02-12 14:35:20 -080059 mLocusIdRequests = new ArrayList<>(size);
Adam He3d0409b2019-01-15 14:22:04 -080060 for (int i = 0; i < size; i++) {
Felipe Leme044c63b2019-02-12 14:35:20 -080061 mLocusIdRequests.add(new LocusIdRequest((LocusId) parcel.readValue(null),
Adam He3d0409b2019-01-15 14:22:04 -080062 parcel.readBoolean()));
63 }
64 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -080065 }
66
67 /**
68 * Gets the name of the app that's making the request.
Felipe Lemeaa5088e2018-12-10 14:53:58 -080069 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -080070 @NonNull
71 public String getPackageName() {
Adam He3d0409b2019-01-15 14:22:04 -080072 return mPackageName;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080073 }
74
75 /**
76 * Checks if app is requesting to remove all user data associated with its package.
Felipe Lemeaa5088e2018-12-10 14:53:58 -080077 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -080078 public boolean isForEverything() {
Adam He3d0409b2019-01-15 14:22:04 -080079 return mForEverything;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080080 }
81
82 /**
Felipe Leme044c63b2019-02-12 14:35:20 -080083 * Gets the list of {@code LousId}s the apps is requesting to remove.
Felipe Lemeaa5088e2018-12-10 14:53:58 -080084 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -080085 @NonNull
Felipe Leme044c63b2019-02-12 14:35:20 -080086 public List<LocusIdRequest> getLocusIdRequests() {
87 return mLocusIdRequests;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080088 }
89
90 /**
91 * Builder for {@link UserDataRemovalRequest} objects.
92 */
93 public static final class Builder {
94
Adam He3d0409b2019-01-15 14:22:04 -080095 private boolean mForEverything;
Felipe Leme044c63b2019-02-12 14:35:20 -080096 private ArrayList<LocusId> mLocusIds;
Adam He3d0409b2019-01-15 14:22:04 -080097 private IntArray mRecursive;
98
99 private boolean mDestroyed;
100
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800101 /**
102 * Requests servive to remove all user data associated with the app's package.
103 *
104 * @return this builder
105 */
106 @NonNull
107 public Builder forEverything() {
Adam He3d0409b2019-01-15 14:22:04 -0800108 throwIfDestroyed();
Felipe Leme044c63b2019-02-12 14:35:20 -0800109 Preconditions.checkState(mLocusIds == null, "Already added LocusIds");
Adam He3d0409b2019-01-15 14:22:04 -0800110
111 mForEverything = true;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800112 return this;
113 }
114
115 /**
Felipe Leme044c63b2019-02-12 14:35:20 -0800116 * Request service to remove data associated with a given {@link LocusId}.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800117 *
Felipe Leme044c63b2019-02-12 14:35:20 -0800118 * @param locusId the {@link LocusId} being requested to be removed.
119 * @param recursive whether it should remove the data associated with just the
120 * {@code LocusId} or its tree of descendants.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800121 *
122 * @return this builder
123 */
Felipe Leme044c63b2019-02-12 14:35:20 -0800124 public Builder addLocusId(@NonNull LocusId locusId, boolean recursive) {
Adam He3d0409b2019-01-15 14:22:04 -0800125 throwIfDestroyed();
Felipe Leme044c63b2019-02-12 14:35:20 -0800126 Preconditions.checkState(!mForEverything, "Already is for everything");
127 Preconditions.checkNotNull(locusId);
Adam He3d0409b2019-01-15 14:22:04 -0800128
Felipe Leme044c63b2019-02-12 14:35:20 -0800129 if (mLocusIds == null) {
130 mLocusIds = new ArrayList<>();
Adam He3d0409b2019-01-15 14:22:04 -0800131 mRecursive = new IntArray();
132 }
133
Felipe Leme044c63b2019-02-12 14:35:20 -0800134 mLocusIds.add(locusId);
Adam He3d0409b2019-01-15 14:22:04 -0800135 mRecursive.add(recursive ? 1 : 0);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800136 return this;
137 }
138
139 /**
140 * Builds the {@link UserDataRemovalRequest}.
141 */
142 @NonNull
143 public UserDataRemovalRequest build() {
Adam He3d0409b2019-01-15 14:22:04 -0800144 throwIfDestroyed();
145
Felipe Leme044c63b2019-02-12 14:35:20 -0800146 Preconditions.checkState(mForEverything || mLocusIds != null);
Adam He3d0409b2019-01-15 14:22:04 -0800147
148 mDestroyed = true;
149 return new UserDataRemovalRequest(this);
150 }
151
152 private void throwIfDestroyed() {
153 Preconditions.checkState(!mDestroyed, "Already destroyed!");
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800154 }
155 }
156
157 @Override
158 public int describeContents() {
159 return 0;
160 }
161
162 @Override
163 public void writeToParcel(Parcel parcel, int flags) {
Adam He3d0409b2019-01-15 14:22:04 -0800164 parcel.writeString(mPackageName);
165 parcel.writeBoolean(mForEverything);
166 if (!mForEverything) {
Felipe Leme044c63b2019-02-12 14:35:20 -0800167 final int size = mLocusIdRequests.size();
Adam He3d0409b2019-01-15 14:22:04 -0800168 parcel.writeInt(size);
169 for (int i = 0; i < size; i++) {
Felipe Leme044c63b2019-02-12 14:35:20 -0800170 final LocusIdRequest request = mLocusIdRequests.get(i);
171 parcel.writeValue(request.getLocusId());
Adam He3d0409b2019-01-15 14:22:04 -0800172 parcel.writeBoolean(request.isRecursive());
173 }
174 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800175 }
176
177 public static final Parcelable.Creator<UserDataRemovalRequest> CREATOR =
178 new Parcelable.Creator<UserDataRemovalRequest>() {
179
180 @Override
181 public UserDataRemovalRequest createFromParcel(Parcel parcel) {
Adam He3d0409b2019-01-15 14:22:04 -0800182 return new UserDataRemovalRequest(parcel);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800183 }
184
185 @Override
186 public UserDataRemovalRequest[] newArray(int size) {
187 return new UserDataRemovalRequest[size];
188 }
189 };
190
191 /**
Felipe Leme044c63b2019-02-12 14:35:20 -0800192 * Representation of a request to remove data associated with a {@link LocusId}.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800193 */
Felipe Leme044c63b2019-02-12 14:35:20 -0800194 public final class LocusIdRequest {
195 private final @NonNull LocusId mLocusId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800196 private final boolean mRecursive;
197
Felipe Leme044c63b2019-02-12 14:35:20 -0800198 private LocusIdRequest(@NonNull LocusId locusId, boolean recursive) {
199 this.mLocusId = locusId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800200 this.mRecursive = recursive;
201 }
202
203 /**
Felipe Leme044c63b2019-02-12 14:35:20 -0800204 * Gets the {@code LocusId} per se.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800205 */
206 @NonNull
Felipe Leme044c63b2019-02-12 14:35:20 -0800207 public LocusId getLocusId() {
208 return mLocusId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800209 }
210
211 /**
Felipe Leme044c63b2019-02-12 14:35:20 -0800212 * Checks whether the request is to remove just the data associated with the {@link LocusId}
213 * per se, or also its descendants.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800214 */
215 @NonNull
216 public boolean isRecursive() {
217 return mRecursive;
218 }
219 }
220}