OSDN Git Service

Allow setImeWindowStatus with null startInputToken
authorYohei Yukawa <yukawa@google.com>
Thu, 16 Feb 2017 05:38:57 +0000 (21:38 -0800)
committerYohei Yukawa <yukawa@google.com>
Thu, 16 Feb 2017 05:38:57 +0000 (21:38 -0800)
This is a follow up CL to my previous CL [1], which caused an unexpected
side effect that leads the current IME to crash due to a too strict
requiremnt.

It turns out that it was too early for us to start requiring non-null
StartInputToken in InputMethodManagerService#setImeWindowStatus()
because in many places we have assumed that an IME can show/hide its
software keyboard even before an IME target window is associated with
the IME.

There are two major cases that we missed:

 - InputMethodManager#showSoftInputFromInputMethod(IBinder, int):

   This method does not require that the calling IME is already bound
   to an IME target window.

 - InputMethodManager#showSoftInputUnchecked(int ,ResultReceiver):

   This @hide method allows the caller application to let current IME
   show the software keyboard with bypassing all the normal focus
   management tasks in InputMethodManager.  We should seriously
   consider to deprecate this @hide method, but to do that we have to
   clean up some internal components and SearchView in the support
   library that still rely on this method.

Bug 35395372 is triggered by the second scenario, but until we sort out
all the possible corner cases, we should allow null startInputToken as a
valid request, like we had have done so before the CL [1] introduced
such a validation.

 [1]: I9921b381e02106dbffff5e0b3d13f0a1245ce807
      6db3bfe33d92127d203ec872a0b353585a99f256

Test: Made sure Bug 35395372 is no longer reproducible, that is,
      1. Flash a new image and complete the setup wizard on a
         direct-boot unaware device.
      2. Set a device password and require it upon each device boot.
      3. adb reboot
      4. Observe the default IME does not crash because of
         InvalidParameterException thrown by IMMS.
Test: Made sure IMM#showSoftInputFromInputMethod(IBinder, int) does
      not throw an InvalidParameterException even in an extreme case.
      1. Rebuild LatinIME with the following code in LatinIME.java
       @Override
       public AbstractInputMethodImpl onCreateInputMethodInterface() {
           return new InputMethodService.InputMethodImpl() {
               @Override
               public void attachToken(IBinder token) {
                   super.attachToken(token);

                   final InputMethodManager imm =
                       getSystemService(InputMethodManager.class);
                   final IBinder imeToken =
                       getWindow().getWindow().getAttributes().token;
                   imm.showSoftInputFromInputMethod(imeToken, 0);
               }
           };
       }
      2. adb install -r LatinIME.apk
      3. adb shell ime enable com.android.inputmethod.latin/.LatinIME
      4. adb shell ime set com.android.inputmethod.latin/.LatinIME
      5. Tap any text field.
      4. Observe LatinIME does not crash because of
         InvalidParameterException thrown by IMMS.
Bug: 34628091
Bug: 35079353
Fixes: 35395372
Change-Id: Ib9448c551d9a30776a999c27a5ff20f1a095633a

core/java/android/view/WindowManagerInternal.java
services/core/java/com/android/server/InputMethodManagerService.java
services/core/java/com/android/server/wm/WindowManagerService.java

index 7a5e670..a541a4c 100644 (file)
@@ -296,9 +296,12 @@ public abstract class WindowManagerInternal {
      *                         to corresponding API calls.  Note that this state is not guaranteed
      *                         to be synchronized with state in WindowManagerService.
      * @param targetWindowToken token to identify the target window that the IME is associated with.
+     *                          {@code null} when application, system, or the IME itself decided to
+     *                          change its window visibility before being associated with any target
+     *                          window.
      */
-    public abstract void updateInputMethodWindowStatus(IBinder imeToken, boolean imeWindowVisible,
-            IBinder targetWindowToken);
+    public abstract void updateInputMethodWindowStatus(@NonNull IBinder imeToken,
+            boolean imeWindowVisible, @Nullable IBinder targetWindowToken);
 
     /**
       * Returns true when the hardware keyboard is available.
index e6f6e57..8442c11 100644 (file)
@@ -2033,10 +2033,6 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
     @Override
     public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
             int backDisposition) {
-        if (startInputToken == null) {
-            throw new InvalidParameterException("startInputToken cannot be null");
-        }
-
         if (!calledWithValidToken(token)) {
             return;
         }
@@ -2044,15 +2040,13 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
         final StartInputInfo info;
         synchronized (mMethodMap) {
             info = mStartInputMap.get(startInputToken);
-            if (info == null) {
-                throw new InvalidParameterException("Unknown startInputToken=" + startInputToken);
-            }
             mImeWindowVis = vis;
             mBackDisposition = backDisposition;
             updateSystemUiLocked(token, vis, backDisposition);
         }
-        mWindowManagerInternal.updateInputMethodWindowStatus(info.mImeToken,
-                (vis & InputMethodService.IME_VISIBLE) != 0, info.mTargetWindow);
+        mWindowManagerInternal.updateInputMethodWindowStatus(token,
+                (vis & InputMethodService.IME_VISIBLE) != 0,
+                token != null ? info.mTargetWindow : null);
     }
 
     private void updateSystemUi(IBinder token, int vis, int backDisposition) {
index a53102f..78568fc 100644 (file)
@@ -7914,8 +7914,8 @@ public class WindowManagerService extends IWindowManager.Stub
         }
 
         @Override
-        public void updateInputMethodWindowStatus(IBinder imeToken, boolean imeWindowVisible,
-                IBinder targetWindowToken) {
+        public void updateInputMethodWindowStatus(@NonNull IBinder imeToken,
+                boolean imeWindowVisible, @Nullable IBinder targetWindowToken) {
             // TODO (b/34628091): Use this method to address the window animation issue.
             if (DEBUG_INPUT_METHOD) {
                 Slog.w(TAG_WM, "updateInputMethodWindowStatus: imeToken=" + imeToken