Search in sources :

Example 1 with WakeLock

use of org.androidannotations.annotations.WakeLock in project androidannotations by androidannotations.

the class WakeLockHandler method process.

@Override
public void process(Element element, EComponentHolder holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    WakeLock annotation = executableElement.getAnnotation(WakeLock.class);
    String tag = extractTag(executableElement);
    Level level = annotation.level();
    Flag[] flags = annotation.flags();
    JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousMethodBody = codeModelHelper.removeBody(method);
    JBlock methodBody = method.body();
    IJExpression levelAndFlags = getClasses().POWER_MANAGER.staticRef(level.name());
    if (flags.length > 0) {
        for (Flag flag : flags) {
            levelAndFlags = levelAndFlags.bor(getClasses().POWER_MANAGER.staticRef(flag.name()));
        }
    }
    JInvocation newWakeLock = holder.getPowerManagerRef().invoke("newWakeLock").arg(levelAndFlags).arg(JExpr.lit(tag));
    JVar wakeLock = methodBody.decl(getClasses().WAKE_LOCK, "wakeLock", JExpr._null());
    JTryBlock tryBlock = methodBody._try();
    tryBlock.body().assign(wakeLock, newWakeLock);
    tryBlock.body().add(wakeLock.invoke("acquire"));
    tryBlock.body().add(previousMethodBody);
    JBlock finallyBlock = tryBlock._finally();
    JConditional ifStatement = finallyBlock._if(wakeLock.ne(JExpr._null()));
    ifStatement._then().add(wakeLock.invoke("release"));
}
Also used : WakeLock(org.androidannotations.annotations.WakeLock) ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) Flag(org.androidannotations.annotations.WakeLock.Flag) JBlock(com.helger.jcodemodel.JBlock) Level(org.androidannotations.annotations.WakeLock.Level) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JVar(com.helger.jcodemodel.JVar)

Example 2 with WakeLock

use of org.androidannotations.annotations.WakeLock in project androidannotations by androidannotations.

the class WakeLockHandler method extractTag.

private String extractTag(Element element) {
    WakeLock annotation = element.getAnnotation(WakeLock.class);
    String tag = annotation.tag();
    if (WakeLock.DEFAULT_TAG.equals(tag)) {
        tag = element.getEnclosingElement().getSimpleName().toString() + "." + element.getSimpleName().toString();
    }
    return tag;
}
Also used : WakeLock(org.androidannotations.annotations.WakeLock)

Aggregations

WakeLock (org.androidannotations.annotations.WakeLock)2 IJExpression (com.helger.jcodemodel.IJExpression)1 JBlock (com.helger.jcodemodel.JBlock)1 JConditional (com.helger.jcodemodel.JConditional)1 JInvocation (com.helger.jcodemodel.JInvocation)1 JMethod (com.helger.jcodemodel.JMethod)1 JTryBlock (com.helger.jcodemodel.JTryBlock)1 JVar (com.helger.jcodemodel.JVar)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 Flag (org.androidannotations.annotations.WakeLock.Flag)1 Level (org.androidannotations.annotations.WakeLock.Level)1