package com.example.podo07;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

// http://nebomusic.net/androidlessons/Pedometer_Project.pdf

public class MainActivity extends Activity 
{
	
	TextView tv_X, tv_Y , tv_Z;
	TextView tv_sensitive;
	TextView tv_steps;
	
	Button btn_reset;
	
	SensorManager sensorManager;
	float acceleration;
	
	float previousY, currentY;
	int numSteps;
	
	SeekBar seekbar;
	int threshold;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tv_X = (TextView) this.findViewById(R.id.tv_X);
		tv_Y = (TextView) this.findViewById(R.id.tv_Y);
		tv_Z = (TextView) this.findViewById(R.id.tv_Z);
		
		tv_steps = (TextView) this.findViewById(R.id.tv_steps);
		tv_sensitive = (TextView) this.findViewById(R.id.tv_sensitive);

		btn_reset = (Button) this.findViewById(R.id.btn_reset);

		seekbar = (SeekBar) this.findViewById(R.id.seekBar);
		
		seekbar.setProgress(10);
		seekbar.setOnSeekBarChangeListener(seekBarListener);
		
		threshold = 10;
		tv_sensitive.setText(String.valueOf(threshold));
		
		previousY = 0;
		currentY = 0;
		numSteps = 0;
		
		acceleration = 0.00f;
		
		enable_accelerometer_listening();
		
		
		
		
	}

	
	private void enable_accelerometer_listening()
	{
	    sensorManager = (SensorManager)  getSystemService(Context.SENSOR_SERVICE);
	    sensorManager.registerListener( sensorEventListner , sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_NORMAL );      
	
		
	}
	
	
	private SensorEventListener sensorEventListner = new SensorEventListener()
	{
		@Override
		public void onSensorChanged(SensorEvent event)
		{
			// TODO Auto-generated method stub
			
			float x = event.values[0];
			float y = event.values[0];
			float z = event.values[0];
			
			currentY = y;
			
			if( Math.abs(currentY-previousY) > threshold)
			{
				numSteps++;
				tv_steps.setText(String.valueOf(numSteps) ) ;
			}
			
			tv_X.setText(String.valueOf(x));
			tv_Y.setText(String.valueOf(y));
			tv_Z.setText(String.valueOf(z));
			
			
			previousY = y;
			
		}

		//@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) 
		{
			// TODO Auto-generated method stub
			
		}
	};
	
	public void resetSteps(View v)
	{
		numSteps = 0;
		tv_steps.setText(String.valueOf(numSteps) ) ;

	}
	
	  private OnSeekBarChangeListener seekBarListener  = new OnSeekBarChangeListener() 
	  {
		  
			  @Override
			  public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) 
			  {
				  threshold = seekbar.getProgress();
				  tv_sensitive.setText(String.valueOf(threshold));
			  }
			
			  @Override
			  public void onStartTrackingTouch(SeekBar seekBar) 
			  {
				
			  }
			
			  @Override
			  public void onStopTrackingTouch(SeekBar seekBar) 
			  {
			  }
	  };
	
}
