summaryrefslogtreecommitdiff
path: root/core/java/android/view/View.java
diff options
context:
space:
mode:
authorJoanne Chung <joannechung@google.com>2021-03-24 22:24:37 +0800
committerJoanne Chung <joannechung@google.com>2021-03-29 20:36:40 +0800
commite1b23cd423453013e65f3bec29d7419fc7bbf62b (patch)
tree610f0fef41d248f4dfb2ec33c3fc9bd19882bfa1 /core/java/android/view/View.java
parent948bd86481993faa0aefa3f276c9f5d8180e7b07 (diff)
Add translation APIs for virtual views.
Bug: 178046780 Test: manual Test: atest CtsTranslationTestCases CTS-Coverage-Bug: 177960696 Change-Id: I99caed0d42b3a164a25c2707eba7057c42c19140
Diffstat (limited to 'core/java/android/view/View.java')
-rw-r--r--core/java/android/view/View.java50
1 files changed, 46 insertions, 4 deletions
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index fb528995b2f7..615dd82fb848 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -107,6 +107,7 @@ import android.util.AttributeSet;
import android.util.FloatProperty;
import android.util.LayoutDirection;
import android.util.Log;
+import android.util.LongSparseArray;
import android.util.LongSparseLongArray;
import android.util.Pair;
import android.util.Pools.SynchronizedPool;
@@ -30741,6 +30742,30 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
}
/**
+ * Returns a {@link ViewTranslationRequest} list which represents the content to be translated
+ * in the virtual view. This is called if this view returned a virtual view structure
+ * from {@link #onProvideContentCaptureStructure} and the system determined that those virtual
+ * views were relevant for translation.
+ *
+ * <p>The default implementation does nothing.</p>
+ *
+ * @param virtualChildIds the virtual child ids which represents the child views in the virtual
+ * view.
+ * @param supportedFormats the supported translation formats. For now, the only possible value
+ * is the {@link android.view.translation.TranslationSpec#DATA_FORMAT_TEXT}.
+ * @param requestsCollector a {@link ViewTranslationRequest} collector that will be called
+ * multiple times to collect the information to be translated in the virtual view. One
+ * {@link ViewTranslationRequest} per virtual child. The {@link ViewTranslationRequest} must
+ * contains the {@link AutofillId} corresponding to the virtualChildIds.
+ */
+ @SuppressLint("NullableCollection")
+ public void onCreateTranslationRequests(@NonNull long[] virtualChildIds,
+ @NonNull @DataFormat int[] supportedFormats,
+ @NonNull Consumer<ViewTranslationRequest> requestsCollector) {
+ // no-op
+ }
+
+ /**
* Returns a {@link ViewTranslationCallback} that is used to display/hide the translated
* information. If the View supports displaying translated content, it should implement
* {@link ViewTranslationCallback}.
@@ -30782,13 +30807,29 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
}
/**
+ * Called when the content from {@link View#onCreateTranslationRequest} had been translated by
+ * the TranslationService.
+ *
+ * <p> The default implementation does nothing.</p>
+ *
+ * @param response a {@link ViewTranslationResponse} SparseArray for the request that send by
+ * {@link View#onCreateTranslationRequests} that contains the translated information which can
+ * be shown in the view. The key of SparseArray is
+ * the virtual child ids.
+ */
+ public void onTranslationResponse(@NonNull LongSparseArray<ViewTranslationResponse> response) {
+ // no-op
+ }
+
+ /**
* Dispatch to collect the {@link ViewTranslationRequest}s for translation purpose by traversing
* the hierarchy when the app requests ui translation. Typically, this method should only be
* overridden by subclasses that provide a view hierarchy (such as {@link ViewGroup}). Other
* classes should override {@link View#onCreateTranslationRequest}. When requested to start the
* ui translation, the system will call this method to traverse the view hierarchy to call
* {@link View#onCreateTranslationRequest} to build {@link ViewTranslationRequest}s and create a
- * {@link android.view.translation.Translator} to translate the requests.
+ * {@link android.view.translation.Translator} to translate the requests. All the
+ * {@link ViewTranslationRequest}s will be added when the traversal is done.
*
* <p> The default implementation will call {@link View#onCreateTranslationRequest} to build
* {@link ViewTranslationRequest} if the view should be translated. </p>
@@ -30808,14 +30849,15 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
@NonNull List<ViewTranslationRequest> requests) {
AutofillId autofillId = getAutofillId();
if (viewIds.containsKey(autofillId)) {
- ViewTranslationRequest request = null;
if (viewIds.get(autofillId) == null) {
- request = onCreateTranslationRequest(supportedFormats);
+ ViewTranslationRequest request = onCreateTranslationRequest(supportedFormats);
if (request != null && request.getKeys().size() > 0) {
requests.add(request);
}
} else {
- // TODO: handle virtual view
+ onCreateTranslationRequests(viewIds.get(autofillId), supportedFormats, request -> {
+ requests.add(request);
+ });
}
}
}