package net.kevxu.senselib;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

/**
 * Manager class. Makes sure only one instance of LocationService, 
 * OrientationService and StepDetector is created. Use get*Instance() methods
 * to get an instance of the service you needed, register your listener, then
 * call start() to start the SenseLib. After SenseLib is no longer needed, 
 * remember to call stop().
 * 
 * @author Kaiwen Xu
 */
public class Sense {
	
	/**
	 * Orientation service.
	 */
	public static final int SERVICE_ORIENTATION = 0x1;
	
	/**
	 * Step detector service. Since it relies on orientation service, orientation
	 * service is implied.
	 */
	public static final int SERVICE_STEP_DETECTOR = 0x3;
	
	/**
	 * Location service. Since it relies on both orientation service and step
	 * detector service, both of them are implied.
	 */
	public static final int SERVICE_LOCATION = 0x7;
	
	/**
	 * All services available.
	 */
	public static final int SERVICE_ALL = 0xFFFFFFFF;

	private Context mContext;
	
	private List<SensorService> mServices;

	private OrientationService mOrientationService;
	private StepDetector mStepDetector;
	private LocationService mLocationService;
	
	public Sense(Context context) throws SensorNotAvailableException {
		this(context, SERVICE_ALL);
	}
	
	public Sense(Context context, int services) throws SensorNotAvailableException {
		mContext = context;
		mServices = new ArrayList<SensorService>();

		if ((services & SERVICE_ORIENTATION) == SERVICE_ORIENTATION) {
			mOrientationService = new OrientationService(mContext);
			mServices.add(mOrientationService);
		}
		
		if ((services & SERVICE_STEP_DETECTOR) == SERVICE_STEP_DETECTOR) {
			mStepDetector = new StepDetector(mContext, mOrientationService);
			mServices.add(mStepDetector);
		}
		
		if ((services & SERVICE_LOCATION) == SERVICE_LOCATION) {
			mLocationService = new LocationService(mContext, mStepDetector);
			mServices.add(mLocationService);
		}
	}

	public void start() {
		for (SensorService service : mServices) {
			if (service != null) {
				service.start();
			}
		}
	}

	public void stop() {
		for (SensorService service : mServices) {
			if (service != null) {
				service.stop();
			}
		}
	}

	public OrientationService getOrientationServiceInstance() {
		return mOrientationService;
	}

	public StepDetector getStepDetectorInstance() {
		return mStepDetector;
	}

	public LocationService getLocationServiceInstance() {
		return mLocationService;
	}

}

@@ -1229,109 +1229,896 @@
 %0A%09%0A%09
-public Sense(Context context) throws SensorNotAvailableException %7B%0A%09%09this(context, SERVICE_ALL);%0A%09%7D%0A%09
+/**%0A%09 * Constructor for SenseLib. Enable all services available.%0A%09 * %0A%09 * @param context Context.%0A%09 * @throws SensorNotAvailableException%0A%09 * %09%09%09%09If sensor required by a specific service is not present,%0A%09 * %09%09%09%09SensorNotAvailableException will be thrown. %0A%09 */%0A%09public Sense(Context context) throws SensorNotAvailableException %7B%0A%09%09this(context, SERVICE_ALL);%0A%09%7D%0A%09%0A%09/**%0A%09 * Constructor for SenseLib. Choose the services you want to enable.%0A%09 * %0A%09 * @param context Context.%0A%09 * @param services%0A%09 * %09%09%09%09Bitmask argument. Choose the services you want to enable.%0A%09 * %09%09%09%09For example, if you want to enable orientation service and%0A%09 * %09%09%09%09location service, you can pass the argument as%0A%09 * %09%09%09%09SERVICE_ORIENTATION%7CSERVICE_LOCATION.%0A%09 * @throws SensorNotAvailableException%0A%09 * %09%09%09%09If sensor required by a specific service is not present,%0A%09 * %09%09%09%09SensorNotAvailableException will be thrown. %0A%09 */
 %0A%09pu

Add javadocs.