4141import android .text .TextUtils ;
4242import android .util .Slog ;
4343import android .view .textservice .SpellCheckerInfo ;
44+ import android .view .textservice .SpellCheckerSubtype ;
4445
4546import java .io .IOException ;
4647import java .util .ArrayList ;
@@ -174,7 +175,7 @@ public SpellCheckerInfo getCurrentSpellChecker(String locale) {
174175 synchronized (mSpellCheckerMap ) {
175176 String curSpellCheckerId =
176177 Settings .Secure .getString (mContext .getContentResolver (),
177- Settings .Secure .SPELL_CHECKER_SERVICE );
178+ Settings .Secure .SELECTED_SPELL_CHECKER );
178179 if (DBG ) {
179180 Slog .w (TAG , "getCurrentSpellChecker: " + curSpellCheckerId );
180181 }
@@ -185,6 +186,35 @@ public SpellCheckerInfo getCurrentSpellChecker(String locale) {
185186 }
186187 }
187188
189+ // TODO: Save SpellCheckerSubtype by supported languages.
190+ @ Override
191+ public SpellCheckerSubtype getCurrentSpellCheckerSubtype (String locale ) {
192+ synchronized (mSpellCheckerMap ) {
193+ final String subtypeHashCodeStr =
194+ Settings .Secure .getString (mContext .getContentResolver (),
195+ Settings .Secure .SELECTED_SPELL_CHECKER_SUBTYPE );
196+ if (DBG ) {
197+ Slog .w (TAG , "getCurrentSpellChecker: " + subtypeHashCodeStr );
198+ }
199+ final SpellCheckerInfo sci = getCurrentSpellChecker (null );
200+ if (sci .getSubtypeCount () == 0 ) {
201+ return null ;
202+ }
203+ if (TextUtils .isEmpty (subtypeHashCodeStr )) {
204+ // Return the first Subtype if there is no settings for the current subtype.
205+ return sci .getSubtypeAt (0 );
206+ }
207+ final int hashCode = Integer .valueOf (subtypeHashCodeStr );
208+ for (int i = 0 ; i < sci .getSubtypeCount (); ++i ) {
209+ final SpellCheckerSubtype scs = sci .getSubtypeAt (i );
210+ if (scs .hashCode () == hashCode ) {
211+ return scs ;
212+ }
213+ }
214+ return sci .getSubtypeAt (0 );
215+ }
216+ }
217+
188218 @ Override
189219 public void getSpellCheckerService (String sciId , String locale ,
190220 ITextServicesSessionListener tsListener , ISpellCheckerSessionListener scListener ,
@@ -301,7 +331,7 @@ public void finishSpellCheckerService(ISpellCheckerSessionListener listener) {
301331 }
302332
303333 @ Override
304- public void setCurrentSpellChecker (String sciId ) {
334+ public void setCurrentSpellChecker (String locale , String sciId ) {
305335 synchronized (mSpellCheckerMap ) {
306336 if (mContext .checkCallingOrSelfPermission (
307337 android .Manifest .permission .WRITE_SECURE_SETTINGS )
@@ -314,6 +344,20 @@ public void setCurrentSpellChecker(String sciId) {
314344 }
315345 }
316346
347+ @ Override
348+ public void setCurrentSpellCheckerSubtype (String locale , int hashCode ) {
349+ synchronized (mSpellCheckerMap ) {
350+ if (mContext .checkCallingOrSelfPermission (
351+ android .Manifest .permission .WRITE_SECURE_SETTINGS )
352+ != PackageManager .PERMISSION_GRANTED ) {
353+ throw new SecurityException (
354+ "Requires permission "
355+ + android .Manifest .permission .WRITE_SECURE_SETTINGS );
356+ }
357+ setCurrentSpellCheckerLocked (hashCode );
358+ }
359+ }
360+
317361 private void setCurrentSpellCheckerLocked (String sciId ) {
318362 if (DBG ) {
319363 Slog .w (TAG , "setCurrentSpellChecker: " + sciId );
@@ -322,7 +366,32 @@ private void setCurrentSpellCheckerLocked(String sciId) {
322366 final long ident = Binder .clearCallingIdentity ();
323367 try {
324368 Settings .Secure .putString (mContext .getContentResolver (),
325- Settings .Secure .SPELL_CHECKER_SERVICE , sciId );
369+ Settings .Secure .SELECTED_SPELL_CHECKER , sciId );
370+ } finally {
371+ Binder .restoreCallingIdentity (ident );
372+ }
373+ }
374+
375+ private void setCurrentSpellCheckerLocked (int hashCode ) {
376+ if (DBG ) {
377+ Slog .w (TAG , "setCurrentSpellCheckerSubtype: " + hashCode );
378+ }
379+ final SpellCheckerInfo sci = getCurrentSpellChecker (null );
380+ if (sci == null ) return ;
381+ boolean found = false ;
382+ for (int i = 0 ; i < sci .getSubtypeCount (); ++i ) {
383+ if (sci .getSubtypeAt (i ).hashCode () == hashCode ) {
384+ found = true ;
385+ break ;
386+ }
387+ }
388+ if (!found ) {
389+ return ;
390+ }
391+ final long ident = Binder .clearCallingIdentity ();
392+ try {
393+ Settings .Secure .putString (mContext .getContentResolver (),
394+ Settings .Secure .SELECTED_SPELL_CHECKER_SUBTYPE , String .valueOf (hashCode ));
326395 } finally {
327396 Binder .restoreCallingIdentity (ident );
328397 }
0 commit comments