Merge acea1da0c39a97a8cc4c810462df55c0d7975fd8 on remote branch

Change-Id: I996078e81e59df967fb2cc0bb652585e2aae2de6
diff --git a/src/com/android/bluetooth/btservice/AdapterProperties.java b/src/com/android/bluetooth/btservice/AdapterProperties.java
index c7eb53c..beff7c8 100644
--- a/src/com/android/bluetooth/btservice/AdapterProperties.java
+++ b/src/com/android/bluetooth/btservice/AdapterProperties.java
@@ -86,18 +86,7 @@
     private CopyOnWriteArrayList<BluetoothDevice> mBondedDevices =
             new CopyOnWriteArrayList<BluetoothDevice>();
 
-    private final class ProfilesConnectionState {
-        public int mProfilesConnecting, mProfilesConnected, mProfilesDisconnecting;
-
-        ProfilesConnectionState(int connecting, int connected, int disconnecting) {
-          mProfilesConnecting    = connecting;
-          mProfilesConnected     = connected;
-          mProfilesDisconnecting = disconnecting;
-        }
-    }
-    private final HashMap<BluetoothDevice, ProfilesConnectionState> mDevicesConnectionState =
-            new HashMap<>();
-
+    private int mProfilesConnecting, mProfilesConnected, mProfilesDisconnecting;
     private final HashMap<Integer, Pair<Integer, Integer>> mProfileConnectionState =
             new HashMap<>();
 
@@ -229,7 +218,6 @@
 
     public void init(RemoteDevices remoteDevices) {
         mProfileConnectionState.clear();
-        mDevicesConnectionState.clear();
         mRemoteDevices = remoteDevices;
 
         // Get default max connected audio devices from config.xml in frameworks/base/core
@@ -286,7 +274,6 @@
         }
         mService = null;
         mBondedDevices.clear();
-        mDevicesConnectionState.clear();
     }
 
     @Override
@@ -800,10 +787,6 @@
                     debugLog("Adding bonded device:" + device);
                     mBondedDevices.add(device);
                 }
-                if (!mDevicesConnectionState.containsKey(device)) {
-                    debugLog("Adding connection state:" + device);
-                    mDevicesConnectionState.put(device, new ProfilesConnectionState(0, 0, 0));
-                }
             } else if (state == BluetoothDevice.BOND_NONE) {
                 // remove device from list
                 if (mBondedDevices.remove(device)) {
@@ -811,10 +794,6 @@
                 } else {
                     debugLog("Failed to remove device: " + device);
                 }
-                if (mDevicesConnectionState.containsKey(device)) {
-                    debugLog("Removing connection state:" + device);
-                    mDevicesConnectionState.remove(device);
-                }
             }
         } catch (Exception ee) {
             Log.w(TAG, "onBondStateChanged: Exception ", ee);
@@ -889,7 +868,7 @@
 
             try {
                 validateConnectionState =
-                   updateCountersAndCheckForConnectionStateChange(device, state, prevState);
+                   updateCountersAndCheckForConnectionStateChange(state, prevState);
             } catch (IllegalStateException ee) {
                 Log.w(TAG, "ADAPTER_CONNECTION_STATE_CHANGE: unexpected transition for profile="
                         + profile + ", " + prevState + " -> " + state);
@@ -953,45 +932,33 @@
         }
     }
 
-    private boolean updateCountersAndCheckForConnectionStateChange(BluetoothDevice device,
-            int state, int prevState) {
-        if(!mDevicesConnectionState.containsKey(device)) {
-            Log.e(TAG, "Can't find device connection record, adding new one: " + device);
-            mDevicesConnectionState.put(device, new ProfilesConnectionState(0, 0, 0));
-        }
-        ProfilesConnectionState connstate = mDevicesConnectionState.get(device);
-
-        Log.e(TAG, "prevState=" + prevState + " -> State=" + state +
-            " mProfilesConnecting=" + connstate.mProfilesConnecting +
-            " mProfilesConnected=" + connstate.mProfilesConnected +
-            " mProfilesDisconnecting=" + connstate.mProfilesDisconnecting);
-
+    private boolean updateCountersAndCheckForConnectionStateChange(int state, int prevState) {
         switch (prevState) {
             case BluetoothProfile.STATE_CONNECTING:
-                if (connstate.mProfilesConnecting > 0) {
-                    connstate.mProfilesConnecting--;
+                if (mProfilesConnecting > 0) {
+                    mProfilesConnecting--;
                 } else {
-                    Log.e(TAG, "mProfilesConnecting " + connstate.mProfilesConnecting);
+                    Log.e(TAG, "mProfilesConnecting " + mProfilesConnecting);
                     throw new IllegalStateException(
                             "Invalid state transition, " + prevState + " -> " + state);
                 }
                 break;
 
             case BluetoothProfile.STATE_CONNECTED:
-                if (connstate.mProfilesConnected > 0) {
-                    connstate.mProfilesConnected--;
+                if (mProfilesConnected > 0) {
+                    mProfilesConnected--;
                 } else {
-                    Log.e(TAG, "mProfilesConnected " + connstate.mProfilesConnected);
+                    Log.e(TAG, "mProfilesConnected " + mProfilesConnected);
                     throw new IllegalStateException(
                             "Invalid state transition, " + prevState + " -> " + state);
                 }
                 break;
 
             case BluetoothProfile.STATE_DISCONNECTING:
-                if (connstate.mProfilesDisconnecting > 0) {
-                    connstate.mProfilesDisconnecting--;
+                if (mProfilesDisconnecting > 0) {
+                    mProfilesDisconnecting--;
                 } else {
-                    Log.e(TAG, "mProfilesDisconnecting " + connstate.mProfilesDisconnecting);
+                    Log.e(TAG, "mProfilesDisconnecting " + mProfilesDisconnecting);
                     throw new IllegalStateException(
                             "Invalid state transition, " + prevState + " -> " + state);
                 }
@@ -1000,19 +967,19 @@
 
         switch (state) {
             case BluetoothProfile.STATE_CONNECTING:
-                connstate.mProfilesConnecting++;
-                return (connstate.mProfilesConnected == 0 && connstate.mProfilesConnecting == 1);
+                mProfilesConnecting++;
+                return (mProfilesConnected == 0 && mProfilesConnecting == 1);
 
             case BluetoothProfile.STATE_CONNECTED:
-                connstate.mProfilesConnected++;
-                return (connstate.mProfilesConnected == 1);
+                mProfilesConnected++;
+                return (mProfilesConnected == 1);
 
             case BluetoothProfile.STATE_DISCONNECTING:
-                connstate.mProfilesDisconnecting++;
-                return (connstate.mProfilesConnected == 0 && connstate.mProfilesDisconnecting == 1);
+                mProfilesDisconnecting++;
+                return (mProfilesConnected == 0 && mProfilesDisconnecting == 1);
 
             case BluetoothProfile.STATE_DISCONNECTED:
-                return (connstate.mProfilesConnected == 0 && connstate.mProfilesConnecting == 0);
+                return (mProfilesConnected == 0 && mProfilesConnecting == 0);
 
             default:
                 return true;
@@ -1273,8 +1240,9 @@
             // Reset adapter and profile connection states
             setConnectionState(BluetoothAdapter.STATE_DISCONNECTED);
             mProfileConnectionState.clear();
-            mDevicesConnectionState.clear();
-
+            mProfilesConnected = 0;
+            mProfilesConnecting = 0;
+            mProfilesDisconnecting = 0;
             // adapterPropertyChangedCallback has already been received.  Set the scan mode.
             setScanMode(AbstractionLayer.BT_SCAN_MODE_CONNECTABLE);
             // This keeps NV up-to date on first-boot after flash.
diff --git a/src/com/android/bluetooth/hfp/HeadsetService.java b/src/com/android/bluetooth/hfp/HeadsetService.java
index fce6d43..d9cadd9 100644
--- a/src/com/android/bluetooth/hfp/HeadsetService.java
+++ b/src/com/android/bluetooth/hfp/HeadsetService.java
@@ -988,13 +988,13 @@
             Log.w(TAG, "connect: PRIORITY_OFF, device=" + device + ", " + Utils.getUidPidString());
             return false;
         }
-        ParcelUuid[] featureUuids = mAdapterService.getRemoteUuids(device);
-        if (!BluetoothUuid.containsAnyUuid(featureUuids, HEADSET_UUIDS)) {
-            Log.e(TAG, "connect: Cannot connect to " + device + ": no headset UUID, "
-                    + Utils.getUidPidString());
-            return false;
-        }
         synchronized (mStateMachines) {
+            ParcelUuid[] featureUuids = mAdapterService.getRemoteUuids(device);
+            if (!BluetoothUuid.containsAnyUuid(featureUuids, HEADSET_UUIDS)) {
+                Log.e(TAG, "connect: Cannot connect to " + device + ": no headset UUID, "
+                    + Utils.getUidPidString());
+                return false;
+            }
             Log.i(TAG, "connect: device=" + device + ", " + Utils.getUidPidString());
             HeadsetStateMachine stateMachine = mStateMachines.get(device);
             if (stateMachine == null) {
@@ -1106,12 +1106,12 @@
             Log.e(TAG, "->States is null");
             return devices;
         }
-        final BluetoothDevice[] bondedDevices = mAdapterService.getBondedDevices();
-        if (bondedDevices == null) {
-            Log.e(TAG, "->Bonded device is null");
-            return devices;
-        }
         synchronized (mStateMachines) {
+            final BluetoothDevice[] bondedDevices = mAdapterService.getBondedDevices();
+            if (bondedDevices == null) {
+                Log.e(TAG, "->Bonded device is null");
+                return devices;
+            }
             for (BluetoothDevice device : bondedDevices) {
 
                 int connectionState = getConnectionState(device);
@@ -1175,8 +1175,12 @@
         enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission");
         Log.i(TAG, "setPriority: device=" + device + ", priority=" + priority + ", "
                 + Utils.getUidPidString());
-        mAdapterService.getDatabase()
+        AdapterService adapterService = AdapterService.getAdapterService();
+        if (adapterService != null)
+            adapterService.getDatabase()
                 .setProfilePriority(device, BluetoothProfile.HEADSET, priority);
+        else
+            Log.i(TAG, "adapter service is null");
         return true;
     }
 
@@ -2249,7 +2253,7 @@
         synchronized (mStateMachines) {
             if (toState == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
                 //Transfer SCO is not needed for TWS+ devices
-                if (mAdapterService.isTwsPlusDevice(device) &&
+                if (mAdapterService != null && mAdapterService.isTwsPlusDevice(device) &&
                    mActiveDevice != null &&
                    mAdapterService.isTwsPlusDevice(mActiveDevice) &&
                    isAudioOn()) {
@@ -2260,7 +2264,7 @@
                     Log.w(TAG, "onAudioStateChangedFromStateMachine:"
                             + "shouldPersistAudio() returns"
                             + shouldPersistAudio());
-                    if (mAdapterService.isTwsPlusDevice(device) &&
+                    if (mAdapterService != null && mAdapterService.isTwsPlusDevice(device) &&
                                    isAudioOn()) {
                         Log.w(TAG, "TWS: Don't stop VR or VOIP");
                     } else {
@@ -2293,11 +2297,11 @@
                     if (mActiveDevice != null &&
                                  !mActiveDevice.equals(device) &&
                                  shouldPersistAudio()) {
-                       if (mAdapterService.isTwsPlusDevice(device)
+                       if (mAdapterService != null && mAdapterService.isTwsPlusDevice(device)
                                                    && isAudioOn()) {
                            Log.d(TAG, "TWS: Wait for both eSCO closed");
                        } else {
-                           if (mAdapterService.isTwsPlusDevice(device) &&
+                           if (mAdapterService != null && mAdapterService.isTwsPlusDevice(device) &&
                                isTwsPlusActive(mActiveDevice)) {
                                /* If the device for which SCO got disconnected
                                   is a TwsPlus device and TWS+ set is active