4040import android .service .textservice .SpellCheckerService ;
4141import android .text .TextUtils ;
4242import android .util .Slog ;
43+ import android .view .inputmethod .InputMethodManager ;
44+ import android .view .inputmethod .InputMethodSubtype ;
4345import android .view .textservice .SpellCheckerInfo ;
4446import 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