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

Commit eb039cc6 authored by Darryl L Johnson's avatar Darryl L Johnson
Browse files

Prevent DecorContext from creating DisplayContext for default display.

The previous implementation of createDisplayContext() was essentially a
noop within ResourcesManager when the default display was passed. However,
the current implementation always overrides the configuration with the provided
display's metrics so when a DecorContext with the default display is
created a new ResourcesImpl object is also created due to the difference in
configuration. This CL changes the DecorContext logic to match the
previous implemenation where the configuration is only overriden for a
non-default display thus only creating a new ResourcesImpl object for
non-default displays.

Bug: 166174272
Test: atest google/perf/app-startup/benchmark-app-hermetic/cold-dropcache-test -v
Test: atest com.android.internal.policy.DecorContextTest

Change-Id: I05920c4f7987ffcaec8b197e8636606c04b418d4
parent 51412036
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -20,8 +20,10 @@ import android.content.AutofillOptions;
import android.content.ContentCaptureOptions;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.view.ContextThemeWrapper;
import android.view.Display;
import android.view.contentcapture.ContentCaptureManager;

import com.android.internal.annotations.VisibleForTesting;
@@ -47,9 +49,17 @@ public class DecorContext extends ContextThemeWrapper {
    public DecorContext(Context baseContext, PhoneWindow phoneWindow) {
        super(null /* base */, null);
        setPhoneWindow(phoneWindow);
        final Context displayContext = baseContext.createDisplayContext(
        // TODO(b/149790106): Non-activity context can be passed.
                phoneWindow.getContext().getDisplayNoVerify());
        final Display display = phoneWindow.getContext().getDisplayNoVerify();
        final Context displayContext;
        if (display.getDisplayId() == Display.DEFAULT_DISPLAY) {
            // TODO(b/166174272): Creating a display context for the default display will result
            // in additional resource creation.
            displayContext = baseContext.createConfigurationContext(Configuration.EMPTY);
            displayContext.updateDisplay(Display.DEFAULT_DISPLAY);
        } else {
            displayContext = baseContext.createDisplayContext(display);
        }
        attachBaseContext(displayContext);
    }

+10 −1
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@ import static android.view.Display.DEFAULT_DISPLAY;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import android.app.Activity;
import android.app.EmptyActivity;
@@ -41,6 +44,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

/**
 * Tests {@link DecorContext}.
@@ -63,13 +67,18 @@ public final class DecorContextTest {

    @Test
    public void testDecorContextWithDefaultDisplay() {
        final Context baseContext = Mockito.spy(mContext.getApplicationContext());
        Display defaultDisplay = new Display(DisplayManagerGlobal.getInstance(), DEFAULT_DISPLAY,
                new DisplayInfo(), DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS);
        final Context defaultDisplayContext = mContext.createDisplayContext(defaultDisplay);
        final PhoneWindow window = new PhoneWindow(defaultDisplayContext);
        DecorContext context = new DecorContext(mContext.getApplicationContext(), window);
        DecorContext context = new DecorContext(baseContext, window);

        assertDecorContextDisplay(DEFAULT_DISPLAY, context);

        // TODO(b/166174272): Creating a display context for the default display will result
        // in additional resource creation.
        verify(baseContext, never()).createDisplayContext(any());
    }

    @Test