From 51f86a1592e78498e76efbead5841059c2698be4 Mon Sep 17 00:00:00 2001 From: Alejandro Nijamkin Date: Wed, 5 Apr 2023 16:37:55 -0700 Subject: [PATCH] Fixes RTL bug in quick affordance slot tabs. When rendering with a right-to-left language, we shouldn't flip the order of the slot names; this way, the "right" slot tab remains on the right and the "left" slot tab remains on the left, regardless of the layout direction of the current locale. Fix: 276477249 Test: included unit test Test: manually verified that "Left shortcut" is always on the left and "Right shortcut" is always on the right, regardless of the direction of the current system language (used Hebrew to verify). Change-Id: Ibcffff3b4f26231c78299fd8afe4a16fe5cb1b11 --- .../KeyguardQuickAffordanceRepository.kt | 50 +++++++++---------- .../KeyguardQuickAffordanceRepositoryTest.kt | 36 +++++++++++++ 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardQuickAffordanceRepository.kt b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardQuickAffordanceRepository.kt index 8ece3183fd569..ab4abbf3d5751 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardQuickAffordanceRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardQuickAffordanceRepository.kt @@ -19,6 +19,7 @@ package com.android.systemui.keyguard.data.repository import android.content.Context import android.os.UserHandle +import android.util.LayoutDirection import com.android.systemui.Dumpable import com.android.systemui.R import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging @@ -113,30 +114,6 @@ constructor( initialValue = emptyMap(), ) - private val _slotPickerRepresentations: List by lazy { - fun parseSlot(unparsedSlot: String): Pair { - val split = unparsedSlot.split(SLOT_CONFIG_DELIMITER) - check(split.size == 2) - val slotId = split[0] - val slotCapacity = split[1].toInt() - return slotId to slotCapacity - } - - val unparsedSlots = - appContext.resources.getStringArray(R.array.config_keyguardQuickAffordanceSlots) - - val seenSlotIds = mutableSetOf() - unparsedSlots.mapNotNull { unparsedSlot -> - val (slotId, slotCapacity) = parseSlot(unparsedSlot) - check(!seenSlotIds.contains(slotId)) { "Duplicate slot \"$slotId\"!" } - seenSlotIds.add(slotId) - KeyguardSlotPickerRepresentation( - id = slotId, - maxSelectedAffordances = slotCapacity, - ) - } - } - init { legacySettingSyncer.startSyncing() dumpManager.registerDumpable("KeyguardQuickAffordances", Dumpster()) @@ -211,7 +188,30 @@ constructor( * each slot and select which affordance(s) is/are installed in each slot on the keyguard. */ fun getSlotPickerRepresentations(): List { - return _slotPickerRepresentations + fun parseSlot(unparsedSlot: String): Pair { + val split = unparsedSlot.split(SLOT_CONFIG_DELIMITER) + check(split.size == 2) + val slotId = split[0] + val slotCapacity = split[1].toInt() + return slotId to slotCapacity + } + + val unparsedSlots = + appContext.resources.getStringArray(R.array.config_keyguardQuickAffordanceSlots) + if (appContext.resources.configuration.layoutDirection == LayoutDirection.RTL) { + unparsedSlots.reverse() + } + + val seenSlotIds = mutableSetOf() + return unparsedSlots.mapNotNull { unparsedSlot -> + val (slotId, slotCapacity) = parseSlot(unparsedSlot) + check(!seenSlotIds.contains(slotId)) { "Duplicate slot \"$slotId\"!" } + seenSlotIds.add(slotId) + KeyguardSlotPickerRepresentation( + id = slotId, + maxSelectedAffordances = slotCapacity, + ) + } } private inner class Dumpster : Dumpable { diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/KeyguardQuickAffordanceRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/KeyguardQuickAffordanceRepositoryTest.kt index 86e8c9accc457..a668af340e7bd 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/KeyguardQuickAffordanceRepositoryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/KeyguardQuickAffordanceRepositoryTest.kt @@ -41,6 +41,7 @@ import com.android.systemui.util.mockito.mock import com.android.systemui.util.mockito.whenever import com.android.systemui.util.settings.FakeSettings import com.google.common.truth.Truth.assertThat +import java.util.Locale import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope @@ -67,6 +68,7 @@ class KeyguardQuickAffordanceRepositoryTest : SysuiTestCase() { @Before fun setUp() { + context.resources.configuration.setLayoutDirection(Locale.US) config1 = FakeKeyguardQuickAffordanceConfig(FakeCustomizationProviderClient.AFFORDANCE_1) config2 = FakeKeyguardQuickAffordanceConfig(FakeCustomizationProviderClient.AFFORDANCE_2) val testDispatcher = StandardTestDispatcher() @@ -221,6 +223,40 @@ class KeyguardQuickAffordanceRepositoryTest : SysuiTestCase() { ) } + @Test + fun getSlotPickerRepresentations_rightToLeft_slotsReversed() { + context.resources.configuration.setLayoutDirection(Locale("he", "IL")) + val slot1 = "slot1" + val slot2 = "slot2" + val slot3 = "slot3" + context.orCreateTestableResources.addOverride( + R.array.config_keyguardQuickAffordanceSlots, + arrayOf( + "$slot1:2", + "$slot2:4", + "$slot3:5", + ), + ) + + assertThat(underTest.getSlotPickerRepresentations()) + .isEqualTo( + listOf( + KeyguardSlotPickerRepresentation( + id = slot3, + maxSelectedAffordances = 5, + ), + KeyguardSlotPickerRepresentation( + id = slot2, + maxSelectedAffordances = 4, + ), + KeyguardSlotPickerRepresentation( + id = slot1, + maxSelectedAffordances = 2, + ), + ) + ) + } + @Test fun `selections for secondary user`() = testScope.runTest {