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

Commit fafbe05d authored by Vladimir Komsiyski's avatar Vladimir Komsiyski
Browse files

Refactor the native RuntimeSensor API.

Return a status from the virtual sensor callback indicating whether
the callback invocation was successful.

Bug: 266042170
Test: atest CtsSensorTestCases

Change-Id: I001019c1915dc33db26e84444314d162a801fb9a
parent 412fb031
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -87,14 +87,15 @@ VirtualSensor::VirtualSensor() :

// ---------------------------------------------------------------------------

RuntimeSensor::RuntimeSensor(const sensor_t& sensor, sp<StateChangeCallback> callback)
RuntimeSensor::RuntimeSensor(const sensor_t& sensor, sp<SensorCallback> callback)
  : BaseSensor(sensor), mCallback(std::move(callback)) {
}

status_t RuntimeSensor::activate(void*, bool enabled) {
    if (enabled != mEnabled) {
        mEnabled = enabled;
        mCallback->onStateChanged(mEnabled, mSamplingPeriodNs, mBatchReportLatencyNs);
        return mCallback->onConfigurationChanged(mSensor.getHandle(), mEnabled, mSamplingPeriodNs,
                mBatchReportLatencyNs);
    }
    return OK;
}
@@ -105,7 +106,8 @@ status_t RuntimeSensor::batch(void*, int, int, int64_t samplingPeriodNs,
        mSamplingPeriodNs = samplingPeriodNs;
        mBatchReportLatencyNs = maxBatchReportLatencyNs;
        if (mEnabled) {
            mCallback->onStateChanged(mEnabled, mSamplingPeriodNs, mBatchReportLatencyNs);
            return mCallback->onConfigurationChanged(mSensor.getHandle(), mEnabled,
                    mSamplingPeriodNs, mBatchReportLatencyNs);
        }
    }
    return OK;
@@ -115,7 +117,8 @@ status_t RuntimeSensor::setDelay(void*, int, int64_t ns) {
    if (mSamplingPeriodNs != ns) {
        mSamplingPeriodNs = ns;
        if (mEnabled) {
            mCallback->onStateChanged(mEnabled, mSamplingPeriodNs, mBatchReportLatencyNs);
            return mCallback->onConfigurationChanged(mSensor.getHandle(), mEnabled,
                    mSamplingPeriodNs, mBatchReportLatencyNs);
        }
    }
    return OK;
+5 −5
Original line number Diff line number Diff line
@@ -108,12 +108,12 @@ class RuntimeSensor : public BaseSensor {
public:
    static constexpr int DEFAULT_DEVICE_ID = 0;

    class StateChangeCallback : public virtual RefBase {
    class SensorCallback : public virtual RefBase {
      public:
        virtual void onStateChanged(bool enabled, int64_t samplingPeriodNs,
        virtual status_t onConfigurationChanged(int handle, bool enabled, int64_t samplingPeriodNs,
                                                int64_t batchReportLatencyNs) = 0;
    };
    RuntimeSensor(const sensor_t& sensor, sp<StateChangeCallback> callback);
    RuntimeSensor(const sensor_t& sensor, sp<SensorCallback> callback);
    virtual status_t activate(void* ident, bool enabled) override;
    virtual status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
                           int64_t maxBatchReportLatencyNs) override;
@@ -125,7 +125,7 @@ private:
    bool mEnabled = false;
    int64_t mSamplingPeriodNs = 0;
    int64_t mBatchReportLatencyNs = 0;
    sp<StateChangeCallback> mCallback;
    sp<SensorCallback> mCallback;
};

// ---------------------------------------------------------------------------
+9 −8
Original line number Diff line number Diff line
@@ -116,16 +116,17 @@ int32_t nextRuntimeSensorHandle() {
    return nextHandle++;
}

class RuntimeSensorCallbackProxy : public RuntimeSensor::StateChangeCallback {
class RuntimeSensorCallbackProxy : public RuntimeSensor::SensorCallback {
 public:
    RuntimeSensorCallbackProxy(sp<SensorService::RuntimeSensorStateChangeCallback> callback)
    RuntimeSensorCallbackProxy(sp<SensorService::RuntimeSensorCallback> callback)
        : mCallback(std::move(callback)) {}
    void onStateChanged(bool enabled, int64_t samplingPeriodNs,
    status_t onConfigurationChanged(int handle, bool enabled, int64_t samplingPeriodNs,
                                    int64_t batchReportLatencyNs) override {
        mCallback->onStateChanged(enabled, samplingPeriodNs, batchReportLatencyNs);
        return mCallback->onConfigurationChanged(handle, enabled, samplingPeriodNs,
                batchReportLatencyNs);
    }
 private:
    sp<SensorService::RuntimeSensorStateChangeCallback> mCallback;
    sp<SensorService::RuntimeSensorCallback> mCallback;
};

} // namespace
@@ -166,7 +167,7 @@ SensorService::SensorService()
}

int SensorService::registerRuntimeSensor(
    const sensor_t& sensor, int deviceId, sp<RuntimeSensorStateChangeCallback> callback) {
        const sensor_t& sensor, int deviceId, sp<RuntimeSensorCallback> callback) {
    int handle = 0;
    while (handle == 0 || !mSensors.isNewHandle(handle)) {
        handle = nextRuntimeSensorHandle();
@@ -179,7 +180,7 @@ int SensorService::registerRuntimeSensor(
    ALOGI("Registering runtime sensor handle 0x%x, type %d, name %s",
            handle, sensor.type, sensor.name);

    sp<RuntimeSensor::StateChangeCallback> runtimeSensorCallback(
    sp<RuntimeSensor::SensorCallback> runtimeSensorCallback(
        new RuntimeSensorCallbackProxy(std::move(callback)));
    sensor_t runtimeSensor = sensor;
    // force the handle to be consistent
+5 −4
Original line number Diff line number Diff line
@@ -147,11 +147,12 @@ public:
        virtual void onProximityActive(bool isActive) = 0;
    };

    class RuntimeSensorStateChangeCallback : public virtual RefBase {
    class RuntimeSensorCallback : public virtual RefBase {
    public:
        // Note that the callback is invoked from an async thread and can interact with the
        // SensorService directly.
        virtual void onStateChanged(bool enabled, int64_t samplingPeriodNanos,
        virtual status_t onConfigurationChanged(int handle, bool enabled,
                                                int64_t samplingPeriodNanos,
                                                int64_t batchReportLatencyNanos) = 0;
    };

@@ -182,7 +183,7 @@ public:
    status_t removeProximityActiveListener(const sp<ProximityActiveListener>& callback) ANDROID_API;

    int registerRuntimeSensor(const sensor_t& sensor, int deviceId,
                              sp<RuntimeSensorStateChangeCallback> callback) ANDROID_API;
                              sp<RuntimeSensorCallback> callback) ANDROID_API;
    status_t unregisterRuntimeSensor(int handle) ANDROID_API;
    status_t sendRuntimeSensorEvent(const sensors_event_t& event) ANDROID_API;