OLD | NEW |
---|---|
(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.hardware.SensorManager; | |
11 import android.os.Build; | |
12 import android.os.Handler; | |
13 | |
14 import org.chromium.mojom.device.mojom.ReportingMode; | |
15 | |
16 import java.nio.BufferOverflowException; | |
17 import java.nio.ByteBuffer; | |
18 | |
19 import java.util.List; | |
20 | |
21 /** | |
22 * Implementation of PlatformSensor that uses Android Sensor Framework. | |
23 */ | |
24 public class GenericPlatformSensor extends PlatformSensor implements SensorEvent Listener { | |
25 private static final double MICROSECONDS_PER_SECOND = 1000000; | |
26 /** | |
27 * SENSOR_FREQUENCY_NORMAL corresponds to SensorManager.SENSOR_DELAY_NORMAL which is | |
28 * defined as 200000 microseconds. | |
Ted C
2016/09/01 00:12:36
forgive me, but how is 5.0 related to 200000 micro
shalamov
2016/09/06 12:36:42
Done.
| |
29 */ | |
30 private static final double SENSOR_FREQUENCY_NORMAL = 5.0; | |
31 private final Sensor mSensor; | |
Ted C
2016/09/01 00:12:36
I would add a space between the statics and the lo
shalamov
2016/09/06 12:36:42
Done.
| |
32 private final SensorManager mSensorManager; | |
33 private final Handler mHandler; | |
34 private final int mMinDelay; | |
Ted C
2016/09/01 00:12:36
what are the units here? add a Suffix (Ms, Sec, e
shalamov
2016/09/06 12:36:43
Done.
| |
35 private final int mReadingCount; | |
36 | |
37 protected GenericPlatformSensor(int sensorType, int readingCount, SensorMana ger sensorManager, | |
Ted C
2016/09/01 00:12:35
javadoc w/ descriptions of these variables.
shalamov
2016/09/06 12:36:43
Done.
| |
38 Handler handler) throws PlatformSensorException { | |
39 mReadingCount = readingCount; | |
40 mSensorManager = sensorManager; | |
41 mHandler = handler; | |
42 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); | |
43 if (sensors.isEmpty()) throw new PlatformSensorException(); | |
Ted C
2016/09/01 00:12:35
we have exposed very few exceptions in our code...
shalamov
2016/09/06 12:36:42
Done.
| |
44 mSensor = sensors.get(0); | |
45 mMinDelay = mSensor.getMinDelay(); | |
46 } | |
47 | |
48 /** | |
49 * Fills shared buffer with sensor reading data, throws exception if number of received values | |
50 * is less than the number required for the PlatformSensor. | |
51 */ | |
52 protected void fillSensorReadingData(SensorEvent event, ByteBuffer buffer) | |
Ted C
2016/09/01 00:12:36
should this be private?
shalamov
2016/09/06 12:36:42
Done.
| |
53 throws PlatformSensorException { | |
54 if (event.values.length < mReadingCount) throw new PlatformSensorExcepti on(); | |
55 for (int i = 0; i < mReadingCount; ++i) { | |
56 buffer.putDouble(event.values[i]); | |
57 } | |
58 } | |
59 | |
60 @Override | |
61 protected int getReportingMode() { | |
62 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
63 return mSensor.getReportingMode() == Sensor.REPORTING_MODE_CONTINUOU S | |
64 ? ReportingMode.CONTINUOUS | |
65 : ReportingMode.ON_CHANGE; | |
66 } | |
67 return ReportingMode.CONTINUOUS; | |
68 } | |
69 | |
70 @Override | |
71 protected PlatformSensorConfiguration getDefaultConfiguration() { | |
72 return new PlatformSensorConfiguration(SENSOR_FREQUENCY_NORMAL); | |
73 } | |
74 | |
75 @Override | |
76 protected boolean startSensor(PlatformSensorConfiguration configuration) { | |
77 if (mNativePlatformSensorAndroid == 0 || mBuffer == null || mSensorReadi ngData == null) { | |
Ted C
2016/09/01 00:12:35
how could this be called if mNativePlatformSensorA
shalamov
2016/09/06 12:36:42
Done.
| |
78 return false; | |
79 } | |
80 | |
81 stopSensor(); | |
82 return mSensorManager.registerListener( | |
83 this, mSensor, getSamplingPeriod(configuration), mHandler); | |
84 } | |
85 | |
86 @Override | |
87 protected void stopSensor() { | |
88 mSensorManager.unregisterListener(this); | |
Ted C
2016/09/01 00:12:36
Hmm...should we actually be calling this:
https://
shalamov
2016/09/06 12:36:42
Done.
| |
89 } | |
90 | |
91 @Override | |
92 protected boolean checkSensorConfiguration(PlatformSensorConfiguration confi guration) { | |
93 return mMinDelay <= getSamplingPeriod(configuration); | |
94 } | |
95 | |
96 /** | |
97 * Converts frequency provided by the SensorConfiguration to sampling period in microseconds. | |
98 */ | |
99 private int getSamplingPeriod(PlatformSensorConfiguration configuration) { | |
100 if (configuration.getFrequency() > 0) { | |
Ted C
2016/09/01 00:12:36
is <= 0 a valid frequency? Should we prevent that
shalamov
2016/09/06 12:36:42
Checked in native side, cannot happen. Removed.
| |
101 return (int) ((1 / configuration.getFrequency()) * MICROSECONDS_PER_ SECOND); | |
102 } else { | |
103 return 0; | |
104 } | |
105 } | |
106 | |
107 @Override | |
108 public void onAccuracyChanged(Sensor sensor, int accuracy) {} | |
109 | |
110 @Override | |
111 public void onSensorChanged(SensorEvent event) { | |
112 try { | |
113 mSensorReadingData.mark(); | |
114 // Timestamp in milliseconds | |
115 mSensorReadingData.putDouble(event.timestamp * 0.000001d); | |
Ted C
2016/09/01 00:12:36
Make 0.000001d a constant too
shalamov
2016/09/06 12:36:42
Done.
| |
116 fillSensorReadingData(event, mSensorReadingData); | |
117 | |
118 mSensorReadingData.reset(); | |
119 | |
120 mBuffer.mark(); | |
121 mBuffer.put(mSensorReadingData); | |
122 mSensorReadingData.reset(); | |
123 mBuffer.reset(); | |
124 | |
125 if (getReportingMode() == ReportingMode.ON_CHANGE) { | |
126 nativeNotifyPlatformSensorReadingChanged(mNativePlatformSensorAn droid); | |
127 } | |
128 } catch (BufferOverflowException | PlatformSensorException e) { | |
129 nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid); | |
130 } | |
131 } | |
132 } | |
OLD | NEW |