package es.shyri.longtaskservice;

/**
 * Created by Shyri on 08/02/2016.
 */
public class LongTaskRunnable implements Runnable {
    LontTaskInterface listener;
    public enum STATUS{
        IDLE,
        STARTING,
        RUNNING,
        CANCELLING,
        END_SUCCESSFULLY,
        END_ERROR,
        END_CANCELED
    }

    STATUS currentStatus =  STATUS.IDLE;

    public LongTaskRunnable(LontTaskInterface listener) {
        this.listener = listener;
    }

    @Override
    public void run() {
        currentStatus = STATUS.STARTING;
        listener.onStatusUpdate(currentStatus);

        int i = 0;
        int percentage = 0;

        //////////////////////// Remain in Starting status for a while
        while(percentage < 100) {
            i++;
            if(i == 20000000) {
                percentage++;
                i = 0;
            }
        }
        ////////////////////////

        i = 0;
        percentage = 0;

        currentStatus = STATUS.RUNNING;
        listener.onStatusUpdate(currentStatus);

        //////////////////////// Start long task
        while(percentage < 100) {
            i++;
            if(i == 40000000) {
                percentage++;
                listener.onProgressUpdate(percentage);
                i = 0;
            }
        }
        ///////////////////////

        currentStatus = STATUS.END_SUCCESSFULLY;
        listener.onStatusUpdate(currentStatus);
    }

    public STATUS getCurrentStatus() {
        return currentStatus;
    }

    public interface LontTaskInterface {
        void onStatusUpdate(STATUS newStatus);
        void onProgressUpdate(int percentage);
    }
}

@@ -361,16 +361,49 @@
 US.IDLE;
+%0A    boolean isCancelled = false;
 %0A%0A    pu
@@ -529,24 +529,402 @@
 oid run() %7B%0A
+        //////////////////////// Remain in Starting status for a while%0A        doStartingStage();%0A        ////////////////////////%0A%0A        //////////////////////// Start long task%0A        doTaskStage();%0A        ///////////////////////%0A%0A        currentStatus = STATUS.END_SUCCESSFULLY;%0A        listener.onStatusUpdate(currentStatus);%0A    %7D%0A%0A    private void doStartingStage() %7B%0A
         curr
@@ -1051,80 +1051,8 @@
 = 0;
-%0A%0A        //////////////////////// Remain in Starting status for a while
 %0A   
@@ -1200,93 +1200,179 @@
     
-%7D%0A        %7D%0A        ////////////////////////%0A%0A        i = 0;
+    if(isCancelled)%7B%0A                    doCancellingStage();%0A                    return;%0A                %7D%0A            %7D%0A        %7D
 %0A    
+%7D%0A%0A
     p
-ercentage = 0;%0A
+rivate void doTaskStage() %7B
 %0A   
@@ -1469,48 +1469,46 @@
     
-//////////////////////// Start long task
+int i = 0;%0A        int percentage = 0;
 %0A   
@@ -1687,32 +1687,152 @@
          i = 0;%0A
+                if(isCancelled)%7B%0A                    doCancellingStage();%0A                    return;%0A                %7D%0A
             %7D%0A  
@@ -1847,35 +1847,348 @@
     
+%7D%0A%0A
     
-///////////////////////
+private void doCancellingStage() %7B%0A        currentStatus = STATUS.CANCELLING;%0A        listener.onStatusUpdate(currentStatus);%0A%0A        int i = 0;%0A        int percentage = 0;%0A        while(percentage %3C 100) %7B%0A            i++;%0A            if(i == 20000000) %7B%0A                percentage++;%0A                i = 0;%0A            %7D%0A        %7D
 %0A%0A  
@@ -2216,36 +2216,32 @@
  STATUS.END_
-SUCCESSFULLY
+CANCELED
 ;%0A        li
@@ -2353,32 +2353,94 @@
 tStatus;%0A    %7D%0A%0A
+    public void cancel() %7B%0A        isCancelled = true;%0A    %7D%0A%0A
     public inter

Update longtaskrunnable with cancel states