OSDN Git Service

Remove an unnecessary int to String conversion.
authorYohei Yukawa <yukawa@google.com>
Thu, 17 Mar 2016 00:22:27 +0000 (17:22 -0700)
committerYohei Yukawa <yukawa@google.com>
Thu, 17 Mar 2016 00:22:27 +0000 (17:22 -0700)
This is a safe refactoring to remove an unnecessary int to String
conversion in TextServicesSettings.

Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE is a integer value that
indicates subtype ID (or SpellCheckerSubtype#hashCode() if the subtype
ID is not specified), and we can just rely on
Settings.Secure#putIntForUser() rather than converting int to String
by ourselves then pass it to Settings.Secure#putStringForUser().

Note that this change is still OK for existing users because
Settings.Secure#putIntForUser() has been internally doing exactly the
same thing.

Bug: 27687531
Change-Id: Ibcf12746f1295c12bec095300ea7f6ced0a51d09

core/java/android/view/textservice/SpellCheckerSubtype.java
services/core/java/com/android/server/TextServicesManagerService.java

index df33698..35d3bf9 100644 (file)
@@ -50,7 +50,10 @@ public final class SpellCheckerSubtype implements Parcelable {
     private static final String TAG = SpellCheckerSubtype.class.getSimpleName();
     private static final String EXTRA_VALUE_PAIR_SEPARATOR = ",";
     private static final String EXTRA_VALUE_KEY_VALUE_SEPARATOR = "=";
-    private static final int SUBTYPE_ID_NONE = 0;
+    /**
+     * @hide
+     */
+    public static final int SUBTYPE_ID_NONE = 0;
     private static final String SUBTYPE_LANGUAGE_TAG_NONE = "";
 
     private final int mSubtypeId;
index 306e933..3da20bf 100644 (file)
@@ -464,9 +464,10 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
             return null;
         }
         synchronized (mSpellCheckerMap) {
-            final String subtypeHashCodeStr = mSettings.getSelectedSpellCheckerSubtype();
+            final int subtypeHashCode =
+                    mSettings.getSelectedSpellCheckerSubtype(SpellCheckerSubtype.SUBTYPE_ID_NONE);
             if (DBG) {
-                Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCodeStr);
+                Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCode);
             }
             final SpellCheckerInfo sci = getCurrentSpellChecker(null);
             if (sci == null || sci.getSubtypeCount() == 0) {
@@ -475,17 +476,12 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
                 }
                 return null;
             }
-            final int hashCode;
-            if (!TextUtils.isEmpty(subtypeHashCodeStr)) {
-                hashCode = Integer.valueOf(subtypeHashCodeStr);
-            } else {
-                hashCode = 0;
-            }
-            if (hashCode == 0 && !allowImplicitlySelectedSubtype) {
+            if (subtypeHashCode == SpellCheckerSubtype.SUBTYPE_ID_NONE
+                    && !allowImplicitlySelectedSubtype) {
                 return null;
             }
             String candidateLocale = null;
-            if (hashCode == 0) {
+            if (subtypeHashCode == 0) {
                 // Spell checker language settings == "auto"
                 final InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
                 if (imm != null) {
@@ -507,7 +503,7 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
             SpellCheckerSubtype candidate = null;
             for (int i = 0; i < sci.getSubtypeCount(); ++i) {
                 final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
-                if (hashCode == 0) {
+                if (subtypeHashCode == 0) {
                     final String scsLocale = scs.getLocale();
                     if (candidateLocale.equals(scsLocale)) {
                         return scs;
@@ -518,7 +514,7 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
                             candidate = scs;
                         }
                     }
-                } else if (scs.hashCode() == hashCode) {
+                } else if (scs.hashCode() == subtypeHashCode) {
                     if (DBG) {
                         Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
                                 + ", " + scs.getLocale());
@@ -1154,7 +1150,7 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
         }
 
         public void putSelectedSpellCheckerSubtype(int hashCode) {
-            putString(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(hashCode));
+            putInt(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, hashCode);
         }
 
         public void setSpellCheckerEnabled(boolean enabled) {
@@ -1165,8 +1161,8 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
             return getString(Settings.Secure.SELECTED_SPELL_CHECKER);
         }
 
-        public String getSelectedSpellCheckerSubtype() {
-            return getString(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE);
+        public int getSelectedSpellCheckerSubtype(final int defaultValue) {
+            return getInt(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, defaultValue);
         }
 
         public boolean isSpellCheckerEnabled() {