From 7b5ae86566d103d16f27f4c08f4a1f9e41738a92 Mon Sep 17 00:00:00 2001 From: Ming-Shin Lu Date: Mon, 12 Sep 2022 18:01:15 +0800 Subject: Fix a potential NPE after scheduleShowImePostLayout When IME control targets request showing IME, ImeInsetsSourceProvider#scheduleShowImePostLayout will end up being called to schedule showInsets with a runner in the next layout. However, there is a timing that the target may be removed before the runner invoked (e.g. force-stop the app), and seeing "uncaught exception in WindowManager" log from applySurfaceChangesTransaction. Even though the exception won't make the system server crash, it's still a potential risk. Add a check in isReadyToShowIme() to ensure the control target is ready to call showInsets when exists. Fix: 244715663 Test: run atest CtsInputMethodTestCases without seeing "uncaught exception in WindowManager" log during force-stop test packages. Test: atest ImeInsetsSourceProviderTest Change-Id: I5327077c2e7292f508d5141125738be46400e5d0 --- .../core/java/com/android/server/wm/ImeInsetsSourceProvider.java | 6 ++++++ .../src/com/android/server/wm/ImeInsetsSourceProviderTest.java | 1 + 2 files changed, 7 insertions(+) diff --git a/services/core/java/com/android/server/wm/ImeInsetsSourceProvider.java b/services/core/java/com/android/server/wm/ImeInsetsSourceProvider.java index 3e6e06a27fdc..14a1cd011ad6 100644 --- a/services/core/java/com/android/server/wm/ImeInsetsSourceProvider.java +++ b/services/core/java/com/android/server/wm/ImeInsetsSourceProvider.java @@ -218,6 +218,12 @@ final class ImeInsetsSourceProvider extends WindowContainerInsetsSourceProvider if (dcTarget == null || mImeRequester == null) { return false; } + // Not ready to show if there is no IME control target. + final InsetsControlTarget controlTarget = mDisplayContent.getImeTarget(IME_TARGET_CONTROL); + if (controlTarget == null) { + return false; + } + ProtoLog.d(WM_DEBUG_IME, "dcTarget: %s mImeRequester: %s", dcTarget.getWindow().getName(), mImeRequester.getWindow() == null ? mImeRequester : mImeRequester.getWindow().getName()); diff --git a/services/tests/wmtests/src/com/android/server/wm/ImeInsetsSourceProviderTest.java b/services/tests/wmtests/src/com/android/server/wm/ImeInsetsSourceProviderTest.java index ca8481a8c50b..c839d1254397 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ImeInsetsSourceProviderTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/ImeInsetsSourceProviderTest.java @@ -63,6 +63,7 @@ public class ImeInsetsSourceProviderTest extends WindowTestsBase { public void testInputMethodInputTargetCanShowIme() { WindowState target = createWindow(null, TYPE_APPLICATION, "app"); mDisplayContent.setImeLayeringTarget(target); + mDisplayContent.updateImeInputAndControlTarget(target); mImeProvider.scheduleShowImePostLayout(target); assertTrue(mImeProvider.isReadyToShowIme()); } -- cgit v1.2.3-59-g8ed1b