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 | |
| 18 | import android.annotation.NonNull; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 19 | import android.app.ActivityThread; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 20 | import android.content.LocusId; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 23 | import android.util.IntArray; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 24 | |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 25 | import com.android.internal.util.Preconditions; |
| 26 | |
| 27 | import java.util.ArrayList; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 28 | import 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 | */ |
| 34 | public final class UserDataRemovalRequest implements Parcelable { |
| 35 | |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 36 | private final String mPackageName; |
| 37 | |
| 38 | private final boolean mForEverything; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 39 | private ArrayList<LocusIdRequest> mLocusIdRequests; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 40 | |
| 41 | private UserDataRemovalRequest(@NonNull Builder builder) { |
| 42 | mPackageName = ActivityThread.currentActivityThread().getApplication().getPackageName(); |
| 43 | mForEverything = builder.mForEverything; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 44 | if (builder.mLocusIds != null) { |
| 45 | final int size = builder.mLocusIds.size(); |
| 46 | mLocusIdRequests = new ArrayList<>(size); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 47 | for (int i = 0; i < size; i++) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 48 | mLocusIdRequests.add(new LocusIdRequest(builder.mLocusIds.get(i), |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 49 | 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 Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 59 | mLocusIdRequests = new ArrayList<>(size); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 60 | for (int i = 0; i < size; i++) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 61 | mLocusIdRequests.add(new LocusIdRequest((LocusId) parcel.readValue(null), |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 62 | parcel.readBoolean())); |
| 63 | } |
| 64 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the name of the app that's making the request. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 69 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 70 | @NonNull |
| 71 | public String getPackageName() { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 72 | return mPackageName; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * 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] | 77 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 78 | public boolean isForEverything() { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 79 | return mForEverything; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 83 | * 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] | 84 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 85 | @NonNull |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 86 | public List<LocusIdRequest> getLocusIdRequests() { |
| 87 | return mLocusIdRequests; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Builder for {@link UserDataRemovalRequest} objects. |
| 92 | */ |
| 93 | public static final class Builder { |
| 94 | |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 95 | private boolean mForEverything; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 96 | private ArrayList<LocusId> mLocusIds; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 97 | private IntArray mRecursive; |
| 98 | |
| 99 | private boolean mDestroyed; |
| 100 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 101 | /** |
| 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 He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 108 | throwIfDestroyed(); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 109 | Preconditions.checkState(mLocusIds == null, "Already added LocusIds"); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 110 | |
| 111 | mForEverything = true; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 112 | return this; |
| 113 | } |
| 114 | |
| 115 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 116 | * Request service to remove data associated with a given {@link LocusId}. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 117 | * |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 118 | * @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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 121 | * |
| 122 | * @return this builder |
| 123 | */ |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 124 | public Builder addLocusId(@NonNull LocusId locusId, boolean recursive) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 125 | throwIfDestroyed(); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 126 | Preconditions.checkState(!mForEverything, "Already is for everything"); |
| 127 | Preconditions.checkNotNull(locusId); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 128 | |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 129 | if (mLocusIds == null) { |
| 130 | mLocusIds = new ArrayList<>(); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 131 | mRecursive = new IntArray(); |
| 132 | } |
| 133 | |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 134 | mLocusIds.add(locusId); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 135 | mRecursive.add(recursive ? 1 : 0); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 136 | return this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Builds the {@link UserDataRemovalRequest}. |
| 141 | */ |
| 142 | @NonNull |
| 143 | public UserDataRemovalRequest build() { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 144 | throwIfDestroyed(); |
| 145 | |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 146 | Preconditions.checkState(mForEverything || mLocusIds != null); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 147 | |
| 148 | mDestroyed = true; |
| 149 | return new UserDataRemovalRequest(this); |
| 150 | } |
| 151 | |
| 152 | private void throwIfDestroyed() { |
| 153 | Preconditions.checkState(!mDestroyed, "Already destroyed!"); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public int describeContents() { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void writeToParcel(Parcel parcel, int flags) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 164 | parcel.writeString(mPackageName); |
| 165 | parcel.writeBoolean(mForEverything); |
| 166 | if (!mForEverything) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 167 | final int size = mLocusIdRequests.size(); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 168 | parcel.writeInt(size); |
| 169 | for (int i = 0; i < size; i++) { |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 170 | final LocusIdRequest request = mLocusIdRequests.get(i); |
| 171 | parcel.writeValue(request.getLocusId()); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 172 | parcel.writeBoolean(request.isRecursive()); |
| 173 | } |
| 174 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | public static final Parcelable.Creator<UserDataRemovalRequest> CREATOR = |
| 178 | new Parcelable.Creator<UserDataRemovalRequest>() { |
| 179 | |
| 180 | @Override |
| 181 | public UserDataRemovalRequest createFromParcel(Parcel parcel) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 182 | return new UserDataRemovalRequest(parcel); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public UserDataRemovalRequest[] newArray(int size) { |
| 187 | return new UserDataRemovalRequest[size]; |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 192 | * Representation of a request to remove data associated with a {@link LocusId}. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 193 | */ |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 194 | public final class LocusIdRequest { |
| 195 | private final @NonNull LocusId mLocusId; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 196 | private final boolean mRecursive; |
| 197 | |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 198 | private LocusIdRequest(@NonNull LocusId locusId, boolean recursive) { |
| 199 | this.mLocusId = locusId; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 200 | this.mRecursive = recursive; |
| 201 | } |
| 202 | |
| 203 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 204 | * Gets the {@code LocusId} per se. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 205 | */ |
| 206 | @NonNull |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 207 | public LocusId getLocusId() { |
| 208 | return mLocusId; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 212 | * Checks whether the request is to remove just the data associated with the {@link LocusId} |
| 213 | * per se, or also its descendants. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 214 | */ |
| 215 | @NonNull |
| 216 | public boolean isRecursive() { |
| 217 | return mRecursive; |
| 218 | } |
| 219 | } |
| 220 | } |