OLD | NEW |
---|---|
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; | 16 import java.nio.ByteBuffer; |
shalamov
2016/10/06 12:46:09
not needed anymore.
Mikhail
2016/10/07 10:07:01
Done.
| |
18 import java.nio.ByteOrder; | |
19 | 17 |
20 import java.util.List; | 18 import java.util.List; |
21 | 19 |
22 /** | 20 /** |
23 * Implementation of PlatformSensor that uses Android Sensor Framework. Lifetime is controlled by | 21 * Implementation of PlatformSensor that uses Android Sensor Framework. Lifetime is controlled by |
24 * the device::PlatformSensorAndroid. | 22 * the device::PlatformSensorAndroid. |
25 */ | 23 */ |
26 @JNINamespace("device") | 24 @JNINamespace("device") |
27 public class PlatformSensor implements SensorEventListener { | 25 public class PlatformSensor implements SensorEventListener { |
28 private static final double MICROSECONDS_IN_SECOND = 1000000; | 26 private static final double MICROSECONDS_IN_SECOND = 1000000; |
29 private static final double MILLISECONDS_IN_NANOSECOND = 0.000001d; | 27 private static final double SECONDS_IN_NANOSECOND = 0.000000001d; |
30 | 28 |
31 /** | 29 /** |
32 * The SENSOR_FREQUENCY_NORMAL is defined as 5Hz which corresponds to a poll ing delay | 30 * 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 | 31 * @see android.hardware.SensorManager.SENSOR_DELAY_NORMAL value that is def ined as 200000 |
34 * microseconds. | 32 * microseconds. |
35 */ | 33 */ |
36 private static final double SENSOR_FREQUENCY_NORMAL = 5.0d; | 34 private static final double SENSOR_FREQUENCY_NORMAL = 5.0d; |
37 | 35 |
38 /** | 36 /** |
39 * Identifier of device::PlatformSensorAndroid instance. | 37 * Identifier of device::PlatformSensorAndroid instance. |
40 */ | 38 */ |
41 private long mNativePlatformSensorAndroid; | 39 private long mNativePlatformSensorAndroid; |
42 | 40 |
43 /** | 41 /** |
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. | 42 * Used for fetching sensor reading values and obtaining information about t he sensor. |
55 * @see android.hardware.Sensor | 43 * @see android.hardware.Sensor |
56 */ | 44 */ |
57 private final Sensor mSensor; | 45 private final Sensor mSensor; |
58 | 46 |
59 /** | 47 /** |
60 * The minimum delay between two readings in microseconds that is supported by the sensor. | 48 * The minimum delay between two readings in microseconds that is supported by the sensor. |
61 */ | 49 */ |
62 private final int mMinDelayUsec; | 50 private final int mMinDelayUsec; |
63 | 51 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 mMinDelayUsec = mSensor.getMinDelay(); | 88 mMinDelayUsec = mSensor.getMinDelay(); |
101 } | 89 } |
102 | 90 |
103 /** | 91 /** |
104 * Initializes PlatformSensor, called by native code. | 92 * Initializes PlatformSensor, called by native code. |
105 * | 93 * |
106 * @param nativePlatformSensorAndroid identifier of device::PlatformSensorAn droid instance. | 94 * @param nativePlatformSensorAndroid identifier of device::PlatformSensorAn droid instance. |
107 * @param buffer shared buffer that is used to return data to the client. | 95 * @param buffer shared buffer that is used to return data to the client. |
108 */ | 96 */ |
109 @CalledByNative | 97 @CalledByNative |
110 protected void initPlatformSensorAndroid(long nativePlatformSensorAndroid, B yteBuffer buffer) { | 98 protected void initPlatformSensorAndroid(long nativePlatformSensorAndroid) { |
111 assert nativePlatformSensorAndroid != 0; | 99 assert nativePlatformSensorAndroid != 0; |
112 assert buffer != null; | |
113 mNativePlatformSensorAndroid = nativePlatformSensorAndroid; | 100 mNativePlatformSensorAndroid = nativePlatformSensorAndroid; |
114 mBuffer = buffer; | |
115 mSensorReadingData = ByteBuffer.allocate(mBuffer.capacity()); | |
116 mSensorReadingData.order(ByteOrder.nativeOrder()); | |
117 } | 101 } |
118 | 102 |
119 /** | 103 /** |
120 * Fills shared buffer with sensor reading data, throws exception if number of received values | 104 * Fills shared buffer with sensor reading data, throws exception if number of received values |
121 * is less than the number required for the PlatformSensor. | 105 * is less than the number required for the PlatformSensor. |
122 */ | 106 */ |
123 private void fillSensorReadingData(SensorEvent event, ByteBuffer buffer) | 107 private void fillSensorReadingData(SensorEvent event, ByteBuffer buffer) |
shalamov
2016/10/06 12:46:09
not needed anymore.
Mikhail
2016/10/07 10:07:02
Done.
| |
124 throws IllegalStateException { | 108 throws IllegalStateException { |
125 if (event.values.length < mReadingCount) throw new IllegalStateException (); | 109 if (event.values.length < mReadingCount) throw new IllegalStateException (); |
126 for (int i = 0; i < mReadingCount; ++i) { | 110 for (int i = 0; i < mReadingCount; ++i) { |
127 buffer.putDouble(event.values[i]); | 111 buffer.putDouble(event.values[i]); |
128 } | 112 } |
129 } | 113 } |
130 | 114 |
131 /** | 115 /** |
132 * Returns reporting mode supported by the sensor. | 116 * Returns reporting mode supported by the sensor. |
133 * | 117 * |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 } | 190 } |
207 | 191 |
208 /** | 192 /** |
209 * Converts frequency to sampling period in microseconds. | 193 * Converts frequency to sampling period in microseconds. |
210 */ | 194 */ |
211 private int getSamplingPeriod(double frequency) { | 195 private int getSamplingPeriod(double frequency) { |
212 return (int) ((1 / frequency) * MICROSECONDS_IN_SECOND); | 196 return (int) ((1 / frequency) * MICROSECONDS_IN_SECOND); |
213 } | 197 } |
214 | 198 |
215 /** | 199 /** |
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. | 200 * Notifies native device::PlatformSensorAndroid when there is an error. |
224 */ | 201 */ |
225 protected void sensorError() { | 202 protected void sensorError() { |
226 nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid); | 203 nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid); |
227 } | 204 } |
228 | 205 |
206 /** | |
207 * Updates reading at native device::PlatformSensorAndroid. | |
208 */ | |
209 protected void updateSensorReading( | |
210 double timestamp, double value1, double value2, double value3) { | |
211 nativeUpdatePlatformSensorReading( | |
212 mNativePlatformSensorAndroid, timestamp, value1, value2, value3) ; | |
213 } | |
214 | |
229 @Override | 215 @Override |
230 public void onAccuracyChanged(Sensor sensor, int accuracy) {} | 216 public void onAccuracyChanged(Sensor sensor, int accuracy) {} |
231 | 217 |
232 @Override | 218 @Override |
233 public void onSensorChanged(SensorEvent event) { | 219 public void onSensorChanged(SensorEvent event) { |
234 try { | 220 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(); | 221 sensorError(); |
252 stopSensor(); | 222 stopSensor(); |
223 return; | |
224 } | |
225 | |
226 double timestamp = event.timestamp * SECONDS_IN_NANOSECOND; | |
227 switch (event.values.length) { | |
228 case 1: | |
229 updateSensorReading(timestamp, event.values[0], 0.0, 0.0); | |
230 break; | |
231 case 2: | |
232 updateSensorReading(timestamp, event.values[0], event.values[1], 0.0); | |
233 break; | |
234 default: | |
235 updateSensorReading(timestamp, event.values[0], event.values[1], event.values[2]); | |
253 } | 236 } |
254 } | 237 } |
255 | 238 |
256 private native void nativeNotifyPlatformSensorReadingChanged(long nativePlat formSensorAndroid); | |
257 private native void nativeNotifyPlatformSensorError(long nativePlatformSenso rAndroid); | 239 private native void nativeNotifyPlatformSensorError(long nativePlatformSenso rAndroid); |
240 private native void nativeUpdatePlatformSensorReading(long nativePlatformSen sorAndroid, | |
241 double timestamp, double value1, double value2, double value3); | |
258 } | 242 } |
OLD | NEW |