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

Commit 9df37440 authored by Marco Loaiza's avatar Marco Loaiza
Browse files

Update deviceId when display changes in context

Whenever a DisplayContext is created or a displayId
is updated in Context, update the deviceId, if not
a Context with an explicit device association.

Test: atest DeviceAssociationTest
Bug: 253201821
Change-Id: Iaef039c6b1cf46fbb0ae020ba41735eb48c7d708
parent 35c5e745
Loading
Loading
Loading
Loading
+27 −7
Original line number Diff line number Diff line
@@ -2709,7 +2709,7 @@ class ContextImpl extends Context {
        context.setResources(createResources(mToken, mPackageInfo, mSplitName, displayId,
                overrideConfig, display.getDisplayAdjustments().getCompatibilityInfo(),
                mResources.getLoaders()));
        context.mDisplay = display;
        context.setDisplay(display);
        // Inherit context type if the container is from System or System UI context to bypass
        // UI context check.
        context.mContextType = mContextType == CONTEXT_TYPE_SYSTEM_OR_SYSTEM_UI
@@ -2725,6 +2725,13 @@ class ContextImpl extends Context {
        return context;
    }

    private void setDisplay(Display display) {
        mDisplay = display;
        if (display != null) {
            updateDeviceIdIfChanged(display.getDisplayId());
        }
    }

    @Override
    public @NonNull Context createDeviceContext(int deviceId) {
        if (!isValidDeviceId(deviceId)) {
@@ -2862,8 +2869,8 @@ class ContextImpl extends Context {
        baseContext.setResources(windowContextResources);
        // Associate the display with window context resources so that configuration update from
        // the server side will also apply to the display's metrics.
        baseContext.mDisplay = ResourcesManager.getInstance().getAdjustedDisplay(displayId,
                windowContextResources);
        baseContext.setDisplay(ResourcesManager.getInstance().getAdjustedDisplay(
                displayId, windowContextResources));

        return baseContext;
    }
@@ -3007,11 +3014,24 @@ class ContextImpl extends Context {

    @Override
    public void updateDisplay(int displayId) {
        mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources);
        setDisplay(mResourcesManager.getAdjustedDisplay(displayId, mResources));
        if (mContextType == CONTEXT_TYPE_NON_UI) {
            mContextType = CONTEXT_TYPE_DISPLAY_CONTEXT;
        }
        // TODO(b/253201821): Update deviceId when display is updated.
    }

    private void updateDeviceIdIfChanged(int displayId) {
        if (mIsExplicitDeviceId) {
            return;
        }
        VirtualDeviceManager vdm = getSystemService(VirtualDeviceManager.class);
        if (vdm != null) {
            int deviceId = vdm.getDeviceIdForDisplayId(displayId);
            if (deviceId != mDeviceId) {
                mDeviceId = deviceId;
                notifyOnDeviceChangedListeners(mDeviceId);
            }
        }
    }

    @Override
@@ -3306,8 +3326,8 @@ class ContextImpl extends Context {
                classLoader,
                packageInfo.getApplication() == null ? null
                        : packageInfo.getApplication().getResources().getLoaders()));
        context.mDisplay = resourcesManager.getAdjustedDisplay(displayId,
                context.getResources());
        context.setDisplay(resourcesManager.getAdjustedDisplay(
                displayId, context.getResources()));
        return context;
    }

+3 −1
Original line number Diff line number Diff line
@@ -7281,7 +7281,9 @@ public abstract class Context {
     * Updates the device ID association of this Context. Since a Context created with
     * {@link #createDeviceContext} cannot change its device association, this method must
     * not be called for instances created with {@link #createDeviceContext}.
     *
     *<p>
     * Note that updating the deviceId of the Context will not update its associated display.
     *</p>
     * @param deviceId The new device ID to assign to this Context.
     * @throws UnsupportedOperationException if the method is called on an instance that was
     *         created with {@link Context#createDeviceContext(int)}