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

Commit d4970c19 authored by Hall Liu's avatar Hall Liu
Browse files

Notify BluetoothRouteManager when Bluetooth turned off

When BluetoothDeviceManager gets notified that the
BluetoothHeadsetService has disconnected, notify BluetoothRouteManager
that all devices have disconnected.

Change-Id: Ie7f4e7398602f90e6669c8b58d085f84354844ca
Test: manual, added unit test
Fix: 34547045
parent e0a6ed95
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -29,8 +29,13 @@ import com.android.server.telecom.BluetoothAdapterProxy;
import com.android.server.telecom.BluetoothHeadsetProxy;
import com.android.server.telecom.TelecomSystem;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class BluetoothDeviceManager {
    private final BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
@@ -60,7 +65,14 @@ public class BluetoothDeviceManager {
                    try {
                        synchronized (mLock) {
                            mBluetoothHeadsetService = null;
                            Log.i(BluetoothDeviceManager.this, "Lost BluetoothHeadset service.");
                            Log.i(BluetoothDeviceManager.this, "Lost BluetoothHeadset service. " +
                                    "Removing all tracked devices.");
                            List<BluetoothDevice> devicesToRemove = new LinkedList<>(
                                    mConnectedDevicesByAddress.values());
                            mConnectedDevicesByAddress.clear();
                            for (BluetoothDevice device : devicesToRemove) {
                                mBluetoothRouteManager.onDeviceLost(device);
                            }
                        }
                    } finally {
                        Log.endSession();
+14 −0
Original line number Diff line number Diff line
@@ -131,6 +131,20 @@ public class BluetoothDeviceManagerTest extends TelecomTestCase {
                mBluetoothDeviceManager.getMostRecentlyConnectedDevice(device3.getAddress()));
    }

    @SmallTest
    public void testHeadsetServiceDisconnect() {
        receiverUnderTest.onReceive(mContext,
                buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device1));
        receiverUnderTest.onReceive(mContext,
                buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device2));
        serviceListenerUnderTest.onServiceDisconnected(0);

        verify(mRouteManager).onDeviceLost(device1);
        verify(mRouteManager).onDeviceLost(device2);
        assertNull(mBluetoothDeviceManager.getHeadsetService());
        assertEquals(0, mBluetoothDeviceManager.getNumConnectedDevices());
    }

    private Intent buildConnectionActionIntent(int state, BluetoothDevice device) {
        Intent i = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        i.putExtra(BluetoothHeadset.EXTRA_STATE, state);