From 79f17d1188ff3af62c9538f34ee4880c71f08a49 Mon Sep 17 00:00:00 2001 From: Haijie Hong Date: Sun, 29 Sep 2024 20:06:49 +0800 Subject: [PATCH] make device setting be able to use both Intent and PendingIntent BUG: 343317785 Test: local tested Flag: com.android.settings.flags.enable_bluetooth_device_details_polish Change-Id: I0c370d64bda1479778b55dc97e136ff73223f5d3 --- .../ui/model/DeviceSettingPreferenceModel.kt | 5 +++-- .../ui/view/DeviceDetailsFragmentFormatter.kt | 20 +++++++++++++------ .../BluetoothDeviceDetailsViewModel.kt | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt b/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt index b16bff1353b..ba6d1a6a4fb 100644 --- a/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt +++ b/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt @@ -18,6 +18,7 @@ package com.android.settings.bluetooth.ui.model import android.content.Intent import com.android.settingslib.bluetooth.devicesettings.DeviceSettingId +import com.android.settingslib.bluetooth.devicesettings.shared.model.DeviceSettingActionModel import com.android.settingslib.bluetooth.devicesettings.shared.model.DeviceSettingIcon import com.android.settingslib.bluetooth.devicesettings.shared.model.ToggleModel @@ -32,7 +33,7 @@ sealed interface DeviceSettingPreferenceModel { val title: String, val summary: String? = null, val icon: DeviceSettingIcon? = null, - val intent: Intent? = null, + val action: DeviceSettingActionModel? = null, ) : DeviceSettingPreferenceModel /** Models a switch preference. */ @@ -43,7 +44,7 @@ sealed interface DeviceSettingPreferenceModel { val icon: DeviceSettingIcon? = null, val checked: Boolean, val onCheckedChange: ((Boolean) -> Unit), - val intent: Intent? = null, + val action: DeviceSettingActionModel? = null, ) : DeviceSettingPreferenceModel /** Models a multi-toggle preference. */ diff --git a/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatter.kt b/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatter.kt index ecd700beee5..5418a04d10b 100644 --- a/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatter.kt +++ b/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatter.kt @@ -55,6 +55,7 @@ import com.android.settings.core.SubSettingLauncher import com.android.settings.overlay.FeatureFactory.Companion.featureFactory import com.android.settings.spa.preference.ComposePreference import com.android.settingslib.bluetooth.CachedBluetoothDevice +import com.android.settingslib.bluetooth.devicesettings.shared.model.DeviceSettingActionModel import com.android.settingslib.bluetooth.devicesettings.shared.model.DeviceSettingConfigItemModel import com.android.settingslib.bluetooth.devicesettings.shared.model.DeviceSettingIcon import com.android.settingslib.spa.framework.theme.SettingsDimension @@ -313,10 +314,10 @@ class DeviceDetailsFragmentFormatterImpl( return { deviceSettingIcon(model.icon) } } } - if (model.intent != null) { + if (model.action != null) { TwoTargetSwitchPreference( switchPrefModel, - primaryOnClick = { startActivity(model.intent) }, + primaryOnClick = { triggerAction(model.action) }, ) } else { SwitchPreference(switchPrefModel) @@ -330,7 +331,7 @@ class DeviceDetailsFragmentFormatterImpl( override val title = model.title override val summary = { model.summary ?: "" } override val onClick = { - model.intent?.let { startActivity(it) } + model.action?.let { triggerAction(it) } Unit } override val icon: (@Composable () -> Unit)? @@ -382,9 +383,16 @@ class DeviceDetailsFragmentFormatterImpl( icon?.let { Icon(it, modifier = Modifier.size(SettingsDimension.itemIconSize)) } } - private fun startActivity(intent: Intent) { - intent.removeFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - context.startActivity(intent) + private fun triggerAction(action: DeviceSettingActionModel) { + when (action) { + is DeviceSettingActionModel.IntentAction -> { + action.intent.removeFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + context.startActivity(action.intent) + } + is DeviceSettingActionModel.PendingIntentAction -> { + action.pendingIntent.send() + } + } } private fun getPreferenceKey(settingId: Int) = "DEVICE_SETTING_${settingId}" diff --git a/src/com/android/settings/bluetooth/ui/viewmodel/BluetoothDeviceDetailsViewModel.kt b/src/com/android/settings/bluetooth/ui/viewmodel/BluetoothDeviceDetailsViewModel.kt index fe66cb5dd51..a9444a57182 100644 --- a/src/com/android/settings/bluetooth/ui/viewmodel/BluetoothDeviceDetailsViewModel.kt +++ b/src/com/android/settings/bluetooth/ui/viewmodel/BluetoothDeviceDetailsViewModel.kt @@ -101,7 +101,7 @@ class BluetoothDeviceDetailsViewModel( DeviceSettingStateModel.ActionSwitchPreferenceState(newState) ) }, - intent = intent, + action = action, ) } else { DeviceSettingPreferenceModel.PlainPreference( @@ -109,7 +109,7 @@ class BluetoothDeviceDetailsViewModel( title = title, summary = summary, icon = icon, - intent = intent, + action = action, ) } }