Debian Codegraph @

core/java/android/view/translation/TranslationResponse.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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.IntDef;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.service.translation.TranslationService;
24 import android.util.SparseArray;
25
26 import com.android.internal.util.DataClass;
27
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.Objects;
31
32 /**
33 * Response from the {@link TranslationService}, which contains the translated result.
34 */
35 @DataClass(genBuilder = true, genToString = true, genHiddenConstDefs = true)
36 public final class TranslationResponse implements Parcelable {
37
38 /**
39 * The {@link TranslationService} was successful in translating.
40 */
41 public static final int TRANSLATION_STATUS_SUCCESS = 0;
42 /**
43 * The {@link TranslationService} returned unknown translation result.
44 */
45 public static final int TRANSLATION_STATUS_UNKNOWN_ERROR = 1;
46 /**
47 * The languages of the request is not available to be translated.
48 */
49 public static final int TRANSLATION_STATUS_CONTEXT_UNSUPPORTED = 2;
50
51 /**
52 * The translation result status code.
53 */
54 private final @TranslationStatus int mTranslationStatus;
55
56 /**
57 * List of translated {@link TranslationResponseValue}s. The key of entries in this list
58 * will be their respective index in {@link TranslationRequest#getTranslationRequestValues()}.
59 */
60 @NonNull
61 private final SparseArray<TranslationResponseValue> mTranslationResponseValues;
62
63 /**
64 * List of translated {@link ViewTranslationResponse}s. The key of entries in this list
65 * will be their respective index in {@link TranslationRequest#getViewTranslationRequests()}.
66 */
67 @NonNull
68 private final SparseArray<ViewTranslationResponse> mViewTranslationResponses;
69
70 /**
71 * Whether this response contains complete translated values, or is the final response in a
72 * series of partial responses.
73 *
74 * <p>This is {@code true} by default.</p>
75 */
76 private final boolean mFinalResponse;
77
78 abstract static class BaseBuilder {
79
80 /**
81 * @removed Use {@link Builder#Builder(int)}.
82 * @hide
83 */
84 @Deprecated
85 public abstract Builder setTranslationStatus(@TranslationStatus int value);
86
87 /**
88 * Adds {@link TranslationResponseValue} to be translated. The input
89 * TranslationResponseValue format should match those provided by the
90 * {@link android.view.translation.Translator}'s targetSpec.
91 *
92 * @param value the translated value.
93 * @return this Builder.
94 */
95 @NonNull
96 @SuppressWarnings("MissingGetterMatchingBuilder")
97 public Builder setTranslationResponseValue(int index,
98 @NonNull TranslationResponseValue value) {
99 Objects.requireNonNull(value, "value should not be null");
100 final Builder builder = (Builder) this;
101
102 if (builder.mTranslationResponseValues == null) {
103 builder.setTranslationResponseValues(new SparseArray<>());
104 }
105 builder.mTranslationResponseValues.put(index, value);
106 return builder;
107 }
108
109 /**
110 * Sets the list of {@link ViewTranslationResponse} to be translated. The input
111 * ViewTranslationResponse contains {@link TranslationResponseValue}s whose format should
112 * match those provided by the {@link android.view.translation.Translator}'s targetSpec.
113 *
114 * @param response the translated response.
115 * @return this Builder.
116 */
117 @NonNull
118 @SuppressWarnings("MissingGetterMatchingBuilder")
119 public Builder setViewTranslationResponse(int index,
120 @NonNull ViewTranslationResponse response) {
121 Objects.requireNonNull(response, "value should not be null");
122 final Builder builder = (Builder) this;
123
124 if (builder.mViewTranslationResponses == null) {
125 builder.setViewTranslationResponses(new SparseArray<>());
126 }
127 builder.mViewTranslationResponses.put(index, response);
128 return builder;
129 }
130 }
131
132 private static SparseArray<TranslationResponseValue> defaultTranslationResponseValues() {
133 return new SparseArray<>();
134 }
135
136 private static SparseArray<ViewTranslationResponse> defaultViewTranslationResponses() {
137 return new SparseArray<>();
138 }
139
140 private static boolean defaultFinalResponse() {
141 return true;
142 }
143
144
145
146
147 // Code below generated by codegen v1.0.23.
148 //
149 // DO NOT MODIFY!
150 // CHECKSTYLE:OFF Generated code
151 //
152 // To regenerate run:
153 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/view/translation/TranslationResponse.java
154 //
155 // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
156 // Settings > Editor > Code Style > Formatter Control
157 //@formatter:off
158
159
160 /** @hide */
161 @IntDef(prefix = "TRANSLATION_STATUS_", value = {
162 TRANSLATION_STATUS_SUCCESS,
163 TRANSLATION_STATUS_UNKNOWN_ERROR,
164 TRANSLATION_STATUS_CONTEXT_UNSUPPORTED
165 })
166 @Retention(RetentionPolicy.SOURCE)
167 @DataClass.Generated.Member
168 public @interface TranslationStatus {}
169
170 /** @hide */
171 @DataClass.Generated.Member
172 public static String translationStatusToString(@TranslationStatus int value) {
173 switch (value) {
174 case TRANSLATION_STATUS_SUCCESS:
175 return "TRANSLATION_STATUS_SUCCESS";
176 case TRANSLATION_STATUS_UNKNOWN_ERROR:
177 return "TRANSLATION_STATUS_UNKNOWN_ERROR";
178 case TRANSLATION_STATUS_CONTEXT_UNSUPPORTED:
179 return "TRANSLATION_STATUS_CONTEXT_UNSUPPORTED";
180 default: return Integer.toHexString(value);
181 }
182 }
183
184 @DataClass.Generated.Member
185 /* package-private */ TranslationResponse(
186 @TranslationStatus int translationStatus,
187 @NonNull SparseArray<TranslationResponseValue> translationResponseValues,
188 @NonNull SparseArray<ViewTranslationResponse> viewTranslationResponses,
189 boolean finalResponse) {
190 this.mTranslationStatus = translationStatus;
191
192 if (!(mTranslationStatus == TRANSLATION_STATUS_SUCCESS)
193 && !(mTranslationStatus == TRANSLATION_STATUS_UNKNOWN_ERROR)
194 && !(mTranslationStatus == TRANSLATION_STATUS_CONTEXT_UNSUPPORTED)) {
195 throw new java.lang.IllegalArgumentException(
196 "translationStatus was " + mTranslationStatus + " but must be one of: "
197 + "TRANSLATION_STATUS_SUCCESS(" + TRANSLATION_STATUS_SUCCESS + "), "
198 + "TRANSLATION_STATUS_UNKNOWN_ERROR(" + TRANSLATION_STATUS_UNKNOWN_ERROR + "), "
199 + "TRANSLATION_STATUS_CONTEXT_UNSUPPORTED(" + TRANSLATION_STATUS_CONTEXT_UNSUPPORTED + ")");
200 }
201
202 this.mTranslationResponseValues = translationResponseValues;
203 com.android.internal.util.AnnotationValidations.validate(
204 NonNull.class, null, mTranslationResponseValues);
205 this.mViewTranslationResponses = viewTranslationResponses;
206 com.android.internal.util.AnnotationValidations.validate(
207 NonNull.class, null, mViewTranslationResponses);
208 this.mFinalResponse = finalResponse;
209
210 // onConstructed(); // You can define this method to get a callback
211 }
212
213 /**
214 * The translation result status code.
215 */
216 @DataClass.Generated.Member
217 public @TranslationStatus int getTranslationStatus() {
218 return mTranslationStatus;
219 }
220
221 /**
222 * List of translated {@link TranslationResponseValue}s. The key of entries in this list
223 * will be their respective index in {@link TranslationRequest#getTranslationRequestValues()}.
224 */
225 @DataClass.Generated.Member
226 public @NonNull SparseArray<TranslationResponseValue> getTranslationResponseValues() {
227 return mTranslationResponseValues;
228 }
229
230 /**
231 * List of translated {@link ViewTranslationResponse}s. The key of entries in this list
232 * will be their respective index in {@link TranslationRequest#getViewTranslationRequests()}.
233 */
234 @DataClass.Generated.Member
235 public @NonNull SparseArray<ViewTranslationResponse> getViewTranslationResponses() {
236 return mViewTranslationResponses;
237 }
238
239 /**
240 * Whether this response contains complete translated values, or is the final response in a
241 * series of partial responses.
242 *
243 * <p>This is {@code true} by default.</p>
244 */
245 @DataClass.Generated.Member
246 public boolean isFinalResponse() {
247 return mFinalResponse;
248 }
249
250 @Override
251 @DataClass.Generated.Member
252 public String toString() {
253 // You can override field toString logic by defining methods like:
254 // String fieldNameToString() { ... }
255
256 return "TranslationResponse { " +
257 "translationStatus = " + translationStatusToString(mTranslationStatus) + ", " +
258 "translationResponseValues = " + mTranslationResponseValues + ", " +
259 "viewTranslationResponses = " + mViewTranslationResponses + ", " +
260 "finalResponse = " + mFinalResponse +
261 " }";
262 }
263
264 @Override
265 @DataClass.Generated.Member
266 public void writeToParcel(@NonNull Parcel dest, int flags) {
267 // You can override field parcelling by defining methods like:
268 // void parcelFieldName(Parcel dest, int flags) { ... }
269
270 byte flg = 0;
271 if (mFinalResponse) flg |= 0x8;
272 dest.writeByte(flg);
273 dest.writeInt(mTranslationStatus);
274 dest.writeSparseArray(mTranslationResponseValues);
275 dest.writeSparseArray(mViewTranslationResponses);
276 }
277
278 @Override
279 @DataClass.Generated.Member
280 public int describeContents() { return 0; }
281
282 /** @hide */
283 @SuppressWarnings({"unchecked", "RedundantCast"})
284 @DataClass.Generated.Member
285 /* package-private */ TranslationResponse(@NonNull Parcel in) {
286 // You can override field unparcelling by defining methods like:
287 // static FieldType unparcelFieldName(Parcel in) { ... }
288
289 byte flg = in.readByte();
290 boolean finalResponse = (flg & 0x8) != 0;
291 int translationStatus = in.readInt();
292 SparseArray<TranslationResponseValue> translationResponseValues = (SparseArray) in.readSparseArray(TranslationResponseValue.class.getClassLoader());
293 SparseArray<ViewTranslationResponse> viewTranslationResponses = (SparseArray) in.readSparseArray(ViewTranslationResponse.class.getClassLoader());
294
295 this.mTranslationStatus = translationStatus;
296
297 if (!(mTranslationStatus == TRANSLATION_STATUS_SUCCESS)
298 && !(mTranslationStatus == TRANSLATION_STATUS_UNKNOWN_ERROR)
299 && !(mTranslationStatus == TRANSLATION_STATUS_CONTEXT_UNSUPPORTED)) {
300 throw new java.lang.IllegalArgumentException(
301 "translationStatus was " + mTranslationStatus + " but must be one of: "
302 + "TRANSLATION_STATUS_SUCCESS(" + TRANSLATION_STATUS_SUCCESS + "), "
303 + "TRANSLATION_STATUS_UNKNOWN_ERROR(" + TRANSLATION_STATUS_UNKNOWN_ERROR + "), "
304 + "TRANSLATION_STATUS_CONTEXT_UNSUPPORTED(" + TRANSLATION_STATUS_CONTEXT_UNSUPPORTED + ")");
305 }
306
307 this.mTranslationResponseValues = translationResponseValues;
308 com.android.internal.util.AnnotationValidations.validate(
309 NonNull.class, null, mTranslationResponseValues);
310 this.mViewTranslationResponses = viewTranslationResponses;
311 com.android.internal.util.AnnotationValidations.validate(
312 NonNull.class, null, mViewTranslationResponses);
313 this.mFinalResponse = finalResponse;
314
315 // onConstructed(); // You can define this method to get a callback
316 }
317
318 @DataClass.Generated.Member
319 public static final @NonNull Parcelable.Creator<TranslationResponse> CREATOR
320 = new Parcelable.Creator<TranslationResponse>() {
321 @Override
322 public TranslationResponse[] newArray(int size) {
323 return new TranslationResponse[size];
324 }
325
326 @Override
327 public TranslationResponse createFromParcel(@NonNull Parcel in) {
328 return new TranslationResponse(in);
329 }
330 };
331
332 /**
333 * A builder for {@link TranslationResponse}
334 */
335 @SuppressWarnings("WeakerAccess")
336 @DataClass.Generated.Member
337 public static final class Builder extends BaseBuilder {
338
339 private @TranslationStatus int mTranslationStatus;
340 private @NonNull SparseArray<TranslationResponseValue> mTranslationResponseValues;
341 private @NonNull SparseArray<ViewTranslationResponse> mViewTranslationResponses;
342 private boolean mFinalResponse;
343
344 private long mBuilderFieldsSet = 0L;
345
346 /**
347 * Creates a new Builder.
348 *
349 * @param translationStatus
350 * The translation result status code.
351 */
352 public Builder(
353 @TranslationStatus int translationStatus) {
354 mTranslationStatus = translationStatus;
355
356 if (!(mTranslationStatus == TRANSLATION_STATUS_SUCCESS)
357 && !(mTranslationStatus == TRANSLATION_STATUS_UNKNOWN_ERROR)
358 && !(mTranslationStatus == TRANSLATION_STATUS_CONTEXT_UNSUPPORTED)) {
359 throw new java.lang.IllegalArgumentException(
360 "translationStatus was " + mTranslationStatus + " but must be one of: "
361 + "TRANSLATION_STATUS_SUCCESS(" + TRANSLATION_STATUS_SUCCESS + "), "
362 + "TRANSLATION_STATUS_UNKNOWN_ERROR(" + TRANSLATION_STATUS_UNKNOWN_ERROR + "), "
363 + "TRANSLATION_STATUS_CONTEXT_UNSUPPORTED(" + TRANSLATION_STATUS_CONTEXT_UNSUPPORTED + ")");
364 }
365
366 }
367
368 /**
369 * The translation result status code.
370 * @removed
371 */
372 @DataClass.Generated.Member
373 @Override
374 @Deprecated
375 public @NonNull Builder setTranslationStatus(@TranslationStatus int value) {
376 checkNotUsed();
377 mBuilderFieldsSet |= 0x1;
378 mTranslationStatus = value;
379 return this;
380 }
381
382 /**
383 * List of translated {@link TranslationResponseValue}s. The key of entries in this list
384 * will be their respective index in {@link TranslationRequest#getTranslationRequestValues()}.
385 */
386 @DataClass.Generated.Member
387 public @NonNull Builder setTranslationResponseValues(@NonNull SparseArray<TranslationResponseValue> value) {
388 checkNotUsed();
389 mBuilderFieldsSet |= 0x2;
390 mTranslationResponseValues = value;
391 return this;
392 }
393
394 /**
395 * List of translated {@link ViewTranslationResponse}s. The key of entries in this list
396 * will be their respective index in {@link TranslationRequest#getViewTranslationRequests()}.
397 */
398 @DataClass.Generated.Member
399 public @NonNull Builder setViewTranslationResponses(@NonNull SparseArray<ViewTranslationResponse> value) {
400 checkNotUsed();
401 mBuilderFieldsSet |= 0x4;
402 mViewTranslationResponses = value;
403 return this;
404 }
405
406 /**
407 * Whether this response contains complete translated values, or is the final response in a
408 * series of partial responses.
409 *
410 * <p>This is {@code true} by default.</p>
411 */
412 @DataClass.Generated.Member
413 public @NonNull Builder setFinalResponse(boolean value) {
414 checkNotUsed();
415 mBuilderFieldsSet |= 0x8;
416 mFinalResponse = value;
417 return this;
418 }
419
420 /** Builds the instance. This builder should not be touched after calling this! */
421 public @NonNull TranslationResponse build() {
422 checkNotUsed();
423 mBuilderFieldsSet |= 0x10; // Mark builder used
424
425 if ((mBuilderFieldsSet & 0x2) == 0) {
426 mTranslationResponseValues = defaultTranslationResponseValues();
427 }
428 if ((mBuilderFieldsSet & 0x4) == 0) {
429 mViewTranslationResponses = defaultViewTranslationResponses();
430 }
431 if ((mBuilderFieldsSet & 0x8) == 0) {
432 mFinalResponse = defaultFinalResponse();
433 }
434 TranslationResponse o = new TranslationResponse(
435 mTranslationStatus,
436 mTranslationResponseValues,
437 mViewTranslationResponses,
438 mFinalResponse);
439 return o;
440 }
441
442 private void checkNotUsed() {
443 if ((mBuilderFieldsSet & 0x10) != 0) {
444 throw new IllegalStateException(
445 "This Builder should not be reused. Use a new Builder instance instead");
446 }
447 }
448 }
449
450 @DataClass.Generated(
451 time = 1621972659130L,
452 codegenVersion = "1.0.23",
453 sourceFile = "frameworks/base/core/java/android/view/translation/TranslationResponse.java",
454 inputSignatures = "public static final int TRANSLATION_STATUS_SUCCESS\npublic static final int TRANSLATION_STATUS_UNKNOWN_ERROR\npublic static final int TRANSLATION_STATUS_CONTEXT_UNSUPPORTED\nprivate final @android.view.translation.TranslationResponse.TranslationStatus int mTranslationStatus\nprivate final @android.annotation.NonNull android.util.SparseArray<android.view.translation.TranslationResponseValue> mTranslationResponseValues\nprivate final @android.annotation.NonNull android.util.SparseArray<android.view.translation.ViewTranslationResponse> mViewTranslationResponses\nprivate final boolean mFinalResponse\nprivate static android.util.SparseArray<android.view.translation.TranslationResponseValue> defaultTranslationResponseValues()\nprivate static android.util.SparseArray<android.view.translation.ViewTranslationResponse> defaultViewTranslationResponses()\nprivate static boolean defaultFinalResponse()\nclass TranslationResponse extends java.lang.Object implements [android.os.Parcelable]\npublic abstract @java.lang.Deprecated android.view.translation.TranslationResponse.Builder setTranslationStatus(int)\npublic @android.annotation.NonNull @java.lang.SuppressWarnings android.view.translation.TranslationResponse.Builder setTranslationResponseValue(int,android.view.translation.TranslationResponseValue)\npublic @android.annotation.NonNull @java.lang.SuppressWarnings android.view.translation.TranslationResponse.Builder setViewTranslationResponse(int,android.view.translation.ViewTranslationResponse)\nclass BaseBuilder extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genBuilder=true, genToString=true, genHiddenConstDefs=true)\npublic abstract @java.lang.Deprecated android.view.translation.TranslationResponse.Builder setTranslationStatus(int)\npublic @android.annotation.NonNull @java.lang.SuppressWarnings android.view.translation.TranslationResponse.Builder setTranslationResponseValue(int,android.view.translation.TranslationResponseValue)\npublic @android.annotation.NonNull @java.lang.SuppressWarnings android.view.translation.TranslationResponse.Builder setViewTranslationResponse(int,android.view.translation.ViewTranslationResponse)\nclass BaseBuilder extends java.lang.Object implements []")
455 @Deprecated
456 private void __metadata() {}
457
458
459 //@formatter:on
460 // End of generated code
461
462 }