diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index fef8d1005e297..cf5a7b629e388 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -2413,7 +2413,6 @@ class ContextImpl extends Context { context.setResources(createResources(mToken, mPackageInfo, mSplitName, overrideDisplayId, overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo(), mResources.getLoaders())); - context.mIsUiContext = isSelfOrOuterUiContext(); return context; } @@ -2442,6 +2441,11 @@ class ContextImpl extends Context { // the display that would otherwise be inherited from mToken (or the global configuration if // mToken is null). context.mForceDisplayOverrideInResources = 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; } @@ -2806,6 +2810,7 @@ class ContextImpl extends Context { mIsAssociatedWithDisplay = container.mIsAssociatedWithDisplay; mIsSystemOrSystemUiContext = container.mIsSystemOrSystemUiContext; mForceDisplayOverrideInResources = container.mForceDisplayOverrideInResources; + mIsUiContext = container.isSelfOrOuterUiContext(); } else { mBasePackageName = packageInfo.mPackageName; ApplicationInfo ainfo = packageInfo.getApplicationInfo(); diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 38bc797506315..5452f92c929fc 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -5820,10 +5820,19 @@ public abstract class Context { * {@link #createWindowContext(int, Bundle)} on the returned display Context or use an * {@link android.app.Activity}. * + *
+ * Note that invoking #createDisplayContext(Display) from an UI context is not regarded + * as an UI context. In other words, it is not suggested to access UI components (such as + * obtain a {@link WindowManager} by {@link #getSystemService(String)}) + * from the context created from #createDisplayContext(Display). + *
+ * * @param display A {@link Display} object specifying the display for whose metrics the * Context's resources should be tailored. * * @return A {@link Context} for the display. + * + * @see #getSystemService(String) */ @DisplayContext public abstract Context createDisplayContext(@NonNull Display display); diff --git a/core/tests/coretests/src/android/content/ContextTest.java b/core/tests/coretests/src/android/content/ContextTest.java index 777f4a3e03a8b..d1776fb7e5c12 100644 --- a/core/tests/coretests/src/android/content/ContextTest.java +++ b/core/tests/coretests/src/android/content/ContextTest.java @@ -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(createUiContext()); assertTrue(wrapper.isUiContext()); } - private static class TestUiContext extends ContextWrapper { - TestUiContext() { - super(null /* base */); - } + @Test + public void testIsUiContext_UiContextDerivedContext() { + final Context uiContext = createUiContext(); + Context context = uiContext.createAttributionContext(null /* attributionTag */); - @Override - public boolean isUiContext() { - return true; - } + assertTrue(context.isUiContext()); + + context = uiContext.createConfigurationContext(new Configuration()); + + assertTrue(context.isUiContext()); + } + + @Test + public void testIsUiContext_UiContextDerivedDisplayContext() { + final Context uiContext = createUiContext(); + final Display secondaryDisplay = + getSecondaryDisplay(uiContext.getSystemService(DisplayManager.class)); + final Context context = uiContext.createDisplayContext(secondaryDisplay); + + assertFalse(context.isUiContext()); + } + + private Context createUiContext() { + 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 */); } }