Merge "Make a context dervied from an UI context as an UI context"

This commit is contained in:
Charles Chen
2020-09-21 10:43:30 +00:00
committed by Android (Google) Code Review
3 changed files with 44 additions and 10 deletions

View File

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

View File

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

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