Add a function to switch back to the last used IME

Change-Id: Iac7bcc2ee16dd04d91a3e75b160622d246788c9a
diff --git a/api/current.xml b/api/current.xml
index ed1d929..ba168be 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -219363,6 +219363,19 @@
 <parameter name="iconId" type="int">
 </parameter>
 </method>
+<method name="switchToLastInputMethod"
+ return="boolean"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="imeToken" type="android.os.IBinder">
+</parameter>
+</method>
 <method name="toggleSoftInput"
  return="void"
  abstract="false"
diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java
index 033ee7c..1d75b42 100644
--- a/core/java/android/view/inputmethod/InputMethodManager.java
+++ b/core/java/android/view/inputmethod/InputMethodManager.java
@@ -1432,6 +1432,17 @@
         }
     }
 
+    public boolean switchToLastInputMethod(IBinder imeToken) {
+        synchronized (mH) {
+            try {
+                return mService.switchToLastInputMethod(imeToken);
+            } catch (RemoteException e) {
+                Log.w(TAG, "IME died: " + mCurId, e);
+                return false;
+            }
+        }
+    }
+
     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 ca1cd59..7d8e624 100644
--- a/core/java/com/android/internal/view/IInputMethodManager.aidl
+++ b/core/java/com/android/internal/view/IInputMethodManager.aidl
@@ -57,7 +57,7 @@
     void updateStatusIcon(in IBinder token, String packageName, int iconId);
     void setIMEButtonVisible(in IBinder token, boolean visible);
     InputMethodSubtype getCurrentInputMethodSubtype();
+    boolean switchToLastInputMethod(in IBinder token);
     
     boolean setInputMethodEnabled(String id, boolean enabled);
 }
-
diff --git a/services/java/com/android/server/InputMethodManagerService.java b/services/java/com/android/server/InputMethodManagerService.java
index 0d3cfde..1df4405 100644
--- a/services/java/com/android/server/InputMethodManagerService.java
+++ b/services/java/com/android/server/InputMethodManagerService.java
@@ -1279,6 +1279,21 @@
         setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID);
     }
 
+    public boolean switchToLastInputMethod(IBinder token) {
+        synchronized (mMethodMap) {
+            Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
+            if (lastIme != null) {
+                InputMethodInfo imi = mMethodMap.get(lastIme.first);
+                if (imi != null) {
+                    setInputMethodWithSubtype(token, lastIme.first, getSubtypeIdFromHashCode(
+                            imi, Integer.valueOf(lastIme.second)));
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
+
     private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) {
         synchronized (mMethodMap) {
             if (token == null) {