diff --git a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java index 8feabe778348e4231060d436349fb7c9c3b851b8..c3357b87fa70f201d25755fa31e84caedce7c2bb 100644 --- a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java +++ b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java @@ -1153,10 +1153,7 @@ public class A2dpService extends ProfileService { mFactory.getAvrcpTargetService().handleA2dpActiveDeviceChanged(device); } - mAdapterService - .getActiveDeviceManager() - .profileActiveDeviceChanged(BluetoothProfile.A2DP, device); - mAdapterService.getSilenceDeviceManager().a2dpActiveDeviceChanged(device); + mAdapterService.handleActiveDeviceChange(BluetoothProfile.A2DP, device); BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED, BluetoothProfile.A2DP, mAdapterService.obfuscateAddress(device), diff --git a/android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java b/android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java index 854da1de52563fe342e6ffd4d134e4b7f033891e..b39e13c177f8f8481217fbf641d3daf00cf1593e 100644 --- a/android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java +++ b/android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java @@ -198,7 +198,7 @@ public class ActiveDeviceManager implements AdapterService.BluetoothStateCallbac * Called when active state of audio profiles changed * * @param profile The Bluetooth profile of which active state changed - * @param device The device currently activated. {@code null} if no A2DP device activated + * @param device The device currently activated. {@code null} if no device is active */ public void profileActiveDeviceChanged(int profile, BluetoothDevice device) { switch (profile) { diff --git a/android/app/src/com/android/bluetooth/btservice/AdapterService.java b/android/app/src/com/android/bluetooth/btservice/AdapterService.java index a2d98eb81508db17a7e267edb6e8be5e1f2332d2..88870c7cea76a719585d919074dc0e6699850b8b 100644 --- a/android/app/src/com/android/bluetooth/btservice/AdapterService.java +++ b/android/app/src/com/android/bluetooth/btservice/AdapterService.java @@ -6916,6 +6916,13 @@ public class AdapterService extends Service { mPhonePolicy.profileConnectionStateChanged(profile, device, fromState, toState); } + /** Handle Bluetooth app state when active device changes for a given {@code profile}. */ + public void handleActiveDeviceChange(int profile, BluetoothDevice device) { + mActiveDeviceManager.profileActiveDeviceChanged(profile, device); + mSilenceDeviceManager.profileActiveDeviceChanged(profile, device); + mPhonePolicy.profileActiveDeviceChanged(profile, device); + } + static int convertScanModeToHal(int mode) { switch (mode) { case BluetoothAdapter.SCAN_MODE_NONE: diff --git a/android/app/src/com/android/bluetooth/btservice/PhonePolicy.java b/android/app/src/com/android/bluetooth/btservice/PhonePolicy.java index 0ca3232a252fb4b9f136d578dabd3be43b592523..39957fd327a341abfa1dccbb8ceb32cb2aee3379 100644 --- a/android/app/src/com/android/bluetooth/btservice/PhonePolicy.java +++ b/android/app/src/com/android/bluetooth/btservice/PhonePolicy.java @@ -19,12 +19,8 @@ package com.android.bluetooth.btservice; import static com.android.bluetooth.Utils.isDualModeAudioEnabled; import android.annotation.RequiresPermission; -import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothHeadset; -import android.bluetooth.BluetoothHearingAid; -import android.bluetooth.BluetoothLeAudio; import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothUuid; import android.content.BroadcastReceiver; @@ -91,7 +87,6 @@ class PhonePolicy { // Message types for the handler (internal messages generated by intents or timeouts) private static final int MESSAGE_CONNECT_OTHER_PROFILES = 3; private static final int MESSAGE_ADAPTER_STATE_TURNED_ON = 4; - private static final int MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED = 5; private static final int MESSAGE_DEVICE_CONNECTED = 6; @VisibleForTesting static final String AUTO_CONNECT_PROFILES_PROPERTY = @@ -129,6 +124,16 @@ class PhonePolicy { } } + /** + * Called when active state of audio profiles changed + * + * @param profile The Bluetooth profile of which active state changed + * @param device The device currently activated. {@code null} if no A2DP device activated + */ + public void profileActiveDeviceChanged(int profile, BluetoothDevice device) { + mHandler.post(() -> processActiveDeviceChanged(device, profile)); + } + // Broadcast receiver for all changes to states of various profiles private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override @@ -139,26 +144,6 @@ class PhonePolicy { return; } switch (action) { - case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED: - mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED, - BluetoothProfile.A2DP, -1, // No-op argument - intent).sendToTarget(); - break; - case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED: - mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED, - BluetoothProfile.HEADSET, -1, // No-op argument - intent).sendToTarget(); - break; - case BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED: - mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED, - BluetoothProfile.HEARING_AID, -1, // No-op argument - intent).sendToTarget(); - break; - case BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED: - mHandler.obtainMessage(MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED, - BluetoothProfile.LE_AUDIO, -1, // No-op argument - intent).sendToTarget(); - break; case BluetoothAdapter.ACTION_STATE_CHANGED: // Only pass the message on if the adapter has actually changed state from // non-ON to ON. NOTE: ON is the state depicting BREDR ON and not just BLE ON. @@ -191,14 +176,6 @@ class PhonePolicy { @Override public void handleMessage(Message msg) { switch (msg.what) { - case MESSAGE_PROFILE_ACTIVE_DEVICE_CHANGED: { - Intent intent = (Intent) msg.obj; - BluetoothDevice activeDevice = - intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - processActiveDeviceChanged(activeDevice, msg.arg1); - } - break; - case MESSAGE_CONNECT_OTHER_PROFILES: { // Called when we try connect some profiles in processConnectOtherProfiles but // we send a delayed message to try connecting the remaining profiles @@ -229,10 +206,6 @@ class PhonePolicy { filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); - filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED); - filter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED); - filter.addAction(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED); - filter.addAction(BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED); mAdapterService.registerReceiver(mReceiver, filter); } diff --git a/android/app/src/com/android/bluetooth/btservice/SilenceDeviceManager.java b/android/app/src/com/android/bluetooth/btservice/SilenceDeviceManager.java index 7c2f7aa55d0d4f2550e138e0044ce2a95f207608..4069a74110ed1eb1e12344963ae84d5ddadf2fd5 100644 --- a/android/app/src/com/android/bluetooth/btservice/SilenceDeviceManager.java +++ b/android/app/src/com/android/bluetooth/btservice/SilenceDeviceManager.java @@ -76,6 +76,25 @@ public class SilenceDeviceManager { private static final int ENABLE_SILENCE = 0; private static final int DISABLE_SILENCE = 1; + /** + * Called when active state of audio profiles changed + * + * @param profile The Bluetooth profile of which active state changed + * @param device The device currently activated. {@code null} if no device is active + */ + public void profileActiveDeviceChanged(int profile, BluetoothDevice device) { + switch (profile) { + case BluetoothProfile.A2DP: + mHandler.obtainMessage(MSG_A2DP_ACTIVE_DEVICE_CHANGED, device).sendToTarget(); + break; + case BluetoothProfile.HEADSET: + mHandler.obtainMessage(MSG_HFP_ACTIVE_DEVICE_CHANGED, device).sendToTarget(); + break; + default: + break; + } + } + /** * Called when A2DP connection state changed by A2dpService * @@ -88,15 +107,6 @@ public class SilenceDeviceManager { .sendToTarget(); } - /** - * Called when A2DP active device changed by A2dpService - * - * @param device The device currently activated. {@code null} if no A2DP device activated - */ - public void a2dpActiveDeviceChanged(BluetoothDevice device) { - mHandler.obtainMessage(MSG_A2DP_ACTIVE_DEVICE_CHANGED, device).sendToTarget(); - } - /** * Called when HFP connection state changed by HeadsetService * @@ -109,15 +119,6 @@ public class SilenceDeviceManager { .sendToTarget(); } - /** - * Called when HFP active device is changed by HeadsetService - * - * @param device The device currently activated. {@code null} if no HFP device activated - */ - public void hfpActiveDeviceChanged(BluetoothDevice device) { - mHandler.obtainMessage(MSG_HFP_ACTIVE_DEVICE_CHANGED, device).sendToTarget(); - } - class SilenceDeviceManagerHandler extends Handler { SilenceDeviceManagerHandler(Looper looper) { super(looper); diff --git a/android/app/src/com/android/bluetooth/hearingaid/HearingAidService.java b/android/app/src/com/android/bluetooth/hearingaid/HearingAidService.java index aa2e0dcab9552415df9c05ba5fe2070bfa4bfd74..832bfa9fddeeb7d602b16c8c01ebd17d4f6d6f0b 100644 --- a/android/app/src/com/android/bluetooth/hearingaid/HearingAidService.java +++ b/android/app/src/com/android/bluetooth/hearingaid/HearingAidService.java @@ -724,9 +724,7 @@ public class HearingAidService extends ProfileService { } private void notifyActiveDeviceChanged() { - mAdapterService - .getActiveDeviceManager() - .profileActiveDeviceChanged(BluetoothProfile.HEARING_AID, mActiveDevice); + mAdapterService.handleActiveDeviceChange(BluetoothProfile.HEARING_AID, mActiveDevice); Intent intent = new Intent(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mActiveDevice); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT diff --git a/android/app/src/com/android/bluetooth/hfp/HeadsetService.java b/android/app/src/com/android/bluetooth/hfp/HeadsetService.java index 52007b65d3d22438d8fcb38d2e9feb33af2270e2..ee25b0a2c7d0e1393ee713b2b0da687c4c14e021 100644 --- a/android/app/src/com/android/bluetooth/hfp/HeadsetService.java +++ b/android/app/src/com/android/bluetooth/hfp/HeadsetService.java @@ -2135,10 +2135,7 @@ public class HeadsetService extends ProfileService { private void broadcastActiveDevice(BluetoothDevice device) { logD("broadcastActiveDevice: " + device); - mAdapterService - .getActiveDeviceManager() - .profileActiveDeviceChanged(BluetoothProfile.HEADSET, device); - mAdapterService.getSilenceDeviceManager().hfpActiveDeviceChanged(device); + mAdapterService.handleActiveDeviceChange(BluetoothProfile.HEADSET, device); BluetoothStatsLog.write( BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED, diff --git a/android/app/src/com/android/bluetooth/le_audio/LeAudioService.java b/android/app/src/com/android/bluetooth/le_audio/LeAudioService.java index 08ed0448a3578ab0f25959482060f11083ee017d..b027a4de15c9c53e009b6b25975d47ac89105d8d 100644 --- a/android/app/src/com/android/bluetooth/le_audio/LeAudioService.java +++ b/android/app/src/com/android/bluetooth/le_audio/LeAudioService.java @@ -1221,9 +1221,7 @@ public class LeAudioService extends ProfileService { + ". Currently active device is " + mActiveAudioOutDevice); } - mAdapterService - .getActiveDeviceManager() - .profileActiveDeviceChanged(BluetoothProfile.LE_AUDIO, device); + mAdapterService.handleActiveDeviceChange(BluetoothProfile.LE_AUDIO, device); Intent intent = new Intent(BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT diff --git a/android/app/tests/unit/src/com/android/bluetooth/btservice/PhonePolicyTest.java b/android/app/tests/unit/src/com/android/bluetooth/btservice/PhonePolicyTest.java index e2f04e5cc4e144ede442adca55b6d88d9f329a4a..fa56f526b644f1787bd795243f1380a6010c84fd 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/btservice/PhonePolicyTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/btservice/PhonePolicyTest.java @@ -21,7 +21,6 @@ import static com.android.bluetooth.TestUtils.waitForLooperToFinishScheduledTask import static org.mockito.Mockito.*; -import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHeadset; @@ -363,10 +362,7 @@ public class PhonePolicyTest { BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); // Make one of the device active - Intent intent = new Intent(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED); - intent.putExtra(BluetoothDevice.EXTRA_DEVICE, connectionOrder.get(0)); - intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); - mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent); + mPhonePolicy.profileActiveDeviceChanged(BluetoothProfile.A2DP, connectionOrder.get(0)); waitForLooperToFinishScheduledTask(mHandlerThread.getLooper()); // Only calls setConnection on device connectionOrder.get(0) with STATE_CONNECTED @@ -379,10 +375,7 @@ public class PhonePolicyTest { // Make another device active when(mHeadsetService.getConnectionState(connectionOrder.get(1))).thenReturn( BluetoothProfile.STATE_CONNECTED); - intent = new Intent(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED); - intent.putExtra(BluetoothDevice.EXTRA_DEVICE, connectionOrder.get(1)); - intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); - mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent); + mPhonePolicy.profileActiveDeviceChanged(BluetoothProfile.A2DP, connectionOrder.get(1)); waitForLooperToFinishScheduledTask(mHandlerThread.getLooper()); // Only calls setConnection on device connectionOrder.get(1) with STATE_CONNECTED diff --git a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceAndStateMachineTest.java b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceAndStateMachineTest.java index d9a5c98b4c88e43e78afc6f7f03060ff3edfa0f0..fd46f1a0cf8cebc78d2e368d176df691d6fa718d 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceAndStateMachineTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceAndStateMachineTest.java @@ -476,9 +476,7 @@ public class HeadsetServiceAndStateMachineTest { BluetoothDevice activeDevice = connectedDevices.get(MAX_HEADSET_CONNECTIONS / 2); Assert.assertTrue(mHeadsetService.setActiveDevice(activeDevice)); verify(mNativeInterface).setActiveDevice(activeDevice); - verify(mActiveDeviceManager) - .profileActiveDeviceChanged(BluetoothProfile.HEADSET, activeDevice); - verify(mSilenceDeviceManager).hfpActiveDeviceChanged(activeDevice); + verify(mAdapterService).handleActiveDeviceChange(BluetoothProfile.HEADSET, activeDevice); Assert.assertEquals(activeDevice, mHeadsetService.getActiveDevice()); // Start virtual call Assert.assertTrue(mHeadsetService.startScoUsingVirtualVoiceCall());