org.androidannotations.annotations.UiThread Maven / Gradle / Ivy
Show all versions of androidannotations-api Show documentation
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2018 the AndroidAnnotations project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.androidannotations.api.KotlinOpen;
/**
*
* Should be used on method that must be run in the Ui thread
*
*
* The annotated method MUST return void and MAY contain parameters.
*
*
* The generated code is based on a local {@link android.os.Handler} instance.
*
*
*
* Delay
*
* Sometimes you may want to delay execution of a Ui thread method. To do so,
* you should use the {@link #delay()} field.
*
* Example :
*
*
* @EBean
* public class MyBean {
*
* @UiThread(delay = 2000)
* void uiThreadTask() {
* // ...
* }
* }
*
*
*
*
* Execution flow
*
* Prior to 3.0, {@link UiThread} annotated method calls was always added in the
* handler execution queue to ensure that execution was done in Ui thread. In
* 3.0, we kept the same behavior for compatibility purpose.
*
*
* But, if you want to optimize UiThread calls, you may want to change
* {@link #propagation()} value to REUSE
. In this configuration,
* the code will make a direct call to the method if current thread is already
* Ui thread. If not, we're falling back to handler call.
*
* Example :
*
*
* @EBean
* public class MyBean {
*
* @UiThread
* void uiThreadTask() {
* // This code is executed via the handler
* // The following method will be directly executed instead of using
* // handler
* uiThreadTaskReused();
* }
*
* @UiThread(propagation = REUSE)
* void uiThreadTaskReused() {
* // ...
* }
* }
*
*
*
*
* Cancellation
*
* You can cancel UiThread tasks if you provide an id (which cannot be an empty
* string) with the {@link #id()} parameter. Please note tasks which use
* REUSE
{@link #propagation()} cannot have an id hence cannot be
* cancelled. To cancel all {@link UiThread} tasks with a given id, call
* {@link org.androidannotations.api.UiThreadExecutor#cancelAll(String)
* UiThreadExecutor#cancelAll(String)}.
*
*
* Example :
*
*
* @EBean
* public class MyBean {
*
* @UiThread(id = "myId")
* void uiThreadTask() {
* // do sg
* }
* }
*
* ...
*
* UiThreadExecutor.cancelAll("myId");
*
*
*
*
*
* @see Background
* @see android.os.Handler
* @see org.androidannotations.api.UiThreadExecutor#cancelAll(String)
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
@KotlinOpen
public @interface UiThread {
/**
* The delay of the execution in milliseconds.
*
* @return the delay of the execution
*/
long delay() default 0;
/**
* If propagation is {@link 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
* {@link Propagation#ENQUEUE}, which will always call the handler.
*
* When using a non-zero {@link #delay() delay} the propagation parameter is ignored.
*
* @return {@link Propagation#ENQUEUE} to always call the handler,
* {@link Propagation#REUSE}, to check whether it is already on the
* UI thread
*/
Propagation propagation() default Propagation.ENQUEUE;
/**
* Indicates the propagation behavior of the UiThread annotated method.
*/
enum Propagation {
/**
* The method will always call the Handler.
*/
ENQUEUE, //
/**
* The method will check first if it is inside the UI thread already.
*/
REUSE
}
/**
* Identifier for cancellation.
*
* To cancel all tasks having a specified id:
*
*
* UiThreadExecutor.cancelAll("my_background_id");
*
*
* @return the id for cancellation
*/
String id() default "";
}