diff --git a/AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/Background.java b/AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/Background.java index 8724637341..323b489223 100644 --- a/AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/Background.java +++ b/AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/Background.java @@ -28,4 +28,5 @@ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Background { + long delay() default 0; } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/api/BackgroundExecutor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/api/BackgroundExecutor.java index c8c56c814b..35b90a96aa 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/api/BackgroundExecutor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/api/BackgroundExecutor.java @@ -17,17 +17,27 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class BackgroundExecutor { private static Executor executor = Executors.newCachedThreadPool(); + private static ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2 * Runtime.getRuntime().availableProcessors()); public static void execute(Runnable runnable) { executor.execute(runnable); } - + public static void setExecutor(Executor executor) { BackgroundExecutor.executor = executor; } + public static void executeDelayed(Runnable runnable, long delay) { + scheduledExecutor.schedule(runnable, delay, TimeUnit.MILLISECONDS); + } + + public static void setScheduledExecutor(ScheduledExecutorService scheduledExecutor) { + BackgroundExecutor.scheduledExecutor = scheduledExecutor; + } } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/BackgroundProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/BackgroundProcessor.java index dc70e3c8ed..9db4750e47 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/BackgroundProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/BackgroundProcessor.java @@ -15,6 +15,9 @@ */ package org.androidannotations.processing; +import static com.sun.codemodel.JExpr._new; +import static com.sun.codemodel.JExpr.lit; + import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; @@ -26,7 +29,6 @@ import com.sun.codemodel.JClassAlreadyExistsException; import com.sun.codemodel.JCodeModel; import com.sun.codemodel.JDefinedClass; -import com.sun.codemodel.JExpr; import com.sun.codemodel.JInvocation; import com.sun.codemodel.JMethod; @@ -52,13 +54,21 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t { // Execute Runnable + Background annotation = element.getAnnotation(Background.class); + long delay = annotation.delay(); + JClass backgroundExecutorClass = holder.refClass(BackgroundExecutor.class); + JInvocation executeCall; - JInvocation executeCall = backgroundExecutorClass.staticInvoke("execute").arg(JExpr._new(anonymousRunnableClass)); + if (delay == 0) { + executeCall = backgroundExecutorClass.staticInvoke("execute").arg(_new(anonymousRunnableClass)); + } else { + executeCall = backgroundExecutorClass.staticInvoke("executeDelayed").arg(_new(anonymousRunnableClass)).arg(lit(delay)); + } delegatingMethod.body().add(executeCall); + } } - } diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/ThreadActivity.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/ThreadActivity.java index a82376a2b1..83410dd0de 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/ThreadActivity.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/ThreadActivity.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.SortedSet; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.EActivity; @@ -40,6 +39,11 @@ void emptyUiMethod() { @Background void emptyBackgroundMethod() { + } + + @Background(delay = 1000) + void emptyDelayedBackgroundMethod() { + } @UiThread