MAP: Fix bugs in SDP search result handling

* Without any SDP record indicating support for 1.1 and 1.2, the default
  version for MAP-event-report XML message should be 1.0 instead of 1.2
* Support for various MAP features should consider both local and remote
  MapSupportedFeatures bit mask
* Move handling of MapSupportedFeatures in BluetoothMapContentObserver
  to one place

Bug: 35389912
Test: make, test MAP with carkit
Change-Id: Ice66d7e82e1a7446915207bea9012ed93f2474c7
diff --git a/android/app/src/com/android/bluetooth/map/BluetoothMapContentObserver.java b/android/app/src/com/android/bluetooth/map/BluetoothMapContentObserver.java
index ea88d08..5f1e6f7 100644
--- a/android/app/src/com/android/bluetooth/map/BluetoothMapContentObserver.java
+++ b/android/app/src/com/android/bluetooth/map/BluetoothMapContentObserver.java
@@ -267,20 +267,7 @@
         mAccount = account;
         mMasInstance = masInstance;
         mMasId = mMasInstance.getMasId();
-
-        mMapSupportedFeatures = mMasInstance.getRemoteFeatureMask();
-        if (D) Log.d(TAG, "BluetoothMapContentObserver: Supported features " +
-                Integer.toHexString(mMapSupportedFeatures) ) ;
-
-        if((BluetoothMapUtils.MAP_FEATURE_EXTENDED_EVENT_REPORT_11_BIT
-                & mMapSupportedFeatures) != 0){
-            mMapEventReportVersion = BluetoothMapUtils.MAP_EVENT_REPORT_V11;
-        }
-        // Make sure support for all formats result in latest version returned
-        if((BluetoothMapUtils.MAP_FEATURE_EVENT_REPORT_V12_BIT
-                & mMapSupportedFeatures) != 0){
-            mMapEventReportVersion = BluetoothMapUtils.MAP_EVENT_REPORT_V12;
-        }
+        setObserverRemoteFeatureMask(mMasInstance.getRemoteFeatureMask());
 
         if(account != null) {
             mAuthority = Uri.parse(account.mBase_uri).getAuthority();
@@ -337,7 +324,8 @@
     }
 
     public void setObserverRemoteFeatureMask(int remoteSupportedFeatures) {
-        mMapSupportedFeatures = remoteSupportedFeatures;
+        mMapSupportedFeatures =
+                remoteSupportedFeatures & BluetoothMapMasInstance.SDP_MAP_MAS_FEATURES;
         if ((BluetoothMapUtils.MAP_FEATURE_EXTENDED_EVENT_REPORT_11_BIT
                 & mMapSupportedFeatures) != 0) {
             mMapEventReportVersion = BluetoothMapUtils.MAP_EVENT_REPORT_V11;
@@ -346,9 +334,20 @@
         if ((BluetoothMapUtils.MAP_FEATURE_EVENT_REPORT_V12_BIT
                 & mMapSupportedFeatures) != 0) {
             mMapEventReportVersion = BluetoothMapUtils.MAP_EVENT_REPORT_V12;
+        } else if (((BluetoothMapUtils.MAP_FEATURE_PARTICIPANT_PRESENCE_CHANGE_BIT
+                            | BluetoothMapUtils.MAP_FEATURE_PARTICIPANT_CHAT_STATE_CHANGE_BIT)
+                           & mMapSupportedFeatures)
+                != 0) {
+            // Warning according to page 46/123 of MAP 1.3 spec
+            Log.w(TAG,
+                    "setObserverRemoteFeatureMask: Extended Event Reports 1.2 is not set even"
+                            + "though PARTICIPANT_PRESENCE_CHANGE_BIT or PARTICIPANT_CHAT_STATE_CHANGE_BIT"
+                            + " were set, mMapSupportedFeatures=" + mMapSupportedFeatures);
         }
-        if (V) Log.d(TAG, "setObserverRemoteFeatureMask : " + mMapEventReportVersion
-            + " mMapSupportedFeatures : " + mMapSupportedFeatures);
+        if (D)
+            Log.d(TAG,
+                    "setObserverRemoteFeatureMask: mMapEventReportVersion=" + mMapEventReportVersion
+                            + " mMapSupportedFeatures=" + mMapSupportedFeatures);
     }
 
     private Map<Long, Msg> getMsgListSms() {
@@ -738,12 +737,12 @@
                 xmlEvtReport.startDocument("UTF-8", true);
                 xmlEvtReport.text("\r\n");
                 xmlEvtReport.startTag("", "MAP-event-report");
-                if (mMapEventReportVersion == BluetoothMapUtils.MAP_EVENT_REPORT_V10) {
-                    xmlEvtReport.attribute("", "version", BluetoothMapUtils.MAP_V10_STR);
+                if (mMapEventReportVersion == BluetoothMapUtils.MAP_EVENT_REPORT_V12) {
+                    xmlEvtReport.attribute("", "version", BluetoothMapUtils.MAP_V12_STR);
                 } else if (mMapEventReportVersion == BluetoothMapUtils.MAP_EVENT_REPORT_V11) {
                     xmlEvtReport.attribute("", "version", BluetoothMapUtils.MAP_V11_STR);
                 } else {
-                    xmlEvtReport.attribute("", "version", BluetoothMapUtils.MAP_V12_STR);
+                    xmlEvtReport.attribute("", "version", BluetoothMapUtils.MAP_V10_STR);
                 }
                 xmlEvtReport.startTag("", "event");
                 xmlEvtReport.attribute("", "type", eventType);
diff --git a/android/app/src/com/android/bluetooth/map/BluetoothMapMasInstance.java b/android/app/src/com/android/bluetooth/map/BluetoothMapMasInstance.java
index 3d32382..6764957 100644
--- a/android/app/src/com/android/bluetooth/map/BluetoothMapMasInstance.java
+++ b/android/app/src/com/android/bluetooth/map/BluetoothMapMasInstance.java
@@ -54,7 +54,7 @@
     private static final int SDP_MAP_MAS_VERSION       = 0x0102;
 
     /* TODO: Should these be adaptive for each MAS? - e.g. read from app? */
-    private static final int SDP_MAP_MAS_FEATURES      = 0x0000007F;
+    static final int SDP_MAP_MAS_FEATURES = 0x0000007F;
 
     private ServerSession mServerSession = null;
     // The handle to the socket registration with SDP
@@ -443,7 +443,7 @@
 
     public void setRemoteFeatureMask(int supportedFeatures) {
        if(V) Log.v(TAG, "setRemoteFeatureMask : Curr: "+ mRemoteFeatureMask);
-       mRemoteFeatureMask  = supportedFeatures;
+       mRemoteFeatureMask = supportedFeatures & SDP_MAP_MAS_FEATURES;
        if (mObserver != null) {
            mObserver.setObserverRemoteFeatureMask(mRemoteFeatureMask);
            if(V) Log.v(TAG, "setRemoteFeatureMask : set: " + mRemoteFeatureMask);