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 * <p> NOTE: In Android version {@link android.os.Build.VERSION_CODES#TIRAMISU} and later, 56 * the implementation must be able to handle a selectable {@link android.widget.TextView} 57 * (i.e., {@link android.widget.TextView#isTextSelectable()} returns {@code true}. The default 58 * callback implementation for TextView uses a {@link android.text.method.TransformationMethod} 59 * to show the translated text, which will cause a crash when the translated text is selected. 60 * Therefore, the default callback temporarily makes the TextView non-selectable while the 61 * translation text is shown. This is one approach for handling selectable TextViews a 62 * TransformationMethod is used. 63 * 64 * See {@link View#onViewTranslationResponse} for how to get the translated information. 65 * 66 * @return {@code true} if the View handles showing the translation. 67 */ onShowTranslation(@onNull View view)68 boolean onShowTranslation(@NonNull View view); 69 /** 70 * Called when user wants to view the original content instead of the translated content. This 71 * method will not be called before {@link View#onViewTranslationResponse} or 72 * {@link View#onVirtualViewTranslationResponses}. 73 * 74 * @return {@code true} if the View handles hiding the translation. 75 */ onHideTranslation(@onNull View view)76 boolean onHideTranslation(@NonNull View view); 77 /** 78 * Called when the translation state is no longer needed. It should restore the original content 79 * and clear all saved states. 80 * 81 * @return {@code true} if the View handles clearing the translation. 82 */ onClearTranslation(@onNull View view)83 boolean onClearTranslation(@NonNull View view); 84 85 /** 86 * Enables padding on the view's original content. 87 * <p> 88 * This is useful when we do not modify the content directly, rather use a mechanism like 89 * {@link android.text.method.TransformationMethod}. If the app misbehaves when the displayed 90 * translation and the underlying content have different sizes, the platform intelligence can 91 * request that the original content be padded to make the sizes match. 92 * 93 * @hide 94 */ enableContentPadding()95 default void enableContentPadding() {} 96 97 /** 98 * Sets the duration for animations while transitioning the view between the original and 99 * translated contents. 100 * 101 * @hide 102 */ setAnimationDurationMillis(int durationMillis)103 default void setAnimationDurationMillis(int durationMillis) {} 104 } 105