| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
public TranslationTransformationMethod(@NonNull ViewTranslationResponse response, |
| 52 |
@Nullable TransformationMethod method) { |
| 53 |
mTranslationResponse = response; |
| 54 |
mOriginalTranslationMethod = method; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public TransformationMethod getOriginalTransformationMethod() { |
| 62 |
return mOriginalTranslationMethod; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public ViewTranslationResponse getViewTranslationResponse() { |
| 69 |
return mTranslationResponse; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 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 |
|
| 91 |
return translatedText; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void onFocusChanged(View view, CharSequence sourceText, |
| 97 |
boolean focused, int direction, |
| 98 |
Rect previouslyFocusedRect) { |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void setLengthChangesAllowed(boolean allowLengthChanges) { |
| 104 |
mAllowLengthChanges = allowLengthChanges; |
| 105 |
} |
| 106 |
|
| 107 |
private boolean isWhitespace(String text) { |
| 108 |
return PATTERN_WHITESPACE.matcher(text.substring(0, text.length())).matches(); |
| 109 |
} |
| 110 |
} |