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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.device.sensors; 5 package org.chromium.device.sensors;
6 6
7 import android.hardware.Sensor; 7 import android.hardware.Sensor;
8 import android.hardware.SensorEvent; 8 import android.hardware.SensorEvent;
9 import android.hardware.SensorEventListener; 9 import android.hardware.SensorEventListener;
10 import android.os.Build; 10 import android.os.Build;
11 11
12 import org.chromium.base.annotations.CalledByNative; 12 import org.chromium.base.annotations.CalledByNative;
13 import org.chromium.base.annotations.JNINamespace; 13 import org.chromium.base.annotations.JNINamespace;
14 import org.chromium.device.mojom.ReportingMode; 14 import org.chromium.device.mojom.ReportingMode;
15 15
16 import java.nio.BufferOverflowException;
17 import java.nio.ByteBuffer;
18 import java.nio.ByteOrder;
19
20 import java.util.List; 16 import java.util.List;
21 17
22 /** 18 /**
23 * Implementation of PlatformSensor that uses Android Sensor Framework. Lifetime is controlled by 19 * Implementation of PlatformSensor that uses Android Sensor Framework. Lifetime is controlled by
24 * the device::PlatformSensorAndroid. 20 * the device::PlatformSensorAndroid.
25 */ 21 */
26 @JNINamespace("device") 22 @JNINamespace("device")
27 public class PlatformSensor implements SensorEventListener { 23 public class PlatformSensor implements SensorEventListener {
28 private static final double MICROSECONDS_IN_SECOND = 1000000; 24 private static final double MICROSECONDS_IN_SECOND = 1000000;
29 private static final double MILLISECONDS_IN_NANOSECOND = 0.000001d; 25 private static final double SECONDS_IN_NANOSECOND = 0.000000001d;
30 26
31 /** 27 /**
32 * The SENSOR_FREQUENCY_NORMAL is defined as 5Hz which corresponds to a poll ing delay 28 * The SENSOR_FREQUENCY_NORMAL is defined as 5Hz which corresponds to a poll ing delay
33 * @see android.hardware.SensorManager.SENSOR_DELAY_NORMAL value that is def ined as 200000 29 * @see android.hardware.SensorManager.SENSOR_DELAY_NORMAL value that is def ined as 200000
34 * microseconds. 30 * microseconds.
35 */ 31 */
36 private static final double SENSOR_FREQUENCY_NORMAL = 5.0d; 32 private static final double SENSOR_FREQUENCY_NORMAL = 5.0d;
37 33
38 /** 34 /**
39 * Identifier of device::PlatformSensorAndroid instance. 35 * Identifier of device::PlatformSensorAndroid instance.
40 */ 36 */
41 private long mNativePlatformSensorAndroid; 37 private long mNativePlatformSensorAndroid;
42 38
43 /** 39 /**
44 * Shared buffer that is used to return data to the client.
45 */
46 private ByteBuffer mBuffer;
47
48 /**
49 * Buffer that is filled with sensor reading data and swapped into mBuffer.
50 */
51 private ByteBuffer mSensorReadingData;
52
53 /**
54 * Used for fetching sensor reading values and obtaining information about t he sensor. 40 * Used for fetching sensor reading values and obtaining information about t he sensor.
55 * @see android.hardware.Sensor 41 * @see android.hardware.Sensor
56 */ 42 */
57 private final Sensor mSensor; 43 private final Sensor mSensor;
58 44
59 /** 45 /**
60 * The minimum delay between two readings in microseconds that is supported by the sensor. 46 * The minimum delay between two readings in microseconds that is supported by the sensor.
61 */ 47 */
62 private final int mMinDelayUsec; 48 private final int mMinDelayUsec;
63 49
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 mMinDelayUsec = mSensor.getMinDelay(); 86 mMinDelayUsec = mSensor.getMinDelay();
101 } 87 }
102 88
103 /** 89 /**
104 * Initializes PlatformSensor, called by native code. 90 * Initializes PlatformSensor, called by native code.
105 * 91 *
106 * @param nativePlatformSensorAndroid identifier of device::PlatformSensorAn droid instance. 92 * @param nativePlatformSensorAndroid identifier of device::PlatformSensorAn droid instance.
107 * @param buffer shared buffer that is used to return data to the client. 93 * @param buffer shared buffer that is used to return data to the client.
108 */ 94 */
109 @CalledByNative 95 @CalledByNative
110 protected void initPlatformSensorAndroid(long nativePlatformSensorAndroid, B yteBuffer buffer) { 96 protected void initPlatformSensorAndroid(long nativePlatformSensorAndroid) {
111 assert nativePlatformSensorAndroid != 0; 97 assert nativePlatformSensorAndroid != 0;
112 assert buffer != null;
113 mNativePlatformSensorAndroid = nativePlatformSensorAndroid; 98 mNativePlatformSensorAndroid = nativePlatformSensorAndroid;
114 mBuffer = buffer;
115 mSensorReadingData = ByteBuffer.allocate(mBuffer.capacity());
116 mSensorReadingData.order(ByteOrder.nativeOrder());
117 } 99 }
118 100
119 /** 101 /**
120 * Fills shared buffer with sensor reading data, throws exception if number of received values
121 * is less than the number required for the PlatformSensor.
122 */
123 private void fillSensorReadingData(SensorEvent event, ByteBuffer buffer)
124 throws IllegalStateException {
125 if (event.values.length < mReadingCount) throw new IllegalStateException ();
126 for (int i = 0; i < mReadingCount; ++i) {
127 buffer.putDouble(event.values[i]);
128 }
129 }
130
131 /**
132 * Returns reporting mode supported by the sensor. 102 * Returns reporting mode supported by the sensor.
133 * 103 *
134 * @return ReportingMode reporting mode. 104 * @return ReportingMode reporting mode.
135 */ 105 */
136 @CalledByNative 106 @CalledByNative
137 protected int getReportingMode() { 107 protected int getReportingMode() {
138 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 108 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
139 return mSensor.getReportingMode() == Sensor.REPORTING_MODE_CONTINUOU S 109 return mSensor.getReportingMode() == Sensor.REPORTING_MODE_CONTINUOU S
140 ? ReportingMode.CONTINUOUS 110 ? ReportingMode.CONTINUOUS
141 : ReportingMode.ON_CHANGE; 111 : ReportingMode.ON_CHANGE;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 } 176 }
207 177
208 /** 178 /**
209 * Converts frequency to sampling period in microseconds. 179 * Converts frequency to sampling period in microseconds.
210 */ 180 */
211 private int getSamplingPeriod(double frequency) { 181 private int getSamplingPeriod(double frequency) {
212 return (int) ((1 / frequency) * MICROSECONDS_IN_SECOND); 182 return (int) ((1 / frequency) * MICROSECONDS_IN_SECOND);
213 } 183 }
214 184
215 /** 185 /**
216 * Notifies native device::PlatformSensorAndroid when reading is changed.
217 */
218 protected void sensorReadingChanged() {
219 nativeNotifyPlatformSensorReadingChanged(mNativePlatformSensorAndroid);
220 }
221
222 /**
223 * Notifies native device::PlatformSensorAndroid when there is an error. 186 * Notifies native device::PlatformSensorAndroid when there is an error.
224 */ 187 */
225 protected void sensorError() { 188 protected void sensorError() {
226 nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid); 189 nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid);
227 } 190 }
228 191
192 /**
193 * Updates reading at native device::PlatformSensorAndroid.
194 */
195 protected void updateSensorReading(
196 double timestamp, double value1, double value2, double value3) {
197 nativeUpdatePlatformSensorReading(
198 mNativePlatformSensorAndroid, timestamp, value1, value2, value3) ;
199 }
200
229 @Override 201 @Override
230 public void onAccuracyChanged(Sensor sensor, int accuracy) {} 202 public void onAccuracyChanged(Sensor sensor, int accuracy) {}
231 203
232 @Override 204 @Override
233 public void onSensorChanged(SensorEvent event) { 205 public void onSensorChanged(SensorEvent event) {
234 try { 206 if (event.values.length < mReadingCount) {
235 mSensorReadingData.mark();
236 // Convert timestamp from nanoseconds to milliseconds.
237 mSensorReadingData.putDouble(event.timestamp * MILLISECONDS_IN_NANOS ECOND);
238 fillSensorReadingData(event, mSensorReadingData);
239
240 mSensorReadingData.reset();
241
242 mBuffer.mark();
243 mBuffer.put(mSensorReadingData);
244 mSensorReadingData.reset();
245 mBuffer.reset();
246
247 if (getReportingMode() == ReportingMode.ON_CHANGE) {
248 sensorReadingChanged();
249 }
250 } catch (BufferOverflowException | IllegalStateException e) {
251 sensorError(); 207 sensorError();
252 stopSensor(); 208 stopSensor();
209 return;
210 }
211
212 double timestamp = event.timestamp * SECONDS_IN_NANOSECOND;
213 switch (event.values.length) {
214 case 1:
215 updateSensorReading(timestamp, event.values[0], 0.0, 0.0);
216 break;
217 case 2:
218 updateSensorReading(timestamp, event.values[0], event.values[1], 0.0);
219 break;
220 default:
221 updateSensorReading(timestamp, event.values[0], event.values[1], event.values[2]);
253 } 222 }
254 } 223 }
255 224
256 private native void nativeNotifyPlatformSensorReadingChanged(long nativePlat formSensorAndroid);
257 private native void nativeNotifyPlatformSensorError(long nativePlatformSenso rAndroid); 225 private native void nativeNotifyPlatformSensorError(long nativePlatformSenso rAndroid);
226 private native void nativeUpdatePlatformSensorReading(long nativePlatformSen sorAndroid,
227 double timestamp, double value1, double value2, double value3);
258 } 228 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698