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

Commit f530f252 authored by Evan Chen's avatar Evan Chen Committed by Android (Google) Code Review
Browse files

Merge "Add logs and lock for BT onDeviceConnected and onDeviceDisconnected events" into main

parents 7ddad9cc df6f1e81
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.");
            }
        }
    }

}