API feedback: change language from String to ULocale.

Bug: 176208267
Bug: 184685163
Test: atest CtsTranslationTestCases
Change-Id: I10310f4980d4b9d869eb76542e0327420ad2f9de
This commit is contained in:
Adam He
2021-04-19 17:35:47 -07:00
parent 9753f5c383
commit e565d31bce
5 changed files with 86 additions and 34 deletions

View File

@@ -52882,10 +52882,12 @@ package android.view.translation {
}
public final class TranslationSpec implements android.os.Parcelable {
ctor public TranslationSpec(@NonNull String, int);
ctor @Deprecated public TranslationSpec(@NonNull String, int);
ctor public TranslationSpec(@NonNull android.icu.util.ULocale, int);
method public int describeContents();
method public int getDataFormat();
method @NonNull public String getLanguage();
method @Deprecated @NonNull public String getLanguage();
method @NonNull public android.icu.util.ULocale getLocale();
method public void writeToParcel(@NonNull android.os.Parcel, int);
field @NonNull public static final android.os.Parcelable.Creator<android.view.translation.TranslationSpec> CREATOR;
field public static final int DATA_FORMAT_TEXT = 1; // 0x1
@@ -52905,7 +52907,8 @@ package android.view.translation {
public interface UiTranslationStateCallback {
method public void onFinished();
method public void onPaused();
method public void onStarted(@NonNull String, @NonNull String);
method @Deprecated public void onStarted(@NonNull String, @NonNull String);
method public default void onStarted(@NonNull android.icu.util.ULocale, @NonNull android.icu.util.ULocale);
}
@UiThread public interface ViewTranslationCallback {

View File

@@ -17,18 +17,22 @@
package android.view.translation;
import android.annotation.NonNull;
import android.icu.util.ULocale;
import android.os.Parcel;
import android.os.Parcelable;
import com.android.internal.util.DataClass;
import java.util.Objects;
/**
* Specs and additional info for the translation data.
*
* <p>This spec help specify information such as the language/locale for the translation, as well
* as the data format for the translation (text, audio, etc.)</p>
*/
@DataClass(genEqualsHashCode = true, genHiddenConstDefs = true, genToString = true)
@DataClass(genEqualsHashCode = true, genHiddenConstDefs = true, genToString = true,
genConstructor = false)
public final class TranslationSpec implements Parcelable {
/** Data format for translation is text. */
@@ -43,15 +47,52 @@ public final class TranslationSpec implements Parcelable {
public @interface DataFormat {}
/**
* String representation of language codes e.g. "en", "es", etc.
* @deprecated use {@code mLocale} instead.
*/
@Deprecated
private final @NonNull String mLanguage;
/**
* {@link ULocale} representing locale information of this spec.
*/
private final @NonNull ULocale mLocale;
private final @DataFormat int mDataFormat;
void parcelLocale(Parcel dest, int flags) {
dest.writeSerializable(mLocale);
}
static ULocale unparcelLocale(Parcel in) {
return (ULocale) in.readSerializable();
}
/**
* @deprecated use {@link #TranslationSpec(ULocale, int)} instead.
*/
@Deprecated
public TranslationSpec(@NonNull String language, @DataFormat int dataFormat) {
mLanguage = language;
mDataFormat = dataFormat;
mLocale = new ULocale.Builder().setLanguage(language).build();
}
/**
* Constructs a translation spec with the given locale and data format.
*
* @param locale locale of the associated translation data.
* @param dataFormat data format of the associated translation data.
*/
public TranslationSpec(@NonNull ULocale locale, @DataFormat int dataFormat) {
Objects.requireNonNull(locale);
mLanguage = locale.getLanguage();
mLocale = locale;
mDataFormat = dataFormat;
}
// Code below generated by codegen v1.0.22.
// Code below generated by codegen v1.0.23.
//
// DO NOT MODIFY!
// CHECKSTYLE:OFF Generated code
@@ -65,31 +106,19 @@ public final class TranslationSpec implements Parcelable {
/**
* Creates a new TranslationSpec.
*
* @param language
* String representation of language codes e.g. "en", "es", etc.
* @deprecated use {@code mLocale} instead.
*/
@DataClass.Generated.Member
public TranslationSpec(
@NonNull String language,
@DataFormat int dataFormat) {
this.mLanguage = language;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mLanguage);
this.mDataFormat = dataFormat;
com.android.internal.util.AnnotationValidations.validate(
DataFormat.class, null, mDataFormat);
// onConstructed(); // You can define this method to get a callback
public @Deprecated @NonNull String getLanguage() {
return mLanguage;
}
/**
* String representation of language codes e.g. "en", "es", etc.
* {@link ULocale} representing locale information of this spec.
*/
@DataClass.Generated.Member
public @NonNull String getLanguage() {
return mLanguage;
public @NonNull ULocale getLocale() {
return mLocale;
}
@DataClass.Generated.Member
@@ -105,6 +134,7 @@ public final class TranslationSpec implements Parcelable {
return "TranslationSpec { " +
"language = " + mLanguage + ", " +
"locale = " + mLocale + ", " +
"dataFormat = " + mDataFormat +
" }";
}
@@ -122,7 +152,8 @@ public final class TranslationSpec implements Parcelable {
TranslationSpec that = (TranslationSpec) o;
//noinspection PointlessBooleanExpression
return true
&& java.util.Objects.equals(mLanguage, that.mLanguage)
&& Objects.equals(mLanguage, that.mLanguage)
&& Objects.equals(mLocale, that.mLocale)
&& mDataFormat == that.mDataFormat;
}
@@ -133,7 +164,8 @@ public final class TranslationSpec implements Parcelable {
// int fieldNameHashCode() { ... }
int _hash = 1;
_hash = 31 * _hash + java.util.Objects.hashCode(mLanguage);
_hash = 31 * _hash + Objects.hashCode(mLanguage);
_hash = 31 * _hash + Objects.hashCode(mLocale);
_hash = 31 * _hash + mDataFormat;
return _hash;
}
@@ -145,6 +177,7 @@ public final class TranslationSpec implements Parcelable {
// void parcelFieldName(Parcel dest, int flags) { ... }
dest.writeString(mLanguage);
parcelLocale(dest, flags);
dest.writeInt(mDataFormat);
}
@@ -160,11 +193,17 @@ public final class TranslationSpec implements Parcelable {
// static FieldType unparcelFieldName(Parcel in) { ... }
String language = in.readString();
ULocale locale = unparcelLocale(in);
int dataFormat = in.readInt();
this.mLanguage = language;
com.android.internal.util.AnnotationValidations.validate(
Deprecated.class, null, mLanguage);
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mLanguage);
this.mLocale = locale;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mLocale);
this.mDataFormat = dataFormat;
com.android.internal.util.AnnotationValidations.validate(
DataFormat.class, null, mDataFormat);
@@ -187,10 +226,10 @@ public final class TranslationSpec implements Parcelable {
};
@DataClass.Generated(
time = 1614326090637L,
codegenVersion = "1.0.22",
time = 1618968175733L,
codegenVersion = "1.0.23",
sourceFile = "frameworks/base/core/java/android/view/translation/TranslationSpec.java",
inputSignatures = "public static final int DATA_FORMAT_TEXT\nprivate final @android.annotation.NonNull java.lang.String mLanguage\nprivate final @android.view.translation.TranslationSpec.DataFormat int mDataFormat\nclass TranslationSpec extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genHiddenConstDefs=true, genToString=true)")
inputSignatures = "public static final int DATA_FORMAT_TEXT\nprivate final @java.lang.Deprecated @android.annotation.NonNull java.lang.String mLanguage\nprivate final @android.annotation.NonNull android.icu.util.ULocale mLocale\nprivate final @android.view.translation.TranslationSpec.DataFormat int mDataFormat\n void parcelLocale(android.os.Parcel,int)\nstatic android.icu.util.ULocale unparcelLocale(android.os.Parcel)\nclass TranslationSpec extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genHiddenConstDefs=true, genToString=true, genConstructor=false)")
@Deprecated
private void __metadata() {}

View File

@@ -23,6 +23,7 @@ import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.app.assist.ActivityId;
import android.content.Context;
import android.icu.util.ULocale;
import android.os.Binder;
import android.os.Bundle;
import android.os.IRemoteCallback;
@@ -423,8 +424,8 @@ public final class UiTranslationManager {
case STATE_UI_TRANSLATION_STARTED:
case STATE_UI_TRANSLATION_RESUMED:
mCallback.onStarted(
bundle.getString(EXTRA_SOURCE_LOCALE),
bundle.getString(EXTRA_TARGET_LOCALE));
(ULocale) bundle.getSerializable(EXTRA_SOURCE_LOCALE),
(ULocale) bundle.getSerializable(EXTRA_TARGET_LOCALE));
break;
case STATE_UI_TRANSLATION_PAUSED:
mCallback.onPaused();

View File

@@ -17,6 +17,7 @@
package android.view.translation;
import android.annotation.NonNull;
import android.icu.util.ULocale;
import java.util.concurrent.Executor;
@@ -26,6 +27,12 @@ import java.util.concurrent.Executor;
*/
public interface UiTranslationStateCallback {
/**
* @deprecated use {@link #onStarted(ULocale, ULocale)} instead.
*/
@Deprecated
void onStarted(@NonNull String sourceLocale, @NonNull String targetLocale);
/**
* The system is requesting translation of the UI from {@code sourceLocale} to {@code
* targetLocale}.
@@ -33,7 +40,9 @@ public interface UiTranslationStateCallback {
* This is also called if either the requested {@code sourceLocale} or {@code targetLocale} has
* changed; or called again after {@link #onPaused()}.
*/
void onStarted(@NonNull String sourceLocale, @NonNull String targetLocale);
default void onStarted(@NonNull ULocale sourceLocale, @NonNull ULocale targetLocale) {
onStarted(sourceLocale.getLanguage(), targetLocale.getLanguage());
}
/**
* The system is requesting that the application temporarily show the UI contents in their

View File

@@ -193,8 +193,8 @@ final class TranslationManagerServiceImpl extends
res.putInt(EXTRA_STATE, state);
// TODO(177500482): Store the locale pair so it can be sent for RESUME events.
if (sourceSpec != null) {
res.putString(EXTRA_SOURCE_LOCALE, sourceSpec.getLanguage());
res.putString(EXTRA_TARGET_LOCALE, targetSpec.getLanguage());
res.putSerializable(EXTRA_SOURCE_LOCALE, sourceSpec.getLocale());
res.putSerializable(EXTRA_TARGET_LOCALE, targetSpec.getLocale());
}
// TODO(177500482): Only support the *current* Input Method.
List<InputMethodInfo> enabledInputMethods =