package com.example.service_app02;


import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
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.Handler;
import android.os.IBinder;
import android.widget.TextView;
import android.widget.Toast;

//https://androidammy.blogspot.fr/2015/05/accelerometer-shake-events-example-with.html

public class class_service extends Service implements SensorEventListener 
{

	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	private float mAccel; // acceleration apart from gravity
	private float mAccelCurrent; // current acceleration including gravity
	private float mAccelLast; // last acceleration including gravity

	@Override
	public IBinder onBind(Intent intent) 
	{
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mAccelerometer = mSensorManager	.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mSensorManager.registerListener(this, mAccelerometer,	SensorManager.SENSOR_DELAY_UI, new Handler());
		return START_STICKY;
	}

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

	@Override
	public void onSensorChanged(SensorEvent event) 
	{
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];
		mAccelLast = mAccelCurrent;
		mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
		float delta = mAccelCurrent - mAccelLast;
		mAccel = mAccel * 0.9f + delta; // perform low-cut filter

		if (mAccel > 11) 
		{
			//showNotification();
			String zorro = "4654";
		}
	}

	/**
	 * show notification when Accel is more then the given int.
	 */
/*	
	private void showNotification()
	{
		final NotificationManager mgr = (NotificationManager) this
				.getSystemService(Context.NOTIFICATION_SERVICE);
		NotificationCompat.Builder note = new NotificationCompat.Builder(this);
		note.setContentTitle("Device Accelerometer Notification");
		note.setTicker("New Message Alert!");
		note.setAutoCancel(true);
		// to set default sound/light/vibrate or all
		note.setDefaults(Notification.DEFAULT_ALL);
		// Icon to be set on Notification
		note.setSmallIcon(R.drawable.ic_launcher);
		// This pending intent will open after notification click
		PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,
				MainActivity.class), 0);
		// set pending intent to notification builder
		note.setContentIntent(pi);
		mgr.notify(101, note.build());
	}
*/
}
