All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ioc.commons.impl.android.flowcontrol.activity.ViewActivity Maven / Gradle / Ivy

package org.ioc.commons.impl.android.flowcontrol.activity;

import java.util.HashMap;
import java.util.Map;

import org.ioc.commons.flowcontrol.actioncontroller.ActionController;
import org.ioc.commons.flowcontrol.actioncontroller.ActionControllerBinder;
import org.ioc.commons.flowcontrol.actioncontroller.IsExternalAction;
import org.ioc.commons.flowcontrol.eventbus.EventBus;
import org.ioc.commons.flowcontrol.eventbus.EventBusBinder;
import org.ioc.commons.flowcontrol.eventbus.IsEvent;
import org.ioc.commons.flowcontrol.operationmanager.IsOperation;
import org.ioc.commons.flowcontrol.operationmanager.OperationManager;
import org.ioc.commons.flowcontrol.operationmanager.OperationManagerBinder;
import org.ioc.commons.impl.android.flowcontrol.AndroidFlowController;
import org.ioc.commons.ui.HasStorage;
import org.ioc.commons.ui.IsView;
import org.ioc.commons.ui.IsViewPresenter;
import org.ioc.commons.ui.IsWidget;

import android.app.Activity;
import android.content.Context;
import android.view.HasContext;

public class ViewActivity, O extends Enum, A extends Enum, P extends IsViewPresenter>
		extends Activity implements IsView, HasStorage, HasContext {

	protected final AndroidFlowController flowController = new AndroidFlowController(this, false);
	protected final EventBus eventbus = flowController.eventBus();
	protected final OperationManager operationManager = flowController.operationManager();
	protected final ActionController actionController = flowController.actionController();
	private final Map storage = new HashMap();

	/**
	 * Determines if {@link #activate()} and {@link #deactivate()} methods will
	 * be automatically invoked when {@link #onLoad()} and {@link #onUnload()}
	 * methods are called. By default value is true.
	 */
	private boolean autoActivation = true;
	private boolean manuallyActive;

	protected P presenter;

	private boolean initialized;
	private boolean isRunning;

	@Override
	public Context getContext() {
		return this;
	}

	@Override
	protected void onStart() {
		super.onStart();

		this.isRunning = true;

		if (this.presenter != null && !this.initialized) {
			this.initialized = true;
			this.presenter.onInitDisplay();
		}
		
		this.flowController.onStart();
	}

	@Override
	public boolean isInitialized() {
		return initialized;
	}

	@Override
	public Object asWidget() {
		/*
		 * The activity as-is
		 */
		return this;
	}

	@Override
	public IsWidget getWidgetParent() {
		/*
		 * Activities do not have parents
		 */
		return null;
	}

	/**
	 * This method should be manually called to indicate view is active when
	 * {@link #autoActivation} is false.
	 */
	@Override
	public void activate() {
		this.manuallyActive = true;
		if (this.presenter != null) {
			this.presenter.onActive();
		}
	}

	/**
	 * This method should be manually called to indicate view is not active when
	 * {@link #autoActivation} is false.
	 */
	@Override
	public void deactivate() {
		this.manuallyActive = false;
		if (this.presenter != null) {
			this.presenter.onDeactive();
		}
	}

	@Override
	public boolean isActive() {
		return this.autoActivation ? this.isRunning : this.manuallyActive;
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.isRunning = true;

		this.flowController.onResume();

		if (this.autoActivation) {
			activate();
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		this.isRunning = false;

		if (this.autoActivation) {
			deactivate();
		}

		this.flowController.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		this.flowController.onDestroy();
	}

	@Override
	public EventBusBinder eventBus() {
		return this.eventbus;
	}

	@Override
	public OperationManagerBinder operationManager() {
		return this.operationManager;
	}

	@Override
	public ActionControllerBinder actionController() {
		return this.actionController;
	}

	public boolean isAutoActivation() {
		return autoActivation;
	}

	@Override
	public void store(Object key, Object data) {
		storage.put(key, data);
	}

	@Override
	public Object retrieve(Object key) {
		return storage.get(key);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy