Copyright (c) 2008-2011 Vrije Universiteit, The Netherlands
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the follo...
If you think the Android project interdroid-swan 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
package interdroid.swan.sensors.impl;
//www.java2s.comimport interdroid.swan.R;
import interdroid.swan.sensors.AbstractConfigurationActivity;
import interdroid.swan.sensors.AbstractMemorySensor;
import java.util.List;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
publicclass GyroscopeSensor extends AbstractMemorySensor {
publicstaticfinal String TAG = "GyroscopeSensor";
/**
* The configuration activity for this sensor.
*
* @author nick <palmer@cs.vu.nl>
*
*/publicstaticclass ConfigurationActivity extends
AbstractConfigurationActivity {
@Override
publicfinalint getPreferencesXML() {
return R.xml.gyroscope_preferences;
}
}
/** Value of ACCURACY must be one of SensorManager.SENSOR_DELAY_* */publicstaticfinal String ACCURACY = "accuracy";
publicstaticfinal String X_FIELD = "x";
publicstaticfinal String Y_FIELD = "y";
publicstaticfinal String Z_FIELD = "z";
protectedstaticfinalint HISTORY_SIZE = 30;
private Sensor gyroscope;
private SensorManager sensorManager;
private SensorEventListener sensorEventListener = new SensorEventListener() {
publicvoid onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
currentConfiguration.putInt(ACCURACY, accuracy);
}
}
publicvoid onSensorChanged(SensorEvent event) {
long now = System.currentTimeMillis();
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
for (int i = 0; i < 3; i++) {
putValueTrimSize(VALUE_PATHS[i], null, now,
event.values[i], HISTORY_SIZE);
}
}
}
};
@Override
public String[] getValuePaths() {
returnnew String[] { X_FIELD, Y_FIELD, Z_FIELD };
}
@Override
publicvoid initDefaultConfiguration(Bundle DEFAULT_CONFIGURATION) {
DEFAULT_CONFIGURATION.putInt(ACCURACY,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
publicvoid onConnected() {
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensorList = sensorManager
.getSensorList(Sensor.TYPE_GYROSCOPE);
if (sensorList.size() > 0) {
gyroscope = sensorList.get(0);
} else {
Log.e(TAG, "No gyroscope found on device!");
}
}
@Override
publicfinalvoid register(String id, String valuePath, Bundle configuration) {
updateAccuracy();
}
privatevoid updateAccuracy() {
sensorManager.unregisterListener(sensorEventListener);
if (registeredConfigurations.size() > 0) {
int highestAccuracy = mDefaultConfiguration.getInt(ACCURACY);
for (Bundle configuration : registeredConfigurations.values()) {
if (configuration == null) {
continue;
}
if (configuration.containsKey(ACCURACY)) {
highestAccuracy = Math
.min(highestAccuracy,
Integer.parseInt(configuration
.getString(ACCURACY)));
}
}
highestAccuracy = Math.max(highestAccuracy,
SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(sensorEventListener, gyroscope,
highestAccuracy);
}
}
@Override
publicfinalvoid unregister(String id) {
updateAccuracy();
}
@Override
publicfinalvoid onDestroySensor() {
sensorManager.unregisterListener(sensorEventListener);
}
@Override
publicfloat getCurrentMilliAmpere() {
return gyroscope.getPower();
}
}