Chromium Code Reviews| 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..c8a82fd408acf3cf866d99a7a86a3e36885dd912 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,9 +13,7 @@ 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; |
|
shalamov
2016/10/06 12:46:09
not needed anymore.
Mikhail
2016/10/07 10:07:01
Done.
|
| -import java.nio.ByteOrder; |
| import java.util.List; |
| @@ -26,7 +24,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 +39,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,13 +95,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()); |
| } |
| /** |
| @@ -213,17 +197,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 +217,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); |
| } |