Skip to content

Commit 044017c

Browse files
author
Yoann Delouis
committed
Add delay in @background
1 parent bd8cd24 commit 044017c

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

‎AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/Background.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
@Retention(RetentionPolicy.CLASS)
2929
@Target(ElementType.METHOD)
3030
public @interface Background {
31+
long delay() default 0;
3132
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,19 @@ public String getIdStringFromIdFieldRef(JFieldRef idRef) {
228228
throw new IllegalStateException("Unable to extract target name from JFieldRef");
229229
}
230230

231+
public JDefinedClass createAnonymousRunnableClass(EBeanHolder holder, JStatement block) {
232+
JCodeModel codeModel = holder.codeModel();
233+
JDefinedClass anonymousRunnableClass = codeModel.anonymousClass(Runnable.class);
234+
235+
JMethod runMethod = anonymousRunnableClass.method(JMod.PUBLIC, codeModel.VOID, "run");
236+
runMethod.annotate(Override.class);
237+
238+
JBlock runMethodBody = runMethod.body();
239+
runMethodBody.add(block);
240+
241+
return anonymousRunnableClass;
242+
}
243+
231244
public JDefinedClass createDelegatingAnonymousRunnableClass(EBeanHolder holder, JMethod delegatedMethod) {
232245

233246
JCodeModel codeModel = holder.codeModel();

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/BackgroundProcessor.java‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.androidannotations.processing;
1717

18+
import static com.sun.codemodel.JExpr._new;
19+
import static com.sun.codemodel.JExpr.lit;
20+
21+
import java.lang.annotation.Annotation;
1822
import javax.lang.model.element.Element;
1923
import javax.lang.model.element.ExecutableElement;
2024

@@ -29,6 +33,7 @@
2933
import com.sun.codemodel.JExpr;
3034
import com.sun.codemodel.JInvocation;
3135
import com.sun.codemodel.JMethod;
36+
import com.sun.codemodel.JMod;
3237

3338
public class BackgroundProcessor implements DecoratingElementProcessor {
3439

@@ -52,13 +57,24 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
5257

5358
{
5459
// Execute Runnable
55-
JClass backgroundExecutorClass = holder.refClass(BackgroundExecutor.class);
60+
Background annotation = element.getAnnotation(Background.class);
61+
long delay = annotation.delay();
5662

57-
JInvocation executeCall = backgroundExecutorClass.staticInvoke("execute").arg(JExpr._new(anonymousRunnableClass));
63+
JClass backgroundExecutorClass = holder.refClass(BackgroundExecutor.class);
64+
JInvocation executeCall = backgroundExecutorClass.staticInvoke("execute").arg(_new(anonymousRunnableClass));
65+
66+
if (delay == 0) {
67+
delegatingMethod.body().add(executeCall);
68+
} else {
69+
JDefinedClass anonymousExecutorRunnableClass = helper.createAnonymousRunnableClass(holder, executeCall);
70+
if (holder.handler == null) {
71+
JClass handlerClass = holder.classes().HANDLER;
72+
holder.handler = holder.generatedClass.field(JMod.PRIVATE, handlerClass, "handler_", _new(handlerClass));
73+
}
74+
delegatingMethod.body().invoke(holder.handler, "postDelayed").arg(_new(anonymousExecutorRunnableClass)).arg(lit(delay));
75+
}
5876

59-
delegatingMethod.body().add(executeCall);
6077
}
6178

6279
}
63-
6480
}

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/ThreadActivity.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.List;
1919
import java.util.Map;
2020
import java.util.Set;
21-
import java.util.SortedSet;
2221

2322
import org.androidannotations.annotations.Background;
2423
import org.androidannotations.annotations.EActivity;
@@ -40,6 +39,11 @@ void emptyUiMethod() {
4039
@Background
4140
void emptyBackgroundMethod() {
4241

42+
}
43+
44+
@Background(delay = 1000)
45+
void emptyDelayedBackgroundMethod() {
46+
4347
}
4448

4549
@UiThread

0 commit comments

Comments
 (0)