Merge "Make transition ready if display becomes empty" into main
diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp
index 8668df5..58c39b4 100644
--- a/packages/SystemUI/Android.bp
+++ b/packages/SystemUI/Android.bp
@@ -368,6 +368,7 @@
         "tests/src/**/systemui/shared/system/RemoteTransitionTest.java",
         "tests/src/**/systemui/navigationbar/NavigationBarControllerImplTest.java",
         "tests/src/**/systemui/bluetooth/qsdialog/AudioSharingInteractorTest.kt",
+        "tests/src/**/systemui/bluetooth/qsdialog/DeviceItemActionInteractorTest.kt",
         "tests/src/**/systemui/notetask/quickaffordance/NoteTaskQuickAffordanceConfigTest.kt",
         "tests/src/**/systemui/notetask/LaunchNotesRoleSettingsTrampolineActivityTest.kt",
         "tests/src/**/systemui/notetask/shortcut/LaunchNoteTaskActivityTest.kt",
diff --git a/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogLogger.kt b/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogLogger.kt
index c30aea0..72312b8 100644
--- a/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogLogger.kt
+++ b/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogLogger.kt
@@ -16,6 +16,7 @@
 
 package com.android.systemui.bluetooth.qsdialog
 
+import android.bluetooth.BluetoothDevice
 import com.android.systemui.log.LogBuffer
 import com.android.systemui.log.core.LogLevel.DEBUG
 import com.android.systemui.log.dagger.BluetoothTileDialogLog
@@ -103,4 +104,29 @@
 
     fun logDeviceUiUpdate(duration: Long) =
         logBuffer.log(TAG, DEBUG, { long1 = duration }, { "DeviceUiUpdate. duration=$long1" })
+
+    fun logDeviceClickInAudioSharingWhenEnabled(inAudioSharing: Boolean) {
+        logBuffer.log(
+            TAG,
+            DEBUG,
+            { str1 = inAudioSharing.toString() },
+            { "DeviceClick. in audio sharing=$str1" }
+        )
+    }
+
+    fun logConnectedLeByGroupId(map: Map<Int, List<BluetoothDevice>>) {
+        logBuffer.log(TAG, DEBUG, { str1 = map.toString() }, { "ConnectedLeByGroupId. map=$str1" })
+    }
+
+    fun logLaunchSettingsCriteriaMatched(criteria: String, deviceItem: DeviceItem) {
+        logBuffer.log(
+            TAG,
+            DEBUG,
+            {
+                str1 = criteria
+                str2 = deviceItem.toString()
+            },
+            { "$str1. deviceItem=$str2" }
+        )
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogModule.kt b/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogModule.kt
deleted file mode 100644
index 2e9169e..0000000
--- a/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogModule.kt
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (C) 2024 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.systemui.bluetooth.qsdialog
-
-import com.android.systemui.dagger.SysUISingleton
-import dagger.Binds
-import dagger.Module
-
-@Module
-interface BluetoothTileDialogModule {
-    @Binds
-    @SysUISingleton
-    fun bindDeviceItemActionInteractor(
-        impl: DeviceItemActionInteractorImpl
-    ): DeviceItemActionInteractor
-}
diff --git a/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogUiEvent.kt b/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogUiEvent.kt
index b592b8e..4a358c0 100644
--- a/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogUiEvent.kt
+++ b/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/BluetoothTileDialogUiEvent.kt
@@ -35,7 +35,17 @@
     CONNECTED_OTHER_DEVICE_DISCONNECT(1508),
     @UiEvent(doc = "The auto on toggle is clicked") BLUETOOTH_AUTO_ON_TOGGLE_CLICKED(1617),
     @UiEvent(doc = "The audio sharing button is clicked")
-    BLUETOOTH_AUDIO_SHARING_BUTTON_CLICKED(1700);
+    BLUETOOTH_AUDIO_SHARING_BUTTON_CLICKED(1700),
+    @UiEvent(doc = "Currently broadcasting and a LE audio supported device is clicked")
+    LAUNCH_SETTINGS_IN_SHARING_LE_DEVICE_CLICKED(1717),
+    @UiEvent(doc = "Currently broadcasting and a non-LE audio supported device is clicked")
+    LAUNCH_SETTINGS_IN_SHARING_NON_LE_DEVICE_CLICKED(1718),
+    @UiEvent(
+        doc = "Not broadcasting, having one connected, another saved LE audio device is clicked"
+    )
+    LAUNCH_SETTINGS_NOT_SHARING_SAVED_LE_DEVICE_CLICKED(1719),
+    @UiEvent(doc = "Not broadcasting, one of the two connected LE audio devices is clicked")
+    LAUNCH_SETTINGS_NOT_SHARING_CONNECTED_LE_DEVICE_CLICKED(1720);
 
     override fun getId() = metricId
 }
diff --git a/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractor.kt b/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractor.kt
index 9311760..4dafa93 100644
--- a/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractor.kt
@@ -16,32 +16,87 @@
 
 package com.android.systemui.bluetooth.qsdialog
 
+import android.bluetooth.BluetoothDevice
+import android.bluetooth.BluetoothProfile
+import android.content.Intent
+import android.os.Bundle
+import android.provider.Settings
 import com.android.internal.logging.UiEventLogger
+import com.android.settingslib.bluetooth.A2dpProfile
+import com.android.settingslib.bluetooth.BluetoothUtils
+import com.android.settingslib.bluetooth.HeadsetProfile
+import com.android.settingslib.bluetooth.HearingAidProfile
+import com.android.settingslib.bluetooth.LeAudioProfile
+import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast
+import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant
+import com.android.settingslib.bluetooth.LocalBluetoothManager
+import com.android.systemui.animation.DialogTransitionAnimator
+import com.android.systemui.bluetooth.qsdialog.DeviceItemActionInteractor.LaunchSettingsCriteria.Companion.getCurrentConnectedLeByGroupId
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.dagger.qualifiers.Background
+import com.android.systemui.plugins.ActivityStarter
 import com.android.systemui.statusbar.phone.SystemUIDialog
 import javax.inject.Inject
 import kotlinx.coroutines.CoroutineDispatcher
 import kotlinx.coroutines.withContext
 
-/** Defines interface for click handling of a DeviceItem. */
-interface DeviceItemActionInteractor {
-    suspend fun onClick(deviceItem: DeviceItem, dialog: SystemUIDialog)
-}
-
 @SysUISingleton
-open class DeviceItemActionInteractorImpl
+class DeviceItemActionInteractor
 @Inject
 constructor(
+    private val activityStarter: ActivityStarter,
+    private val dialogTransitionAnimator: DialogTransitionAnimator,
+    private val localBluetoothManager: LocalBluetoothManager?,
     @Background private val backgroundDispatcher: CoroutineDispatcher,
     private val logger: BluetoothTileDialogLogger,
     private val uiEventLogger: UiEventLogger,
-) : DeviceItemActionInteractor {
+) {
+    private val leAudioProfile: LeAudioProfile?
+        get() = localBluetoothManager?.profileManager?.leAudioProfile
 
-    override suspend fun onClick(deviceItem: DeviceItem, dialog: SystemUIDialog) {
+    private val assistantProfile: LocalBluetoothLeBroadcastAssistant?
+        get() = localBluetoothManager?.profileManager?.leAudioBroadcastAssistantProfile
+
+    private val launchSettingsCriteriaList: List<LaunchSettingsCriteria>
+        get() =
+            listOf(
+                InSharingClickedNoSource(localBluetoothManager, backgroundDispatcher, logger),
+                NotSharingClickedNonConnect(
+                    leAudioProfile,
+                    assistantProfile,
+                    backgroundDispatcher,
+                    logger
+                ),
+                NotSharingClickedConnected(
+                    leAudioProfile,
+                    assistantProfile,
+                    backgroundDispatcher,
+                    logger
+                )
+            )
+
+    suspend fun onClick(deviceItem: DeviceItem, dialog: SystemUIDialog) {
         withContext(backgroundDispatcher) {
             logger.logDeviceClick(deviceItem.cachedBluetoothDevice.address, deviceItem.type)
+            if (
+                BluetoothUtils.isAudioSharingEnabled() &&
+                    localBluetoothManager != null &&
+                    leAudioProfile != null &&
+                    assistantProfile != null
+            ) {
+                val inAudioSharing = BluetoothUtils.isBroadcasting(localBluetoothManager)
+                logger.logDeviceClickInAudioSharingWhenEnabled(inAudioSharing)
 
+                val criteriaMatched =
+                    launchSettingsCriteriaList.firstOrNull {
+                        it.matched(inAudioSharing, deviceItem)
+                    }
+                if (criteriaMatched != null) {
+                    uiEventLogger.log(criteriaMatched.getClickUiEvent(deviceItem))
+                    launchSettings(deviceItem.cachedBluetoothDevice.device, dialog)
+                    return@withContext
+                }
+            }
             deviceItem.cachedBluetoothDevice.apply {
                 when (deviceItem.type) {
                     DeviceItemType.ACTIVE_MEDIA_BLUETOOTH_DEVICE -> {
@@ -69,4 +124,184 @@
             }
         }
     }
+
+    private fun launchSettings(device: BluetoothDevice, dialog: SystemUIDialog) {
+        val intent =
+            Intent(Settings.ACTION_BLUETOOTH_SETTINGS).apply {
+                putExtra(
+                    EXTRA_SHOW_FRAGMENT_ARGUMENTS,
+                    Bundle().apply {
+                        putParcelable(LocalBluetoothLeBroadcast.EXTRA_BLUETOOTH_DEVICE, device)
+                    }
+                )
+            }
+        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
+        activityStarter.postStartActivityDismissingKeyguard(
+            intent,
+            0,
+            dialogTransitionAnimator.createActivityTransitionController(dialog)
+        )
+    }
+
+    private interface LaunchSettingsCriteria {
+        suspend fun matched(inAudioSharing: Boolean, deviceItem: DeviceItem): Boolean
+
+        suspend fun getClickUiEvent(deviceItem: DeviceItem): BluetoothTileDialogUiEvent
+
+        companion object {
+            suspend fun getCurrentConnectedLeByGroupId(
+                leAudioProfile: LeAudioProfile,
+                assistantProfile: LocalBluetoothLeBroadcastAssistant,
+                @Background backgroundDispatcher: CoroutineDispatcher,
+                logger: BluetoothTileDialogLogger,
+            ): Map<Int, List<BluetoothDevice>> {
+                return withContext(backgroundDispatcher) {
+                    assistantProfile
+                        .getDevicesMatchingConnectionStates(
+                            intArrayOf(BluetoothProfile.STATE_CONNECTED)
+                        )
+                        ?.filterNotNull()
+                        ?.groupBy { leAudioProfile.getGroupId(it) }
+                        ?.also { logger.logConnectedLeByGroupId(it) } ?: emptyMap()
+                }
+            }
+        }
+    }
+
+    private class InSharingClickedNoSource(
+        private val localBluetoothManager: LocalBluetoothManager?,
+        @Background private val backgroundDispatcher: CoroutineDispatcher,
+        private val logger: BluetoothTileDialogLogger,
+    ) : LaunchSettingsCriteria {
+        // If currently broadcasting and the clicked device is not connected to the source
+        override suspend fun matched(inAudioSharing: Boolean, deviceItem: DeviceItem): Boolean {
+            return withContext(backgroundDispatcher) {
+                val matched =
+                    inAudioSharing &&
+                        deviceItem.isMediaDevice &&
+                        !BluetoothUtils.hasConnectedBroadcastSource(
+                            deviceItem.cachedBluetoothDevice,
+                            localBluetoothManager
+                        )
+
+                if (matched) {
+                    logger.logLaunchSettingsCriteriaMatched("InSharingClickedNoSource", deviceItem)
+                }
+
+                matched
+            }
+        }
+
+        override suspend fun getClickUiEvent(deviceItem: DeviceItem) =
+            if (deviceItem.isLeAudioSupported)
+                BluetoothTileDialogUiEvent.LAUNCH_SETTINGS_IN_SHARING_LE_DEVICE_CLICKED
+            else BluetoothTileDialogUiEvent.LAUNCH_SETTINGS_IN_SHARING_NON_LE_DEVICE_CLICKED
+    }
+
+    private class NotSharingClickedNonConnect(
+        private val leAudioProfile: LeAudioProfile?,
+        private val assistantProfile: LocalBluetoothLeBroadcastAssistant?,
+        @Background private val backgroundDispatcher: CoroutineDispatcher,
+        private val logger: BluetoothTileDialogLogger,
+    ) : LaunchSettingsCriteria {
+        // If not broadcasting, having one device connected, and clicked on a not yet connected LE
+        // audio device
+        override suspend fun matched(inAudioSharing: Boolean, deviceItem: DeviceItem): Boolean {
+            return withContext(backgroundDispatcher) {
+                val matched =
+                    leAudioProfile?.let { leAudio ->
+                        assistantProfile?.let { assistant ->
+                            !inAudioSharing &&
+                                getCurrentConnectedLeByGroupId(
+                                        leAudio,
+                                        assistant,
+                                        backgroundDispatcher,
+                                        logger
+                                    )
+                                    .size == 1 &&
+                                deviceItem.isNotConnectedLeAudioSupported
+                        }
+                    } ?: false
+
+                if (matched) {
+                    logger.logLaunchSettingsCriteriaMatched(
+                        "NotSharingClickedNonConnect",
+                        deviceItem
+                    )
+                }
+
+                matched
+            }
+        }
+
+        override suspend fun getClickUiEvent(deviceItem: DeviceItem) =
+            BluetoothTileDialogUiEvent.LAUNCH_SETTINGS_NOT_SHARING_SAVED_LE_DEVICE_CLICKED
+    }
+
+    private class NotSharingClickedConnected(
+        private val leAudioProfile: LeAudioProfile?,
+        private val assistantProfile: LocalBluetoothLeBroadcastAssistant?,
+        @Background private val backgroundDispatcher: CoroutineDispatcher,
+        private val logger: BluetoothTileDialogLogger,
+    ) : LaunchSettingsCriteria {
+        // If not broadcasting, having two device connected, clicked on any connected LE audio
+        // devices
+        override suspend fun matched(inAudioSharing: Boolean, deviceItem: DeviceItem): Boolean {
+            return withContext(backgroundDispatcher) {
+                val matched =
+                    leAudioProfile?.let { leAudio ->
+                        assistantProfile?.let { assistant ->
+                            !inAudioSharing &&
+                                getCurrentConnectedLeByGroupId(
+                                        leAudio,
+                                        assistant,
+                                        backgroundDispatcher,
+                                        logger
+                                    )
+                                    .size == 2 &&
+                                deviceItem.isActiveOrConnectedLeAudioSupported
+                        }
+                    } ?: false
+
+                if (matched) {
+                    logger.logLaunchSettingsCriteriaMatched(
+                        "NotSharingClickedConnected",
+                        deviceItem
+                    )
+                }
+
+                matched
+            }
+        }
+
+        override suspend fun getClickUiEvent(deviceItem: DeviceItem) =
+            BluetoothTileDialogUiEvent.LAUNCH_SETTINGS_NOT_SHARING_CONNECTED_LE_DEVICE_CLICKED
+    }
+
+    private companion object {
+        const val EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args"
+
+        val DeviceItem.isLeAudioSupported: Boolean
+            get() =
+                cachedBluetoothDevice.profiles.any { profile ->
+                    profile is LeAudioProfile && profile.isEnabled(cachedBluetoothDevice.device)
+                }
+
+        val DeviceItem.isNotConnectedLeAudioSupported: Boolean
+            get() = type == DeviceItemType.SAVED_BLUETOOTH_DEVICE && isLeAudioSupported
+
+        val DeviceItem.isActiveOrConnectedLeAudioSupported: Boolean
+            get() =
+                (type == DeviceItemType.ACTIVE_MEDIA_BLUETOOTH_DEVICE ||
+                    type == DeviceItemType.AVAILABLE_MEDIA_BLUETOOTH_DEVICE) && isLeAudioSupported
+
+        val DeviceItem.isMediaDevice: Boolean
+            get() =
+                cachedBluetoothDevice.connectableProfiles.any {
+                    it is A2dpProfile ||
+                        it is HearingAidProfile ||
+                        it is LeAudioProfile ||
+                        it is HeadsetProfile
+                }
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/dagger/QSModule.java b/packages/SystemUI/src/com/android/systemui/qs/dagger/QSModule.java
index ea89be6..b705a03 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/dagger/QSModule.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/dagger/QSModule.java
@@ -21,7 +21,6 @@
 import android.content.Context;
 import android.os.Handler;
 
-import com.android.systemui.bluetooth.qsdialog.BluetoothTileDialogModule;
 import com.android.systemui.dagger.NightDisplayListenerModule;
 import com.android.systemui.dagger.SysUISingleton;
 import com.android.systemui.dagger.qualifiers.Background;
@@ -61,7 +60,6 @@
  */
 @Module(subcomponents = {QSFragmentComponent.class, QSSceneComponent.class},
         includes = {
-                BluetoothTileDialogModule.class,
                 MediaModule.class,
                 PanelsModule.class,
                 QSExternalModule.class,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorImplTest.kt
deleted file mode 100644
index 64bd742..0000000
--- a/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorImplTest.kt
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Copyright (C) 2024 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.systemui.bluetooth.qsdialog
-
-import android.bluetooth.BluetoothDevice
-import android.testing.TestableLooper
-import androidx.test.ext.junit.runners.AndroidJUnit4
-import androidx.test.filters.SmallTest
-import com.android.settingslib.bluetooth.CachedBluetoothDevice
-import com.android.systemui.SysuiTestCase
-import com.android.systemui.kosmos.testDispatcher
-import com.android.systemui.kosmos.testScope
-import com.android.systemui.statusbar.phone.SystemUIDialog
-import com.android.systemui.testKosmos
-import com.android.systemui.util.mockito.whenever
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.test.UnconfinedTestDispatcher
-import kotlinx.coroutines.test.runTest
-import org.junit.Before
-import org.junit.Rule
-import org.junit.Test
-import org.junit.runner.RunWith
-import org.mockito.Mock
-import org.mockito.Mockito.verify
-import org.mockito.junit.MockitoJUnit
-import org.mockito.junit.MockitoRule
-
-@SmallTest
-@RunWith(AndroidJUnit4::class)
-@TestableLooper.RunWithLooper(setAsMainLooper = true)
-@OptIn(ExperimentalCoroutinesApi::class)
-class DeviceItemActionInteractorImplTest : SysuiTestCase() {
-    @get:Rule val mockitoRule: MockitoRule = MockitoJUnit.rule()
-    private val kosmos = testKosmos().apply { testDispatcher = UnconfinedTestDispatcher() }
-    private lateinit var actionInteractorImpl: DeviceItemActionInteractor
-
-    @Mock private lateinit var dialog: SystemUIDialog
-    @Mock private lateinit var cachedDevice: CachedBluetoothDevice
-    @Mock private lateinit var device: BluetoothDevice
-    @Mock private lateinit var deviceItem: DeviceItem
-
-    @Before
-    fun setUp() {
-        actionInteractorImpl = kosmos.deviceItemActionInteractor
-        whenever(deviceItem.cachedBluetoothDevice).thenReturn(cachedDevice)
-        whenever(cachedDevice.address).thenReturn("ADDRESS")
-        whenever(cachedDevice.device).thenReturn(device)
-    }
-
-    @Test
-    fun testOnClick_connectedMedia_setActive() {
-        with(kosmos) {
-            testScope.runTest {
-                whenever(deviceItem.type)
-                    .thenReturn(DeviceItemType.AVAILABLE_MEDIA_BLUETOOTH_DEVICE)
-                actionInteractorImpl.onClick(deviceItem, dialog)
-                verify(cachedDevice).setActive()
-                verify(bluetoothTileDialogLogger)
-                    .logDeviceClick(
-                        cachedDevice.address,
-                        DeviceItemType.AVAILABLE_MEDIA_BLUETOOTH_DEVICE
-                    )
-            }
-        }
-    }
-
-    @Test
-    fun testOnClick_activeMedia_disconnect() {
-        with(kosmos) {
-            testScope.runTest {
-                whenever(deviceItem.type).thenReturn(DeviceItemType.ACTIVE_MEDIA_BLUETOOTH_DEVICE)
-                actionInteractorImpl.onClick(deviceItem, dialog)
-                verify(cachedDevice).disconnect()
-                verify(bluetoothTileDialogLogger)
-                    .logDeviceClick(
-                        cachedDevice.address,
-                        DeviceItemType.ACTIVE_MEDIA_BLUETOOTH_DEVICE
-                    )
-            }
-        }
-    }
-
-    @Test
-    fun testOnClick_connectedOtherDevice_disconnect() {
-        with(kosmos) {
-            testScope.runTest {
-                whenever(deviceItem.type).thenReturn(DeviceItemType.CONNECTED_BLUETOOTH_DEVICE)
-                actionInteractorImpl.onClick(deviceItem, dialog)
-                verify(cachedDevice).disconnect()
-                verify(bluetoothTileDialogLogger)
-                    .logDeviceClick(cachedDevice.address, DeviceItemType.CONNECTED_BLUETOOTH_DEVICE)
-            }
-        }
-    }
-
-    @Test
-    fun testOnClick_saved_connect() {
-        with(kosmos) {
-            testScope.runTest {
-                whenever(deviceItem.type).thenReturn(DeviceItemType.SAVED_BLUETOOTH_DEVICE)
-                actionInteractorImpl.onClick(deviceItem, dialog)
-                verify(cachedDevice).connect()
-                verify(bluetoothTileDialogLogger)
-                    .logDeviceClick(cachedDevice.address, DeviceItemType.SAVED_BLUETOOTH_DEVICE)
-            }
-        }
-    }
-}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorKosmos.kt b/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorKosmos.kt
index e8e37bc..5ff4634 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorKosmos.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorKosmos.kt
@@ -13,19 +13,28 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package com.android.systemui.bluetooth.qsdialog
 
 import com.android.internal.logging.uiEventLogger
+import com.android.settingslib.bluetooth.LocalBluetoothManager
+import com.android.systemui.animation.DialogTransitionAnimator
 import com.android.systemui.kosmos.Kosmos
 import com.android.systemui.kosmos.testDispatcher
+import com.android.systemui.plugins.activityStarter
 import com.android.systemui.util.mockito.mock
 
 val Kosmos.bluetoothTileDialogLogger: BluetoothTileDialogLogger by Kosmos.Fixture { mock {} }
 
+val Kosmos.localBluetoothManager: LocalBluetoothManager by Kosmos.Fixture { mock {} }
+
+val Kosmos.dialogTransitionAnimator: DialogTransitionAnimator by Kosmos.Fixture { mock {} }
+
 val Kosmos.deviceItemActionInteractor: DeviceItemActionInteractor by
     Kosmos.Fixture {
-        DeviceItemActionInteractorImpl(
+        DeviceItemActionInteractor(
+            activityStarter,
+            dialogTransitionAnimator,
+            localBluetoothManager,
             testDispatcher,
             bluetoothTileDialogLogger,
             uiEventLogger,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorTest.kt
new file mode 100644
index 0000000..8246506
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/bluetooth/qsdialog/DeviceItemActionInteractorTest.kt
@@ -0,0 +1,459 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.systemui.bluetooth.qsdialog
+
+import android.bluetooth.BluetoothDevice
+import android.testing.AndroidTestingRunner
+import android.testing.TestableLooper
+import androidx.test.filters.SmallTest
+import com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession
+import com.android.dx.mockito.inline.extended.StaticMockitoSession
+import com.android.settingslib.bluetooth.BluetoothUtils
+import com.android.settingslib.bluetooth.CachedBluetoothDevice
+import com.android.settingslib.bluetooth.LeAudioProfile
+import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager
+import com.android.systemui.SysuiTestCase
+import com.android.systemui.kosmos.testDispatcher
+import com.android.systemui.kosmos.testScope
+import com.android.systemui.plugins.activityStarter
+import com.android.systemui.statusbar.phone.SystemUIDialog
+import com.android.systemui.testKosmos
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.test.UnconfinedTestDispatcher
+import kotlinx.coroutines.test.runTest
+import org.junit.After
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentMatchers
+import org.mockito.Mock
+import org.mockito.Mockito
+import org.mockito.Mockito.verify
+import org.mockito.junit.MockitoJUnit
+import org.mockito.junit.MockitoRule
+import org.mockito.kotlin.whenever
+
+@SmallTest
+@RunWith(AndroidTestingRunner::class)
+@TestableLooper.RunWithLooper(setAsMainLooper = true)
+@OptIn(ExperimentalCoroutinesApi::class)
+class DeviceItemActionInteractorTest : SysuiTestCase() {
+    @get:Rule val mockitoRule: MockitoRule = MockitoJUnit.rule()
+    private val kosmos = testKosmos().apply { testDispatcher = UnconfinedTestDispatcher() }
+    private lateinit var actionInteractorImpl: DeviceItemActionInteractor
+    private lateinit var mockitoSession: StaticMockitoSession
+    private lateinit var activeMediaDeviceItem: DeviceItem
+    private lateinit var notConnectedDeviceItem: DeviceItem
+    private lateinit var connectedMediaDeviceItem: DeviceItem
+    private lateinit var connectedOtherDeviceItem: DeviceItem
+    @Mock private lateinit var dialog: SystemUIDialog
+    @Mock private lateinit var profileManager: LocalBluetoothProfileManager
+    @Mock private lateinit var leAudioProfile: LeAudioProfile
+    @Mock private lateinit var assistantProfile: LocalBluetoothLeBroadcastAssistant
+    @Mock private lateinit var bluetoothDevice: BluetoothDevice
+    @Mock private lateinit var bluetoothDeviceGroupId2: BluetoothDevice
+    @Mock private lateinit var cachedBluetoothDevice: CachedBluetoothDevice
+
+    @Before
+    fun setUp() {
+        mockitoSession =
+            mockitoSession().initMocks(this).mockStatic(BluetoothUtils::class.java).startMocking()
+        activeMediaDeviceItem =
+            DeviceItem(
+                type = DeviceItemType.ACTIVE_MEDIA_BLUETOOTH_DEVICE,
+                cachedBluetoothDevice = cachedBluetoothDevice,
+                deviceName = DEVICE_NAME,
+                connectionSummary = DEVICE_CONNECTION_SUMMARY,
+                iconWithDescription = null,
+                background = null
+            )
+        notConnectedDeviceItem =
+            DeviceItem(
+                type = DeviceItemType.SAVED_BLUETOOTH_DEVICE,
+                cachedBluetoothDevice = cachedBluetoothDevice,
+                deviceName = DEVICE_NAME,
+                connectionSummary = DEVICE_CONNECTION_SUMMARY,
+                iconWithDescription = null,
+                background = null
+            )
+        connectedMediaDeviceItem =
+            DeviceItem(
+                type = DeviceItemType.AVAILABLE_MEDIA_BLUETOOTH_DEVICE,
+                cachedBluetoothDevice = cachedBluetoothDevice,
+                deviceName = DEVICE_NAME,
+                connectionSummary = DEVICE_CONNECTION_SUMMARY,
+                iconWithDescription = null,
+                background = null
+            )
+        connectedOtherDeviceItem =
+            DeviceItem(
+                type = DeviceItemType.CONNECTED_BLUETOOTH_DEVICE,
+                cachedBluetoothDevice = cachedBluetoothDevice,
+                deviceName = DEVICE_NAME,
+                connectionSummary = DEVICE_CONNECTION_SUMMARY,
+                iconWithDescription = null,
+                background = null
+            )
+        actionInteractorImpl = kosmos.deviceItemActionInteractor
+    }
+
+    @After
+    fun tearDown() {
+        mockitoSession.finishMocking()
+    }
+
+    @Test
+    fun testOnClick_connectedMedia_setActive() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(false)
+                actionInteractorImpl.onClick(connectedMediaDeviceItem, dialog)
+                verify(cachedBluetoothDevice).setActive()
+                verify(bluetoothTileDialogLogger)
+                    .logDeviceClick(
+                        cachedBluetoothDevice.address,
+                        DeviceItemType.AVAILABLE_MEDIA_BLUETOOTH_DEVICE
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_activeMedia_disconnect() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(false)
+                actionInteractorImpl.onClick(activeMediaDeviceItem, dialog)
+                verify(cachedBluetoothDevice).disconnect()
+                verify(bluetoothTileDialogLogger)
+                    .logDeviceClick(
+                        cachedBluetoothDevice.address,
+                        DeviceItemType.ACTIVE_MEDIA_BLUETOOTH_DEVICE
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_connectedOtherDevice_disconnect() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(false)
+                actionInteractorImpl.onClick(connectedOtherDeviceItem, dialog)
+                verify(cachedBluetoothDevice).disconnect()
+                verify(bluetoothTileDialogLogger)
+                    .logDeviceClick(
+                        cachedBluetoothDevice.address,
+                        DeviceItemType.CONNECTED_BLUETOOTH_DEVICE
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_saved_connect() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(false)
+                actionInteractorImpl.onClick(notConnectedDeviceItem, dialog)
+                verify(cachedBluetoothDevice).connect()
+                verify(bluetoothTileDialogLogger)
+                    .logDeviceClick(
+                        cachedBluetoothDevice.address,
+                        DeviceItemType.SAVED_BLUETOOTH_DEVICE
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_audioSharingDisabled_shouldNotLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(false)
+
+                actionInteractorImpl.onClick(connectedMediaDeviceItem, dialog)
+                verify(activityStarter, Mockito.never())
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_inAudioSharing_clickedDeviceHasSource_shouldNotLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(cachedBluetoothDevice.connectableProfiles)
+                        .thenReturn(listOf(leAudioProfile))
+
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(true)
+                whenever(localBluetoothManager.profileManager).thenReturn(profileManager)
+                whenever(profileManager.leAudioProfile).thenReturn(leAudioProfile)
+                whenever(profileManager.leAudioBroadcastAssistantProfile)
+                    .thenReturn(assistantProfile)
+
+                whenever(BluetoothUtils.isBroadcasting(ArgumentMatchers.any())).thenReturn(true)
+                whenever(
+                        BluetoothUtils.hasConnectedBroadcastSource(
+                            ArgumentMatchers.any(),
+                            ArgumentMatchers.any()
+                        )
+                    )
+                    .thenReturn(true)
+
+                actionInteractorImpl.onClick(connectedMediaDeviceItem, dialog)
+                verify(activityStarter, Mockito.never())
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_inAudioSharing_clickedDeviceNoSource_shouldLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(cachedBluetoothDevice.device).thenReturn(bluetoothDevice)
+                whenever(cachedBluetoothDevice.connectableProfiles)
+                        .thenReturn(listOf(leAudioProfile))
+
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(true)
+                whenever(localBluetoothManager.profileManager).thenReturn(profileManager)
+                whenever(profileManager.leAudioProfile).thenReturn(leAudioProfile)
+                whenever(profileManager.leAudioBroadcastAssistantProfile)
+                    .thenReturn(assistantProfile)
+
+                whenever(BluetoothUtils.isBroadcasting(ArgumentMatchers.any())).thenReturn(true)
+                whenever(
+                    BluetoothUtils.hasConnectedBroadcastSource(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.any()
+                    )
+                )
+                        .thenReturn(false)
+
+                actionInteractorImpl.onClick(connectedMediaDeviceItem, dialog)
+                verify(activityStarter)
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_noConnectedLeDevice_shouldNotLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(true)
+                whenever(localBluetoothManager.profileManager).thenReturn(profileManager)
+                whenever(profileManager.leAudioProfile).thenReturn(leAudioProfile)
+                whenever(profileManager.leAudioBroadcastAssistantProfile)
+                    .thenReturn(assistantProfile)
+
+                actionInteractorImpl.onClick(notConnectedDeviceItem, dialog)
+                verify(activityStarter, Mockito.never())
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_hasOneConnectedLeDevice_clickedNonLe_shouldNotLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(true)
+                whenever(localBluetoothManager.profileManager).thenReturn(profileManager)
+                whenever(profileManager.leAudioProfile).thenReturn(leAudioProfile)
+                whenever(profileManager.leAudioBroadcastAssistantProfile)
+                    .thenReturn(assistantProfile)
+
+                whenever(
+                        assistantProfile.getDevicesMatchingConnectionStates(ArgumentMatchers.any())
+                    )
+                    .thenReturn(listOf(bluetoothDevice))
+
+                actionInteractorImpl.onClick(notConnectedDeviceItem, dialog)
+                verify(activityStarter, Mockito.never())
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_hasOneConnectedLeDevice_clickedLe_shouldLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.device).thenReturn(bluetoothDevice)
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(cachedBluetoothDevice.profiles).thenReturn(listOf(leAudioProfile))
+                whenever(leAudioProfile.isEnabled(ArgumentMatchers.any())).thenReturn(true)
+
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(true)
+                whenever(localBluetoothManager.profileManager).thenReturn(profileManager)
+                whenever(profileManager.leAudioProfile).thenReturn(leAudioProfile)
+                whenever(profileManager.leAudioBroadcastAssistantProfile)
+                    .thenReturn(assistantProfile)
+
+                whenever(
+                        assistantProfile.getDevicesMatchingConnectionStates(ArgumentMatchers.any())
+                    )
+                    .thenReturn(listOf(bluetoothDevice))
+
+                actionInteractorImpl.onClick(notConnectedDeviceItem, dialog)
+                verify(activityStarter)
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_hasOneConnectedLeDevice_clickedConnectedLe_shouldNotLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(true)
+                whenever(localBluetoothManager.profileManager).thenReturn(profileManager)
+                whenever(profileManager.leAudioProfile).thenReturn(leAudioProfile)
+                whenever(profileManager.leAudioBroadcastAssistantProfile)
+                    .thenReturn(assistantProfile)
+
+                whenever(
+                        assistantProfile.getDevicesMatchingConnectionStates(ArgumentMatchers.any())
+                    )
+                    .thenReturn(listOf(bluetoothDevice))
+
+                actionInteractorImpl.onClick(connectedMediaDeviceItem, dialog)
+                verify(activityStarter, Mockito.never())
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_hasTwoConnectedLeDevice_clickedNotConnectedLe_shouldNotLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(true)
+                whenever(localBluetoothManager.profileManager).thenReturn(profileManager)
+                whenever(profileManager.leAudioProfile).thenReturn(leAudioProfile)
+                whenever(profileManager.leAudioBroadcastAssistantProfile)
+                    .thenReturn(assistantProfile)
+
+                whenever(
+                        assistantProfile.getDevicesMatchingConnectionStates(ArgumentMatchers.any())
+                    )
+                    .thenReturn(listOf(bluetoothDevice, bluetoothDeviceGroupId2))
+                whenever(leAudioProfile.getGroupId(ArgumentMatchers.any())).thenAnswer {
+                    val device = it.arguments.first() as BluetoothDevice
+                    if (device == bluetoothDevice) GROUP_ID_1 else GROUP_ID_2
+                }
+
+                actionInteractorImpl.onClick(notConnectedDeviceItem, dialog)
+                verify(activityStarter, Mockito.never())
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    @Test
+    fun testOnClick_hasTwoConnectedLeDevice_clickedConnectedLe_shouldLaunchSettings() {
+        with(kosmos) {
+            testScope.runTest {
+                whenever(cachedBluetoothDevice.device).thenReturn(bluetoothDevice)
+                whenever(cachedBluetoothDevice.address).thenReturn(DEVICE_ADDRESS)
+                whenever(cachedBluetoothDevice.profiles).thenReturn(listOf(leAudioProfile))
+                whenever(leAudioProfile.isEnabled(ArgumentMatchers.any())).thenReturn(true)
+
+                whenever(BluetoothUtils.isAudioSharingEnabled()).thenReturn(true)
+                whenever(localBluetoothManager.profileManager).thenReturn(profileManager)
+                whenever(profileManager.leAudioProfile).thenReturn(leAudioProfile)
+                whenever(profileManager.leAudioBroadcastAssistantProfile)
+                    .thenReturn(assistantProfile)
+
+                whenever(
+                        assistantProfile.getDevicesMatchingConnectionStates(ArgumentMatchers.any())
+                    )
+                    .thenReturn(listOf(bluetoothDevice, bluetoothDeviceGroupId2))
+                whenever(leAudioProfile.getGroupId(ArgumentMatchers.any())).thenAnswer {
+                    val device = it.arguments.first() as BluetoothDevice
+                    if (device == bluetoothDevice) GROUP_ID_1 else GROUP_ID_2
+                }
+
+                actionInteractorImpl.onClick(connectedMediaDeviceItem, dialog)
+                verify(activityStarter)
+                    .postStartActivityDismissingKeyguard(
+                        ArgumentMatchers.any(),
+                        ArgumentMatchers.anyInt(),
+                        ArgumentMatchers.any()
+                    )
+            }
+        }
+    }
+
+    private companion object {
+        const val DEVICE_NAME = "device"
+        const val DEVICE_CONNECTION_SUMMARY = "active"
+        const val DEVICE_ADDRESS = "address"
+        const val GROUP_ID_1 = 1
+        const val GROUP_ID_2 = 2
+    }
+}
diff --git a/services/core/java/com/android/server/am/ActivityManagerConstants.java b/services/core/java/com/android/server/am/ActivityManagerConstants.java
index 26aa053..2c04883 100644
--- a/services/core/java/com/android/server/am/ActivityManagerConstants.java
+++ b/services/core/java/com/android/server/am/ActivityManagerConstants.java
@@ -169,6 +169,11 @@
      */
     static final String KEY_ENABLE_NEW_OOMADJ = "enable_new_oom_adj";
 
+    /**
+     * Whether or not to enable the batching of OOM adjuster calls to LMKD
+     */
+    static final String KEY_ENABLE_BATCHING_OOM_ADJ = "enable_batching_oom_adj";
+
     private static final int DEFAULT_MAX_CACHED_PROCESSES = 1024;
     private static final boolean DEFAULT_PRIORITIZE_ALARM_BROADCASTS = true;
     private static final long DEFAULT_FGSERVICE_MIN_SHOWN_TIME = 2*1000;
@@ -244,6 +249,11 @@
     private static final boolean DEFAULT_ENABLE_NEW_OOM_ADJ = Flags.oomadjusterCorrectnessRewrite();
 
     /**
+     * The default value to {@link #KEY_ENABLE_BATCHING_OOM_ADJ}.
+     */
+    private static final boolean DEFAULT_ENABLE_BATCHING_OOM_ADJ = Flags.batchingOomAdj();
+
+    /**
      * Same as {@link TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED}
      */
     private static final int
@@ -1136,6 +1146,9 @@
     /** @see #KEY_ENABLE_NEW_OOMADJ */
     public boolean ENABLE_NEW_OOMADJ = DEFAULT_ENABLE_NEW_OOM_ADJ;
 
+    /** @see #KEY_ENABLE_BATCHING_OOM_ADJ */
+    public boolean ENABLE_BATCHING_OOM_ADJ = DEFAULT_ENABLE_BATCHING_OOM_ADJ;
+
     /**
      * Indicates whether PSS profiling in AppProfiler is disabled or not.
      */
@@ -1479,6 +1492,8 @@
     private void loadNativeBootDeviceConfigConstants() {
         ENABLE_NEW_OOMADJ = getDeviceConfigBoolean(KEY_ENABLE_NEW_OOMADJ,
                 DEFAULT_ENABLE_NEW_OOM_ADJ);
+        ENABLE_BATCHING_OOM_ADJ = getDeviceConfigBoolean(KEY_ENABLE_BATCHING_OOM_ADJ,
+                DEFAULT_ENABLE_BATCHING_OOM_ADJ);
     }
 
     public void setOverrideMaxCachedProcesses(int value) {
@@ -2248,6 +2263,13 @@
                 mDefaultPssToRssThresholdModifier);
     }
 
+    private void updateEnableBatchingOomAdj() {
+        ENABLE_BATCHING_OOM_ADJ = DeviceConfig.getBoolean(
+                DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT,
+                KEY_ENABLE_BATCHING_OOM_ADJ,
+                DEFAULT_ENABLE_BATCHING_OOM_ADJ);
+    }
+
     boolean shouldDebugUidForProcState(int uid) {
         SparseBooleanArray ar = mProcStateDebugUids;
         final var size = ar.size();
@@ -2476,6 +2498,9 @@
         pw.print("  "); pw.print(KEY_MAX_PREVIOUS_TIME);
         pw.print("="); pw.println(MAX_PREVIOUS_TIME);
 
+        pw.print("  "); pw.print(KEY_ENABLE_BATCHING_OOM_ADJ);
+        pw.print("="); pw.println(ENABLE_BATCHING_OOM_ADJ);
+
         pw.println();
         if (mOverrideMaxCachedProcesses >= 0) {
             pw.print("  mOverrideMaxCachedProcesses="); pw.println(mOverrideMaxCachedProcesses);
diff --git a/services/core/java/com/android/server/am/OomAdjuster.java b/services/core/java/com/android/server/am/OomAdjuster.java
index ab34dd4..fc81d3e 100644
--- a/services/core/java/com/android/server/am/OomAdjuster.java
+++ b/services/core/java/com/android/server/am/OomAdjuster.java
@@ -384,6 +384,13 @@
     protected final ArraySet<ProcessRecord> mProcessesInCycle = new ArraySet<>();
 
     /**
+     * List of processes that we want to batch for LMKD to adjust their respective
+     * OOM scores.
+     */
+    @GuardedBy("mService")
+    protected final ArrayList<ProcessRecord> mProcsToOomAdj = new ArrayList<ProcessRecord>();
+
+    /**
      * Flag to mark if there is an ongoing oomAdjUpdate: potentially the oomAdjUpdate
      * could be called recursively because of the indirect calls during the update;
      * however the oomAdjUpdate itself doesn't support recursion - in this case we'd
@@ -1246,7 +1253,7 @@
             if (!app.isKilledByAm() && app.getThread() != null) {
                 // We don't need to apply the update for the process which didn't get computed
                 if (state.getCompletedAdjSeq() == mAdjSeq) {
-                    applyOomAdjLSP(app, doingAll, now, nowElapsed, oomAdjReason);
+                    applyOomAdjLSP(app, doingAll, now, nowElapsed, oomAdjReason, true);
                 }
 
                 if (app.isPendingFinishAttach()) {
@@ -1348,6 +1355,11 @@
             }
         }
 
+        if (!mProcsToOomAdj.isEmpty()) {
+            ProcessList.batchSetOomAdj(mProcsToOomAdj);
+            mProcsToOomAdj.clear();
+        }
+
         if (proactiveKillsEnabled                               // Proactive kills enabled?
                 && doKillExcessiveProcesses                     // Should kill excessive processes?
                 && freeSwapPercent < lowSwapThresholdPercent    // Swap below threshold?
@@ -3246,10 +3258,16 @@
         mCachedAppOptimizer.onWakefulnessChanged(wakefulness);
     }
 
+    @GuardedBy({"mService", "mProcLock"})
+    protected boolean applyOomAdjLSP(ProcessRecord app, boolean doingAll, long now,
+            long nowElapsed, @OomAdjReason int oomAdjReason) {
+        return applyOomAdjLSP(app, doingAll, now, nowElapsed, oomAdjReason, false);
+    }
+
     /** Applies the computed oomadj, procstate and sched group values and freezes them in set* */
     @GuardedBy({"mService", "mProcLock"})
     protected boolean applyOomAdjLSP(ProcessRecord app, boolean doingAll, long now,
-            long nowElapsed, @OomAdjReason int oomAdjReson) {
+            long nowElapsed, @OomAdjReason int oomAdjReson, boolean isBatchingOomAdj) {
         boolean success = true;
         final ProcessStateRecord state = app.mState;
         final UidRecord uidRec = app.getUidRecord();
@@ -3266,7 +3284,12 @@
 
         final int oldOomAdj = state.getSetAdj();
         if (state.getCurAdj() != state.getSetAdj()) {
-            ProcessList.setOomAdj(app.getPid(), app.uid, state.getCurAdj());
+            if (isBatchingOomAdj && mConstants.ENABLE_BATCHING_OOM_ADJ) {
+                mProcsToOomAdj.add(app);
+            } else {
+                ProcessList.setOomAdj(app.getPid(), app.uid, state.getCurAdj());
+            }
+
             if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mService.mCurOomAdjUid == app.info.uid) {
                 String msg = "Set " + app.getPid() + " " + app.processName + " adj "
                         + state.getCurAdj() + ": " + state.getAdjType();
diff --git a/services/core/java/com/android/server/am/ProcessList.java b/services/core/java/com/android/server/am/ProcessList.java
index 219de70..c094724 100644
--- a/services/core/java/com/android/server/am/ProcessList.java
+++ b/services/core/java/com/android/server/am/ProcessList.java
@@ -354,6 +354,7 @@
     // LMK_KILL_OCCURRED
     // LMK_START_MONITORING
     // LMK_BOOT_COMPLETED
+    // LMK_PROCS_PRIO
     static final byte LMK_TARGET = 0;
     static final byte LMK_PROCPRIO = 1;
     static final byte LMK_PROCREMOVE = 2;
@@ -365,6 +366,7 @@
     static final byte LMK_KILL_OCCURRED = 8; // Msg to subscribed clients on kill occurred event
     static final byte LMK_START_MONITORING = 9; // Start monitoring if delayed earlier
     static final byte LMK_BOOT_COMPLETED = 10;
+    static final byte LMK_PROCS_PRIO = 11;  // Batch option for LMK_PROCPRIO
 
     // Low Memory Killer Daemon command codes.
     // These must be kept in sync with async_event_type definitions in lmkd.h
@@ -1561,6 +1563,50 @@
         }
     }
 
+
+    // The max size for PROCS_PRIO cmd in LMKD
+    private static final int MAX_PROCS_PRIO_PACKET_SIZE = 3;
+
+    // (4 bytes per field * 4 fields * 3 processes per batch) + 4 bytes for the LMKD cmd
+    private static final int MAX_OOM_ADJ_BATCH_LENGTH = ((4 * 4) * MAX_PROCS_PRIO_PACKET_SIZE) + 4;
+
+    /**
+     * Set the out-of-memory badness adjustment for a list of processes.
+     *
+     * @param apps App list to adjust their respective oom score.
+     *
+     * {@hide}
+     */
+    public static void batchSetOomAdj(ArrayList<ProcessRecord> apps) {
+        final int totalApps = apps.size();
+        if (totalApps == 0) {
+            return;
+        }
+
+        ByteBuffer buf = ByteBuffer.allocate(MAX_OOM_ADJ_BATCH_LENGTH);
+        int total_procs_in_buf = 0;
+        buf.putInt(LMK_PROCS_PRIO);
+        for (int i = 0; i < totalApps; i++) {
+            final int pid = apps.get(i).getPid();
+            final int amt = apps.get(i).mState.getCurAdj();
+            final int uid = apps.get(i).uid;
+            if (pid <= 0 || amt == UNKNOWN_ADJ) continue;
+            if (total_procs_in_buf >= MAX_PROCS_PRIO_PACKET_SIZE) {
+                writeLmkd(buf, null);
+                buf.clear();
+                total_procs_in_buf = 0;
+                buf.allocate(MAX_OOM_ADJ_BATCH_LENGTH);
+                buf.putInt(LMK_PROCS_PRIO);
+            }
+            buf.putInt(pid);
+            buf.putInt(uid);
+            buf.putInt(amt);
+            buf.putInt(0);  // Default proc type to PROC_TYPE_APP
+            total_procs_in_buf++;
+        }
+        writeLmkd(buf, null);
+    }
+
     /*
      * {@hide}
      */
diff --git a/services/core/java/com/android/server/am/flags.aconfig b/services/core/java/com/android/server/am/flags.aconfig
index afde4f7..2abfad9 100644
--- a/services/core/java/com/android/server/am/flags.aconfig
+++ b/services/core/java/com/android/server/am/flags.aconfig
@@ -134,3 +134,11 @@
         purpose: PURPOSE_BUGFIX
     }
 }
+
+flag {
+    name: "batching_oom_adj"
+    namespace: "backstage_power"
+    description: "Batch OOM adjustment calls to LMKD"
+    bug: "244232958"
+    is_fixed_read_only: true
+}