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

Commit bacca571 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Two modifications to the magnetic target finder algorithm." into main

parents 4abe48ea ee4c7c4a
Loading
Loading
Loading
Loading
+68 −4
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.whenever

/** Tests for {@link NotificationTargetsHelper}. */
@SmallTest
@@ -21,7 +22,7 @@ import org.junit.runner.RunWith
@RunWithLooper
class NotificationTargetsHelperTest : SysuiTestCase() {
    private val featureFlags = FakeFeatureFlagsClassic()
    lateinit var notificationTestHelper: NotificationTestHelper
    private lateinit var notificationTestHelper: NotificationTestHelper
    private val sectionsManager: NotificationSectionsManager = mock()
    private val stackScrollLayout: NotificationStackScrollLayout = mock()

@@ -107,7 +108,12 @@ class NotificationTargetsHelperTest : SysuiTestCase() {
        // THEN all the views that surround it become targets with the swiped view at the middle
        val actual =
            notificationTargetsHelper()
                .findMagneticTargets(viewSwiped = swiped, stackScrollLayout = stackScrollLayout, 5)
                .findMagneticTargets(
                    viewSwiped = swiped,
                    stackScrollLayout = stackScrollLayout,
                    sectionsManager,
                    totalMagneticTargets = 5,
                )
        assertMagneticTargetsForChildren(actual, children.attachedChildren)
    }

@@ -123,7 +129,12 @@ class NotificationTargetsHelperTest : SysuiTestCase() {
        // to the left
        val actual =
            notificationTargetsHelper()
                .findMagneticTargets(viewSwiped = swiped, stackScrollLayout = stackScrollLayout, 5)
                .findMagneticTargets(
                    viewSwiped = swiped,
                    stackScrollLayout = stackScrollLayout,
                    sectionsManager,
                    totalMagneticTargets = 5,
                )
        val expectedRows =
            listOf(null, null, swiped, children.attachedChildren[1], children.attachedChildren[2])
        assertMagneticTargetsForChildren(actual, expectedRows)
@@ -141,7 +152,12 @@ class NotificationTargetsHelperTest : SysuiTestCase() {
        // to the right
        val actual =
            notificationTargetsHelper()
                .findMagneticTargets(viewSwiped = swiped, stackScrollLayout = stackScrollLayout, 5)
                .findMagneticTargets(
                    viewSwiped = swiped,
                    stackScrollLayout = stackScrollLayout,
                    sectionsManager,
                    totalMagneticTargets = 5,
                )
        val expectedRows =
            listOf(
                children.attachedChildren[childrenNumber - 3],
@@ -153,6 +169,54 @@ class NotificationTargetsHelperTest : SysuiTestCase() {
        assertMagneticTargetsForChildren(actual, expectedRows)
    }

    @Test
    fun findMagneticTargets_doesNotCrossSectionAtTop() {
        val childrenNumber = 5
        val children = notificationTestHelper.createGroup(childrenNumber).childrenContainer

        // WHEN the second child is swiped and the first one begins a new section
        val swiped = children.attachedChildren[1]
        whenever(sectionsManager.beginsSection(swiped, children.attachedChildren[0])).then { true }

        // THEN the neighboring views become targets, with the swiped view at the middle and nulls
        // to the left since the top view relative to swiped begins a new section
        val actual =
            notificationTargetsHelper()
                .findMagneticTargets(
                    viewSwiped = swiped,
                    stackScrollLayout = stackScrollLayout,
                    sectionsManager,
                    totalMagneticTargets = 5,
                )
        val expectedRows =
            listOf(null, null, swiped, children.attachedChildren[2], children.attachedChildren[3])
        assertMagneticTargetsForChildren(actual, expectedRows)
    }

    @Test
    fun findMagneticTargets_doesNotCrossSectionAtBottom() {
        val childrenNumber = 5
        val children = notificationTestHelper.createGroup(childrenNumber).childrenContainer

        // WHEN the fourth child is swiped and the last one begins a new section
        val swiped = children.attachedChildren[3]
        whenever(sectionsManager.beginsSection(children.attachedChildren[4], swiped)).then { true }

        // THEN the neighboring views become targets, with the swiped view at the middle and nulls
        // to the right since the bottom view relative to swiped begins a new section
        val actual =
            notificationTargetsHelper()
                .findMagneticTargets(
                    viewSwiped = swiped,
                    stackScrollLayout = stackScrollLayout,
                    sectionsManager,
                    totalMagneticTargets = 5,
                )
        val expectedRows =
            listOf(children.attachedChildren[1], children.attachedChildren[2], swiped, null, null)
        assertMagneticTargetsForChildren(actual, expectedRows)
    }

    private fun assertMagneticTargetsForChildren(
        targets: List<MagneticRowListener?>,
        children: List<ExpandableNotificationRow?>,
+4 −1
Original line number Diff line number Diff line
@@ -95,12 +95,15 @@ constructor(
            notificationTargetsHelper.findMagneticTargets(
                expandableNotificationRow,
                stackScrollLayout,
                sectionsManager,
                MAGNETIC_TRANSLATION_MULTIPLIERS.size,
            )
        currentMagneticListeners.swipedListener()?.cancelTranslationAnimations()
        newListeners.forEach {
            if (currentMagneticListeners.contains(it)) {
                it?.cancelMagneticAnimations()
                if (it == currentMagneticListeners.swipedListener()) {
                    it?.cancelTranslationAnimations()
                }
            }
        }
        currentMagneticListeners = newListeners
+14 −6
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ import androidx.core.view.isVisible
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.statusbar.NotificationShelf
import com.android.systemui.statusbar.notification.Roundable
import com.android.systemui.statusbar.notification.footer.ui.view.FooterView
import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow
import com.android.systemui.statusbar.notification.row.ExpandableView
import javax.inject.Inject
@@ -88,6 +87,7 @@ class NotificationTargetsHelper @Inject constructor() {
     *
     * @param[viewSwiped] The [ExpandableNotificationRow] that is swiped.
     * @param[stackScrollLayout] [NotificationStackScrollLayout] container.
     * @param[sectionsManager] The [NotificationSectionsManager]
     * @param[totalMagneticTargets] The total number of magnetic listeners in the resulting list.
     *   This includes the listener of the view swiped.
     * @return The list of [MagneticRowListener]s above and below the swiped
@@ -96,6 +96,7 @@ class NotificationTargetsHelper @Inject constructor() {
    fun findMagneticTargets(
        viewSwiped: ExpandableNotificationRow,
        stackScrollLayout: NotificationStackScrollLayout,
        sectionsManager: NotificationSectionsManager,
        totalMagneticTargets: Int,
    ): List<MagneticRowListener?> {
        val notificationParent = viewSwiped.notificationParent
@@ -126,26 +127,34 @@ class NotificationTargetsHelper @Inject constructor() {
        var canMoveRight = true
        for (distance in 1..totalMagneticTargets / 2) {
            if (canMoveLeft) {
                val leftElement = container.getOrNull(index = centerIndex - distance)
                val leftElement =
                    container.getOrNull(index = centerIndex - distance)?.takeIf {
                        it.isValidMagneticBoundary() ||
                            !sectionsManager.beginsSection(view = viewSwiped, previous = it)
                    }
                if (leftElement is ExpandableNotificationRow) {
                    magneticTargets[leftIndex] = leftElement.magneticRowListener
                    leftIndex--
                } else {
                    if (leftElement.isValidMagneticBoundary()) {
                        // Add the boundary and then stop the iterating
                        // Add the boundary and then stop iterating
                        magneticTargets[leftIndex] = leftElement?.magneticRowListener
                    }
                    canMoveLeft = false
                }
            }
            if (canMoveRight) {
                val rightElement = container.getOrNull(index = centerIndex + distance)
                val rightElement =
                    container.getOrNull(index = centerIndex + distance)?.takeIf {
                        it.isValidMagneticBoundary() ||
                            !sectionsManager.beginsSection(view = it, previous = viewSwiped)
                    }
                if (rightElement is ExpandableNotificationRow) {
                    magneticTargets[rightIndex] = rightElement.magneticRowListener
                    rightIndex++
                } else {
                    if (rightElement.isValidMagneticBoundary()) {
                        // Add the boundary and then stop the iterating
                        // Add the boundary and then stop iterating
                        magneticTargets[rightIndex] = rightElement?.magneticRowListener
                    }
                    canMoveRight = false
@@ -157,7 +166,6 @@ class NotificationTargetsHelper @Inject constructor() {

    private fun ExpandableView?.isValidMagneticBoundary(): Boolean =
        when (this) {
            is FooterView,
            is NotificationShelf,
            is SectionHeaderView -> true
            else -> false