Search in sources :

Example 1 with HasOnActivityResult

use of org.androidannotations.holder.HasOnActivityResult in project androidannotations by androidannotations.

the class OnActivityResultHandler method process.

@Override
public void process(Element element, HasOnActivityResult holder) throws Exception {
    String methodName = element.getSimpleName().toString();
    ExecutableElement executableElement = (ExecutableElement) element;
    List<? extends VariableElement> parameters = executableElement.getParameters();
    int requestCode = executableElement.getAnnotation(OnActivityResult.class).value();
    JBlock onResultBlock = holder.getOnActivityResultCaseBlock(requestCode).blockSimple();
    IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
    JInvocation onResultInvocation = JExpr.invoke(activityRef, methodName);
    JVar intent = holder.getOnActivityResultDataParam();
    JVar extras = null;
    for (VariableElement parameter : parameters) {
        TypeMirror parameterType = parameter.asType();
        if (parameter.getAnnotation(OnActivityResult.Extra.class) != null) {
            if (extras == null) {
                extras = onResultBlock.decl(getClasses().BUNDLE, "extras_", JOp.cond(intent.ne(_null()).cand(intent.invoke("getExtras").ne(_null())), intent.invoke("getExtras"), _new(getClasses().BUNDLE)));
            }
            IJExpression extraParameter = extraHandler.getExtraValue(parameter, extras, onResultBlock, holder);
            onResultInvocation.arg(extraParameter);
        } else if (CanonicalNameConstants.INTENT.equals(parameterType.toString())) {
            onResultInvocation.arg(intent);
        } else if (//
        parameterType.getKind().equals(TypeKind.INT) || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
            onResultInvocation.arg(holder.getOnActivityResultResultCodeParam());
        }
    }
    onResultBlock.add(onResultInvocation);
}
Also used : HasOnActivityResult(org.androidannotations.holder.HasOnActivityResult) OnActivityResult(org.androidannotations.annotations.OnActivityResult) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Aggregations

IJExpression (com.helger.jcodemodel.IJExpression)1 JBlock (com.helger.jcodemodel.JBlock)1 JInvocation (com.helger.jcodemodel.JInvocation)1 JVar (com.helger.jcodemodel.JVar)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 VariableElement (javax.lang.model.element.VariableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 OnActivityResult (org.androidannotations.annotations.OnActivityResult)1 HasOnActivityResult (org.androidannotations.holder.HasOnActivityResult)1