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     /**
66      * Returns the {@link TextView}'s {@link ViewTranslationResponse}.
67      */
getViewTranslationResponse()68     public ViewTranslationResponse getViewTranslationResponse() {
69         return mTranslationResponse;
70     }
71 
72     @Override
getTransformation(CharSequence source, View view)73     public CharSequence getTransformation(CharSequence source, View view) {
74         if (!mAllowLengthChanges) {
75             Log.w(TAG, "Caller did not enable length changes; not transforming to translated text");
76             return source;
77         }
78         TranslationResponseValue value = mTranslationResponse.getValue(
79                 ViewTranslationRequest.ID_TEXT);
80         CharSequence translatedText;
81         if (value.getStatusCode() == TranslationResponseValue.STATUS_SUCCESS) {
82             translatedText = value.getText();
83         } else {
84             translatedText = "";
85         }
86 
87         if (TextUtils.isEmpty(translatedText) || isWhitespace(translatedText.toString())) {
88             return source;
89         } else {
90             // TODO(b/174283799): apply the spans to the text
91             return translatedText;
92         }
93     }
94 
95     @Override
onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect)96     public void onFocusChanged(View view, CharSequence sourceText,
97             boolean focused, int direction,
98             Rect previouslyFocusedRect) {
99         // do nothing
100     }
101 
102     @Override
setLengthChangesAllowed(boolean allowLengthChanges)103     public void setLengthChangesAllowed(boolean allowLengthChanges) {
104         mAllowLengthChanges = allowLengthChanges;
105     }
106 
isWhitespace(String text)107     private boolean isWhitespace(String text) {
108         return PATTERN_WHITESPACE.matcher(text.substring(0, text.length())).matches();
109     }
110 }
111