|
33 | 33 | import android.graphics.Typeface; |
34 | 34 | import android.graphics.drawable.Drawable; |
35 | 35 | import android.inputmethodservice.ExtractEditText; |
| 36 | +import android.os.AsyncTask; |
36 | 37 | import android.os.Bundle; |
37 | 38 | import android.os.Handler; |
38 | 39 | import android.os.Message; |
|
132 | 133 | import java.lang.ref.WeakReference; |
133 | 134 | import java.util.ArrayList; |
134 | 135 | import java.util.Locale; |
| 136 | +import java.util.concurrent.locks.ReentrantLock; |
135 | 137 |
|
136 | 138 | /** |
137 | 139 | * Displays text to the user and optionally allows them to edit it. A TextView |
@@ -378,6 +380,9 @@ static class Drawables { |
378 | 380 |
|
379 | 381 | private InputFilter[] mFilters = NO_FILTERS; |
380 | 382 |
|
| 383 | + private volatile Locale mCurrentTextServicesLocaleCache; |
| 384 | + private final ReentrantLock mCurrentTextServicesLocaleLock = new ReentrantLock(); |
| 385 | + |
381 | 386 | // It is possible to have a selection even when mEditor is null (programmatically set, like when |
382 | 387 | // a link is pressed). These highlight-related fields do not go in mEditor. |
383 | 388 | int mHighlightColor = 0x6633B5E5; |
@@ -451,6 +456,8 @@ public TextView(Context context, AttributeSet attrs, int defStyle) { |
451 | 456 | final Resources res = getResources(); |
452 | 457 | final CompatibilityInfo compat = res.getCompatibilityInfo(); |
453 | 458 |
|
| 459 | + updateTextServicesLocaleAsync(); |
| 460 | + |
454 | 461 | mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); |
455 | 462 | mTextPaint.density = res.getDisplayMetrics().density; |
456 | 463 | mTextPaint.setCompatibilityScaling(compat.applicationScale); |
@@ -7675,21 +7682,51 @@ boolean textCanBeSelected() { |
7675 | 7682 |
|
7676 | 7683 | /** |
7677 | 7684 | * This is a temporary method. Future versions may support multi-locale text. |
| 7685 | + * Caveat: This method may not return the latest text services locale, but this should be |
| 7686 | + * acceptable and it's more important to make this method asynchronous. |
7678 | 7687 | * |
7679 | 7688 | * @return The locale that should be used for a word iterator and a spell checker |
7680 | 7689 | * in this TextView, based on the current spell checker settings, |
7681 | 7690 | * the current IME's locale, or the system default locale. |
7682 | 7691 | * @hide |
7683 | 7692 | */ |
| 7693 | + // TODO: Support multi-locale |
| 7694 | + // TODO: Update the text services locale immediately after the keyboard locale is switched |
| 7695 | + // by catching intent of keyboard switch event |
7684 | 7696 | public Locale getTextServicesLocale() { |
| 7697 | + if (mCurrentTextServicesLocaleCache == null) { |
| 7698 | + // If there is no cached text services locale, just return the default locale. |
| 7699 | + mCurrentTextServicesLocaleCache = Locale.getDefault(); |
| 7700 | + } |
| 7701 | + // Start fetching the text services locale asynchronously. |
| 7702 | + updateTextServicesLocaleAsync(); |
| 7703 | + return mCurrentTextServicesLocaleCache; |
| 7704 | + } |
| 7705 | + |
| 7706 | + private void updateTextServicesLocaleAsync() { |
| 7707 | + AsyncTask.execute(new Runnable() { |
| 7708 | + @Override |
| 7709 | + public void run() { |
| 7710 | + if (mCurrentTextServicesLocaleLock.tryLock()) { |
| 7711 | + try { |
| 7712 | + updateTextServicesLocaleLocked(); |
| 7713 | + } finally { |
| 7714 | + mCurrentTextServicesLocaleLock.unlock(); |
| 7715 | + } |
| 7716 | + } |
| 7717 | + } |
| 7718 | + }); |
| 7719 | + } |
| 7720 | + |
| 7721 | + private void updateTextServicesLocaleLocked() { |
7685 | 7722 | Locale locale = Locale.getDefault(); |
7686 | 7723 | final TextServicesManager textServicesManager = (TextServicesManager) |
7687 | 7724 | mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); |
7688 | 7725 | final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true); |
7689 | 7726 | if (subtype != null) { |
7690 | 7727 | locale = SpellCheckerSubtype.constructLocaleFromString(subtype.getLocale()); |
7691 | 7728 | } |
7692 | | - return locale; |
| 7729 | + mCurrentTextServicesLocaleCache = locale; |
7693 | 7730 | } |
7694 | 7731 |
|
7695 | 7732 | void onLocaleChanged() { |
|
0 commit comments