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

Commit 767e466f authored by Matt Pietal's avatar Matt Pietal Committed by Android (Google) Code Review
Browse files

Merge "Dispose bindings on rebind" into main

parents 83b8cf2c 836b2715
Loading
Loading
Loading
Loading
+38 −107
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

/** Defines interface for classes that encapsulate application state for the keyguard. */
interface KeyguardRepository {
@@ -362,30 +363,8 @@ constructor(

    override val topClippingBounds = MutableStateFlow<Int?>(null)

    override val isKeyguardShowing: Flow<Boolean> =
        conflatedCallbackFlow {
                val callback =
                    object : KeyguardStateController.Callback {
                        override fun onKeyguardShowingChanged() {
                            trySendWithFailureLogging(
                                keyguardStateController.isShowing,
                                TAG,
                                "updated isKeyguardShowing"
                            )
                        }
                    }

                keyguardStateController.addCallback(callback)
                // Adding the callback does not send an initial update.
                trySendWithFailureLogging(
                    keyguardStateController.isShowing,
                    TAG,
                    "initial isKeyguardShowing"
                )

                awaitClose { keyguardStateController.removeCallback(callback) }
            }
            .distinctUntilChanged()
    override val isKeyguardShowing: MutableStateFlow<Boolean> =
        MutableStateFlow(keyguardStateController.isShowing)

    private val _isAodAvailable = MutableStateFlow(false)
    override val isAodAvailable: StateFlow<Boolean> = _isAodAvailable.asStateFlow()
@@ -394,91 +373,14 @@ constructor(
        _isAodAvailable.value = value
    }

    override val isKeyguardOccluded: Flow<Boolean> =
        conflatedCallbackFlow {
                val callback =
                    object : KeyguardStateController.Callback {
                        override fun onKeyguardShowingChanged() {
                            trySendWithFailureLogging(
                                keyguardStateController.isOccluded,
                                TAG,
                                "updated isKeyguardOccluded"
                            )
                        }
                    }

                keyguardStateController.addCallback(callback)
                // Adding the callback does not send an initial update.
                trySendWithFailureLogging(
                    keyguardStateController.isOccluded,
                    TAG,
                    "initial isKeyguardOccluded"
                )

                awaitClose { keyguardStateController.removeCallback(callback) }
            }
            .distinctUntilChanged()

    override val isKeyguardDismissible: StateFlow<Boolean> =
        conflatedCallbackFlow {
                val callback =
                    object : KeyguardStateController.Callback {
                        override fun onUnlockedChanged() {
                            trySendWithFailureLogging(
                                keyguardStateController.isUnlocked,
                                TAG,
                                "updated isKeyguardDismissible due to onUnlockedChanged"
                            )
                        }

                        override fun onKeyguardShowingChanged() {
                            trySendWithFailureLogging(
                                keyguardStateController.isUnlocked,
                                TAG,
                                "updated isKeyguardDismissible due to onKeyguardShowingChanged"
                            )
                        }
                    }

                keyguardStateController.addCallback(callback)
                // Adding the callback does not send an initial update.
                trySendWithFailureLogging(
                    keyguardStateController.isUnlocked,
                    TAG,
                    "initial isKeyguardUnlocked"
                )
    override val isKeyguardOccluded: MutableStateFlow<Boolean> =
        MutableStateFlow(keyguardStateController.isOccluded)

                awaitClose { keyguardStateController.removeCallback(callback) }
            }
            .distinctUntilChanged()
            .stateIn(
                scope,
                SharingStarted.Eagerly,
                initialValue = false,
            )
    override val isKeyguardDismissible: MutableStateFlow<Boolean> =
        MutableStateFlow(keyguardStateController.isUnlocked)

    override val isKeyguardGoingAway: Flow<Boolean> = conflatedCallbackFlow {
        val callback =
            object : KeyguardStateController.Callback {
                override fun onKeyguardGoingAwayChanged() {
                    trySendWithFailureLogging(
                        keyguardStateController.isKeyguardGoingAway,
                        TAG,
                        "updated isKeyguardGoingAway"
                    )
                }
            }

        keyguardStateController.addCallback(callback)
        // Adding the callback does not send an initial update.
        trySendWithFailureLogging(
            keyguardStateController.isKeyguardGoingAway,
            TAG,
            "initial isKeyguardGoingAway"
        )

        awaitClose { keyguardStateController.removeCallback(callback) }
    }
    override val isKeyguardGoingAway: MutableStateFlow<Boolean> =
        MutableStateFlow(keyguardStateController.isKeyguardGoingAway)

    private val _isKeyguardEnabled =
        MutableStateFlow(!lockPatternUtils.isLockScreenDisabled(userTracker.userId))
@@ -669,6 +571,35 @@ constructor(
    private val _isActiveDreamLockscreenHosted = MutableStateFlow(false)
    override val isActiveDreamLockscreenHosted = _isActiveDreamLockscreenHosted.asStateFlow()

    init {
        val callback =
            object : KeyguardStateController.Callback {
                override fun onKeyguardShowingChanged() {
                    isKeyguardShowing.value = keyguardStateController.isShowing
                    isKeyguardOccluded.value = keyguardStateController.isOccluded
                    isKeyguardDismissible.value = keyguardStateController.isUnlocked
                }

                override fun onUnlockedChanged() {
                    isKeyguardDismissible.value = keyguardStateController.isUnlocked
                }

                override fun onKeyguardGoingAwayChanged() {
                    isKeyguardGoingAway.value = keyguardStateController.isKeyguardGoingAway
                }
            }

        keyguardStateController.addCallback(callback)

        scope
            .launch {
                isKeyguardShowing.collect {
                    // no-op to allow for callback removal
                }
            }
            .invokeOnCompletion { keyguardStateController.removeCallback(callback) }
    }

    override fun setAnimateDozingTransitions(animate: Boolean) {
        _animateBottomAreaDozingTransitions.value = animate
    }
+101 −91
Original line number Diff line number Diff line
@@ -38,7 +38,9 @@ import com.android.systemui.lifecycle.repeatWhenAttached
import com.android.systemui.plugins.FalsingManager
import com.android.systemui.res.R
import com.android.systemui.statusbar.VibratorHelper
import com.android.systemui.util.kotlin.DisposableHandles
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DisposableHandle
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch

@@ -64,8 +66,9 @@ object DeviceEntryIconViewBinder {
        falsingManager: FalsingManager,
        vibratorHelper: VibratorHelper,
        overrideColor: Color? = null,
    ) {
    ): DisposableHandle {
        DeviceEntryUdfpsRefactor.isUnexpectedlyInLegacyMode()
        val disposables = DisposableHandles()
        val longPressHandlingView = view.longPressHandlingView
        val fgIconView = view.iconView
        val bgView = view.bgView
@@ -83,6 +86,7 @@ object DeviceEntryIconViewBinder {
                }
            }

        disposables +=
            view.repeatWhenAttached {
                // Repeat on CREATED so that the view will always observe the entire
                // GONE => AOD transition (even though the view may not be visible until the middle
@@ -104,7 +108,9 @@ object DeviceEntryIconViewBinder {
                                if (udfpsSupported) {
                                    {
                                        view.resources
                                        .getInteger(R.integer.config_udfpsDeviceEntryIconLongPress)
                                            .getInteger(
                                                R.integer.config_udfpsDeviceEntryIconLongPress
                                            )
                                            .toLong()
                                    }
                                } else {
@@ -155,6 +161,7 @@ object DeviceEntryIconViewBinder {
                }
            }

        disposables +=
            fgIconView.repeatWhenAttached {
                repeatOnLifecycle(Lifecycle.State.STARTED) {
                    // Start with an empty state
@@ -184,6 +191,7 @@ object DeviceEntryIconViewBinder {
                }
            }

        disposables +=
            bgView.repeatWhenAttached {
                repeatOnLifecycle(Lifecycle.State.CREATED) {
                    launch("$TAG#bgViewModel.alpha") {
@@ -196,5 +204,7 @@ object DeviceEntryIconViewBinder {
                    }
                }
            }

        return disposables
    }
}
+72 −61
Original line number Diff line number Diff line
@@ -37,7 +37,9 @@ import com.android.systemui.keyguard.ui.viewmodel.KeyguardRootViewModel
import com.android.systemui.lifecycle.repeatWhenAttached
import com.android.systemui.plugins.clocks.AodClockBurnInModel
import com.android.systemui.plugins.clocks.ClockController
import com.android.systemui.util.kotlin.DisposableHandles
import com.android.systemui.util.ui.value
import kotlinx.coroutines.DisposableHandle
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
@@ -56,13 +58,16 @@ object KeyguardClockViewBinder {
        keyguardClockInteractor: KeyguardClockInteractor,
        blueprintInteractor: KeyguardBlueprintInteractor,
        rootViewModel: KeyguardRootViewModel,
    ) {
    ): DisposableHandle {
        val disposables = DisposableHandles()
        disposables +=
            keyguardRootView.repeatWhenAttached {
                repeatOnLifecycle(Lifecycle.State.CREATED) {
                    keyguardClockInteractor.clockEventController.registerListeners(keyguardRootView)
                }
            }

        disposables +=
            keyguardRootView.repeatWhenAttached {
                repeatOnLifecycle(Lifecycle.State.CREATED) {
                    launch {
@@ -70,7 +75,11 @@ object KeyguardClockViewBinder {
                        viewModel.currentClock.collect { currentClock ->
                            cleanupClockViews(currentClock, keyguardRootView, viewModel.burnInLayer)
                            addClockViews(currentClock, keyguardRootView)
                        updateBurnInLayer(keyguardRootView, viewModel, viewModel.clockSize.value)
                            updateBurnInLayer(
                                keyguardRootView,
                                viewModel,
                                viewModel.clockSize.value
                            )
                            applyConstraints(clockSection, keyguardRootView, true)
                        }
                    }
@@ -135,6 +144,8 @@ object KeyguardClockViewBinder {
                    }
                }
            }

        return disposables
    }

    @VisibleForTesting
+3 −2
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import com.android.systemui.keyguard.ui.viewmodel.KeyguardSmartspaceViewModel
import com.android.systemui.lifecycle.repeatWhenAttached
import com.android.systemui.res.R
import com.android.systemui.shared.R as sharedR
import kotlinx.coroutines.DisposableHandle

object KeyguardSmartspaceViewBinder {
    @JvmStatic
@@ -39,8 +40,8 @@ object KeyguardSmartspaceViewBinder {
        clockViewModel: KeyguardClockViewModel,
        smartspaceViewModel: KeyguardSmartspaceViewModel,
        blueprintInteractor: KeyguardBlueprintInteractor,
    ) {
        keyguardRootView.repeatWhenAttached {
    ): DisposableHandle {
        return keyguardRootView.repeatWhenAttached {
            repeatOnLifecycle(Lifecycle.State.CREATED) {
                launch("$TAG#clockViewModel.hasCustomWeatherDataDisplay") {
                    if (!MigrateClocksToBlueprint.isEnabled) return@launch
+20 −10
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import com.android.systemui.shared.R as sharedR
import com.android.systemui.util.ui.value
import dagger.Lazy
import javax.inject.Inject
import kotlinx.coroutines.DisposableHandle

internal fun ConstraintSet.setVisibility(
    views: Iterable<View>,
@@ -70,13 +71,16 @@ constructor(
    val blueprintInteractor: Lazy<KeyguardBlueprintInteractor>,
    private val rootViewModel: KeyguardRootViewModel,
) : KeyguardSection() {
    private var disposableHandle: DisposableHandle? = null

    override fun addViews(constraintLayout: ConstraintLayout) {}

    override fun bindData(constraintLayout: ConstraintLayout) {
        if (!MigrateClocksToBlueprint.isEnabled) {
            return
        }

        disposableHandle?.dispose()
        disposableHandle =
            KeyguardClockViewBinder.bind(
                this,
                constraintLayout,
@@ -97,7 +101,13 @@ constructor(
        }
    }

    override fun removeViews(constraintLayout: ConstraintLayout) {}
    override fun removeViews(constraintLayout: ConstraintLayout) {
        if (!MigrateClocksToBlueprint.isEnabled) {
            return
        }

        disposableHandle?.dispose()
    }

    private fun buildConstraints(
        clock: ClockController,
Loading