Skip to content

Commit 0aaffb1

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 d175a75 + 8fa0838 commit 0aaffb1

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

core/java/android/widget/TextView.java

Lines changed: 38 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;
@@ -451,6 +456,8 @@ public TextView(Context context, AttributeSet attrs, int defStyle) {
451456
final Resources res = getResources();
452457
final CompatibilityInfo compat = res.getCompatibilityInfo();
453458

459+
updateTextServicesLocaleAsync();
460+
454461
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
455462
mTextPaint.density = res.getDisplayMetrics().density;
456463
mTextPaint.setCompatibilityScaling(compat.applicationScale);
@@ -7675,21 +7682,51 @@ boolean textCanBeSelected() {
76757682

76767683
/**
76777684
* 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.
76787687
*
76797688
* @return The locale that should be used for a word iterator and a spell checker
76807689
* in this TextView, based on the current spell checker settings,
76817690
* the current IME's locale, or the system default locale.
76827691
* @hide
76837692
*/
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
76847696
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() {
76857722
Locale locale = Locale.getDefault();
76867723
final TextServicesManager textServicesManager = (TextServicesManager)
76877724
mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
76887725
final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true);
76897726
if (subtype != null) {
76907727
locale = SpellCheckerSubtype.constructLocaleFromString(subtype.getLocale());
76917728
}
7692-
return locale;
7729+
mCurrentTextServicesLocaleCache = locale;
76937730
}
76947731

76957732
void onLocaleChanged() {

0 commit comments

Comments
 (0)