Loading core/java/android/app/ContextImpl.java +6 −1 Original line number Diff line number Diff line Loading @@ -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; } Loading Loading @@ -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; } Loading Loading @@ -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(); Loading core/java/android/content/Context.java +9 −0 Original line number Diff line number Diff line Loading @@ -5820,10 +5820,19 @@ public abstract class Context { * {@link #createWindowContext(int, Bundle)} on the returned display Context or use an * {@link android.app.Activity}. * * <p> * 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). * </p> * * @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); Loading core/tests/coretests/src/android/content/ContextTest.java +29 −9 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 */); 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 = 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 */); } } Loading
core/java/android/app/ContextImpl.java +6 −1 Original line number Diff line number Diff line Loading @@ -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; } Loading Loading @@ -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; } Loading Loading @@ -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(); Loading
core/java/android/content/Context.java +9 −0 Original line number Diff line number Diff line Loading @@ -5820,10 +5820,19 @@ public abstract class Context { * {@link #createWindowContext(int, Bundle)} on the returned display Context or use an * {@link android.app.Activity}. * * <p> * 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). * </p> * * @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); Loading
core/tests/coretests/src/android/content/ContextTest.java +29 −9 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 */); 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 = 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 */); } }