Tweak settings

Bug: 5069983

Change-Id: I96002ec726ba2c6c8962bdb02ce63ecf295df90f
diff --git a/Android.mk b/Android.mk
index 7e4d14d..4aebc01 100644
--- a/Android.mk
+++ b/Android.mk
@@ -16,8 +16,7 @@
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := inputmethod-common
-# TODO: Change a sdk version to 14 once the framework gets ready
-LOCAL_SDK_VERSION := 13
+LOCAL_SDK_VERSION := current
 LOCAL_SRC_FILES := \
      $(call all-java-files-under, java)
 include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/java/com/android/inputmethodcommon/InputMethodSettingsActivity.java b/java/com/android/inputmethodcommon/InputMethodSettingsActivity.java
index f7d00e1..6e868c9 100644
--- a/java/com/android/inputmethodcommon/InputMethodSettingsActivity.java
+++ b/java/com/android/inputmethodcommon/InputMethodSettingsActivity.java
@@ -71,22 +71,6 @@
      * {@inheritDoc}
      */
     @Override
-    public void setSubtypeEnablerSummary(int resId) {
-        mSettings.setSubtypeEnablerSummary(resId);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void setSubtypeEnablerSummary(CharSequence summary) {
-        mSettings.setSubtypeEnablerSummary(summary);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
     public void setSubtypeEnablerIcon(int resId) {
         mSettings.setSubtypeEnablerIcon(resId);
     }
@@ -98,4 +82,13 @@
     public void setSubtypeEnablerIcon(Drawable drawable) {
         mSettings.setSubtypeEnablerIcon(drawable);
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onResume() {
+        super.onResume();
+        mSettings.updateSubtypeEnabler();
+    }
 }
diff --git a/java/com/android/inputmethodcommon/InputMethodSettingsFragment.java b/java/com/android/inputmethodcommon/InputMethodSettingsFragment.java
index 7b5c793..49f0b87 100644
--- a/java/com/android/inputmethodcommon/InputMethodSettingsFragment.java
+++ b/java/com/android/inputmethodcommon/InputMethodSettingsFragment.java
@@ -72,22 +72,6 @@
      * {@inheritDoc}
      */
     @Override
-    public void setSubtypeEnablerSummary(int resId) {
-        mSettings.setSubtypeEnablerSummary(resId);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void setSubtypeEnablerSummary(CharSequence summary) {
-        mSettings.setSubtypeEnablerSummary(summary);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
     public void setSubtypeEnablerIcon(int resId) {
         mSettings.setSubtypeEnablerIcon(resId);
     }
@@ -99,4 +83,13 @@
     public void setSubtypeEnablerIcon(Drawable drawable) {
         mSettings.setSubtypeEnablerIcon(drawable);
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onResume() {
+        super.onResume();
+        mSettings.updateSubtypeEnabler();
+    }
 }
diff --git a/java/com/android/inputmethodcommon/InputMethodSettingsImpl.java b/java/com/android/inputmethodcommon/InputMethodSettingsImpl.java
index 5d11205..8c209d7 100644
--- a/java/com/android/inputmethodcommon/InputMethodSettingsImpl.java
+++ b/java/com/android/inputmethodcommon/InputMethodSettingsImpl.java
@@ -21,12 +21,12 @@
 import android.graphics.drawable.Drawable;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceClickListener;
-import android.preference.PreferenceCategory;
 import android.preference.PreferenceScreen;
 import android.provider.Settings;
 import android.text.TextUtils;
 import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodManager;
+import android.view.inputmethod.InputMethodSubtype;
 
 import java.util.List;
 
@@ -36,10 +36,11 @@
     private CharSequence mInputMethodSettingsCategoryTitle;
     private int mSubtypeEnablerTitleRes;
     private CharSequence mSubtypeEnablerTitle;
-    private int mSubtypeEnablerSummaryRes;
-    private CharSequence mSubtypeEnablerSummary;
     private int mSubtypeEnablerIconRes;
     private Drawable mSubtypeEnablerIcon;
+    private InputMethodManager mImm;
+    private InputMethodInfo mImi;
+    private Context mContext;
 
     /**
      * Initialize internal states of this object.
@@ -48,10 +49,10 @@
      * @return true if this application is an IME and has two or more subtypes, false otherwise.
      */
     public boolean init(final Context context, final PreferenceScreen prefScreen) {
-        final InputMethodManager imm =
-                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
-        final InputMethodInfo imi = getMyImi(context, imm);
-        if (imi == null || imi.getSubtypeCount() <= 1) {
+        mContext = context;
+        mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
+        mImi = getMyImi(context, mImm);
+        if (mImi == null || mImi.getSubtypeCount() <= 1) {
             return false;
         }
         mSubtypeEnablerPreference = new Preference(context);
@@ -62,7 +63,7 @@
                         final CharSequence title = getSubtypeEnablerTitle(context);
                         final Intent intent =
                                 new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
-                        intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, imi.getId());
+                        intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi.getId());
                         if (!TextUtils.isEmpty(title)) {
                             intent.putExtra(Intent.EXTRA_TITLE, title);
                         }
@@ -89,6 +90,22 @@
         return null;
     }
 
+    private static String getEnabledSubtypesLabel(
+            Context context, InputMethodManager imm, InputMethodInfo imi) {
+        if (context == null || imm == null || imi == null) return null;
+        final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true);
+        final StringBuilder sb = new StringBuilder();
+        final int N = subtypes.size();
+        for (int i = 0; i < N; ++i) {
+            final InputMethodSubtype subtype = subtypes.get(i);
+            if (sb.length() > 0) {
+                sb.append(", ");
+            }
+            sb.append(subtype.getDisplayName(context, imi.getPackageName(),
+                    imi.getServiceInfo().applicationInfo));
+        }
+        return sb.toString();
+    }
     /**
      * {@inheritDoc}
      */
@@ -131,25 +148,6 @@
      * {@inheritDoc}
      */
     @Override
-    public void setSubtypeEnablerSummary(int resId) {
-        mSubtypeEnablerSummaryRes = resId;
-        updateSubtypeEnabler();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void setSubtypeEnablerSummary(CharSequence summary) {
-        mSubtypeEnablerSummaryRes = 0;
-        mSubtypeEnablerSummary = summary;
-        updateSubtypeEnabler();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
     public void setSubtypeEnablerIcon(int resId) {
         mSubtypeEnablerIconRes = resId;
         updateSubtypeEnabler();
@@ -173,17 +171,16 @@
         }
     }
 
-    private void updateSubtypeEnabler() {
+    public void updateSubtypeEnabler() {
         if (mSubtypeEnablerPreference != null) {
             if (mSubtypeEnablerTitleRes != 0) {
                 mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitleRes);
             } else if (!TextUtils.isEmpty(mSubtypeEnablerTitle)) {
                 mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitle);
             }
-            if (mSubtypeEnablerSummaryRes != 0) {
-                mSubtypeEnablerPreference.setSummary(mSubtypeEnablerSummaryRes);
-            } else if (!TextUtils.isEmpty(mSubtypeEnablerSummary)) {
-                mSubtypeEnablerPreference.setSummary(mSubtypeEnablerSummary);
+            final String summary = getEnabledSubtypesLabel(mContext, mImm, mImi);
+            if (!TextUtils.isEmpty(summary)) {
+                mSubtypeEnablerPreference.setSummary(summary);
             }
             if (mSubtypeEnablerIconRes != 0) {
                 mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIconRes);
diff --git a/java/com/android/inputmethodcommon/InputMethodSettingsInterface.java b/java/com/android/inputmethodcommon/InputMethodSettingsInterface.java
index 8e0f027..6e97fae 100644
--- a/java/com/android/inputmethodcommon/InputMethodSettingsInterface.java
+++ b/java/com/android/inputmethodcommon/InputMethodSettingsInterface.java
@@ -50,20 +50,6 @@
     public void setSubtypeEnablerTitle(CharSequence title);
 
     /**
-     * Sets the summary for the inputmethod enabler preference for launching subtype enabler with a
-     * resource ID.
-     * @param resId The resource id of the summary for the preference.
-     */
-    public void setSubtypeEnablerSummary(int resId);
-
-    /**
-     * Sets the summary for the inputmethod enabler preference for launching subtype enabler with a
-     * CharSequence.
-     * @param summary The summary of the preference.
-     */
-    public void setSubtypeEnablerSummary(CharSequence summary);
-
-    /**
      * Sets the icon for the preference for launching subtype enabler with a resource ID.
      * @param resId The resource id of an optional icon for the preference.
      */