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

Commit 8181876d authored by Matthew Ng's avatar Matthew Ng
Browse files

Do not crash NearestTouchFrame when views are not attached to window

Change-Id: I6f4d086dc6aabee1d519794e86dbe5c0df8b6bfc
Fixes: 121212324
Test: atest NearestTouchFrameTest
parent e49bb322
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -97,10 +97,11 @@ public class NearestTouchFrame extends FrameLayout {
        }
        return mClickableChildren
                .stream()
                .filter(v -> v.isAttachedToWindow())
                .filter(View::isAttachedToWindow)
                .map(v -> new Pair<>(distance(v, event), v))
                .min(Comparator.comparingInt(f -> f.first))
                .get().second;
                .map(data -> data.second)
                .orElse(null);
    }

    private int distance(View v, MotionEvent event) {
+13 −1
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ package com.android.systemui.statusbar.phone;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -171,6 +170,19 @@ public class NearestTouchFrameTest extends SysuiTestCase {
        ev.recycle();
    }

    @Test
    public void testViewNotAttachedNoCrash() {
        View view = mockViewAt(0, 20, 10, 10);
        when(view.isAttachedToWindow()).thenReturn(false);
        mNearestTouchFrame.addView(view);
        mNearestTouchFrame.onMeasure(0, 0);

        MotionEvent ev = MotionEvent.obtain(0, 0, 0, 5 /* x */, 18 /* y */, 0);
        mNearestTouchFrame.onTouchEvent(ev);
        verify(view, never()).onTouchEvent(eq(ev));
        ev.recycle();
    }

    private View mockViewAt(int x, int y, int width, int height) {
        View v = spy(new View(mContext));
        doAnswer(invocation -> {