package org.ccci.gto.android.common.util;

import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.NonNull;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class AsyncTaskCompat {
    private static final Object LOCK_EXECUTOR = new Object();
    private static Executor EXECUTOR;

    @NonNull
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    private static Executor getExecutor() {
        synchronized (LOCK_EXECUTOR) {
            if (EXECUTOR == null) {
                EXECUTOR = Executors.newFixedThreadPool(1);
                if (EXECUTOR instanceof ThreadPoolExecutor) {
                    ((ThreadPoolExecutor) EXECUTOR).setKeepAliveTime(30, TimeUnit.SECONDS);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                        ((ThreadPoolExecutor) EXECUTOR).allowCoreThreadTimeOut(true);
                    } else {
                        ((ThreadPoolExecutor) EXECUTOR).setCorePoolSize(0);
                    }
                }
            }

            return EXECUTOR;
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void execute(@NonNull final Runnable task) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            getExecutor().execute(task);
        } else {
            AsyncTask.execute(task);
        }
    }
}

@@ -395,45 +395,614 @@
 nal 
-Object LOCK_EXECUTOR = new Object();%0A
+Compat COMPAT;%0A    static %7B%0A        if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.HONEYCOMB) %7B%0A            COMPAT = new FroyoCompat();%0A        %7D else %7B%0A            COMPAT = new HoneycombCompat();%0A        %7D%0A    %7D%0A%0A    public static void execute(@NonNull final Runnable task) %7B%0A        COMPAT.execute(task);%0A    %7D%0A%0A    @NonNull%0A    public static Executor SERIAL_EXECUTOR() %7B%0A        return COMPAT.SERIAL_EXECUTOR();%0A    %7D%0A%0A    interface Compat %7B%0A        void execute(@NonNull Runnable task);%0A%0A        @NonNull%0A        Executor SERIAL_EXECUTOR();%0A    %7D%0A%0A    static class FroyoCompat implements Compat %7B%0A    
     
@@ -1036,16 +1036,20 @@
 CUTOR;%0A%0A
+    
     @Non
@@ -1053,24 +1053,28 @@
 NonNull%0A    
+   
+ 
 @TargetApi(B
@@ -1105,16 +1105,20 @@
 RBREAD)%0A
+    
     priv
@@ -1120,32 +1120,45 @@
  private static 
+synchronized 
 Executor getExec
@@ -1170,47 +1170,8 @@
 ) %7B%0A
-        synchronized (LOCK_EXECUTOR) %7B%0A
     
@@ -1789,61 +1789,31 @@
   %7D%0A
+%0A
     
-%7D%0A%0A
     @
-TargetApi(Build.VERSION_CODES.HONEYCOMB)%0A
+Override%0A    
     
@@ -1811,39 +1811,32 @@
 %0A        public 
-static 
 void execute(@No
@@ -1876,124 +1876,479 @@
     
-if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.HONEYCOMB) %7B%0A            getExecutor().execute(task);%0A        %7D else
+    getExecutor().execute(task);%0A        %7D%0A%0A        @NonNull%0A        @Override%0A        public Executor SERIAL_EXECUTOR() %7B%0A            return getExecutor();%0A        %7D%0A    %7D%0A%0A    @TargetApi(Build.VERSION_CODES.HONEYCOMB)%0A    static class HoneycombCompat extends FroyoCompat %7B%0A        @Override%0A        public void execute(@NonNull Runnable task) %7B%0A            AsyncTask.execute(task);%0A        %7D%0A%0A        @NonNull%0A        @Override%0A        public Executor SERIAL_EXECUTOR()
  %7B%0A 
@@ -2350,32 +2350,39 @@
 ) %7B%0A            
+return 
 AsyncTask.execut
@@ -2371,37 +2371,39 @@
 n AsyncTask.
-execute(task)
+SERIAL_EXECUTOR
 ;%0A        %7D%0A

add gingerbread compatibility for SERIAL_EXECUTOR