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"));
}
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;
}
Aggregations