Loading core/java/android/app/ContextImpl.java +22 −17 Original line number Original line Diff line number Diff line Loading @@ -1900,26 +1900,31 @@ class ContextImpl extends Context { @Override @Override public Object getSystemService(String name) { public Object getSystemService(String name) { if (vmIncorrectContextUseEnabled()) { // We may override this API from outer context. // We may override this API from outer context. final boolean isUiContext = isUiContext() || getOuterContext().isUiContext(); final boolean isUiContext = isUiContext() || isOuterUiContext(); // Check incorrect Context usage. // Check incorrect Context usage. if (isUiComponent(name) && !isUiContext && vmIncorrectContextUseEnabled()) { if (isUiComponent(name) && !isUiContext) { final String errorMessage = "Tried to access visual service " final String errorMessage = "Tried to access visual service " + SystemServiceRegistry.getSystemServiceClassName(name) + SystemServiceRegistry.getSystemServiceClassName(name) + " from a non-visual Context:" + getOuterContext(); + " from a non-visual Context:" + getOuterContext(); final String message = "Visual services, such as WindowManager, WallpaperService or " final String message = "Visual services, such as WindowManager, WallpaperService " + "LayoutInflater should be accessed from Activity or other visual Context. " + "or LayoutInflater should be accessed from Activity or other visual " + "Use an Activity or a Context created with " + "Context. Use an Activity or a Context created with " + "Context#createWindowContext(int, Bundle), which are adjusted to the " + "Context#createWindowContext(int, Bundle), which are adjusted to " + "configuration and visual bounds of an area on screen."; + "the configuration and visual bounds of an area on screen."; final Exception exception = new IllegalAccessException(errorMessage); final Exception exception = new IllegalAccessException(errorMessage); StrictMode.onIncorrectContextUsed(message, exception); StrictMode.onIncorrectContextUsed(message, exception); Log.e(TAG, errorMessage + message, exception); Log.e(TAG, errorMessage + " " + message, exception); } } } return SystemServiceRegistry.getSystemService(this, name); return SystemServiceRegistry.getSystemService(this, name); } } private boolean isOuterUiContext() { return getOuterContext() != null && getOuterContext().isUiContext(); } @Override @Override public String getSystemServiceName(Class<?> serviceClass) { public String getSystemServiceName(Class<?> serviceClass) { return SystemServiceRegistry.getSystemServiceName(serviceClass); return SystemServiceRegistry.getSystemServiceName(serviceClass); Loading Loading @@ -2371,7 +2376,7 @@ 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 = isUiContext() || getOuterContext().isUiContext(); context.mIsUiContext = isUiContext() || isOuterUiContext(); return context; return context; } } Loading core/java/android/content/ContextWrapper.java +3 −0 Original line number Original line Diff line number Diff line Loading @@ -1151,6 +1151,9 @@ public class ContextWrapper extends Context { */ */ @Override @Override public boolean isUiContext() { public boolean isUiContext() { if (mBase == null) { return false; } return mBase.isUiContext(); return mBase.isUiContext(); } } } } core/tests/coretests/src/android/content/ContextTest.java +23 −0 Original line number Original line Diff line number Diff line Loading @@ -23,6 +23,7 @@ import static android.view.Display.DEFAULT_DISPLAY; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue; import android.app.ActivityThread; import android.app.ActivityThread; Loading Loading @@ -180,4 +181,26 @@ public class ContextTest { VIRTUAL_DISPLAY_FLAG_PUBLIC | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY); VIRTUAL_DISPLAY_FLAG_PUBLIC | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY); return virtualDisplay.getDisplay(); return virtualDisplay.getDisplay(); } } @Test public void testIsUiContext_ContextWrapper() { ContextWrapper wrapper = new ContextWrapper(null /* base */); assertFalse(wrapper.isUiContext()); wrapper = new ContextWrapper(new TestUiContext()); assertTrue(wrapper.isUiContext()); } private static class TestUiContext extends ContextWrapper { TestUiContext() { super(null /* base */); } @Override public boolean isUiContext() { return true; } } } } Loading
core/java/android/app/ContextImpl.java +22 −17 Original line number Original line Diff line number Diff line Loading @@ -1900,26 +1900,31 @@ class ContextImpl extends Context { @Override @Override public Object getSystemService(String name) { public Object getSystemService(String name) { if (vmIncorrectContextUseEnabled()) { // We may override this API from outer context. // We may override this API from outer context. final boolean isUiContext = isUiContext() || getOuterContext().isUiContext(); final boolean isUiContext = isUiContext() || isOuterUiContext(); // Check incorrect Context usage. // Check incorrect Context usage. if (isUiComponent(name) && !isUiContext && vmIncorrectContextUseEnabled()) { if (isUiComponent(name) && !isUiContext) { final String errorMessage = "Tried to access visual service " final String errorMessage = "Tried to access visual service " + SystemServiceRegistry.getSystemServiceClassName(name) + SystemServiceRegistry.getSystemServiceClassName(name) + " from a non-visual Context:" + getOuterContext(); + " from a non-visual Context:" + getOuterContext(); final String message = "Visual services, such as WindowManager, WallpaperService or " final String message = "Visual services, such as WindowManager, WallpaperService " + "LayoutInflater should be accessed from Activity or other visual Context. " + "or LayoutInflater should be accessed from Activity or other visual " + "Use an Activity or a Context created with " + "Context. Use an Activity or a Context created with " + "Context#createWindowContext(int, Bundle), which are adjusted to the " + "Context#createWindowContext(int, Bundle), which are adjusted to " + "configuration and visual bounds of an area on screen."; + "the configuration and visual bounds of an area on screen."; final Exception exception = new IllegalAccessException(errorMessage); final Exception exception = new IllegalAccessException(errorMessage); StrictMode.onIncorrectContextUsed(message, exception); StrictMode.onIncorrectContextUsed(message, exception); Log.e(TAG, errorMessage + message, exception); Log.e(TAG, errorMessage + " " + message, exception); } } } return SystemServiceRegistry.getSystemService(this, name); return SystemServiceRegistry.getSystemService(this, name); } } private boolean isOuterUiContext() { return getOuterContext() != null && getOuterContext().isUiContext(); } @Override @Override public String getSystemServiceName(Class<?> serviceClass) { public String getSystemServiceName(Class<?> serviceClass) { return SystemServiceRegistry.getSystemServiceName(serviceClass); return SystemServiceRegistry.getSystemServiceName(serviceClass); Loading Loading @@ -2371,7 +2376,7 @@ 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 = isUiContext() || getOuterContext().isUiContext(); context.mIsUiContext = isUiContext() || isOuterUiContext(); return context; return context; } } Loading
core/java/android/content/ContextWrapper.java +3 −0 Original line number Original line Diff line number Diff line Loading @@ -1151,6 +1151,9 @@ public class ContextWrapper extends Context { */ */ @Override @Override public boolean isUiContext() { public boolean isUiContext() { if (mBase == null) { return false; } return mBase.isUiContext(); return mBase.isUiContext(); } } } }
core/tests/coretests/src/android/content/ContextTest.java +23 −0 Original line number Original line Diff line number Diff line Loading @@ -23,6 +23,7 @@ import static android.view.Display.DEFAULT_DISPLAY; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue; import android.app.ActivityThread; import android.app.ActivityThread; Loading Loading @@ -180,4 +181,26 @@ public class ContextTest { VIRTUAL_DISPLAY_FLAG_PUBLIC | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY); VIRTUAL_DISPLAY_FLAG_PUBLIC | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY); return virtualDisplay.getDisplay(); return virtualDisplay.getDisplay(); } } @Test public void testIsUiContext_ContextWrapper() { ContextWrapper wrapper = new ContextWrapper(null /* base */); assertFalse(wrapper.isUiContext()); wrapper = new ContextWrapper(new TestUiContext()); assertTrue(wrapper.isUiContext()); } private static class TestUiContext extends ContextWrapper { TestUiContext() { super(null /* base */); } @Override public boolean isUiContext() { return true; } } } }