Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.view.contentcapture; |
| 17 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 18 | import android.annotation.IntDef; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 20 | import android.app.ActivityThread; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 21 | import android.content.LocusId; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 24 | import android.util.IntArray; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 25 | |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 26 | import com.android.internal.util.Preconditions; |
| 27 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 30 | import java.util.ArrayList; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 31 | import 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 | */ |
| 37 | public final class UserDataRemovalRequest implements Parcelable { |
| 38 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 39 | /** |
| 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 He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 52 | private final String mPackageName; |
| 53 | |
| 54 | private final boolean mForEverything; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 55 | private ArrayList<LocusIdRequest> mLocusIdRequests; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 56 | |
| 57 | private UserDataRemovalRequest(@NonNull Builder builder) { |
| 58 | mPackageName = ActivityThread.currentActivityThread().getApplication().getPackageName(); |
| 59 | mForEverything = builder.mForEverything; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 60 | if (builder.mLocusIds != null) { |
| 61 | final int size = builder.mLocusIds.size(); |
| 62 | mLocusIdRequests = new ArrayList<>(size); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 63 | for (int i = 0; i < size; i++) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 64 | mLocusIdRequests.add(new LocusIdRequest(builder.mLocusIds.get(i), |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 65 | builder.mFlags.get(i))); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 66 | } |
| 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 Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 75 | mLocusIdRequests = new ArrayList<>(size); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 76 | for (int i = 0; i < size; i++) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 77 | mLocusIdRequests.add(new LocusIdRequest((LocusId) parcel.readValue(null), |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 78 | parcel.readInt())); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets the name of the app that's making the request. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 85 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 86 | @NonNull |
| 87 | public String getPackageName() { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 88 | return mPackageName; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks if app is requesting to remove all user data associated with its package. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 93 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 94 | public boolean isForEverything() { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 95 | return mForEverything; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 99 | * Gets the list of {@code LousId}s the apps is requesting to remove. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 100 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 101 | @NonNull |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 102 | public List<LocusIdRequest> getLocusIdRequests() { |
| 103 | return mLocusIdRequests; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Builder for {@link UserDataRemovalRequest} objects. |
| 108 | */ |
| 109 | public static final class Builder { |
| 110 | |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 111 | private boolean mForEverything; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 112 | private ArrayList<LocusId> mLocusIds; |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 113 | private IntArray mFlags; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 114 | |
| 115 | private boolean mDestroyed; |
| 116 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 117 | /** |
| 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 He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 124 | throwIfDestroyed(); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 125 | Preconditions.checkState(mLocusIds == null, "Already added LocusIds"); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 126 | |
| 127 | mForEverything = true; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 128 | return this; |
| 129 | } |
| 130 | |
| 131 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 132 | * Request service to remove data associated with a given {@link LocusId}. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 133 | * |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 134 | * @param locusId the {@link LocusId} being requested to be removed. |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 135 | * @param flags either {@link UserDataRemovalRequest#FLAG_IS_PREFIX} or {@code 0} |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 136 | * |
| 137 | * @return this builder |
| 138 | */ |
Felipe Leme | ce6877b | 2019-02-28 09:02:26 -0800 | [diff] [blame] | 139 | @NonNull |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 140 | public Builder addLocusId(@NonNull LocusId locusId, @Flags int flags) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 141 | throwIfDestroyed(); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 142 | Preconditions.checkState(!mForEverything, "Already is for everything"); |
| 143 | Preconditions.checkNotNull(locusId); |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 144 | // felipeal: check flags |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 145 | |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 146 | if (mLocusIds == null) { |
| 147 | mLocusIds = new ArrayList<>(); |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 148 | mFlags = new IntArray(); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 151 | mLocusIds.add(locusId); |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 152 | mFlags.add(flags); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 153 | return this; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Builds the {@link UserDataRemovalRequest}. |
| 158 | */ |
| 159 | @NonNull |
| 160 | public UserDataRemovalRequest build() { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 161 | throwIfDestroyed(); |
| 162 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 163 | Preconditions.checkState(mForEverything || mLocusIds != null, |
| 164 | "must call either #forEverything() or add one #addLocusId()"); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 165 | |
| 166 | mDestroyed = true; |
| 167 | return new UserDataRemovalRequest(this); |
| 168 | } |
| 169 | |
| 170 | private void throwIfDestroyed() { |
| 171 | Preconditions.checkState(!mDestroyed, "Already destroyed!"); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public int describeContents() { |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void writeToParcel(Parcel parcel, int flags) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 182 | parcel.writeString(mPackageName); |
| 183 | parcel.writeBoolean(mForEverything); |
| 184 | if (!mForEverything) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 185 | final int size = mLocusIdRequests.size(); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 186 | parcel.writeInt(size); |
| 187 | for (int i = 0; i < size; i++) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 188 | final LocusIdRequest request = mLocusIdRequests.get(i); |
| 189 | parcel.writeValue(request.getLocusId()); |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 190 | parcel.writeInt(request.getFlags()); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 195 | public static final @android.annotation.NonNull Parcelable.Creator<UserDataRemovalRequest> CREATOR = |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 196 | new Parcelable.Creator<UserDataRemovalRequest>() { |
| 197 | |
| 198 | @Override |
Felipe Leme | ce6877b | 2019-02-28 09:02:26 -0800 | [diff] [blame] | 199 | @NonNull |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 200 | public UserDataRemovalRequest createFromParcel(Parcel parcel) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 201 | return new UserDataRemovalRequest(parcel); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
Felipe Leme | ce6877b | 2019-02-28 09:02:26 -0800 | [diff] [blame] | 205 | @NonNull |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 206 | public UserDataRemovalRequest[] newArray(int size) { |
| 207 | return new UserDataRemovalRequest[size]; |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 212 | * Representation of a request to remove data associated with a {@link LocusId}. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 213 | */ |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 214 | public final class LocusIdRequest { |
| 215 | private final @NonNull LocusId mLocusId; |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 216 | private final @Flags int mFlags; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 217 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 218 | private LocusIdRequest(@NonNull LocusId locusId, @Flags int flags) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 219 | this.mLocusId = locusId; |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 220 | this.mFlags = flags; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 224 | * Gets the {@code LocusId} per se. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 225 | */ |
| 226 | @NonNull |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 227 | public LocusId getLocusId() { |
| 228 | return mLocusId; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 232 | * Gets the flags associates with request. |
| 233 | * |
| 234 | * @return either {@link UserDataRemovalRequest#FLAG_IS_PREFIX} or {@code 0}. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 235 | */ |
| 236 | @NonNull |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 237 | public @Flags int getFlags() { |
| 238 | return mFlags; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |