From c4bdf37af2bb70df47969e557c27c6c7a1cdbada Mon Sep 17 00:00:00 2001 From: Bill Lin Date: Tue, 22 Jun 2021 11:22:37 +0800 Subject: [PATCH] Fix SysUI NPE crash during the boot/init progress When Device boot and SystemUI servies starting, settings provider may callback onChange() when OneHandedController register observer. If the callback timing earlier han mEventCallback registered by WMShll#initOneHanded, then the NPE will happen. The simple fix is to add NPE check in OneHandedController#notifyExpandNotifcation() we can just ignore the callback during init time since the singal is to expand notification and come from shorcut after user enable and tap shortcut.(No need to act for the signal during boot progress.) Test: manual reboot device and observe Test: atest WMShellUnitTests Bug: 191600033 Change-Id: I73849afa9904031759da304298221dbb222aeaaa --- .../wm/shell/onehanded/OneHandedController.java | 4 +++- .../wm/shell/onehanded/OneHandedControllerTest.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedController.java index b43daa0da2c09..bd816c314b3e4 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedController.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedController.java @@ -481,7 +481,9 @@ public class OneHandedController implements RemoteCallable @VisibleForTesting void notifyExpandNotification() { - mMainExecutor.execute(() -> mEventCallback.notifyExpandNotification()); + if (mEventCallback != null) { + mMainExecutor.execute(() -> mEventCallback.notifyExpandNotification()); + } } @VisibleForTesting diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/onehanded/OneHandedControllerTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/onehanded/OneHandedControllerTest.java index 47789b7490ee2..950900337918b 100644 --- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/onehanded/OneHandedControllerTest.java +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/onehanded/OneHandedControllerTest.java @@ -435,4 +435,17 @@ public class OneHandedControllerTest extends OneHandedTestCase { verify(mSpiedOneHandedController).notifyShortcutState(anyInt()); } + + @Test + public void testNotifyExpandNotification_withNullCheckProtection() { + when(mSpiedOneHandedController.isOneHandedEnabled()).thenReturn(false); + when(mSpiedTransitionState.getState()).thenReturn(STATE_NONE); + when(mSpiedTransitionState.isTransitioning()).thenReturn(false); + when(mSpiedOneHandedController.isSwipeToNotificationEnabled()).thenReturn(true); + mSpiedOneHandedController.setOneHandedEnabled(true); + mSpiedOneHandedController.notifyExpandNotification(); + + // Verify no NPE crash and mMockShellMainExecutor never be execute. + verify(mMockShellMainExecutor, never()).execute(any()); + } }