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

Commit 2c637fbb authored by Haijie Hong's avatar Haijie Hong
Browse files

Implement 1-to-many Setting ID

BUG: 343317785
Test: atest DeviceSettingRepositoryTest
Flag: com.android.settings.flags.enable_bluetooth_device_details_polish
Change-Id: Id8f10e37aeee79ea6df8daa1d09dae50da1bc3a1
parent cd4bc44f
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -120,4 +120,10 @@ public @interface DeviceSettingId {

    /** Device setting ID for ANC. */
    int DEVICE_SETTING_ID_ANC = 1001;

    /** Device setting expandable ID 1. */
    int DEVICE_SETTING_ID_EXPANDABLE_1 = 3001;

    /** Device setting expandable ID 2. */
    int DEVICE_SETTING_ID_EXPANDABLE_2 = 3100;
}
+35 −7
Original line number Diff line number Diff line
@@ -106,26 +106,40 @@ class DeviceSettingRepositoryImpl(

    private fun DeviceSettingsConfig.toModel(): DeviceSettingConfigModel =
        DeviceSettingConfigModel(
            mainItems = mainContentItems.map { it.toModel() },
            moreSettingsItems = moreSettingsItems.map { it.toModel() },
            mainItems = mainContentItems.toModel(),
            moreSettingsItems = moreSettingsItems.toModel(),
            moreSettingsHelpItem = moreSettingsHelpItem?.toModel(),
        )

    private fun DeviceSettingItem.toModel(): DeviceSettingConfigItemModel {
    private fun List<DeviceSettingItem>.toModel(): List<DeviceSettingConfigItemModel> {
        return this.flatMap { item ->
            if (item.settingId in EXPANDABLE_SETTING_IDS) {
                IntRange(item.settingId, item.settingId + SETTING_ID_EXPAND_LIMIT - 1).map {
                    item.toModel(overrideSettingId = it)
                }
            } else {
                listOf(item.toModel())
            }
        }
    }

    private fun DeviceSettingItem.toModel(
        overrideSettingId: Int? = null
    ): DeviceSettingConfigItemModel {
        return if (!TextUtils.isEmpty(preferenceKey)) {
            if (settingId == DeviceSettingId.DEVICE_SETTING_ID_BLUETOOTH_PROFILES) {
                BluetoothProfilesItem(
                    settingId,
                    overrideSettingId ?: settingId,
                    highlighted,
                    preferenceKey!!,
                    extras.getStringArrayList(DeviceSettingContract.INVISIBLE_PROFILES)
                        ?: emptyList(),
                )
            } else {
                CommonBuiltinItem(settingId, highlighted, preferenceKey!!)
                CommonBuiltinItem(overrideSettingId ?: settingId, highlighted, preferenceKey!!)
            }
        } else {
            AppProvidedItem(settingId, highlighted)
            AppProvidedItem(overrideSettingId ?: settingId, highlighted)
        }
    }

@@ -134,6 +148,7 @@ class DeviceSettingRepositoryImpl(
            is DeviceSettingIntentAction -> DeviceSettingActionModel.IntentAction(this.intent)
            is DeviceSettingPendingIntentAction ->
                DeviceSettingActionModel.PendingIntentAction(this.pendingIntent)

            else -> null
        }

@@ -150,7 +165,7 @@ class DeviceSettingRepositoryImpl(
                    summary = pref.summary,
                    icon = pref.icon?.let { DeviceSettingIcon.BitmapIcon(it) },
                    isAllowedChangingState = pref.isAllowedChangingState,
                    action = pref.action?.toModel(),
                    action = pref.action.toModel(),
                    switchState =
                        if (pref.hasSwitch()) {
                            DeviceSettingStateModel.ActionSwitchPreferenceState(pref.checked)
@@ -163,6 +178,7 @@ class DeviceSettingRepositoryImpl(
                        }
                    },
                )

            is MultiTogglePreference ->
                DeviceSettingModel.MultiTogglePreference(
                    cachedDevice = cachedDevice,
@@ -178,21 +194,33 @@ class DeviceSettingRepositoryImpl(
                        }
                    },
                )

            is DeviceSettingFooterPreference ->
                DeviceSettingModel.FooterPreference(
                    cachedDevice = cachedDevice,
                    id = settingId,
                    footerText = pref.footerText,
                )

            is DeviceSettingHelpPreference ->
                DeviceSettingModel.HelpPreference(
                    cachedDevice = cachedDevice,
                    id = settingId,
                    intent = pref.intent,
                )

            else -> DeviceSettingModel.Unknown(cachedDevice, settingId)
        }

    private fun ToggleInfo.toModel(): ToggleModel =
        ToggleModel(label, DeviceSettingIcon.BitmapIcon(icon))

    companion object {
        private val EXPANDABLE_SETTING_IDS =
            listOf(
                DeviceSettingId.DEVICE_SETTING_ID_EXPANDABLE_1,
                DeviceSettingId.DEVICE_SETTING_ID_EXPANDABLE_2,
            )
        private const val SETTING_ID_EXPAND_LIMIT = 15
    }
}
+35 −0
Original line number Diff line number Diff line
@@ -166,6 +166,26 @@ class DeviceSettingRepositoryTest {
        }
    }

    @Test
    fun getDeviceSettingsConfig_expandable_success() {
        testScope.runTest {
            setUpConfigService(true, DEVICE_SETTING_CONFIG_EXPANDABLE)
            `when`(settingProviderService1.serviceStatus)
                .thenReturn(DeviceSettingsProviderServiceStatus(true))
            `when`(settingProviderService2.serviceStatus)
                .thenReturn(DeviceSettingsProviderServiceStatus(true))

            val config = underTest.getDeviceSettingsConfig(cachedDevice)!!

            assertThat(config.mainItems.map { it.settingId }).isEqualTo(
                IntRange(
                    DeviceSettingId.DEVICE_SETTING_ID_EXPANDABLE_1,
                    DeviceSettingId.DEVICE_SETTING_ID_EXPANDABLE_1 + 14
                ).toList()
            )
        }
    }

    @Test
    fun getDeviceSettingsConfig_noMetadata_returnNull() {
        testScope.runTest {
@@ -510,6 +530,13 @@ class DeviceSettingRepositoryTest {
                SETTING_PROVIDER_SERVICE_CLASS_NAME_2,
                SETTING_PROVIDER_SERVICE_INTENT_ACTION_2,
            )
        val DEVICE_SETTING_APP_PROVIDED_ITEM_EXPANDABLE =
            DeviceSettingItem(
                DeviceSettingId.DEVICE_SETTING_ID_EXPANDABLE_1,
                SETTING_PROVIDER_SERVICE_PACKAGE_NAME_1,
                SETTING_PROVIDER_SERVICE_CLASS_NAME_1,
                SETTING_PROVIDER_SERVICE_INTENT_ACTION_1,
            )
        val DEVICE_SETTING_BUILT_IN_ITEM =
            DeviceSettingItem(
                DeviceSettingId.DEVICE_SETTING_ID_BLUETOOTH_AUDIO_DEVICE_TYPE_GROUP,
@@ -581,5 +608,13 @@ class DeviceSettingRepositoryTest {
                listOf(DEVICE_SETTING_APP_PROVIDED_ITEM_2),
                DEVICE_SETTING_HELP_ITEM,
            )
        val DEVICE_SETTING_CONFIG_EXPANDABLE =
            DeviceSettingsConfig(
                listOf(
                    DEVICE_SETTING_APP_PROVIDED_ITEM_EXPANDABLE,
                ),
                listOf(),
                DEVICE_SETTING_HELP_ITEM,
            )
    }
}