[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
This commit is contained in:
Charles Chen
2020-08-24 09:56:57 +08:00
parent a4f73d7f1f
commit b2444cbef5
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 */);
assertTrue(context.isUiContext());
context = uiContext.createConfigurationContext(new Configuration());
assertTrue(context.isUiContext());
} }
@Override @Test
public boolean isUiContext() { public void testIsUiContext_UiContextDerivedDisplayContext() {
return true; 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 */);
} }
} }