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

android.view.HasContext Maven / Gradle / Ivy

package android.view;

import java.lang.reflect.Method;

import org.ioc.commons.flowcontrol.actioncontroller.IsExternalAction;
import org.ioc.commons.flowcontrol.eventbus.IsEvent;
import org.ioc.commons.flowcontrol.operationmanager.IsOperation;
import org.ioc.commons.impl.android.integration.service.AndroidRequestManagerException;
import org.ioc.commons.ui.HasDisplay;
import org.ioc.commons.ui.IsDisplay;

import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.content.Context;

public interface HasContext {
	Context getContext();

	public static class Helper {

		public static Context getFromObject(Object obj) {
			return getFromObject(obj, null);
		}

		public static Context getFromObject(Object isOrHasContext, Method callerMethod) {
			Context context = null;
			if (isOrHasContext instanceof Application) {
				context = (Application) isOrHasContext;
			} else if (isOrHasContext instanceof Activity) {
				context = (Activity) isOrHasContext;
			} else if (isOrHasContext instanceof android.app.Service) {
				context = (android.app.Service) isOrHasContext;
			} else if (isOrHasContext instanceof Fragment) {
				context = ((Fragment) isOrHasContext).getActivity();

				if (context == null) {
					throw new AndroidRequestManagerException(
							"Fragment activity cannot be null when you are invoking JAPTSY services."
									+ (callerMethod != null ? " Invoked from " + callerMethod : ""));
				}

			} else if (isOrHasContext instanceof android.support.v4.app.Fragment) {
				context = ((android.support.v4.app.Fragment) isOrHasContext).getActivity();

				if (context == null) {
					throw new AndroidRequestManagerException(
							"Fragment activity cannot be null when you are invoking JAPTSY services."
									+ (callerMethod != null ? " Invoked from " + callerMethod : ""));
				}
			} else if (isOrHasContext instanceof HasDisplay) {
				IsDisplay display = ((HasDisplay) isOrHasContext).getDisplay();

				if (display != isOrHasContext) {
					context = getFromObject(display, callerMethod);
				}

			} else if (isOrHasContext instanceof HasContext) {
				HasContext contextProvider = (HasContext) isOrHasContext;
				context = contextProvider.getContext();
			} else if (isOrHasContext instanceof View) {
				context = ((View) isOrHasContext).getContext();
			} else if (isOrHasContext instanceof IsDisplay) {
				@SuppressWarnings("unchecked")
				Object nativeWidget = ((IsDisplay, ? extends Enum, ? extends Enum>) isOrHasContext)
						.asWidget();

				if (nativeWidget == null) {
					throw new AndroidRequestManagerException("Got an empty native widget from display " + nativeWidget
							+ "." + (callerMethod != null ? " Invoked from " + callerMethod : ""));
				}

				if (nativeWidget != isOrHasContext) {
					context = getFromObject(nativeWidget, callerMethod);
				}
			}

			return context;
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy