Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Unified Diff: device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensor.java

Issue 2395853003: [Sensors] Improvements in shared buffer managing (Closed)
Patch Set: Test compilation fix + comment from Ken Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensor.java
diff --git a/device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensor.java b/device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensor.java
index fb4de42a150bcdf7ca591d848be3e2d27c770d05..2da8e583d0aeb2875e2f46366f49737d634104bf 100644
--- a/device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensor.java
+++ b/device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensor.java
@@ -13,10 +13,6 @@ import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.device.mojom.ReportingMode;
-import java.nio.BufferOverflowException;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
import java.util.List;
/**
@@ -26,7 +22,7 @@ import java.util.List;
@JNINamespace("device")
public class PlatformSensor implements SensorEventListener {
private static final double MICROSECONDS_IN_SECOND = 1000000;
- private static final double MILLISECONDS_IN_NANOSECOND = 0.000001d;
+ private static final double SECONDS_IN_NANOSECOND = 0.000000001d;
/**
* The SENSOR_FREQUENCY_NORMAL is defined as 5Hz which corresponds to a polling delay
@@ -41,16 +37,6 @@ public class PlatformSensor implements SensorEventListener {
private long mNativePlatformSensorAndroid;
/**
- * Shared buffer that is used to return data to the client.
- */
- private ByteBuffer mBuffer;
-
- /**
- * Buffer that is filled with sensor reading data and swapped into mBuffer.
- */
- private ByteBuffer mSensorReadingData;
-
- /**
* Used for fetching sensor reading values and obtaining information about the sensor.
* @see android.hardware.Sensor
*/
@@ -107,25 +93,9 @@ public class PlatformSensor implements SensorEventListener {
* @param buffer shared buffer that is used to return data to the client.
*/
@CalledByNative
- protected void initPlatformSensorAndroid(long nativePlatformSensorAndroid, ByteBuffer buffer) {
+ protected void initPlatformSensorAndroid(long nativePlatformSensorAndroid) {
assert nativePlatformSensorAndroid != 0;
- assert buffer != null;
mNativePlatformSensorAndroid = nativePlatformSensorAndroid;
- mBuffer = buffer;
- mSensorReadingData = ByteBuffer.allocate(mBuffer.capacity());
- mSensorReadingData.order(ByteOrder.nativeOrder());
- }
-
- /**
- * Fills shared buffer with sensor reading data, throws exception if number of received values
- * is less than the number required for the PlatformSensor.
- */
- private void fillSensorReadingData(SensorEvent event, ByteBuffer buffer)
- throws IllegalStateException {
- if (event.values.length < mReadingCount) throw new IllegalStateException();
- for (int i = 0; i < mReadingCount; ++i) {
- buffer.putDouble(event.values[i]);
- }
}
/**
@@ -213,17 +183,19 @@ public class PlatformSensor implements SensorEventListener {
}
/**
- * Notifies native device::PlatformSensorAndroid when reading is changed.
+ * Notifies native device::PlatformSensorAndroid when there is an error.
*/
- protected void sensorReadingChanged() {
- nativeNotifyPlatformSensorReadingChanged(mNativePlatformSensorAndroid);
+ protected void sensorError() {
+ nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid);
}
/**
- * Notifies native device::PlatformSensorAndroid when there is an error.
+ * Updates reading at native device::PlatformSensorAndroid.
*/
- protected void sensorError() {
- nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid);
+ protected void updateSensorReading(
+ double timestamp, double value1, double value2, double value3) {
+ nativeUpdatePlatformSensorReading(
+ mNativePlatformSensorAndroid, timestamp, value1, value2, value3);
}
@Override
@@ -231,28 +203,26 @@ public class PlatformSensor implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent event) {
- try {
- mSensorReadingData.mark();
- // Convert timestamp from nanoseconds to milliseconds.
- mSensorReadingData.putDouble(event.timestamp * MILLISECONDS_IN_NANOSECOND);
- fillSensorReadingData(event, mSensorReadingData);
-
- mSensorReadingData.reset();
-
- mBuffer.mark();
- mBuffer.put(mSensorReadingData);
- mSensorReadingData.reset();
- mBuffer.reset();
-
- if (getReportingMode() == ReportingMode.ON_CHANGE) {
- sensorReadingChanged();
- }
- } catch (BufferOverflowException | IllegalStateException e) {
+ if (event.values.length < mReadingCount) {
sensorError();
stopSensor();
+ return;
+ }
+
+ double timestamp = event.timestamp * SECONDS_IN_NANOSECOND;
+ switch (event.values.length) {
+ case 1:
+ updateSensorReading(timestamp, event.values[0], 0.0, 0.0);
+ break;
+ case 2:
+ updateSensorReading(timestamp, event.values[0], event.values[1], 0.0);
+ break;
+ default:
+ updateSensorReading(timestamp, event.values[0], event.values[1], event.values[2]);
}
}
- private native void nativeNotifyPlatformSensorReadingChanged(long nativePlatformSensorAndroid);
private native void nativeNotifyPlatformSensorError(long nativePlatformSensorAndroid);
+ private native void nativeUpdatePlatformSensorReading(long nativePlatformSensorAndroid,
+ double timestamp, double value1, double value2, double value3);
}

Powered by Google App Engine
This is Rietveld 408576698