Show Subtype Icon properly in the system status bar

- Added API for getting the current subtype
- Added functions for show/hide status icon

Change-Id: Ifcaad00f7f4c658cdb3af367387476bbf316eb19
diff --git a/api/current.xml b/api/current.xml
index 86ed7b8..1ca51c2 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -218267,6 +218267,17 @@
 <parameter name="completions" type="android.view.inputmethod.CompletionInfo[]">
 </parameter>
 </method>
+<method name="getCurrentInputMethodSubtype"
+ return="android.view.inputmethod.InputMethodSubtype"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
 <method name="getEnabledInputMethodList"
  return="java.util.List&lt;android.view.inputmethod.InputMethodInfo&gt;"
  abstract="false"
diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java
index 8e355d6..7cb6291 100644
--- a/core/java/android/view/inputmethod/InputMethodManager.java
+++ b/core/java/android/view/inputmethod/InputMethodManager.java
@@ -33,6 +33,7 @@
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewRoot;
+import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.internal.os.HandlerCaller;
 import com.android.internal.view.IInputConnectionWrapper;
@@ -1411,6 +1412,17 @@
         }
     }
 
+    public InputMethodSubtype getCurrentInputMethodSubtype() {
+        synchronized (mH) {
+            try {
+                return mService.getCurrentInputMethodSubtype();
+            } catch (RemoteException e) {
+                Log.w(TAG, "IME died: " + mCurId, e);
+                return null;
+            }
+        }
+    }
+
     void doDump(FileDescriptor fd, PrintWriter fout, String[] args) {
         final Printer p = new PrintWriterPrinter(fout);
         p.println("Input method client state for " + this + ":");
diff --git a/core/java/com/android/internal/view/IInputMethodManager.aidl b/core/java/com/android/internal/view/IInputMethodManager.aidl
index bffec1d..49ae2bc7 100644
--- a/core/java/com/android/internal/view/IInputMethodManager.aidl
+++ b/core/java/com/android/internal/view/IInputMethodManager.aidl
@@ -18,6 +18,7 @@
 
 import android.os.ResultReceiver;
 import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodSubtype;
 import android.view.inputmethod.EditorInfo;
 import com.android.internal.view.InputBindResult;
 import com.android.internal.view.IInputContext;
@@ -54,6 +55,7 @@
     void hideMySoftInput(in IBinder token, int flags);
     void showMySoftInput(in IBinder token, int flags);
     void updateStatusIcon(in IBinder token, String packageName, int iconId);
+    InputMethodSubtype getCurrentInputMethodSubtype();
     
     boolean setInputMethodEnabled(String id, boolean enabled);
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/InputMethodButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/InputMethodButton.java
index ba682b7..56b4f24 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/InputMethodButton.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/InputMethodButton.java
@@ -17,25 +17,38 @@
 package com.android.systemui.statusbar.tablet;
 
 import android.content.Context;
+import android.content.pm.PackageManager;
+import android.graphics.drawable.Drawable;
+import android.provider.Settings;
+import android.util.Log;
 import android.util.Slog;
-import android.view.View;
 import android.util.AttributeSet;
-import android.widget.ImageView;
+import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodManager;
+import android.view.inputmethod.InputMethodSubtype;
+import android.view.View;
+import android.widget.ImageView;
 
 import com.android.server.InputMethodManagerService;
+import com.android.systemui.R;
+
+import java.util.List;
 
 public class InputMethodButton extends ImageView {
 
+    private static final String  TAG = "StatusBar/InputMethodButton";
+
+    private boolean mKeyboardShown;
+    private ImageView mIcon;
     // other services we wish to talk to
-    InputMethodManager mImm;
+    private InputMethodManager mImm;
 
     public InputMethodButton(Context context, AttributeSet attrs) {
         super(context, attrs);
 
+        mKeyboardShown = false;
         // IME hookup
         mImm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
-        
         // TODO: read the current icon & visibility state directly from the service
 
         // TODO: register for notifications about changes to visibility & subtype from service
@@ -47,5 +60,80 @@
             }
         });
     }
-}
 
+    protected void onAttachedToWindow() {
+        mIcon = (ImageView) findViewById(R.id.imeButton);
+        refreshStatusIcon(mKeyboardShown);
+    }
+
+    private InputMethodInfo getCurrentInputMethodInfo() {
+        String curInputMethodId = Settings.Secure.getString(getContext()
+                .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
+        List<InputMethodInfo> imis = mImm.getEnabledInputMethodList();
+        if (curInputMethodId != null) {
+            for (InputMethodInfo imi: imis) {
+                if (imi.getId().equals(curInputMethodId)) {
+                    return imi;
+                }
+            }
+        }
+        return null;
+    }
+
+    private Drawable getCurrentSubtypeIcon() {
+        final PackageManager pm = getContext().getPackageManager();
+        InputMethodInfo imi = getCurrentInputMethodInfo();
+        InputMethodSubtype subtype = mImm.getCurrentInputMethodSubtype();
+        Drawable icon = null;
+        if (imi != null) {
+            if (subtype != null) {
+                return pm.getDrawable(imi.getPackageName(), subtype.getIconResId(),
+                        imi.getServiceInfo().applicationInfo);
+            } else if (imi.getSubtypes().size() > 0) {
+                return pm.getDrawable(imi.getPackageName(),
+                        imi.getSubtypes().get(0).getIconResId(),
+                        imi.getServiceInfo().applicationInfo);
+            } else {
+                try {
+                    return pm.getApplicationInfo(imi.getPackageName(), 0).loadIcon(pm);
+                } catch (PackageManager.NameNotFoundException e) {
+                    Log.w(TAG, "Current IME cann't be found: " + imi.getPackageName());
+                }
+            }
+        }
+        return null;
+    }
+
+    private void refreshStatusIcon(boolean keyboardShown) {
+        if (!keyboardShown) {
+            setVisibility(View.INVISIBLE);
+            return;
+        } else {
+            setVisibility(View.VISIBLE);
+        }
+        Drawable icon = getCurrentSubtypeIcon();
+        if (icon == null) {
+            mIcon.setImageResource(R.drawable.ic_sysbar_ime_default);
+        } else {
+            mIcon.setImageDrawable(icon);
+        }
+    }
+
+    private void postRefreshStatusIcon() {
+        getHandler().post(new Runnable() {
+            public void run() {
+                refreshStatusIcon(mKeyboardShown);
+            }
+        });
+    }
+
+    public void showSoftInput() {
+        mKeyboardShown = true;
+        postRefreshStatusIcon();
+    }
+
+    public void hideSoftInput() {
+        mKeyboardShown = false;
+        postRefreshStatusIcon();
+    }
+}