package net.kevxu.senselib;

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

import net.kevxu.senselib.StepDetector.StepListener;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationService implements LocationListener, StepListener {

	private static final String TAG = "LocationService";

	public static int LEVEL_GPS_NOT_ENABLED = 0;

	private Context mContext;
	private LocationManager mLocationManager;
	private List<LocationServiceListener> mLocationServiceListeners;

	private StepDetector mStepDetector;

	private int mServiceLevel;

	public interface LocationServiceListener {

		public void onServiceLevelChanged(int level);

	}

	public LocationService(Context context) throws SensorNotAvailableException {
		this(context, null);
	}

	public LocationService(Context context, LocationServiceListener locationServiceListener) throws SensorNotAvailableException {
		mContext = context;
		mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

		mLocationServiceListeners = new ArrayList<LocationServiceListener>();

		if (locationServiceListener != null) {
			mLocationServiceListeners.add(locationServiceListener);
		}

		mStepDetector = new StepDetector(mContext, this);
	}

	/**
	 * Call this when resume.
	 */
	public void start() {
		mStepDetector.start();
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0F, this);
	}

	/**
	 * Call this when pause.
	 */
	public void stop() {
		mStepDetector.stop();
		mLocationManager.removeUpdates(this);
	}

	public void addListener(LocationServiceListener locationServiceListener) {
		if (locationServiceListener != null) {
			mLocationServiceListeners.add(locationServiceListener);
		} else {
			throw new NullPointerException("LocationServiceListener is null.");
		}
	}

	public void removeListeners() {
		mLocationServiceListeners.clear();
	}

	private synchronized void setServiceLevel(int serviceLevel) {
		mServiceLevel = serviceLevel;
	}

	private synchronized void pushServiceLevel() {
		for (LocationServiceListener listener : mLocationServiceListeners) {
			listener.onServiceLevelChanged(mServiceLevel);
		}
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onProviderDisabled(String provider) {
		if (provider.equals(LocationManager.GPS_PROVIDER)) {
			setServiceLevel(LEVEL_GPS_NOT_ENABLED);
			pushServiceLevel();
		}
	}

	@Override
	public void onStep() {
		// TODO

	}

	@Override
	public void onMovement(float[] values) {
		// TODO Auto-generated method stub

	}

}

@@ -663,16 +663,25 @@
 private 
+volatile 
 int mSer
@@ -786,16 +786,69 @@
 evel);%0A%0A
+%09%09public void onLocationChanged(Location location);%0A%0A
 %09%7D%0A%0A%09pub
@@ -2133,18 +2133,21 @@
 el) %7B%0A%09%09
-mS
+if (s
 erviceLe
@@ -2154,11 +2154,13 @@
 vel 
+!
 = 
-s
+mS
 ervi
@@ -2170,46 +2170,32 @@
 evel
-;%0A%09%7D%0A%0A%09private synchronized void pushS
+) %7B%0A%09%09%09mServiceLevel = s
 ervi
@@ -2201,21 +2201,19 @@
 iceLevel
-() %7B%0A
+;%0A%09
 %09%09for (L
@@ -2278,16 +2278,17 @@
 s) %7B%0A%09%09%09
+%09
 listener
@@ -2326,16 +2326,21 @@
 Level);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A
@@ -2802,31 +2802,8 @@
 D);%0A
-%09%09%09pushServiceLevel();%0A
 %09%09%7D%0A

Set and push service level at the same time.