1818import java .lang .annotation .Annotation ;
1919import java .util .ArrayList ;
2020import java .util .List ;
21+ import java .util .concurrent .Executor ;
22+ import java .util .concurrent .Executors ;
2123
2224import javax .lang .model .element .Element ;
2325import javax .lang .model .element .ExecutableElement ;
2426import javax .lang .model .element .VariableElement ;
2527
2628import com .googlecode .androidannotations .annotations .Background ;
29+ import com .googlecode .androidannotations .helper .AndroidManifest ;
2730import com .sun .codemodel .JBlock ;
2831import com .sun .codemodel .JCatchBlock ;
2932import com .sun .codemodel .JClass ;
3235import com .sun .codemodel .JDefinedClass ;
3336import com .sun .codemodel .JExpr ;
3437import com .sun .codemodel .JExpression ;
38+ import com .sun .codemodel .JFieldVar ;
3539import com .sun .codemodel .JInvocation ;
3640import com .sun .codemodel .JMethod ;
3741import com .sun .codemodel .JMod ;
4044
4145public class BackgroundProcessor implements ElementProcessor {
4246
47+ private final AndroidManifest androidManifest ;
48+
49+ public BackgroundProcessor (AndroidManifest androidManifest ) {
50+ this .androidManifest = androidManifest ;
51+ }
52+
4353 @ Override
4454 public Class <? extends Annotation > getTarget () {
4555 return Background .class ;
@@ -50,6 +60,17 @@ public void process(Element element, JCodeModel codeModel, ActivitiesHolder acti
5060
5161 ActivityHolder holder = activitiesHolder .getEnclosingActivityHolder (element );
5262
63+ if (activitiesHolder .backgroundExecutor == null ) {
64+ activitiesHolder .backgroundExecutor = codeModel ._class (androidManifest .getApplicationPackage () + ".BackgroundExecutor_" );
65+
66+ JInvocation newCachedThreadPool = codeModel .ref (Executors .class ).staticInvoke ("newCachedThreadPool" );
67+
68+ JFieldVar executorStaticField = activitiesHolder .backgroundExecutor .field (JMod .STATIC | JMod .PRIVATE | JMod .FINAL , codeModel .ref (Executor .class ), "executor" , newCachedThreadPool );
69+ activitiesHolder .executorMethod = activitiesHolder .backgroundExecutor .method (JMod .PUBLIC | JMod .STATIC , codeModel .VOID , "execute" );
70+ JVar runnableParam = activitiesHolder .executorMethod .param (Runnable .class , "runnable" );
71+ activitiesHolder .executorMethod .body ().invoke (executorStaticField , "execute" ).arg (runnableParam );
72+ }
73+
5374 // Method
5475 String backgroundMethodName = element .getSimpleName ().toString ();
5576 JMethod backgroundMethod = holder .activity .method (JMod .PUBLIC , codeModel .VOID , backgroundMethodName );
@@ -65,16 +86,16 @@ public void process(Element element, JCodeModel codeModel, ActivitiesHolder acti
6586 parameters .add (param );
6687 }
6788
68- JDefinedClass anonymousThreadClass = codeModel .anonymousClass (Thread .class );
89+ JDefinedClass anonymousRunnableClass = codeModel .anonymousClass (Runnable .class );
6990
70- JMethod runMethod = anonymousThreadClass .method (JMod .PUBLIC , codeModel .VOID , "run" );
91+ JMethod runMethod = anonymousRunnableClass .method (JMod .PUBLIC , codeModel .VOID , "run" );
7192 runMethod .annotate (Override .class );
7293
7394 JBlock runMethodBody = runMethod .body ();
7495 JTryBlock runTry = runMethodBody ._try ();
75-
96+
7697 JExpression activitySuper = holder .activity .staticRef ("super" );
77-
98+
7899 JInvocation superCall = runTry .body ().invoke (activitySuper , backgroundMethod );
79100 for (JVar param : parameters ) {
80101 superCall .arg (param );
@@ -93,8 +114,9 @@ public void process(Element element, JCodeModel codeModel, ActivitiesHolder acti
93114 runCatch .body ().add (errorInvoke );
94115
95116 JBlock backgroundBody = backgroundMethod .body ();
96-
97- backgroundBody .add (JExpr ._new (anonymousThreadClass ).invoke ("start" ));
98-
117+
118+ JInvocation executeCall = activitiesHolder .backgroundExecutor .staticInvoke (activitiesHolder .executorMethod ).arg (JExpr ._new (anonymousRunnableClass ));
119+
120+ backgroundBody .add (executeCall );
99121 }
100122}
0 commit comments