diff --git a/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt b/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt index f6e6f168144..b16bff1353b 100644 --- a/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt +++ b/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt @@ -16,6 +16,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.DeviceSettingIcon import com.android.settingslib.bluetooth.devicesettings.shared.model.ToggleModel @@ -31,7 +32,7 @@ sealed interface DeviceSettingPreferenceModel { val title: String, val summary: String? = null, val icon: DeviceSettingIcon? = null, - val onClick: (() -> Unit)? = null, + val intent: Intent? = null, ) : DeviceSettingPreferenceModel /** Models a switch preference. */ @@ -42,7 +43,7 @@ sealed interface DeviceSettingPreferenceModel { val icon: DeviceSettingIcon? = null, val checked: Boolean, val onCheckedChange: ((Boolean) -> Unit), - val onPrimaryClick: (() -> Unit)? = null, + val intent: Intent? = null, ) : DeviceSettingPreferenceModel /** Models a multi-toggle preference. */ @@ -71,6 +72,6 @@ sealed interface DeviceSettingPreferenceModel { data class HelpPreference( @DeviceSettingId override val id: Int, val icon: DeviceSettingIcon, - val onClick: (() -> Unit), + val intent: Intent, ) : DeviceSettingPreferenceModel } diff --git a/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatter.kt b/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatter.kt index a5997e7bc83..074e520cbcc 100644 --- a/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatter.kt +++ b/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatter.kt @@ -18,6 +18,7 @@ package com.android.settings.bluetooth.ui.view import android.bluetooth.BluetoothAdapter import android.content.Context +import android.content.Intent import android.media.AudioManager import android.os.Bundle import androidx.compose.animation.AnimatedVisibility @@ -101,13 +102,13 @@ class DeviceDetailsFragmentFormatterImpl( ) : DeviceDetailsFragmentFormatter { private val repository = featureFactory.bluetoothFeatureProvider.getDeviceSettingRepository( - context, + fragment.requireActivity().application, bluetoothAdapter, fragment.lifecycleScope, ) private val spatialAudioInteractor = featureFactory.bluetoothFeatureProvider.getSpatialAudioInteractor( - context, + fragment.requireActivity().application, context.getSystemService(AudioManager::class.java), fragment.lifecycleScope, ) @@ -312,10 +313,10 @@ class DeviceDetailsFragmentFormatterImpl( return { deviceSettingIcon(model.icon) } } } - if (model.onPrimaryClick != null) { + if (model.intent != null) { TwoTargetSwitchPreference( switchPrefModel, - primaryOnClick = model.onPrimaryClick::invoke, + primaryOnClick = { startActivity(model.intent) }, ) } else { SwitchPreference(switchPrefModel) @@ -329,7 +330,7 @@ class DeviceDetailsFragmentFormatterImpl( override val title = model.title override val summary = { model.summary ?: "" } override val onClick = { - model.onClick?.invoke() + model.intent?.let { startActivity(it) } Unit } override val icon: (@Composable () -> Unit)? @@ -376,6 +377,11 @@ 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 getPreferenceKey(settingId: Int) = "DEVICE_SETTING_${settingId}" companion object { diff --git a/src/com/android/settings/bluetooth/ui/view/DeviceDetailsMoreSettingsFragment.kt b/src/com/android/settings/bluetooth/ui/view/DeviceDetailsMoreSettingsFragment.kt index c0fbd4f2c4a..98de96dfa0c 100644 --- a/src/com/android/settings/bluetooth/ui/view/DeviceDetailsMoreSettingsFragment.kt +++ b/src/com/android/settings/bluetooth/ui/view/DeviceDetailsMoreSettingsFragment.kt @@ -19,6 +19,7 @@ package com.android.settings.bluetooth.ui.view import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothManager import android.content.Context +import android.content.Intent import android.graphics.PorterDuff import android.os.Bundle import android.view.Menu @@ -73,7 +74,10 @@ class DeviceDetailsMoreSettingsFragment : DashboardFragment() { override fun onOptionsItemSelected(menuItem: MenuItem): Boolean { if (menuItem.itemId == MENU_HELP_ITEM_ID) { - helpItem.value?.let { it.onClick() } + helpItem.value?.intent?.let { + it.removeFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + requireContext().startActivity(it) + } return true } return super.onOptionsItemSelected(menuItem) diff --git a/src/com/android/settings/bluetooth/ui/viewmodel/BluetoothDeviceDetailsViewModel.kt b/src/com/android/settings/bluetooth/ui/viewmodel/BluetoothDeviceDetailsViewModel.kt index 67a0ebc8398..fe66cb5dd51 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) ) }, - onPrimaryClick = { intent?.let { application.startActivity(it) } }, + intent = intent, ) } else { DeviceSettingPreferenceModel.PlainPreference( @@ -109,7 +109,7 @@ class BluetoothDeviceDetailsViewModel( title = title, summary = summary, icon = icon, - onClick = { intent?.let { application.startActivity(it) } }, + intent = intent, ) } } @@ -119,7 +119,7 @@ class BluetoothDeviceDetailsViewModel( DeviceSettingPreferenceModel.HelpPreference( id = id, icon = DeviceSettingIcon.ResourceIcon(R.drawable.ic_help), - onClick = { application.startActivity(intent) }, + intent = intent, ) is DeviceSettingModel.MultiTogglePreference -> DeviceSettingPreferenceModel.MultiTogglePreference( diff --git a/tests/robotests/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatterTest.kt b/tests/robotests/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatterTest.kt index 51c0c3076ee..1ea804449c8 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatterTest.kt +++ b/tests/robotests/src/com/android/settings/bluetooth/ui/view/DeviceDetailsFragmentFormatterTest.kt @@ -178,11 +178,9 @@ class DeviceDetailsFragmentFormatterTest { }.launchIn(testScope.backgroundScope) delay(100) runCurrent() - helpPreference!!.onClick() ShadowLooper.idleMainLooper() - val shadowActivity = Shadows.shadowOf(fragmentActivity) - assertThat(shadowActivity.nextStartedActivity).isSameInstanceAs(intent) + assertThat(helpPreference?.intent).isSameInstanceAs(intent) } }