2323import javax .lang .model .element .ExecutableElement ;
2424
2525import org .androidannotations .annotations .UiThread ;
26+ import org .androidannotations .api .UiThreadExecutor ;
2627import org .androidannotations .helper .APTCodeModelHelper ;
2728import org .androidannotations .holder .EComponentHolder ;
29+ import org .androidannotations .model .AnnotationElements ;
30+ import org .androidannotations .process .IsValid ;
2831
2932import com .sun .codemodel .JBlock ;
3033import com .sun .codemodel .JClass ;
3437import com .sun .codemodel .JExpression ;
3538import com .sun .codemodel .JMethod ;
3639import com .sun .codemodel .JOp ;
40+ import com .sun .codemodel .JVar ;
3741
3842public class UiThreadHandler extends AbstractRunnableHandler {
3943
4044 private static final String METHOD_CUR_THREAD = "currentThread" ;
4145 private static final String METHOD_MAIN_LOOPER = "getMainLooper" ;
4246 private static final String METHOD_GET_THREAD = "getThread" ;
47+ private static final String METHOD_ADD_TASK = "addTask" ;
4348
4449 private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper ();
4550
4651 public UiThreadHandler (ProcessingEnvironment processingEnvironment ) {
4752 super (UiThread .class , processingEnvironment );
4853 }
4954
55+ @ Override
56+ public void validate (Element element , AnnotationElements validatedElements , IsValid valid ) {
57+ super .validate (element , validatedElements , valid );
58+
59+ validatorHelper .usesEnqueueIfHasId (element , valid );
60+ }
61+
5062 @ Override
5163 public void process (Element element , EComponentHolder holder ) throws Exception {
5264 ExecutableElement executableElement = (ExecutableElement ) element ;
@@ -58,15 +70,17 @@ public void process(Element element, EComponentHolder holder) throws Exception {
5870 long delay = annotation .delay ();
5971 UiThread .Propagation propagation = annotation .propagation ();
6072
73+ JExpression runnable = createAndStoreRunnable (delegatingMethod .body (), annotation .id (), anonymousRunnableClass , holder );
74+
6175 if (delay == 0 ) {
6276 if (propagation == UiThread .Propagation .REUSE ) {
6377 // Put in the check for the UI thread.
6478 addUIThreadCheck (delegatingMethod , previousBody , holder );
6579 }
6680
67- delegatingMethod .body ().invoke (holder .getHandler (), "post" ).arg (_new ( anonymousRunnableClass ) );
81+ delegatingMethod .body ().invoke (holder .getHandler (), "post" ).arg (runnable );
6882 } else {
69- delegatingMethod .body ().invoke (holder .getHandler (), "postDelayed" ).arg (_new ( anonymousRunnableClass ) ).arg (lit (delay ));
83+ delegatingMethod .body ().invoke (holder .getHandler (), "postDelayed" ).arg (runnable ).arg (lit (delay ));
7084 }
7185 }
7286
@@ -91,4 +105,15 @@ private void addUIThreadCheck(JMethod delegatingMethod, JBlock previousBody, ECo
91105 JBlock thenBlock = con ._then ().add (previousBody );
92106 thenBlock ._return ();
93107 }
108+
109+ private JExpression createAndStoreRunnable (JBlock body , String id , JDefinedClass anonymousRunnableClass , EComponentHolder holder ) {
110+ if ("" .equals (id )) {
111+ return _new (anonymousRunnableClass );
112+ }
113+
114+ JVar runnableVar = body .decl (refClass (Runnable .class ), "runnable" , _new (anonymousRunnableClass ));
115+ body .add (refClass (UiThreadExecutor .class ).staticInvoke (METHOD_ADD_TASK ).arg (id ).arg (runnableVar ).arg (holder .getHandler ()));
116+
117+ return runnableVar ;
118+ }
94119}
0 commit comments