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

Commit ef84c714 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Synchronize connected devices in BluetoothController" into sc-dev

parents 2ee596d2 236e647a
Loading
Loading
Loading
Loading
+19 −7
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.util.Log;

import androidx.annotation.NonNull;

import com.android.internal.annotations.GuardedBy;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -66,6 +67,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
    private final WeakHashMap<CachedBluetoothDevice, ActuallyCachedState> mCachedState =
            new WeakHashMap<>();
    private final Handler mBgHandler;
    @GuardedBy("mConnectedDevices")
    private final List<CachedBluetoothDevice> mConnectedDevices = new ArrayList<>();

    private boolean mEnabled;
@@ -118,7 +120,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
        pw.print("  mConnectionState="); pw.println(stateToString(mConnectionState));
        pw.print("  mAudioProfileOnly="); pw.println(mAudioProfileOnly);
        pw.print("  mIsActive="); pw.println(mIsActive);
        pw.print("  mConnectedDevices="); pw.println(mConnectedDevices);
        pw.print("  mConnectedDevices="); pw.println(getConnectedDevices());
        pw.print("  mCallbacks.size="); pw.println(mHandler.mCallbacks.size());
        pw.println("  Bluetooth Devices:");
        for (CachedBluetoothDevice device : getDevices()) {
@@ -151,7 +153,11 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa

    @Override
    public List<CachedBluetoothDevice> getConnectedDevices() {
        return mConnectedDevices;
        List<CachedBluetoothDevice> out;
        synchronized (mConnectedDevices) {
            out = new ArrayList<>(mConnectedDevices);
        }
        return out;
    }

    @Override
@@ -226,9 +232,11 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa

    @Override
    public String getConnectedDeviceName() {
        synchronized (mConnectedDevices) {
            if (mConnectedDevices.size() == 1) {
                return mConnectedDevices.get(0).getName();
            }
        }
        return null;
    }

@@ -242,7 +250,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
    private void updateConnected() {
        // Make sure our connection state is up to date.
        int state = mLocalBluetoothManager.getBluetoothAdapter().getConnectionState();
        mConnectedDevices.clear();
        List<CachedBluetoothDevice> newList = new ArrayList<>();
        // If any of the devices are in a higher state than the adapter, move the adapter into
        // that state.
        for (CachedBluetoothDevice device : getDevices()) {
@@ -251,15 +259,19 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
                state = maxDeviceState;
            }
            if (device.isConnected()) {
                mConnectedDevices.add(device);
                newList.add(device);
            }
        }

        if (mConnectedDevices.isEmpty() && state == BluetoothAdapter.STATE_CONNECTED) {
        if (newList.isEmpty() && state == BluetoothAdapter.STATE_CONNECTED) {
            // If somehow we think we are connected, but have no connected devices, we aren't
            // connected.
            state = BluetoothAdapter.STATE_DISCONNECTED;
        }
        synchronized (mConnectedDevices) {
            mConnectedDevices.clear();
            mConnectedDevices.addAll(newList);
        }
        if (state != mConnectionState) {
            mConnectionState = state;
            mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);