Merge cherrypicks of [3063860, 3064438, 3064439, 3063679, 3063680, 3063681, 3064498] into oc-mr1-release

Change-Id: I3e5426d43227e5ad01b8477a848e50cb6c311afd
diff --git a/src/com/android/bluetooth/mapclient/MnsService.java b/src/com/android/bluetooth/mapclient/MnsService.java
index 7b3419e..1078cf1 100644
--- a/src/com/android/bluetooth/mapclient/MnsService.java
+++ b/src/com/android/bluetooth/mapclient/MnsService.java
@@ -50,26 +50,49 @@
 
     static private MapClientService mContext;
     private volatile boolean mShutdown = false;         // Used to interrupt socket accept thread
+    private int mSdpHandle = -1;
 
     MnsService(MapClientService context) {
         if (VDBG) Log.v(TAG, "MnsService()");
         mContext = context;
         mAcceptThread = new SocketAcceptor();
         mServerSockets = ObexServerSockets.create(mAcceptThread);
-        SdpManager.getDefaultManager().createMapMnsRecord(
-                "MAP Message Notification Service", mServerSockets.getRfcommChannel(), -1,
-                MNS_VERSION, MNS_FEATURE_BITS);
+        SdpManager sdpManager = SdpManager.getDefaultManager();
+        if (sdpManager == null) {
+            Log.e(TAG, "SdpManager is null");
+            return;
+        }
+        mSdpHandle = sdpManager.createMapMnsRecord("MAP Message Notification Service",
+                mServerSockets.getRfcommChannel(), -1, MNS_VERSION, MNS_FEATURE_BITS);
     }
 
     void stop() {
         if (VDBG) Log.v(TAG, "stop()");
         mShutdown = true;
+        cleanUpSdpRecord();
         if (mServerSockets != null) {
             mServerSockets.shutdown(false);
             mServerSockets = null;
         }
     }
 
+    private void cleanUpSdpRecord() {
+        if (mSdpHandle < 0) {
+            Log.e(TAG, "cleanUpSdpRecord, SDP record never created");
+            return;
+        }
+        int sdpHandle = mSdpHandle;
+        mSdpHandle = -1;
+        SdpManager sdpManager = SdpManager.getDefaultManager();
+        if (sdpManager == null) {
+            Log.e(TAG, "cleanUpSdpRecord failed, sdpManager is null, sdpHandle=" + sdpHandle);
+            return;
+        }
+        Log.i(TAG, "cleanUpSdpRecord, mSdpHandle=" + sdpHandle);
+        if (!sdpManager.removeSdpRecord(sdpHandle)) {
+            Log.e(TAG, "cleanUpSdpRecord, removeSdpRecord failed, sdpHandle=" + sdpHandle);
+        }
+    }
 
     private class SocketAcceptor implements IObexConnectionHandler {
 
diff --git a/src/com/android/bluetooth/opp/BluetoothOppService.java b/src/com/android/bluetooth/opp/BluetoothOppService.java
index b68936a..7f94820 100644
--- a/src/com/android/bluetooth/opp/BluetoothOppService.java
+++ b/src/com/android/bluetooth/opp/BluetoothOppService.java
@@ -226,14 +226,6 @@
         public void handleMessage(Message msg) {
             switch (msg.what) {
                 case STOP_LISTENER:
-                    if (mAdapter != null && mOppSdpHandle >= 0
-                            && SdpManager.getDefaultManager() != null) {
-                        if (D) Log.d(TAG, "Removing SDP record mOppSdpHandle :" + mOppSdpHandle);
-                        boolean status =
-                                SdpManager.getDefaultManager().removeSdpRecord(mOppSdpHandle);
-                        Log.d(TAG, "RemoveSDPrecord returns " + status);
-                        mOppSdpHandle = -1;
-                    }
                     stopListeners();
                     mListenStarted = false;
                     //Stop Active INBOUND Transfer
@@ -365,8 +357,10 @@
                             + " mServerSocket:" + mServerSocket);
             return;
         }
-        sdpManager.createOppOpsRecord("OBEX Object Push", mServerSocket.getRfcommChannel(),
-                mServerSocket.getL2capPsm(), 0x0102, SUPPORTED_OPP_FORMAT);
+        mOppSdpHandle =
+                sdpManager.createOppOpsRecord("OBEX Object Push", mServerSocket.getRfcommChannel(),
+                        mServerSocket.getL2capPsm(), 0x0102, SUPPORTED_OPP_FORMAT);
+        if (D) Log.d(TAG, "mOppSdpHandle :" + mOppSdpHandle);
     }
 
     @Override
@@ -1063,6 +1057,12 @@
     }
 
     private void stopListeners() {
+        if (mAdapter != null && mOppSdpHandle >= 0 && SdpManager.getDefaultManager() != null) {
+            if (D) Log.d(TAG, "Removing SDP record mOppSdpHandle :" + mOppSdpHandle);
+            boolean status = SdpManager.getDefaultManager().removeSdpRecord(mOppSdpHandle);
+            Log.d(TAG, "RemoveSDPrecord returns " + status);
+            mOppSdpHandle = -1;
+        }
         if (mServerSocket != null) {
             mServerSocket.shutdown(false);
             mServerSocket = null;