Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 @@ -31,4 +31,18 @@
@Target(ElementType.METHOD)
public @interface UiThread {
long delay() default 0;

/**
* If propagation = REUSE, the method will check first if it is inside the
* UI thread already. If so, it will directly call the method instead of
* using the handler. The default value is ENQUEUE, which will always call
* the handler.
*
* @return
*/
Propagation propagation() default Propagation.ENQUEUE;

public enum Propagation {
ENQUEUE, REUSE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,29 @@
import javax.lang.model.element.ExecutableElement;

import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.UiThread.Propagation;
import org.androidannotations.helper.APTCodeModelHelper;

import android.os.Looper;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JConditional;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JOp;

public class UiThreadProcessor implements DecoratingElementProcessor {

private static final String METHOD_CUR_THREAD = "currentThread";
private static final String METHOD_MAIN_LOOPER = "getMainLooper";
private static final String METHOD_GET_THREAD = "getThread";

private final APTCodeModelHelper helper = new APTCodeModelHelper();

@Override
Expand All @@ -45,15 +56,15 @@ public String getTarget() {
public void process(Element element, JCodeModel codeModel, EBeanHolder holder) throws JClassAlreadyExistsException {

ExecutableElement executableElement = (ExecutableElement) element;
UiThread annotation = element.getAnnotation(UiThread.class);
Propagation propagation = annotation.propagation();

JMethod delegatingMethod = helper.overrideAnnotatedMethod(executableElement, holder);

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

{
// Execute Runnable

UiThread annotation = element.getAnnotation(UiThread.class);
long delay = annotation.delay();

if (holder.handler == null) {
Expand All @@ -62,6 +73,11 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
}

if (delay == 0) {
if (propagation == Propagation.REUSE) {
// Put in the check for the UI thread.
addUIThreadCheck(delegatingMethod, codeModel, holder);
}

delegatingMethod.body().invoke(holder.handler, "post").arg(_new(anonymousRunnableClass));
} else {
delegatingMethod.body().invoke(holder.handler, "postDelayed").arg(_new(anonymousRunnableClass)).arg(lit(delay));
Expand All @@ -70,4 +86,29 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t

}

/**
* Add the pre-check to see if we are already in the UI thread.
*
* @param delegatingMethod
* @param codeModel
* @param holder
* @throws JClassAlreadyExistsException
*/
private void addUIThreadCheck(JMethod delegatingMethod, JCodeModel codeModel, EBeanHolder holder) throws JClassAlreadyExistsException {
// Get the Thread and Looper class.
JClass tClass = codeModel.ref(Thread.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't codeModel.ref(<a class>) because it messed up sometimes.
Have a look to EBeansHolder class instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EBeansHolder.refClass(Class) just calls codeModel.ref(). Should I be using EBeansHolder.refClass(String) instead and use the String name of the class? Do you know what situations that the ref() method doesn't work in? It works in my application at least. Just want to make sure I am using it correctly.

JClass lClass = codeModel.ref(Looper.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will look into this.


// invoke the methods.
JExpression lhs = tClass.staticInvoke(METHOD_CUR_THREAD);
JExpression rhs = lClass.staticInvoke(METHOD_MAIN_LOOPER).invoke(METHOD_GET_THREAD);

// create the conditional and the block.
JConditional con = delegatingMethod.body()._if(JOp.eq(lhs, rhs));
JBlock thenBlock = con._then();

helper.callSuperMethod(delegatingMethod, holder, thenBlock);

thenBlock._return();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.