MAP: CleanUp for SDP record in MAP Service shutdown

Remove SDP record per MAS Instance during MAP Service
shutdown procedure.
Add more relevant debug logging.

Change-Id: I222ad97d719b9a2c45db3ab8526dd3de9c553f3d
diff --git a/src/com/android/bluetooth/map/BluetoothMapMasInstance.java b/src/com/android/bluetooth/map/BluetoothMapMasInstance.java
index 0da7292..0dda44b 100644
--- a/src/com/android/bluetooth/map/BluetoothMapMasInstance.java
+++ b/src/com/android/bluetooth/map/BluetoothMapMasInstance.java
@@ -130,6 +130,17 @@
         init();
     }
 
+    private void removeSdpRecord() {
+        if (mAdapter != null && mSdpHandle >= 0 &&
+                SdpManager.getDefaultManager() != null) {
+            if (V) Log.d(TAG, "Removing SDP record for MAS instance: " + mMasInstanceId +
+                    " Object reference: " + this + "SDP handle: " + mSdpHandle);
+            boolean status = SdpManager.getDefaultManager().removeSdpRecord(mSdpHandle);
+            Log.d(TAG, "RemoveSDPrecord returns " + status);
+            mSdpHandle = -1;
+        }
+    }
+
     /* Needed only for test */
     protected BluetoothMapMasInstance() {
         TAG = "BluetoothMapMasInstance" + sInstanceCounter++;
@@ -277,11 +288,7 @@
                 Log.e(TAG, "Failed to start the listeners");
                 return;
             }
-            if(mSdpHandle >= 0) {
-                SdpManager.getDefaultManager().removeSdpRecord(mSdpHandle);
-                if(V) Log.d(TAG, "Removing SDP record for MAS instance: " + mMasInstanceId +
-                        " Object reference: " + this + "SDP handle: " + mSdpHandle);
-            }
+            removeSdpRecord();
             mSdpHandle = createMasSdpRecord(mServerSockets.getRfcommChannel(),
                     mServerSockets.getL2capPsm());
             // Here we might have changed crucial data, hence reset DB identifier
@@ -399,6 +406,8 @@
             mObserver = null;
         }
 
+        removeSdpRecord();
+
         closeConnectionSocket();
 
         closeServerSockets(true);