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

Commit df6f1e81 authored by Evan Chen's avatar Evan Chen
Browse files

Add logs and lock for BT onDeviceConnected and onDeviceDisconnected

events

Test: CTS
Bug: 422776277
Flag: EXEMPT bugfix
Change-Id: I2e4ea186270170ea5266b7dc23e7f0bea453aa9d
parent 1a065ef5
Loading
Loading
Loading
Loading
+25 −7
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@ import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.ParcelUuid;
import android.os.UserHandle;
import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.ArrayUtils;
import com.android.server.companion.association.AssociationStore;

@@ -61,6 +63,7 @@ public class BluetoothDeviceProcessor
    private final Callback mCallback;

    /** A set of ALL connected BT device (not only companion.) */
    @GuardedBy("mAllConnectedDevices")
    @NonNull
    private final Map<MacAddress, BluetoothDevice> mAllConnectedDevices = new HashMap<>();

@@ -85,9 +88,14 @@ public class BluetoothDeviceProcessor
    public void onDeviceConnected(@NonNull BluetoothDevice device) {
        final MacAddress macAddress = MacAddress.fromString(device.getAddress());

        synchronized (mAllConnectedDevices) {
            if (mAllConnectedDevices.put(macAddress, device) != null) {
                Slog.i(TAG, "Device " + device.getAddress()
                        + " already marked as connected. Skipping duplicate event.");
                return;
            }
        }


        onDeviceConnectivityChanged(device, true);
    }
@@ -102,9 +110,13 @@ public class BluetoothDeviceProcessor
            int reason) {
        final MacAddress macAddress = MacAddress.fromString(device.getAddress());

        synchronized (mAllConnectedDevices) {
            if (mAllConnectedDevices.remove(macAddress) == null) {
                Slog.i(TAG, "Device " + device.getAddress()
                        + " not found as connected. Skipping disconnect event");
                return;
            }
        }

        onDeviceConnectivityChanged(device, false);
    }
@@ -140,9 +152,15 @@ public class BluetoothDeviceProcessor

    @Override
    public void onAssociationAdded(AssociationInfo association) {
        synchronized (mAllConnectedDevices) {
            if (mAllConnectedDevices.containsKey(association.getDeviceMacAddress())) {
                mCallback.onBluetoothCompanionDeviceConnected(
                        association.getId(), association.getUserId());
            } else {
                Slog.i(TAG, "onAssociationAdded: Device " + association.getDeviceMacAddress()
                        + " is not currently connected.");
            }
        }
    }

}