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.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 { |
| 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 * Creates new PlatformSensor. |
| 81 * |
| 82 * @param sensorType type of the sensor to be constructed. @see android.hard
ware.Sensor.TYPE_* |
| 83 * @param readingCount number of sensor reading values required from the sen
sor. |
| 84 * @param provider object that shares SensorManager and polling thread Handl
er with sensors. |
| 85 */ |
| 86 public static PlatformSensor create( |
| 87 int sensorType, int readingCount, PlatformSensorProvider provider) { |
| 88 List<Sensor> sensors = provider.getSensorManager().getSensorList(sensorT
ype); |
| 89 if (sensors.isEmpty()) return null; |
| 90 return new PlatformSensor(sensors.get(0), readingCount, provider); |
| 91 } |
| 92 |
| 93 /** |
| 94 * Constructor. |
| 95 */ |
| 96 protected PlatformSensor(Sensor sensor, int readingCount, PlatformSensorProv
ider provider) { |
| 97 mReadingCount = readingCount; |
| 98 mProvider = provider; |
| 99 mSensor = sensor; |
| 100 mMinDelayUsec = mSensor.getMinDelay(); |
| 101 } |
| 102 |
| 103 /** |
| 104 * Initializes PlatformSensor, called by native code. |
| 105 * |
| 106 * @param nativePlatformSensorAndroid identifier of device::PlatformSensorAn
droid instance. |
| 107 * @param buffer shared buffer that is used to return data to the client. |
| 108 */ |
| 109 @CalledByNative |
| 110 protected void initPlatformSensorAndroid(long nativePlatformSensorAndroid, B
yteBuffer buffer) { |
| 111 assert nativePlatformSensorAndroid != 0; |
| 112 assert buffer != null; |
| 113 mNativePlatformSensorAndroid = nativePlatformSensorAndroid; |
| 114 mBuffer = buffer; |
| 115 mSensorReadingData = ByteBuffer.allocate(mBuffer.capacity()); |
| 116 mSensorReadingData.order(ByteOrder.nativeOrder()); |
| 117 } |
| 118 |
| 119 /** |
| 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. |
| 133 * |
| 134 * @return ReportingMode reporting mode. |
| 135 */ |
| 136 @CalledByNative |
| 137 protected int getReportingMode() { |
| 138 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 139 return mSensor.getReportingMode() == Sensor.REPORTING_MODE_CONTINUOU
S |
| 140 ? ReportingMode.CONTINUOUS |
| 141 : ReportingMode.ON_CHANGE; |
| 142 } |
| 143 return ReportingMode.CONTINUOUS; |
| 144 } |
| 145 |
| 146 /** |
| 147 * Returns default configuration supported by the sensor. Currently only fre
quency is supported. |
| 148 * |
| 149 * @return double frequency. |
| 150 */ |
| 151 @CalledByNative |
| 152 protected double getDefaultConfiguration() { |
| 153 return SENSOR_FREQUENCY_NORMAL; |
| 154 } |
| 155 |
| 156 /** |
| 157 * Requests sensor to start polling for data. |
| 158 * |
| 159 * @return boolean true if successful, false otherwise. |
| 160 */ |
| 161 @CalledByNative |
| 162 protected boolean startSensor(double frequency) { |
| 163 // If we already polling hw with same frequency, do not restart the sens
or. |
| 164 if (mCurrentPollingFrequency == frequency) return true; |
| 165 |
| 166 // Unregister old listener if polling frequency has changed. |
| 167 unregisterListener(); |
| 168 mProvider.sensorStarted(this); |
| 169 boolean sensorStarted = mProvider.getSensorManager().registerListener( |
| 170 this, mSensor, getSamplingPeriod(frequency), mProvider.getHandle
r()); |
| 171 |
| 172 if (!sensorStarted) { |
| 173 stopSensor(); |
| 174 return sensorStarted; |
| 175 } |
| 176 |
| 177 mCurrentPollingFrequency = frequency; |
| 178 return sensorStarted; |
| 179 } |
| 180 |
| 181 private void unregisterListener() { |
| 182 // Do not unregister if current polling frequency is 0, not polling for
data. |
| 183 if (mCurrentPollingFrequency == 0) return; |
| 184 mProvider.getSensorManager().unregisterListener(this, mSensor); |
| 185 } |
| 186 |
| 187 /** |
| 188 * Requests sensor to stop polling for data. |
| 189 */ |
| 190 @CalledByNative |
| 191 protected void stopSensor() { |
| 192 unregisterListener(); |
| 193 mProvider.sensorStopped(this); |
| 194 mCurrentPollingFrequency = 0; |
| 195 } |
| 196 |
| 197 /** |
| 198 * Checks whether configuration is supported by the sensor. Currently only f
requency is |
| 199 * supported. |
| 200 * |
| 201 * @return boolean true if configuration is supported, false otherwise. |
| 202 */ |
| 203 @CalledByNative |
| 204 protected boolean checkSensorConfiguration(double frequency) { |
| 205 return mMinDelayUsec <= getSamplingPeriod(frequency); |
| 206 } |
| 207 |
| 208 /** |
| 209 * Converts frequency to sampling period in microseconds. |
| 210 */ |
| 211 private int getSamplingPeriod(double frequency) { |
| 212 return (int) ((1 / frequency) * MICROSECONDS_IN_SECOND); |
| 213 } |
| 214 |
| 215 /** |
| 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. |
| 224 */ |
| 225 protected void sensorError() { |
| 226 nativeNotifyPlatformSensorError(mNativePlatformSensorAndroid); |
| 227 } |
| 228 |
| 229 @Override |
| 230 public void onAccuracyChanged(Sensor sensor, int accuracy) {} |
| 231 |
| 232 @Override |
| 233 public void onSensorChanged(SensorEvent event) { |
| 234 try { |
| 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(); |
| 252 stopSensor(); |
| 253 } |
| 254 } |
| 255 |
| 256 private native void nativeNotifyPlatformSensorReadingChanged(long nativePlat
formSensorAndroid); |
| 257 private native void nativeNotifyPlatformSensorError(long nativePlatformSenso
rAndroid); |
| 258 } |
OLD | NEW |