package com.nbusy.app.services;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.nbusy.app.worker.Worker;
import com.nbusy.app.worker.WorkerSingleton;

/**
 * Hosts {@link Worker} class to ensure continuous operation even when no activity is visible.
 */
public class WorkerService extends Service {

    private static final String TAG = WorkerService.class.getSimpleName();
    public static final String STARTED_BY = "StartedBy";
    private static final int STANDBY_TIME = 3 * 60 * 1000;
    private final Worker worker = WorkerSingleton.getWorker();
    private int startId;

    private void init() {
        class StopStandby extends AsyncTask<Void, Void, Void> {
            @Override
            protected Void doInBackground(Void... params) {
                while (worker.needConnection()) {
                    try {
                        Thread.sleep(STANDBY_TIME);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                if (!stopSelfResult(startId)) {
                    Log.e(TAG, "failed to stop service with startId: " + startId + " which did not match the one from the last start request");
                }
            }
        }
        new StopStandby().execute();
    }

    /*********************
     * Service Overrides *
     *********************/

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.startId = startId;

        if (intent != null) {
            String startedBy = intent.getStringExtra(STARTED_BY);
            Log.i(TAG, "Started by: " + startedBy);
        } else {
            // service is restarted by Android system after a termination so intent will be null in this case
            Log.i(TAG, "Restarted by Android system after termination.");
        }

        init();

        // we want this service to continue running until it is explicitly stopped, so return sticky
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "Destroyed.");
        WorkerSingleton.destroyWorker();
    }

    /*************************
     * Local service binding *
     *************************/

    private final IBinder binder = new WorkerServiceBinder();

    /**
     * Returns an instance of this service so binding components can directly call public methods of this service.
     */
    public class WorkerServiceBinder extends Binder {
        WorkerService getService() {
            return WorkerService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        String startedBy = intent.getStringExtra(STARTED_BY);
        Log.i(TAG, "Was bound to by: " + startedBy);

        // allow binding to this local service directly so anyone can call public functions on this service directly
        return binder;
    }
}

@@ -615,16 +615,79 @@
 * 1000;%0A
+    private final StopStandby stopStandby = new StopStandby();%0A
     priv
@@ -771,38 +771,8 @@
 d;%0A%0A
-    private void init() %7B%0A    
     
@@ -827,36 +827,32 @@
 Void%3E %7B%0A        
-    
 @Override%0A      
@@ -845,36 +845,32 @@
 verride%0A        
- 
-   
 protected Void d
@@ -913,20 +913,16 @@
         
-   
- 
 while (w
@@ -963,20 +963,16 @@
         
-    
 try %7B%0A  
@@ -989,20 +989,16 @@
         
-  
-  
 Thread.s
@@ -1017,20 +1017,16 @@
 _TIME);%0A
-    
         
@@ -1088,20 +1088,16 @@
         
-    
 e.printS
@@ -1121,34 +1121,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -1149,28 +1149,24 @@
             
- 
-   
 return null;
@@ -1166,20 +1166,16 @@
 n null;%0A
-    
         
@@ -1181,28 +1181,24 @@
  %7D%0A%0A        
-    
 @Override%0A  
@@ -1199,28 +1199,24 @@
 ide%0A        
-  
-  
 protected vo
@@ -1255,28 +1255,24 @@
             
-    
 if (!stopSel
@@ -1287,28 +1287,24 @@
 startId)) %7B%0A
-    
             
@@ -1431,34 +1431,16 @@
 uest%22);%0A
-                %7D%0A
         
@@ -1459,45 +1459,8 @@
   %7D%0A
-        new StopStandby().execute();%0A
     
@@ -2112,14 +2112,110 @@
    i
-nit();
+f (stopStandby.getStatus() != AsyncTask.Status.RUNNING) %7B%0A            stopStandby.execute();%0A        %7D
 %0A%0A  

move StopStandby into a subclass