1 /* 2 * Copyright (C) 2020 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 package android.text.method; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.graphics.Rect; 21 import android.text.TextUtils; 22 import android.util.Log; 23 import android.view.View; 24 import android.view.translation.TranslationResponseValue; 25 import android.view.translation.ViewTranslationRequest; 26 import android.view.translation.ViewTranslationResponse; 27 import android.widget.TextView; 28 29 import java.util.regex.Pattern; 30 31 /** 32 * Transforms source text into an translated string. 33 * 34 * @hide 35 */ 36 @android.ravenwood.annotation.RavenwoodKeepWholeClass 37 public class TranslationTransformationMethod implements TransformationMethod2 { 38 39 private static final String TAG = "TranslationTransformationMethod"; 40 private static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+"); 41 42 @NonNull 43 private final ViewTranslationResponse mTranslationResponse; 44 @Nullable 45 private TransformationMethod mOriginalTranslationMethod; 46 private boolean mAllowLengthChanges; 47 48 /** 49 * @param response the translated result from translation service. 50 * @param method the {@link TextView}'s original {@link TransformationMethod} 51 */ TranslationTransformationMethod(@onNull ViewTranslationResponse response, @Nullable TransformationMethod method)52 public TranslationTransformationMethod(@NonNull ViewTranslationResponse response, 53 @Nullable TransformationMethod method) { 54 mTranslationResponse = response; 55 mOriginalTranslationMethod = method; 56 } 57 58 /** 59 * Returns the {@link TextView}'s original {@link TransformationMethod}. This can be used to 60 * restore to show if the user pauses or finish the ui translation. 61 */ getOriginalTransformationMethod()62 public TransformationMethod getOriginalTransformationMethod() { 63 return mOriginalTranslationMethod; 64 } 65 66 /** 67 * Returns the {@link TextView}'s {@link ViewTranslationResponse}. 68 */ getViewTranslationResponse()69 public ViewTranslationResponse getViewTranslationResponse() { 70 return mTranslationResponse; 71 } 72 73 @Override getTransformation(CharSequence source, View view)74 public CharSequence getTransformation(CharSequence source, View view) { 75 if (!mAllowLengthChanges) { 76 Log.w(TAG, "Caller did not enable length changes; not transforming to translated text"); 77 return source; 78 } 79 TranslationResponseValue value = mTranslationResponse.getValue( 80 ViewTranslationRequest.ID_TEXT); 81 CharSequence translatedText; 82 if (value.getStatusCode() == TranslationResponseValue.STATUS_SUCCESS) { 83 translatedText = value.getText(); 84 } else { 85 translatedText = ""; 86 } 87 88 if (TextUtils.isEmpty(translatedText) || isWhitespace(translatedText.toString())) { 89 return source; 90 } else { 91 // TODO(b/174283799): apply the spans to the text 92 return translatedText; 93 } 94 } 95 96 @Override onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect)97 public void onFocusChanged(View view, CharSequence sourceText, 98 boolean focused, int direction, 99 Rect previouslyFocusedRect) { 100 // do nothing 101 } 102 103 @Override setLengthChangesAllowed(boolean allowLengthChanges)104 public void setLengthChangesAllowed(boolean allowLengthChanges) { 105 mAllowLengthChanges = allowLengthChanges; 106 } 107 isWhitespace(String text)108 private boolean isWhitespace(String text) { 109 return PATTERN_WHITESPACE.matcher(text.substring(0, text.length())).matches(); 110 } 111 } 112