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
|
/*
* 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.annotation.SystemApi;
import android.app.assist.ActivityId;
import android.os.Parcel;
import android.os.Parcelable;
import com.android.internal.util.DataClass;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
/**
* Info class holding information for {@link Translator}s and used by
* {@link TranslationManager#createOnDeviceTranslator(TranslationContext, Executor, Consumer)}.
*/
@DataClass(genHiddenConstDefs = true, genToString = true, genBuilder = true)
public final class TranslationContext implements Parcelable {
/**
* This context will perform translations in low latency mode.
*/
public static final @TranslationFlag int FLAG_LOW_LATENCY = 0x1;
/**
* This context will enable the {@link Translator} to return transliteration results.
*/
public static final @TranslationFlag int FLAG_TRANSLITERATION = 0x2;
/**
* This context will enable the {@link Translator} to return dictionary definitions.
*/
public static final @TranslationFlag int FLAG_DEFINITIONS = 0x4;
/**
* {@link TranslationSpec} describing the source data to be translated.
*/
@NonNull
private final TranslationSpec mSourceSpec;
/**
* {@link TranslationSpec} describing the target translated data.
*/
@NonNull
private final TranslationSpec mTargetSpec;
/**
* Translation flags to be used by the {@link Translator}
*/
private final @TranslationFlag int mTranslationFlags;
private static int defaultTranslationFlags() {
return 0;
}
/**
* The identifier for the Activity which needs UI translation.
*
* @hide
*/
@Nullable
private final ActivityId mActivityId;
private static ActivityId defaultActivityId() {
return null;
}
private void parcelActivityId(@NonNull Parcel dest, int flags) {
dest.writeBoolean(mActivityId != null);
if (mActivityId != null) {
mActivityId.writeToParcel(dest, flags);
}
}
@Nullable
private ActivityId unparcelActivityId(@NonNull Parcel in) {
final boolean hasActivityId = in.readBoolean();
return hasActivityId ? new ActivityId(in) : null;
}
/**
* Returns the identifier for the Activity which needs UI translation or {@code null}
* if it is a non-UI translation request.
*
* NOTE: If the application receiving this ActivityId also provides a ContentCaptureService, it
* can be used to associate a TranslationRequest with a particular ContentCaptureSession.
*
* @hide
*/
@SystemApi
@Nullable
public ActivityId getActivityId() {
return mActivityId;
}
@DataClass.Suppress({"setSourceSpec", "setTargetSpec"})
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/TranslationContext.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(flag = true, prefix = "FLAG_", value = {
FLAG_LOW_LATENCY,
FLAG_TRANSLITERATION,
FLAG_DEFINITIONS
})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE)
@DataClass.Generated.Member
public @interface TranslationFlag {}
/** @hide */
@DataClass.Generated.Member
public static String translationFlagToString(@TranslationFlag int value) {
return com.android.internal.util.BitUtils.flagsToString(
value, TranslationContext::singleTranslationFlagToString);
}
@DataClass.Generated.Member
static String singleTranslationFlagToString(@TranslationFlag int value) {
switch (value) {
case FLAG_LOW_LATENCY:
return "FLAG_LOW_LATENCY";
case FLAG_TRANSLITERATION:
return "FLAG_TRANSLITERATION";
case FLAG_DEFINITIONS:
return "FLAG_DEFINITIONS";
default: return Integer.toHexString(value);
}
}
@DataClass.Generated.Member
/* package-private */ TranslationContext(
@NonNull TranslationSpec sourceSpec,
@NonNull TranslationSpec targetSpec,
@TranslationFlag int translationFlags,
@Nullable ActivityId activityId) {
this.mSourceSpec = sourceSpec;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mSourceSpec);
this.mTargetSpec = targetSpec;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mTargetSpec);
this.mTranslationFlags = translationFlags;
com.android.internal.util.Preconditions.checkFlagsArgument(
mTranslationFlags,
FLAG_LOW_LATENCY
| FLAG_TRANSLITERATION
| FLAG_DEFINITIONS);
this.mActivityId = activityId;
// onConstructed(); // You can define this method to get a callback
}
/**
* {@link TranslationSpec} describing the source data to be translated.
*/
@DataClass.Generated.Member
public @NonNull TranslationSpec getSourceSpec() {
return mSourceSpec;
}
/**
* {@link TranslationSpec} describing the target translated data.
*/
@DataClass.Generated.Member
public @NonNull TranslationSpec getTargetSpec() {
return mTargetSpec;
}
/**
* Translation flags to be used by the {@link Translator}
*/
@DataClass.Generated.Member
public @TranslationFlag int getTranslationFlags() {
return mTranslationFlags;
}
@Override
@DataClass.Generated.Member
public String toString() {
// You can override field toString logic by defining methods like:
// String fieldNameToString() { ... }
return "TranslationContext { " +
"sourceSpec = " + mSourceSpec + ", " +
"targetSpec = " + mTargetSpec + ", " +
"translationFlags = " + translationFlagToString(mTranslationFlags) + ", " +
"activityId = " + mActivityId +
" }";
}
@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 (mActivityId != null) flg |= 0x8;
dest.writeByte(flg);
dest.writeTypedObject(mSourceSpec, flags);
dest.writeTypedObject(mTargetSpec, flags);
dest.writeInt(mTranslationFlags);
parcelActivityId(dest, flags);
}
@Override
@DataClass.Generated.Member
public int describeContents() { return 0; }
/** @hide */
@SuppressWarnings({"unchecked", "RedundantCast"})
@DataClass.Generated.Member
/* package-private */ TranslationContext(@NonNull Parcel in) {
// You can override field unparcelling by defining methods like:
// static FieldType unparcelFieldName(Parcel in) { ... }
byte flg = in.readByte();
TranslationSpec sourceSpec = (TranslationSpec) in.readTypedObject(TranslationSpec.CREATOR);
TranslationSpec targetSpec = (TranslationSpec) in.readTypedObject(TranslationSpec.CREATOR);
int translationFlags = in.readInt();
ActivityId activityId = unparcelActivityId(in);
this.mSourceSpec = sourceSpec;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mSourceSpec);
this.mTargetSpec = targetSpec;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mTargetSpec);
this.mTranslationFlags = translationFlags;
com.android.internal.util.Preconditions.checkFlagsArgument(
mTranslationFlags,
FLAG_LOW_LATENCY
| FLAG_TRANSLITERATION
| FLAG_DEFINITIONS);
this.mActivityId = activityId;
// onConstructed(); // You can define this method to get a callback
}
@DataClass.Generated.Member
public static final @NonNull Parcelable.Creator<TranslationContext> CREATOR
= new Parcelable.Creator<TranslationContext>() {
@Override
public TranslationContext[] newArray(int size) {
return new TranslationContext[size];
}
@Override
public TranslationContext createFromParcel(@NonNull Parcel in) {
return new TranslationContext(in);
}
};
/**
* A builder for {@link TranslationContext}
*/
@SuppressWarnings("WeakerAccess")
@DataClass.Generated.Member
public static final class Builder extends BaseBuilder {
private @NonNull TranslationSpec mSourceSpec;
private @NonNull TranslationSpec mTargetSpec;
private @TranslationFlag int mTranslationFlags;
private @Nullable ActivityId mActivityId;
private long mBuilderFieldsSet = 0L;
/**
* Creates a new Builder.
*
* @param sourceSpec
* {@link TranslationSpec} describing the source data to be translated.
* @param targetSpec
* {@link TranslationSpec} describing the target translated data.
*/
public Builder(
@NonNull TranslationSpec sourceSpec,
@NonNull TranslationSpec targetSpec) {
mSourceSpec = sourceSpec;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mSourceSpec);
mTargetSpec = targetSpec;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mTargetSpec);
}
/**
* Translation flags to be used by the {@link Translator}
*/
@DataClass.Generated.Member
public @NonNull Builder setTranslationFlags(@TranslationFlag int value) {
checkNotUsed();
mBuilderFieldsSet |= 0x4;
mTranslationFlags = value;
return this;
}
/**
* The identifier for the Activity which needs UI translation.
*
* @hide
*/
@DataClass.Generated.Member
public @NonNull Builder setActivityId(@NonNull ActivityId value) {
checkNotUsed();
mBuilderFieldsSet |= 0x8;
mActivityId = value;
return this;
}
/** Builds the instance. This builder should not be touched after calling this! */
public @NonNull TranslationContext build() {
checkNotUsed();
mBuilderFieldsSet |= 0x10; // Mark builder used
if ((mBuilderFieldsSet & 0x4) == 0) {
mTranslationFlags = defaultTranslationFlags();
}
if ((mBuilderFieldsSet & 0x8) == 0) {
mActivityId = defaultActivityId();
}
TranslationContext o = new TranslationContext(
mSourceSpec,
mTargetSpec,
mTranslationFlags,
mActivityId);
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 = 1638348645427L,
codegenVersion = "1.0.23",
sourceFile = "frameworks/base/core/java/android/view/translation/TranslationContext.java",
inputSignatures = "public static final @android.view.translation.TranslationContext.TranslationFlag int FLAG_LOW_LATENCY\npublic static final @android.view.translation.TranslationContext.TranslationFlag int FLAG_TRANSLITERATION\npublic static final @android.view.translation.TranslationContext.TranslationFlag int FLAG_DEFINITIONS\nprivate final @android.annotation.NonNull android.view.translation.TranslationSpec mSourceSpec\nprivate final @android.annotation.NonNull android.view.translation.TranslationSpec mTargetSpec\nprivate final @android.view.translation.TranslationContext.TranslationFlag int mTranslationFlags\nprivate final @android.annotation.Nullable android.app.assist.ActivityId mActivityId\nprivate static int defaultTranslationFlags()\nprivate static android.app.assist.ActivityId defaultActivityId()\nprivate void parcelActivityId(android.os.Parcel,int)\nprivate @android.annotation.Nullable android.app.assist.ActivityId unparcelActivityId(android.os.Parcel)\npublic @android.annotation.SystemApi @android.annotation.Nullable android.app.assist.ActivityId getActivityId()\nclass TranslationContext extends java.lang.Object implements [android.os.Parcelable]\nclass BaseBuilder extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genHiddenConstDefs=true, genToString=true, genBuilder=true)\nclass BaseBuilder extends java.lang.Object implements []")
@Deprecated
private void __metadata() {}
//@formatter:on
// End of generated code
}
|