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

Commit 59ee0723 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Stop using intent from A2DP profile to ActiveDeviceManager

Bug: 285981195
Bug: 285959170
Test: atest BluetoothInstrumentationTests
Change-Id: I21fcb10d35802cb1b19f4925d63822bac54cbd8e
parent 06884523
Loading
Loading
Loading
Loading
+4 −0
Original line number Original line Diff line number Diff line
@@ -1071,6 +1071,7 @@ public class A2dpService extends ProfileService {
        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
                        | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
                        | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mAdapterService.getActiveDeviceManager().a2dpActiveStateChanged(device);
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
                Utils.getTempAllowlistBroadcastOptions());
    }
    }
@@ -1258,6 +1259,9 @@ public class A2dpService extends ProfileService {
                removeStateMachine(device);
                removeStateMachine(device);
            }
            }
        }
        }
        mAdapterService
                .getActiveDeviceManager()
                .a2dpConnectionStateChanged(device, fromState, toState);
    }
    }


    /**
    /**
+25 −15
Original line number Original line Diff line number Diff line
@@ -20,7 +20,6 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
@@ -73,7 +72,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:
 *    - BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED for A2DP
 *    - BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED for HFP
 *    - BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED for HFP
 *    - BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED for HearingAid
 *    - 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
@@ -114,7 +112,7 @@ import java.util.Set;
 *      will have no impact. E.g., music will continue streaming over the
 *      will have no impact. E.g., music will continue streaming over the
 *      active Bluetooth device.
 *      active Bluetooth device.
 */
 */
class ActiveDeviceManager {
public class ActiveDeviceManager {
    private static final String TAG = "ActiveDeviceManager";
    private static final String TAG = "ActiveDeviceManager";
    private static final boolean DBG = true;
    private static final boolean DBG = true;
    @VisibleForTesting
    @VisibleForTesting
@@ -187,13 +185,6 @@ class ActiveDeviceManager {
            }
            }


            switch (action) {
            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:
                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                    if (currentState == BluetoothProfile.STATE_CONNECTED) {
                    if (currentState == BluetoothProfile.STATE_CONNECTED) {
                        mHandler.post(() -> handleHfpConnected(device));
                        mHandler.post(() -> handleHfpConnected(device));
@@ -222,9 +213,6 @@ class ActiveDeviceManager {
                        mHandler.post(() -> handleHapDisconnected(device));
                        mHandler.post(() -> handleHapDisconnected(device));
                    }
                    }
                    break;
                    break;
                case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.post(() -> handleA2dpActiveDeviceChanged(device));
                    break;
                case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED:
                case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.post(() -> handleHfpActiveDeviceChanged(device));
                    mHandler.post(() -> handleHfpActiveDeviceChanged(device));
                    break;
                    break;
@@ -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) {
    private void handleAdapterStateChanged(int currentState) {
        if (DBG) {
        if (DBG) {
            Log.d(TAG, "handleAdapterStateChanged: currentState=" + currentState);
            Log.d(TAG, "handleAdapterStateChanged: currentState=" + currentState);
@@ -803,8 +815,6 @@ 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(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
+4 −0
Original line number Original line Diff line number Diff line
@@ -736,6 +736,10 @@ public class AdapterService extends Service {
        }
        }
    }
    }


    public ActiveDeviceManager getActiveDeviceManager() {
        return mActiveDeviceManager;
    }

    private boolean initMetricsLogger() {
    private boolean initMetricsLogger() {
        if (mMetricsLogger != null) {
        if (mMetricsLogger != null) {
            return false;
            return false;
+3 −0
Original line number Original line Diff line number Diff line
@@ -40,6 +40,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;


@@ -76,6 +77,7 @@ public class A2dpServiceTest {
    private final BlockingQueue<Intent> mCodecConfigChangedQueue = new LinkedBlockingQueue<>();
    private final BlockingQueue<Intent> mCodecConfigChangedQueue = new LinkedBlockingQueue<>();


    @Mock private AdapterService mAdapterService;
    @Mock private AdapterService mAdapterService;
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private A2dpNativeInterface mA2dpNativeInterface;
    @Mock private A2dpNativeInterface mA2dpNativeInterface;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private DatabaseManager mDatabaseManager;


@@ -97,6 +99,7 @@ public class A2dpServiceTest {
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
        doReturn(false).when(mAdapterService).isQuietModeEnabled();
        doReturn(false).when(mAdapterService).isQuietModeEnabled();
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager();


        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAdapter = BluetoothAdapter.getDefaultAdapter();


+4 −2
Original line number Original line Diff line number Diff line
@@ -33,14 +33,13 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.runner.AndroidJUnit4;


import com.android.bluetooth.R;
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 org.hamcrest.core.IsInstanceOf;
import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
@@ -65,6 +64,7 @@ public class A2dpStateMachineTest {
    private BluetoothCodecConfig mCodecConfigOpus;
    private BluetoothCodecConfig mCodecConfigOpus;


    @Mock private AdapterService mAdapterService;
    @Mock private AdapterService mAdapterService;
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private A2dpService mA2dpService;
    @Mock private A2dpService mA2dpService;
    @Mock private A2dpNativeInterface mA2dpNativeInterface;
    @Mock private A2dpNativeInterface mA2dpNativeInterface;


@@ -73,6 +73,8 @@ public class A2dpStateMachineTest {
        mTargetContext = InstrumentationRegistry.getTargetContext();
        mTargetContext = InstrumentationRegistry.getTargetContext();
        // Set up mocks and test assets
        // Set up mocks and test assets
        MockitoAnnotations.initMocks(this);
        MockitoAnnotations.initMocks(this);
        doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager();

        TestUtils.setAdapterService(mAdapterService);
        TestUtils.setAdapterService(mAdapterService);


        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAdapter = BluetoothAdapter.getDefaultAdapter();
Loading