Bluetooth: Handle exception in SAP/DUN during BT ON/OFF

A case where BT ON/OFF APP is configured to do BT ON/OFF
for 5000 iteration. Every time when BT is turned on, then
SAP & DUN status properties need to be set as running, to
start SAP & DUN daemons. Property setting get failed rarely,
which is throwing runtime exception & BT process gets
terminated. Handling this exception & cleaning up bound
adapter service to solve this issue.

Change-Id: Ie3db97b57cf09e5b7b3394e7fdb7f81208f458a9
CRs-Fixed: 605568
diff --git a/src/org/codeaurora/bluetooth/dun/BluetoothDunService.java b/src/org/codeaurora/bluetooth/dun/BluetoothDunService.java
index 88ea756..d1c8986 100644
--- a/src/org/codeaurora/bluetooth/dun/BluetoothDunService.java
+++ b/src/org/codeaurora/bluetooth/dun/BluetoothDunService.java
@@ -399,12 +399,29 @@
                 }
 
                 Log.v(TAG, "Starting DUN server process");
-                SystemProperties.set(BLUETOOTH_DUN_PROFILE_STATUS, "running");
+                try {
+                    SystemProperties.set(BLUETOOTH_DUN_PROFILE_STATUS, "running");
+                    mDunEnable = true;
+                } catch (RuntimeException e) {
+                    Log.v(TAG, "Could not start DUN server process: " + e);
+                }
 
-                mDunHandler.sendMessage(mDunHandler
-                        .obtainMessage(MESSAGE_START_LISTENER));
-
-                mDunEnable = true;
+                if (mDunEnable) {
+                    mDunHandler.sendMessage(mDunHandler
+                            .obtainMessage(MESSAGE_START_LISTENER));
+                } else {
+                    //DUN server process is not started successfully.
+                    //So clean up service connection to avoid service connection leak
+                    if (mAdapterService != null) {
+                        try {
+                            mAdapterService = null;
+                            unbindService(mConnection);
+                        } catch (IllegalArgumentException e) {
+                            Log.e(TAG, "could not unbind the adapter Service", e);
+                        }
+                    }
+                    return;
+                }
             }
             else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
                 // Send any pending timeout now, as this service will be destroyed.
@@ -416,7 +433,11 @@
 
                 if (mDunEnable) {
                     Log.v(TAG, "Stopping DUN server process");
-                    SystemProperties.set(BLUETOOTH_DUN_PROFILE_STATUS, "stopped");
+                    try {
+                        SystemProperties.set(BLUETOOTH_DUN_PROFILE_STATUS, "stopped");
+                    } catch (RuntimeException e) {
+                        Log.v(TAG, "Could not stop DUN server process: " + e);
+                    }
 
                     synchronized(mConnection) {
                         try {
diff --git a/src/org/codeaurora/bluetooth/sap/BluetoothSapService.java b/src/org/codeaurora/bluetooth/sap/BluetoothSapService.java
index ab754e0..7dd6125 100644
--- a/src/org/codeaurora/bluetooth/sap/BluetoothSapService.java
+++ b/src/org/codeaurora/bluetooth/sap/BluetoothSapService.java
@@ -420,12 +420,29 @@
                 }
 
                 Log.v(TAG, "Starting SAP server process");
-                SystemProperties.set(BLUETOOTH_SAP_PROFILE_STATUS, "running");
+                try {
+                    SystemProperties.set(BLUETOOTH_SAP_PROFILE_STATUS, "running");
+                    mSapEnable = true;
+                } catch (RuntimeException e) {
+                    Log.v(TAG, "Could not start SAP server process: " + e);
+                }
 
-                mSapHandler.sendMessage(mSapHandler
-                        .obtainMessage(MESSAGE_START_LISTENER));
-
-                mSapEnable = true;
+                if (mSapEnable) {
+                    mSapHandler.sendMessage(mSapHandler
+                            .obtainMessage(MESSAGE_START_LISTENER));
+                } else {
+                    //Sap server process is not started successfully.
+                    //So clean up service connection to avoid service connection leak
+                    if (mAdapterService != null) {
+                        try {
+                            mAdapterService = null;
+                            unbindService(mConnection);
+                        } catch (IllegalArgumentException e) {
+                            Log.e(TAG, "could not unbind the adapter Service", e);
+                        }
+                    }
+                    return;
+                }
             }
             else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
                 // Send any pending timeout now, as this service will be destroyed.
@@ -437,7 +454,12 @@
 
                 if (mSapEnable) {
                     Log.v(TAG, "Stopping SAP server process");
-                    SystemProperties.set(BLUETOOTH_SAP_PROFILE_STATUS, "stopped");
+
+                    try {
+                        SystemProperties.set(BLUETOOTH_SAP_PROFILE_STATUS, "stopped");
+                    } catch (RuntimeException e) {
+                        Log.v(TAG, "Could not stop SAP server process: " + e);
+                    }
 
                     synchronized(mConnection) {
                         try {