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

Commit 755737cf authored by Fabián Kozynski's avatar Fabián Kozynski
Browse files

Remove removeCallbacks when processing tiles

This is not needed and can lead to the following error:
1. QSPanel added a callback for tile A
2. CurrentTilesInteractor processes tiles and removes all callbacks from
   A. After that, the list of tiles didn't change. As it's backed by a
   StateFlow, consumers don't get notified of a change (they don't need
   to, the list is the same).
3. QSPanelControllerBase doesn't get a notified so it doesn't re-add the
   callback.

The error is not usually observable, as it requires the tile setting to
change but the list of actual tiles not to change, though certain
scenarios could make it happen (like restoring from backup with tiles
that are not available, or changing tiles through adb).

Also, have QSPanelControllerBase only remove the associated callback
(and not all of them) onViewDetached.

Flag: QS_PIPELINE_NEW_HOST
Fixes: 282978588
Test: atest com.android.systemui.qs
Change-Id: I6451c80a7595bd3b3614de7b2f2870f6e97ba9d1
parent b8ef4889
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -522,7 +522,7 @@ public class QSPanel extends LinearLayout implements Tunable {
        return mExpanded;
    }

    void addTile(QSPanelControllerBase.TileRecord tileRecord) {
    final void addTile(QSPanelControllerBase.TileRecord tileRecord) {
        final QSTile.Callback callback = new QSTile.Callback() {
            @Override
            public void onStateChanged(QSTile.State state) {
+1 −1
Original line number Diff line number Diff line
@@ -199,7 +199,7 @@ public abstract class QSPanelControllerBase<T extends QSPanel> extends ViewContr
        mMediaHost.removeVisibilityChangeListener(mMediaHostVisibilityListener);

        for (TileRecord record : mRecords) {
            record.tile.removeCallbacks();
            record.tile.removeCallback(record.callback);
        }
        mRecords.clear();
        mDumpManager.unregisterDumpable(mView.getDumpableTag());
+0 −2
Original line number Diff line number Diff line
@@ -318,7 +318,6 @@ constructor(
            // We have a handful of different cases
            qsTile !is CustomTile -> {
                // The tile is not a custom tile. Make sure they are reset to the correct user
                qsTile.removeCallbacks()
                if (userChanged) {
                    qsTile.userSwitch(user)
                    logger.logTileUserChanged(tileSpec, user)
@@ -327,7 +326,6 @@ constructor(
            }
            qsTile.user == user -> {
                // The tile is a custom tile for the same user, just return it
                qsTile.removeCallbacks()
                qsTile
            }
            else -> {
+26 −0
Original line number Diff line number Diff line
@@ -321,4 +321,30 @@ public class QSPanelControllerBaseTest extends SysuiTestCase {
        assertThat(mController.shouldUseHorizontalLayout()).isFalse();
        verify(mHorizontalLayoutListener).run();
    }

    @Test
    public void changeTiles_callbackRemovedOnOldOnes() {
        // Start with one tile
        assertThat(mController.mRecords.size()).isEqualTo(1);
        QSPanelControllerBase.TileRecord record = mController.mRecords.get(0);

        assertThat(record.tile).isEqualTo(mQSTile);

        // Change to a different tile
        when(mQSHost.getTiles()).thenReturn(List.of(mOtherTile));
        mController.setTiles();

        verify(mQSTile).removeCallback(record.callback);
        verify(mOtherTile, never()).removeCallback(any());
        verify(mOtherTile, never()).removeCallbacks();
    }

    @Test
    public void onViewDetached_removesJustTheAssociatedCallback() {
        QSPanelControllerBase.TileRecord record = mController.mRecords.get(0);

        mController.onViewDetached();
        verify(mQSTile).removeCallback(record.callback);
        verify(mQSTile, never()).removeCallbacks();
    }
}
+14 −0
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ import androidx.test.filters.SmallTest
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.plugins.qs.QSTile
import com.android.systemui.plugins.qs.QSTileView
import com.android.systemui.qs.QSPanelControllerBase.TileRecord
import com.android.systemui.qs.logging.QSLogger
import com.android.systemui.qs.tileimpl.QSIconViewImpl
import com.android.systemui.qs.tileimpl.QSTileViewImpl
@@ -192,6 +194,18 @@ class QSPanelTest : SysuiTestCase() {
        verify(accessibilityInfo, never()).addAction(actionCollapse)
    }

    @Test
    fun addTile_callbackAdded() {
        val tile = mock(QSTile::class.java)
        val tileView = mock(QSTileView::class.java)

        val record = TileRecord(tile, tileView)

        qsPanel.addTile(record)

        verify(tile).addCallback(record.callback)
    }

    private infix fun View.isLeftOf(other: View): Boolean {
        val rect = Rect()
        getBoundsOnScreen(rect)
Loading