package com.example.androidsensors;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {

	TextView prox, x, y, z, pos;

	SensorManager sm;
	Sensor proxSensor;
	Sensor accSensor;

	GPSTracker gps;
	
	RelativeLayout myLayout;

	private float mAccel; // acceleration apart from gravity
	private float mAccelCurrent; // current acceleration including gravity
	private float mAccelLast; // last acceleration including gravity
	float sensorX,sensorY, sensorZ;
	
	Vibrator v;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		prox = (TextView) findViewById(R.id.prox);
		x = (TextView) findViewById(R.id.x);
		y = (TextView) findViewById(R.id.z);
		z = (TextView) findViewById(R.id.y);
		pos = (TextView) findViewById(R.id.pos);
		myLayout = (RelativeLayout) findViewById(R.id.myLayout);
		
		myLayout.setBackgroundColor(Color.WHITE);

		sm = (SensorManager) getSystemService(SENSOR_SERVICE);
		proxSensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
		accSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

		gps = new GPSTracker(this);

		// check if GPS enabled
		if (gps.canGetLocation()) {

			double latitude = gps.getLatitude();
			double longitude = gps.getLongitude();

			// \n is for new line
			Toast.makeText(
					getApplicationContext(),
					"Your Location is - \nLat: " + latitude + "\nLong: "
							+ longitude, Toast.LENGTH_LONG).show();
		} else {
			// can't get location
			// GPS or Network is not enabled
			// Ask user to enable GPS/network in settings
			gps.showSettingsAlert();
		}
		mAccel = 0.00f;
		mAccelCurrent = SensorManager.GRAVITY_EARTH;
		mAccelLast = SensorManager.GRAVITY_EARTH;
		
		v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);


	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
			prox.setText("PROX: " + String.valueOf(event.values[0]));
		}
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			x.setText("X: " + String.valueOf(event.values[0]));
			y.setText("Y: " + String.valueOf(event.values[1]));
			z.setText("Z: " + String.valueOf(event.values[2]));

			sensorX = event.values[0];
			sensorY = event.values[1];
			sensorZ = event.values[2];

			mAccelLast = mAccelCurrent;
			mAccelCurrent = (float) Math.sqrt((double) (sensorX * sensorX
					+ sensorY * sensorY + sensorZ * sensorZ));

			float delta = mAccelCurrent - mAccelLast;

			mAccel = mAccel * 0.9f + delta; // perform low-cut filter
			
			tabletPosition(event.values[0],event.values[1],event.values[2]);
		}
		if (mAccel > 1.5) {
			shake();
			
		}

	}

	private void shake() {
		Random random = new Random();
		int myRandom = random.nextInt() % 4;
	    switch (myRandom) {
	    case 0:
			myLayout.setBackgroundColor(Color.YELLOW);
	        break;
	    case 1:
			myLayout.setBackgroundColor(Color.GREEN);
	        break;
	    case 2:
			myLayout.setBackgroundColor(Color.RED);
	        break;
	    case 3:
			myLayout.setBackgroundColor(Color.CYAN);
	        break;
	     default:
	    	 myLayout.setBackgroundColor(Color.WHITE);
		    break;
	    }
	    
	    v.vibrate(200);

	}


public void tabletPosition(float sensorX, float sensorY, float sensorZ) {
	if (sensorZ > 9 && sensorZ < 10) {
		pos.setText("POS: " + "Horizontal");
	} if ((sensorY > 9 && sensorY < 10)) {
		pos.setText("POS: " + "Vertical");
	} if ((sensorZ > 0 && sensorZ < 9) && (sensorY < 9 && sensorY > 0)) {
		pos.setText("POS: " + "Diagonal");
	}
}

@Override
protected void onPause() {
	sm.unregisterListener(this);
	super.onPause();
}

@Override
protected void onResume() {
	sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
	sm.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL);
	super.onResume();
}

@Override
protected void onStop() {
	sm.unregisterListener(this);
	super.onStop();
}

}
