Search in sources :

Example 1 with HasActivityLifecycleMethods

use of org.androidannotations.holder.HasActivityLifecycleMethods 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)

Aggregations

IJExpression (com.helger.jcodemodel.IJExpression)1 JBlock (com.helger.jcodemodel.JBlock)1 EFragmentHolder (org.androidannotations.holder.EFragmentHolder)1 HasActivityLifecycleMethods (org.androidannotations.holder.HasActivityLifecycleMethods)1