package com.facepp.library.util;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class SensorEventUtil implements SensorEventListener {
	private SensorManager mSensorManager;
	private Sensor mSensor;
	
	public int orientation = 0;

	public SensorEventUtil(Activity activity) {
		mSensorManager = (SensorManager) activity.getSystemService(activity.SENSOR_SERVICE);
		mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);// TYPE_GRAVITY
		// 参数三，检测的精准度
		mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);// SENSOR_DELAY_GAME
	}

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

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor == null) {
			return;
		}

		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			float x = event.values[0];
			float y = event.values[1];
			float z = event.values[2];
			
			if(x >= 9)
				orientation = 1;
			else if(x <= -9)
				orientation = 2;
			else if(y <= -9)
				orientation = 3;
			else
				orientation = 0;
		}
	}
}

@@ -851,16 +851,74 @@
 vent) %7B%0A
+%09%09final double G = 9.81;%0A%09%09final double SQRT2 = 1.414213;%0A
 %09%09if (ev
@@ -939,16 +939,16 @@
 null) %7B%0A
-
 %09%09%09retur
@@ -1113,23 +1113,301 @@
 %0A%09%09%09
-%0A
+if (z %3E= G / SQRT2) %7B //screen is more likely lying on the table%0A%09
 %09%09%09if
+ 
 (x %3E= 
-9)%0A
+G / 2) %7B%0A%09%09%09%09%09orientation = 1;%0A%09%09%09%09%7D else if (x %3C= -G / 2) %7B%0A%09%09%09%09%09orientation = 2;%0A%09%09%09%09%7D else if (y %3C= -G / 2) %7B%0A%09%09%09%09%09orientation = 3;%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09orientation = 0;%0A%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A%09%09%09%09if (x %3E= G / SQRT2) %7B%0A%09
 %09%09%09%09
@@ -1430,25 +1430,40 @@
 %0A%09%09%09
+%09%7D 
 else if
+ 
 (x %3C= -
-9)%0A
+G / SQRT2) %7B%0A%09
 %09%09%09%09
@@ -1486,25 +1486,40 @@
 %0A%09%09%09
+%09%7D 
 else if
+ 
 (y %3C= -
-9)%0A
+G / SQRT2) %7B%0A%09
 %09%09%09%09
@@ -1542,13 +1542,19 @@
 %0A%09%09%09
+%09%7D 
 else
-%0A
+ %7B%0A%09
 %09%09%09%09
@@ -1570,16 +1570,27 @@
 on = 0;%0A
+%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

Fix logic for guessing face orientation