Skip to content

Commit 34bd596

Browse files
satok16Android (Google) Code Review
authored andcommitted
Merge "Respect user settings for spell checking language if explicitly set" into ics-mr1
2 parents 42694dc + 05f2470 commit 34bd596

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

core/java/android/widget/SpellChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public SpellChecker(TextView textView) {
7676
mIds = new int[size];
7777
mSpellCheckSpans = new SpellCheckSpan[size];
7878

79-
setLocale(mTextView.getLocale());
79+
setLocale(mTextView.getTextServicesLocale());
8080

8181
mCookie = hashCode();
8282
}
@@ -173,7 +173,7 @@ public void onSelectionChanged() {
173173
}
174174

175175
public void spellCheck(int start, int end) {
176-
final Locale locale = mTextView.getLocale();
176+
final Locale locale = mTextView.getTextServicesLocale();
177177
if (mCurrentLocale == null || (!(mCurrentLocale.equals(locale)))) {
178178
setLocale(locale);
179179
// Re-check the entire text

core/java/android/widget/TextView.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
import android.view.inputmethod.InputConnection;
134134
import android.view.inputmethod.InputMethodManager;
135135
import android.view.inputmethod.InputMethodSubtype;
136+
import android.view.textservice.SpellCheckerSubtype;
137+
import android.view.textservice.TextServicesManager;
136138
import android.widget.AdapterView.OnItemClickListener;
137139
import android.widget.RemoteViews.RemoteView;
138140

@@ -8904,21 +8906,18 @@ private boolean selectCurrentWord() {
89048906
/**
89058907
* This is a temporary method. Future versions may support multi-locale text.
89068908
*
8907-
* @return The current locale used in this TextView, based on the current IME's locale,
8908-
* or the system default locale if this is not defined.
8909+
* @return The locale that should be used for a word iterator and a spell checker
8910+
* in this TextView, based on the current spell checker settings,
8911+
* the current IME's locale, or the system default locale.
89098912
* @hide
89108913
*/
8911-
public Locale getLocale() {
8914+
public Locale getTextServicesLocale() {
89128915
Locale locale = Locale.getDefault();
8913-
final InputMethodManager imm = InputMethodManager.peekInstance();
8914-
if (imm != null) {
8915-
final InputMethodSubtype currentInputMethodSubtype = imm.getCurrentInputMethodSubtype();
8916-
if (currentInputMethodSubtype != null) {
8917-
String localeString = currentInputMethodSubtype.getLocale();
8918-
if (!TextUtils.isEmpty(localeString)) {
8919-
locale = new Locale(localeString);
8920-
}
8921-
}
8916+
final TextServicesManager textServicesManager = (TextServicesManager)
8917+
mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
8918+
final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true);
8919+
if (subtype != null) {
8920+
locale = new Locale(subtype.getLocale());
89228921
}
89238922
return locale;
89248923
}
@@ -8933,7 +8932,7 @@ void onLocaleChanged() {
89338932
*/
89348933
public WordIterator getWordIterator() {
89358934
if (mWordIterator == null) {
8936-
mWordIterator = new WordIterator(getLocale());
8935+
mWordIterator = new WordIterator(getTextServicesLocale());
89378936
}
89388937
return mWordIterator;
89398938
}

services/java/com/android/server/TextServicesManagerService.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import android.service.textservice.SpellCheckerService;
4141
import android.text.TextUtils;
4242
import android.util.Slog;
43+
import android.view.inputmethod.InputMethodManager;
44+
import android.view.inputmethod.InputMethodSubtype;
4345
import android.view.textservice.SpellCheckerInfo;
4446
import android.view.textservice.SpellCheckerSubtype;
4547

@@ -222,20 +224,40 @@ public SpellCheckerSubtype getCurrentSpellCheckerSubtype(
222224
if (hashCode == 0 && !allowImplicitlySelectedSubtype) {
223225
return null;
224226
}
225-
final String systemLocale =
226-
mContext.getResources().getConfiguration().locale.toString();
227+
String candidateLocale = null;
228+
if (hashCode == 0) {
229+
// Spell checker language settings == "auto"
230+
final InputMethodManager imm =
231+
(InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
232+
if (imm != null) {
233+
final InputMethodSubtype currentInputMethodSubtype =
234+
imm.getCurrentInputMethodSubtype();
235+
if (currentInputMethodSubtype != null) {
236+
final String localeString = currentInputMethodSubtype.getLocale();
237+
if (!TextUtils.isEmpty(localeString)) {
238+
// 1. Use keyboard locale if available in the spell checker
239+
candidateLocale = localeString;
240+
}
241+
}
242+
}
243+
if (candidateLocale == null) {
244+
// 2. Use System locale if available in the spell checker
245+
candidateLocale = mContext.getResources().getConfiguration().locale.toString();
246+
}
247+
}
227248
SpellCheckerSubtype candidate = null;
228249
for (int i = 0; i < sci.getSubtypeCount(); ++i) {
229250
final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
230251
if (hashCode == 0) {
231-
if (systemLocale.equals(locale)) {
252+
if (candidateLocale.equals(locale)) {
232253
return scs;
233254
} else if (candidate == null) {
234255
final String scsLocale = scs.getLocale();
235-
if (systemLocale.length() >= 2
256+
if (candidateLocale.length() >= 2
236257
&& scsLocale.length() >= 2
237-
&& systemLocale.substring(0, 2).equals(
258+
&& candidateLocale.substring(0, 2).equals(
238259
scsLocale.substring(0, 2))) {
260+
// Fall back to the applicable language
239261
candidate = scs;
240262
}
241263
}
@@ -244,9 +266,13 @@ public SpellCheckerSubtype getCurrentSpellCheckerSubtype(
244266
Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
245267
+ ", " + scs.getLocale());
246268
}
269+
// 3. Use the user specified spell check language
247270
return scs;
248271
}
249272
}
273+
// 4. Fall back to the applicable language and return it if not null
274+
// 5. Simply just return it even if it's null which means we could find no suitable
275+
// spell check languages
250276
return candidate;
251277
}
252278
}

0 commit comments

Comments
 (0)