package com.nbusy.app;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import java.util.Objects;

public class WorkerService extends Service {

    private boolean running;

    private boolean startedByBoot;

    @Override
    public IBinder onBind(Intent intent) {
        // don't allow direct binding to this service
        return null;
    }

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

        // todo: initiate connection to nbusy server using neptulon json-rpc java client (or nbusy java client which wraps that and auto-adds all routes)?
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // we want this service to continue running until it is explicitly stopped, so return sticky
        int startType = Service.START_STICKY;

        if (running) {
            return startType;
        }

        running = true;

        String startedBy = intent.getStringExtra("StartedBy");
        if (startedBy != null && Objects.equals(startedBy, "DeviceBootReceiver")) {
            startedByBoot = true;
        }

        return startType;
    }
}

@@ -185,31 +185,72 @@
     
-private boolean
+// whether to terminate after task queue is done or keep
  running
 ;%0A%0A 
@@ -245,18 +245,16 @@
  running
-;%0A
 %0A    pri
@@ -266,29 +266,34 @@
 boolean 
-startedByBoot
+terminateAfterDone
 ;%0A%0A    @
@@ -758,20 +758,21 @@
  // 
-we want this
+terminate the
  ser
@@ -780,212 +780,130 @@
 ice 
-to continue running until it is explicitly stopped, so return sticky%0A        int startType = Service.START_STICKY;%0A%0A        if (running) %7B%0A            return startType;%0A        %7D%0A%0A        running = true;%0A
+after queued tasks are done if service was started%0A        // by device boot event and application is not actively running
 %0A   
@@ -970,18 +970,36 @@
         
-if
+terminateAfterDone =
  (starte
@@ -1061,18 +1061,18 @@
 eiver%22))
- %7B
+;%0A
 %0A       
@@ -1076,69 +1076,136 @@
     
-    startedByBoot = true;%0A        %7D%0A%0A        return startType
+// we want this service to continue running until it is explicitly stopped, so return sticky%0A        return Service.START_STICKY
 ;%0A  

simplify boot logic