Skip to content

Commit acd3972

Browse files
committed
added useDirect variable to UiThread annotation
1 parent 6b7d716 commit acd3972

2 files changed

Lines changed: 36 additions & 17 deletions

File tree

  • AndroidAnnotations

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,22 @@
2222

2323
/**
2424
* Should be used on method that must be run in the Ui thread
25-
*
25+
*
2626
* The annotation parameter delay is the delay (in milliseconds) until the
2727
* method will be executed. The default is 0 (no delay).
28-
*
28+
*
2929
*/
3030
@Retention(RetentionPolicy.CLASS)
3131
@Target(ElementType.METHOD)
3232
public @interface UiThread {
3333
long delay() default 0;
34+
35+
/**
36+
* If useDirect = true, the method will check first if it is inside the UI
37+
* thread already. If so, it will directly call the method instead of using
38+
* the handler.
39+
*
40+
* @return
41+
*/
42+
boolean useDirect() default false;
3443
}

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,34 @@ public String getTarget() {
5656
public void process(Element element, JCodeModel codeModel, EBeanHolder holder) throws JClassAlreadyExistsException {
5757

5858
ExecutableElement executableElement = (ExecutableElement) element;
59+
UiThread annotation = element.getAnnotation(UiThread.class);
60+
boolean useDirect = annotation.useDirect();
5961

6062
JMethod delegatingMethod = helper.overrideAnnotatedMethod(executableElement, holder);
63+
JBlock clonedBody = null;
6164

62-
//Need to clone the body to be put in the check. This is done here because the next line
63-
//will remove the body from the method.
64-
JBlock clonedBody = cloneMethodBody(delegatingMethod);
65+
if (useDirect) {
66+
// Need to clone the body to be put in the check. This is done here
67+
// because the next line
68+
// will remove the body from the method.
69+
clonedBody = cloneMethodBody(delegatingMethod);
70+
}
6571

6672
JDefinedClass anonymousRunnableClass = helper.createDelegatingAnonymousRunnableClass(holder, delegatingMethod);
6773

6874
{
69-
//Execute Runnable
70-
UiThread annotation = element.getAnnotation(UiThread.class);
75+
// Execute Runnable
7176
long delay = annotation.delay();
7277

7378
if (holder.handler == null) {
7479
JClass handlerClass = holder.classes().HANDLER;
7580
holder.handler = holder.generatedClass.field(JMod.PRIVATE, handlerClass, "handler_", JExpr._new(handlerClass));
7681
}
7782

78-
//Put in the check for the UI thread.
79-
addUIThreadCheck(delegatingMethod, clonedBody, codeModel);
83+
if (useDirect) {
84+
// Put in the check for the UI thread.
85+
addUIThreadCheck(delegatingMethod, clonedBody, codeModel);
86+
}
8087

8188
if (delay == 0) {
8289
delegatingMethod.body().invoke(holder.handler, "post").arg(_new(anonymousRunnableClass));
@@ -89,8 +96,9 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
8996

9097
/**
9198
* Clone a method body.
92-
*
93-
* @param method the method to clone.
99+
*
100+
* @param method
101+
* the method to clone.
94102
* @return A new JBlock containing the method body.
95103
*/
96104
private JBlock cloneMethodBody(JMethod method) {
@@ -105,26 +113,28 @@ private JBlock cloneMethodBody(JMethod method) {
105113

106114
/**
107115
* Add the pre-check to see if we are already in the UI thread.
108-
*
116+
*
109117
* @param delegatingMethod
110118
* @param codeModel
111119
* @throws JClassAlreadyExistsException
112120
*/
113121
private void addUIThreadCheck(JMethod delegatingMethod, JBlock clonedBody, JCodeModel codeModel) throws JClassAlreadyExistsException {
114-
//Get the Thread and Looper class.
122+
// Get the Thread and Looper class.
115123
JClass tClass = codeModel.ref(Thread.class);
116124
JClass lClass = codeModel.ref(Looper.class);
117125

118-
//invoke the methods.
126+
// invoke the methods.
119127
JExpression lhs = tClass.staticInvoke(METHOD_CUR_THREAD);
120128
JExpression rhs = lClass.staticInvoke(METHOD_MAIN_LOOPER).invoke(METHOD_GET_THREAD);
121129

122-
//create the conditional and the block.
130+
// create the conditional and the block.
123131
JConditional con = delegatingMethod.body()._if(JOp.eq(lhs, rhs));
124132
JBlock block = con._then();
125133

126-
//Put the cloned method body in the if() block, this is necessary because the
127-
//method may have parameters and a simple super.methodCall() won't work.
134+
// Put the cloned method body in the if() block, this is necessary
135+
// because the
136+
// method may have parameters and a simple super.methodCall() won't
137+
// work.
128138
for (Object statement : clonedBody.getContents()) {
129139
block.add((JStatement) statement);
130140
}

0 commit comments

Comments
 (0)