======================
LOOK! LICENSING TERMS
======================
look! is licensed under the BSD 3-Clause (also known as "BSD New" or
"BSD Simplified"), as follows:
Copyright (c) 2010-2012, Look...
If you think the Android project Look listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/**
*-----------------------------------------------------------------------------
* Copyright (c) 2012, Look! Development Team
* All rights reserved./*fromwww.java2s.com*/
*
* Distributed under the terms of the BSD Simplified License.
*
* The full license is in the LICENSE file, distributed with this software.
*-----------------------------------------------------------------------------
*/package es.ucm.look.ar.util;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
/**
* A class holding device's orientation
*
* @author ??ngel Serrano
*
*/publicclass DeviceOrientation implements SensorEventListener {
privatestatic DeviceOrientation instance;
private SensorManager mSM;
privatefloat[] inR = newfloat[16];
privatefloat[] outR = newfloat[16];
privatefloat[] I = newfloat[16];
privatefloat[] gravity = newfloat[3];
privatefloat[] geomag = newfloat[3];
privatefloat[] orientVals = newfloat[3];
/**
* Device azimuth ( y coordinate )
*/privatefloat azimuth;
privatefloat azimuthOffset = 0.0f;
/**
* Device pitch ( x coordinate )
*/privatefloat pitch;
privatefloat pitchOffset = 0.0f;
/**
* Device roll ( z coordinate )
*/privatefloat roll;
privatefloat rollOffset = 0.0f;
publicfloat getAzimuth() {
return azimuth;
}
publicfloat getPitch() {
return pitch;
}
publicfloat getRoll() {
return roll;
}
private DeviceOrientation(Context context) {
mSM = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSM.registerListener(this, mSM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
mSM.registerListener(this, mSM.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
}
publicstatic DeviceOrientation getDeviceOrientation(Context c) {
if (instance == null)
instance = new DeviceOrientation(c);
return instance;
}
@Override
publicvoid onAccuracyChanged(Sensor sensor, int accuracy) {
}
/**
* Returns the matrix rotation for the current device orientation
*
* @return the matrix rotation for the current device orientation
*/publicfloat[] getRotationMatrix() {
return outR;
}
@Override
publicvoid onSensorChanged(SensorEvent event) {
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
return;
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
gravity = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
geomag = event.values.clone();
break;
}
if (gravity != null && geomag != null) {
boolean success = SensorManager.getRotationMatrix(inR, I, gravity, geomag);
if (success) {
SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, orientVals);
this.azimuth = orientVals[0] + azimuthOffset;
this.pitch = orientVals[1] + pitchOffset;
this.roll = orientVals[2] + rollOffset;
}
}
}
publicvoid setAzimuthOffset(float azimuthOffset) {
this.azimuthOffset = azimuthOffset;
}
publicvoid setPitchOffset(float pitchOffset) {
this.pitchOffset = pitchOffset;
}
publicvoid setRollOffset(float rollOffset) {
this.rollOffset = rollOffset;
}
}