Skip to content
Browse files

Adding a delay in @UiThread, and setting @UiThreadDelayed as deprecat…

…ed. #97
  • Loading branch information...
1 parent bbe4e8c commit 7cce593051b80485d50dfeb71c69f5ccaecd9869 @pyricau pyricau committed Feb 16, 2012
View
4 ...roidannotations/src/main/java/com/googlecode/androidannotations/annotations/UiThread.java
@@ -23,8 +23,12 @@
/**
* Should be used on method that must be run in the Ui thread
*
+ * The annotation parameter delay is the delay (in milliseconds) until the
+ * method will be executed. The default is 0 (no delay).
+ *
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public @interface UiThread {
+ long delay() default 0;
}
View
5 ...otations/src/main/java/com/googlecode/androidannotations/annotations/UiThreadDelayed.java
@@ -21,12 +21,17 @@
import java.lang.annotation.Target;
/**
+ * <b>This annotation is deprecated. You should use {@link UiThread} with the delay parameter instead</b>
+ *
* Should be used on method that must be run in the Ui thread, after the
* specified amount of time elapses.
*
* The annotation value is the delay (in milliseconds) until the method will be
* executed.
+ *
+ *
*/
+@Deprecated
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public @interface UiThreadDelayed {
View
12 ...tations/src/main/java/com/googlecode/androidannotations/processing/UiThreadProcessor.java
@@ -15,6 +15,9 @@
*/
package com.googlecode.androidannotations.processing;
+import static com.sun.codemodel.JExpr._new;
+import static com.sun.codemodel.JExpr.lit;
+
import java.lang.annotation.Annotation;
import javax.lang.model.element.Element;
@@ -53,12 +56,19 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
{
// Execute Runnable
+ UiThread annotation = element.getAnnotation(UiThread.class);
+ long delay = annotation.delay();
+
if (holder.handler == null) {
JClass handlerClass = holder.refClass("android.os.Handler");
holder.handler = holder.eBean.field(JMod.PRIVATE, handlerClass, "handler_", JExpr._new(handlerClass));
}
- delegatingMethod.body().invoke(holder.handler, "post").arg(JExpr._new(anonymousRunnableClass));
+ if (delay == 0) {
+ delegatingMethod.body().invoke(holder.handler, "post").arg(_new(anonymousRunnableClass));
+ } else {
+ delegatingMethod.body().invoke(holder.handler, "postDelayed").arg(_new(anonymousRunnableClass)).arg(lit(delay));
+ }
}
}
View
7 ...ional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/ThreadActivity.java
@@ -24,7 +24,6 @@
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.UiThread;
-import com.googlecode.androidannotations.annotations.UiThreadDelayed;
@EActivity
public class ThreadActivity extends Activity {
@@ -49,7 +48,6 @@ void objectBackgroundMethod(Object param) {
}
-
@UiThread
void genericUiMethod(List<Map<String, List<Set<Void>>>> param) {
@@ -59,11 +57,10 @@ void genericUiMethod(List<Map<String, List<Set<Void>>>> param) {
void genericBackgroundMethod(List<Map<String, List<Set<Void>>>> param) {
}
-
- @UiThreadDelayed(1000)
+
+ @UiThread(delay = 1000)
void emptyUiDelayedMethod() {
}
-
}
View
29 ...ional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/TracedActivity.java
@@ -27,9 +27,8 @@
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.Trace;
import com.googlecode.androidannotations.annotations.UiThread;
-import com.googlecode.androidannotations.annotations.UiThreadDelayed;
-@EActivity
+@EActivity
public class TracedActivity extends Activity {
public boolean tracedMethodCalled = false;
@@ -41,7 +40,7 @@
public boolean voidTracedMethodInfoCalled = false;
public boolean overloadedMethodInt = false;
public boolean overloadedMethodIntFLoat = false;
-
+
@Trace
Object tracedMethod(List<Map<String, List<Set<Void>>>> param1, Void param2) throws IOException {
tracedMethodCalled = true;
@@ -53,27 +52,27 @@ void voidTracedMethod(List<Map<String, List<Set<Void>>>> param1, Void param2) th
voidTracedMethodCalled = true;
}
- @Trace(tag="TAGGED", level=Log.DEBUG)
+ @Trace(tag = "TAGGED", level = Log.DEBUG)
void voidTracedMethodDebug() {
voidTracedMethodDebugCalled = true;
}
- @Trace(level=Log.VERBOSE)
+ @Trace(level = Log.VERBOSE)
void voidTracedMethodVerbose() {
voidTracedMethodVerboseCalled = true;
}
- @Trace(level=Log.WARN)
+ @Trace(level = Log.WARN)
void voidTracedMethodWarn() {
voidTracedMethodWarnCalled = true;
}
- @Trace(level=Log.ERROR)
+ @Trace(level = Log.ERROR)
void voidTracedMethodError() {
voidTracedMethodErrorCalled = true;
}
- @Trace(level=Log.INFO)
+ @Trace(level = Log.INFO)
void voidTracedMethodInfo() {
voidTracedMethodInfoCalled = true;
}
@@ -87,22 +86,22 @@ void overloadedMethod(int x) {
void overloadedMethod(int x, float f) {
overloadedMethodIntFLoat = true;
}
-
+
@Trace
@UiThread
void mixedUiThreadMethod() {
-
+
}
-
+
@Trace
- @UiThreadDelayed(1000)
+ @UiThread(delay = 1000)
void mixedUiThreadDelayedMethod() {
-
+
}
-
+
@Trace
@Background
void mixedBackgroundMethod() {
-
+
}
}
View
3 ...-test-1-5/src/main/java/com/googlecode/androidannotations/test15/ebean/EnhancedClass.java
@@ -34,7 +34,6 @@
import com.googlecode.androidannotations.annotations.Trace;
import com.googlecode.androidannotations.annotations.Transactional;
import com.googlecode.androidannotations.annotations.UiThread;
-import com.googlecode.androidannotations.annotations.UiThreadDelayed;
import com.googlecode.androidannotations.annotations.ViewById;
import com.googlecode.androidannotations.annotations.res.StringRes;
import com.googlecode.androidannotations.test15.ThreadActivity;
@@ -84,7 +83,7 @@ void uiThread() {
}
- @UiThreadDelayed(2000)
+ @UiThread(delay = 2000)
@Trace
void uiThreadDelayed() {

0 comments on commit 7cce593

Please sign in to comment.
Something went wrong with that request. Please try again.