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

Commit 9c62414f authored by Nicolo' Mazzucato's avatar Nicolo' Mazzucato
Browse files

Refactor how config change is propagated to WindowTokenClient from VRI

This uses WindowTokenClientController to propagate the configuration change to window tokens from ViewRootImpl, and adds checks to make sure we're calling the config change callback from the expected thread in windowTokenClient.

Bug: 362719719
Bug: 394527409
Test: WindowTokenClientControllerTest
Flag: com.android.window.flags.enable_window_context_resources_update_on_config_change
Change-Id: I8f313fb5cc5dcd99c3c746e5e7c8056d7f31380b
parent d245cab3
Loading
Loading
Loading
Loading
+9 −11
Original line number Diff line number Diff line
@@ -272,9 +272,9 @@ import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;
import android.window.ScreenCapture;
import android.window.SurfaceSyncGroup;
import android.window.WindowContext;
import android.window.WindowOnBackInvokedDispatcher;
import android.window.WindowTokenClient;
import android.window.WindowTokenClientController;
import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
@@ -6614,12 +6614,15 @@ public final class ViewRootImpl implements ViewParent,
        } else {
            if (enableWindowContextResourcesUpdateOnConfigChange()) {
                // There is no activity callback - update resources for window token, if needed.
                final WindowTokenClient windowTokenClient = getWindowTokenClient();
                if (windowTokenClient != null) {
                    windowTokenClient.onConfigurationChanged(
                final IBinder windowContextToken = mContext.getWindowContextToken();
                if (windowContextToken instanceof WindowTokenClient) {
                    WindowTokenClientController.getInstance().onWindowConfigurationChanged(
                            windowContextToken,
                            mLastReportedMergedConfiguration.getMergedConfiguration(),
                            newDisplayId == INVALID_DISPLAY ? mDisplay.getDisplayId()
                                    : newDisplayId);
                            newDisplayId == INVALID_DISPLAY
                                    ? mDisplay.getDisplayId()
                                    : newDisplayId
                    );
                }
            }
            updateConfiguration(newDisplayId);
@@ -6627,11 +6630,6 @@ public final class ViewRootImpl implements ViewParent,
        mForceNextConfigUpdate = false;
    }
    private WindowTokenClient getWindowTokenClient() {
        if (!(mContext instanceof WindowContext)) return null;
        return (WindowTokenClient) mContext.getWindowContextToken();
    }
    /**
     * Update display and views if last applied merged configuration changed.
     * @param newDisplayId Id of new display if moved, {@link Display#INVALID_DISPLAY} otherwise.
+1 −0
Original line number Diff line number Diff line
@@ -107,6 +107,7 @@ public class WindowTokenClient extends Binder {
     * @param newDisplayId the updated {@link android.view.Display} ID
     */
    @MainThread
    @VisibleForTesting(visibility = PACKAGE)
    public void onConfigurationChanged(Configuration newConfig, int newDisplayId) {
        onConfigurationChanged(newConfig, newDisplayId, true /* shouldReportConfigChange */);
    }
+17 −0
Original line number Diff line number Diff line
@@ -25,7 +25,9 @@ import android.app.IApplicationThread;
import android.app.servertransaction.WindowContextInfoChangeItem;
import android.app.servertransaction.WindowContextWindowRemovalItem;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.ArraySet;
@@ -50,6 +52,7 @@ public class WindowTokenClientController {
    private final Object mLock = new Object();
    private final IApplicationThread mAppThread = ActivityThread.currentActivityThread()
            .getApplicationThread();
    private final Handler mHandler = ActivityThread.currentActivityThread().getHandler();

    /** Attached {@link WindowTokenClient}. */
    @GuardedBy("mLock")
@@ -257,6 +260,20 @@ public class WindowTokenClientController {
        }
    }

    /** Propagates the configuration change to the client token. */
    public void onWindowConfigurationChanged(@NonNull IBinder clientToken,
            @NonNull Configuration config, int displayId) {
        final WindowTokenClient windowTokenClient = getWindowTokenClientIfAttached(clientToken);
        if (windowTokenClient != null) {
            // Let's make sure it's called on the main thread!
            if (mHandler.getLooper().isCurrentThread()) {
                windowTokenClient.onConfigurationChanged(config, displayId);
            } else {
                windowTokenClient.postOnConfigurationChanged(config, displayId);
            }
        }
    }

    @Nullable
    private WindowTokenClient getWindowTokenClientIfAttached(@NonNull IBinder clientToken) {
        if (!(clientToken instanceof WindowTokenClient windowTokenClient)) {
+21 −0
Original line number Diff line number Diff line
@@ -266,4 +266,25 @@ public class WindowTokenClientControllerTest {

        verify(mWindowTokenClient).onWindowTokenRemoved();
    }

    @Test
    public void testOnWindowConfigurationChanged_propagatedToCorrectToken() throws RemoteException {
        doReturn(mWindowContextInfo).when(mWindowManagerService)
                .attachWindowContextToDisplayContent(any(), any(), anyInt());

        mController.onWindowConfigurationChanged(mWindowTokenClient, mConfiguration,
                DEFAULT_DISPLAY + 1);

        // Not propagated before attaching
        verify(mWindowTokenClient, never()).onConfigurationChanged(mConfiguration,
                DEFAULT_DISPLAY + 1);

        assertTrue(mController.attachToDisplayContent(mWindowTokenClient, DEFAULT_DISPLAY));

        mController.onWindowConfigurationChanged(mWindowTokenClient, mConfiguration,
                DEFAULT_DISPLAY + 1);

        // Now that's attached, propagating it.
        verify(mWindowTokenClient).postOnConfigurationChanged(mConfiguration, DEFAULT_DISPLAY + 1);
    }
}