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

Commit 8b782bd7 authored by Fabián Kozynski's avatar Fabián Kozynski
Browse files

Trigger configuration listener after reattach

In some cases, the configuration change may happen when we are not
attached (for example, in compose when folding). This causes values to
not be up to date.

Instead, keep track if at least one of those happened while detached and
trigger the listener on re-attach.

Test: atest QSPanelTest QSPanelControllerBaseTest
Test: manual
Fixes: 374107471
Flag: EXEMPT bug fix
Change-Id: Ic95f263c7328e0dd71ea99b9e3231e9619689193
parent 76580d70
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
@@ -604,6 +606,46 @@ public class QSPanelControllerBaseTest extends SysuiTestCase {
        assertThat(mPagedTileLayoutListening).isFalse();
    }

    @Test
    public void reAttach_configurationChangePending_triggersConfigurationListener() {
        mController.onViewDetached();

        when(mQSPanel.hadConfigurationChangeWhileDetached()).thenReturn(true);
        clearInvocations(mQSLogger);

        mController.onViewAttached();

        verify(mQSLogger).logOnConfigurationChanged(
                anyInt(),
                anyInt(),
                anyBoolean(),
                anyBoolean(),
                anyInt(),
                anyInt(),
                anyString()
        );
    }

    @Test
    public void reAttach_noConfigurationChangePending_doesntTriggerConfigurationListener() {
        mController.onViewDetached();

        when(mQSPanel.hadConfigurationChangeWhileDetached()).thenReturn(false);
        clearInvocations(mQSLogger);

        mController.onViewAttached();

        verify(mQSLogger, never()).logOnConfigurationChanged(
                anyInt(),
                anyInt(),
                anyBoolean(),
                anyBoolean(),
                anyInt(),
                anyInt(),
                anyString()
        );
    }


    private boolean usingMediaPlayer() {
        return !SceneContainerFlag.isEnabled();
+15 −0
Original line number Diff line number Diff line
@@ -116,6 +116,8 @@ public class QSPanel extends LinearLayout implements Tunable {
    @Nullable
    private View mMediaViewPlaceHolderForScene;

    private boolean mHadConfigurationChangeWhileDetached;

    public QSPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        mUsingMediaPlayer = useQsMediaPlayer(context);
@@ -425,10 +427,23 @@ public class QSPanel extends LinearLayout implements Tunable {
    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (!isAttachedToWindow()) {
            mHadConfigurationChangeWhileDetached = true;
        }
        mOnConfigurationChangedListeners.forEach(
                listener -> listener.onConfigurationChange(newConfig));
    }

    final boolean hadConfigurationChangeWhileDetached() {
        return mHadConfigurationChangeWhileDetached;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHadConfigurationChangeWhileDetached = false;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
+6 −0
Original line number Diff line number Diff line
@@ -234,6 +234,12 @@ public abstract class QSPanelControllerBase<T extends QSPanel> extends ViewContr
            mMediaHost.addVisibilityChangeListener(mMediaHostVisibilityListener);
        }
        mView.addOnConfigurationChangedListener(mOnConfigurationChangedListener);
        // We were not attached and the configuration may have changed, trigger the listener.
        if (mView.hadConfigurationChangeWhileDetached()) {
            mOnConfigurationChangedListener.onConfigurationChange(
                    getContext().getResources().getConfiguration()
            );
        }
        setTiles();
        mLastOrientation = getResources().getConfiguration().orientation;
        mLastScreenLayout = getResources().getConfiguration().screenLayout;
+64 −5
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
 */
package com.android.systemui.qs

import android.content.res.Configuration
import android.graphics.Rect
import android.platform.test.flag.junit.FlagsParameterization
import android.testing.TestableContext
@@ -91,8 +92,10 @@ class QSPanelTest(flags: FlagsParameterization) : SysuiTestCase() {

    @After
    fun tearDown() {
        if (qsPanel.isAttachedToWindow) {
            ViewUtils.detachView(qsPanel)
        }
    }

    @Test
    fun testHasCollapseAccessibilityAction() {
@@ -119,7 +122,7 @@ class QSPanelTest(flags: FlagsParameterization) : SysuiTestCase() {
        qsPanel.tileLayout?.addTile(
            QSPanelControllerBase.TileRecord(
                mock(QSTile::class.java),
                QSTileViewImpl(themedContext)
                QSTileViewImpl(themedContext),
            )
        )

@@ -129,7 +132,7 @@ class QSPanelTest(flags: FlagsParameterization) : SysuiTestCase() {
        qsPanel.setUsingHorizontalLayout(/* horizontal */ true, mediaView, /* force */ true)
        qsPanel.measure(
            /* width */ View.MeasureSpec.makeMeasureSpec(3000, View.MeasureSpec.EXACTLY),
            /* height */ View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY)
            /* height */ View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY),
        )
        qsPanel.layout(0, 0, qsPanel.measuredWidth, qsPanel.measuredHeight)

@@ -147,7 +150,7 @@ class QSPanelTest(flags: FlagsParameterization) : SysuiTestCase() {
        val padding = 10
        themedContext.orCreateTestableResources.addOverride(
            R.dimen.qs_panel_padding_bottom,
            padding
            padding,
        )
        qsPanel.updatePadding()
        assertThat(qsPanel.paddingBottom).isEqualTo(padding)
@@ -160,7 +163,7 @@ class QSPanelTest(flags: FlagsParameterization) : SysuiTestCase() {
        themedContext.orCreateTestableResources.addOverride(R.dimen.qs_panel_padding_top, padding)
        themedContext.orCreateTestableResources.addOverride(
            R.dimen.qs_panel_padding_top,
            paddingCombined
            paddingCombined,
        )

        qsPanel.updatePadding()
@@ -267,6 +270,62 @@ class QSPanelTest(flags: FlagsParameterization) : SysuiTestCase() {
        assertThat(qsPanel.tileLayout!!.maxColumns).isEqualTo(2)
    }

    @Test
    fun noPendingConfigChangesAtBeginning() {
        assertThat(qsPanel.hadConfigurationChangeWhileDetached()).isFalse()
    }

    @Test
    fun configChangesWhileDetached_pendingConfigChanges() {
        ViewUtils.detachView(qsPanel)

        qsPanel.onConfigurationChanged(Configuration())

        assertThat(qsPanel.hadConfigurationChangeWhileDetached()).isTrue()
    }

    @Test
    fun configChangesWhileDetached_reattach_pendingConfigChanges() {
        ViewUtils.detachView(qsPanel)

        qsPanel.onConfigurationChanged(Configuration())
        testableLooper.runWithLooper { ViewUtils.attachView(qsPanel) }

        assertThat(qsPanel.hadConfigurationChangeWhileDetached()).isTrue()
    }

    @Test
    fun configChangesWhileDetached_reattach_detach_pendingConfigChanges_reset() {
        ViewUtils.detachView(qsPanel)

        qsPanel.onConfigurationChanged(Configuration())

        testableLooper.runWithLooper { ViewUtils.attachView(qsPanel) }
        ViewUtils.detachView(qsPanel)

        assertThat(qsPanel.hadConfigurationChangeWhileDetached()).isFalse()
    }

    @Test
    fun configChangeWhileAttached_noPendingConfigChanges() {
        qsPanel.onConfigurationChanged(Configuration())

        assertThat(qsPanel.hadConfigurationChangeWhileDetached()).isFalse()
    }

    @Test
    fun configChangeWhileAttachedWithPending_doesntResetPending() {
        ViewUtils.detachView(qsPanel)

        qsPanel.onConfigurationChanged(Configuration())

        testableLooper.runWithLooper { ViewUtils.attachView(qsPanel) }

        qsPanel.onConfigurationChanged(Configuration())

        assertThat(qsPanel.hadConfigurationChangeWhileDetached()).isTrue()
    }

    companion object {
        @Parameters(name = "{0}") @JvmStatic fun getParams() = parameterizeSceneContainerFlag()
    }