Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8260aebe authored by Ming-Shin Lu's avatar Ming-Shin Lu
Browse files

Add Null check when excluding IME window surface for snapshot

TaskSnapshotController.createTaskSnapshot crashes when initializing
SurfaceControl$LayerCaptureArgs for excluding NULL IME window surface
control.

As SurfaceControl#nativeCaptureLayers specified the elements in
excludeLayers array should be non-null,

Add a null check before adding excludeLayers when calling
SurfaceControl.captureLayersExcluding to fix the crash issue.

Fix: 174504297
Test: atest WmTests:TaskSnapshotControllerTest#\
      testCreateTaskSnapshotWithExcludingIme

Change-Id: Ie6fa7aa4ac61fc49a6235882d700e1e4d8437666
parent d1ea1214
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -369,7 +369,8 @@ class TaskSnapshotController {
        SurfaceControl[] excludeLayers;
        final WindowState imeWindow = task.getDisplayContent().mInputMethodWindow;
        // Exclude IME window snapshot when IME isn't proper to attach to app.
        if (imeWindow != null && !task.getDisplayContent().isImeAttachedToApp()) {
        if (imeWindow != null && imeWindow.getSurfaceControl() != null
                && !task.getDisplayContent().isImeAttachedToApp()) {
            excludeLayers = new SurfaceControl[1];
            excludeLayers[0] = imeWindow.getSurfaceControl();
        } else {
+20 −0
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

import android.app.ActivityManager;
import android.app.WindowConfiguration;
@@ -183,6 +185,24 @@ public class TaskSnapshotControllerTest extends WindowTestsBase {
        }
    }

    @UseTestDisplay(addWindows = {W_ACTIVITY, W_INPUT_METHOD})
    @Test
    public void testCreateTaskSnapshotWithExcludingIme() {
        Task task = mAppWindow.mActivityRecord.getTask();
        spyOn(task);
        spyOn(mDisplayContent);
        when(task.getDisplayContent().isImeAttachedToApp()).thenReturn(false);
        // Intentionally set the SurfaceControl of input method window as null.
        mDisplayContent.mInputMethodWindow.setSurfaceControl(null);
        // Verify no NPE happens when calling createTaskSnapshot.
        try {
            mWm.mTaskSnapshotController.createTaskSnapshot(mAppWindow.mActivityRecord.getTask(),
                    1f /* scaleFraction */, PixelFormat.UNKNOWN, null /* outTaskSize */);
        } catch (NullPointerException e) {
            fail("There should be no exception when calling createTaskSnapshot");
        }
    }

    @UseTestDisplay(addWindows = W_ACTIVITY)
    @Test
    public void testPrepareTaskSnapshot() {