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

Commit 228bc14e authored by Eric Lin's avatar Eric Lin
Browse files

Align non-UI context behavior in getCurrentWindowLayoutInfo.

This commit aligns the behavior of `getCurrentWindowLayoutInfo` with
`addWindowLayoutInfoListener` by throwing an `IllegalArgumentException`
when the provided context is not a `UiContext`.

This ensures that both methods have consistent behavior and provide
clear error messages when used incorrectly.

Bug: 337820752
Flag: com.android.window.flags.wlinfo_oncreate
Test: atest CtsWindowManagerJetpackTestCases:ExtensionWindowLayoutComponentTest
Test: atest WMJetpackUnitTests:WindowLayoutComponentImplTest
Change-Id: I3cd579a96c2fda93c57dbf046f221950f5e11cd8
parent 229d51bb
Loading
Loading
Loading
Loading
+21 −15
Original line number Diff line number Diff line
@@ -145,21 +145,7 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent {
                    || containsConsumer(consumer)) {
                return;
            }
            final IllegalArgumentException exception = new IllegalArgumentException(
                    "Context must be a UI Context with display association, which should be"
                    + " an Activity, WindowContext or InputMethodService");
            if (!context.isUiContext()) {
                throw exception;
            }
            if (context.getAssociatedDisplayId() == INVALID_DISPLAY) {
                // This is to identify if #isUiContext of a non-UI Context is overridden.
                // #isUiContext is more likely to be overridden than #getAssociatedDisplayId
                // since #isUiContext is a public API.
                StrictMode.onIncorrectContextUsed("The registered Context is a UI Context "
                        + "but not associated with any display. "
                        + "This Context may not receive any WindowLayoutInfo update. "
                        + dumpAllBaseContextToString(context), exception);
            }
            assertUiContext(context);
            Log.d(TAG, "Register WindowLayoutInfoListener on "
                    + dumpAllBaseContextToString(context));
            mFoldingFeatureProducer.getData((features) -> {
@@ -339,6 +325,7 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent {
    @Override
    @NonNull
    public WindowLayoutInfo getCurrentWindowLayoutInfo(@NonNull @UiContext Context context) {
        assertUiContext(context);
        synchronized (mLock) {
            return getWindowLayoutInfo(context, mLastReportedFoldingFeatures);
        }
@@ -353,6 +340,25 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent {
        return mSupportedWindowFeatures;
    }

    private void assertUiContext(@NonNull Context context) {
        final IllegalArgumentException exception = new IllegalArgumentException(
                "Context must be a UI Context with display association, which should be "
                        + "an Activity, WindowContext or InputMethodService");
        if (!context.isUiContext()) {
            throw exception;
        }
        if (context.getAssociatedDisplayId() == INVALID_DISPLAY) {
            // This is to identify if #isUiContext of a non-UI Context is overridden.
            // #isUiContext is more likely to be overridden than #getAssociatedDisplayId
            // since #isUiContext is a public API.
            StrictMode.onIncorrectContextUsed("The given context is a UI context, "
                    + "but it is not associated with any display. "
                    + "This context may not receive WindowLayoutInfo updates and "
                    + "may get an empty WindowLayoutInfo return value. "
                    + dumpAllBaseContextToString(context), exception);
        }
    }

    /** @see #getWindowLayoutInfo(Context, List) */
    private WindowLayoutInfo getWindowLayoutInfo(int displayId,
            @NonNull WindowConfiguration windowConfiguration,
+8 −6
Original line number Diff line number Diff line
@@ -72,6 +72,11 @@ public class WindowLayoutComponentImplTest {
        mWindowLayoutComponent.onDisplayFeaturesChanged(Collections.emptyList());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testAddWindowLayoutListener_nonUiContext_throwsError() {
        mWindowLayoutComponent.addWindowLayoutInfoListener(mAppContext, info -> {});
    }

    @Test
    public void testGetCurrentWindowLayoutInfo_noFoldingFeature_returnsEmptyList() {
        final Context testUiContext = new TestUiContext(mAppContext);
@@ -102,12 +107,9 @@ public class WindowLayoutComponentImplTest {
                featureRect, FoldingFeature.TYPE_HINGE, FoldingFeature.STATE_FLAT));
    }

    @Test
    public void testGetCurrentWindowLayoutInfo_nonUiContext_returnsEmptyList() {
        final WindowLayoutInfo layoutInfo =
    @Test(expected = IllegalArgumentException.class)
    public void testGetCurrentWindowLayoutInfo_nonUiContext_throwsError() {
        mWindowLayoutComponent.getCurrentWindowLayoutInfo(mAppContext);

        assertThat(layoutInfo.getDisplayFeatures()).isEmpty();
    }

    /**