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

Commit 36c87c90 authored by Abel Lucas's avatar Abel Lucas Committed by Gerrit Code Review
Browse files

Revert "Stop using intent from HearingAid profile to ActiveDeviceManager"

This reverts commit 812e1460.

Reason for revert: b/291031841

Change-Id: Iaadc31f58574c5ff503fc4aeedbfa9bd906c84b1
parent 812e1460
Loading
Loading
Loading
Loading
+18 −29
Original line number Original line Diff line number Diff line
@@ -71,6 +71,8 @@ import java.util.Set;
 * 3) The HFP active device might be different from the A2DP active device.
 * 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
 * 4) The Active Device Manager always listens for ACTION_ACTIVE_DEVICE_CHANGED
 *    broadcasts for each profile:
 *    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
 *    If such broadcast is received (e.g., triggered indirectly by user
 *    action on the UI), the device in the received broadcast is marked
 *    action on the UI), the device in the received broadcast is marked
 *    as the current active device for that profile.
 *    as the current active device for that profile.
@@ -181,6 +183,13 @@ public class ActiveDeviceManager {
            }
            }


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


    /**
    /**
     * Called when A2DP connection state changed by A2dpService
     * Called when A2DP connection state changed by A2dpStateMachine
     *
     *
     * @param device The device of which connection state was changed
     * @param device The device of which connection state was changed
     * @param fromState The previous connection state of the device
     * @param fromState The previous connection state of the device
@@ -230,7 +242,7 @@ public class ActiveDeviceManager {
    }
    }


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


    /**
    /**
     * Called when HFP active state changed by HeadsetService
     * Called when HFP active state changed by HeadsetStateMachine
     *
     *
     * @param device The device currently activated. {@code null} if no HFP device activated
     * @param device The device currently activated. {@code null} if no A2DP device activated
     */
     */
    public void hfpActiveStateChanged(BluetoothDevice device) {
    public void hfpActiveStateChanged(BluetoothDevice device) {
        mHandler.post(() -> handleHfpActiveDeviceChanged(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) {
    private void handleAdapterStateChanged(int currentState) {
        if (DBG) {
        if (DBG) {
            Log.d(TAG, "handleAdapterStateChanged: currentState=" + currentState);
            Log.d(TAG, "handleAdapterStateChanged: currentState=" + currentState);
@@ -840,6 +827,8 @@ public class ActiveDeviceManager {
        IntentFilter filter = new IntentFilter();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        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_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED);
+3 −4
Original line number Original line Diff line number Diff line
@@ -155,6 +155,9 @@ public class HearingAidService extends ProfileService {
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mBondStateChangedReceiver = new BondStateChangedReceiver();
        mBondStateChangedReceiver = new BondStateChangedReceiver();
        registerReceiver(mBondStateChangedReceiver, filter);
        registerReceiver(mBondStateChangedReceiver, filter);
        filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);


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


    private void notifyActiveDeviceChanged() {
    private void notifyActiveDeviceChanged() {
        mAdapterService.getActiveDeviceManager().hearingAidActiveStateChanged(mActiveDevice);
        Intent intent = new Intent(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        Intent intent = new Intent(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mActiveDevice);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mActiveDevice);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
@@ -962,9 +964,6 @@ public class HearingAidService extends ProfileService {
                removeStateMachine(device);
                removeStateMachine(device);
            }
            }
        }
        }
        mAdapterService
                .getActiveDeviceManager()
                .hearingAidConnectionStateChanged(device, fromState, toState);
    }
    }


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


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


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


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


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


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


        mActiveDeviceManager.hearingAidActiveStateChanged(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);
    }
    }


    /**
    /**
+0 −3
Original line number Original line Diff line number Diff line
@@ -44,7 +44,6 @@ import androidx.test.rule.ServiceTestRule;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.runner.AndroidJUnit4;


import com.android.bluetooth.TestUtils;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.ActiveDeviceManager;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.x.com.android.modules.utils.SynchronousResultReceiver;
import com.android.bluetooth.x.com.android.modules.utils.SynchronousResultReceiver;
@@ -81,7 +80,6 @@ public class HearingAidServiceTest {
    private BroadcastReceiver mHearingAidIntentReceiver;
    private BroadcastReceiver mHearingAidIntentReceiver;


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


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