Merge "[RESTRICT AUTOMERGE] Make a context dervied from an UI context as an UI context" into rvc-qpr-dev

This commit is contained in:
Charles Chen
2020-08-27 16:53:29 +00:00
committed by Android (Google) Code Review
2 changed files with 35 additions and 10 deletions

View File

@@ -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();

View File

@@ -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 */);
@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 = 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 */);
}
}