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

Commit 5baf6747 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Stop using intent from A2DP profile to ActiveDeviceManager" into main

parents 523e8471 59ee0723
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -1071,6 +1071,7 @@ public class A2dpService extends ProfileService {
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                        | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mAdapterService.getActiveDeviceManager().a2dpActiveStateChanged(device);
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
    }
@@ -1258,6 +1259,9 @@ public class A2dpService extends ProfileService {
                removeStateMachine(device);
            }
        }
        mAdapterService
                .getActiveDeviceManager()
                .a2dpConnectionStateChanged(device, fromState, toState);
    }

    /**
+25 −15
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
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.
 * 4) The Active Device Manager always listens for ACTION_ACTIVE_DEVICE_CHANGED
 *    broadcasts for each profile:
 *    - BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED for A2DP
 *    - BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED for HFP
 *    - BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED for HearingAid
 *    - 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
 *      active Bluetooth device.
 */
class ActiveDeviceManager {
public class ActiveDeviceManager {
    private static final String TAG = "ActiveDeviceManager";
    private static final boolean DBG = true;
    @VisibleForTesting
@@ -187,13 +185,6 @@ class ActiveDeviceManager {
            }

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

    public ActiveDeviceManager getActiveDeviceManager() {
        return mActiveDeviceManager;
    }

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

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

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

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

        mAdapter = BluetoothAdapter.getDefaultAdapter();

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

import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.ActiveDeviceManager;
import com.android.bluetooth.btservice.AdapterService;

import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -65,6 +64,7 @@ public class A2dpStateMachineTest {
    private BluetoothCodecConfig mCodecConfigOpus;

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

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

        TestUtils.setAdapterService(mAdapterService);

        mAdapter = BluetoothAdapter.getDefaultAdapter();
Loading