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 public class TranslationTransformationMethod implements TransformationMethod2 { 37 38 private static final String TAG = "TranslationTransformationMethod"; 39 private static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+"); 40 41 @NonNull 42 private final ViewTranslationResponse mTranslationResponse; 43 @Nullable 44 private TransformationMethod mOriginalTranslationMethod; 45 private boolean mAllowLengthChanges; 46 47 /** 48 * @param response the translated result from translation service. 49 * @param method the {@link TextView}'s original {@link TransformationMethod} 50 */ TranslationTransformationMethod(@onNull ViewTranslationResponse response, @Nullable TransformationMethod method)51 public TranslationTransformationMethod(@NonNull ViewTranslationResponse response, 52 @Nullable TransformationMethod method) { 53 mTranslationResponse = response; 54 mOriginalTranslationMethod = method; 55 } 56 57 /** 58 * Returns the {@link TextView}'s original {@link TransformationMethod}. This can be used to 59 * restore to show if the user pauses or finish the ui translation. 60 */ getOriginalTransformationMethod()61 public TransformationMethod getOriginalTransformationMethod() { 62 return mOriginalTranslationMethod; 63 } 64 65 @Override getTransformation(CharSequence source, View view)66 public CharSequence getTransformation(CharSequence source, View view) { 67 if (!mAllowLengthChanges) { 68 Log.w(TAG, "Caller did not enable length changes; not transforming to translated text"); 69 return source; 70 } 71 TranslationResponseValue value = mTranslationResponse.getValue( 72 ViewTranslationRequest.ID_TEXT); 73 CharSequence translatedText; 74 if (value.getStatusCode() == TranslationResponseValue.STATUS_SUCCESS) { 75 translatedText = value.getText(); 76 } else { 77 translatedText = ""; 78 } 79 80 if (TextUtils.isEmpty(translatedText) || isWhitespace(translatedText.toString())) { 81 return source; 82 } else { 83 // TODO(b/174283799): apply the spans to the text 84 return translatedText; 85 } 86 } 87 88 @Override onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect)89 public void onFocusChanged(View view, CharSequence sourceText, 90 boolean focused, int direction, 91 Rect previouslyFocusedRect) { 92 // do nothing 93 } 94 95 @Override setLengthChangesAllowed(boolean allowLengthChanges)96 public void setLengthChangesAllowed(boolean allowLengthChanges) { 97 mAllowLengthChanges = allowLengthChanges; 98 } 99 isWhitespace(String text)100 private boolean isWhitespace(String text) { 101 return PATTERN_WHITESPACE.matcher(text.substring(0, text.length())).matches(); 102 } 103 } 104