Merge "Fire off BluetoothConnectionCallback when we fire off the broadcasts for ACL connections and disconnections."
diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java
index d07909f..3b68755 100644
--- a/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/src/com/android/bluetooth/btservice/AdapterService.java
@@ -42,6 +42,7 @@
 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.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 @@
     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;
@@ -1929,6 +1933,30 @@
         }
 
         @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();
             if (service == null || !callerIsSystemOrActiveUser(TAG, "registerCallback")) {
@@ -2629,6 +2657,10 @@
         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(),
diff --git a/src/com/android/bluetooth/btservice/RemoteDevices.java b/src/com/android/bluetooth/btservice/RemoteDevices.java
index 1dcb599..ff38e73 100644
--- a/src/com/android/bluetooth/btservice/RemoteDevices.java
+++ b/src/com/android/bluetooth/btservice/RemoteDevices.java
@@ -22,6 +22,7 @@
 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.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 @@
                     | 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());