1 /* 2 * Copyright (C) 2021 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.view.translation; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SuppressLint; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.util.ArrayMap; 25 import android.view.autofill.AutofillId; 26 27 import com.android.internal.util.DataClass; 28 29 import java.util.Collections; 30 import java.util.Map; 31 import java.util.Objects; 32 import java.util.Set; 33 34 /** 35 * Wrapper class representing a translation request associated with a {@link android.view.View} to 36 * be used by {@link android.service.translation.TranslationService}. 37 */ 38 @DataClass(genBuilder = false, genToString = true, genEqualsHashCode = true, genGetters = false, 39 genHiddenConstructor = true, genHiddenConstDefs = true) 40 public final class ViewTranslationRequest implements Parcelable { 41 42 /** 43 * Constant id for the default view text to be translated. This is used by 44 * {@link Builder#setValue(String, TranslationRequestValue)}. 45 */ 46 public static final String ID_TEXT = "android:text"; 47 48 /** 49 * Constant id for the default view content description to be translated. This is used by 50 * {@link Builder#setValue(String, TranslationRequestValue)}. 51 * 52 * @hide 53 */ 54 public static final String ID_CONTENT_DESCRIPTION = "android:content_description"; 55 56 /** 57 * The {@link AutofillId} of the view associated with this request. 58 */ 59 @NonNull 60 private final AutofillId mAutofillId; 61 62 @NonNull 63 @DataClass.PluralOf("translationRequestValue") 64 private final Map<String, TranslationRequestValue> mTranslationRequestValues; 65 66 /** 67 * Gets the corresponding {@link TranslationRequestValue} of the provided key. 68 * @param key String id of the translation request value to be translated. 69 * @return the {@link TranslationRequestValue}. 70 * @throws IllegalArgumentException if the key does not exist. 71 */ 72 @NonNull getValue(@onNull String key)73 public TranslationRequestValue getValue(@NonNull String key) { 74 Objects.requireNonNull(key, "key should not be null"); 75 if (!mTranslationRequestValues.containsKey(key)) { 76 throw new IllegalArgumentException("Request does not contain value for key=" + key); 77 } 78 return mTranslationRequestValues.get(key); 79 } 80 81 /** 82 * Returns all keys in this request as a {@link Set} of Strings. The keys are used by 83 * {@link #getValue(String)} to get the {@link TranslationRequestValue}s. 84 */ 85 @NonNull getKeys()86 public Set<String> getKeys() { 87 return mTranslationRequestValues.keySet(); 88 } 89 90 91 /** 92 * Returns the associated {@link AutofillId} of this request. 93 */ 94 @NonNull getAutofillId()95 public AutofillId getAutofillId() { 96 return mAutofillId; 97 } 98 defaultTranslationRequestValues()99 private static Map<String, TranslationRequestValue> defaultTranslationRequestValues() { 100 return Collections.emptyMap(); 101 } 102 103 /** 104 * A builder for building ViewTranslationRequest. 105 */ 106 public static final class Builder { 107 108 private @NonNull AutofillId mAutofillId; 109 private @NonNull Map<String, TranslationRequestValue> mTranslationRequestValues; 110 111 private long mBuilderFieldsSet = 0L; 112 113 /** 114 * Creates a new Builder. 115 * 116 * @param autofillId The {@link AutofillId} of the view associated with this request. 117 */ Builder(@onNull AutofillId autofillId)118 public Builder(@NonNull AutofillId autofillId) { 119 mAutofillId = autofillId; 120 com.android.internal.util.AnnotationValidations.validate( 121 NonNull.class, null, mAutofillId); 122 } 123 124 /** 125 * Creates a new Builder. 126 * 127 * @param autofillId the {@link AutofillId} of the non-virtual view hosting the virtual view 128 * hierarchy associated with this request. 129 * @param virtualChildId the id of the virtual view in the host view. This id is the same 130 * virtual id provided through content capture. 131 */ Builder(@onNull AutofillId autofillId, long virtualChildId)132 public Builder(@NonNull AutofillId autofillId, long virtualChildId) { 133 mAutofillId = new AutofillId(autofillId, virtualChildId, AutofillId.NO_SESSION); 134 com.android.internal.util.AnnotationValidations.validate( 135 NonNull.class, null, mAutofillId); 136 } 137 138 /** 139 * Sets the corresponding {@link TranslationRequestValue} for the provided key. 140 * 141 * @param key The key for this translation request value. 142 * @param value the translation request value holding the content to be translated. 143 * @return this builder. 144 */ 145 @SuppressLint("MissingGetterMatchingBuilder") 146 @NonNull setValue(@onNull String key, @NonNull TranslationRequestValue value)147 public Builder setValue(@NonNull String key, @NonNull TranslationRequestValue value) { 148 if (mTranslationRequestValues == null) { 149 setTranslationRequestValues(new ArrayMap<>()); 150 } 151 mTranslationRequestValues.put(key, value); 152 return this; 153 } 154 155 /** 156 * Builds the instance. This builder should not be touched after calling this! 157 */ 158 @NonNull build()159 public ViewTranslationRequest build() { 160 checkNotUsed(); 161 mBuilderFieldsSet |= 0x4; // Mark builder used 162 163 if ((mBuilderFieldsSet & 0x2) == 0) { 164 mTranslationRequestValues = defaultTranslationRequestValues(); 165 } 166 ViewTranslationRequest o = new ViewTranslationRequest( 167 mAutofillId, 168 mTranslationRequestValues); 169 return o; 170 } 171 setTranslationRequestValues(@onNull Map<String, TranslationRequestValue> value)172 Builder setTranslationRequestValues(@NonNull Map<String, TranslationRequestValue> value) { 173 checkNotUsed(); 174 mBuilderFieldsSet |= 0x2; 175 mTranslationRequestValues = value; 176 return this; 177 } 178 checkNotUsed()179 private void checkNotUsed() { 180 if ((mBuilderFieldsSet & 0x4) != 0) { 181 throw new IllegalStateException( 182 "This Builder should not be reused. Use a new Builder instance instead"); 183 } 184 } 185 } 186 187 188 189 // Code below generated by codegen v1.0.23. 190 // 191 // DO NOT MODIFY! 192 // CHECKSTYLE:OFF Generated code 193 // 194 // To regenerate run: 195 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/view/translation/ViewTranslationRequest.java 196 // 197 // To exclude the generated code from IntelliJ auto-formatting enable (one-time): 198 // Settings > Editor > Code Style > Formatter Control 199 //@formatter:off 200 201 202 /** @hide */ 203 @android.annotation.StringDef(prefix = "ID_", value = { 204 ID_TEXT, 205 ID_CONTENT_DESCRIPTION 206 }) 207 @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE) 208 @DataClass.Generated.Member 209 public @interface Id {} 210 211 /** 212 * Creates a new ViewTranslationRequest. 213 * 214 * @param autofillId 215 * The {@link AutofillId} of the view associated with this request. 216 * @hide 217 */ 218 @DataClass.Generated.Member ViewTranslationRequest( @onNull AutofillId autofillId, @NonNull Map<String,TranslationRequestValue> translationRequestValues)219 public ViewTranslationRequest( 220 @NonNull AutofillId autofillId, 221 @NonNull Map<String,TranslationRequestValue> translationRequestValues) { 222 this.mAutofillId = autofillId; 223 com.android.internal.util.AnnotationValidations.validate( 224 NonNull.class, null, mAutofillId); 225 this.mTranslationRequestValues = translationRequestValues; 226 com.android.internal.util.AnnotationValidations.validate( 227 NonNull.class, null, mTranslationRequestValues); 228 229 // onConstructed(); // You can define this method to get a callback 230 } 231 232 @Override 233 @DataClass.Generated.Member toString()234 public String toString() { 235 // You can override field toString logic by defining methods like: 236 // String fieldNameToString() { ... } 237 238 return "ViewTranslationRequest { " + 239 "autofillId = " + mAutofillId + ", " + 240 "translationRequestValues = " + mTranslationRequestValues + 241 " }"; 242 } 243 244 @Override 245 @DataClass.Generated.Member equals(@ullable Object o)246 public boolean equals(@Nullable Object o) { 247 // You can override field equality logic by defining either of the methods like: 248 // boolean fieldNameEquals(ViewTranslationRequest other) { ... } 249 // boolean fieldNameEquals(FieldType otherValue) { ... } 250 251 if (this == o) return true; 252 if (o == null || getClass() != o.getClass()) return false; 253 @SuppressWarnings("unchecked") 254 ViewTranslationRequest that = (ViewTranslationRequest) o; 255 //noinspection PointlessBooleanExpression 256 return true 257 && Objects.equals(mAutofillId, that.mAutofillId) 258 && Objects.equals(mTranslationRequestValues, that.mTranslationRequestValues); 259 } 260 261 @Override 262 @DataClass.Generated.Member hashCode()263 public int hashCode() { 264 // You can override field hashCode logic by defining methods like: 265 // int fieldNameHashCode() { ... } 266 267 int _hash = 1; 268 _hash = 31 * _hash + Objects.hashCode(mAutofillId); 269 _hash = 31 * _hash + Objects.hashCode(mTranslationRequestValues); 270 return _hash; 271 } 272 273 @Override 274 @DataClass.Generated.Member writeToParcel(@onNull Parcel dest, int flags)275 public void writeToParcel(@NonNull Parcel dest, int flags) { 276 // You can override field parcelling by defining methods like: 277 // void parcelFieldName(Parcel dest, int flags) { ... } 278 279 dest.writeTypedObject(mAutofillId, flags); 280 dest.writeMap(mTranslationRequestValues); 281 } 282 283 @Override 284 @DataClass.Generated.Member describeContents()285 public int describeContents() { return 0; } 286 287 /** @hide */ 288 @SuppressWarnings({"unchecked", "RedundantCast"}) 289 @DataClass.Generated.Member ViewTranslationRequest(@onNull Parcel in)290 /* package-private */ ViewTranslationRequest(@NonNull Parcel in) { 291 // You can override field unparcelling by defining methods like: 292 // static FieldType unparcelFieldName(Parcel in) { ... } 293 294 AutofillId autofillId = (AutofillId) in.readTypedObject(AutofillId.CREATOR); 295 Map<String,TranslationRequestValue> translationRequestValues = new java.util.LinkedHashMap<>(); 296 in.readMap(translationRequestValues, TranslationRequestValue.class.getClassLoader()); 297 298 this.mAutofillId = autofillId; 299 com.android.internal.util.AnnotationValidations.validate( 300 NonNull.class, null, mAutofillId); 301 this.mTranslationRequestValues = translationRequestValues; 302 com.android.internal.util.AnnotationValidations.validate( 303 NonNull.class, null, mTranslationRequestValues); 304 305 // onConstructed(); // You can define this method to get a callback 306 } 307 308 @DataClass.Generated.Member 309 public static final @NonNull Parcelable.Creator<ViewTranslationRequest> CREATOR 310 = new Parcelable.Creator<ViewTranslationRequest>() { 311 @Override 312 public ViewTranslationRequest[] newArray(int size) { 313 return new ViewTranslationRequest[size]; 314 } 315 316 @Override 317 public ViewTranslationRequest createFromParcel(@NonNull Parcel in) { 318 return new ViewTranslationRequest(in); 319 } 320 }; 321 322 @DataClass.Generated( 323 time = 1621230365943L, 324 codegenVersion = "1.0.23", 325 sourceFile = "frameworks/base/core/java/android/view/translation/ViewTranslationRequest.java", 326 inputSignatures = "public static final java.lang.String ID_TEXT\npublic static final java.lang.String ID_CONTENT_DESCRIPTION\nprivate final @android.annotation.NonNull android.view.autofill.AutofillId mAutofillId\nprivate final @android.annotation.NonNull @com.android.internal.util.DataClass.PluralOf(\"translationRequestValue\") java.util.Map<java.lang.String,android.view.translation.TranslationRequestValue> mTranslationRequestValues\npublic @android.annotation.NonNull android.view.translation.TranslationRequestValue getValue(java.lang.String)\npublic @android.annotation.NonNull java.util.Set<java.lang.String> getKeys()\npublic @android.annotation.NonNull android.view.autofill.AutofillId getAutofillId()\nprivate static java.util.Map<java.lang.String,android.view.translation.TranslationRequestValue> defaultTranslationRequestValues()\nclass ViewTranslationRequest extends java.lang.Object implements [android.os.Parcelable]\nprivate @android.annotation.NonNull android.view.autofill.AutofillId mAutofillId\nprivate @android.annotation.NonNull java.util.Map<java.lang.String,android.view.translation.TranslationRequestValue> mTranslationRequestValues\nprivate long mBuilderFieldsSet\npublic @android.annotation.SuppressLint @android.annotation.NonNull android.view.translation.ViewTranslationRequest.Builder setValue(java.lang.String,android.view.translation.TranslationRequestValue)\npublic @android.annotation.NonNull android.view.translation.ViewTranslationRequest build()\n android.view.translation.ViewTranslationRequest.Builder setTranslationRequestValues(java.util.Map<java.lang.String,android.view.translation.TranslationRequestValue>)\nprivate void checkNotUsed()\nclass Builder extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genBuilder=false, genToString=true, genEqualsHashCode=true, genGetters=false, genHiddenConstructor=true, genHiddenConstDefs=true)") 327 @Deprecated __metadata()328 private void __metadata() {} 329 330 331 //@formatter:on 332 // End of generated code 333 334 } 335