1 /* 2 * Copyright (C) 2022 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 17 package android.service.autofill; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.view.autofill.AutofillValue; 22 23 import com.android.internal.util.DataClass; 24 25 import java.util.regex.Pattern; 26 27 /** 28 * This class is used to set all information of a field. Such as the {@link AutofillValue} 29 * to be autofilled, a <a href="#Filtering">explicit filter</a>, and presentations to be 30 * visualized, etc. 31 */ 32 public final class Field { 33 34 /** 35 * The value to be autofilled. Pass {@code null} if you do not have the value 36 * but the target view is a logical part of the dataset. For example, if the 37 * dataset needs authentication and you have no access to the value. 38 */ 39 private @Nullable AutofillValue mValue; 40 41 /** 42 * Regex used to determine if the dataset should be shown in the autofill UI; 43 * when {@code null}, it disables filtering on that dataset (this is the recommended 44 * approach when {@code value} is not {@code null} and field contains sensitive data 45 * such as passwords). 46 * 47 * @see Dataset.DatasetFieldFilter 48 * @hide 49 */ 50 private @Nullable Dataset.DatasetFieldFilter mFilter; 51 52 /** 53 * The presentations used to visualize this field in Autofill UI. 54 */ 55 private @Nullable Presentations mPresentations; 56 57 Field( @ullable AutofillValue value, @Nullable Dataset.DatasetFieldFilter filter, @Nullable Presentations presentations)58 /* package-private */ Field( 59 @Nullable AutofillValue value, 60 @Nullable Dataset.DatasetFieldFilter filter, 61 @Nullable Presentations presentations) { 62 this.mValue = value; 63 this.mFilter = filter; 64 this.mPresentations = presentations; 65 } 66 67 /** 68 * The value to be autofilled. Pass {@code null} if you do not have the value 69 * but the target view is a logical part of the dataset. For example, if the 70 * dataset needs authentication and you have no access to the value. 71 */ 72 @DataClass.Generated.Member getValue()73 public @Nullable AutofillValue getValue() { 74 return mValue; 75 } 76 77 /** 78 * Regex used to determine if the dataset should be shown in the autofill UI; 79 * when {@code null}, it disables filtering on that dataset (this is the recommended 80 * approach when {@code value} is not {@code null} and field contains sensitive data 81 * such as passwords). 82 * 83 * @see Dataset.DatasetFieldFilter 84 * @hide 85 */ getDatasetFieldFilter()86 public @Nullable Dataset.DatasetFieldFilter getDatasetFieldFilter() { 87 return mFilter; 88 } 89 90 /** 91 * Regex used to determine if the dataset should be shown in the autofill UI; 92 * when {@code null}, it disables filtering on that dataset (this is the recommended 93 * approach when {@code value} is not {@code null} and field contains sensitive data 94 * such as passwords). 95 */ getFilter()96 public @Nullable Pattern getFilter() { 97 return mFilter == null ? null : mFilter.pattern; 98 } 99 100 /** 101 * The presentations used to visualize this field in Autofill UI. 102 */ getPresentations()103 public @Nullable Presentations getPresentations() { 104 return mPresentations; 105 } 106 107 /** 108 * A builder for {@link Field} 109 */ 110 public static final class Builder { 111 112 private @Nullable AutofillValue mValue = null; 113 private @Nullable Dataset.DatasetFieldFilter mFilter = null; 114 private @Nullable Presentations mPresentations = null; 115 private boolean mDestroyed = false; 116 Builder()117 public Builder() { 118 } 119 120 /** 121 * The value to be autofilled. Pass {@code null} if you do not have the value 122 * but the target view is a logical part of the dataset. For example, if the 123 * dataset needs authentication and you have no access to the value. 124 */ setValue(@onNull AutofillValue value)125 public @NonNull Builder setValue(@NonNull AutofillValue value) { 126 checkNotUsed(); 127 mValue = value; 128 return this; 129 } 130 131 /** 132 * Regex used to determine if the dataset should be shown in the autofill UI; 133 * when {@code null}, it disables filtering on that dataset (this is the recommended 134 * approach when {@code value} is not {@code null} and field contains sensitive data 135 * such as passwords). 136 */ setFilter(@ullable Pattern value)137 public @NonNull Builder setFilter(@Nullable Pattern value) { 138 checkNotUsed(); 139 mFilter = new Dataset.DatasetFieldFilter(value); 140 return this; 141 } 142 143 /** 144 * The presentations used to visualize this field in Autofill UI. 145 */ setPresentations(@onNull Presentations value)146 public @NonNull Builder setPresentations(@NonNull Presentations value) { 147 checkNotUsed(); 148 mPresentations = value; 149 return this; 150 } 151 152 /** Builds the instance. This builder should not be touched after calling this! */ build()153 public @NonNull Field build() { 154 checkNotUsed(); 155 mDestroyed = true; // Mark builder used 156 157 Field o = new Field( 158 mValue, 159 mFilter, 160 mPresentations); 161 return o; 162 } 163 checkNotUsed()164 private void checkNotUsed() { 165 if (mDestroyed) { 166 throw new IllegalStateException( 167 "This Builder should not be reused. Use a new Builder instance instead"); 168 } 169 } 170 } 171 } 172