From 8c48915e429fe991ac03109adc926140ebf8e1e9 Mon Sep 17 00:00:00 2001 From: Ahaan Ugale Date: Mon, 5 Apr 2021 11:30:01 -0700 Subject: [PATCH] UiTranslation: Temporary fix for crash due to length increases. Ellipsizes the translated text if it's longer than the view text. This fixes a crash in apps that operate on the view text based on layout calculations made on the transformed text. A proper fix will follow to modify the UiTranslationManger API to make this compat behavior happen only on request, and probably change the behavior to instead add padding to the view text to make it match the translated text length. Bug: 179693024 Test: atest CtsTranslationTestCases Change-Id: I685ae4a9752c817db81f0d736f1d746b2e4a9839 --- .../method/TranslationTransformationMethod.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/java/android/text/method/TranslationTransformationMethod.java b/core/java/android/text/method/TranslationTransformationMethod.java index 54c0ffcdbb655..7db999a89fbe7 100644 --- a/core/java/android/text/method/TranslationTransformationMethod.java +++ b/core/java/android/text/method/TranslationTransformationMethod.java @@ -78,11 +78,24 @@ public class TranslationTransformationMethod implements TransformationMethod2 { if (TextUtils.isEmpty(translatedText) || isWhitespace(translatedText.toString())) { return source; } else { + // TODO(b/179693024): Remove this once we have the fix to pad the view text instead. + translatedText = ellipsize(translatedText, ((TextView) view).getText().length()); // TODO(b/174283799): apply the spans to the text return translatedText; } } + private static CharSequence ellipsize(CharSequence text, int newLength) { + if (text.length() <= newLength) { + return text; + } + String ellipsis = String.valueOf('\u2026'); + if (newLength == 1) { + return ellipsis; + } + return TextUtils.concat(TextUtils.trimToSize(text, newLength - 1), ellipsis); + } + @Override public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction,