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

Commit b96d294b authored by Aritra Sen's avatar Aritra Sen
Browse files

Stop using A2DP active device changed for SilenceDeviceManager.

Bug: 296932947
Test: atest BluetoothInstrumentationTests
Tag: #refactor
Change-Id: I5d701c9b29acea5423efa20f5cad63dd901fc5e3
parent 55522c4d
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -1075,14 +1075,17 @@ public class A2dpService extends ProfileService {
            mActiveDevice = device;
        }

        mAdapterService.getActiveDeviceManager().a2dpActiveStateChanged(device);
        mAdapterService.getSilenceDeviceManager().a2dpActiveDeviceChanged(device);

        BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED,
                BluetoothProfile.A2DP, mAdapterService.obfuscateAddress(device),
                mAdapterService.getMetricId(device));

        Intent intent = new Intent(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
        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());
    }
+4 −0
Original line number Diff line number Diff line
@@ -758,6 +758,10 @@ public class AdapterService extends Service {
        return mActiveDeviceManager;
    }

    public SilenceDeviceManager getSilenceDeviceManager() {
        return mSilenceDeviceManager;
    }

    private boolean initMetricsLogger() {
        if (mMetricsLogger != null) {
            return false;
+13 −12
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ public class SilenceDeviceManager {
    private static final int MSG_SILENCE_DEVICE_STATE_CHANGED = 1;
    private static final int MSG_A2DP_CONNECTION_STATE_CHANGED = 10;
    private static final int MSG_HFP_CONNECTION_STATE_CHANGED = 11;
    private static final int MSG_A2DP_ACTIVE_DEIVCE_CHANGED = 20;
    private static final int MSG_A2DP_ACTIVE_DEVICE_CHANGED = 20;
    private static final int MSG_HFP_ACTIVE_DEVICE_CHANGED = 21;
    private static final int ENABLE_SILENCE = 0;
    private static final int DISABLE_SILENCE = 1;
@@ -99,10 +99,6 @@ public class SilenceDeviceManager {
                    mHandler.obtainMessage(MSG_HFP_CONNECTION_STATE_CHANGED,
                                           intent).sendToTarget();
                    break;
                case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MSG_A2DP_ACTIVE_DEIVCE_CHANGED,
                                           intent).sendToTarget();
                    break;
                case BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MSG_HFP_ACTIVE_DEVICE_CHANGED,
                        intent).sendToTarget();
@@ -114,6 +110,15 @@ public class SilenceDeviceManager {
        }
    };

    /**
     * Called when A2DP active device changed by A2dpService
     *
     * @param device The device currently activated. {@code null} if no A2DP device activated
     */
    public void a2dpActiveDeviceChanged(BluetoothDevice device) {
        mHandler.obtainMessage(MSG_A2DP_ACTIVE_DEVICE_CHANGED, device).sendToTarget();
    }

    class SilenceDeviceManagerHandler extends Handler {
        SilenceDeviceManagerHandler(Looper looper) {
            super(looper);
@@ -180,15 +185,12 @@ public class SilenceDeviceManager {
                }
                break;

                case MSG_A2DP_ACTIVE_DEIVCE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice a2dpActiveDevice =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                case MSG_A2DP_ACTIVE_DEVICE_CHANGED:
                    BluetoothDevice a2dpActiveDevice = (BluetoothDevice) msg.obj;
                    if (getSilenceMode(a2dpActiveDevice)) {
                        // Resume the device from silence mode.
                        setSilenceMode(a2dpActiveDevice, false);
                    }
                }
                    break;

                case MSG_HFP_ACTIVE_DEVICE_CHANGED: {
@@ -224,7 +226,6 @@ public class SilenceDeviceManager {
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        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);
        mAdapterService.registerReceiver(mReceiver, filter);
+3 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ 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.SilenceDeviceManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;

import org.junit.After;
@@ -79,6 +80,7 @@ public class A2dpServiceTest {
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private A2dpNativeInterface mMockNativeInterface;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private SilenceDeviceManager mSilenceDeviceManager;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

@@ -99,6 +101,7 @@ public class A2dpServiceTest {
        doReturn(false).when(mAdapterService).isQuietModeEnabled();
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager();
        doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager();

        mAdapter = BluetoothAdapter.getDefaultAdapter();

+3 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import androidx.test.runner.AndroidJUnit4;

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

import org.hamcrest.core.IsInstanceOf;
@@ -65,6 +66,7 @@ public class A2dpStateMachineTest {

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

@@ -74,6 +76,7 @@ public class A2dpStateMachineTest {
        // Set up mocks and test assets
        MockitoAnnotations.initMocks(this);
        doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager();
        doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager();

        TestUtils.setAdapterService(mAdapterService);