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

Commit 3b88ff80 authored by Rahul Sabnis's avatar Rahul Sabnis
Browse files

Fire off BluetoothConnectionCallback when we fire off the broadcasts for

ACL connections and disconnections.

Tag: #feature
Bug: 171902843
Test: Manual
Change-Id: I30a510dac361cdeb24f41b14aa5df6e95893f847
parent 6f569a88
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.bluetooth.BluetoothProtoEnums;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.IBluetooth;
import android.bluetooth.IBluetoothCallback;
import android.bluetooth.IBluetoothConnectionCallback;
import android.bluetooth.IBluetoothMetadataListener;
import android.bluetooth.IBluetoothSocketManager;
import android.bluetooth.OobData;
@@ -113,7 +114,9 @@ import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AdapterService extends Service {
    private static final String TAG = "BluetoothAdapterService";
@@ -206,6 +209,7 @@ public class AdapterService extends Service {
    private final HashMap<BluetoothDevice, ArrayList<IBluetoothMetadataListener>>
            mMetadataListeners = new HashMap<>();
    private final HashMap<String, Integer> mProfileServicesState = new HashMap<String, Integer>();
    private Set<IBluetoothConnectionCallback> mBluetoothConnectionCallbacks = new HashSet<>();
    //Only BluetoothManagerService should be registered
    private RemoteCallbackList<IBluetoothCallback> mCallbacks;
    private int mCurrentRequestId;
@@ -1924,6 +1928,30 @@ public class AdapterService extends Service {
            return service.factoryResetNative();
        }

        @Override
        public boolean registerBluetoothConnectionCallback(IBluetoothConnectionCallback callback) {
            AdapterService service = getService();
            if (service == null || !callerIsSystemOrActiveUser(TAG,
                    "registerBluetoothConnectionCallback")) {
                return false;
            }
            enforceBluetoothPrivilegedPermission(service);
            service.mBluetoothConnectionCallbacks.add(callback);
            return true;
        }

        @Override
        public boolean unregisterBluetoothConnectionCallback(
                IBluetoothConnectionCallback callback) {
            AdapterService service = getService();
            if (service == null || !callerIsSystemOrActiveUser(TAG,
                    "unregisterBluetoothConnectionCallback")) {
                return false;
            }
            enforceBluetoothPrivilegedPermission(service);
            return service.mBluetoothConnectionCallbacks.remove(callback);
        }

        @Override
        public void registerCallback(IBluetoothCallback callback) {
            AdapterService service = getService();
@@ -2625,6 +2653,10 @@ public class AdapterService extends Service {
        return deviceProp.getUuids();
    }

    public Set<IBluetoothConnectionCallback> getBluetoothConnectionCallbacks() {
        return mBluetoothConnectionCallbacks;
    }

    void logUserBondResponse(BluetoothDevice device, boolean accepted, int event) {
        BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_BOND_STATE_CHANGED,
                obfuscateAddress(device), 0, device.getType(),
+18 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.IBluetoothConnectionCallback;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -30,6 +31,7 @@ import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;

import com.android.bluetooth.BluetoothStatsLog;
@@ -666,6 +668,22 @@ final class RemoteDevices {
                    | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
            sAdapterService.sendBroadcast(intent, sAdapterService.BLUETOOTH_PERM);

            synchronized (sAdapterService.getBluetoothConnectionCallbacks()) {
                Set<IBluetoothConnectionCallback> bluetoothConnectionCallbacks =
                        sAdapterService.getBluetoothConnectionCallbacks();
                for (IBluetoothConnectionCallback callback : bluetoothConnectionCallbacks) {
                    try {
                        if (connectionState == BluetoothAdapter.STATE_CONNECTED) {
                            callback.onDeviceConnected(device);
                        } else {
                            callback.onDeviceDisconnected(device);
                        }
                    } catch (RemoteException ex) {
                        Log.e(TAG, "RemoteException in calling IBluetoothConnectionCallback");
                    }
                }
            }
        } else {
            Log.e(TAG, "aclStateChangeCallback intent is null. deviceBondState: "
                    + device.getBondState());