Proper handling of AV connection collision

Use case:
1. Pair to Remote
2. Turn OFF/ON BT on DUT
3. After DUT's BT is turned ON, Remote would reconnect to DUT

Failure:
  Bluetooth settings UI continues to display Connecting when
  BT was Turned OFF and ON.

Root cause:
- This is a connection collision case where remote is not responding
  to DUT initiated SDP attribute fetch request for AV. AV holds outgoing
  connection for sometime and meanwhile remote starts AV connection
  from its end. Then DUT disconnects the SDP channel after a second
  without waiting to fetch requested info, and this confuses DUT's AV
  state machine. The outgoing connection could not proceed as incoming
  AV L2CAP connects, because DUT's AV state machine is not equipped
  with handling this scenario. On incoming connection timer expiry, it
  does not start AV media channel as it finds AV Open has not been done
  from upper layer after incoming L2CAP got connected.

- When the incoming path 2s timer expires, the DUT checks whether Open
  has been called after incoming state is set. This is not the case here,
  because Open was triggered earlier as as part of the outgoing
  connection. As a result, A2DP cannot connect at all.

Fix:
- Properly set collision flags for handling AV connection collision.
- Update BTA AV handle even when BTIF AV state machine is yet to be built.

Bug: 30362987
Change-Id: I02a3adb62479b0f762bc792a5727d06e11eaaa52
(cherry picked from commit db1e3a5fbb41cd1faf572c5f89ec9212c5ff2986)
diff --git a/bta/av/bta_av_aact.c b/bta/av/bta_av_aact.c
index 6c5d874..1e73fa6 100644
--- a/bta/av/bta_av_aact.c
+++ b/bta/av/bta_av_aact.c
@@ -1567,7 +1567,10 @@
     {
         /* SNK initiated L2C connection while SRC was doing SDP.    */
         /* Wait until timeout to check if SNK starts signalling.    */
-        APPL_TRACE_EVENT("bta_av_connect_req: coll_mask = 0x%2X", p_scb->coll_mask);
+        APPL_TRACE_EVENT("%s: coll_mask = 0x%2X", __func__, p_scb->coll_mask);
+        p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
+        APPL_TRACE_EVENT("%s: updated coll_mask = 0x%2X", __func__,
+                         p_scb->coll_mask);
         return;
     }
 
diff --git a/bta/av/bta_av_act.c b/bta/av/bta_av_act.c
index 5857dd6..9cc802f 100644
--- a/bta/av/bta_av_act.c
+++ b/bta/av/bta_av_act.c
@@ -1613,6 +1613,8 @@
 
             if (bta_av_is_scb_opening(p_scb))
             {
+                APPL_TRACE_DEBUG("%s: stream state opening: SDP started = %d",
+                                 __func__, p_scb->sdp_discovery_started);
                 if (p_scb->sdp_discovery_started)
                 {
                     /* We are still doing SDP. Run the timer again. */
@@ -1634,6 +1636,7 @@
             {
                 /* Stay in incoming state if SNK does not start signalling */
 
+                APPL_TRACE_DEBUG("%s: stream state incoming", __func__);
                 /* API open was called right after SNK opened L2C connection. */
                 if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED)
                 {
diff --git a/btif/src/btif_av.c b/btif/src/btif_av.c
index 03ba842..f852d48 100644
--- a/btif/src/btif_av.c
+++ b/btif/src/btif_av.c
@@ -1044,13 +1044,21 @@
 
 static void btif_av_handle_event(UINT16 event, char* p_param)
 {
+    BTIF_TRACE_EVENT("%s event:%s", __func__,
+                     dump_av_sm_event_name((btif_av_sm_event_t)event));
     switch(event)
     {
         case BTIF_AV_CLEANUP_REQ_EVT:
-            BTIF_TRACE_EVENT("%s: BTIF_AV_CLEANUP_REQ_EVT", __FUNCTION__);
             btif_a2dp_stop_media_task();
             break;
 
+        case BTA_AV_REGISTER_EVT:
+            if (btif_av_cb.sm_handle == NULL)
+            {
+                btif_av_cb.bta_handle = ((tBTA_AV*)p_param)->registr.hndl;
+                BTIF_TRACE_DEBUG("%s: BTA AV Handle updated", __func__);
+            }
+            /* FALLTHROUGH */
         default:
             btif_sm_dispatch(btif_av_cb.sm_handle, event, (void*)p_param);
             btif_av_event_free_data(event, p_param);