Merge "Add metrics category for more settings fragment" into main
diff --git a/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt b/src/com/android/settings/bluetooth/ui/model/DeviceSettingPreferenceModel.kt
index f6e6f16..b16bff1 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 @@
         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 @@
         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 @@
     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 a5997e7..074e520 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 @@
 
 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 @@
 ) : 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 @@
                         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 @@
                 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 @@
         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 4384eff..885217e 100644
--- a/src/com/android/settings/bluetooth/ui/view/DeviceDetailsMoreSettingsFragment.kt
+++ b/src/com/android/settings/bluetooth/ui/view/DeviceDetailsMoreSettingsFragment.kt
@@ -20,6 +20,7 @@
 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 @@
 
     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 67a0ebc..fe66cb5 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 @@
                                 DeviceSettingStateModel.ActionSwitchPreferenceState(newState)
                             )
                         },
-                        onPrimaryClick = { intent?.let { application.startActivity(it) } },
+                        intent = intent,
                     )
                 } else {
                     DeviceSettingPreferenceModel.PlainPreference(
@@ -109,7 +109,7 @@
                         title = title,
                         summary = summary,
                         icon = icon,
-                        onClick = { intent?.let { application.startActivity(it) } },
+                        intent = intent,
                     )
                 }
             }
@@ -119,7 +119,7 @@
                 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 51c0c30..1ea8044 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 @@
             }.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)
         }
     }