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

Commit b2444cbe authored by Charles Chen's avatar Charles Chen
Browse files

[RESTRICT AUTOMERGE] Make a context dervied from an UI context as an UI context

fixes: 165833103
Test: atest StrictModeTest ContextAccessTest
Test: atest InputMethodServiceTest InputMethodServiceStrictModeTest

Merged-In: Ia97e1a0cc290be516d2618148600238b3273c54c
Change-Id: Ia97e1a0cc290be516d2618148600238b3273c54c
parent a4f73d7f
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -2389,7 +2389,6 @@ class ContextImpl extends Context {
        context.setResources(createResources(mToken, mPackageInfo, mSplitName, displayId,
                overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo(),
                mResources.getLoaders()));
        context.mIsUiContext = isSelfOrOuterUiContext();
        return context;
    }

@@ -2409,6 +2408,11 @@ class ContextImpl extends Context {
                mResources.getLoaders()));
        context.mDisplay = display;
        context.mIsAssociatedWithDisplay = true;
        // Note that even if a display context is derived from an UI context, it should not be
        // treated as UI context because it does not handle configuration changes from the server
        // side. If the context does need to handle configuration changes, please use
        // Context#createWindowContext(int, Bundle).
        context.mIsUiContext = false;
        return context;
    }

@@ -2770,6 +2774,7 @@ class ContextImpl extends Context {
            mDisplay = container.mDisplay;
            mIsAssociatedWithDisplay = container.mIsAssociatedWithDisplay;
            mIsSystemOrSystemUiContext = container.mIsSystemOrSystemUiContext;
            mIsUiContext = container.isSelfOrOuterUiContext();
        } else {
            mBasePackageName = packageInfo.mPackageName;
            ApplicationInfo ainfo = packageInfo.getApplicationInfo();
+29 −9
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.content;
import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

import static com.google.common.truth.Truth.assertThat;

@@ -188,19 +189,38 @@ public class ContextTest {

        assertFalse(wrapper.isUiContext());

        wrapper = new ContextWrapper(new TestUiContext());
        wrapper = new ContextWrapper(getUiContext());

        assertTrue(wrapper.isUiContext());
    }

    private static class TestUiContext extends ContextWrapper {
        TestUiContext() {
            super(null /* base */);
    @Test
    public void testIsUiContext_UiContextDerivedContext() {
        final Context uiContext = getUiContext();
        Context context = uiContext.createAttributionContext(null /* attributionTag */);

        assertTrue(context.isUiContext());

        context = uiContext.createConfigurationContext(new Configuration());

        assertTrue(context.isUiContext());
    }

        @Override
        public boolean isUiContext() {
            return true;
    @Test
    public void testIsUiContext_UiContextDerivedDisplayContext() {
        final Context uiContext = getUiContext();
        final Display secondaryDisplay =
                getSecondaryDisplay(uiContext.getSystemService(DisplayManager.class));
        final Context context = uiContext.createDisplayContext(secondaryDisplay);

        assertFalse(context.isUiContext());
    }

    private Context getUiContext() {
        final Context appContext = ApplicationProvider.getApplicationContext();
        final DisplayManager displayManager = appContext.getSystemService(DisplayManager.class);
        final Display display = displayManager.getDisplay(DEFAULT_DISPLAY);
        return appContext.createDisplayContext(display)
                .createWindowContext(TYPE_APPLICATION_OVERLAY, null /* options */);
    }
}