Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1172fd88 authored by Vladimir Komsiyski's avatar Vladimir Komsiyski
Browse files

Always cache the default device sensors.

Since ag/24955733 the native SensorManager may be initialized with
a VirtualDevice's sensors instead of the default device ones and this
is WAI. However, this breaks an assumption in SystemSensorManager.java

SystemSensorManager's mFullSensorsList must always hold the default
device's sensors. Any virtual device's sensors are stored in
mFullRuntimeSensorListByDevice and the correct sensors are returned
from getSensorsList based on the manager's context.

If mFullSensorsList is initialized with virtual sensors, then there
is no way to get the default device's sensors anymore.

Fix: 315295201
Test: manual with the VDM test app
Change-Id: I8add9f4ece2fdd6d83f6388990b1772987eae939
parent 1b5e0a7b
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -86,6 +86,8 @@ public class SystemSensorManager extends SensorManager {
    private static native long nativeCreate(String opPackageName);
    private static native boolean nativeGetSensorAtIndex(long nativeInstance,
            Sensor sensor, int index);
    private static native boolean nativeGetDefaultDeviceSensorAtIndex(long nativeInstance,
            Sensor sensor, int index);
    private static native void nativeGetDynamicSensors(long nativeInstance, List<Sensor> list);
    private static native void nativeGetRuntimeSensors(
            long nativeInstance, int deviceId, List<Sensor> list);
@@ -162,11 +164,14 @@ public class SystemSensorManager extends SensorManager {
        // initialize the sensor list
        for (int index = 0;; ++index) {
            Sensor sensor = new Sensor();
            if (android.companion.virtual.flags.Flags.enableNativeVdm()) {
                if (!nativeGetDefaultDeviceSensorAtIndex(mNativeInstance, sensor, index)) break;
            } else {
                if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
            }
            mFullSensorsList.add(sensor);
            mHandleToSensor.put(sensor.getHandle(), sensor);
        }

    }

    /** @hide */
+16 −0
Original line number Diff line number Diff line
@@ -225,6 +225,19 @@ nativeGetSensorAtIndex(JNIEnv *env, jclass clazz, jlong sensorManager, jobject s
    return translateNativeSensorToJavaSensor(env, sensor, *sensorList[index]) != NULL;
}

static jboolean nativeGetDefaultDeviceSensorAtIndex(JNIEnv *env, jclass clazz, jlong sensorManager,
                                                    jobject sensor, jint index) {
    SensorManager *mgr = reinterpret_cast<SensorManager *>(sensorManager);

    Vector<Sensor> sensorList;
    ssize_t count = mgr->getDefaultDeviceSensorList(sensorList);
    if (ssize_t(index) >= count) {
        return false;
    }

    return translateNativeSensorToJavaSensor(env, sensor, sensorList[index]) != NULL;
}

static void
nativeGetDynamicSensors(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sensorList) {

@@ -539,6 +552,9 @@ static const JNINativeMethod gSystemSensorManagerMethods[] = {
        {"nativeGetSensorAtIndex", "(JLandroid/hardware/Sensor;I)Z",
         (void *)nativeGetSensorAtIndex},

        {"nativeGetDefaultDeviceSensorAtIndex", "(JLandroid/hardware/Sensor;I)Z",
         (void *)nativeGetDefaultDeviceSensorAtIndex},

        {"nativeGetDynamicSensors", "(JLjava/util/List;)V", (void *)nativeGetDynamicSensors},

        {"nativeGetRuntimeSensors", "(JILjava/util/List;)V", (void *)nativeGetRuntimeSensors},