Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9e19211d authored by Charles Chen's avatar Charles Chen Committed by Automerger Merge Worker
Browse files

Merge "Fix NPE when invoking Context#isUiContext" into rvc-dev am: 5c25605d...

Merge "Fix NPE when invoking Context#isUiContext" into rvc-dev am: 5c25605d am: a91c84fa am: 6263253b

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

Change-Id: Ibd9f56546ba9001c2d6f7786f3205227f5393a53
parents d753383b 6263253b
Loading
Loading
Loading
Loading
+22 −17
Original line number Diff line number Diff line
@@ -1900,26 +1900,31 @@ class ContextImpl extends Context {

    @Override
    public Object getSystemService(String name) {
        if (vmIncorrectContextUseEnabled()) {
            // We may override this API from outer context.
        final boolean isUiContext = isUiContext() || getOuterContext().isUiContext();
            final boolean isUiContext = isUiContext() || isOuterUiContext();
            // Check incorrect Context usage.
        if (isUiComponent(name) && !isUiContext && vmIncorrectContextUseEnabled()) {
            if (isUiComponent(name) && !isUiContext) {
                final String errorMessage = "Tried to access visual service "
                        + SystemServiceRegistry.getSystemServiceClassName(name)
                        + " from a non-visual Context:" + getOuterContext();
            final String message = "Visual services, such as WindowManager, WallpaperService or "
                    + "LayoutInflater should be accessed from Activity or other visual Context. "
                    + "Use an Activity or a Context created with "
                    + "Context#createWindowContext(int, Bundle), which are adjusted to the "
                    + "configuration and visual bounds of an area on screen.";
                final String message = "Visual services, such as WindowManager, WallpaperService "
                        + "or LayoutInflater should be accessed from Activity or other visual "
                        + "Context. Use an Activity or a Context created with "
                        + "Context#createWindowContext(int, Bundle), which are adjusted to "
                        + "the configuration and visual bounds of an area on screen.";
                final Exception exception = new IllegalAccessException(errorMessage);
                StrictMode.onIncorrectContextUsed(message, exception);
            Log.e(TAG, errorMessage + message, exception);
                Log.e(TAG, errorMessage + " " + message, exception);
            }
        }

        return SystemServiceRegistry.getSystemService(this, name);
    }

    private boolean isOuterUiContext() {
        return getOuterContext() != null && getOuterContext().isUiContext();
    }

    @Override
    public String getSystemServiceName(Class<?> serviceClass) {
        return SystemServiceRegistry.getSystemServiceName(serviceClass);
@@ -2371,7 +2376,7 @@ class ContextImpl extends Context {
        context.setResources(createResources(mToken, mPackageInfo, mSplitName, displayId,
                overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo(),
                mResources.getLoaders()));
        context.mIsUiContext = isUiContext() || getOuterContext().isUiContext();
        context.mIsUiContext = isUiContext() || isOuterUiContext();
        return context;
    }

+3 −0
Original line number Diff line number Diff line
@@ -1151,6 +1151,9 @@ public class ContextWrapper extends Context {
     */
    @Override
    public boolean isUiContext() {
        if (mBase == null) {
            return false;
        }
        return mBase.isUiContext();
    }
}
+23 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static android.view.Display.DEFAULT_DISPLAY;
import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.app.ActivityThread;
@@ -180,4 +181,26 @@ public class ContextTest {
                VIRTUAL_DISPLAY_FLAG_PUBLIC | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
        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;
        }
    }
}