From 050b56244ff46d43e4886018d7cd20f0b1dc02b9 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Thu, 29 Jul 2010 16:51:38 -0700 Subject: Added SensorManager.getMinDelay() Exposed the new "min delay" sensor property through native and java sensor apis. This allows the caller to know what is the maximum rate at which a sensor can return events, or, if a sensor works in "update" mode (events returned only when the value changes). Also augmented SensorManager.regusterSensorEvent() so that it can accept a value in microsecond in addition to the 4 constants already defined. Change-Id: If425e9979892666df8c989d7de3c362230fa19e0 --- api/current.xml | 11 +++++++++++ core/java/android/hardware/Sensor.java | 10 ++++++++++ core/java/android/hardware/SensorManager.java | 13 ++++++++----- core/jni/android_hardware_SensorManager.cpp | 3 +++ include/gui/Sensor.h | 2 ++ include/gui/SensorEventQueue.h | 2 +- libs/gui/Sensor.cpp | 12 ++++++++++-- libs/gui/SensorEventQueue.cpp | 4 ++-- native/android/sensor.cpp | 4 ++++ native/include/android/sensor.h | 11 ++++++++++- 10 files changed, 61 insertions(+), 11 deletions(-) diff --git a/api/current.xml b/api/current.xml index f1a383f..9de160a 100644 --- a/api/current.xml +++ b/api/current.xml @@ -77549,6 +77549,17 @@ visibility="public" > + + true if the sensor is supported and successfully * enabled. @@ -967,6 +968,7 @@ public class SensorManager * are received faster. The value must be one of * {@link #SENSOR_DELAY_NORMAL}, {@link #SENSOR_DELAY_UI}, * {@link #SENSOR_DELAY_GAME}, or {@link #SENSOR_DELAY_FASTEST}. + * or, the desired delay between events in microsecond. * * @param handler * The {@link android.os.Handler Handler} the @@ -992,16 +994,17 @@ public class SensorManager delay = 0; break; case SENSOR_DELAY_GAME: - delay = 20; + delay = 20000; break; case SENSOR_DELAY_UI: - delay = 60; + delay = 60000; break; case SENSOR_DELAY_NORMAL: - delay = 200; + delay = 200000; break; default: - return false; + delay = rate; + break; } synchronized (sListeners) { diff --git a/core/jni/android_hardware_SensorManager.cpp b/core/jni/android_hardware_SensorManager.cpp index 7b23418..e29495c 100644 --- a/core/jni/android_hardware_SensorManager.cpp +++ b/core/jni/android_hardware_SensorManager.cpp @@ -38,6 +38,7 @@ struct SensorOffsets jfieldID range; jfieldID resolution; jfieldID power; + jfieldID minDelay; } gSensorOffsets; /* @@ -74,6 +75,7 @@ sensors_module_get_next_sensor(JNIEnv *env, jobject clazz, jobject sensor, jint env->SetFloatField(sensor, sensorOffsets.range, list->getMaxValue()); env->SetFloatField(sensor, sensorOffsets.resolution, list->getResolution()); env->SetFloatField(sensor, sensorOffsets.power, list->getPowerUsage()); + env->SetIntField(sensor, sensorOffsets.minDelay, list->getMinDelay()); next++; return nextGetFieldID(sensorClass, "mMaxRange", "F"); sensorOffsets.resolution = _env->GetFieldID(sensorClass, "mResolution","F"); sensorOffsets.power = _env->GetFieldID(sensorClass, "mPower", "F"); + sensorOffsets.minDelay = _env->GetFieldID(sensorClass, "mMinDelay", "I"); } static JNINativeMethod gMethods[] = { diff --git a/include/gui/Sensor.h b/include/gui/Sensor.h index e696d63..2de07b1 100644 --- a/include/gui/Sensor.h +++ b/include/gui/Sensor.h @@ -63,6 +63,7 @@ public: float getMaxValue() const; float getResolution() const; float getPowerUsage() const; + int32_t getMinDelay() const; // Flattenable interface virtual size_t getFlattenedSize() const; @@ -81,6 +82,7 @@ private: float mMaxValue; float mResolution; float mPower; + int32_t mMinDelay; }; // ---------------------------------------------------------------------------- diff --git a/include/gui/SensorEventQueue.h b/include/gui/SensorEventQueue.h index ad36dac..6581ae3 100644 --- a/include/gui/SensorEventQueue.h +++ b/include/gui/SensorEventQueue.h @@ -65,7 +65,7 @@ public: status_t setEventRate(Sensor const* sensor, nsecs_t ns) const; // these are here only to support SensorManager.java - status_t enableSensor(int32_t handle, int32_t ms) const; + status_t enableSensor(int32_t handle, int32_t us) const; status_t disableSensor(int32_t handle) const; private: diff --git a/libs/gui/Sensor.cpp b/libs/gui/Sensor.cpp index cb85df9..b1f37ff 100644 --- a/libs/gui/Sensor.cpp +++ b/libs/gui/Sensor.cpp @@ -32,7 +32,7 @@ namespace android { Sensor::Sensor() : mHandle(0), mType(0), mMinValue(0), mMaxValue(0), mResolution(0), - mPower(0) + mPower(0), mMinDelay(0) { } @@ -46,6 +46,7 @@ Sensor::Sensor(struct sensor_t const* hwSensor) mMaxValue = hwSensor->maxRange; // FIXME: maxValue mResolution = hwSensor->resolution; mPower = hwSensor->power; + mMinDelay = hwSensor->minDelay; } Sensor::~Sensor() @@ -84,12 +85,17 @@ float Sensor::getPowerUsage() const { return mPower; } +int32_t Sensor::getMinDelay() const { + return mMinDelay; +} + size_t Sensor::getFlattenedSize() const { return sizeof(int32_t) + ((mName.length() + 3) & ~3) + sizeof(int32_t) + ((mVendor.length() + 3) & ~3) + sizeof(int32_t) * 2 + - sizeof(float) * 4; + sizeof(float) * 4 + + sizeof(int32_t); } size_t Sensor::getFdCount() const @@ -132,6 +138,7 @@ status_t Sensor::flatten(void* buffer, size_t size, offset += write(buffer, offset, mMaxValue); offset += write(buffer, offset, mResolution); offset += write(buffer, offset, mPower); + offset += write(buffer, offset, mMinDelay); return NO_ERROR; } @@ -169,6 +176,7 @@ status_t Sensor::unflatten(void const* buffer, size_t size, offset += read(buffer, offset, &mMaxValue); offset += read(buffer, offset, &mResolution); offset += read(buffer, offset, &mPower); + offset += read(buffer, offset, &mMinDelay); return NO_ERROR; } diff --git a/libs/gui/SensorEventQueue.cpp b/libs/gui/SensorEventQueue.cpp index 4b46842..3396f25 100644 --- a/libs/gui/SensorEventQueue.cpp +++ b/libs/gui/SensorEventQueue.cpp @@ -114,10 +114,10 @@ status_t SensorEventQueue::disableSensor(Sensor const* sensor) const { return mSensorEventConnection->enableDisable(sensor->getHandle(), false); } -status_t SensorEventQueue::enableSensor(int32_t handle, int32_t ms) const { +status_t SensorEventQueue::enableSensor(int32_t handle, int32_t us) const { status_t err = mSensorEventConnection->enableDisable(handle, true); if (err == NO_ERROR) { - mSensorEventConnection->setEventRate(handle, ms2ns(ms)); + mSensorEventConnection->setEventRate(handle, us2ns(us)); } return err; } diff --git a/native/android/sensor.cpp b/native/android/sensor.cpp index e1fc4e7..db534e0 100644 --- a/native/android/sensor.cpp +++ b/native/android/sensor.cpp @@ -149,3 +149,7 @@ float ASensor_getResolution(ASensor const* sensor) return static_cast(sensor)->getResolution(); } +int ASensor_getMinDelay(ASensor const* sensor) +{ + return static_cast(sensor)->getMinDelay(); +} diff --git a/native/include/android/sensor.h b/native/include/android/sensor.h index 00d95d8..b4ce024 100644 --- a/native/include/android/sensor.h +++ b/native/include/android/sensor.h @@ -121,6 +121,7 @@ typedef struct ASensorEvent { float temperature; float distance; float light; + float pressure; }; int32_t reserved1[4]; } ASensorEvent; @@ -188,7 +189,8 @@ int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sen /* * Sets the delivery rate of events in microseconds for the given sensor. * Note that this is a hint only, generally event will arrive at a higher - * rate. + * rate. It is an error to set a rate inferior to the value returned by + * ASensor_getMinDelay(). * Returns a negative error code on failure. */ int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec); @@ -239,6 +241,13 @@ int ASensor_getType(ASensor const* sensor); */ float ASensor_getResolution(ASensor const* sensor); +/* + * Returns the minimum delay allowed between events in microseconds. + * A value of zero means that this sensor doesn't report events at a + * constant rate, but rather only when a new data is available. + */ +int ASensor_getMinDelay(ASensor const* sensor); + #ifdef __cplusplus }; -- cgit v1.1