1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view.translation;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import com.android.internal.util.DataClass;
import java.util.Objects;
/**
* A translated response value from {@link android.service.translation.TranslationService}.
*/
@DataClass(genBuilder = true, genToString = true, genEqualsHashCode = true,
genHiddenConstDefs = true)
public final class TranslationResponseValue implements Parcelable {
/**
* This value was successfully translated.
*/
public static final int STATUS_SUCCESS = 0;
/**
* This value was not successfully translated. No value can be obtained with {@link #getText()}.
*/
public static final int STATUS_ERROR = 1;
/**
* Name in the result of {@link #getExtras()} to pass dictionary definitions of the text
* categorized by parts of speech.
*
* <p>The dictionary definitions consists of groups of terms keyed by their corresponding parts
* of speech. This map-like structure is stored in a {@link Bundle}. The individual parts of
* speech can be traversed by {@link Bundle#keySet()} and used to get the corresponding list
* of terms as {@link CharSequence}s.
*
* <ul>
* <li>"noun" -> ["def1", "def2", ...]</li>
* <li>"verb" -> ["def3", "def4", ...]</li>
* <li>...</li>
* </ul>
*
* The set of parts of speech can then be used by
* {@link Bundle#getCharSequenceArrayList(String)} to get the list of terms.
*
* <b>Example</b>:
*
* {@code for (String partOfSpeech : extras.getBundle(EXTRA_DEFINITIONS).keySet()) {
* ArrayList<CharSequence> terms =
* extras.getBundle(EXTRA_DEFINITIONS).getCharSequenceArrayList(partOfSpeech);
* ...
* }}</p>
*/
public static final String EXTRA_DEFINITIONS = "android.view.translation.extra.DEFINITIONS";
/**
* The status code of this {@link TranslationResponseValue}.
*
* <p>If the status code is {@link #STATUS_ERROR}, no values are attached, and all getters will
* return {@code null}.
*/
private final @Status int mStatusCode;
/**
* The translated text result.
*/
@Nullable
private final CharSequence mText;
/**
* Extra results associated with the translated text.
*
* <p>The bundle includes {@link #EXTRA_DEFINITIONS}, obtained by {@link Bundle#getBundle}.
* </p>
*/
@NonNull
private final Bundle mExtras;
// TODO: Add example of transliteration.
/**
* The transliteration result of the translated text.
*
* <p>This returns a CharSequence representation of the transliteration of the translated text.
*/
@Nullable
private final CharSequence mTransliteration;
/**
* Creates a {@link TranslationResponseValue} with the {@link #STATUS_ERROR} result;
*/
@NonNull
public static TranslationResponseValue forError() {
return new TranslationResponseValue(STATUS_ERROR, null, Bundle.EMPTY, null);
}
private static CharSequence defaultText() {
return null;
}
private static Bundle defaultExtras() {
return Bundle.EMPTY;
}
private boolean extrasEquals(Bundle other) {
// TODO: Also compare the contents.
return Objects.equals(mExtras, other) || (mExtras.isEmpty() && other.isEmpty());
}
private static CharSequence defaultTransliteration() {
return null;
}
@DataClass.Suppress("setStatusCode")
abstract static class BaseBuilder {
}
// Code below generated by codegen v1.0.23.
//
// DO NOT MODIFY!
// CHECKSTYLE:OFF Generated code
//
// To regenerate run:
// $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/view/translation/TranslationResponseValue.java
//
// To exclude the generated code from IntelliJ auto-formatting enable (one-time):
// Settings > Editor > Code Style > Formatter Control
//@formatter:off
/** @hide */
@android.annotation.IntDef(prefix = "STATUS_", value = {
STATUS_SUCCESS,
STATUS_ERROR
})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE)
@DataClass.Generated.Member
public @interface Status {}
/** @hide */
@DataClass.Generated.Member
public static String statusToString(@Status int value) {
switch (value) {
case STATUS_SUCCESS:
return "STATUS_SUCCESS";
case STATUS_ERROR:
return "STATUS_ERROR";
default: return Integer.toHexString(value);
}
}
@DataClass.Generated.Member
/* package-private */ TranslationResponseValue(
@Status int statusCode,
@Nullable CharSequence text,
@NonNull Bundle extras,
@Nullable CharSequence transliteration) {
this.mStatusCode = statusCode;
if (!(mStatusCode == STATUS_SUCCESS)
&& !(mStatusCode == STATUS_ERROR)) {
throw new java.lang.IllegalArgumentException(
"statusCode was " + mStatusCode + " but must be one of: "
+ "STATUS_SUCCESS(" + STATUS_SUCCESS + "), "
+ "STATUS_ERROR(" + STATUS_ERROR + ")");
}
this.mText = text;
this.mExtras = extras;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mExtras);
this.mTransliteration = transliteration;
// onConstructed(); // You can define this method to get a callback
}
/**
* The status code of this {@link TranslationResponseValue}.
*
* <p>If the status code is {@link #STATUS_ERROR}, no values are attached, and all getters will
* return {@code null}.
*/
@DataClass.Generated.Member
public @Status int getStatusCode() {
return mStatusCode;
}
/**
* The translated text result.
*/
@DataClass.Generated.Member
public @Nullable CharSequence getText() {
return mText;
}
/**
* Extra results associated with the translated text.
*
* <p>The bundle includes {@link #EXTRA_DEFINITIONS}, obtained by {@link Bundle#getBundle}.
* </p>
*/
@DataClass.Generated.Member
public @NonNull Bundle getExtras() {
return mExtras;
}
/**
* The transliteration result of the translated text.
*
* <p>This returns a CharSequence representation of the transliteration of the translated text.
*/
@DataClass.Generated.Member
public @Nullable CharSequence getTransliteration() {
return mTransliteration;
}
@Override
@DataClass.Generated.Member
public String toString() {
// You can override field toString logic by defining methods like:
// String fieldNameToString() { ... }
return "TranslationResponseValue { " +
"statusCode = " + statusToString(mStatusCode) + ", " +
"text = " + mText + ", " +
"extras = " + mExtras + ", " +
"transliteration = " + mTransliteration +
" }";
}
@Override
@DataClass.Generated.Member
public boolean equals(@Nullable Object o) {
// You can override field equality logic by defining either of the methods like:
// boolean fieldNameEquals(TranslationResponseValue other) { ... }
// boolean fieldNameEquals(FieldType otherValue) { ... }
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
@SuppressWarnings("unchecked")
TranslationResponseValue that = (TranslationResponseValue) o;
//noinspection PointlessBooleanExpression
return true
&& mStatusCode == that.mStatusCode
&& Objects.equals(mText, that.mText)
&& extrasEquals(that.mExtras)
&& Objects.equals(mTransliteration, that.mTransliteration);
}
@Override
@DataClass.Generated.Member
public int hashCode() {
// You can override field hashCode logic by defining methods like:
// int fieldNameHashCode() { ... }
int _hash = 1;
_hash = 31 * _hash + mStatusCode;
_hash = 31 * _hash + Objects.hashCode(mText);
_hash = 31 * _hash + Objects.hashCode(mExtras);
_hash = 31 * _hash + Objects.hashCode(mTransliteration);
return _hash;
}
@Override
@DataClass.Generated.Member
public void writeToParcel(@NonNull Parcel dest, int flags) {
// You can override field parcelling by defining methods like:
// void parcelFieldName(Parcel dest, int flags) { ... }
byte flg = 0;
if (mText != null) flg |= 0x2;
if (mTransliteration != null) flg |= 0x8;
dest.writeByte(flg);
dest.writeInt(mStatusCode);
if (mText != null) dest.writeCharSequence(mText);
dest.writeBundle(mExtras);
if (mTransliteration != null) dest.writeCharSequence(mTransliteration);
}
@Override
@DataClass.Generated.Member
public int describeContents() { return 0; }
/** @hide */
@SuppressWarnings({"unchecked", "RedundantCast"})
@DataClass.Generated.Member
/* package-private */ TranslationResponseValue(@NonNull Parcel in) {
// You can override field unparcelling by defining methods like:
// static FieldType unparcelFieldName(Parcel in) { ... }
byte flg = in.readByte();
int statusCode = in.readInt();
CharSequence text = (flg & 0x2) == 0 ? null : (CharSequence) in.readCharSequence();
Bundle extras = in.readBundle();
CharSequence transliteration = (flg & 0x8) == 0 ? null : (CharSequence) in.readCharSequence();
this.mStatusCode = statusCode;
if (!(mStatusCode == STATUS_SUCCESS)
&& !(mStatusCode == STATUS_ERROR)) {
throw new java.lang.IllegalArgumentException(
"statusCode was " + mStatusCode + " but must be one of: "
+ "STATUS_SUCCESS(" + STATUS_SUCCESS + "), "
+ "STATUS_ERROR(" + STATUS_ERROR + ")");
}
this.mText = text;
this.mExtras = extras;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mExtras);
this.mTransliteration = transliteration;
// onConstructed(); // You can define this method to get a callback
}
@DataClass.Generated.Member
public static final @NonNull Parcelable.Creator<TranslationResponseValue> CREATOR
= new Parcelable.Creator<TranslationResponseValue>() {
@Override
public TranslationResponseValue[] newArray(int size) {
return new TranslationResponseValue[size];
}
@Override
public TranslationResponseValue createFromParcel(@NonNull Parcel in) {
return new TranslationResponseValue(in);
}
};
/**
* A builder for {@link TranslationResponseValue}
*/
@SuppressWarnings("WeakerAccess")
@DataClass.Generated.Member
public static final class Builder extends BaseBuilder {
private @Status int mStatusCode;
private @Nullable CharSequence mText;
private @NonNull Bundle mExtras;
private @Nullable CharSequence mTransliteration;
private long mBuilderFieldsSet = 0L;
/**
* Creates a new Builder.
*
* @param statusCode
* The status code of this {@link TranslationResponseValue}.
*
* <p>If the status code is {@link #STATUS_ERROR}, no values are attached, and all getters will
* return {@code null}.
*/
public Builder(
@Status int statusCode) {
mStatusCode = statusCode;
if (!(mStatusCode == STATUS_SUCCESS)
&& !(mStatusCode == STATUS_ERROR)) {
throw new java.lang.IllegalArgumentException(
"statusCode was " + mStatusCode + " but must be one of: "
+ "STATUS_SUCCESS(" + STATUS_SUCCESS + "), "
+ "STATUS_ERROR(" + STATUS_ERROR + ")");
}
}
/**
* The translated text result.
*/
@DataClass.Generated.Member
public @NonNull Builder setText(@NonNull CharSequence value) {
checkNotUsed();
mBuilderFieldsSet |= 0x2;
mText = value;
return this;
}
/**
* Extra results associated with the translated text.
*
* <p>The bundle includes {@link #EXTRA_DEFINITIONS}, obtained by {@link Bundle#getBundle}.
* </p>
*/
@DataClass.Generated.Member
public @NonNull Builder setExtras(@NonNull Bundle value) {
checkNotUsed();
mBuilderFieldsSet |= 0x4;
mExtras = value;
return this;
}
/**
* The transliteration result of the translated text.
*
* <p>This returns a CharSequence representation of the transliteration of the translated text.
*/
@DataClass.Generated.Member
public @NonNull Builder setTransliteration(@NonNull CharSequence value) {
checkNotUsed();
mBuilderFieldsSet |= 0x8;
mTransliteration = value;
return this;
}
/** Builds the instance. This builder should not be touched after calling this! */
public @NonNull TranslationResponseValue build() {
checkNotUsed();
mBuilderFieldsSet |= 0x10; // Mark builder used
if ((mBuilderFieldsSet & 0x2) == 0) {
mText = defaultText();
}
if ((mBuilderFieldsSet & 0x4) == 0) {
mExtras = defaultExtras();
}
if ((mBuilderFieldsSet & 0x8) == 0) {
mTransliteration = defaultTransliteration();
}
TranslationResponseValue o = new TranslationResponseValue(
mStatusCode,
mText,
mExtras,
mTransliteration);
return o;
}
private void checkNotUsed() {
if ((mBuilderFieldsSet & 0x10) != 0) {
throw new IllegalStateException(
"This Builder should not be reused. Use a new Builder instance instead");
}
}
}
@DataClass.Generated(
time = 1631057245846L,
codegenVersion = "1.0.23",
sourceFile = "frameworks/base/core/java/android/view/translation/TranslationResponseValue.java",
inputSignatures = "public static final int STATUS_SUCCESS\npublic static final int STATUS_ERROR\npublic static final java.lang.String EXTRA_DEFINITIONS\nprivate final @android.view.translation.TranslationResponseValue.Status int mStatusCode\nprivate final @android.annotation.Nullable java.lang.CharSequence mText\nprivate final @android.annotation.NonNull android.os.Bundle mExtras\nprivate final @android.annotation.Nullable java.lang.CharSequence mTransliteration\npublic static @android.annotation.NonNull android.view.translation.TranslationResponseValue forError()\nprivate static java.lang.CharSequence defaultText()\nprivate static android.os.Bundle defaultExtras()\nprivate boolean extrasEquals(android.os.Bundle)\nprivate static java.lang.CharSequence defaultTransliteration()\nclass TranslationResponseValue extends java.lang.Object implements [android.os.Parcelable]\nclass BaseBuilder extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genBuilder=true, genToString=true, genEqualsHashCode=true, genHiddenConstDefs=true)\nclass BaseBuilder extends java.lang.Object implements []")
@Deprecated
private void __metadata() {}
//@formatter:on
// End of generated code
}
|