Search in sources :

Example 1 with SharedPref

use of org.androidannotations.annotations.sharedpreferences.SharedPref in project androidannotations by androidannotations.

the class SharedPrefHandler method validate.

@Override
public void validate(Element element, ElementValidation validation) {
    super.validate(element, validation);
    TypeElement typeElement = (TypeElement) element;
    validatorHelper.isInterface(typeElement, validation);
    List<? extends Element> inheritedMembers = getProcessingEnvironment().getElementUtils().getAllMembers(typeElement);
    for (Element memberElement : inheritedMembers) {
        if (!memberElement.getEnclosingElement().asType().toString().equals("java.lang.Object")) {
            coreValidatorHelper.isPrefMethod(memberElement, validation);
            DefaultPrefInfo<?> info;
            IdValidatorHelper defaultAnnotationValidatorHelper = null;
            if (validation.isValid()) {
                info = DEFAULT_PREF_INFOS.get(((ExecutableElement) memberElement).getReturnType().toString());
                coreValidatorHelper.hasCorrectDefaultAnnotation((ExecutableElement) memberElement, validation);
                if (validation.isValid() && memberElement.getAnnotation(DefaultRes.class) != null) {
                    defaultAnnotationValidatorHelper = new IdValidatorHelper(new IdAnnotationHelper(getEnvironment(), DefaultRes.class.getName()));
                    defaultAnnotationValidatorHelper.resIdsExist(memberElement, info.resType, FallbackStrategy.USE_ELEMENT_NAME, validation);
                } else if (validation.isValid() && memberElement.getAnnotation(info.annotationClass) != null) {
                    defaultAnnotationValidatorHelper = new IdValidatorHelper(new IdAnnotationHelper(getEnvironment(), info.annotationClass.getName()));
                }
                if (validation.isValid() && defaultAnnotationValidatorHelper != null) {
                    defaultAnnotationValidatorHelper.annotationParameterIsOptionalValidResId(memberElement, IRClass.Res.STRING, "keyRes", validation);
                }
            }
        }
    }
    SharedPref sharedPrefAnnotation = element.getAnnotation(SharedPref.class);
    SharedPref.Scope scope = sharedPrefAnnotation.value();
    String name = sharedPrefAnnotation.name();
    boolean hasCustomName = !name.trim().isEmpty();
    EnumSet<SharedPref.Scope> allowedScopes = EnumSet.of(SharedPref.Scope.ACTIVITY, SharedPref.Scope.UNIQUE);
    if (hasCustomName && !allowedScopes.contains(scope)) {
        validation.addError("SharedPref#name() is only supported for Scope.ACTIVITY and Scope.UNIQUE.");
    }
}
Also used : IdValidatorHelper(org.androidannotations.helper.IdValidatorHelper) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString) IdAnnotationHelper(org.androidannotations.helper.IdAnnotationHelper) DefaultRes(org.androidannotations.annotations.sharedpreferences.DefaultRes) SharedPref(org.androidannotations.annotations.sharedpreferences.SharedPref)

Example 2 with SharedPref

use of org.androidannotations.annotations.sharedpreferences.SharedPref in project androidannotations by androidannotations.

the class SharedPrefHandler method generateConstructor.

private void generateConstructor(Element element, SharedPrefHolder holder) {
    SharedPref sharedPrefAnnotation = element.getAnnotation(SharedPref.class);
    SharedPref.Scope scope = sharedPrefAnnotation.value();
    String name = sharedPrefAnnotation.name();
    int mode = sharedPrefAnnotation.mode();
    if (name.trim().isEmpty()) {
        name = element.getSimpleName().toString();
    }
    JBlock constructorSuperBlock = holder.getConstructorSuperBlock();
    JVar contextParam = holder.getConstructorContextParam();
    switch(scope) {
        case ACTIVITY_DEFAULT:
            {
                JMethod getLocalClassName = getLocalClassName(holder);
                //
                constructorSuperBlock.invoke("super").arg(//
                contextParam.invoke("getSharedPreferences").arg(//
                invoke(getLocalClassName).arg(contextParam)).arg(lit(mode)));
                break;
            }
        case ACTIVITY:
            {
                JMethod getLocalClassName = getLocalClassName(holder);
                //
                constructorSuperBlock.invoke("super").arg(//
                contextParam.invoke("getSharedPreferences").arg(//
                invoke(getLocalClassName).arg(contextParam).plus(//
                lit("_" + name))).arg(lit(mode)));
                break;
            }
        case UNIQUE:
            {
                //
                constructorSuperBlock.invoke("super").arg(//
                contextParam.invoke("getSharedPreferences").arg(//
                lit(name)).arg(lit(mode)));
                break;
            }
        case APPLICATION_DEFAULT:
            {
                AbstractJClass preferenceManagerClass = getJClass("android.preference.PreferenceManager");
                //
                constructorSuperBlock.invoke("super").arg(//
                preferenceManagerClass.staticInvoke("getDefaultSharedPreferences").arg(contextParam));
                break;
            }
    }
}
Also used : JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString) JMethod(com.helger.jcodemodel.JMethod) SharedPref(org.androidannotations.annotations.sharedpreferences.SharedPref) JVar(com.helger.jcodemodel.JVar)

Aggregations

DefaultString (org.androidannotations.annotations.sharedpreferences.DefaultString)2 SharedPref (org.androidannotations.annotations.sharedpreferences.SharedPref)2 AbstractJClass (com.helger.jcodemodel.AbstractJClass)1 JBlock (com.helger.jcodemodel.JBlock)1 JMethod (com.helger.jcodemodel.JMethod)1 JVar (com.helger.jcodemodel.JVar)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 DefaultRes (org.androidannotations.annotations.sharedpreferences.DefaultRes)1 IdAnnotationHelper (org.androidannotations.helper.IdAnnotationHelper)1 IdValidatorHelper (org.androidannotations.helper.IdValidatorHelper)1