Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface Background {
long delay() default 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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);

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,6 +39,11 @@ void emptyUiMethod() {
@Background
void emptyBackgroundMethod() {

}

@Background(delay = 1000)
void emptyDelayedBackgroundMethod() {

}

@UiThread
Expand Down