Loading android/app/src/com/android/bluetooth/a2dp/A2dpService.java +4 −0 Original line number Diff line number Diff line Loading @@ -1071,6 +1071,7 @@ public class A2dpService extends ProfileService { 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()); } Loading Loading @@ -1258,6 +1259,9 @@ public class A2dpService extends ProfileService { removeStateMachine(device); } } mAdapterService .getActiveDeviceManager() .a2dpConnectionStateChanged(device, fromState, toState); } /** Loading android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java +25 −15 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SuppressLint; import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; Loading Loading @@ -73,7 +72,6 @@ import java.util.Set; * 3) The HFP active device might be different from the A2DP active device. * 4) The Active Device Manager always listens for ACTION_ACTIVE_DEVICE_CHANGED * broadcasts for each profile: * - BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED for A2DP * - BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED for HFP * - BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED for HearingAid * - BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED for LE audio Loading Loading @@ -114,7 +112,7 @@ import java.util.Set; * will have no impact. E.g., music will continue streaming over the * active Bluetooth device. */ class ActiveDeviceManager { public class ActiveDeviceManager { private static final String TAG = "ActiveDeviceManager"; private static final boolean DBG = true; @VisibleForTesting Loading Loading @@ -187,13 +185,6 @@ class ActiveDeviceManager { } switch (action) { case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED: if (currentState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleA2dpConnected(device)); } else if (previousState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleA2dpDisconnected(device)); } break; case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED: if (currentState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleHfpConnected(device)); Loading Loading @@ -222,9 +213,6 @@ class ActiveDeviceManager { mHandler.post(() -> handleHapDisconnected(device)); } break; case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED: mHandler.post(() -> handleA2dpActiveDeviceChanged(device)); break; case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED: mHandler.post(() -> handleHfpActiveDeviceChanged(device)); break; Loading @@ -241,6 +229,30 @@ class ActiveDeviceManager { } }; /** * Called when A2DP connection state changed by A2dpStateMachine * * @param device The device of which connection state was changed * @param fropmState The previous connection state of the device * @param toState The new connection state of the device */ public void a2dpConnectionStateChanged(BluetoothDevice device, int fropmState, int toState) { if (toState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleA2dpConnected(device)); } else if (fropmState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleA2dpDisconnected(device)); } } /** * Called when A2DP active state changed by A2dpService * * @param device The device currently activated. {@code null} if no A2DP device activated */ public void a2dpActiveStateChanged(BluetoothDevice device) { mHandler.post(() -> handleA2dpActiveDeviceChanged(device)); } private void handleAdapterStateChanged(int currentState) { if (DBG) { Log.d(TAG, "handleAdapterStateChanged: currentState=" + currentState); Loading Loading @@ -803,8 +815,6 @@ class ActiveDeviceManager { IntentFilter filter = new IntentFilter(); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 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); filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); Loading android/app/src/com/android/bluetooth/btservice/AdapterService.java +4 −0 Original line number Diff line number Diff line Loading @@ -736,6 +736,10 @@ public class AdapterService extends Service { } } public ActiveDeviceManager getActiveDeviceManager() { return mActiveDeviceManager; } private boolean initMetricsLogger() { if (mMetricsLogger != null) { return false; Loading android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpServiceTest.java +3 −0 Original line number Diff line number Diff line Loading @@ -40,6 +40,7 @@ import androidx.test.rule.ServiceTestRule; 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.storage.DatabaseManager; Loading Loading @@ -76,6 +77,7 @@ public class A2dpServiceTest { private final BlockingQueue<Intent> mCodecConfigChangedQueue = new LinkedBlockingQueue<>(); @Mock private AdapterService mAdapterService; @Mock private ActiveDeviceManager mActiveDeviceManager; @Mock private A2dpNativeInterface mA2dpNativeInterface; @Mock private DatabaseManager mDatabaseManager; Loading @@ -97,6 +99,7 @@ public class A2dpServiceTest { doReturn(true, false).when(mAdapterService).isStartedProfile(anyString()); doReturn(false).when(mAdapterService).isQuietModeEnabled(); doReturn(mDatabaseManager).when(mAdapterService).getDatabase(); doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); mAdapter = BluetoothAdapter.getDefaultAdapter(); Loading android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpStateMachineTest.java +4 −2 Original line number Diff line number Diff line Loading @@ -33,14 +33,13 @@ import androidx.test.InstrumentationRegistry; import androidx.test.filters.MediumTest; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.R; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.ActiveDeviceManager; import com.android.bluetooth.btservice.AdapterService; import org.hamcrest.core.IsInstanceOf; import org.junit.After; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; Loading @@ -65,6 +64,7 @@ public class A2dpStateMachineTest { private BluetoothCodecConfig mCodecConfigOpus; @Mock private AdapterService mAdapterService; @Mock private ActiveDeviceManager mActiveDeviceManager; @Mock private A2dpService mA2dpService; @Mock private A2dpNativeInterface mA2dpNativeInterface; Loading @@ -73,6 +73,8 @@ public class A2dpStateMachineTest { mTargetContext = InstrumentationRegistry.getTargetContext(); // Set up mocks and test assets MockitoAnnotations.initMocks(this); doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); TestUtils.setAdapterService(mAdapterService); mAdapter = BluetoothAdapter.getDefaultAdapter(); Loading Loading
android/app/src/com/android/bluetooth/a2dp/A2dpService.java +4 −0 Original line number Diff line number Diff line Loading @@ -1071,6 +1071,7 @@ public class A2dpService extends ProfileService { 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()); } Loading Loading @@ -1258,6 +1259,9 @@ public class A2dpService extends ProfileService { removeStateMachine(device); } } mAdapterService .getActiveDeviceManager() .a2dpConnectionStateChanged(device, fromState, toState); } /** Loading
android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java +25 −15 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SuppressLint; import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; Loading Loading @@ -73,7 +72,6 @@ import java.util.Set; * 3) The HFP active device might be different from the A2DP active device. * 4) The Active Device Manager always listens for ACTION_ACTIVE_DEVICE_CHANGED * broadcasts for each profile: * - BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED for A2DP * - BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED for HFP * - BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED for HearingAid * - BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED for LE audio Loading Loading @@ -114,7 +112,7 @@ import java.util.Set; * will have no impact. E.g., music will continue streaming over the * active Bluetooth device. */ class ActiveDeviceManager { public class ActiveDeviceManager { private static final String TAG = "ActiveDeviceManager"; private static final boolean DBG = true; @VisibleForTesting Loading Loading @@ -187,13 +185,6 @@ class ActiveDeviceManager { } switch (action) { case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED: if (currentState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleA2dpConnected(device)); } else if (previousState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleA2dpDisconnected(device)); } break; case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED: if (currentState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleHfpConnected(device)); Loading Loading @@ -222,9 +213,6 @@ class ActiveDeviceManager { mHandler.post(() -> handleHapDisconnected(device)); } break; case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED: mHandler.post(() -> handleA2dpActiveDeviceChanged(device)); break; case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED: mHandler.post(() -> handleHfpActiveDeviceChanged(device)); break; Loading @@ -241,6 +229,30 @@ class ActiveDeviceManager { } }; /** * Called when A2DP connection state changed by A2dpStateMachine * * @param device The device of which connection state was changed * @param fropmState The previous connection state of the device * @param toState The new connection state of the device */ public void a2dpConnectionStateChanged(BluetoothDevice device, int fropmState, int toState) { if (toState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleA2dpConnected(device)); } else if (fropmState == BluetoothProfile.STATE_CONNECTED) { mHandler.post(() -> handleA2dpDisconnected(device)); } } /** * Called when A2DP active state changed by A2dpService * * @param device The device currently activated. {@code null} if no A2DP device activated */ public void a2dpActiveStateChanged(BluetoothDevice device) { mHandler.post(() -> handleA2dpActiveDeviceChanged(device)); } private void handleAdapterStateChanged(int currentState) { if (DBG) { Log.d(TAG, "handleAdapterStateChanged: currentState=" + currentState); Loading Loading @@ -803,8 +815,6 @@ class ActiveDeviceManager { IntentFilter filter = new IntentFilter(); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 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); filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); Loading
android/app/src/com/android/bluetooth/btservice/AdapterService.java +4 −0 Original line number Diff line number Diff line Loading @@ -736,6 +736,10 @@ public class AdapterService extends Service { } } public ActiveDeviceManager getActiveDeviceManager() { return mActiveDeviceManager; } private boolean initMetricsLogger() { if (mMetricsLogger != null) { return false; Loading
android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpServiceTest.java +3 −0 Original line number Diff line number Diff line Loading @@ -40,6 +40,7 @@ import androidx.test.rule.ServiceTestRule; 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.storage.DatabaseManager; Loading Loading @@ -76,6 +77,7 @@ public class A2dpServiceTest { private final BlockingQueue<Intent> mCodecConfigChangedQueue = new LinkedBlockingQueue<>(); @Mock private AdapterService mAdapterService; @Mock private ActiveDeviceManager mActiveDeviceManager; @Mock private A2dpNativeInterface mA2dpNativeInterface; @Mock private DatabaseManager mDatabaseManager; Loading @@ -97,6 +99,7 @@ public class A2dpServiceTest { doReturn(true, false).when(mAdapterService).isStartedProfile(anyString()); doReturn(false).when(mAdapterService).isQuietModeEnabled(); doReturn(mDatabaseManager).when(mAdapterService).getDatabase(); doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); mAdapter = BluetoothAdapter.getDefaultAdapter(); Loading
android/app/tests/unit/src/com/android/bluetooth/a2dp/A2dpStateMachineTest.java +4 −2 Original line number Diff line number Diff line Loading @@ -33,14 +33,13 @@ import androidx.test.InstrumentationRegistry; import androidx.test.filters.MediumTest; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.R; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.ActiveDeviceManager; import com.android.bluetooth.btservice.AdapterService; import org.hamcrest.core.IsInstanceOf; import org.junit.After; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; Loading @@ -65,6 +64,7 @@ public class A2dpStateMachineTest { private BluetoothCodecConfig mCodecConfigOpus; @Mock private AdapterService mAdapterService; @Mock private ActiveDeviceManager mActiveDeviceManager; @Mock private A2dpService mA2dpService; @Mock private A2dpNativeInterface mA2dpNativeInterface; Loading @@ -73,6 +73,8 @@ public class A2dpStateMachineTest { mTargetContext = InstrumentationRegistry.getTargetContext(); // Set up mocks and test assets MockitoAnnotations.initMocks(this); doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager(); TestUtils.setAdapterService(mAdapterService); mAdapter = BluetoothAdapter.getDefaultAdapter(); Loading