@@ -2619,7 +2619,11 @@ private int getSelectedInputMethodSubtypeId(String id) {
26192619 return getSubtypeIdFromHashCode (imi , subtypeId );
26202620 }
26212621
2622- private int getSubtypeIdFromHashCode (InputMethodInfo imi , int subtypeHashCode ) {
2622+ private static boolean isValidSubtypeId (InputMethodInfo imi , int subtypeHashCode ) {
2623+ return getSubtypeIdFromHashCode (imi , subtypeHashCode ) != NOT_A_SUBTYPE_ID ;
2624+ }
2625+
2626+ private static int getSubtypeIdFromHashCode (InputMethodInfo imi , int subtypeHashCode ) {
26232627 if (imi != null ) {
26242628 final int subtypeCount = imi .getSubtypeCount ();
26252629 for (int i = 0 ; i < subtypeCount ; ++i ) {
@@ -2844,43 +2848,45 @@ private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
28442848 */
28452849 @ Override
28462850 public InputMethodSubtype getCurrentInputMethodSubtype () {
2851+ if (mCurMethodId == null ) {
2852+ return null ;
2853+ }
28472854 boolean subtypeIsSelected = false ;
28482855 try {
28492856 subtypeIsSelected = Settings .Secure .getInt (mContext .getContentResolver (),
28502857 Settings .Secure .SELECTED_INPUT_METHOD_SUBTYPE ) != NOT_A_SUBTYPE_ID ;
28512858 } catch (SettingNotFoundException e ) {
28522859 }
28532860 synchronized (mMethodMap ) {
2854- if (!subtypeIsSelected || mCurrentSubtype == null ) {
2855- String lastInputMethodId = Settings .Secure .getString (
2856- mContext .getContentResolver (), Settings .Secure .DEFAULT_INPUT_METHOD );
2857- int subtypeId = getSelectedInputMethodSubtypeId (lastInputMethodId );
2861+ final InputMethodInfo imi = mMethodMap .get (mCurMethodId );
2862+ if (imi == null || imi .getSubtypeCount () == 0 ) {
2863+ return null ;
2864+ }
2865+ if (!subtypeIsSelected || mCurrentSubtype == null
2866+ || !isValidSubtypeId (imi , mCurrentSubtype .hashCode ())) {
2867+ int subtypeId = getSelectedInputMethodSubtypeId (mCurMethodId );
28582868 if (subtypeId == NOT_A_SUBTYPE_ID ) {
2859- InputMethodInfo imi = mMethodMap .get (lastInputMethodId );
2860- if (imi != null ) {
2861- // If there are no selected subtypes, the framework will try to find
2862- // the most applicable subtype from explicitly or implicitly enabled
2863- // subtypes.
2864- List <InputMethodSubtype > explicitlyOrImplicitlyEnabledSubtypes =
2865- getEnabledInputMethodSubtypeList (imi , true );
2866- // If there is only one explicitly or implicitly enabled subtype,
2867- // just returns it.
2868- if (explicitlyOrImplicitlyEnabledSubtypes .size () == 1 ) {
2869- mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes .get (0 );
2870- } else if (explicitlyOrImplicitlyEnabledSubtypes .size () > 1 ) {
2869+ // If there are no selected subtypes, the framework will try to find
2870+ // the most applicable subtype from explicitly or implicitly enabled
2871+ // subtypes.
2872+ List <InputMethodSubtype > explicitlyOrImplicitlyEnabledSubtypes =
2873+ getEnabledInputMethodSubtypeList (imi , true );
2874+ // If there is only one explicitly or implicitly enabled subtype,
2875+ // just returns it.
2876+ if (explicitlyOrImplicitlyEnabledSubtypes .size () == 1 ) {
2877+ mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes .get (0 );
2878+ } else if (explicitlyOrImplicitlyEnabledSubtypes .size () > 1 ) {
2879+ mCurrentSubtype = findLastResortApplicableSubtypeLocked (
2880+ mRes , explicitlyOrImplicitlyEnabledSubtypes ,
2881+ SUBTYPE_MODE_KEYBOARD , null , true );
2882+ if (mCurrentSubtype == null ) {
28712883 mCurrentSubtype = findLastResortApplicableSubtypeLocked (
2872- mRes , explicitlyOrImplicitlyEnabledSubtypes ,
2873- SUBTYPE_MODE_KEYBOARD , null , true );
2874- if (mCurrentSubtype == null ) {
2875- mCurrentSubtype = findLastResortApplicableSubtypeLocked (
2876- mRes , explicitlyOrImplicitlyEnabledSubtypes , null , null ,
2877- true );
2878- }
2884+ mRes , explicitlyOrImplicitlyEnabledSubtypes , null , null ,
2885+ true );
28792886 }
28802887 }
28812888 } else {
2882- mCurrentSubtype =
2883- getSubtypes (mMethodMap .get (lastInputMethodId )).get (subtypeId );
2889+ mCurrentSubtype = getSubtypes (imi ).get (subtypeId );
28842890 }
28852891 }
28862892 return mCurrentSubtype ;
@@ -2979,7 +2985,7 @@ public ImeSubtypeListItem getNextInputMethod(
29792985 }
29802986 final int N = imList .size ();
29812987 final int currentSubtypeId = subtype != null
2982- ? mImms . getSubtypeIdFromHashCode (imi , subtype .hashCode ())
2988+ ? getSubtypeIdFromHashCode (imi , subtype .hashCode ())
29832989 : NOT_A_SUBTYPE_ID ;
29842990 for (int i = 0 ; i < N ; ++i ) {
29852991 final ImeSubtypeListItem isli = imList .get (i );
@@ -3356,10 +3362,10 @@ private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair
33563362 for (Pair <String , ArrayList <String >> enabledIme : enabledImes ) {
33573363 if (enabledIme .first .equals (imeId )) {
33583364 final ArrayList <String > explicitlyEnabledSubtypes = enabledIme .second ;
3365+ final InputMethodInfo imi = mMethodMap .get (imeId );
33593366 if (explicitlyEnabledSubtypes .size () == 0 ) {
33603367 // If there are no explicitly enabled subtypes, applicable subtypes are
33613368 // enabled implicitly.
3362- InputMethodInfo imi = mMethodMap .get (imeId );
33633369 // If IME is enabled and no subtypes are enabled, applicable subtypes
33643370 // are enabled implicitly, so needs to treat them to be enabled.
33653371 if (imi != null && imi .getSubtypeCount () > 0 ) {
@@ -3379,7 +3385,17 @@ private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair
33793385 for (String s : explicitlyEnabledSubtypes ) {
33803386 if (s .equals (subtypeHashCode )) {
33813387 // If both imeId and subtypeId are enabled, return subtypeId.
3382- return s ;
3388+ try {
3389+ final int hashCode = Integer .valueOf (subtypeHashCode );
3390+ // Check whether the subtype id is valid or not
3391+ if (isValidSubtypeId (imi , hashCode )) {
3392+ return s ;
3393+ } else {
3394+ return NOT_A_SUBTYPE_ID_STR ;
3395+ }
3396+ } catch (NumberFormatException e ) {
3397+ return NOT_A_SUBTYPE_ID_STR ;
3398+ }
33833399 }
33843400 }
33853401 }
0 commit comments