package com.nbusy.app.services;

import android.app.Service;
import android.content.Intent;
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 final Worker worker = WorkerSingleton.getWorker();
    private int startId;

    private void init() {
        if (!stopSelfResult(startId)) {
            Log.e(TAG, "failed to stop service with startId: " + startId + " which did not match the one from the last start request");
        }
    }

    /*********************
     * 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;
    }
}

@@ -85,16 +85,45 @@
 Intent;%0A
+import android.os.AsyncTask;%0A
 import a
@@ -132,32 +132,32 @@
 roid.os.Binder;%0A
-
 import android.o
@@ -556,16 +556,75 @@
 tedBy%22;%0A
+    private static final int STANDBY_TIME = 3 * 60 * 1000;%0A
     priv
@@ -726,24 +726,568 @@
 id init() %7B%0A
+        class StopStandby extends AsyncTask%3CVoid, Void, Void%3E %7B%0A            @Override%0A            protected Void doInBackground(Void... params) %7B%0A                while (worker.needConnection()) %7B%0A                    try %7B%0A                        Thread.sleep(STANDBY_TIME);%0A                    %7D catch (InterruptedException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A                return null;%0A            %7D%0A%0A            @Override%0A            protected void onPostExecute(Void result) %7B%0A        
         if (
@@ -1322,24 +1322,32 @@
             
+        
 Log.e(TAG, %22
@@ -1458,33 +1458,102 @@
 uest%22);%0A        
-%7D
+        %7D%0A            %7D%0A        %7D%0A        new StopStandby().execute();
 %0A    %7D%0A%0A    /***

add WorkerService.StopStandby