diff --git a/core/java/android/widget/SpellChecker.java b/core/java/android/widget/SpellChecker.java index 74ea038f78f39..9e7f97ea16ebd 100644 --- a/core/java/android/widget/SpellChecker.java +++ b/core/java/android/widget/SpellChecker.java @@ -109,7 +109,7 @@ public class SpellChecker implements SpellCheckerSessionListener { mIds = new int[size]; mSpellCheckSpans = new SpellCheckSpan[size]; - setLocale(mTextView.getTextServicesLocale()); + setLocale(mTextView.getSpellCheckerLocale()); mCookie = hashCode(); } @@ -120,7 +120,8 @@ public class SpellChecker implements SpellCheckerSessionListener { mTextServicesManager = (TextServicesManager) mTextView.getContext(). getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); if (!mTextServicesManager.isSpellCheckerEnabled() - || mTextServicesManager.getCurrentSpellCheckerSubtype(true) == null) { + || mCurrentLocale == null + || mTextServicesManager.getCurrentSpellCheckerSubtype(true) == null) { mSpellCheckerSession = null; } else { mSpellCheckerSession = mTextServicesManager.newSpellCheckerSession( @@ -146,8 +147,10 @@ public class SpellChecker implements SpellCheckerSessionListener { resetSession(); - // Change SpellParsers' wordIterator locale - mWordIterator = new WordIterator(locale); + if (locale != null) { + // Change SpellParsers' wordIterator locale + mWordIterator = new WordIterator(locale); + } // This class is the listener for locale change: warn other locale-aware objects mTextView.onLocaleChanged(); @@ -222,9 +225,9 @@ public class SpellChecker implements SpellCheckerSessionListener { if (DBG) { Log.d(TAG, "Start spell-checking: " + start + ", " + end); } - final Locale locale = mTextView.getTextServicesLocale(); + final Locale locale = mTextView.getSpellCheckerLocale(); final boolean isSessionActive = isSessionActive(); - if (mCurrentLocale == null || (!(mCurrentLocale.equals(locale)))) { + if (locale == null || mCurrentLocale == null || (!(mCurrentLocale.equals(locale)))) { setLocale(locale); // Re-check the entire text start = 0; diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 1f09ddd9e4a7c..1f64c5b0041a4 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -511,7 +511,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private InputFilter[] mFilters = NO_FILTERS; - private volatile Locale mCurrentTextServicesLocaleCache; + private volatile Locale mCurrentSpellCheckerLocaleCache; private final ReentrantLock mCurrentTextServicesLocaleLock = new ReentrantLock(); // It is possible to have a selection even when mEditor is null (programmatically set, like when @@ -7825,27 +7825,46 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener (isTextSelectable() && mText instanceof Spannable && isEnabled()); } + private Locale getTextServicesLocale(boolean allowNullLocale) { + // Start fetching the text services locale asynchronously. + updateTextServicesLocaleAsync(); + // If !allowNullLocale and there is no cached text services locale, just return the default + // locale. + return (mCurrentSpellCheckerLocaleCache == null && !allowNullLocale) ? Locale.getDefault() + : mCurrentSpellCheckerLocaleCache; + } + /** * This is a temporary method. Future versions may support multi-locale text. * Caveat: This method may not return the latest text services locale, but this should be * acceptable and it's more important to make this method asynchronous. * - * @return The locale that should be used for a word iterator and a spell checker + * @return The locale that should be used for a word iterator * in this TextView, based on the current spell checker settings, * the current IME's locale, or the system default locale. + * Please note that a word iterator in this TextView is different from another word iterator + * used by SpellChecker.java of TextView. This method should be used for the former. * @hide */ // TODO: Support multi-locale // TODO: Update the text services locale immediately after the keyboard locale is switched // by catching intent of keyboard switch event public Locale getTextServicesLocale() { - if (mCurrentTextServicesLocaleCache == null) { - // If there is no cached text services locale, just return the default locale. - mCurrentTextServicesLocaleCache = Locale.getDefault(); - } - // Start fetching the text services locale asynchronously. - updateTextServicesLocaleAsync(); - return mCurrentTextServicesLocaleCache; + return getTextServicesLocale(false /* allowNullLocale */); + } + + /** + * This is a temporary method. Future versions may support multi-locale text. + * Caveat: This method may not return the latest spell checker locale, but this should be + * acceptable and it's more important to make this method asynchronous. + * + * @return The locale that should be used for a spell checker in this TextView, + * based on the current spell checker settings, the current IME's locale, or the system default + * locale. + * @hide + */ + public Locale getSpellCheckerLocale() { + return getTextServicesLocale(true /* allowNullLocale */); } private void updateTextServicesLocaleAsync() { @@ -7864,14 +7883,16 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } private void updateTextServicesLocaleLocked() { - Locale locale = Locale.getDefault(); final TextServicesManager textServicesManager = (TextServicesManager) mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true); + final Locale locale; if (subtype != null) { locale = SpellCheckerSubtype.constructLocaleFromString(subtype.getLocale()); + } else { + locale = null; } - mCurrentTextServicesLocaleCache = locale; + mCurrentSpellCheckerLocaleCache = locale; } void onLocaleChanged() {