package net.kevxu.senselib;

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

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class StepDetector implements SensorEventListener {

	private final static String TAG = "StepDetector";

	private Context mContext;
	private SensorManager mSensorManager;
	private List<StepListener> mStepListeners;

	private Sensor mLinearAccelSensor;
	private Sensor mGravitySensor;

	protected interface StepListener {
		public void onStep();
	}

	protected StepDetector(Context context) throws SensorNotAvailableException {
		mContext = context;
		mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);

		List<Sensor> liearAccelSensors = mSensorManager.getSensorList(Sensor.TYPE_LINEAR_ACCELERATION);
		List<Sensor> gravitySensors = mSensorManager.getSensorList(Sensor.TYPE_GRAVITY);

		if (liearAccelSensors.size() == 0) {
			throw new SensorNotAvailableException("Linear Acceleration sensors not available.");
		} else {
			mLinearAccelSensor = liearAccelSensors.get(0);
		}

		if (gravitySensors.size() == 0) {
			throw new SensorNotAvailableException("Gravity sensors not available.");
		} else {
			mGravitySensor = gravitySensors.get(0);
		}

		mSensorManager.registerListener(this, mLinearAccelSensor, SensorManager.SENSOR_DELAY_GAME);
		mSensorManager.registerListener(this, mGravitySensor, SensorManager.SENSOR_DELAY_GAME);

		mStepListeners = new ArrayList<StepListener>();
	}

	protected void addListener(StepListener stepListener) {
		mStepListeners.add(stepListener);
	}

	protected void removeListeners() {
		mStepListeners.clear();
	}

	protected void close() {
		mSensorManager.unregisterListener(this);
	}

	protected void reload() {
		mSensorManager.registerListener(this, mLinearAccelSensor, SensorManager.SENSOR_DELAY_GAME);
		mSensorManager.registerListener(this, mGravitySensor, SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		Sensor sensor = event.sensor;
		if (sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {

		} else if (sensor.getType() == Sensor.TYPE_GRAVITY) {

		}
	}

}
@@ -694,16 +694,148 @@
 ption %7B%0A
+%09%09this(context, null);%0A%09%7D%0A%0A%09protected StepDetector(Context context, StepListener stepListener) throws SensorNotAvailableException %7B%0A
 %09%09mConte
@@ -1715,16 +1715,88 @@
 ener%3E();
+%0A%0A%09%09if (stepListener != null) %7B%0A%09%09%09mStepListeners.add(stepListener);%0A%09%09%7D
 %0A%09%7D%0A%0A%09pr
@@ -1845,24 +1845,53 @@
 Listener) %7B%0A
+%09%09if (stepListener != null)%0A%09
 %09%09mStepListe
@@ -1914,16 +1914,239 @@
 tener);%0A
+%09%09else%0A%09%09%09throw new NullPointerException(%22stepListener can not be null.%22);%0A%09%7D%0A%0A%09protected void addListeners(List%3CStepListener%3E stepListeners) %7B%0A%09%09if (stepListeners.size() %3E 0) %7B%0A%09%09%09mStepListeners.addAll(stepListeners);%0A%09%09%7D%0A
 %09%7D%0A%0A%09pro

Add constructor with listeners.