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

Commit 5f8b94d3 authored by Jeff DeCew's avatar Jeff DeCew Committed by Android (Google) Code Review
Browse files

Merge "Have KeyguardRootViewBinder use DisposableHandles to colocate cleanup...

Merge "Have KeyguardRootViewBinder use DisposableHandles to colocate cleanup code and reduce view boilerplate with extensions." into main
parents f3679599 15d0713b
Loading
Loading
Loading
Loading
+19 −2
Original line number Diff line number Diff line
@@ -46,8 +46,25 @@ inline fun <reified T : View> View.getNearestParent(): T? {
}

/** Adds a [View.OnLayoutChangeListener] and provides a [DisposableHandle] for teardown. */
fun View.onLayoutChanged(onLayoutChanged: (v: View) -> Unit): DisposableHandle {
    val listener = View.OnLayoutChangeListener { v, _, _, _, _, _, _, _, _ -> onLayoutChanged(v) }
fun View.onLayoutChanged(onLayoutChanged: (v: View) -> Unit): DisposableHandle =
    onLayoutChanged { v, _, _, _, _, _, _, _, _ ->
        onLayoutChanged(v)
    }

/** Adds the [View.OnLayoutChangeListener] and provides a [DisposableHandle] for teardown. */
fun View.onLayoutChanged(listener: View.OnLayoutChangeListener): DisposableHandle {
    addOnLayoutChangeListener(listener)
    return DisposableHandle { removeOnLayoutChangeListener(listener) }
}

/** Adds a [View.OnApplyWindowInsetsListener] and provides a [DisposableHandle] for teardown. */
fun View.onApplyWindowInsets(listener: View.OnApplyWindowInsetsListener): DisposableHandle {
    setOnApplyWindowInsetsListener(listener)
    return DisposableHandle { setOnApplyWindowInsetsListener(null) }
}

/** Adds a [View.OnTouchListener] and provides a [DisposableHandle] for teardown. */
fun View.onTouchListener(listener: View.OnTouchListener): DisposableHandle {
    setOnTouchListener(listener)
    return DisposableHandle { setOnTouchListener(null) }
}
+27 −24
Original line number Diff line number Diff line
@@ -41,6 +41,9 @@ import com.android.systemui.common.shared.model.Icon
import com.android.systemui.common.shared.model.Text
import com.android.systemui.common.shared.model.TintedIcon
import com.android.systemui.common.ui.ConfigurationState
import com.android.systemui.common.ui.view.onApplyWindowInsets
import com.android.systemui.common.ui.view.onLayoutChanged
import com.android.systemui.common.ui.view.onTouchListener
import com.android.systemui.deviceentry.domain.interactor.DeviceEntryHapticsInteractor
import com.android.systemui.deviceentry.shared.DeviceEntryUdfpsRefactor
import com.android.systemui.keyguard.KeyguardBottomAreaRefactor
@@ -64,6 +67,7 @@ import com.android.systemui.statusbar.phone.ScreenOffAnimationController
import com.android.systemui.temporarydisplay.ViewPriority
import com.android.systemui.temporarydisplay.chipbar.ChipbarCoordinator
import com.android.systemui.temporarydisplay.chipbar.ChipbarInfo
import com.android.systemui.util.kotlin.DisposableHandles
import com.android.systemui.util.ui.AnimatedValue
import com.android.systemui.util.ui.isAnimating
import com.android.systemui.util.ui.stopAnimating
@@ -99,13 +103,16 @@ object KeyguardRootViewBinder {
        keyguardViewMediator: KeyguardViewMediator?,
        mainImmediateDispatcher: CoroutineDispatcher,
    ): DisposableHandle {
        var onLayoutChangeListener: OnLayoutChange? = null
        val disposables = DisposableHandles()
        val childViews = mutableMapOf<Int, View>()

        if (KeyguardBottomAreaRefactor.isEnabled) {
            view.setOnTouchListener { _, event ->
            disposables +=
                view.onTouchListener { _, event ->
                    if (falsingManager?.isFalseTap(FalsingManager.LOW_PENALTY) == false) {
                    viewModel.setRootViewLastTapPosition(Point(event.x.toInt(), event.y.toInt()))
                        viewModel.setRootViewLastTapPosition(
                            Point(event.x.toInt(), event.y.toInt())
                        )
                    }
                    false
                }
@@ -117,7 +124,7 @@ object KeyguardRootViewBinder {
                alpha = { view.alpha },
            )

        val disposableHandle =
        disposables +=
            view.repeatWhenAttached(mainImmediateDispatcher) {
                repeatOnLifecycle(Lifecycle.State.CREATED) {
                    launch("$TAG#occludingAppDeviceEntryMessageViewModel.message") {
@@ -346,8 +353,7 @@ object KeyguardRootViewBinder {
            }
        }

        onLayoutChangeListener = OnLayoutChange(viewModel, childViews, burnInParams)
        view.addOnLayoutChangeListener(onLayoutChangeListener)
        disposables += view.onLayoutChanged(OnLayoutChange(viewModel, childViews, burnInParams))

        // Views will be added or removed after the call to bind(). This is needed to avoid many
        // calls to findViewById
@@ -362,8 +368,13 @@ object KeyguardRootViewBinder {
                }
            }
        )
        disposables += DisposableHandle {
            view.setOnHierarchyChangeListener(null)
            childViews.clear()
        }

        view.setOnApplyWindowInsetsListener { v: View, insets: WindowInsets ->
        disposables +=
            view.onApplyWindowInsets { _: View, insets: WindowInsets ->
                val insetTypes = WindowInsets.Type.systemBars() or WindowInsets.Type.displayCutout()
                burnInParams.update { current ->
                    current.copy(topInset = insets.getInsetsIgnoringVisibility(insetTypes).top)
@@ -371,15 +382,7 @@ object KeyguardRootViewBinder {
                insets
            }

        return object : DisposableHandle {
            override fun dispose() {
                disposableHandle.dispose()
                view.removeOnLayoutChangeListener(onLayoutChangeListener)
                view.setOnHierarchyChangeListener(null)
                view.setOnApplyWindowInsetsListener(null)
                childViews.clear()
            }
        }
        return disposables
    }

    /**
+10 −14
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.view.WindowInsets
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.android.systemui.Flags.communalHub
import com.android.systemui.common.ui.view.onApplyWindowInsets
import com.android.systemui.common.ui.view.onLayoutChanged
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.keyguard.ui.viewmodel.BurnInParameters
@@ -168,22 +170,16 @@ constructor(
        controller.setOnHeightChangedRunnable { viewModel.notificationStackChanged() }
        disposables += DisposableHandle { controller.setOnHeightChangedRunnable(null) }

        view.setOnApplyWindowInsetsListener { v: View, insets: WindowInsets ->
        disposables +=
            view.onApplyWindowInsets { _: View, insets: WindowInsets ->
                val insetTypes = WindowInsets.Type.systemBars() or WindowInsets.Type.displayCutout()
                burnInParams.update { current ->
                    current.copy(topInset = insets.getInsetsIgnoringVisibility(insetTypes).top)
                }
                insets
            }
        disposables += DisposableHandle { view.setOnApplyWindowInsetsListener(null) }

        // Required to capture keyguard media changes and ensure the notification count is correct
        val layoutChangeListener =
            View.OnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
                viewModel.notificationStackChanged()
            }
        view.addOnLayoutChangeListener(layoutChangeListener)
        disposables += DisposableHandle { view.removeOnLayoutChangeListener(layoutChangeListener) }
        disposables += view.onLayoutChanged { viewModel.notificationStackChanged() }

        return disposables
    }