Search in sources :

Example 1 with EFragmentHolder

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

the class ReceiverHandler method registerAndUnregisterReceiver.

private void registerAndUnregisterReceiver(HasReceiverRegistration holder, Receiver.RegisterAt registerAt, JFieldVar intentFilterField, JFieldVar receiverField, boolean local) {
    JBlock registerBlock = null;
    JBlock unregisterBlock = null;
    if (holder instanceof HasActivityLifecycleMethods) {
        HasActivityLifecycleMethods activityLifecycleMethods = (HasActivityLifecycleMethods) holder;
        switch(registerAt) {
            case OnCreateOnDestroy:
                registerBlock = activityLifecycleMethods.getOnCreateAfterSuperBlock();
                unregisterBlock = activityLifecycleMethods.getOnDestroyBeforeSuperBlock();
                break;
            case OnStartOnStop:
                registerBlock = activityLifecycleMethods.getOnStartAfterSuperBlock();
                unregisterBlock = activityLifecycleMethods.getOnStopBeforeSuperBlock();
                break;
            case OnResumeOnPause:
                registerBlock = activityLifecycleMethods.getOnResumeAfterSuperBlock();
                unregisterBlock = activityLifecycleMethods.getOnPauseBeforeSuperBlock();
                break;
        }
        if (holder instanceof EFragmentHolder && registerAt == Receiver.RegisterAt.OnAttachOnDetach) {
            EFragmentHolder fragmentHolder = (EFragmentHolder) holder;
            registerBlock = fragmentHolder.getOnAttachAfterSuperBlock();
            unregisterBlock = fragmentHolder.getOnDetachBeforeSuperBlock();
        }
    } else {
        registerBlock = holder.getStartLifecycleAfterSuperBlock();
        unregisterBlock = holder.getEndLifecycleBeforeSuperBlock();
    }
    IJExpression broadcastManager;
    if (local) {
        if (getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.LOCAL_BROADCAST_MANAGER) == null) {
            broadcastManager = getClasses().ANDROIDX_LOCAL_BROADCAST_MANAGER.staticInvoke("getInstance").arg(holder.getContextRef());
        } else {
            broadcastManager = getClasses().LOCAL_BROADCAST_MANAGER.staticInvoke("getInstance").arg(holder.getContextRef());
        }
    } else {
        broadcastManager = holder.getContextRef();
    }
    registerBlock.invoke(broadcastManager, "registerReceiver").arg(receiverField).arg(intentFilterField);
    unregisterBlock.invoke(broadcastManager, "unregisterReceiver").arg(receiverField);
}
Also used : HasActivityLifecycleMethods(org.androidannotations.holder.HasActivityLifecycleMethods) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) EFragmentHolder(org.androidannotations.holder.EFragmentHolder)

Example 2 with EFragmentHolder

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

the class AbstractFragmentByHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentWithViewSupportHolder holder, Element element, Element param) {
    TypeMirror elementType = param.asType();
    String typeQualifiedName = elementType.toString();
    TypeElement nativeFragmentElement = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.FRAGMENT);
    boolean isNativeFragment = nativeFragmentElement != null && annotationHelper.isSubtype(elementType, nativeFragmentElement.asType());
    String fieldName = element.getSimpleName().toString();
    if (holder instanceof EFragmentHolder) {
        boolean childFragment = annotationHelper.extractAnnotationParameter(element, "childFragment");
        String fragmentManagerGetter = childFragment ? "getChildFragmentManager" : "getFragmentManager";
        targetBlock.add(fieldRef.assign(cast(getJClass(typeQualifiedName), invoke(fragmentManagerGetter).invoke(findFragmentMethodName).arg(getFragmentId(element, fieldName)))));
    } else {
        JMethod findFragmentMethod = getFindFragmentMethod(isNativeFragment, holder);
        targetBlock.add(fieldRef.assign(cast(getJClass(typeQualifiedName), invoke(findFragmentMethod).arg(getFragmentId(element, fieldName)))));
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) EFragmentHolder(org.androidannotations.holder.EFragmentHolder) JMethod(com.helger.jcodemodel.JMethod)

Aggregations

EFragmentHolder (org.androidannotations.holder.EFragmentHolder)2 IJExpression (com.helger.jcodemodel.IJExpression)1 JBlock (com.helger.jcodemodel.JBlock)1 JMethod (com.helger.jcodemodel.JMethod)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 HasActivityLifecycleMethods (org.androidannotations.holder.HasActivityLifecycleMethods)1