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.NonNull;
20 import android.annotation.UiThread;
21 import android.view.View;
22 import android.view.contentcapture.ContentCaptureSession;
23 
24 /**
25  * <p> Callback for handling the translated information show or hide in the {@link View}.
26  *
27  * <p> When the platform intelligence starts translation of an app's ui, the system will call
28  * {@link View#dispatchCreateViewTranslationRequest} to collect the {@link ViewTranslationRequest}s
29  * for translation purpose by traversing the hierarchy then send to translation service. After
30  * receiving the {@link ViewTranslationResponse}, the system will call
31  * {@link ViewTranslationCallback#onShowTranslation(View)} to show the translated information for
32  * the {@link View}.
33  */
34 @UiThread
35 public interface ViewTranslationCallback {
36     /**
37      * Called when the translated text is ready to show or if the user has requested to reshow the
38      * translated content after hiding it.
39      * <p>
40      * The translated content can be obtained from {@link View#getViewTranslationResponse}. This
41      * method will not be called before {@link View#onViewTranslationResponse} or
42      * {@link View#onVirtualViewTranslationResponses}.
43      *
44      * <p> NOTE: It is possible the user changes text that causes a new
45      * {@link ViewTranslationResponse} returns to show the new translation. If you cache the
46      * {@link ViewTranslationResponse} here, you should remember to keep the cached value up
47      * to date.
48      *
49      * <p> NOTE: For TextView implementation, {@link ContentCaptureSession#notifyViewTextChanged}
50      * shouldn't be called with the translated text, simply calling setText() here will trigger the
51      * method. You should either override {@code View#onProvideContentCaptureStructure()} to report
52      * the original text instead of the translated text or use a different approach to display the
53      * translated text.
54      *
55      * See {@link View#onViewTranslationResponse} for how to get the translated information.
56      *
57      * @return {@code true} if the View handles showing the translation.
58      */
onShowTranslation(@onNull View view)59     boolean onShowTranslation(@NonNull View view);
60     /**
61      * Called when user wants to view the original content instead of the translated content. This
62      * method will not be called before {@link View#onViewTranslationResponse} or
63      * {@link View#onViewTranslationResponse}.
64      *
65      * @return {@code true} if the View handles hiding the translation.
66      */
onHideTranslation(@onNull View view)67     boolean onHideTranslation(@NonNull View view);
68     /**
69      * Called when the translation state is no longer needed. It should restore the original content
70      * and clear all saved states.
71      *
72      * @return {@code true} if the View handles clearing the translation.
73      */
onClearTranslation(@onNull View view)74     boolean onClearTranslation(@NonNull View view);
75 
76     /**
77      * Enables padding on the view's original content.
78      * <p>
79      * This is useful when we do not modify the content directly, rather use a mechanism like
80      * {@link android.text.method.TransformationMethod}. If the app misbehaves when the displayed
81      * translation and the underlying content have different sizes, the platform intelligence can
82      * request that the original content be padded to make the sizes match.
83      *
84      * @hide
85      */
enableContentPadding()86     default void enableContentPadding() {}
87 
88     /**
89      * Sets the duration for animations while transitioning the view between the original and
90      * translated contents.
91      *
92      * @hide
93      */
setAnimationDurationMillis(int durationMillis)94     default void setAnimationDurationMillis(int durationMillis) {}
95 }
96