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

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

Issue 2284613002: [sensors] Android platform adaptation for Generic Sensor API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes for Ted.C review comments + unit tests. Created 4 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.device.sensors;
6
7 import android.hardware.Sensor;
8 import android.hardware.SensorEvent;
9 import android.hardware.SensorEventListener;
10 import android.os.Build;
11
12 import org.chromium.base.annotations.CalledByNative;
13 import org.chromium.base.annotations.JNINamespace;
14 import org.chromium.mojom.device.mojom.ReportingMode;
15
16 import java.nio.BufferOverflowException;
17 import java.nio.ByteBuffer;
18 import java.nio.ByteOrder;
19
20 import java.util.List;
21
22 /**
23 * Implementation of PlatformSensor that uses Android Sensor Framework. Lifetime is controlled by
24 * the device::PlatformSensorAndroid.
25 */
26 @JNINamespace("device")
27 public class PlatformSensor implements SensorEventListener, PlatformSensorNotifi er {
timvolodine 2016/09/06 17:44:37 would it make sense to have some tests for Platfor
shalamov 2016/09/07 13:53:42 They are in the same file PlatformSensorProviderTe
timvolodine 2016/09/07 14:51:05 oh I see, maybe just rename it to PlatformSensorAn
28 private static final double MICROSECONDS_IN_SECOND = 1000000;
29 private static final double MILLISECONDS_IN_NANOSECOND = 0.000001d;
30
31 /**
32 * 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
34 * microseconds.
35 */
36 private static final double SENSOR_FREQUENCY_NORMAL = 5.0d;
37
38 /**
39 * Identifier of device::PlatformSensorAndroid instance.
40 */
41 private long mNativePlatformSensorAndroid;
42
43 /**
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.
55 * @see android.hardware.Sensor
56 */
57 private final Sensor mSensor;
58
59 /**
60 * The minimum delay between two readings in microseconds that is supported by the sensor.
61 */
62 private final int mMinDelayUsec;
63
64 /**
65 * The number of sensor reading values required from the sensor.
66 */
67 private final int mReadingCount;
68
69 /**
70 * Frequncy that is currently used by the sensor for polling.
71 */
72 private double mCurrentPollingFrequency;
73
74 /**
75 * Provides shared SensorManager and event processing thread Handler to Plat formSensor objects.
76 */
77 private final PlatformSensorProvider mProvider;
78
79 /**
80 * Provides methods to notify native PlatformSensorAndroid.
81 */
82 private PlatformSensorNotifier mNotifier;
83
84 /**
85 * Creates new PlatformSensor.
86 *
87 * @param sensorType type of the sensor to be constructed. @see android.hard ware.Sensor.TYPE_*
88 * @param readingCount number of sensor reading values required from the sen sor.
89 * @param provider object that shares SensorManager and polling thread Handl er with sensors.
90 */
91 public static PlatformSensor create(
92 int sensorType, int readingCount, PlatformSensorProvider provider) {
93 List<Sensor> sensors = provider.getSensorManager().getSensorList(sensorT ype);
94 if (sensors.isEmpty()) return null;
95 return new PlatformSensor(sensors.get(0), readingCount, provider);
96 }
97
98 protected PlatformSensor(Sensor sensor, int readingCount, PlatformSensorProv ider provider) {
99 mReadingCount = readingCount;
100 mProvider = provider;
101 mSensor = sensor;
102 mMinDelayUsec = mSensor.getMinDelay();
103 }
104
105 /**
106 * Initializes PlatformSensor, called by native code.
107 *
108 * @param nativePlatformSensorAndroid identifier of device::PlatformSensorAn droid instance.
109 * @param buffer shared buffer that is used to return data to the client.
110 */
111 @CalledByNative
112 protected void initPlatformSensorAndroid(long nativePlatformSensorAndroid, B yteBuffer buffer) {
113 initPlatformSensorAndroid(this, nativePlatformSensorAndroid, buffer);
114 }
115
116 /**
117 * Initializes PlatformSensor.
118 *
119 * @param notifier object that is used to notify native device::PlatformSens orAndroid.
120 * @param nativePlatformSensorAndroid identifier of device::PlatformSensorAn droid instance.
121 * @param buffer shared buffer that is used to return data to the client.
122 */
123 protected void initPlatformSensorAndroid(
124 PlatformSensorNotifier notifier, long nativePlatformSensorAndroid, B yteBuffer buffer) {
125 assert nativePlatformSensorAndroid != 0;
126 assert buffer != null;
127 mNativePlatformSensorAndroid = nativePlatformSensorAndroid;
128 mNotifier = notifier;
129 mBuffer = buffer;
130 mSensorReadingData = ByteBuffer.allocate(mBuffer.capacity());
131 mSensorReadingData.order(ByteOrder.nativeOrder());
132 }
133
134 /**
135 * Fills shared buffer with sensor reading data, throws exception if number of received values
136 * is less than the number required for the PlatformSensor.
137 */
138 private void fillSensorReadingData(SensorEvent event, ByteBuffer buffer)
139 throws IllegalStateException {
140 if (event.values.length < mReadingCount) throw new IllegalStateException ();
141 for (int i = 0; i < mReadingCount; ++i) {
142 buffer.putDouble(event.values[i]);
143 }
144 }
145
146 /**
147 * Returns reporting mode supported by the sensor.
148 *
149 * @return ReportingMode reporting mode.
150 */
151 @CalledByNative
152 protected int getReportingMode() {
153 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
154 return mSensor.getReportingMode() == Sensor.REPORTING_MODE_CONTINUOU S
155 ? ReportingMode.CONTINUOUS
156 : ReportingMode.ON_CHANGE;
157 }
158 return ReportingMode.CONTINUOUS;
159 }
160
161 /**
162 * Returns default configuration supported by the sensor. Currently only fre quency is supported.
163 *
164 * @return double frequency.
165 */
166 @CalledByNative
167 protected double getDefaultConfiguration() {
168 return SENSOR_FREQUENCY_NORMAL;
169 }
170
171 /**
172 * Requests sensor to start polling for data.
173 *
174 * @return boolean true if successful, false otherwise.
175 */
176 @CalledByNative
177 protected boolean startSensor(double frequency) {
178 // If we already polling hw with same frequency, do not restart the sens or.
179 if (mCurrentPollingFrequency == frequency) return true;
180
181 // Unregister old listener if polling frequency has changed.
182 unregisterListener();
183 mProvider.sensorStarted(this);
184 boolean sensorStarted = mProvider.getSensorManager().registerListener(
185 this, mSensor, getSamplingPeriod(frequency), mProvider.getHandle r());
186
187 if (!sensorStarted) {
188 stopSensor();
189 return sensorStarted;
190 }
191
192 mCurrentPollingFrequency = frequency;
193 return sensorStarted;
194 }
195
196 private void unregisterListener() {
197 mProvider.getSensorManager().unregisterListener(this, mSensor);
198 }
199
200 /**
201 * Requests sensor to stop polling for data.
202 */
203 @CalledByNative
204 protected void stopSensor() {
205 unregisterListener();
206 mProvider.sensorStopped(this);
207 mCurrentPollingFrequency = 0;
208 }
209
210 /**
211 * Checks whether configuration is supported by the sensor. Currently only f requency is
212 * supported.
213 *
214 * @return boolean true if configuration is supported, false otherwise.
215 */
216 @CalledByNative
217 protected boolean checkSensorConfiguration(double frequency) {
218 return mMinDelayUsec <= getSamplingPeriod(frequency);
219 }
220
221 /**
222 * Converts frequency to sampling period in microseconds.
223 */
224 private int getSamplingPeriod(double frequency) {
225 return (int) ((1 / frequency) * MICROSECONDS_IN_SECOND);
226 }
227
228 @Override
229 public void sensorReadingChanged() {
230 nativeNotifyPlatformSensorReadingChanged(mNativePlatformSensorAndroid);
231 }
232
233 @Override
234 public void sensorError() {
235 nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid);
236 }
237
238 /**
239 * Notifies native device::PlatformSensorAndroid when reading is changed.
240 */
241 protected native void nativeNotifyPlatformSensorReadingChanged(
242 long nativePlatformSensorAndroid);
243
244 /**
245 * Notifies native device::PlatformSensorAndroid when there is an error.
246 */
247 protected native void nativeNotifyPlatformSensorError(long nativePlatformSen sorAndroid);
248
249 @Override
250 public void onAccuracyChanged(Sensor sensor, int accuracy) {}
251
252 @Override
253 public void onSensorChanged(SensorEvent event) {
254 try {
255 mSensorReadingData.mark();
256 // Convert timestamp from nanoseconds to milliseconds.
257 mSensorReadingData.putDouble(event.timestamp * MILLISECONDS_IN_NANOS ECOND);
258 fillSensorReadingData(event, mSensorReadingData);
259
260 mSensorReadingData.reset();
261
262 mBuffer.mark();
263 mBuffer.put(mSensorReadingData);
264 mSensorReadingData.reset();
265 mBuffer.reset();
266
267 if (getReportingMode() == ReportingMode.ON_CHANGE) {
268 mNotifier.sensorReadingChanged();
269 }
270 } catch (BufferOverflowException | IllegalStateException e) {
271 mNotifier.sensorError();
272 stopSensor();
273 }
274 }
275 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698