Skip to content

Commit 8e76711

Browse files
satok16Android (Google) Code Review
authored andcommitted
Merge "Don't update the text services locale in the main thread" into jb-mr1-dev
2 parents 524dcb7 + 1eac6b7 commit 8e76711

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

core/java/android/view/textservice/TextServicesManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ public void setCurrentSpellChecker(SpellCheckerInfo sci) {
217217
public SpellCheckerSubtype getCurrentSpellCheckerSubtype(
218218
boolean allowImplicitlySelectedSubtype) {
219219
try {
220+
if (sService == null) {
221+
// TODO: This is a workaround. Needs to investigate why sService could be null
222+
// here.
223+
Log.e(TAG, "sService is null.");
224+
return null;
225+
}
220226
// Passing null as a locale until we support multiple enabled spell checker subtypes.
221227
return sService.getCurrentSpellCheckerSubtype(null, allowImplicitlySelectedSubtype);
222228
} catch (RemoteException e) {

core/java/android/widget/TextView.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import android.graphics.Typeface;
3434
import android.graphics.drawable.Drawable;
3535
import android.inputmethodservice.ExtractEditText;
36+
import android.os.AsyncTask;
3637
import android.os.Bundle;
3738
import android.os.Handler;
3839
import android.os.Message;
@@ -132,6 +133,7 @@
132133
import java.lang.ref.WeakReference;
133134
import java.util.ArrayList;
134135
import java.util.Locale;
136+
import java.util.concurrent.locks.ReentrantLock;
135137

136138
/**
137139
* Displays text to the user and optionally allows them to edit it. A TextView
@@ -378,6 +380,9 @@ static class Drawables {
378380

379381
private InputFilter[] mFilters = NO_FILTERS;
380382

383+
private volatile Locale mCurrentTextServicesLocaleCache;
384+
private final ReentrantLock mCurrentTextServicesLocaleLock = new ReentrantLock();
385+
381386
// It is possible to have a selection even when mEditor is null (programmatically set, like when
382387
// a link is pressed). These highlight-related fields do not go in mEditor.
383388
int mHighlightColor = 0x6633B5E5;
@@ -7675,21 +7680,51 @@ boolean textCanBeSelected() {
76757680

76767681
/**
76777682
* This is a temporary method. Future versions may support multi-locale text.
7683+
* Caveat: This method may not return the latest text services locale, but this should be
7684+
* acceptable and it's more important to make this method asynchronous.
76787685
*
76797686
* @return The locale that should be used for a word iterator and a spell checker
76807687
* in this TextView, based on the current spell checker settings,
76817688
* the current IME's locale, or the system default locale.
76827689
* @hide
76837690
*/
7691+
// TODO: Support multi-locale
7692+
// TODO: Update the text services locale immediately after the keyboard locale is switched
7693+
// by catching intent of keyboard switch event
76847694
public Locale getTextServicesLocale() {
7695+
if (mCurrentTextServicesLocaleCache == null) {
7696+
// If there is no cached text services locale, just return the default locale.
7697+
mCurrentTextServicesLocaleCache = Locale.getDefault();
7698+
}
7699+
// Start fetching the text services locale asynchronously.
7700+
updateTextServicesLocaleAsync();
7701+
return mCurrentTextServicesLocaleCache;
7702+
}
7703+
7704+
private void updateTextServicesLocaleAsync() {
7705+
AsyncTask.execute(new Runnable() {
7706+
@Override
7707+
public void run() {
7708+
if (mCurrentTextServicesLocaleLock.tryLock()) {
7709+
try {
7710+
updateTextServicesLocaleLocked();
7711+
} finally {
7712+
mCurrentTextServicesLocaleLock.unlock();
7713+
}
7714+
}
7715+
}
7716+
});
7717+
}
7718+
7719+
private void updateTextServicesLocaleLocked() {
76857720
Locale locale = Locale.getDefault();
76867721
final TextServicesManager textServicesManager = (TextServicesManager)
76877722
mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
76887723
final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true);
76897724
if (subtype != null) {
76907725
locale = SpellCheckerSubtype.constructLocaleFromString(subtype.getLocale());
76917726
}
7692-
return locale;
7727+
mCurrentTextServicesLocaleCache = locale;
76937728
}
76947729

76957730
void onLocaleChanged() {

0 commit comments

Comments
 (0)