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

Commit a220ed0d authored by Sungsoo Lim's avatar Sungsoo Lim Committed by Gerrit Code Review
Browse files

Merge "Revert "Revert "Stop using intent from HearingAid profile to...

Merge "Revert "Revert "Stop using intent from HearingAid profile to ActiveDeviceManager""" into main
parents 137586b7 ca583018
Loading
Loading
Loading
Loading
+29 −17
Original line number Original line 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.
 * 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
 *    - 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
@@ -183,13 +182,6 @@ 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));
@@ -204,9 +196,6 @@ 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;
@@ -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 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
@@ -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 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
@@ -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) {
    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);
@@ -827,8 +841,6 @@ 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);
+6 −5
Original line number Original line Diff line number Diff line
@@ -155,9 +155,6 @@ 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);
@@ -733,6 +730,7 @@ 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
@@ -746,11 +744,11 @@ public class HearingAidService extends ProfileService {
        public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
        public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
            for (AudioDeviceInfo deviceInfo : removedDevices) {
            for (AudioDeviceInfo deviceInfo : removedDevices) {
                if (deviceInfo.getType() == AudioDeviceInfo.TYPE_HEARING_AID) {
                if (deviceInfo.getType() == AudioDeviceInfo.TYPE_HEARING_AID) {
                    notifyActiveDeviceChanged();
                    if (DBG) {
                    if (DBG) {
                        Log.d(TAG, " onAudioDevicesRemoved: device type: " + deviceInfo.getType());
                        Log.d(TAG, " onAudioDevicesRemoved: device type: " + deviceInfo.getType());
                    }
                    }
                    if (mAudioManager != null) {
                    if (mAudioManager != null) {
                        notifyActiveDeviceChanged();
                        mAudioManager.unregisterAudioDeviceCallback(this);
                        mAudioManager.unregisterAudioDeviceCallback(this);
                    } else {
                    } else {
                        Log.w(TAG, "onAudioDevicesRemoved: mAudioManager is null");
                        Log.w(TAG, "onAudioDevicesRemoved: mAudioManager is null");
@@ -766,11 +764,11 @@ public class HearingAidService extends ProfileService {
        public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
        public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
            for (AudioDeviceInfo deviceInfo : addedDevices) {
            for (AudioDeviceInfo deviceInfo : addedDevices) {
                if (deviceInfo.getType() == AudioDeviceInfo.TYPE_HEARING_AID) {
                if (deviceInfo.getType() == AudioDeviceInfo.TYPE_HEARING_AID) {
                    notifyActiveDeviceChanged();
                    if (DBG) {
                    if (DBG) {
                        Log.d(TAG, " onAudioDevicesAdded: device type: " + deviceInfo.getType());
                        Log.d(TAG, " onAudioDevicesAdded: device type: " + deviceInfo.getType());
                    }
                    }
                    if (mAudioManager != null) {
                    if (mAudioManager != null) {
                        notifyActiveDeviceChanged();
                        mAudioManager.unregisterAudioDeviceCallback(this);
                        mAudioManager.unregisterAudioDeviceCallback(this);
                    } else {
                    } else {
                        Log.w(TAG, "onAudioDevicesAdded: mAudioManager is null");
                        Log.w(TAG, "onAudioDevicesAdded: mAudioManager is null");
@@ -964,6 +962,9 @@ public class HearingAidService extends ProfileService {
                removeStateMachine(device);
                removeStateMachine(device);
            }
            }
        }
        }
        mAdapterService
                .getActiveDeviceManager()
                .hearingAidConnectionStateChanged(device, fromState, toState);
    }
    }


    /**
    /**
+8 −16
Original line number Original line Diff line number Diff line
@@ -33,7 +33,6 @@ 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;
@@ -1213,13 +1212,11 @@ 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;


        Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
        mActiveDeviceManager.hearingAidConnectionStateChanged(
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
                device, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_CONNECTED);
        mActiveDeviceManager.getBroadcastReceiver().onReceive(mContext, intent);
    }
    }


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


        Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
        mActiveDeviceManager.hearingAidConnectionStateChanged(
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
                device, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED);
        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) {
        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.remove(device);
        mDeviceConnectionStack.add(device);
        mDeviceConnectionStack.add(device);
        mMostRecentDevice = device;

        mActiveDeviceManager.hearingAidActiveStateChanged(device);
    }
    }


    /**
    /**
+3 −0
Original line number Original line Diff line number Diff line
@@ -44,6 +44,7 @@ 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;
@@ -80,6 +81,7 @@ 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;
@@ -97,6 +99,7 @@ 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());