use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.
the class ServiceActionHandler method addActionInOnHandleIntent.
private void addActionInOnHandleIntent(EIntentServiceHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField) {
JMethod onHandleIntentMethod = holder.getOnHandleIntentMethod();
// If action match, call the method
JInvocation actionCondition = actionKeyField.invoke("equals").arg(holder.getOnHandleIntentIntentAction());
JBlock callActionBlock = holder.getOnHandleIntentBody()._if(actionCondition)._then();
JInvocation callActionInvocation = JExpr._super().invoke(methodName);
// For each method params, we get back value from extras and put it
// in super calls
List<? extends VariableElement> methodParameters = executableElement.getParameters();
if (methodParameters.size() > 0) {
// Extras
JVar intent = holder.getOnHandleIntentIntent();
JVar extras = callActionBlock.decl(getClasses().BUNDLE, "extras");
extras.init(intent.invoke("getExtras"));
callActionBlock = callActionBlock._if(extras.ne(_null()))._then();
// Extras params
for (VariableElement param : methodParameters) {
String paramName = param.getSimpleName().toString();
String extraParamName = paramName + "Extra";
JFieldVar paramVar = getStaticExtraField(holder, paramName);
AbstractJClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType());
BundleHelper bundleHelper = new BundleHelper(getEnvironment(), param.asType());
IJExpression getExtraExpression = bundleHelper.getExpressionToRestoreFromBundle(extraParamClass, extras, paramVar, onHandleIntentMethod);
JVar extraField = callActionBlock.decl(extraParamClass, extraParamName, getExtraExpression);
callActionInvocation.arg(extraField);
}
}
callActionBlock.add(callActionInvocation);
callActionBlock._return();
}
use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.
the class FragmentArgHandler method createBuilderInjectMethod.
public void createBuilderInjectMethod(EFragmentHolder holder, Element element, List<ArgHelper> argHelpers) {
JDefinedClass builderClass = holder.getBuilderClass();
JFieldRef builderArgsField = holder.getBuilderArgsField();
JMethod builderMethod = builderClass.method(PUBLIC, holder.narrow(builderClass), element.getSimpleName().toString());
String docComment = getProcessingEnvironment().getElementUtils().getDocComment(element);
codeModelHelper.addTrimmedDocComment(builderMethod, docComment);
for (ArgHelper argHelper : argHelpers) {
String fieldName = argHelper.param.getSimpleName().toString();
TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, argHelper.param);
BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
JFieldVar argKeyStaticField = getOrCreateStaticArgField(holder, argHelper.argKey, fieldName);
AbstractJClass paramClass = codeModelHelper.typeMirrorToJClass(actualType);
JVar arg = builderMethod.param(paramClass, fieldName);
builderMethod.body().add(bundleHelper.getExpressionToSaveFromField(builderArgsField, argKeyStaticField, arg));
builderMethod.javadoc().addParam(fieldName).append("value for this Fragment argument");
}
builderMethod.javadoc().addReturn().append("the FragmentBuilder to chain calls");
builderMethod.body()._return(_this());
}
use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.
the class FragmentArgHandler method assignValue.
@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EFragmentHolder holder, Element element, Element param) {
String fieldName = element.getSimpleName().toString();
String argKey = extractArgKey(element, fieldName);
if (element.getKind() != ElementKind.PARAMETER) {
createBuilderInjectionMethod(holder, element, new ArgHelper(param, argKey));
}
TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, param);
AbstractJClass elementClass = codeModelHelper.typeMirrorToJClass(actualType);
BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
JVar bundle = holder.getInjectBundleArgs();
JMethod injectExtrasMethod = holder.getInjectArgsMethod();
JFieldVar extraKeyStaticField = getOrCreateStaticArgField(holder, argKey, fieldName);
IJExpression restoreMethodCall = bundleHelper.getExpressionToRestoreFromBundle(elementClass, bundle, extraKeyStaticField, injectExtrasMethod);
JConditional conditional = targetBlock._if(JExpr.invoke(bundle, "containsKey").arg(extraKeyStaticField));
conditional._then().add(fieldRef.assign(restoreMethodCall));
}
use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.
the class ExtraHandler method assignValue.
@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EActivityHolder holder, Element element, Element param) {
String fieldName = element.getSimpleName().toString();
String extraKey = extractExtraKey(element, fieldName);
TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, param);
BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
JFieldVar extraKeyStaticField = getOrCreateStaticExtraField(holder, extraKey, fieldName);
if (element.getKind() != ElementKind.PARAMETER) {
holder.getIntentBuilder().getPutExtraMethod(element, new IntentBuilder.IntentExtra(param.asType(), fieldName, extraKeyStaticField));
}
AbstractJClass elementClass = codeModelHelper.typeMirrorToJClass(param.asType());
JMethod injectExtrasMethod = holder.getInjectExtrasMethod();
JVar extras = holder.getInjectExtras();
IJExpression restoreMethodCall = bundleHelper.getExpressionToRestoreFromBundle(elementClass, extras, extraKeyStaticField, injectExtrasMethod);
JBlock ifContainsKey = targetBlock._if(JExpr.invoke(extras, "containsKey").arg(extraKeyStaticField))._then();
ifContainsKey.assign(fieldRef, restoreMethodCall);
}
use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.
the class InstanceStateHandler method process.
@Override
public void process(Element element, HasInstanceState holder) {
AbstractJClass elementClass = codeModelHelper.typeMirrorToJClass(element.asType());
String fieldName = element.getSimpleName().toString();
JBlock saveStateBody = holder.getSaveStateMethodBody();
JVar saveStateBundleParam = holder.getSaveStateBundleParam();
JMethod restoreStateMethod = holder.getRestoreStateMethod();
JBlock restoreStateBody = holder.getRestoreStateMethodBody();
JVar restoreStateBundleParam = holder.getRestoreStateBundleParam();
TypeMirror type = codeModelHelper.getActualType(element, holder);
BundleHelper bundleHelper = new BundleHelper(getEnvironment(), type);
JFieldRef ref = ref(fieldName);
saveStateBody.add(bundleHelper.getExpressionToSaveFromField(saveStateBundleParam, JExpr.lit(fieldName), ref));
IJExpression restoreMethodCall = bundleHelper.getExpressionToRestoreFromBundle(elementClass, restoreStateBundleParam, JExpr.lit(fieldName), restoreStateMethod);
restoreStateBody.assign(ref, restoreMethodCall);
}
Aggregations