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

Commit ca583018 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Revert "Revert "Stop using intent from HearingAid profile to ActiveDeviceManager""

This also prevent the call of notifyActiveDeviceChange() if
HearingAidService stopped.

Bug: 291031841
Bug: 289879962
Test: atest BluetoothInstrumentationTests
Test: avatar run --mobly-std-log --include-filter=AshaTest
Change-Id: I944730c90ed3705bf9a7e2e56aad7aaa19db000f
parent deda5454
Loading
Loading
Loading
Loading
+29 −17
Original line number Diff line number Diff line
@@ -71,7 +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
@@ -183,13 +182,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 +196,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 +207,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 +231,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 +246,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 +841,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);
+6 −5
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
@@ -746,11 +744,11 @@ public class HearingAidService extends ProfileService {
        public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
            for (AudioDeviceInfo deviceInfo : removedDevices) {
                if (deviceInfo.getType() == AudioDeviceInfo.TYPE_HEARING_AID) {
                    notifyActiveDeviceChanged();
                    if (DBG) {
                        Log.d(TAG, " onAudioDevicesRemoved: device type: " + deviceInfo.getType());
                    }
                    if (mAudioManager != null) {
                        notifyActiveDeviceChanged();
                        mAudioManager.unregisterAudioDeviceCallback(this);
                    } else {
                        Log.w(TAG, "onAudioDevicesRemoved: mAudioManager is null");
@@ -766,11 +764,11 @@ public class HearingAidService extends ProfileService {
        public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
            for (AudioDeviceInfo deviceInfo : addedDevices) {
                if (deviceInfo.getType() == AudioDeviceInfo.TYPE_HEARING_AID) {
                    notifyActiveDeviceChanged();
                    if (DBG) {
                        Log.d(TAG, " onAudioDevicesAdded: device type: " + deviceInfo.getType());
                    }
                    if (mAudioManager != null) {
                        notifyActiveDeviceChanged();
                        mAudioManager.unregisterAudioDeviceCallback(this);
                    } else {
                        Log.w(TAG, "onAudioDevicesAdded: mAudioManager is null");
@@ -964,6 +962,9 @@ public class HearingAidService extends ProfileService {
                removeStateMachine(device);
            }
        }
        mAdapterService
                .getActiveDeviceManager()
                .hearingAidConnectionStateChanged(device, fromState, toState);
    }

    /**
+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());