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 Compat COMPAT;
    static {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            COMPAT = new FroyoCompat();
        } else {
            COMPAT = new HoneycombCompat();
        }
    }

    public static final Executor SERIAL_EXECUTOR = COMPAT.serialExecutor();

    public static void execute(@NonNull final Runnable task) {
        COMPAT.execute(task);
    }

    interface Compat {
        void execute(@NonNull Runnable task);

        @NonNull
        Executor serialExecutor();
    }

    static class FroyoCompat implements Compat {
        private Executor mExecutor;

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

            return mExecutor;
        }

        @Override
        public void execute(@NonNull final Runnable task) {
            getExecutor().execute(task);
        }

        @NonNull
        @Override
        public Executor serialExecutor() {
            return getExecutor();
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    static class HoneycombCompat extends FroyoCompat {
        @Override
        public void execute(@NonNull Runnable task) {
            AsyncTask.execute(task);
        }

        @NonNull
        @Override
        public Executor serialExecutor() {
            return AsyncTask.SERIAL_EXECUTOR;
        }
    }
}

@@ -513,21 +513,27 @@
 T = new 
-Froyo
+Gingerbread
 Compat()
@@ -935,21 +935,27 @@
 c class 
-Froyo
+Gingerbread
 Compat i
@@ -1031,60 +1031,8 @@
 ull%0A
-        @TargetApi(Build.VERSION_CODES.GINGERBREAD)%0A
     
@@ -1359,286 +1359,70 @@
     
-if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.GINGERBREAD) %7B%0A                        ((ThreadPoolExecutor) mExecutor).allowCoreThreadTimeOut(true);%0A                    %7D else %7B%0A                        ((ThreadPoolExecutor) mExecutor).setCorePoolSize(0);%0A                    %7D
+((ThreadPoolExecutor) mExecutor).allowCoreThreadTimeOut(true);
 %0A   
@@ -1842,13 +1842,19 @@
 nds 
-Froyo
+Gingerbread
 Comp

remove some Froyo compat logic that is no longer needed