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