package com.example.accelerometre01;


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.hardware.SensorEvent;

public class service02 extends Service
{
	
	private SensorManager SensorManager;
    private Sensor Accelerometer;
   
    public static int CURRENT_SETP = 0;

	public static float SENSITIVITY = 0;   

	private float mLastValues[] = new float[3 * 2];
	private float mScale[] = new float[2];
	private float mYOffset;
	private static long end = 0;
	private static long start = 0;

	
	private float mLastDirections[] = new float[3 * 2];
	private float mLastExtremes[][] = { new float[3 * 2], new float[3 * 2] };
	private float mLastDiff[] = new float[3 * 2];
	private int mLastMatch = -1;
	  
	@Override
	public IBinder onBind(Intent intent)
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	public void onCreate()
	{
	   
	      SensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
	      Accelerometer = SensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	      
	      SensorManager.registerListener(mySensorEventListener , Accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
	      
	        
        //  http://androidexample.com/Accelerometer_Basic_Example_-_Detect_Phone_Shake_Motion/index.php?view=article_discription&aid=109/
	      // https://github.com/bagilevi/android-pedometer/blob/master/src/name/bagi/levente/pedometer/StepDetector.java
     
	      //toto
	      
	      	int h = 480;
			mYOffset = h * 0.5f;
			mScale[0] = -(h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
			mScale[1] = -(h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
			//if (SettingsActivity.sharedPreferences == null)
			//{
			//	SettingsActivity.sharedPreferences = context.getSharedPreferences(SettingsActivity.SETP_SHARED_PREFERENCES,Context.MODE_PRIVATE);
			//}
			//SENSITIVITY = SettingsActivity.sharedPreferences.getInt(SettingsActivity.SENSITIVITY_VALUE, 3);
	      
			SENSITIVITY = 3;
		      
			
	}
	
	private SensorEventListener mySensorEventListener = new SensorEventListener() 
	{

		@Override
		public void onSensorChanged(SensorEvent event) 
		{
			
				if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
				{
		            return;
				}
			 
				float vSum = 0;
				for (int i = 0; i < 3; i++) 
				{
					final float v = mYOffset + event.values[i] * mScale[1]; // j
					vSum += v;
				}
				
				int k = 0;
				float v = vSum / 3;

				float direction = (v > mLastValues[k] ? 1: (v < mLastValues[k] ? -1 : 0));
				if (direction == -mLastDirections[k]) 
				{
					// Direction changed
					int extType = (direction > 0 ? 0 : 1); // minumum or
					// maximum?
					mLastExtremes[extType][k] = mLastValues[k];
					float diff = Math.abs(mLastExtremes[extType][k]- mLastExtremes[1 - extType][k]);

					if (diff > SENSITIVITY) 
					{
							boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k] * 2 / 3);
							boolean isPreviousLargeEnough = mLastDiff[k] > (diff / 3);
							boolean isNotContra = (mLastMatch != 1 - extType);

							if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough && isNotContra) 
							{
							
								end = System.currentTimeMillis();
								if (end - start > 500) 
								{
									Log.i("StepDetector", "CURRENT_SETP:"+ CURRENT_SETP);
									CURRENT_SETP++;
									mLastMatch = extType;
									start = end;
								}
							} 
							
							else 
							{
								mLastMatch = -1;
							}
					}
					
					mLastDiff[k] = diff;
				}
				
				mLastDirections[k] = direction;
				mLastValues[k] = v;
		   
			//Intent i = new Intent("refresh_event_accelerometre");
			//int toto = 0;
        	//i.putExtra("idstep", numSteps);
            //sendBroadcast(i);

			
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
			
		}
		
	};
	
	
	
	@Override
	public void onDestroy() 
	{
        super.onDestroy();
        if (SensorManager != null) {
        	SensorManager.unregisterListener(mySensorEventListener);
        }
    }
	
}





