Merge "Clean up get sensorList." into mnc-dev

This commit is contained in:
Aravind Akella
2015-07-07 19:53:45 +00:00
committed by Android (Google) Code Review
2 changed files with 16 additions and 19 deletions

View File

@@ -42,7 +42,8 @@ import java.util.List;
public class SystemSensorManager extends SensorManager { public class SystemSensorManager extends SensorManager {
private static native void nativeClassInit(); private static native void nativeClassInit();
private static native long nativeCreate(String opPackageName); private static native long nativeCreate(String opPackageName);
private static native int nativeGetNextSensor(long nativeInstance, Sensor sensor, int next); private static native boolean nativeGetSensorAtIndex(long nativeInstance,
Sensor sensor, int index);
private static native boolean nativeIsDataInjectionEnabled(long nativeInstance); private static native boolean nativeIsDataInjectionEnabled(long nativeInstance);
private static boolean sSensorModuleInitialized = false; private static boolean sSensorModuleInitialized = false;
@@ -80,15 +81,12 @@ public class SystemSensorManager extends SensorManager {
} }
// initialize the sensor list // initialize the sensor list
int i = 0; for (int index = 0;;++index) {
do {
Sensor sensor = new Sensor(); Sensor sensor = new Sensor();
i = nativeGetNextSensor(mNativeInstance, sensor, i); if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
if (i >= 0) { mFullSensorsList.add(sensor);
mFullSensorsList.add(sensor); mHandleToSensor.append(sensor.getHandle(), sensor);
mHandleToSensor.append(sensor.getHandle(), sensor); }
}
} while (i > 0);
} }

View File

@@ -141,18 +141,18 @@ nativeCreate
return (jlong) new SensorManager(String16(opPackageNameUtf.c_str())); return (jlong) new SensorManager(String16(opPackageNameUtf.c_str()));
} }
static jint static jboolean
nativeGetNextSensor(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sensor, jint next) nativeGetSensorAtIndex(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sensor, jint index)
{ {
SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager); SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager);
Sensor const* const* sensorList; Sensor const* const* sensorList;
size_t count = mgr->getSensorList(&sensorList); size_t count = mgr->getSensorList(&sensorList);
if (size_t(next) >= count) { if (size_t(index) >= count) {
return -1; return false;
} }
Sensor const* const list = sensorList[next]; Sensor const* const list = sensorList[index];
const SensorOffsets& sensorOffsets(gSensorOffsets); const SensorOffsets& sensorOffsets(gSensorOffsets);
jstring name = getInternedString(env, &list->getName()); jstring name = getInternedString(env, &list->getName());
jstring vendor = getInternedString(env, &list->getVendor()); jstring vendor = getInternedString(env, &list->getVendor());
@@ -177,8 +177,7 @@ nativeGetNextSensor(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sens
jstring stringType = getInternedString(env, &list->getStringType()); jstring stringType = getInternedString(env, &list->getStringType());
env->SetObjectField(sensor, sensorOffsets.stringType, stringType); env->SetObjectField(sensor, sensorOffsets.stringType, stringType);
} }
next++; return true;
return size_t(next) < count ? next : 0;
} }
static jboolean nativeIsDataInjectionEnabled(JNIEnv *_env, jclass _this, jlong sensorManager) { static jboolean nativeIsDataInjectionEnabled(JNIEnv *_env, jclass _this, jlong sensorManager) {
@@ -352,9 +351,9 @@ static JNINativeMethod gSystemSensorManagerMethods[] = {
"(Ljava/lang/String;)J", "(Ljava/lang/String;)J",
(void*)nativeCreate }, (void*)nativeCreate },
{"nativeGetNextSensor", {"nativeGetSensorAtIndex",
"(JLandroid/hardware/Sensor;I)I", "(JLandroid/hardware/Sensor;I)Z",
(void*)nativeGetNextSensor }, (void*)nativeGetSensorAtIndex },
{"nativeIsDataInjectionEnabled", {"nativeIsDataInjectionEnabled",
"(J)Z", "(J)Z",