diff --git a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java index 7e4d791185d079bbf17c8af268e58bfb05ffb3e3..ed6ff1019ac47f893e092cd327db94254c58429d 100644 --- a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java +++ b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java @@ -1080,14 +1080,17 @@ public class A2dpService extends ProfileService { mActiveDevice = device; } + mAdapterService.getActiveDeviceManager().a2dpActiveStateChanged(device); + mAdapterService.getSilenceDeviceManager().a2dpActiveDeviceChanged(device); + BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED, BluetoothProfile.A2DP, mAdapterService.obfuscateAddress(device), mAdapterService.getMetricId(device)); + Intent intent = new Intent(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); - mAdapterService.getActiveDeviceManager().a2dpActiveStateChanged(device); Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT, Utils.getTempAllowlistBroadcastOptions()); } @@ -1285,6 +1288,9 @@ public class A2dpService extends ProfileService { mAdapterService .getActiveDeviceManager() .a2dpConnectionStateChanged(device, fromState, toState); + mAdapterService + .getSilenceDeviceManager() + .a2dpConnectionStateChanged(device, fromState, toState); } /** diff --git a/android/app/src/com/android/bluetooth/btservice/AdapterService.java b/android/app/src/com/android/bluetooth/btservice/AdapterService.java index 40f7c7464a05ae425166f0ce54ea6be825765535..3b47a06c4b134b485905a58946f0b8862254a645 100644 --- a/android/app/src/com/android/bluetooth/btservice/AdapterService.java +++ b/android/app/src/com/android/bluetooth/btservice/AdapterService.java @@ -758,6 +758,10 @@ public class AdapterService extends Service { return mActiveDeviceManager; } + public SilenceDeviceManager getSilenceDeviceManager() { + return mSilenceDeviceManager; + } + private boolean initMetricsLogger() { if (mMetricsLogger != null) { return false; diff --git a/android/app/src/com/android/bluetooth/btservice/SilenceDeviceManager.java b/android/app/src/com/android/bluetooth/btservice/SilenceDeviceManager.java index c75ad49b0b8b83e2c07e53163648aac778792ca7..7c2f7aa55d0d4f2550e138e0044ce2a95f207608 100644 --- a/android/app/src/com/android/bluetooth/btservice/SilenceDeviceManager.java +++ b/android/app/src/com/android/bluetooth/btservice/SilenceDeviceManager.java @@ -19,14 +19,9 @@ package com.android.bluetooth.btservice; import static android.Manifest.permission.BLUETOOTH_CONNECT; import android.annotation.RequiresPermission; -import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothProfile; -import android.content.BroadcastReceiver; -import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; import android.os.Handler; import android.os.Looper; import android.os.Message; @@ -76,43 +71,52 @@ public class SilenceDeviceManager { private static final int MSG_SILENCE_DEVICE_STATE_CHANGED = 1; private static final int MSG_A2DP_CONNECTION_STATE_CHANGED = 10; private static final int MSG_HFP_CONNECTION_STATE_CHANGED = 11; - private static final int MSG_A2DP_ACTIVE_DEIVCE_CHANGED = 20; + private static final int MSG_A2DP_ACTIVE_DEVICE_CHANGED = 20; private static final int MSG_HFP_ACTIVE_DEVICE_CHANGED = 21; private static final int ENABLE_SILENCE = 0; private static final int DISABLE_SILENCE = 1; - // Broadcast receiver for all changes - private final BroadcastReceiver mReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - if (action == null) { - Log.e(TAG, "Received intent with null action"); - return; - } - switch (action) { - case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED: - mHandler.obtainMessage(MSG_A2DP_CONNECTION_STATE_CHANGED, - intent).sendToTarget(); - break; - case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED: - mHandler.obtainMessage(MSG_HFP_CONNECTION_STATE_CHANGED, - intent).sendToTarget(); - break; - case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED: - mHandler.obtainMessage(MSG_A2DP_ACTIVE_DEIVCE_CHANGED, - intent).sendToTarget(); - break; - case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED: - mHandler.obtainMessage(MSG_HFP_ACTIVE_DEVICE_CHANGED, - intent).sendToTarget(); - break; - default: - Log.e(TAG, "Received unexpected intent, action=" + action); - break; - } - } - }; + /** + * Called when A2DP connection state changed by A2dpService + * + * @param device The device of which connection state was changed + * @param fromState The previous connection state of the device + * @param toState The new connection state of the device + */ + public void a2dpConnectionStateChanged(BluetoothDevice device, int fromState, int toState) { + mHandler.obtainMessage(MSG_A2DP_CONNECTION_STATE_CHANGED, fromState, toState, device) + .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 + * + * @param device The device of which connection state was changed + * @param fromState The previous connection state of the device + * @param toState The new connection state of the device + */ + public void hfpConnectionStateChanged(BluetoothDevice device, int fromState, int toState) { + mHandler.obtainMessage(MSG_HFP_CONNECTION_STATE_CHANGED, fromState, toState, device) + .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) { @@ -132,12 +136,10 @@ public class SilenceDeviceManager { } break; - case MSG_A2DP_CONNECTION_STATE_CHANGED: { - Intent intent = (Intent) msg.obj; - BluetoothDevice device = - intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1); - int nextState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); + case MSG_A2DP_CONNECTION_STATE_CHANGED: + BluetoothDevice device = (BluetoothDevice) msg.obj; + int prevState = msg.arg1; + int nextState = msg.arg2; if (nextState == BluetoothProfile.STATE_CONNECTED) { // enter connected state @@ -153,62 +155,51 @@ public class SilenceDeviceManager { mSilenceDevices.remove(device); } } - } - break; + break; - case MSG_HFP_CONNECTION_STATE_CHANGED: { - Intent intent = (Intent) msg.obj; - BluetoothDevice device = - intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1); - int nextState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); + case MSG_HFP_CONNECTION_STATE_CHANGED: + BluetoothDevice bluetoothDevice = (BluetoothDevice) msg.obj; + int prev = msg.arg1; + int next = msg.arg2; - if (nextState == BluetoothProfile.STATE_CONNECTED) { + if (next == BluetoothProfile.STATE_CONNECTED) { // enter connected state - addConnectedDevice(device, BluetoothProfile.HEADSET); - if (!mSilenceDevices.containsKey(device)) { - mSilenceDevices.put(device, false); + addConnectedDevice(bluetoothDevice, BluetoothProfile.HEADSET); + if (!mSilenceDevices.containsKey(bluetoothDevice)) { + mSilenceDevices.put(bluetoothDevice, false); } - } else if (prevState == BluetoothProfile.STATE_CONNECTED) { + } else if (prev == BluetoothProfile.STATE_CONNECTED) { // exiting from connected state - removeConnectedDevice(device, BluetoothProfile.HEADSET); - if (!isBluetoothAudioConnected(device)) { - handleSilenceDeviceStateChanged(device, false); - mSilenceDevices.remove(device); + removeConnectedDevice(bluetoothDevice, BluetoothProfile.HEADSET); + if (!isBluetoothAudioConnected(bluetoothDevice)) { + handleSilenceDeviceStateChanged(bluetoothDevice, false); + mSilenceDevices.remove(bluetoothDevice); } } - } - break; + break; - case MSG_A2DP_ACTIVE_DEIVCE_CHANGED: { - Intent intent = (Intent) msg.obj; - BluetoothDevice a2dpActiveDevice = - intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + case MSG_A2DP_ACTIVE_DEVICE_CHANGED: + BluetoothDevice a2dpActiveDevice = (BluetoothDevice) msg.obj; if (getSilenceMode(a2dpActiveDevice)) { // Resume the device from silence mode. setSilenceMode(a2dpActiveDevice, false); } - } - break; + break; - case MSG_HFP_ACTIVE_DEVICE_CHANGED: { - Intent intent = (Intent) msg.obj; - BluetoothDevice hfpActiveDevice = - intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + case MSG_HFP_ACTIVE_DEVICE_CHANGED: + BluetoothDevice hfpActiveDevice = (BluetoothDevice) msg.obj; if (getSilenceMode(hfpActiveDevice)) { // Resume the device from silence mode. setSilenceMode(hfpActiveDevice, false); } - } - break; + break; - default: { + default: Log.e(TAG, "Unknown message: " + msg.what); - } - break; + break; } } - }; + } SilenceDeviceManager(AdapterService service, ServiceFactory factory, Looper looper) { mAdapterService = service; @@ -221,13 +212,6 @@ public class SilenceDeviceManager { Log.v(TAG, "start()"); } mHandler = new SilenceDeviceManagerHandler(mLooper); - IntentFilter filter = new IntentFilter(); - filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); - filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); - filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED); - filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); - filter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED); - mAdapterService.registerReceiver(mReceiver, filter); } void cleanup() { @@ -235,7 +219,6 @@ public class SilenceDeviceManager { Log.v(TAG, "cleanup()"); } mSilenceDevices.clear(); - mAdapterService.unregisterReceiver(mReceiver); } @VisibleForTesting @@ -347,9 +330,4 @@ public class SilenceDeviceManager { writer.println(" " + device+ " | " + getSilenceMode(device)); } } - - @VisibleForTesting - BroadcastReceiver getBroadcastReceiver() { - return mReceiver; - } } diff --git a/android/app/src/com/android/bluetooth/hfp/HeadsetService.java b/android/app/src/com/android/bluetooth/hfp/HeadsetService.java index 6e11509defcb30d35a01e89865d9bad34784c065..abbc4110442a979fb2dfdaed48bc5ccd7e587e32 100644 --- a/android/app/src/com/android/bluetooth/hfp/HeadsetService.java +++ b/android/app/src/com/android/bluetooth/hfp/HeadsetService.java @@ -2008,6 +2008,9 @@ public class HeadsetService extends ProfileService { } } mActiveDeviceManager.hfpConnectionStateChanged(device, fromState, toState); + mAdapterService + .getSilenceDeviceManager() + .hfpConnectionStateChanged(device, fromState, toState); } /** @@ -2133,10 +2136,16 @@ public class HeadsetService extends ProfileService { private void broadcastActiveDevice(BluetoothDevice device) { logD("broadcastActiveDevice: " + device); + mAdapterService.getActiveDeviceManager().hfpActiveStateChanged(device); - BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED, - BluetoothProfile.HEADSET, mAdapterService.obfuscateAddress(device), + mAdapterService.getSilenceDeviceManager().hfpActiveDeviceChanged(device); + + BluetoothStatsLog.write( + BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED, + BluetoothProfile.HEADSET, + mAdapterService.obfuscateAddress(device), mAdapterService.getMetricId(device)); + Intent intent = new Intent(BluetoothHeadset.ACTION_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/a2dp/A2dpServiceTest.java b/android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpServiceTest.java index 9b5d11825840c4d56001a272fbb064536ce983e6..8114dbaefdae2e2ef62b7c2427b873de193585e5 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpServiceTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpServiceTest.java @@ -42,6 +42,7 @@ import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.ActiveDeviceManager; import com.android.bluetooth.btservice.AdapterService; +import com.android.bluetooth.btservice.SilenceDeviceManager; import com.android.bluetooth.btservice.storage.DatabaseManager; import org.junit.After; @@ -79,6 +80,7 @@ public class A2dpServiceTest { @Mock private ActiveDeviceManager mActiveDeviceManager; @Mock private A2dpNativeInterface mMockNativeInterface; @Mock private DatabaseManager mDatabaseManager; + @Mock private SilenceDeviceManager mSilenceDeviceManager; @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); @@ -99,6 +101,7 @@ public class A2dpServiceTest { doReturn(false).when(mAdapterService).isQuietModeEnabled(); doReturn(mDatabaseManager).when(mAdapterService).getDatabase(); doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); + doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager(); mAdapter = BluetoothAdapter.getDefaultAdapter(); diff --git a/android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpStateMachineTest.java b/android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpStateMachineTest.java index 1a5e6f33564b52a9240ed809ed127d06ec944407..979f26e02995665c157c7dc1c7398418567ca820 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpStateMachineTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpStateMachineTest.java @@ -35,6 +35,7 @@ import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.ActiveDeviceManager; +import com.android.bluetooth.btservice.SilenceDeviceManager; import com.android.bluetooth.btservice.AdapterService; import org.hamcrest.core.IsInstanceOf; @@ -65,6 +66,7 @@ public class A2dpStateMachineTest { @Mock private AdapterService mAdapterService; @Mock private ActiveDeviceManager mActiveDeviceManager; + @Mock private SilenceDeviceManager mSilenceDeviceManager; @Mock private A2dpService mA2dpService; @Mock private A2dpNativeInterface mA2dpNativeInterface; @@ -74,6 +76,7 @@ public class A2dpStateMachineTest { // Set up mocks and test assets MockitoAnnotations.initMocks(this); doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); + doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager(); TestUtils.setAdapterService(mAdapterService); diff --git a/android/app/tests/unit/src/com/android/bluetooth/btservice/SilenceDeviceManagerTest.java b/android/app/tests/unit/src/com/android/bluetooth/btservice/SilenceDeviceManagerTest.java index b90030565d77c94e5a4a04813e54d486a4c5c42d..23aac3991ca736fc1086e6d12f702f482d012fed 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/btservice/SilenceDeviceManagerTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/btservice/SilenceDeviceManagerTest.java @@ -17,12 +17,11 @@ package com.android.bluetooth.btservice; import static android.Manifest.permission.BLUETOOTH_CONNECT; + import static org.mockito.Mockito.*; -import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothProfile; import android.content.Context; import android.content.Intent; @@ -172,11 +171,8 @@ public class SilenceDeviceManagerTest { * Helper to indicate A2dp connected for a device. */ private void a2dpConnected(BluetoothDevice device) { - Intent intent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); - intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); - intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED); - intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_CONNECTED); - mSilenceDeviceManager.getBroadcastReceiver().onReceive(mContext, intent); + mSilenceDeviceManager.a2dpConnectionStateChanged( + device, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED); TestUtils.waitForLooperToFinishScheduledTask(mLooper); } @@ -184,11 +180,8 @@ public class SilenceDeviceManagerTest { * Helper to indicate A2dp disconnected for a device. */ private void a2dpDisconnected(BluetoothDevice device) { - Intent intent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); - intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); - intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTED); - intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED); - mSilenceDeviceManager.getBroadcastReceiver().onReceive(mContext, intent); + mSilenceDeviceManager.a2dpConnectionStateChanged( + device, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED); TestUtils.waitForLooperToFinishScheduledTask(mLooper); } @@ -196,11 +189,8 @@ public class SilenceDeviceManagerTest { * Helper to indicate Headset connected for a device. */ private void headsetConnected(BluetoothDevice device) { - Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); - intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); - intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED); - intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_CONNECTED); - mSilenceDeviceManager.getBroadcastReceiver().onReceive(mContext, intent); + mSilenceDeviceManager.hfpConnectionStateChanged( + device, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED); TestUtils.waitForLooperToFinishScheduledTask(mLooper); } @@ -208,12 +198,8 @@ public class SilenceDeviceManagerTest { * Helper to indicate Headset disconnected for a device. */ private void headsetDisconnected(BluetoothDevice device) { - Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); - intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); - intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTED); - intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED); - mSilenceDeviceManager.getBroadcastReceiver().onReceive(mContext, intent); + mSilenceDeviceManager.hfpConnectionStateChanged( + device, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED); TestUtils.waitForLooperToFinishScheduledTask(mLooper); } } - 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 26649e96f0641149161374018e092942984ee145..1097f882551be397e9d3961b3c4c1be3834d9230 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 @@ -52,6 +52,7 @@ import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.ActiveDeviceManager; import com.android.bluetooth.btservice.AdapterService; +import com.android.bluetooth.btservice.SilenceDeviceManager; import com.android.bluetooth.btservice.storage.DatabaseManager; import org.hamcrest.Matchers; @@ -152,6 +153,7 @@ public class HeadsetServiceAndStateMachineTest { @Spy private HeadsetObjectsFactory mObjectsFactory = HeadsetObjectsFactory.getInstance(); @Mock private AdapterService mAdapterService; @Mock private ActiveDeviceManager mActiveDeviceManager; + @Mock private SilenceDeviceManager mSilenceDeviceManager; @Mock private DatabaseManager mDatabaseManager; @Mock private HeadsetSystemInterface mSystemInterface; @Mock private AudioManager mAudioManager; @@ -187,6 +189,7 @@ public class HeadsetServiceAndStateMachineTest { doReturn(new BluetoothSinkAudioPolicy.Builder().build()).when(mAdapterService) .getRequestedAudioPolicyAsSink(any(BluetoothDevice.class)); doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); + doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager(); // Mock system interface doNothing().when(mSystemInterface).stop(); doReturn(mPhoneState).when(mSystemInterface).getHeadsetPhoneState(); @@ -471,6 +474,7 @@ public class HeadsetServiceAndStateMachineTest { Assert.assertTrue(mHeadsetService.setActiveDevice(activeDevice)); verify(mNativeInterface).setActiveDevice(activeDevice); verify(mActiveDeviceManager).hfpActiveStateChanged(activeDevice); + verify(mSilenceDeviceManager).hfpActiveDeviceChanged(activeDevice); Assert.assertEquals(activeDevice, mHeadsetService.getActiveDevice()); // Start virtual call Assert.assertTrue(mHeadsetService.startScoUsingVirtualVoiceCall()); @@ -1181,6 +1185,11 @@ public class HeadsetServiceAndStateMachineTest { device, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTING); + verify(mSilenceDeviceManager, timeout(STATE_CHANGE_TIMEOUT_MILLIS)) + .hfpConnectionStateChanged( + device, + BluetoothProfile.STATE_DISCONNECTED, + BluetoothProfile.STATE_CONNECTING); Assert.assertEquals(BluetoothProfile.STATE_CONNECTING, mHeadsetService.getConnectionState(device)); Assert.assertEquals(Collections.singletonList(device), @@ -1196,6 +1205,11 @@ public class HeadsetServiceAndStateMachineTest { device, BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED); + verify(mSilenceDeviceManager, timeout(STATE_CHANGE_TIMEOUT_MILLIS)) + .hfpConnectionStateChanged( + device, + BluetoothProfile.STATE_CONNECTING, + BluetoothProfile.STATE_CONNECTED); Assert.assertEquals(BluetoothProfile.STATE_CONNECTED, mHeadsetService.getConnectionState(device)); } diff --git a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceTest.java b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceTest.java index 643fd3ebf56218c37f071e63fe88c5b292e7c6e4..d36f98e4b815fb7242dab71af8a35d06e70f27a7 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceTest.java @@ -53,6 +53,7 @@ import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.ActiveDeviceManager; import com.android.bluetooth.btservice.AdapterService; +import com.android.bluetooth.btservice.SilenceDeviceManager; import com.android.bluetooth.btservice.storage.DatabaseManager; import org.hamcrest.Matchers; @@ -94,6 +95,7 @@ public class HeadsetServiceTest { @Spy private HeadsetObjectsFactory mObjectsFactory = HeadsetObjectsFactory.getInstance(); @Mock private AdapterService mAdapterService; @Mock private ActiveDeviceManager mActiveDeviceManager; + @Mock private SilenceDeviceManager mSilenceDeviceManager; @Mock private DatabaseManager mDatabaseManager; @Mock private HeadsetSystemInterface mSystemInterface; @Mock private HeadsetNativeInterface mNativeInterface; @@ -123,6 +125,7 @@ public class HeadsetServiceTest { doReturn(BluetoothDevice.BOND_BONDED).when(mAdapterService) .getBondState(any(BluetoothDevice.class)); doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); + doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager(); doReturn(mDatabaseManager).when(mAdapterService).getDatabase(); doReturn(true, false).when(mAdapterService).isStartedProfile(anyString()); doAnswer(invocation -> { diff --git a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetStateMachineTest.java b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetStateMachineTest.java index ed6cda9bb999e2f9b7b1712425919487357fc5b0..aedb1b0ceb4ba180849fbd8b1e9f9cc29bdbe443 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetStateMachineTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetStateMachineTest.java @@ -51,6 +51,7 @@ import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.ActiveDeviceManager; import com.android.bluetooth.btservice.AdapterService; +import com.android.bluetooth.btservice.SilenceDeviceManager; import com.android.bluetooth.btservice.storage.DatabaseManager; import org.hamcrest.core.IsInstanceOf; @@ -87,6 +88,7 @@ public class HeadsetStateMachineTest { @Mock private AdapterService mAdapterService; @Mock private ActiveDeviceManager mActiveDeviceManager; + @Mock private SilenceDeviceManager mSilenceDeviceManager; @Mock private DatabaseManager mDatabaseManager; @Mock private HeadsetService mHeadsetService; @Mock private HeadsetSystemInterface mSystemInterface; @@ -114,6 +116,8 @@ public class HeadsetStateMachineTest { doReturn(true).when(mDatabaseManager).setAudioPolicyMetadata(anyObject(), anyObject()); // Get an active device manager doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); + // Get a silence device manager + doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager(); doReturn(true).when(mNativeInterface).connectHfp(mTestDevice); doReturn(true).when(mNativeInterface).disconnectHfp(mTestDevice); doReturn(true).when(mNativeInterface).connectAudio(mTestDevice);