Update the current IME label

Bug: 5069983
Change-Id: I84d6ac7c39b5f3a035aa04c35065280a14024c47
diff --git a/res/layout/preference_inputmethod.xml b/res/layout/preference_inputmethod.xml
index 0edc060..0e0c1ef 100644
--- a/res/layout/preference_inputmethod.xml
+++ b/res/layout/preference_inputmethod.xml
@@ -33,13 +33,11 @@
             android:id="@android:id/widget_frame"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
-            android:layout_marginLeft="10dip"
             android:gravity="center_vertical"
             android:orientation="vertical" />
         <RelativeLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_marginLeft="15dip"
             android:layout_marginRight="6dip"
             android:layout_marginTop="6dip"
             android:layout_marginBottom="6dip"
diff --git a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
index 65e9169..4454389 100644
--- a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
+++ b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
@@ -19,16 +19,18 @@
 import com.android.settings.R;
 import com.android.settings.Settings.SpellCheckersSettingsActivity;
 import com.android.settings.SettingsPreferenceFragment;
-import com.android.settings.UserDictionarySettings;
 import com.android.settings.Utils;
 import com.android.settings.VoiceInputOutputSettings;
 
 import android.app.Activity;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.res.Configuration;
+import android.database.ContentObserver;
 import android.os.Bundle;
+import android.os.Handler;
 import android.preference.CheckBoxPreference;
 import android.preference.ListPreference;
 import android.preference.Preference;
@@ -74,6 +76,9 @@
     private InputMethodManager mImm;
     private List<InputMethodInfo> mImis;
     private boolean mIsOnlyImeSettings;
+    private Handler mHandler;
+    @SuppressWarnings("unused")
+    private SettingsObserver mSettingsObserver;
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -122,6 +127,9 @@
         if (scp != null) {
             scp.setFragmentIntent(this, intent);
         }
+
+        mHandler = new Handler();
+        mSettingsObserver = new SettingsObserver(mHandler, getActivity());
     }
 
     private void updateInputMethodSelectorSummary(int value) {
@@ -269,6 +277,22 @@
                 ((InputMethodPreference)pref).updateSummary();
             }
         }
+        updateCurrentImeName();
+    }
+
+    private void updateCurrentImeName() {
+        final Context context = getActivity();
+        if (context == null || mImm == null) return;
+        final Preference curPref = getPreferenceScreen().findPreference(KEY_CURRENT_INPUT_METHOD);
+        if (curPref != null) {
+            final CharSequence curIme = InputMethodAndSubtypeUtil.getCurrentInputMethodName(
+                    context, getContentResolver(), mImm, mImis, getPackageManager());
+            if (!TextUtils.isEmpty(curIme)) {
+                synchronized(this) {
+                    curPref.setSummary(curIme);
+                }
+            }
+        }
     }
 
     private InputMethodPreference getInputMethodPreference(InputMethodInfo imi, int imiSize) {
@@ -333,4 +357,19 @@
             root.addPreference(mInputMethodPreferenceList.get(i));
         }
     }
+
+    private class SettingsObserver extends ContentObserver {
+        public SettingsObserver(Handler handler, Context context) {
+            super(handler);
+            final ContentResolver cr = context.getContentResolver();
+            cr.registerContentObserver(
+                    Settings.Secure.getUriFor(Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
+            cr.registerContentObserver(Settings.Secure.getUriFor(
+                    Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
+        }
+
+        @Override public void onChange(boolean selfChange) {
+            updateCurrentImeName();
+        }
+    }
 }
diff --git a/src/com/android/settings/inputmethod/InputMethodAndSubtypeUtil.java b/src/com/android/settings/inputmethod/InputMethodAndSubtypeUtil.java
index 413b5b4..df58ec0 100644
--- a/src/com/android/settings/inputmethod/InputMethodAndSubtypeUtil.java
+++ b/src/com/android/settings/inputmethod/InputMethodAndSubtypeUtil.java
@@ -19,7 +19,9 @@
 import com.android.settings.SettingsPreferenceFragment;
 
 import android.content.ContentResolver;
+import android.content.Context;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.preference.CheckBoxPreference;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
@@ -28,6 +30,7 @@
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodManager;
 import android.view.inputmethod.InputMethodSubtype;
 
 import java.util.HashMap;
@@ -148,6 +151,28 @@
         return set;
     }
 
+    public static CharSequence getCurrentInputMethodName(Context context, ContentResolver resolver,
+            InputMethodManager imm, List<InputMethodInfo> imis, PackageManager pm) {
+        if (resolver == null || imis == null) return null;
+        final String currentInputMethodId = Settings.Secure.getString(resolver,
+                Settings.Secure.DEFAULT_INPUT_METHOD);
+        if (TextUtils.isEmpty(currentInputMethodId)) return null;
+        for (InputMethodInfo imi : imis) {
+            if (currentInputMethodId.equals(imi.getId())) {
+                final InputMethodSubtype subtype = imm.getCurrentInputMethodSubtype();
+                final CharSequence imiLabel = imi.loadLabel(pm);
+                final CharSequence summary = subtype != null
+                        ? TextUtils.concat(subtype.getDisplayName(context,
+                                    imi.getPackageName(), imi.getServiceInfo().applicationInfo),
+                                            (TextUtils.isEmpty(imiLabel) ?
+                                                    "" : " - " + imiLabel))
+                        : imiLabel;
+                return summary;
+            }
+        }
+        return null;
+    }
+
     public static void saveInputMethodSubtypeList(SettingsPreferenceFragment context,
             ContentResolver resolver, List<InputMethodInfo> inputMethodInfos,
             boolean hasHardKeyboard) {