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

Commit 21efc2c8 authored by Hawkwood Glazier's avatar Hawkwood Glazier
Browse files

Cleanup LastClock references when detached

Bug: 380347366
Test: Manual + Perfetto
Flag: NONE Memory Bugfix
Change-Id: Ie68cbf1ef8c35cc0018f63e9ee0844fb6894ecd0
parent ffb9c126
Loading
Loading
Loading
Loading
+30 −23
Original line number Diff line number Diff line
@@ -48,8 +48,6 @@ import kotlinx.coroutines.flow.map

object KeyguardClockViewBinder {
    private val TAG = KeyguardClockViewBinder::class.simpleName!!
    // When changing to new clock, we need to remove old clock views from burnInLayer
    private var lastClock: ClockController? = null

    @JvmStatic
    fun bind(
@@ -72,10 +70,20 @@ object KeyguardClockViewBinder {
        disposables +=
            keyguardRootView.repeatWhenAttached {
                repeatOnLifecycle(Lifecycle.State.CREATED) {
                    // When changing to new clock, we need to remove old views from burnInLayer
                    var lastClock: ClockController? = null
                    launch {
                            if (!MigrateClocksToBlueprint.isEnabled) return@launch
                            viewModel.currentClock.collect { currentClock ->
                            cleanupClockViews(currentClock, keyguardRootView, viewModel.burnInLayer)
                                if (lastClock != currentClock) {
                                    cleanupClockViews(
                                        lastClock,
                                        keyguardRootView,
                                        viewModel.burnInLayer,
                                    )
                                    lastClock = currentClock
                                }

                                addClockViews(currentClock, keyguardRootView)
                                updateBurnInLayer(
                                    keyguardRootView,
@@ -85,6 +93,10 @@ object KeyguardClockViewBinder {
                                applyConstraints(clockSection, keyguardRootView, true)
                            }
                        }
                        .invokeOnCompletion {
                            cleanupClockViews(lastClock, keyguardRootView, viewModel.burnInLayer)
                            lastClock = null
                        }

                    launch {
                        if (!MigrateClocksToBlueprint.isEnabled) return@launch
@@ -185,23 +197,18 @@ object KeyguardClockViewBinder {
        viewModel.burnInLayer?.updatePostLayout(keyguardRootView)
    }

    private fun cleanupClockViews(
        currentClock: ClockController?,
    fun cleanupClockViews(
        lastClock: ClockController?,
        rootView: ConstraintLayout,
        burnInLayer: Layer?,
    ) {
        if (lastClock == currentClock) {
            return
        }

        lastClock?.let { clock ->
            clock.smallClock.layout.views.forEach {
        lastClock?.run {
            smallClock.layout.views.forEach {
                burnInLayer?.removeView(it)
                rootView.removeView(it)
            }
            clock.largeClock.layout.views.forEach { rootView.removeView(it) }
            largeClock.layout.views.forEach { rootView.removeView(it) }
        }
        lastClock = currentClock
    }

    @VisibleForTesting
+1 −0
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ object KeyguardPreviewClockViewBinder {
                                )
                            }
                        }
                        lastClock = null
                    }
            }
        }