Re-introduce event order guaranee in IME client registration

This is a follow up CL to my previous CL [1], that may have slightly
changed event order guarantee about how an IME client
(InputMethodManager instance running in the IME cliernt process) will
be recognized by the system.

In short, IME client is recognized by InputMethodManagerService (IMMS)
as follows.
 1. The client process internally calls IWindowManager.openSession()
    with a callback Binder object IInputMethodClient.
 2. WindowManagerService (WMS) does in-process method call to let IMMS
    know about this client.
 3. Now the client process can start calling InputMethodManager APIs
    that internally relies on IInputMethodClient as the identify of
    the client.

Before the previous CL [1], both the step 1 and step 2 were executed
synchronously.  The client process was blocked until the step 2 were
fully executed in IMMS.

With the previous CL [1], now the step 2 is asynchronous and it is no
longer guaranteed that the client is actually registered even after
IWindowManager.openSession() is done.

With this CL, the step 2 becomes synchronous again to recover the
original strong guarantee about event order in IME client setup
process.

For consistency, this CL does the same thing for removeClient()
callback from WMS to IMMS, which had also been done in a synchronous
manner.

 [1]: I453200fd5847e9a78876affb6a1caec221525e1d
      e24ed79edb937c743b96b3f9baddc689fb694be1

Bug: 112670859
Fix: 113877122
Test: atest CtsInputMethodTestCases CtsInputMethodServiceHostTestCases
Change-Id: Id91dd600120e4981aa1d9005ce644728968430c9
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index aa1b303..4d3468e 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -204,8 +204,6 @@
     static final int MSG_START_INPUT = 2000;
     static final int MSG_START_VR_INPUT = 2010;
 
-    static final int MSG_ADD_CLIENT = 2980;
-    static final int MSG_REMOVE_CLIENT = 2990;
     static final int MSG_UNBIND_CLIENT = 3000;
     static final int MSG_BIND_CLIENT = 3010;
     static final int MSG_SET_ACTIVE = 3020;
@@ -1302,7 +1300,7 @@
         @Override
         public void onStart() {
             LocalServices.addService(InputMethodManagerInternal.class,
-                    new LocalServiceImpl(mService.mHandler));
+                    new LocalServiceImpl(mService));
             publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
         }
 
@@ -3395,15 +3393,6 @@
             }
 
             // ---------------------------------------------------------
-            case MSG_ADD_CLIENT:
-                addClient((ClientState) msg.obj);
-                return true;
-
-            case MSG_REMOVE_CLIENT:
-                removeClient((IInputMethodClient) msg.obj);
-                return true;
-
-            // ---------------------------------------------------------
 
             case MSG_UNBIND_CLIENT:
                 try {
@@ -4396,22 +4385,27 @@
 
     private static final class LocalServiceImpl extends InputMethodManagerInternal {
         @NonNull
+        private final InputMethodManagerService mService;
+        @NonNull
         private final Handler mHandler;
 
-        LocalServiceImpl(@NonNull final Handler handler) {
-            mHandler = handler;
+        LocalServiceImpl(@NonNull InputMethodManagerService service) {
+            mService = service;
+            mHandler = service.mHandler;
         }
 
         @Override
         public void addClient(IInputMethodClient client, IInputContext inputContext, int uid,
                 int pid) {
-            mHandler.sendMessage(mHandler.obtainMessage(MSG_ADD_CLIENT,
-                    new ClientState(client, inputContext, uid, pid)));
+            // Work around Bug 113877122: We need to handle this synchronously.  Otherwise, some
+            // IMM binder calls from the client process before we register this client.
+            mService.addClient(new ClientState(client, inputContext, uid, pid));
         }
 
         @Override
         public void removeClient(IInputMethodClient client) {
-            mHandler.sendMessage(mHandler.obtainMessage(MSG_REMOVE_CLIENT, client));
+            // Handle this synchronously to be consistent with addClient().
+            mService.removeClient(client);
         }
 
         @Override
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerInternal.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerInternal.java
index b57356f..b5a9f74 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodManagerInternal.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerInternal.java
@@ -28,6 +28,10 @@
     /**
      * Called by the window manager service when a client process is being attached to the window
      * manager service.
+     *
+     * <p>The caller must not have WindowManagerService lock.  This method internally acquires
+     * InputMethodManagerService lock.</p>
+     *
      * @param client {@link android.os.Binder} proxy that is associated with the singleton instance
      *               of {@link android.view.inputmethod.InputMethodManager} that runs on the client
      *               process
@@ -42,6 +46,10 @@
     /**
      * Called by the window manager service when a client process is being attached to the window
      * manager service.
+     *
+     * <p>The caller must not have WindowManagerService lock.  This method internally acquires
+     * InputMethodManagerService lock.</p>
+     *
      * @param client {@link android.os.Binder} proxy that is associated with the singleton instance
      *               of {@link android.view.inputmethod.InputMethodManager} that runs on the client
      *               process