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.com*/import 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 ProximitySensor extends AbstractMemorySensor {
publicstaticfinal String TAG = "ProximitySensor";
/**
* The configuration activity for this sensor.
*
* @author nick <palmer@cs.vu.nl>
*
*/publicstaticclass ConfigurationActivity extends
AbstractConfigurationActivity {
@Override
publicfinalint getPreferencesXML() {
return R.xml.proximity_preferences;
}
}
/** Value of ACCURACY must be one of SensorManager.SENSOR_DELAY_* */publicstaticfinal String ACCURACY = "accuracy";
publicstaticfinal String DISTANCE_FIELD = "distance";
protectedstaticfinalint HISTORY_SIZE = 300;
private Sensor proximitySensor;
private SensorManager sensorManager;
private SensorEventListener sensorEventListener = new SensorEventListener() {
publicvoid onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_PROXIMITY) {
currentConfiguration.putInt(ACCURACY, accuracy);
}
}
publicvoid onSensorChanged(SensorEvent event) {
long now = System.currentTimeMillis();
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
putValueTrimSize(VALUE_PATHS[0], null, now,
(double) event.values[0], HISTORY_SIZE);
}
}
};
@Override
public String[] getValuePaths() {
returnnew String[] { DISTANCE_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_PROXIMITY);
if (sensorList.size() > 0) {
proximitySensor = sensorList.get(0);
} else {
Log.e(TAG, "No proximitySensor 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,configuration.getInt(ACCURACY));
}
}
highestAccuracy = Math.max(highestAccuracy,
SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(sensorEventListener,
proximitySensor, highestAccuracy);
}
}
@Override
publicfinalvoid unregister(String id) {
updateAccuracy();
}
@Override
publicfinalvoid onDestroySensor() {
sensorManager.unregisterListener(sensorEventListener);
}
@Override
publicfloat getCurrentMilliAmpere() {
return proximitySensor.getPower();
}
}