Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 812e1460 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Stop using intent from HearingAid profile to ActiveDeviceManager

Bug: 289879962
Test: atest BluetoothInstrumentationTest
Change-Id: I44cd7d04d98c951c606d6a048f2cdc83c34cbf1c
parent 35390b96
Loading
Loading
Loading
Loading
+29 −18
Original line number Diff line number Diff line
@@ -71,8 +71,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:
 *    - BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED for HearingAid
 *    - BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED for LE audio
 *    If such broadcast is received (e.g., triggered indirectly by user
 *    action on the UI), the device in the received broadcast is marked
 *    as the current active device for that profile.
@@ -183,13 +181,6 @@ public class ActiveDeviceManager {
            }

            switch (action) {
                case BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED:
                    if (currentState == BluetoothProfile.STATE_CONNECTED) {
                        mHandler.post(() -> handleHearingAidConnected(device));
                    } else if (previousState == BluetoothProfile.STATE_CONNECTED) {
                        mHandler.post(() -> handleHearingAidDisconnected(device));
                    }
                    break;
                case BluetoothLeAudio.ACTION_LE_AUDIO_CONNECTION_STATE_CHANGED:
                    if (currentState == BluetoothProfile.STATE_CONNECTED) {
                        mHandler.post(() -> handleLeAudioConnected(device));
@@ -204,9 +195,6 @@ public class ActiveDeviceManager {
                        mHandler.post(() -> handleHapDisconnected(device));
                    }
                    break;
                case BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.post(() -> handleHearingAidActiveDeviceChanged(device));
                    break;
                case BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED:
                    mHandler.post(() -> handleLeAudioActiveDeviceChanged(device));
                    break;
@@ -218,7 +206,7 @@ public class ActiveDeviceManager {
    };

    /**
     * Called when A2DP connection state changed by A2dpStateMachine
     * 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
@@ -242,7 +230,7 @@ public class ActiveDeviceManager {
    }

    /**
     * Called when HFP connection state changed by HeadsetStateMachine
     * Called when HFP connection state changed by HeadsetService
     *
     * @param device The device of which connection state was changed
     * @param prevState The previous connection state of the device
@@ -257,14 +245,39 @@ public class ActiveDeviceManager {
    }

    /**
     * Called when HFP active state changed by HeadsetStateMachine
     * Called when HFP active state changed by HeadsetService
     *
     * @param device The device currently activated. {@code null} if no A2DP device activated
     * @param device The device currently activated. {@code null} if no HFP device activated
     */
    public void hfpActiveStateChanged(BluetoothDevice device) {
        mHandler.post(() -> handleHfpActiveDeviceChanged(device));
    }

    /**
     * Called when HearingAid connection state changed by HearingAidService
     *
     * @param device The device of which connection state was changed
     * @param prevState The previous connection state of the device
     * @param newState The new connection state of the device
     */
    public void hearingAidConnectionStateChanged(
            BluetoothDevice device, int prevState, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mHandler.post(() -> handleHearingAidConnected(device));
        } else if (prevState == BluetoothProfile.STATE_CONNECTED) {
            mHandler.post(() -> handleHearingAidDisconnected(device));
        }
    }

    /**
     * Called when HearingAid active state changed by HearingAidService
     *
     * @param device The device currently activated. {@code null} if no HearingAid device activated
     */
    public void hearingAidActiveStateChanged(BluetoothDevice device) {
        mHandler.post(() -> handleHearingAidActiveDeviceChanged(device));
    }

    private void handleAdapterStateChanged(int currentState) {
        if (DBG) {
            Log.d(TAG, "handleAdapterStateChanged: currentState=" + currentState);
@@ -827,8 +840,6 @@ public class ActiveDeviceManager {
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothLeAudio.ACTION_LE_AUDIO_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED);
+4 −3
Original line number Diff line number Diff line
@@ -155,9 +155,6 @@ public class HearingAidService extends ProfileService {
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mBondStateChangedReceiver = new BondStateChangedReceiver();
        registerReceiver(mBondStateChangedReceiver, filter);
        filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);

        // Mark service as started
        setHearingAidService(this);
@@ -733,6 +730,7 @@ public class HearingAidService extends ProfileService {
    }

    private void notifyActiveDeviceChanged() {
        mAdapterService.getActiveDeviceManager().hearingAidActiveStateChanged(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
@@ -964,6 +962,9 @@ public class HearingAidService extends ProfileService {
                removeStateMachine(device);
            }
        }
        mAdapterService
                .getActiveDeviceManager()
                .hearingAidConnectionStateChanged(device, fromState, toState);
    }

    /**
+1 −0
Original line number Diff line number Diff line
@@ -1143,6 +1143,7 @@ public class LeAudioService extends ProfileService {
                    + ". Currently active device is " + mActiveAudioOutDevice);
        }

        mAdapterService.getActiveDeviceManager();
        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
+8 −16
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHapClient;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothSinkAudioPolicy;
@@ -1213,13 +1212,11 @@ public class ActiveDeviceManagerTest {
     * Helper to indicate Hearing Aid connected for a device.
     */
    private void hearingAidConnected(BluetoothDevice device) {
        mDeviceConnectionStack.add(device);
        mMostRecentDevice = device;

        Intent intent = new Intent(BluetoothHearingAid.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);
        mActiveDeviceManager.getBroadcastReceiver().onReceive(mContext, intent);
        mActiveDeviceManager.hearingAidConnectionStateChanged(
                device, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED);
    }

    /**
@@ -1230,24 +1227,19 @@ public class ActiveDeviceManagerTest {
        mMostRecentDevice = (mDeviceConnectionStack.size() > 0)
                ? mDeviceConnectionStack.get(mDeviceConnectionStack.size() - 1) : null;

        Intent intent = new Intent(BluetoothHearingAid.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);
        mActiveDeviceManager.getBroadcastReceiver().onReceive(mContext, intent);
        mActiveDeviceManager.hearingAidConnectionStateChanged(
                device, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED);
    }

    /**
     * Helper to indicate Hearing Aid active device changed for a device.
     */
    private void hearingAidActiveDeviceChanged(BluetoothDevice device) {
        mMostRecentDevice = device;

        Intent intent = new Intent(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        mActiveDeviceManager.getBroadcastReceiver().onReceive(mContext, intent);
        mDeviceConnectionStack.remove(device);
        mDeviceConnectionStack.add(device);
        mMostRecentDevice = device;

        mActiveDeviceManager.hearingAidActiveStateChanged(device);
    }

    /**
+3 −0
Original line number Diff line number Diff line
@@ -44,6 +44,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;
import com.android.bluetooth.x.com.android.modules.utils.SynchronousResultReceiver;
@@ -80,6 +81,7 @@ public class HearingAidServiceTest {
    private BroadcastReceiver mHearingAidIntentReceiver;

    @Mock private AdapterService mAdapterService;
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private HearingAidNativeInterface mNativeInterface;
    @Mock private AudioManager mAudioManager;
@@ -97,6 +99,7 @@ public class HearingAidServiceTest {
        }

        TestUtils.setAdapterService(mAdapterService);
        doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager();
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());