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

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

Stop using BluetoothA2dp and BluetoothHeadset Broadcasts for...

Stop using BluetoothA2dp and BluetoothHeadset Broadcasts for CONNECTION_STATE_CHANGED in SilenceDeviceManager.

Bug: 296932947
Test: atest BluetoothInstrumentationTests
Tag: #refactor
Change-Id: I590175049320ceb943586adc08b24b0f2bb69428
parent 0a03e9b8
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -1279,6 +1279,9 @@ public class A2dpService extends ProfileService {
        mAdapterService
                .getActiveDeviceManager()
                .a2dpConnectionStateChanged(device, fromState, toState);
        mAdapterService
                .getSilenceDeviceManager()
                .a2dpConnectionStateChanged(device, fromState, toState);
    }

    /**
+43 −66
Original line number Diff line number Diff line
@@ -19,14 +19,9 @@ package com.android.bluetooth.btservice;
import static android.Manifest.permission.BLUETOOTH_CONNECT;

import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
@@ -81,30 +76,17 @@ public class SilenceDeviceManager {
    private static final int ENABLE_SILENCE = 0;
    private static final int DISABLE_SILENCE = 1;

    // Broadcast receiver for all changes
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action == null) {
                Log.e(TAG, "Received intent with null action");
                return;
            }
            switch (action) {
                case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MSG_A2DP_CONNECTION_STATE_CHANGED,
                                           intent).sendToTarget();
                    break;
                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MSG_HFP_CONNECTION_STATE_CHANGED,
                                           intent).sendToTarget();
                    break;
                default:
                    Log.e(TAG, "Received unexpected intent, action=" + action);
                    break;
            }
    /**
     * Called when A2DP connection state changed by A2dpService
     *
     * @param device The device of which connection state was changed
     * @param fromState The previous connection state of the device
     * @param toState The new connection state of the device
     */
    public void a2dpConnectionStateChanged(BluetoothDevice device, int fromState, int toState) {
        mHandler.obtainMessage(MSG_A2DP_CONNECTION_STATE_CHANGED, fromState, toState, device)
                .sendToTarget();
    }
    };

    /**
     * Called when A2DP active device changed by A2dpService
@@ -115,6 +97,18 @@ public class SilenceDeviceManager {
        mHandler.obtainMessage(MSG_A2DP_ACTIVE_DEVICE_CHANGED, device).sendToTarget();
    }

    /**
     * Called when HFP connection state changed by HeadsetService
     *
     * @param device The device of which connection state was changed
     * @param fromState The previous connection state of the device
     * @param toState The new connection state of the device
     */
    public void hfpConnectionStateChanged(BluetoothDevice device, int fromState, int toState) {
        mHandler.obtainMessage(MSG_HFP_CONNECTION_STATE_CHANGED, fromState, toState, device)
                .sendToTarget();
    }

    /**
     * Called when HFP active device is changed by HeadsetService
     *
@@ -142,12 +136,10 @@ public class SilenceDeviceManager {
                }
                break;

                case MSG_A2DP_CONNECTION_STATE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
                    int nextState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
                case MSG_A2DP_CONNECTION_STATE_CHANGED:
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    int prevState = msg.arg1;
                    int nextState = msg.arg2;

                    if (nextState == BluetoothProfile.STATE_CONNECTED) {
                        // enter connected state
@@ -163,29 +155,25 @@ public class SilenceDeviceManager {
                            mSilenceDevices.remove(device);
                        }
                    }
                }
                    break;

                case MSG_HFP_CONNECTION_STATE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
                    int nextState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
                case MSG_HFP_CONNECTION_STATE_CHANGED:
                    BluetoothDevice bluetoothDevice = (BluetoothDevice) msg.obj;
                    int prev = msg.arg1;
                    int next = msg.arg2;

                    if (nextState == BluetoothProfile.STATE_CONNECTED) {
                    if (next == BluetoothProfile.STATE_CONNECTED) {
                        // enter connected state
                        addConnectedDevice(device, BluetoothProfile.HEADSET);
                        if (!mSilenceDevices.containsKey(device)) {
                            mSilenceDevices.put(device, false);
                        addConnectedDevice(bluetoothDevice, BluetoothProfile.HEADSET);
                        if (!mSilenceDevices.containsKey(bluetoothDevice)) {
                            mSilenceDevices.put(bluetoothDevice, false);
                        }
                    } else if (prevState == BluetoothProfile.STATE_CONNECTED) {
                    } else if (prev == BluetoothProfile.STATE_CONNECTED) {
                        // exiting from connected state
                        removeConnectedDevice(device, BluetoothProfile.HEADSET);
                        if (!isBluetoothAudioConnected(device)) {
                            handleSilenceDeviceStateChanged(device, false);
                            mSilenceDevices.remove(device);
                        }
                        removeConnectedDevice(bluetoothDevice, BluetoothProfile.HEADSET);
                        if (!isBluetoothAudioConnected(bluetoothDevice)) {
                            handleSilenceDeviceStateChanged(bluetoothDevice, false);
                            mSilenceDevices.remove(bluetoothDevice);
                        }
                    }
                    break;
@@ -211,7 +199,7 @@ public class SilenceDeviceManager {
                    break;
            }
        }
    };
    }

    SilenceDeviceManager(AdapterService service, ServiceFactory factory, Looper looper) {
        mAdapterService = service;
@@ -224,11 +212,6 @@ public class SilenceDeviceManager {
            Log.v(TAG, "start()");
        }
        mHandler = new SilenceDeviceManagerHandler(mLooper);
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        mAdapterService.registerReceiver(mReceiver, filter);
    }

    void cleanup() {
@@ -236,7 +219,6 @@ public class SilenceDeviceManager {
            Log.v(TAG, "cleanup()");
        }
        mSilenceDevices.clear();
        mAdapterService.unregisterReceiver(mReceiver);
    }

    @VisibleForTesting
@@ -348,9 +330,4 @@ public class SilenceDeviceManager {
            writer.println("  " + device+ "  | " + getSilenceMode(device));
        }
    }

    @VisibleForTesting
    BroadcastReceiver getBroadcastReceiver() {
        return mReceiver;
    }
}
+7 −2
Original line number Diff line number Diff line
@@ -2008,6 +2008,9 @@ public class HeadsetService extends ProfileService {
            }
        }
        mActiveDeviceManager.hfpConnectionStateChanged(device, fromState, toState);
        mAdapterService
                .getSilenceDeviceManager()
                .hfpConnectionStateChanged(device, fromState, toState);
    }

    /**
@@ -2137,8 +2140,10 @@ public class HeadsetService extends ProfileService {
        mAdapterService.getActiveDeviceManager().hfpActiveStateChanged(device);
        mAdapterService.getSilenceDeviceManager().hfpActiveDeviceChanged(device);

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

        Intent intent = new Intent(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
+9 −23
Original line number Diff line number Diff line
@@ -17,12 +17,11 @@
package com.android.bluetooth.btservice;

import static android.Manifest.permission.BLUETOOTH_CONNECT;

import static org.mockito.Mockito.*;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
@@ -172,11 +171,8 @@ public class SilenceDeviceManagerTest {
     * Helper to indicate A2dp connected for a device.
     */
    private void a2dpConnected(BluetoothDevice device) {
        Intent intent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_CONNECTED);
        mSilenceDeviceManager.getBroadcastReceiver().onReceive(mContext, intent);
        mSilenceDeviceManager.a2dpConnectionStateChanged(
                device, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED);
        TestUtils.waitForLooperToFinishScheduledTask(mLooper);
    }

@@ -184,11 +180,8 @@ public class SilenceDeviceManagerTest {
     * Helper to indicate A2dp disconnected for a device.
     */
    private void a2dpDisconnected(BluetoothDevice device) {
        Intent intent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTED);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED);
        mSilenceDeviceManager.getBroadcastReceiver().onReceive(mContext, intent);
        mSilenceDeviceManager.a2dpConnectionStateChanged(
                device, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED);
        TestUtils.waitForLooperToFinishScheduledTask(mLooper);
    }

@@ -196,11 +189,8 @@ public class SilenceDeviceManagerTest {
     * Helper to indicate Headset connected for a device.
     */
    private void headsetConnected(BluetoothDevice device) {
        Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_CONNECTED);
        mSilenceDeviceManager.getBroadcastReceiver().onReceive(mContext, intent);
        mSilenceDeviceManager.hfpConnectionStateChanged(
                device, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED);
        TestUtils.waitForLooperToFinishScheduledTask(mLooper);
    }

@@ -208,12 +198,8 @@ public class SilenceDeviceManagerTest {
     * Helper to indicate Headset disconnected for a device.
     */
    private void headsetDisconnected(BluetoothDevice device) {
        Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTED);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED);
        mSilenceDeviceManager.getBroadcastReceiver().onReceive(mContext, intent);
        mSilenceDeviceManager.hfpConnectionStateChanged(
                device, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED);
        TestUtils.waitForLooperToFinishScheduledTask(mLooper);
    }
}
+10 −0
Original line number Diff line number Diff line
@@ -1185,6 +1185,11 @@ public class HeadsetServiceAndStateMachineTest {
                        device,
                        BluetoothProfile.STATE_DISCONNECTED,
                        BluetoothProfile.STATE_CONNECTING);
        verify(mSilenceDeviceManager, timeout(STATE_CHANGE_TIMEOUT_MILLIS))
                .hfpConnectionStateChanged(
                        device,
                        BluetoothProfile.STATE_DISCONNECTED,
                        BluetoothProfile.STATE_CONNECTING);
        Assert.assertEquals(BluetoothProfile.STATE_CONNECTING,
                mHeadsetService.getConnectionState(device));
        Assert.assertEquals(Collections.singletonList(device),
@@ -1200,6 +1205,11 @@ public class HeadsetServiceAndStateMachineTest {
                        device,
                        BluetoothProfile.STATE_CONNECTING,
                        BluetoothProfile.STATE_CONNECTED);
        verify(mSilenceDeviceManager, timeout(STATE_CHANGE_TIMEOUT_MILLIS))
                .hfpConnectionStateChanged(
                        device,
                        BluetoothProfile.STATE_CONNECTING,
                        BluetoothProfile.STATE_CONNECTED);
        Assert.assertEquals(BluetoothProfile.STATE_CONNECTED,
                mHeadsetService.getConnectionState(device));
    }