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.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.os.Parcelable;
23 
24 import com.android.internal.util.DataClass;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 import java.util.Objects;
29 import java.util.concurrent.Executor;
30 import java.util.function.Consumer;
31 
32 /**
33  * Capability class holding information for a pair of {@link TranslationSpec}s.
34  *
35  * <p>Holds information and limitations on how to create a {@link TranslationContext} which can
36  * be used by
37  * {@link TranslationManager#createOnDeviceTranslator(TranslationContext, Executor, Consumer)}.
38  */
39 @DataClass(genHiddenConstDefs = true, genToString = true, genConstructor = false)
40 public final class TranslationCapability implements Parcelable {
41 
42     /**
43      * The translation service supports translation between the source and target specs, and it is
44      * ready to be downloaded onto the device.
45      */
46     public static final @ModelState int STATE_AVAILABLE_TO_DOWNLOAD = 1;
47     /**
48      * The translation service supports translation between the source and target specs, and it is
49      * being downloaded onto the device currently.
50      */
51     public static final @ModelState int STATE_DOWNLOADING = 2;
52     /**
53      * The translation service supports translation between the source and target specs, and it is
54      * downloaded and ready to use on device.
55      */
56     public static final @ModelState int STATE_ON_DEVICE = 3;
57     /**
58      * The translation service does not support translation between the source and target specs.
59      *
60      * <p>Note: This state is not returned from calling
61      * {@link TranslationManager#getOnDeviceTranslationCapabilities}. This state will only appear as
62      * part of capability updates from
63      * {@link TranslationManager#addOnDeviceTranslationCapabilityUpdateListener} if existing support
64      * was dropped.</p>
65      */
66     public static final @ModelState int STATE_NOT_AVAILABLE = 4;
67     /**
68      * The translation between the source and target specs were removed from the system, but is
69      * still available to be downloaded again.
70      *
71      * @hide
72      */
73     public static final @ModelState int STATE_REMOVED_AND_AVAILABLE = 1000;
74 
75     /**
76      * The state of translation readiness between {@code mSourceSpec} and {@code mTargetSpec}.
77      */
78     private final @ModelState int mState;
79 
80     /**
81      * {@link TranslationSpec} describing the source data specs for this
82      * capability.
83      */
84     @NonNull
85     private final TranslationSpec mSourceSpec;
86 
87     /**
88      * {@link TranslationSpec} describing the target data specs for this
89      * capability.
90      */
91     @NonNull
92     private final TranslationSpec mTargetSpec;
93 
94     /**
95      * Whether ui translation for the source-target {@link TranslationSpec}s is enabled.
96      *
97      * <p>Translation service will still support translation requests for this capability.</p>
98      */
99     private final boolean mUiTranslationEnabled;
100 
101     /**
102      * Translation flags for settings that are supported by the
103      * {@link android.service.translation.TranslationService} between the {@link TranslationSpec}s
104      * provided in this capability.
105      */
106     private final @TranslationContext.TranslationFlag int mSupportedTranslationFlags;
107 
108     /**
109      * Constructor for creating a {@link TranslationCapability}.
110      *
111      * @hide
112      */
113     @SystemApi
TranslationCapability(@odelState int state, @NonNull TranslationSpec sourceSpec, @NonNull TranslationSpec targetSpec, boolean uiTranslationEnabled, @TranslationContext.TranslationFlag int supportedTranslationFlags)114     public TranslationCapability(@ModelState int state, @NonNull TranslationSpec sourceSpec,
115             @NonNull TranslationSpec targetSpec, boolean uiTranslationEnabled,
116             @TranslationContext.TranslationFlag int supportedTranslationFlags) {
117         Objects.requireNonNull(sourceSpec, "sourceSpec should not be null");
118         Objects.requireNonNull(targetSpec, "targetSpec should not be null");
119 
120         this.mState = state;
121         this.mSourceSpec = sourceSpec;
122         this.mTargetSpec = targetSpec;
123         this.mUiTranslationEnabled = uiTranslationEnabled;
124         this.mSupportedTranslationFlags = supportedTranslationFlags;
125     }
126 
127 
128 
129     // Code below generated by codegen v1.0.23.
130     //
131     // DO NOT MODIFY!
132     // CHECKSTYLE:OFF Generated code
133     //
134     // To regenerate run:
135     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/view/translation/TranslationCapability.java
136     //
137     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
138     //   Settings > Editor > Code Style > Formatter Control
139     //@formatter:off
140 
141 
142     /** @hide */
143     @IntDef(prefix = "STATE_", value = {
144         STATE_AVAILABLE_TO_DOWNLOAD,
145         STATE_DOWNLOADING,
146         STATE_ON_DEVICE,
147         STATE_NOT_AVAILABLE,
148         STATE_REMOVED_AND_AVAILABLE
149     })
150     @Retention(RetentionPolicy.SOURCE)
151     @DataClass.Generated.Member
152     public @interface ModelState {}
153 
154     /** @hide */
155     @DataClass.Generated.Member
modelStateToString(@odelState int value)156     public static String modelStateToString(@ModelState int value) {
157         switch (value) {
158             case STATE_AVAILABLE_TO_DOWNLOAD:
159                     return "STATE_AVAILABLE_TO_DOWNLOAD";
160             case STATE_DOWNLOADING:
161                     return "STATE_DOWNLOADING";
162             case STATE_ON_DEVICE:
163                     return "STATE_ON_DEVICE";
164             case STATE_NOT_AVAILABLE:
165                     return "STATE_NOT_AVAILABLE";
166             case STATE_REMOVED_AND_AVAILABLE:
167                     return "STATE_REMOVED_AND_AVAILABLE";
168             default: return Integer.toHexString(value);
169         }
170     }
171 
172     /**
173      * The state of translation readiness between {@code mSourceSpec} and {@code mTargetSpec}.
174      */
175     @DataClass.Generated.Member
getState()176     public @ModelState int getState() {
177         return mState;
178     }
179 
180     /**
181      * {@link TranslationSpec} describing the source data specs for this
182      * capability.
183      */
184     @DataClass.Generated.Member
getSourceSpec()185     public @NonNull TranslationSpec getSourceSpec() {
186         return mSourceSpec;
187     }
188 
189     /**
190      * {@link TranslationSpec} describing the target data specs for this
191      * capability.
192      */
193     @DataClass.Generated.Member
getTargetSpec()194     public @NonNull TranslationSpec getTargetSpec() {
195         return mTargetSpec;
196     }
197 
198     /**
199      * Whether ui translation for the source-target {@link TranslationSpec}s is enabled.
200      *
201      * <p>Translation service will still support translation requests for this capability.</p>
202      */
203     @DataClass.Generated.Member
isUiTranslationEnabled()204     public boolean isUiTranslationEnabled() {
205         return mUiTranslationEnabled;
206     }
207 
208     /**
209      * Translation flags for settings that are supported by the
210      * {@link android.service.translation.TranslationService} between the {@link TranslationSpec}s
211      * provided in this capability.
212      */
213     @DataClass.Generated.Member
getSupportedTranslationFlags()214     public @TranslationContext.TranslationFlag int getSupportedTranslationFlags() {
215         return mSupportedTranslationFlags;
216     }
217 
218     @Override
219     @DataClass.Generated.Member
toString()220     public String toString() {
221         // You can override field toString logic by defining methods like:
222         // String fieldNameToString() { ... }
223 
224         return "TranslationCapability { " +
225                 "state = " + modelStateToString(mState) + ", " +
226                 "sourceSpec = " + mSourceSpec + ", " +
227                 "targetSpec = " + mTargetSpec + ", " +
228                 "uiTranslationEnabled = " + mUiTranslationEnabled + ", " +
229                 "supportedTranslationFlags = " + mSupportedTranslationFlags +
230         " }";
231     }
232 
233     @Override
234     @DataClass.Generated.Member
writeToParcel(@onNull android.os.Parcel dest, int flags)235     public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
236         // You can override field parcelling by defining methods like:
237         // void parcelFieldName(Parcel dest, int flags) { ... }
238 
239         byte flg = 0;
240         if (mUiTranslationEnabled) flg |= 0x8;
241         dest.writeByte(flg);
242         dest.writeInt(mState);
243         dest.writeTypedObject(mSourceSpec, flags);
244         dest.writeTypedObject(mTargetSpec, flags);
245         dest.writeInt(mSupportedTranslationFlags);
246     }
247 
248     @Override
249     @DataClass.Generated.Member
describeContents()250     public int describeContents() { return 0; }
251 
252     /** @hide */
253     @SuppressWarnings({"unchecked", "RedundantCast"})
254     @DataClass.Generated.Member
TranslationCapability(@onNull android.os.Parcel in)255     /* package-private */ TranslationCapability(@NonNull android.os.Parcel in) {
256         // You can override field unparcelling by defining methods like:
257         // static FieldType unparcelFieldName(Parcel in) { ... }
258 
259         byte flg = in.readByte();
260         boolean uiTranslationEnabled = (flg & 0x8) != 0;
261         int state = in.readInt();
262         TranslationSpec sourceSpec = (TranslationSpec) in.readTypedObject(TranslationSpec.CREATOR);
263         TranslationSpec targetSpec = (TranslationSpec) in.readTypedObject(TranslationSpec.CREATOR);
264         int supportedTranslationFlags = in.readInt();
265 
266         this.mState = state;
267 
268         if (!(mState == STATE_AVAILABLE_TO_DOWNLOAD)
269                 && !(mState == STATE_DOWNLOADING)
270                 && !(mState == STATE_ON_DEVICE)
271                 && !(mState == STATE_NOT_AVAILABLE)
272                 && !(mState == STATE_REMOVED_AND_AVAILABLE)) {
273             throw new java.lang.IllegalArgumentException(
274                     "state was " + mState + " but must be one of: "
275                             + "STATE_AVAILABLE_TO_DOWNLOAD(" + STATE_AVAILABLE_TO_DOWNLOAD + "), "
276                             + "STATE_DOWNLOADING(" + STATE_DOWNLOADING + "), "
277                             + "STATE_ON_DEVICE(" + STATE_ON_DEVICE + "), "
278                             + "STATE_NOT_AVAILABLE(" + STATE_NOT_AVAILABLE + "), "
279                             + "STATE_REMOVED_AND_AVAILABLE(" + STATE_REMOVED_AND_AVAILABLE + ")");
280         }
281 
282         this.mSourceSpec = sourceSpec;
283         com.android.internal.util.AnnotationValidations.validate(
284                 NonNull.class, null, mSourceSpec);
285         this.mTargetSpec = targetSpec;
286         com.android.internal.util.AnnotationValidations.validate(
287                 NonNull.class, null, mTargetSpec);
288         this.mUiTranslationEnabled = uiTranslationEnabled;
289         this.mSupportedTranslationFlags = supportedTranslationFlags;
290         com.android.internal.util.AnnotationValidations.validate(
291                 TranslationContext.TranslationFlag.class, null, mSupportedTranslationFlags);
292 
293         // onConstructed(); // You can define this method to get a callback
294     }
295 
296     @DataClass.Generated.Member
297     public static final @NonNull Parcelable.Creator<TranslationCapability> CREATOR
298             = new Parcelable.Creator<TranslationCapability>() {
299         @Override
300         public TranslationCapability[] newArray(int size) {
301             return new TranslationCapability[size];
302         }
303 
304         @Override
305         public TranslationCapability createFromParcel(@NonNull android.os.Parcel in) {
306             return new TranslationCapability(in);
307         }
308     };
309 
310     @DataClass.Generated(
311             time = 1629158466039L,
312             codegenVersion = "1.0.23",
313             sourceFile = "frameworks/base/core/java/android/view/translation/TranslationCapability.java",
314             inputSignatures = "public static final @android.view.translation.TranslationCapability.ModelState int STATE_AVAILABLE_TO_DOWNLOAD\npublic static final @android.view.translation.TranslationCapability.ModelState int STATE_DOWNLOADING\npublic static final @android.view.translation.TranslationCapability.ModelState int STATE_ON_DEVICE\npublic static final @android.view.translation.TranslationCapability.ModelState int STATE_NOT_AVAILABLE\npublic static final @android.view.translation.TranslationCapability.ModelState int STATE_REMOVED_AND_AVAILABLE\nprivate final @android.view.translation.TranslationCapability.ModelState int mState\nprivate final @android.annotation.NonNull android.view.translation.TranslationSpec mSourceSpec\nprivate final @android.annotation.NonNull android.view.translation.TranslationSpec mTargetSpec\nprivate final  boolean mUiTranslationEnabled\nprivate final @android.view.translation.TranslationContext.TranslationFlag int mSupportedTranslationFlags\nclass TranslationCapability extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstDefs=true, genToString=true, genConstructor=false)")
315     @Deprecated
__metadata()316     private void __metadata() {}
317 
318 
319     //@formatter:on
320     // End of generated code
321 
322 }
323