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

Commit 2d222ff3 authored by brycelee's avatar brycelee
Browse files

Properly handle JavaAdapterKt flow lifetimes.

Flows created through JavaAdapterKt#collectFlow must be explicitly
canceled when not used. This changelist ties the lifetime of a number of
such flows to the view attached state.

Fixes: 420871821
Flag: EXEMPT bugfix
Test: atest ComplicationHostViewControllerTest
Test: atest AmbientStatusBarViewControllerTest
Change-Id: Id7699a2b20edcec50aa8651e05c71366d7715774
parent 0d451afe
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -156,7 +156,6 @@ public class ComplicationHostViewControllerTest extends SysuiTestCase {
    @Test
    @EnableFlags(FLAG_DREAMS_V2)
    public void updateLayoutEngine_isCalled_onConfigurationChange_flagEnabled() {
        mController.onViewAttached();
        // Attach the complication host view so flows collecting on it start running.
        ViewUtils.attachView(mComplicationHostView);
        mLooper.processAllMessages();
@@ -174,7 +173,6 @@ public class ComplicationHostViewControllerTest extends SysuiTestCase {
    @Test
    @DisableFlags(FLAG_DREAMS_V2)
    public void updateLayoutEngine_notCalled_onConfigurationChange_flagDisabled() {
        mController.onViewAttached();
        // Attach the complication host view so flows collecting on it start running.
        ViewUtils.attachView(mComplicationHostView);
        mLooper.processAllMessages();
+12 −4
Original line number Diff line number Diff line
@@ -60,6 +60,8 @@ import com.android.systemui.statusbar.window.StatusBarWindowStateListener;
import com.android.systemui.util.ViewController;
import com.android.systemui.util.time.DateFormatUtil;

import kotlinx.coroutines.DisposableHandle;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -145,6 +147,8 @@ public class AmbientStatusBarViewController extends ViewController<AmbientStatus
    private final PrivacyItemController.Callback mPrivacyItemControllerCallback =
            this::onPrivacyItemsChanged;

    private final ArrayList<DisposableHandle> mFlows = new ArrayList<>();

    @Inject
    public AmbientStatusBarViewController(
            AmbientStatusBarView view,
@@ -219,17 +223,17 @@ public class AmbientStatusBarViewController extends ViewController<AmbientStatus
                    mIconViewStoreFactory);
        }

        collectFlow(
        mFlows.add(collectFlow(
                mView,
                mWifiInteractor.getWifiNetwork(),
                network -> updateWifiUnavailableStatusIcon(
                        network instanceof WifiNetworkModel.Active));
                        network instanceof WifiNetworkModel.Active)));

        collectFlow(
        mFlows.add(collectFlow(
                mView,
                mCommunalSceneInteractor.isCommunalVisible(),
                this::onCommunalVisibleChanged
        );
        ));

        mNextAlarmController.addCallback(mNextAlarmCallback);
        updateAlarmStatusIcon();
@@ -260,6 +264,10 @@ public class AmbientStatusBarViewController extends ViewController<AmbientStatus
        mDreamOverlayStateController.setDreamOverlayStatusBarVisible(false);
        mDreamOverlayStateController.removeCallback(mDreamOverlayStateCallback);

        for (DisposableHandle flow : mFlows) {
            flow.dispose();
        }
        mFlows.clear();
        mIsAttached = false;
    }

+29 −9
Original line number Diff line number Diff line
@@ -41,7 +41,9 @@ import com.android.systemui.util.ViewController;
import com.android.systemui.util.settings.SecureSettings;

import kotlinx.coroutines.CoroutineDispatcher;
import kotlinx.coroutines.DisposableHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -67,6 +69,12 @@ public class ComplicationHostViewController extends ViewController<ConstraintLay
    private final ComplicationCollectionViewModel mComplicationCollectionViewModel;
    private final HashMap<ComplicationId, Complication.ViewHolder> mComplications = new HashMap<>();

    private final ConfigurationInteractor mConfigurationInteractor;

    private final CoroutineDispatcher mMainDispatcher;

    private final ArrayList<DisposableHandle> mFlows = new ArrayList<>();

    private final Observer<Collection<ComplicationViewModel>> mComplicationViewModelObserver =
            new Observer<>() {
                @Override
@@ -96,15 +104,9 @@ public class ComplicationHostViewController extends ViewController<ConstraintLay
        // Whether animations are enabled.
        mIsAnimationEnabled = secureSettings.getFloatForUser(
                Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f, UserHandle.USER_CURRENT) != 0.0f;
        if (Flags.dreamsV2()) {
            // Update layout on configuration change like rotation, fold etc.
            collectFlow(
                    view,
                    configurationInteractor.getMaxBounds(),
                    this::updateLayoutEngine,
                    mainDispatcher
            );
        }

        mConfigurationInteractor = configurationInteractor;
        mMainDispatcher = mainDispatcher;
    }

    /**
@@ -198,12 +200,30 @@ public class ComplicationHostViewController extends ViewController<ConstraintLay
    protected void onViewAttached() {
        mComplicationCollectionViewModel.getComplications().observe(mLifecycleOwner,
                mComplicationViewModelObserver);

        if (Flags.dreamsV2()) {
            // Update layout on configuration change like rotation, fold etc.
            mFlows.add(collectFlow(
                    mView,
                    mConfigurationInteractor.getMaxBounds(),
                    this::updateLayoutEngine,
                    mMainDispatcher
            ));
        }
    }

    @Override
    protected void onViewDetached() {
        mComplicationCollectionViewModel.getComplications().removeObserver(
                mComplicationViewModelObserver);

        if (Flags.dreamsV2()) {
            for (DisposableHandle flow : mFlows) {
                flow.dispose();
            }

            mFlows.clear();
        }
    }

    @Override