| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.animation.Animator; |
| 20 |
import android.animation.ValueAnimator; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.content.res.ColorStateList; |
| 24 |
import android.graphics.Color; |
| 25 |
import android.text.TextUtils; |
| 26 |
import android.text.method.TransformationMethod; |
| 27 |
import android.text.method.TranslationTransformationMethod; |
| 28 |
import android.util.Log; |
| 29 |
import android.view.View; |
| 30 |
import android.view.translation.UiTranslationManager; |
| 31 |
import android.view.translation.ViewTranslationCallback; |
| 32 |
import android.view.translation.ViewTranslationRequest; |
| 33 |
import android.view.translation.ViewTranslationResponse; |
| 34 |
|
| 35 |
import java.lang.ref.WeakReference; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class TextViewTranslationCallback implements ViewTranslationCallback { |
| 44 |
|
| 45 |
private static final String TAG = "TextViewTranslationCb"; |
| 46 |
|
| 47 |
private static final boolean DEBUG = Log.isLoggable(UiTranslationManager.LOG_TAG, Log.DEBUG); |
| 48 |
|
| 49 |
private TranslationTransformationMethod mTranslationTransformation; |
| 50 |
private boolean mIsShowingTranslation = false; |
| 51 |
private boolean mAnimationRunning = false; |
| 52 |
private boolean mIsTextPaddingEnabled = false; |
| 53 |
private boolean mOriginalIsTextSelectable = false; |
| 54 |
private int mOriginalFocusable = 0; |
| 55 |
private boolean mOriginalFocusableInTouchMode = false; |
| 56 |
private boolean mOriginalClickable = false; |
| 57 |
private boolean mOriginalLongClickable = false; |
| 58 |
private CharSequence mPaddedText; |
| 59 |
private int mAnimationDurationMillis = 250; |
| 60 |
|
| 61 |
private CharSequence mContentDescription; |
| 62 |
|
| 63 |
private void clearTranslationTransformation() { |
| 64 |
if (DEBUG) { |
| 65 |
Log.v(TAG, "clearTranslationTransformation: " + mTranslationTransformation); |
| 66 |
} |
| 67 |
mTranslationTransformation = null; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
@Override |
| 74 |
public boolean onShowTranslation(@NonNull View view) { |
| 75 |
if (mIsShowingTranslation) { |
| 76 |
if (DEBUG) { |
| 77 |
Log.d(TAG, view + " is already showing translated text."); |
| 78 |
} |
| 79 |
return false; |
| 80 |
} |
| 81 |
ViewTranslationResponse response = view.getViewTranslationResponse(); |
| 82 |
if (response == null) { |
| 83 |
Log.e(TAG, "onShowTranslation() shouldn't be called before " |
| 84 |
+ "onViewTranslationResponse()."); |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
TextView theTextView = (TextView) view; |
| 92 |
if (mTranslationTransformation == null |
| 93 |
|| !response.equals(mTranslationTransformation.getViewTranslationResponse())) { |
| 94 |
TransformationMethod originalTranslationMethod = |
| 95 |
theTextView.getTransformationMethod(); |
| 96 |
mTranslationTransformation = new TranslationTransformationMethod(response, |
| 97 |
originalTranslationMethod); |
| 98 |
} |
| 99 |
final TransformationMethod transformation = mTranslationTransformation; |
| 100 |
WeakReference<TextView> textViewRef = new WeakReference<>(theTextView); |
| 101 |
runChangeTextWithAnimationIfNeeded( |
| 102 |
theTextView, |
| 103 |
() -> { |
| 104 |
mIsShowingTranslation = true; |
| 105 |
mAnimationRunning = false; |
| 106 |
|
| 107 |
TextView textView = textViewRef.get(); |
| 108 |
if (textView == null) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
mOriginalIsTextSelectable = textView.isTextSelectable(); |
| 119 |
if (mOriginalIsTextSelectable) { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
mOriginalFocusableInTouchMode = textView.isFocusableInTouchMode(); |
| 125 |
mOriginalFocusable = textView.getFocusable(); |
| 126 |
mOriginalClickable = textView.isClickable(); |
| 127 |
mOriginalLongClickable = textView.isLongClickable(); |
| 128 |
textView.setTextIsSelectable(false); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
textView.setTransformationMethod(transformation); |
| 135 |
}); |
| 136 |
if (response.getKeys().contains(ViewTranslationRequest.ID_CONTENT_DESCRIPTION)) { |
| 137 |
CharSequence translatedContentDescription = |
| 138 |
response.getValue(ViewTranslationRequest.ID_CONTENT_DESCRIPTION).getText(); |
| 139 |
if (!TextUtils.isEmpty(translatedContentDescription)) { |
| 140 |
mContentDescription = view.getContentDescription(); |
| 141 |
view.setContentDescription(translatedContentDescription); |
| 142 |
} |
| 143 |
} |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@Override |
| 151 |
public boolean onHideTranslation(@NonNull View view) { |
| 152 |
if (view.getViewTranslationResponse() == null) { |
| 153 |
Log.e(TAG, "onHideTranslation() shouldn't be called before " |
| 154 |
+ "onViewTranslationResponse()."); |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
if (mTranslationTransformation != null) { |
| 159 |
final TransformationMethod transformation = |
| 160 |
mTranslationTransformation.getOriginalTransformationMethod(); |
| 161 |
TextView theTextView = (TextView) view; |
| 162 |
WeakReference<TextView> textViewRef = new WeakReference<>(theTextView); |
| 163 |
runChangeTextWithAnimationIfNeeded( |
| 164 |
theTextView, |
| 165 |
() -> { |
| 166 |
mIsShowingTranslation = false; |
| 167 |
mAnimationRunning = false; |
| 168 |
|
| 169 |
TextView textView = textViewRef.get(); |
| 170 |
if (textView == null) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
textView.setTransformationMethod(transformation); |
| 177 |
|
| 178 |
if (mOriginalIsTextSelectable && !textView.isTextSelectable()) { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
textView.setTextIsSelectable(true); |
| 184 |
textView.setFocusableInTouchMode(mOriginalFocusableInTouchMode); |
| 185 |
textView.setFocusable(mOriginalFocusable); |
| 186 |
textView.setClickable(mOriginalClickable); |
| 187 |
textView.setLongClickable(mOriginalLongClickable); |
| 188 |
} |
| 189 |
}); |
| 190 |
if (!TextUtils.isEmpty(mContentDescription)) { |
| 191 |
view.setContentDescription(mContentDescription); |
| 192 |
} |
| 193 |
} else { |
| 194 |
if (DEBUG) { |
| 195 |
Log.w(TAG, "onHideTranslation(): no translated text."); |
| 196 |
} |
| 197 |
return false; |
| 198 |
} |
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
@Override |
| 206 |
public boolean onClearTranslation(@NonNull View view) { |
| 207 |
|
| 208 |
if (mTranslationTransformation != null) { |
| 209 |
onHideTranslation(view); |
| 210 |
clearTranslationTransformation(); |
| 211 |
mPaddedText = null; |
| 212 |
mContentDescription = null; |
| 213 |
} else { |
| 214 |
if (DEBUG) { |
| 215 |
Log.w(TAG, "onClearTranslation(): no translated text."); |
| 216 |
} |
| 217 |
return false; |
| 218 |
} |
| 219 |
return true; |
| 220 |
} |
| 221 |
|
| 222 |
public boolean isShowingTranslation() { |
| 223 |
return mIsShowingTranslation; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public boolean isAnimationRunning() { |
| 230 |
return mAnimationRunning; |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public void enableContentPadding() { |
| 235 |
mIsTextPaddingEnabled = true; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
boolean isTextPaddingEnabled() { |
| 243 |
return mIsTextPaddingEnabled; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
@Nullable |
| 255 |
CharSequence getPaddedText(CharSequence text, CharSequence translatedText) { |
| 256 |
if (text == null) { |
| 257 |
return null; |
| 258 |
} |
| 259 |
if (mPaddedText == null) { |
| 260 |
mPaddedText = computePaddedText(text, translatedText); |
| 261 |
} |
| 262 |
return mPaddedText; |
| 263 |
} |
| 264 |
|
| 265 |
@NonNull |
| 266 |
private CharSequence computePaddedText(CharSequence text, CharSequence translatedText) { |
| 267 |
if (translatedText == null) { |
| 268 |
return text; |
| 269 |
} |
| 270 |
int newLength = translatedText.length(); |
| 271 |
if (newLength <= text.length()) { |
| 272 |
return text; |
| 273 |
} |
| 274 |
StringBuilder sb = new StringBuilder(newLength); |
| 275 |
sb.append(text); |
| 276 |
for (int i = text.length(); i < newLength; i++) { |
| 277 |
sb.append(COMPAT_PAD_CHARACTER); |
| 278 |
} |
| 279 |
return sb; |
| 280 |
} |
| 281 |
|
| 282 |
private static final char COMPAT_PAD_CHARACTER = '\u2002'; |
| 283 |
|
| 284 |
@Override |
| 285 |
public void setAnimationDurationMillis(int durationMillis) { |
| 286 |
mAnimationDurationMillis = durationMillis; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private void runChangeTextWithAnimationIfNeeded(TextView view, Runnable changeTextRunnable) { |
| 297 |
boolean areAnimatorsEnabled = ValueAnimator.areAnimatorsEnabled(); |
| 298 |
if (!areAnimatorsEnabled) { |
| 299 |
|
| 300 |
changeTextRunnable.run(); |
| 301 |
return; |
| 302 |
} |
| 303 |
if (mAnimator != null) { |
| 304 |
mAnimator.end(); |
| 305 |
|
| 306 |
} |
| 307 |
mAnimationRunning = true; |
| 308 |
int fadedOutColor = colorWithAlpha(view.getCurrentTextColor(), 0); |
| 309 |
mAnimator = ValueAnimator.ofArgb(view.getCurrentTextColor(), fadedOutColor); |
| 310 |
mAnimator.addUpdateListener( |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
(valueAnimator) -> view.setTextColor((Integer) valueAnimator.getAnimatedValue())); |
| 315 |
mAnimator.setRepeatMode(ValueAnimator.REVERSE); |
| 316 |
mAnimator.setRepeatCount(1); |
| 317 |
mAnimator.setDuration(mAnimationDurationMillis); |
| 318 |
final ColorStateList originalColors = view.getTextColors(); |
| 319 |
WeakReference<TextView> viewRef = new WeakReference<>(view); |
| 320 |
mAnimator.addListener(new Animator.AnimatorListener() { |
| 321 |
@Override |
| 322 |
public void onAnimationStart(Animator animation) { |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
public void onAnimationEnd(Animator animation) { |
| 327 |
TextView view = viewRef.get(); |
| 328 |
if (view != null) { |
| 329 |
view.setTextColor(originalColors); |
| 330 |
} |
| 331 |
mAnimator = null; |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
public void onAnimationCancel(Animator animation) { |
| 336 |
} |
| 337 |
|
| 338 |
@Override |
| 339 |
public void onAnimationRepeat(Animator animation) { |
| 340 |
changeTextRunnable.run(); |
| 341 |
} |
| 342 |
}); |
| 343 |
mAnimator.start(); |
| 344 |
} |
| 345 |
|
| 346 |
private ValueAnimator mAnimator; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
private static int colorWithAlpha(int color, int newAlpha) { |
| 352 |
return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color)); |
| 353 |
} |
| 354 |
} |