Move compass to separate class
This commit is contained in:
74
mobile/src/com/cradle/iitc_mobile/compass/AccMagCompass.java
Normal file
74
mobile/src/com/cradle/iitc_mobile/compass/AccMagCompass.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package com.cradle.iitc_mobile.compass;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
|
||||
public class AccMagCompass extends Compass {
|
||||
private static final double SENSOR_DELAY_USER = 100 * 1e6; // 100 milliseconds
|
||||
|
||||
private final Context mContext;
|
||||
private long mLastUpdate = 0;
|
||||
private final SensorListener mListener = new SensorListener();
|
||||
private final float[] mOrientation = new float[3];
|
||||
private final float[] mRotationMatrix = new float[9];
|
||||
private final Sensor mSensorAcc, mSensorMag;
|
||||
private final SensorManager mSensorManager;
|
||||
private float[] mValuesAcc = null, mValuesMag = null;
|
||||
|
||||
public AccMagCompass(final Context context) {
|
||||
mContext = context;
|
||||
|
||||
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
|
||||
|
||||
mSensorAcc = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
mSensorMag = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
|
||||
}
|
||||
|
||||
private void calculateOrientation() {
|
||||
|
||||
// wait until both sensors have given us an event
|
||||
if (mValuesAcc == null || mValuesMag == null) return;
|
||||
|
||||
if (!SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAcc, mValuesMag)) return;
|
||||
SensorManager.getOrientation(mRotationMatrix, mOrientation);
|
||||
|
||||
publishOrientation(mOrientation[0], mOrientation[1], mOrientation[2]);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
mSensorManager.registerListener(mListener, mSensorAcc, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
mSensorManager.registerListener(mListener, mSensorMag, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
mSensorManager.unregisterListener(mListener);
|
||||
}
|
||||
|
||||
private class SensorListener implements SensorEventListener {
|
||||
@Override
|
||||
public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(final SensorEvent event) {
|
||||
switch (event.sensor.getType()) {
|
||||
case Sensor.TYPE_ACCELEROMETER:
|
||||
mValuesAcc = event.values;
|
||||
break;
|
||||
case Sensor.TYPE_MAGNETIC_FIELD:
|
||||
mValuesMag = event.values;
|
||||
|
||||
// save some battery, 10 updates per second should be enough
|
||||
if ((event.timestamp - mLastUpdate) < SENSOR_DELAY_USER) break;
|
||||
mLastUpdate = event.timestamp;
|
||||
|
||||
calculateOrientation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
mobile/src/com/cradle/iitc_mobile/compass/Compass.java
Normal file
39
mobile/src/com/cradle/iitc_mobile/compass/Compass.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.cradle.iitc_mobile.compass;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class Compass
|
||||
{
|
||||
private final ArrayList<CompassListener> mListeners = new ArrayList<CompassListener>();
|
||||
private boolean mStarted = false;
|
||||
|
||||
protected void publishOrientation(final float x, final float y, final float z)
|
||||
{
|
||||
for (final CompassListener listener : mListeners)
|
||||
listener.onCompassChanged(x, y, z);
|
||||
}
|
||||
|
||||
protected abstract void onStart();
|
||||
|
||||
protected abstract void onStop();
|
||||
|
||||
public void registerListener(final CompassListener listener)
|
||||
{
|
||||
mListeners.add(listener);
|
||||
if (!mStarted)
|
||||
{
|
||||
onStart();
|
||||
mStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void unregisterListener(final CompassListener listener)
|
||||
{
|
||||
mListeners.remove(listener);
|
||||
if (mListeners.size() == 0)
|
||||
{
|
||||
onStop();
|
||||
mStarted = false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package com.cradle.iitc_mobile.compass;
|
||||
|
||||
public interface CompassListener {
|
||||
public void onCompassChanged(float x, float y, float z);
|
||||
}
|
Reference in New Issue
Block a user