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

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/12432005

Change-Id: Ie5a1f6808654d80fb54bed9665c1c95c4a0245d4
This commit is contained in:
Charles Chen
2020-08-27 17:11:45 +00:00
committed by Automerger Merge Worker
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, context.setResources(createResources(mToken, mPackageInfo, mSplitName, displayId,
overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo(), overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo(),
mResources.getLoaders())); mResources.getLoaders()));
context.mIsUiContext = isSelfOrOuterUiContext();
return context; return context;
} }
@@ -2409,6 +2408,11 @@ class ContextImpl extends Context {
mResources.getLoaders())); mResources.getLoaders()));
context.mDisplay = display; context.mDisplay = display;
context.mIsAssociatedWithDisplay = true; 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; return context;
} }
@@ -2770,6 +2774,7 @@ class ContextImpl extends Context {
mDisplay = container.mDisplay; mDisplay = container.mDisplay;
mIsAssociatedWithDisplay = container.mIsAssociatedWithDisplay; mIsAssociatedWithDisplay = container.mIsAssociatedWithDisplay;
mIsSystemOrSystemUiContext = container.mIsSystemOrSystemUiContext; mIsSystemOrSystemUiContext = container.mIsSystemOrSystemUiContext;
mIsUiContext = container.isSelfOrOuterUiContext();
} else { } else {
mBasePackageName = packageInfo.mPackageName; mBasePackageName = packageInfo.mPackageName;
ApplicationInfo ainfo = packageInfo.getApplicationInfo(); 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_OWN_CONTENT_ONLY;
import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC; import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
import static android.view.Display.DEFAULT_DISPLAY; import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@@ -188,19 +189,38 @@ public class ContextTest {
assertFalse(wrapper.isUiContext()); assertFalse(wrapper.isUiContext());
wrapper = new ContextWrapper(new TestUiContext()); wrapper = new ContextWrapper(getUiContext());
assertTrue(wrapper.isUiContext()); assertTrue(wrapper.isUiContext());
} }
private static class TestUiContext extends ContextWrapper { @Test
TestUiContext() { public void testIsUiContext_UiContextDerivedContext() {
super(null /* base */); final Context uiContext = getUiContext();
} Context context = uiContext.createAttributionContext(null /* attributionTag */);
@Override assertTrue(context.isUiContext());
public boolean isUiContext() {
return true; 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 */);
} }
} }