package com.freak.fidji.locationoverbt;

import android.app.IntentService;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Intent;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

public class ManagerService extends IntentService {

    private static final boolean DEBUG = true;
    private static final String TAG = "MANAGER_SERVICE";

    private String mState;
    private BluetoothDevice mDevice;

    public ManagerService() {
        super("ManagerService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (DEBUG)
            Log.d(TAG, "Create service, handle intent");
        if (intent != null) {
            mState = intent.getStringExtra(BtEventsReceiver.ACTION_EXTRA);
            mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            Intent serviceIntent = new Intent(this, MyService.class);
            if (DEBUG)
                Log.d(TAG, "Start service");
            this.startService(serviceIntent);
            if (DEBUG)
                Log.d(TAG, "Bind service");
            this.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            if (DEBUG)
                Log.d(TAG, "Service connected");

            MyService myService = ((MyService.LocalBinder)service).getService();
            if (mState.equals(BluetoothDevice.ACTION_ACL_CONNECTED)){
                if (DEBUG)
                    Log.d(TAG, "Start thread");
                myService.startThread(mDevice);
            }
            else if (mState.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){
                if (DEBUG)
                    Log.d(TAG, "Stop thread");
                myService.stopThread(mDevice);
            }
            else {
                if (DEBUG)
                    Log.d(TAG, "Unknown action");
            }
            ManagerService.this.unbindService(mConnection);
        }

        public void onServiceDisconnected(ComponentName className) {
            if (DEBUG)
                Log.d(TAG, "Service disconnected");
        }
    };

    @Override
    public void onDestroy() {
        if (DEBUG)
            Log.d(TAG, "Destroy service");
        super.onDestroy();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (DEBUG)
            Log.d(TAG, "Create service");
    }
}

@@ -356,16 +356,44 @@
 Service 
+implements ServiceConnection
 %7B%0A%0A    p
@@ -555,16 +555,45 @@
 mDevice;
+%0A    private boolean isBound;
 %0A%0A    pu
@@ -1277,27 +1277,20 @@
 Intent, 
-mConnection
+this
 , Contex
@@ -1331,82 +1331,8 @@
  %7D%0A%0A
-    private ServiceConnection mConnection = new ServiceConnection() %7B%0A    
     
@@ -1406,36 +1406,32 @@
 vice) %7B%0A        
-    
 if (DEBUG)%0A     
@@ -1417,36 +1417,32 @@
      if (DEBUG)%0A
-    
             Log.
@@ -1479,20 +1479,16 @@
         
- 
-   
 MyServic
@@ -1552,28 +1552,24 @@
 ();%0A        
-    
 if (mState.e
@@ -1618,36 +1618,32 @@
 ))%7B%0A            
-   
- 
 if (DEBUG)%0A     
@@ -1629,36 +1629,32 @@
      if (DEBUG)%0A
-    
                 
@@ -1673,36 +1673,32 @@
 Start thread%22);%0A
-    
             mySe
@@ -1729,34 +1729,26 @@
 e);%0A        
-    %7D%0A    
+%7D%0A
         else
@@ -1813,36 +1813,32 @@
 ))%7B%0A            
-    
 if (DEBUG)%0A     
@@ -1824,36 +1824,32 @@
      if (DEBUG)%0A
-    
                 
@@ -1879,36 +1879,32 @@
 %22);%0A            
-   
- 
 myService.stopTh
@@ -1922,34 +1922,26 @@
 e);%0A        
-    %7D%0A    
+%7D%0A
         else
@@ -1947,36 +1947,32 @@
 e %7B%0A            
-    
 if (DEBUG)%0A     
@@ -1958,36 +1958,32 @@
      if (DEBUG)%0A
-    
                 
@@ -2016,28 +2016,24 @@
 %22);%0A        
- 
-   
 %7D%0A          
@@ -2034,75 +2034,51 @@
     
-    ManagerService.this.unbindService(mConnection);%0A        %7D%0A
+isBound = true;%0A        stopSelf();
 %0A    
+%7D%0A%0A
     
@@ -2138,36 +2138,32 @@
 Name) %7B%0A        
-    
 if (DEBUG)%0A     
@@ -2161,36 +2161,32 @@
 UG)%0A            
-  
-  
 Log.d(TAG, %22Serv
@@ -2217,16 +2217,30 @@
     
-%7D
+isBound = false;
 %0A    %7D
-;
 %0A%0A  
@@ -2335,32 +2335,86 @@
 troy service%22);%0A
+        if (isBound)%0A            unbindService(this);%0A
         super.on

Fix sometime crash of ManagerService when unbinding