Use Intent in preference to invoke subtype enabler

This CL also allows resetting a title and an icon of subtype enabler.

Change-Id: I1de10557aeae9d2185ea0560ba8fdd9889248459
diff --git a/java/com/android/inputmethodcommon/InputMethodSettingsImpl.java b/java/com/android/inputmethodcommon/InputMethodSettingsImpl.java
index 8c209d7..cfa1a65 100644
--- a/java/com/android/inputmethodcommon/InputMethodSettingsImpl.java
+++ b/java/com/android/inputmethodcommon/InputMethodSettingsImpl.java
@@ -40,7 +40,6 @@
     private Drawable mSubtypeEnablerIcon;
     private InputMethodManager mImm;
     private InputMethodInfo mImi;
-    private Context mContext;
 
     /**
      * Initialize internal states of this object.
@@ -49,31 +48,18 @@
      * @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) {
-        mContext = context;
         mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
         mImi = getMyImi(context, mImm);
         if (mImi == null || mImi.getSubtypeCount() <= 1) {
             return false;
         }
+        final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
+        intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi.getId());
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
+                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
+                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
         mSubtypeEnablerPreference = new Preference(context);
-        mSubtypeEnablerPreference
-                .setOnPreferenceClickListener(new OnPreferenceClickListener() {
-                    @Override
-                    public boolean onPreferenceClick(Preference preference) {
-                        final CharSequence title = getSubtypeEnablerTitle(context);
-                        final Intent intent =
-                                new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
-                        intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi.getId());
-                        if (!TextUtils.isEmpty(title)) {
-                            intent.putExtra(Intent.EXTRA_TITLE, title);
-                        }
-                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
-                                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
-                                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
-                        context.startActivity(intent);
-                        return true;
-                    }
-                });
+        mSubtypeEnablerPreference.setIntent(intent);
         prefScreen.addPreference(mSubtypeEnablerPreference);
         updateSubtypeEnabler();
         return true;
@@ -163,30 +149,31 @@
         updateSubtypeEnabler();
     }
 
-    private CharSequence getSubtypeEnablerTitle(Context context) {
-        if (mSubtypeEnablerTitleRes != 0) {
-            return context.getString(mSubtypeEnablerTitleRes);
-        } else {
-            return mSubtypeEnablerTitle;
-        }
-    }
-
     public void updateSubtypeEnabler() {
-        if (mSubtypeEnablerPreference != null) {
-            if (mSubtypeEnablerTitleRes != 0) {
-                mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitleRes);
-            } else if (!TextUtils.isEmpty(mSubtypeEnablerTitle)) {
-                mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitle);
-            }
-            final String summary = getEnabledSubtypesLabel(mContext, mImm, mImi);
-            if (!TextUtils.isEmpty(summary)) {
-                mSubtypeEnablerPreference.setSummary(summary);
-            }
-            if (mSubtypeEnablerIconRes != 0) {
-                mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIconRes);
-            } else if (mSubtypeEnablerIcon != null) {
-                mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIcon);
-            }
+        final Preference pref = mSubtypeEnablerPreference;
+        if (pref == null) {
+            return;
+        }
+        final Context context = pref.getContext();
+        final CharSequence title;
+        if (mSubtypeEnablerTitleRes != 0) {
+            title = context.getString(mSubtypeEnablerTitleRes);
+        } else {
+            title = mSubtypeEnablerTitle;
+        }
+        pref.setTitle(title);
+        final Intent intent = pref.getIntent();
+        if (intent != null) {
+            intent.putExtra(Intent.EXTRA_TITLE, title);
+        }
+        final String summary = getEnabledSubtypesLabel(context, mImm, mImi);
+        if (!TextUtils.isEmpty(summary)) {
+            pref.setSummary(summary);
+        }
+        if (mSubtypeEnablerIconRes != 0) {
+            pref.setIcon(mSubtypeEnablerIconRes);
+        } else {
+            pref.setIcon(mSubtypeEnablerIcon);
         }
     }
 }