@@ -173,7 +173,7 @@ private SpellCheckerInfo findAvailSpellCheckerLocked(String locale, String prefP
173173 @ Override
174174 public SpellCheckerInfo getCurrentSpellChecker (String locale ) {
175175 synchronized (mSpellCheckerMap ) {
176- String curSpellCheckerId =
176+ final String curSpellCheckerId =
177177 Settings .Secure .getString (mContext .getContentResolver (),
178178 Settings .Secure .SELECTED_SPELL_CHECKER );
179179 if (DBG ) {
@@ -197,20 +197,32 @@ public SpellCheckerSubtype getCurrentSpellCheckerSubtype(String locale) {
197197 Slog .w (TAG , "getCurrentSpellChecker: " + subtypeHashCodeStr );
198198 }
199199 final SpellCheckerInfo sci = getCurrentSpellChecker (null );
200- if (sci .getSubtypeCount () == 0 ) {
200+ if (sci == null || sci .getSubtypeCount () == 0 ) {
201+ if (DBG ) {
202+ Slog .w (TAG , "Subtype not found." );
203+ }
201204 return null ;
202205 }
203206 if (TextUtils .isEmpty (subtypeHashCodeStr )) {
207+ if (DBG ) {
208+ Slog .w (TAG , "Return first subtype in " + sci .getId ());
209+ }
204210 // Return the first Subtype if there is no settings for the current subtype.
205211 return sci .getSubtypeAt (0 );
206212 }
207213 final int hashCode = Integer .valueOf (subtypeHashCodeStr );
208214 for (int i = 0 ; i < sci .getSubtypeCount (); ++i ) {
209215 final SpellCheckerSubtype scs = sci .getSubtypeAt (i );
210216 if (scs .hashCode () == hashCode ) {
217+ if (DBG ) {
218+ Slog .w (TAG , "Return subtype " + scs .hashCode ());
219+ }
211220 return scs ;
212221 }
213222 }
223+ if (DBG ) {
224+ Slog .w (TAG , "Return first subtype in " + sci .getId ());
225+ }
214226 return sci .getSubtypeAt (0 );
215227 }
216228 }
@@ -283,6 +295,13 @@ public void getSpellCheckerService(String sciId, String locale,
283295 return ;
284296 }
285297
298+ @ Override
299+ public boolean isSpellCheckerEnabled () {
300+ synchronized (mSpellCheckerMap ) {
301+ return isSpellCheckerEnabledLocked ();
302+ }
303+ }
304+
286305 private void startSpellCheckerServiceInnerLocked (SpellCheckerInfo info , String locale ,
287306 ITextServicesSessionListener tsListener , ISpellCheckerSessionListener scListener ,
288307 int uid , Bundle bundle ) {
@@ -354,7 +373,21 @@ public void setCurrentSpellCheckerSubtype(String locale, int hashCode) {
354373 "Requires permission "
355374 + android .Manifest .permission .WRITE_SECURE_SETTINGS );
356375 }
357- setCurrentSpellCheckerLocked (hashCode );
376+ setCurrentSpellCheckerSubtypeLocked (hashCode );
377+ }
378+ }
379+
380+ @ Override
381+ public void setSpellCheckerEnabled (boolean enabled ) {
382+ synchronized (mSpellCheckerMap ) {
383+ if (mContext .checkCallingOrSelfPermission (
384+ android .Manifest .permission .WRITE_SECURE_SETTINGS )
385+ != PackageManager .PERMISSION_GRANTED ) {
386+ throw new SecurityException (
387+ "Requires permission "
388+ + android .Manifest .permission .WRITE_SECURE_SETTINGS );
389+ }
390+ setSpellCheckerEnabledLocked (enabled );
358391 }
359392 }
360393
@@ -372,7 +405,7 @@ private void setCurrentSpellCheckerLocked(String sciId) {
372405 }
373406 }
374407
375- private void setCurrentSpellCheckerLocked (int hashCode ) {
408+ private void setCurrentSpellCheckerSubtypeLocked (int hashCode ) {
376409 if (DBG ) {
377410 Slog .w (TAG , "setCurrentSpellCheckerSubtype: " + hashCode );
378411 }
@@ -397,6 +430,33 @@ private void setCurrentSpellCheckerLocked(int hashCode) {
397430 }
398431 }
399432
433+ private void setSpellCheckerEnabledLocked (boolean enabled ) {
434+ if (DBG ) {
435+ Slog .w (TAG , "setSpellCheckerEnabled: " + enabled );
436+ }
437+ final long ident = Binder .clearCallingIdentity ();
438+ try {
439+ Settings .Secure .putInt (mContext .getContentResolver (),
440+ Settings .Secure .SPELL_CHECKER_ENABLED , enabled ? 1 : 0 );
441+ } finally {
442+ Binder .restoreCallingIdentity (ident );
443+ }
444+ }
445+
446+ private boolean isSpellCheckerEnabledLocked () {
447+ final long ident = Binder .clearCallingIdentity ();
448+ try {
449+ final boolean retval = Settings .Secure .getInt (mContext .getContentResolver (),
450+ Settings .Secure .SPELL_CHECKER_ENABLED , 1 ) == 1 ;
451+ if (DBG ) {
452+ Slog .w (TAG , "getSpellCheckerEnabled: " + retval );
453+ }
454+ return retval ;
455+ } finally {
456+ Binder .restoreCallingIdentity (ident );
457+ }
458+ }
459+
400460 // SpellCheckerBindGroup contains active text service session listeners.
401461 // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from
402462 // mSpellCheckerBindGroups
0 commit comments