diff options
author | Jean-Baptiste Queru <jbq@google.com> | 2013-08-03 00:13:24 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2013-08-03 00:13:24 +0000 |
commit | 83485dc920cdc4a6f3dd6797623f536c99f08a6c (patch) | |
tree | ee1409deef8d2b23006d862428a536f73f0d10aa | |
parent | 60ae2617b4b304fc3e45a441f87d1765714477ba (diff) | |
parent | 80ba0a6baec17d5ac5a1e6f9e4b84f7d013d07e6 (diff) | |
download | base-83485dc920cdc4a6f3dd6797623f536c99f08a6c.tar.gz |
Merge "be more robust with handling unknown sensors"
-rw-r--r-- | core/java/android/hardware/Sensor.java | 78 | ||||
-rw-r--r-- | core/jni/android_hardware_SensorManager.cpp | 8 |
2 files changed, 63 insertions, 23 deletions
diff --git a/core/java/android/hardware/Sensor.java b/core/java/android/hardware/Sensor.java index 5cc11505d6b9..c3e9cb71ec1f 100644 --- a/core/java/android/hardware/Sensor.java +++ b/core/java/android/hardware/Sensor.java @@ -204,37 +204,71 @@ public final class Sensor { // TODO(): The following arrays are fragile and error-prone. This needs to be refactored. // Note: This needs to be updated, whenever a new sensor is added. - private static int[] sSensorReportingModes = { - REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, - REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS, - REPORTING_MODE_ON_CHANGE, REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS, - REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ON_CHANGE, - REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, - REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ONE_SHOT }; - - // Note: This needs to be updated, whenever a new sensor is added. - // Holds the maximum length of the values array associated with {@link SensorEvent} or - // {@link TriggerEvent} for the Sensor - private static int[] sMaxLengthValuesArray = { - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, - 6, 4, 6, 1 }; + // Holds the reporting mode and maximum length of the values array + // associated with + // {@link SensorEvent} or {@link TriggerEvent} for the Sensor + private static final int[] sSensorReportingModes = { + 0, 0, // padding because sensor types start at 1 + REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_ACCELEROMETER + REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_GEOMAGNETIC_FIELD + REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_ORIENTATION + REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_GYROSCOPE + REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_LIGHT + REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_PRESSURE + REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_TEMPERATURE + REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_PROXIMITY + REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_GRAVITY + REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_LINEAR_ACCELERATION + REPORTING_MODE_CONTINUOUS, 5, // SENSOR_TYPE_ROTATION_VECTOR + REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_RELATIVE_HUMIDITY + REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_AMBIENT_TEMPERATURE + REPORTING_MODE_CONTINUOUS, 6, // SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED + REPORTING_MODE_CONTINUOUS, 4, // SENSOR_TYPE_GAME_ROTATION_VECTOR + REPORTING_MODE_CONTINUOUS, 6, // SENSOR_TYPE_GYROSCOPE_UNCALIBRATED + REPORTING_MODE_ONE_SHOT, 1, // SENSOR_TYPE_SIGNIFICANT_MOTION + // added post 4.3 + REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_STEP_DETECTOR + REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_STEP_COUNTER + REPORTING_MODE_CONTINUOUS, 5 // SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR + }; static int getReportingMode(Sensor sensor) { - // mType starts from offset 1. - return sSensorReportingModes[sensor.mType - 1]; + int offset = sensor.mType * 2; + if (offset >= sSensorReportingModes.length) { + // we don't know about this sensor, so this is probably a + // vendor-defined sensor, in that case, we figure out the reporting + // mode from the sensor meta-data. + int minDelay = sensor.mMinDelay; + if (minDelay == 0) { + return REPORTING_MODE_ON_CHANGE; + } else if (minDelay < 0) { + return REPORTING_MODE_ONE_SHOT; + } else { + return REPORTING_MODE_CONTINUOUS; + } + } + return sSensorReportingModes[offset]; } static int getMaxLengthValuesArray(Sensor sensor, int sdkLevel) { - // mType starts from offset 1. - int len = sMaxLengthValuesArray[sensor.mType - 1]; - + int type = sensor.mType; // RotationVector length has changed to 3 to 5 for API level 18 // Set it to 3 for backward compatibility. - if (sensor.getType() == Sensor.TYPE_ROTATION_VECTOR && + if (type == Sensor.TYPE_ROTATION_VECTOR && sdkLevel <= Build.VERSION_CODES.JELLY_BEAN_MR1) { - len = 3; + return 3; + } + int offset = type * 2 + 1; + if (offset >= sSensorReportingModes.length) { + // we don't know about this sensor, so this is probably a + // vendor-defined sensor, in that case, we don't know how many value + // it has + // so we return the maximum and assume the app will know. + // FIXME: sensor HAL should advertise how much data is returned per + // sensor + return 16; } - return len; + return sSensorReportingModes[offset]; } /* Some of these fields are set only by the native bindings in diff --git a/core/jni/android_hardware_SensorManager.cpp b/core/jni/android_hardware_SensorManager.cpp index 637449421c13..ae0113b3541f 100644 --- a/core/jni/android_hardware_SensorManager.cpp +++ b/core/jni/android_hardware_SensorManager.cpp @@ -142,7 +142,13 @@ private: while ((n = q->read(buffer, 16)) > 0) { for (int i=0 ; i<n ; i++) { - env->SetFloatArrayRegion(mScratch, 0, 16, buffer[i].data); + if (buffer[i].type == SENSOR_TYPE_STEP_COUNTER) { + // step-counter returns a uint64, but the java API only deals with floats + float value = float(buffer[i].u64.step_counter); + env->SetFloatArrayRegion(mScratch, 0, 1, &value); + } else { + env->SetFloatArrayRegion(mScratch, 0, 16, buffer[i].data); + } env->CallVoidMethod(mReceiverObject, gBaseEventQueueClassInfo.dispatchSensorEvent, |