Dataset.java revision 9f9ee25515591ef33281708c0ab911962f4364a6
1/* 2 * Copyright (C) 2016 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 17package android.service.autofill; 18 19import static android.view.autofill.Helper.sDebug; 20 21import android.annotation.NonNull; 22import android.annotation.Nullable; 23import android.content.IntentSender; 24import android.os.Parcel; 25import android.os.Parcelable; 26import android.view.autofill.AutofillId; 27import android.view.autofill.AutofillValue; 28import android.widget.RemoteViews; 29import com.android.internal.util.Preconditions; 30 31import java.util.ArrayList; 32 33/** 34 * A set of data that can be used to autofill an {@link android.app.Activity}. 35 * 36 * <p>It contains: 37 * 38 * <ol> 39 * <li>A list of values for input fields. 40 * <li>A presentation view to visualize. 41 * <li>An optional intent to authenticate. 42 * </ol> 43 * 44 * @see android.service.autofill.FillResponse for examples. 45 */ 46public final class Dataset implements Parcelable { 47 48 private final ArrayList<AutofillId> mFieldIds; 49 private final ArrayList<AutofillValue> mFieldValues; 50 private final ArrayList<RemoteViews> mFieldPresentations; 51 private final RemoteViews mPresentation; 52 private final IntentSender mAuthentication; 53 @Nullable String mId; 54 55 private Dataset(Builder builder) { 56 mFieldIds = builder.mFieldIds; 57 mFieldValues = builder.mFieldValues; 58 mFieldPresentations = builder.mFieldPresentations; 59 mPresentation = builder.mPresentation; 60 mAuthentication = builder.mAuthentication; 61 mId = builder.mId; 62 } 63 64 /** @hide */ 65 public @Nullable ArrayList<AutofillId> getFieldIds() { 66 return mFieldIds; 67 } 68 69 /** @hide */ 70 public @Nullable ArrayList<AutofillValue> getFieldValues() { 71 return mFieldValues; 72 } 73 74 /** @hide */ 75 public RemoteViews getFieldPresentation(int index) { 76 final RemoteViews customPresentation = mFieldPresentations.get(index); 77 return customPresentation != null ? customPresentation : mPresentation; 78 } 79 80 /** @hide */ 81 public @Nullable IntentSender getAuthentication() { 82 return mAuthentication; 83 } 84 85 /** @hide */ 86 public boolean isEmpty() { 87 return mFieldIds == null || mFieldIds.isEmpty(); 88 } 89 90 @Override 91 public String toString() { 92 if (!sDebug) return super.toString(); 93 94 return new StringBuilder("Dataset " + mId + " [") 95 .append("fieldIds=").append(mFieldIds) 96 .append(", fieldValues=").append(mFieldValues) 97 .append(", fieldPresentations=") 98 .append(mFieldPresentations == null ? 0 : mFieldPresentations.size()) 99 .append(", hasPresentation=").append(mPresentation != null) 100 .append(", hasAuthentication=").append(mAuthentication != null) 101 .append(']').toString(); 102 } 103 104 /** 105 * Gets the id of this dataset. 106 * 107 * @return The id of this dataset or {@code null} if not set 108 * 109 * @hide 110 */ 111 public String getId() { 112 return mId; 113 } 114 115 /** 116 * A builder for {@link Dataset} objects. You must to provide at least 117 * one value for a field or set an authentication intent. 118 */ 119 public static final class Builder { 120 private ArrayList<AutofillId> mFieldIds; 121 private ArrayList<AutofillValue> mFieldValues; 122 private ArrayList<RemoteViews> mFieldPresentations; 123 private RemoteViews mPresentation; 124 private IntentSender mAuthentication; 125 private boolean mDestroyed; 126 @Nullable private String mId; 127 128 /** 129 * Creates a new builder. 130 * 131 * @param presentation The presentation used to visualize this dataset. 132 */ 133 public Builder(@NonNull RemoteViews presentation) { 134 Preconditions.checkNotNull(presentation, "presentation must be non-null"); 135 mPresentation = presentation; 136 } 137 138 /** 139 * Creates a new builder for a dataset where each field will be visualized independently. 140 * 141 * <p>When using this constructor, fields must be set through 142 * {@link #setValue(AutofillId, AutofillValue, RemoteViews)}. 143 */ 144 public Builder() { 145 } 146 147 /** 148 * Requires a dataset authentication before autofilling the activity with this dataset. 149 * 150 * <p>This method is called when you need to provide an authentication 151 * UI for the data set. For example, when a data set contains credit card information 152 * (such as number, expiration date, and verification code), you can display UI 153 * asking for the verification code before filing in the data. Even if the 154 * data set is completely populated the system will launch the specified authentication 155 * intent and will need your approval to fill it in. Since the data set is "locked" 156 * until the user authenticates it, typically this data set name is masked 157 * (for example, "VISA....1234"). Typically you would want to store the data set 158 * labels non-encrypted and the actual sensitive data encrypted and not in memory. 159 * This allows showing the labels in the UI while involving the user if one of 160 * the items with these labels is chosen. Note that if you use sensitive data as 161 * a label, for example an email address, then it should also be encrypted.</p> 162 * 163 * <p>When a user triggers autofill, the system launches the provided intent 164 * whose extras will have the {@link 165 * android.view.autofill.AutofillManager#EXTRA_ASSIST_STRUCTURE screen content}. Once 166 * you complete your authentication flow you should set the activity result to {@link 167 * android.app.Activity#RESULT_OK} and provide the fully populated {@link Dataset 168 * dataset} by setting it to the {@link 169 * android.view.autofill.AutofillManager#EXTRA_AUTHENTICATION_RESULT} extra. For example, 170 * if you provided credit card information without the CVV for the data set in the 171 * {@link FillResponse response} then the returned data set should contain the 172 * CVV entry.</p> 173 * 174 * <p></><strong>Note:</strong> Do not make the provided pending intent 175 * immutable by using {@link android.app.PendingIntent#FLAG_IMMUTABLE} as the 176 * platform needs to fill in the authentication arguments.</p> 177 * 178 * @param authentication Intent to an activity with your authentication flow. 179 * @return This builder. 180 * 181 * @see android.app.PendingIntent 182 */ 183 public @NonNull Builder setAuthentication(@Nullable IntentSender authentication) { 184 throwIfDestroyed(); 185 mAuthentication = authentication; 186 return this; 187 } 188 189 /** 190 * Sets the id for the dataset. 191 * 192 * <p>The id of the last selected dataset can be read from 193 * {@link AutofillService#getFillEventHistory()}. If the id is not set it will not be clear 194 * if a dataset was selected as {@link AutofillService#getFillEventHistory()} uses 195 * {@code null} to indicate that no dataset was selected. 196 * 197 * @param id id for this dataset or {@code null} to unset. 198 199 * @return This builder. 200 */ 201 public @NonNull Builder setId(@Nullable String id) { 202 throwIfDestroyed(); 203 204 mId = id; 205 return this; 206 } 207 208 /** 209 * Sets the value of a field. 210 * 211 * @param id id returned by {@link 212 * android.app.assist.AssistStructure.ViewNode#getAutofillId()}. 213 * @param value value to be auto filled. 214 * @return This builder. 215 * @throws IllegalStateException if the builder was constructed without a presentation 216 * ({@link RemoteViews}). 217 */ 218 public @NonNull Builder setValue(@NonNull AutofillId id, @NonNull AutofillValue value) { 219 throwIfDestroyed(); 220 if (mPresentation == null) { 221 throw new IllegalStateException("Dataset presentation not set on constructor"); 222 } 223 setValueAndPresentation(id, value, null); 224 return this; 225 } 226 227 /** 228 * Sets the value of a field, using a custom presentation to visualize it. 229 * 230 * @param id id returned by {@link 231 * android.app.assist.AssistStructure.ViewNode#getAutofillId()}. 232 * @param value value to be auto filled. 233 * @param presentation The presentation used to visualize this field. 234 * @return This builder. 235 */ 236 public @NonNull Builder setValue(@NonNull AutofillId id, @NonNull AutofillValue value, 237 @NonNull RemoteViews presentation) { 238 throwIfDestroyed(); 239 Preconditions.checkNotNull(presentation, "presentation cannot be null"); 240 setValueAndPresentation(id, value, presentation); 241 return this; 242 } 243 244 private void setValueAndPresentation(AutofillId id, AutofillValue value, 245 RemoteViews presentation) { 246 Preconditions.checkNotNull(id, "id cannot be null"); 247 Preconditions.checkNotNull(value, "value cannot be null"); 248 if (mFieldIds != null) { 249 final int existingIdx = mFieldIds.indexOf(id); 250 if (existingIdx >= 0) { 251 mFieldValues.set(existingIdx, value); 252 mFieldPresentations.set(existingIdx, presentation); 253 return; 254 } 255 } else { 256 mFieldIds = new ArrayList<>(); 257 mFieldValues = new ArrayList<>(); 258 mFieldPresentations = new ArrayList<>(); 259 } 260 mFieldIds.add(id); 261 mFieldValues.add(value); 262 mFieldPresentations.add(presentation); 263 } 264 265 /** 266 * Creates a new {@link Dataset} instance. You should not interact 267 * with this builder once this method is called. It is required 268 * that you specified at least one field. Also it is mandatory to 269 * provide a presentation view to visualize the data set in the UI. 270 * 271 * @return The built dataset. 272 */ 273 public @NonNull Dataset build() { 274 throwIfDestroyed(); 275 mDestroyed = true; 276 if (mFieldIds == null) { 277 throw new IllegalArgumentException("at least one value must be set"); 278 } 279 return new Dataset(this); 280 } 281 282 private void throwIfDestroyed() { 283 if (mDestroyed) { 284 throw new IllegalStateException("Already called #build()"); 285 } 286 } 287 } 288 289 ///////////////////////////////////// 290 // Parcelable "contract" methods. // 291 ///////////////////////////////////// 292 293 @Override 294 public int describeContents() { 295 return 0; 296 } 297 298 @Override 299 public void writeToParcel(Parcel parcel, int flags) { 300 parcel.writeParcelable(mPresentation, flags); 301 parcel.writeTypedArrayList(mFieldIds, flags); 302 parcel.writeTypedArrayList(mFieldValues, flags); 303 parcel.writeParcelableList(mFieldPresentations, flags); 304 parcel.writeParcelable(mAuthentication, flags); 305 parcel.writeString(mId); 306 } 307 308 public static final Creator<Dataset> CREATOR = new Creator<Dataset>() { 309 @Override 310 public Dataset createFromParcel(Parcel parcel) { 311 // Always go through the builder to ensure the data ingested by 312 // the system obeys the contract of the builder to avoid attacks 313 // using specially crafted parcels. 314 final RemoteViews presentation = parcel.readParcelable(null); 315 final Builder builder = (presentation == null) 316 ? new Builder() 317 : new Builder(presentation); 318 final ArrayList<AutofillId> ids = parcel.readTypedArrayList(null); 319 final ArrayList<AutofillValue> values = parcel.readTypedArrayList(null); 320 final ArrayList<RemoteViews> presentations = new ArrayList<>(); 321 parcel.readParcelableList(presentations, null); 322 final int idCount = (ids != null) ? ids.size() : 0; 323 final int valueCount = (values != null) ? values.size() : 0; 324 for (int i = 0; i < idCount; i++) { 325 final AutofillId id = ids.get(i); 326 final AutofillValue value = (valueCount > i) ? values.get(i) : null; 327 final RemoteViews fieldPresentation = presentations.isEmpty() ? null 328 : presentations.get(i); 329 builder.setValueAndPresentation(id, value, fieldPresentation); 330 } 331 builder.setAuthentication(parcel.readParcelable(null)); 332 builder.setId(parcel.readString()); 333 return builder.build(); 334 } 335 336 @Override 337 public Dataset[] newArray(int size) { 338 return new Dataset[size]; 339 } 340 }; 341} 342