AudioService: simplify/document locking

  In AudioDeviceBroker, mDeviceStateLock was the main lock and
was previously used almost as a global synchronization, present
on the handling of most messages, and numerous other methods
without actually protecting the only states it maintains. Note
that BtHelper and AudioDeviceInventory have their own sync
model, so they don't need to be protected by a lock from
AudioDeviceBroker.
  This CL renames mDeviceStateLock to mDeviceBrokerLock
specifically protecting:
- "force use" setting for communication
- Bluetooth A2DP enabled (coming from AudioManager API)
- all management of the message queue, both for enqueueing
  and removal (which was exposed to race condition since commit
  dc552e9 for A2DP (dis)connection.
It also removes the lock used to synchronize the audio mode
owner with the SCO operations: the only information AudioDeviceBroker
needed from AudioService regarding audio mode was the PID of the
mode owner. The new code uses an atomic integer to store/provide
this information, instead of relying on a shared lock amongst
multiple classes.
  Add test for behavior where media gets unmuted if it was muted
when the user connects an A2DP headset.

Bug: 140200704
Test: atest AudioDeviceBrokerTest ; atest AudioManagerTest
Change-Id: If6a98d8aad61da6eef4b54ef9c57dc80d74be593
diff --git a/services/tests/servicestests/src/com/android/server/audio/AudioDeviceBrokerTest.java b/services/tests/servicestests/src/com/android/server/audio/AudioDeviceBrokerTest.java
index 5c2ad94..29a8dad 100644
--- a/services/tests/servicestests/src/com/android/server/audio/AudioDeviceBrokerTest.java
+++ b/services/tests/servicestests/src/com/android/server/audio/AudioDeviceBrokerTest.java
@@ -126,6 +126,22 @@
         doTestConnectionDisconnectionReconnection(AudioService.BECOMING_NOISY_DELAY_MS / 2);
     }
 
+    /**
+     * Verify connecting an A2DP sink will call into AudioService to unmute media
+     */
+    @Test
+    public void testA2dpConnectionUnmutesMedia() throws Exception {
+        Log.i(TAG, "testA2dpConnectionUnmutesMedia");
+        Assert.assertNotNull("invalid null BT device", mFakeBtDevice);
+
+        mAudioDeviceBroker.postBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(mFakeBtDevice,
+                BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP, true, 1);
+        Thread.sleep(MAX_MESSAGE_HANDLING_DELAY_MS);
+        verify(mMockAudioService, times(1)).postAccessoryPlugMediaUnmute(
+                ArgumentMatchers.eq(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP));
+
+    }
+
     private void doTestConnectionDisconnectionReconnection(int delayAfterDisconnection)
             throws Exception {
         when(mMockAudioService.getDeviceForStream(AudioManager.STREAM_MUSIC))