PAN: Fix service restart during force kill.

Use case:
During sns test.

Root cause:
During force kill adapter service not sending
clean up request so same service restarting by framework
because of active service instance.

Fix:
stop pan service when we receive stop request
from adapter service so that we can avoid
restart of service during force kill.

CRs-Fixed: 2517852
Change-Id: I9fa08352b9403b345e40059b4318634ba53120e5
diff --git a/src/com/android/bluetooth/pan/PanService.java b/src/com/android/bluetooth/pan/PanService.java
index 2685c0e..be19f10 100644
--- a/src/com/android/bluetooth/pan/PanService.java
+++ b/src/com/android/bluetooth/pan/PanService.java
@@ -72,6 +72,7 @@
     private static final int MESSAGE_CONNECT = 1;
     private static final int MESSAGE_DISCONNECT = 2;
     private static final int MESSAGE_CONNECT_STATE_CHANGED = 11;
+    private static final int STOP_LISTENER = 200;
     private boolean mTetherOn = false;
 
     private BluetoothTetheringNetworkFactory mNetworkFactory;
@@ -127,34 +128,15 @@
 
     @Override
     protected boolean stop() {
-        mHandler.removeCallbacksAndMessages(null);
+        Log.i(TAG, " stop");
+        mHandler.sendMessage(mHandler.obtainMessage(STOP_LISTENER));
         return true;
     }
 
     @Override
     protected void cleanup() {
-        // TODO(b/72948646): this should be moved to stop()
-        setPanService(null);
-        if (mNativeAvailable) {
-            cleanupNative();
-            mNativeAvailable = false;
-        }
-        if (mPanDevices != null) {
-           int[] desiredStates = {BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED,
-                                  BluetoothProfile.STATE_DISCONNECTING};
-           List<BluetoothDevice> devList =
-                   getDevicesMatchingConnectionStates(desiredStates);
-           for (BluetoothDevice device : devList) {
-                BluetoothPanDevice panDevice = mPanDevices.get(device);
-                Log.d(TAG, "panDevice: " + panDevice + " device address: " + device);
-                if (panDevice != null) {
-                    handlePanDeviceStateChange(device, mPanIfName,
-                        BluetoothProfile.STATE_DISCONNECTED,
-                        panDevice.mLocalRole, panDevice.mRemoteRole);
-                }
-            }
-            mPanDevices.clear();
-        }
+        Log.i(TAG, " cleanup");
+        mHandler.removeCallbacksAndMessages(null);
     }
 
     private final Handler mHandler = new Handler() {
@@ -199,6 +181,30 @@
                             convertHalState(cs.state), cs.local_role, cs.remote_role);
                 }
                 break;
+                case STOP_LISTENER :
+                    setPanService(null);
+                    if (mNativeAvailable) {
+                        cleanupNative();
+                        mNativeAvailable = false;
+                    }
+                    if (mPanDevices != null) {
+                       int[] desiredStates = {BluetoothProfile.STATE_CONNECTING,
+                                              BluetoothProfile.STATE_CONNECTED,
+                                              BluetoothProfile.STATE_DISCONNECTING};
+                       List<BluetoothDevice> devList =
+                               getDevicesMatchingConnectionStates(desiredStates);
+                       for (BluetoothDevice device : devList) {
+                            BluetoothPanDevice panDevice = mPanDevices.get(device);
+                            Log.d(TAG, "panDevice: " + panDevice + " device address: " + device);
+                            if (panDevice != null) {
+                                handlePanDeviceStateChange(device, mPanIfName,
+                                    BluetoothProfile.STATE_DISCONNECTED,
+                                    panDevice.mLocalRole, panDevice.mRemoteRole);
+                            }
+                        }
+                        mPanDevices.clear();
+                    }
+                    break;
             }
         }
     };