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

Commit 4a3ffa46 authored by Darrell Shi's avatar Darrell Shi Committed by Android (Google) Code Review
Browse files

Merge changes I7af9b535,Iea88a24e,I69f8e6e1 into main

* changes:
  Reverse ranks order and add widgets at the end
  Database migration to reverse ranks
  Rename priority to rank
parents 6ed2b179 57e034a0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1154,7 +1154,7 @@ private fun WidgetContent(
                .then(selectableModifier)
                .thenIf(!viewModel.isEditMode && !model.inQuietMode) {
                    Modifier.pointerInput(Unit) {
                        observeTaps { viewModel.onTapWidget(model.componentName, model.priority) }
                        observeTaps { viewModel.onTapWidget(model.componentName, model.rank) }
                    }
                }
                .thenIf(!viewModel.isEditMode && model.inQuietMode) {
+16 −17
Original line number Diff line number Diff line
@@ -34,11 +34,11 @@ fun rememberContentListState(
    return remember(communalContent) {
        ContentListState(
            communalContent,
            { componentName, user, priority ->
            { componentName, user, rank ->
                viewModel.onAddWidget(
                    componentName,
                    user,
                    priority,
                    rank,
                    widgetConfigurator,
                )
            },
@@ -56,10 +56,9 @@ fun rememberContentListState(
class ContentListState
internal constructor(
    communalContent: List<CommunalContentModel>,
    private val onAddWidget:
        (componentName: ComponentName, user: UserHandle, priority: Int) -> Unit,
    private val onDeleteWidget: (id: Int, componentName: ComponentName, priority: Int) -> Unit,
    private val onReorderWidgets: (widgetIdToPriorityMap: Map<Int, Int>) -> Unit,
    private val onAddWidget: (componentName: ComponentName, user: UserHandle, rank: Int) -> Unit,
    private val onDeleteWidget: (id: Int, componentName: ComponentName, rank: Int) -> Unit,
    private val onReorderWidgets: (widgetIdToRankMap: Map<Int, Int>) -> Unit,
) {
    var list = communalContent.toMutableStateList()
        private set
@@ -74,7 +73,7 @@ internal constructor(
        if (list[indexToRemove].isWidgetContent()) {
            val widget = list[indexToRemove] as CommunalContentModel.WidgetContent
            list.apply { removeAt(indexToRemove) }
            onDeleteWidget(widget.appWidgetId, widget.componentName, widget.priority)
            onDeleteWidget(widget.appWidgetId, widget.componentName, widget.rank)
        }
    }

@@ -94,24 +93,24 @@ internal constructor(
        newItemUser: UserHandle? = null,
        newItemIndex: Int? = null
    ) {
        // filters placeholder, but, maintains the indices of the widgets as if the placeholder was
        // in the list. When persisted in DB, this leaves space for the new item (to be added) at
        // the correct priority.
        val widgetIdToPriorityMap: Map<Int, Int> =
        // New widget added to the grid. Other widgets are shifted as needed at the database level.
        if (newItemComponentName != null && newItemUser != null && newItemIndex != null) {
            onAddWidget(newItemComponentName, newItemUser, /* rank= */ newItemIndex)
            return
        }

        // No new widget, only reorder existing widgets.
        val widgetIdToRankMap: Map<Int, Int> =
            list
                .mapIndexedNotNull { index, item ->
                    if (item is CommunalContentModel.WidgetContent) {
                        item.appWidgetId to list.size - index
                        item.appWidgetId to index
                    } else {
                        null
                    }
                }
                .toMap()
        // reorder and then add the new widget
        onReorderWidgets(widgetIdToPriorityMap)
        if (newItemComponentName != null && newItemUser != null && newItemIndex != null) {
            onAddWidget(newItemComponentName, newItemUser, /* priority= */ list.size - newItemIndex)
        }
        onReorderWidgets(widgetIdToRankMap)
    }

    /** Returns true if the item at given index is editable. */
+1 −1
Original line number Diff line number Diff line
@@ -193,7 +193,7 @@ internal class DragAndDropTargetState(
            val widgetExtra = event.maybeWidgetExtra() ?: return false
            val (componentName, user) = widgetExtra
            if (componentName != null && user != null) {
                // Placeholder isn't removed yet to allow the setting the right priority for items
                // Placeholder isn't removed yet to allow the setting the right rank for items
                // before adding in the new item.
                contentListState.onSaveList(
                    newItemComponentName = componentName,
+4 −4
Original line number Diff line number Diff line
@@ -115,21 +115,21 @@ class DefaultWidgetPopulationTest : SysuiTestCase() {
                .addWidget(
                    widgetId = 0,
                    componentName = defaultWidgets[0],
                    priority = 3,
                    rank = 0,
                    userSerialNumber = 0,
                )
            verify(communalWidgetDao)
                .addWidget(
                    widgetId = 1,
                    componentName = defaultWidgets[1],
                    priority = 2,
                    rank = 1,
                    userSerialNumber = 0,
                )
            verify(communalWidgetDao)
                .addWidget(
                    widgetId = 2,
                    componentName = defaultWidgets[2],
                    priority = 1,
                    rank = 2,
                    userSerialNumber = 0,
                )
        }
@@ -150,7 +150,7 @@ class DefaultWidgetPopulationTest : SysuiTestCase() {
                .addWidget(
                    widgetId = anyInt(),
                    componentName = any(),
                    priority = anyInt(),
                    rank = anyInt(),
                    userSerialNumber = anyInt(),
                )
        }
+24 −26
Original line number Diff line number Diff line
@@ -154,7 +154,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    CommunalWidgetContentModel.Available(
                        appWidgetId = communalWidgetItemEntry.widgetId,
                        providerInfo = providerInfoA,
                        priority = communalItemRankEntry.rank,
                        rank = communalItemRankEntry.rank,
                    )
                )

@@ -190,12 +190,12 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    CommunalWidgetContentModel.Available(
                        appWidgetId = 1,
                        providerInfo = providerInfoA,
                        priority = 1,
                        rank = 1,
                    ),
                    CommunalWidgetContentModel.Available(
                        appWidgetId = 2,
                        providerInfo = providerInfoB,
                        priority = 2,
                        rank = 2,
                    ),
                )
        }
@@ -225,12 +225,12 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    CommunalWidgetContentModel.Available(
                        appWidgetId = 1,
                        providerInfo = providerInfoA,
                        priority = 1,
                        rank = 1,
                    ),
                    CommunalWidgetContentModel.Available(
                        appWidgetId = 2,
                        providerInfo = providerInfoB,
                        priority = 2,
                        rank = 2,
                    ),
                )

@@ -248,12 +248,12 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                        appWidgetId = 1,
                        // Verify that provider info updated
                        providerInfo = providerInfoC,
                        priority = 1,
                        rank = 1,
                    ),
                    CommunalWidgetContentModel.Available(
                        appWidgetId = 2,
                        providerInfo = providerInfoB,
                        priority = 2,
                        rank = 2,
                    ),
                )
        }
@@ -263,7 +263,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
        testScope.runTest {
            val provider = ComponentName("pkg_name", "cls_name")
            val id = 1
            val priority = 1
            val rank = 1
            whenever(communalWidgetHost.getAppWidgetInfo(id))
                .thenReturn(PROVIDER_INFO_REQUIRES_CONFIGURATION)
            whenever(
@@ -273,12 +273,11 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    )
                )
                .thenReturn(id)
            underTest.addWidget(provider, mainUser, priority, kosmos.widgetConfiguratorSuccess)
            underTest.addWidget(provider, mainUser, rank, kosmos.widgetConfiguratorSuccess)
            runCurrent()

            verify(communalWidgetHost).allocateIdAndBindWidget(provider, mainUser)
            verify(communalWidgetDao)
                .addWidget(id, provider, priority, testUserSerialNumber(mainUser))
            verify(communalWidgetDao).addWidget(id, provider, rank, testUserSerialNumber(mainUser))

            // Verify backup requested
            verify(backupManager).dataChanged()
@@ -289,7 +288,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
        testScope.runTest {
            val provider = ComponentName("pkg_name", "cls_name")
            val id = 1
            val priority = 1
            val rank = 1
            whenever(communalWidgetHost.getAppWidgetInfo(id))
                .thenReturn(PROVIDER_INFO_REQUIRES_CONFIGURATION)
            whenever(
@@ -299,7 +298,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    )
                )
                .thenReturn(id)
            underTest.addWidget(provider, mainUser, priority, kosmos.widgetConfiguratorFail)
            underTest.addWidget(provider, mainUser, rank, kosmos.widgetConfiguratorFail)
            runCurrent()

            verify(communalWidgetHost).allocateIdAndBindWidget(provider, mainUser)
@@ -316,7 +315,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
        testScope.runTest {
            val provider = ComponentName("pkg_name", "cls_name")
            val id = 1
            val priority = 1
            val rank = 1
            whenever(communalWidgetHost.getAppWidgetInfo(id))
                .thenReturn(PROVIDER_INFO_REQUIRES_CONFIGURATION)
            whenever(
@@ -326,7 +325,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    )
                )
                .thenReturn(id)
            underTest.addWidget(provider, mainUser, priority) {
            underTest.addWidget(provider, mainUser, rank) {
                throw IllegalStateException("some error")
            }
            runCurrent()
@@ -345,7 +344,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
        testScope.runTest {
            val provider = ComponentName("pkg_name", "cls_name")
            val id = 1
            val priority = 1
            val rank = 1
            whenever(communalWidgetHost.getAppWidgetInfo(id))
                .thenReturn(PROVIDER_INFO_CONFIGURATION_OPTIONAL)
            whenever(
@@ -355,12 +354,11 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    )
                )
                .thenReturn(id)
            underTest.addWidget(provider, mainUser, priority, kosmos.widgetConfiguratorFail)
            underTest.addWidget(provider, mainUser, rank, kosmos.widgetConfiguratorFail)
            runCurrent()

            verify(communalWidgetHost).allocateIdAndBindWidget(provider, mainUser)
            verify(communalWidgetDao)
                .addWidget(id, provider, priority, testUserSerialNumber(mainUser))
            verify(communalWidgetDao).addWidget(id, provider, rank, testUserSerialNumber(mainUser))

            // Verify backup requested
            verify(backupManager).dataChanged()
@@ -399,11 +397,11 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
    @Test
    fun reorderWidgets_queryDb() =
        testScope.runTest {
            val widgetIdToPriorityMap = mapOf(104 to 1, 103 to 2, 101 to 3)
            underTest.updateWidgetOrder(widgetIdToPriorityMap)
            val widgetIdToRankMap = mapOf(104 to 1, 103 to 2, 101 to 3)
            underTest.updateWidgetOrder(widgetIdToRankMap)
            runCurrent()

            verify(communalWidgetDao).updateWidgetOrder(widgetIdToPriorityMap)
            verify(communalWidgetDao).updateWidgetOrder(widgetIdToRankMap)

            // Verify backup requested
            verify(backupManager).dataChanged()
@@ -691,11 +689,11 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    CommunalWidgetContentModel.Available(
                        appWidgetId = 1,
                        providerInfo = providerInfoA,
                        priority = 1,
                        rank = 1,
                    ),
                    CommunalWidgetContentModel.Pending(
                        appWidgetId = 2,
                        priority = 2,
                        rank = 2,
                        componentName = ComponentName("pk_2", "cls_2"),
                        icon = fakeIcon,
                        user = mainUser,
@@ -730,7 +728,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                .containsExactly(
                    CommunalWidgetContentModel.Pending(
                        appWidgetId = 1,
                        priority = 1,
                        rank = 1,
                        componentName = ComponentName("pk_1", "cls_1"),
                        icon = fakeIcon,
                        user = mainUser,
@@ -750,7 +748,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    CommunalWidgetContentModel.Available(
                        appWidgetId = 1,
                        providerInfo = providerInfoA,
                        priority = 1,
                        rank = 1,
                    ),
                )
        }
Loading