blob: 7297426130ac4a2994c226f02dbfcd6cf60b4d83 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.hardware;
19
Nick Vaccarof5021f62015-01-23 15:09:45 -080020import android.annotation.SystemApi;
Peng Xua35b5532016-01-20 00:05:45 -080021import android.os.Build;
22
Mathias Agopian74cde2c2010-06-16 18:55:46 -070023/**
24 * Class representing a sensor. Use {@link SensorManager#getSensorList} to get
Jonathan Dormody69151702018-01-11 13:18:59 -070025 * the list of available sensors. For more information about Android sensors,
26 * read the
27 * <a href="/guide/topics/sensors/sensors_motion.html">Motion Sensors guide</a>.</p>
Mathias Agopian0f791a72010-06-22 21:55:01 -070028 *
29 * @see SensorManager
30 * @see SensorEventListener
31 * @see SensorEvent
32 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 */
Jeff Brown6a2ef122012-08-10 18:19:10 -070034public final class Sensor {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
Mathias Agopian74cde2c2010-06-16 18:55:46 -070036 /**
Scott Main92053f82013-06-13 16:32:50 -070037 * A constant describing an accelerometer sensor type.
38 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
39 * for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 */
Mathias Agopian74cde2c2010-06-16 18:55:46 -070041 public static final int TYPE_ACCELEROMETER = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Mathias Agopian74cde2c2010-06-16 18:55:46 -070043 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +000044 * A constant string describing an accelerometer sensor type.
45 *
46 * @see #TYPE_ACCELEROMETER
47 */
48 public static final String STRING_TYPE_ACCELEROMETER = "android.sensor.accelerometer";
49
50 /**
Scott Main92053f82013-06-13 16:32:50 -070051 * A constant describing a magnetic field sensor type.
52 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
53 * for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 */
55 public static final int TYPE_MAGNETIC_FIELD = 2;
Mathias Agopian74cde2c2010-06-16 18:55:46 -070056
57 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +000058 * A constant string describing a magnetic field sensor type.
59 *
60 * @see #TYPE_MAGNETIC_FIELD
61 */
62 public static final String STRING_TYPE_MAGNETIC_FIELD = "android.sensor.magnetic_field";
63
64 /**
Scott Main92053f82013-06-13 16:32:50 -070065 * A constant describing an orientation sensor type.
66 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
67 * for more details.
Mathias Agopian74cde2c2010-06-16 18:55:46 -070068 *
Mathias Agopian61a44932010-02-26 18:06:26 -080069 * @deprecated use {@link android.hardware.SensorManager#getOrientation
Mathias Agopian74cde2c2010-06-16 18:55:46 -070070 * SensorManager.getOrientation()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
Mathias Agopian61a44932010-02-26 18:06:26 -080072 @Deprecated
Mathias Agopian74cde2c2010-06-16 18:55:46 -070073 public static final int TYPE_ORIENTATION = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
Aravind Akellacd9a7bb2014-04-07 22:55:21 +000075 /**
76 * A constant string describing an orientation sensor type.
77 *
78 * @see #TYPE_ORIENTATION
79 * @deprecated use {@link android.hardware.SensorManager#getOrientation
80 * SensorManager.getOrientation()} instead.
81 */
82 @Deprecated
83 public static final String STRING_TYPE_ORIENTATION = "android.sensor.orientation";
84
85 /**
86 * A constant describing a gyroscope sensor type.
Scott Main92053f82013-06-13 16:32:50 -070087 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
88 * for more details. */
Mathias Agopian74cde2c2010-06-16 18:55:46 -070089 public static final int TYPE_GYROSCOPE = 4;
90
Mike Lockwood4115c512009-11-03 10:35:43 -050091 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +000092 * A constant string describing a gyroscope sensor type.
93 *
94 * @see #TYPE_GYROSCOPE
95 */
96 public static final String STRING_TYPE_GYROSCOPE = "android.sensor.gyroscope";
97
98 /**
Scott Main92053f82013-06-13 16:32:50 -070099 * A constant describing a light sensor type.
100 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
101 * for more details.
Mike Lockwood4115c512009-11-03 10:35:43 -0500102 */
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700103 public static final int TYPE_LIGHT = 5;
Mathias Agopian210fc912010-02-26 13:51:39 -0800104
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000105 /**
106 * A constant string describing a light sensor type.
107 *
108 * @see #TYPE_LIGHT
109 */
110 public static final String STRING_TYPE_LIGHT = "android.sensor.light";
111
112 /**
113 * A constant describing a pressure sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700114 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000115 * for more details.
116 */
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700117 public static final int TYPE_PRESSURE = 6;
Mathias Agopian210fc912010-02-26 13:51:39 -0800118
Mathias Agopian462db222011-03-22 18:44:26 -0700119 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000120 * A constant string describing a pressure sensor type.
121 *
122 * @see #TYPE_PRESSURE
123 */
124 public static final String STRING_TYPE_PRESSURE = "android.sensor.pressure";
125
126 /**
Mathias Agopian462db222011-03-22 18:44:26 -0700127 * A constant describing a temperature sensor type
128 *
129 * @deprecated use
130 * {@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE
131 * Sensor.TYPE_AMBIENT_TEMPERATURE} instead.
132 */
133 @Deprecated
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700134 public static final int TYPE_TEMPERATURE = 7;
Mathias Agopian210fc912010-02-26 13:51:39 -0800135
Mike Lockwood4115c512009-11-03 10:35:43 -0500136 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000137 * A constant string describing a temperature sensor type
138 *
139 * @see #TYPE_TEMPERATURE
140 * @deprecated use
141 * {@link android.hardware.Sensor#STRING_TYPE_AMBIENT_TEMPERATURE
142 * Sensor.STRING_TYPE_AMBIENT_TEMPERATURE} instead.
143 */
144 @Deprecated
145 public static final String STRING_TYPE_TEMPERATURE = "android.sensor.temperature";
146
147 /**
Aravind Akella2f8b9142014-05-12 14:39:29 -0700148 * A constant describing a proximity sensor type. This is a wake up sensor.
Scott Main92053f82013-06-13 16:32:50 -0700149 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
150 * for more details.
Aravind Akella2f8b9142014-05-12 14:39:29 -0700151 * @see #isWakeUpSensor()
Mike Lockwood4115c512009-11-03 10:35:43 -0500152 */
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700153 public static final int TYPE_PROXIMITY = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700155 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000156 * A constant string describing a proximity sensor type.
157 *
158 * @see #TYPE_PROXIMITY
159 */
160 public static final String STRING_TYPE_PROXIMITY = "android.sensor.proximity";
161
162 /**
Kevin Powellf35fd952010-07-19 19:10:40 -0700163 * A constant describing a gravity sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700164 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Kevin Powellf35fd952010-07-19 19:10:40 -0700165 * for more details.
166 */
167 public static final int TYPE_GRAVITY = 9;
168
169 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000170 * A constant string describing a gravity sensor type.
171 *
172 * @see #TYPE_GRAVITY
173 */
174 public static final String STRING_TYPE_GRAVITY = "android.sensor.gravity";
175
176 /**
Kevin Powellf35fd952010-07-19 19:10:40 -0700177 * A constant describing a linear acceleration sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700178 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Kevin Powellf35fd952010-07-19 19:10:40 -0700179 * for more details.
180 */
181 public static final int TYPE_LINEAR_ACCELERATION = 10;
182
183 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000184 * A constant string describing a linear acceleration sensor type.
185 *
186 * @see #TYPE_LINEAR_ACCELERATION
187 */
188 public static final String STRING_TYPE_LINEAR_ACCELERATION =
Peng Xufb1c9412016-03-29 21:50:43 -0700189 "android.sensor.linear_acceleration";
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000190
191 /**
Kevin Powellf35fd952010-07-19 19:10:40 -0700192 * A constant describing a rotation vector sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700193 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Kevin Powellf35fd952010-07-19 19:10:40 -0700194 * for more details.
195 */
196 public static final int TYPE_ROTATION_VECTOR = 11;
197
Urs Fleisch58190512010-12-29 17:02:02 +0100198 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000199 * A constant string describing a rotation vector sensor type.
200 *
201 * @see #TYPE_ROTATION_VECTOR
202 */
203 public static final String STRING_TYPE_ROTATION_VECTOR = "android.sensor.rotation_vector";
204
205 /**
Urs Fleisch58190512010-12-29 17:02:02 +0100206 * A constant describing a relative humidity sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700207 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Urs Fleisch58190512010-12-29 17:02:02 +0100208 * for more details.
209 */
210 public static final int TYPE_RELATIVE_HUMIDITY = 12;
211
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000212 /**
213 * A constant string describing a relative humidity sensor type
214 *
215 * @see #TYPE_RELATIVE_HUMIDITY
216 */
217 public static final String STRING_TYPE_RELATIVE_HUMIDITY = "android.sensor.relative_humidity";
218
219 /**
220 * A constant describing an ambient temperature sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700221 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000222 * for more details.
223 */
Mathias Agopian462db222011-03-22 18:44:26 -0700224 public static final int TYPE_AMBIENT_TEMPERATURE = 13;
225
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800226 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000227 * A constant string describing an ambient temperature sensor type.
228 *
229 * @see #TYPE_AMBIENT_TEMPERATURE
230 */
231 public static final String STRING_TYPE_AMBIENT_TEMPERATURE =
Peng Xufb1c9412016-03-29 21:50:43 -0700232 "android.sensor.ambient_temperature";
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000233
234 /**
Scott Main92053f82013-06-13 16:32:50 -0700235 * A constant describing an uncalibrated magnetic field sensor type.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800236 * <p>
Scott Main92053f82013-06-13 16:32:50 -0700237 * Similar to {@link #TYPE_MAGNETIC_FIELD} but the hard iron calibration (device calibration
238 * due to distortions that arise from magnetized iron, steel or permanent magnets on the
239 * device) is not considered in the given sensor values. However, such hard iron bias values
240 * are returned to you separately in the result {@link android.hardware.SensorEvent#values}
Aravind Akella4b628cb2013-08-05 18:43:55 -0700241 * so you may use them for custom calibrations.
Scott Main92053f82013-06-13 16:32:50 -0700242 * <p>Also, no periodic calibration is performed
243 * (i.e. there are no discontinuities in the data stream while using this sensor) and
244 * assumptions that the magnetic field is due to the Earth's poles is avoided, but
245 * factory calibration and temperature compensation have been performed.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800246 * </p>
Scott Main92053f82013-06-13 16:32:50 -0700247 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
248 * details.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800249 */
250 public static final int TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14;
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000251 /**
252 * A constant string describing an uncalibrated magnetic field sensor type.
253 *
254 * @see #TYPE_MAGNETIC_FIELD_UNCALIBRATED
255 */
256 public static final String STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED =
Peng Xufb1c9412016-03-29 21:50:43 -0700257 "android.sensor.magnetic_field_uncalibrated";
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800258
259 /**
Scott Main92053f82013-06-13 16:32:50 -0700260 * A constant describing an uncalibrated rotation vector sensor type.
261 * <p>Identical to {@link #TYPE_ROTATION_VECTOR} except that it doesn't
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800262 * use the geomagnetic field. Therefore the Y axis doesn't
263 * point north, but instead to some other reference, that reference is
264 * allowed to drift by the same order of magnitude as the gyroscope
265 * drift around the Z axis.
266 * <p>
267 * In the ideal case, a phone rotated and returning to the same real-world
268 * orientation should report the same game rotation vector
269 * (without using the earth's geomagnetic field). However, the orientation
270 * may drift somewhat over time.
271 * </p>
Scott Main92053f82013-06-13 16:32:50 -0700272 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
273 * details.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800274 */
Etienne Le Grand819239e2014-04-05 04:47:14 +0000275 public static final int TYPE_GAME_ROTATION_VECTOR = 15;
Etienne Le Grandfd53d832014-03-24 21:29:28 -0700276
277 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000278 * A constant string describing an uncalibrated rotation vector sensor type.
279 *
280 * @see #TYPE_GAME_ROTATION_VECTOR
281 */
282 public static final String STRING_TYPE_GAME_ROTATION_VECTOR =
Peng Xufb1c9412016-03-29 21:50:43 -0700283 "android.sensor.game_rotation_vector";
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000284
285 /**
Scott Main92053f82013-06-13 16:32:50 -0700286 * A constant describing an uncalibrated gyroscope sensor type.
287 * <p>Similar to {@link #TYPE_GYROSCOPE} but no gyro-drift compensation has been performed
288 * to adjust the given sensor values. However, such gyro-drift bias values
289 * are returned to you separately in the result {@link android.hardware.SensorEvent#values}
290 * so you may use them for custom calibrations.
291 * <p>Factory calibration and temperature compensation is still applied
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800292 * to the rate of rotation (angular speeds).
293 * </p>
Scott Main92053f82013-06-13 16:32:50 -0700294 * <p> See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
295 * details.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800296 */
297 public static final int TYPE_GYROSCOPE_UNCALIBRATED = 16;
298
299 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000300 * A constant string describing an uncalibrated gyroscope sensor type.
301 *
302 * @see #TYPE_GYROSCOPE_UNCALIBRATED
303 */
304 public static final String STRING_TYPE_GYROSCOPE_UNCALIBRATED =
Peng Xufb1c9412016-03-29 21:50:43 -0700305 "android.sensor.gyroscope_uncalibrated";
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000306
307 /**
Aravind Akella4b628cb2013-08-05 18:43:55 -0700308 * A constant describing a significant motion trigger sensor.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800309 * <p>
310 * It triggers when an event occurs and then automatically disables
311 * itself. The sensor continues to operate while the device is asleep
312 * and will automatically wake the device to notify when significant
313 * motion is detected. The application does not need to hold any wake
Aravind Akella2f8b9142014-05-12 14:39:29 -0700314 * locks for this sensor to trigger. This is a wake up sensor.
Scott Main92053f82013-06-13 16:32:50 -0700315 * <p>See {@link TriggerEvent} for more details.
Aravind Akella2f8b9142014-05-12 14:39:29 -0700316 *
317 * @see #isWakeUpSensor()
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800318 */
319 public static final int TYPE_SIGNIFICANT_MOTION = 17;
320
321 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000322 * A constant string describing a significant motion trigger sensor.
323 *
324 * @see #TYPE_SIGNIFICANT_MOTION
325 */
326 public static final String STRING_TYPE_SIGNIFICANT_MOTION =
Peng Xufb1c9412016-03-29 21:50:43 -0700327 "android.sensor.significant_motion";
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000328
329 /**
Aravind Akella4b628cb2013-08-05 18:43:55 -0700330 * A constant describing a step detector sensor.
331 * <p>
332 * A sensor of this type triggers an event each time a step is taken by the user. The only
333 * allowed value to return is 1.0 and an event is generated for each step. Like with any other
334 * event, the timestamp indicates when the event (here the step) occurred, this corresponds to
Aravind Akella1f9b32f2014-09-16 15:21:44 -0700335 * when the foot hit the ground, generating a high variation in acceleration. This sensor is
336 * only for detecting every individual step as soon as it is taken, for example to perform dead
337 * reckoning. If you only need aggregate number of steps taken over a period of time, register
338 * for {@link #TYPE_STEP_COUNTER} instead. It is defined as a
339 * {@link Sensor#REPORTING_MODE_SPECIAL_TRIGGER} sensor.
Aravind Akella4b628cb2013-08-05 18:43:55 -0700340 * <p>
341 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
342 */
343 public static final int TYPE_STEP_DETECTOR = 18;
344
345 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000346 * A constant string describing a step detector sensor.
347 *
348 * @see #TYPE_STEP_DETECTOR
349 */
350 public static final String STRING_TYPE_STEP_DETECTOR = "android.sensor.step_detector";
351
352 /**
Aravind Akella4b628cb2013-08-05 18:43:55 -0700353 * A constant describing a step counter sensor.
354 * <p>
355 * A sensor of this type returns the number of steps taken by the user since the last reboot
356 * while activated. The value is returned as a float (with the fractional part set to zero) and
357 * is reset to zero only on a system reboot. The timestamp of the event is set to the time when
Aravind Akella0c44d692014-08-19 18:28:12 -0700358 * the last step for that event was taken. This sensor is implemented in hardware and is
Aravind Akella1f9b32f2014-09-16 15:21:44 -0700359 * expected to be low power. If you want to continuously track the number of steps over a long
360 * period of time, do NOT unregister for this sensor, so that it keeps counting steps in the
361 * background even when the AP is in suspend mode and report the aggregate count when the AP
362 * is awake. Application needs to stay registered for this sensor because step counter does not
363 * count steps if it is not activated. This sensor is ideal for fitness tracking applications.
364 * It is defined as an {@link Sensor#REPORTING_MODE_ON_CHANGE} sensor.
Aravind Akella4b628cb2013-08-05 18:43:55 -0700365 * <p>
366 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
367 */
368 public static final int TYPE_STEP_COUNTER = 19;
369
370 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000371 * A constant string describing a step counter sensor.
372 *
373 * @see #TYPE_STEP_COUNTER
374 */
375 public static final String STRING_TYPE_STEP_COUNTER = "android.sensor.step_counter";
376
377 /**
378 * A constant describing a geo-magnetic rotation vector.
Aravind Akella4b628cb2013-08-05 18:43:55 -0700379 * <p>
Aravind Akellaf0f4a832013-08-07 20:05:29 -0700380 * Similar to {@link #TYPE_ROTATION_VECTOR}, but using a magnetometer instead of using a
Aravind Akella4b628cb2013-08-05 18:43:55 -0700381 * gyroscope. This sensor uses lower power than the other rotation vectors, because it doesn't
382 * use the gyroscope. However, it is more noisy and will work best outdoors.
383 * <p>
384 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
385 */
386 public static final int TYPE_GEOMAGNETIC_ROTATION_VECTOR = 20;
387
388 /**
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000389 * A constant string describing a geo-magnetic rotation vector.
390 *
391 * @see #TYPE_GEOMAGNETIC_ROTATION_VECTOR
392 */
393 public static final String STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR =
Peng Xufb1c9412016-03-29 21:50:43 -0700394 "android.sensor.geomagnetic_rotation_vector";
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000395
396 /**
397 * A constant describing a heart rate monitor.
398 * <p>
Etienne Le Grandaf805102014-05-16 12:21:41 -0700399 * The reported value is the heart rate in beats per minute.
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000400 * <p>
Etienne Le Grandaf805102014-05-16 12:21:41 -0700401 * The reported accuracy represents the status of the monitor during the reading. See the
402 * {@code SENSOR_STATUS_*} constants in {@link android.hardware.SensorManager SensorManager}
403 * for more details on accuracy/status values. In particular, when the accuracy is
404 * {@code SENSOR_STATUS_UNRELIABLE} or {@code SENSOR_STATUS_NO_CONTACT}, the heart rate
405 * value should be discarded.
406 * <p>
407 * This sensor requires permission {@code android.permission.BODY_SENSORS}.
408 * It will not be returned by {@code SensorManager.getSensorsList} nor
409 * {@code SensorManager.getDefaultSensor} if the application doesn't have this permission.
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000410 */
411 public static final int TYPE_HEART_RATE = 21;
412
413 /**
414 * A constant string describing a heart rate monitor.
415 *
416 * @see #TYPE_HEART_RATE
417 */
418 public static final String STRING_TYPE_HEART_RATE = "android.sensor.heart_rate";
419
420 /**
Aravind Akella2f8b9142014-05-12 14:39:29 -0700421 * A sensor of this type generates an event each time a tilt event is detected. A tilt event
422 * is generated if the direction of the 2-seconds window average gravity changed by at
423 * least 35 degrees since the activation of the sensor. It is a wake up sensor.
424 *
Aravind Akella27900352014-06-03 19:20:42 -0700425 * @hide
Aravind Akella2f8b9142014-05-12 14:39:29 -0700426 * @see #isWakeUpSensor()
427 */
Aravind Akella0288ca62014-07-28 14:53:44 -0700428 public static final int TYPE_TILT_DETECTOR = 22;
Aravind Akella2f8b9142014-05-12 14:39:29 -0700429
430 /**
431 * A constant string describing a wake up tilt detector sensor type.
432 *
Aravind Akella27900352014-06-03 19:20:42 -0700433 * @hide
Peng Xufb1c9412016-03-29 21:50:43 -0700434 * @see #TYPE_TILT_DETECTOR
Aravind Akella2f8b9142014-05-12 14:39:29 -0700435 */
Aravind Akella0288ca62014-07-28 14:53:44 -0700436 public static final String SENSOR_STRING_TYPE_TILT_DETECTOR =
437 "android.sensor.tilt_detector";
Aravind Akella2f8b9142014-05-12 14:39:29 -0700438
439 /**
Etienne Le Grand28071e32014-05-07 19:47:43 -0700440 * A constant describing a wake gesture sensor.
441 * <p>
442 * Wake gesture sensors enable waking up the device based on a device specific motion.
443 * <p>
444 * When this sensor triggers, the device behaves as if the power button was pressed, turning the
445 * screen on. This behavior (turning on the screen when this sensor triggers) might be
446 * deactivated by the user in the device settings. Changes in settings do not impact the
447 * behavior of the sensor: only whether the framework turns the screen on when it triggers.
448 * <p>
449 * The actual gesture to be detected is not specified, and can be chosen by the manufacturer of
450 * the device. This sensor must be low power, as it is likely to be activated 24/7.
451 * Values of events created by this sensors should not be used.
452 *
Aravind Akella2f8b9142014-05-12 14:39:29 -0700453 * @see #isWakeUpSensor()
Jeff Brown241e70d2014-07-17 15:12:37 -0700454 * @hide This sensor is expected to only be used by the system ui
Etienne Le Grand28071e32014-05-07 19:47:43 -0700455 */
Aravind Akella0288ca62014-07-28 14:53:44 -0700456 public static final int TYPE_WAKE_GESTURE = 23;
Etienne Le Grand28071e32014-05-07 19:47:43 -0700457
458 /**
459 * A constant string describing a wake gesture sensor.
460 *
Jeff Brown241e70d2014-07-17 15:12:37 -0700461 * @hide This sensor is expected to only be used by the system ui
Etienne Le Grand28071e32014-05-07 19:47:43 -0700462 * @see #TYPE_WAKE_GESTURE
463 */
464 public static final String STRING_TYPE_WAKE_GESTURE = "android.sensor.wake_gesture";
465
466 /**
Jeff Brown241e70d2014-07-17 15:12:37 -0700467 * A constant describing a wake gesture sensor.
468 * <p>
469 * A sensor enabling briefly turning the screen on to enable the user to
470 * glance content on screen based on a specific motion. The device should
471 * turn the screen off after a few moments.
472 * <p>
473 * When this sensor triggers, the device turns the screen on momentarily
474 * to allow the user to glance notifications or other content while the
475 * device remains locked in a non-interactive state (dozing). This behavior
476 * (briefly turning on the screen when this sensor triggers) might be deactivated
477 * by the user in the device settings. Changes in settings do not impact the
478 * behavior of the sensor: only whether the framework briefly turns the screen on
479 * when it triggers.
480 * <p>
481 * The actual gesture to be detected is not specified, and can be chosen by the manufacturer of
482 * the device. This sensor must be low power, as it is likely to be activated 24/7.
483 * Values of events created by this sensors should not be used.
484 *
485 * @see #isWakeUpSensor()
486 * @hide This sensor is expected to only be used by the system ui
487 */
Aravind Akella0288ca62014-07-28 14:53:44 -0700488 public static final int TYPE_GLANCE_GESTURE = 24;
Jeff Brown241e70d2014-07-17 15:12:37 -0700489
490 /**
491 * A constant string describing a wake gesture sensor.
492 *
493 * @hide This sensor is expected to only be used by the system ui
494 * @see #TYPE_GLANCE_GESTURE
495 */
496 public static final String STRING_TYPE_GLANCE_GESTURE = "android.sensor.glance_gesture";
497
Peng Xufb1c9412016-03-29 21:50:43 -0700498 /**
Aravind Akella96eb62c2014-07-17 12:42:00 -0700499 * A constant describing a pick up sensor.
500 *
501 * A sensor of this type triggers when the device is picked up regardless of wherever it was
502 * before (desk, pocket, bag). The only allowed return value is 1.0. This sensor deactivates
503 * itself immediately after it triggers.
504 *
505 * @hide Expected to be used internally for always on display.
506 */
Aravind Akella0288ca62014-07-28 14:53:44 -0700507 public static final int TYPE_PICK_UP_GESTURE = 25;
Aravind Akella96eb62c2014-07-17 12:42:00 -0700508
509 /**
510 * A constant string describing a pick up sensor.
511 *
512 * @hide This sensor is expected to be used internally for always on display.
513 * @see #TYPE_PICK_UP_GESTURE
514 */
515 public static final String STRING_TYPE_PICK_UP_GESTURE = "android.sensor.pick_up_gesture";
516
Peng Xufb1c9412016-03-29 21:50:43 -0700517 /**
Nick Vaccarof5021f62015-01-23 15:09:45 -0800518 * A constant describing a wrist tilt gesture sensor.
519 *
520 * A sensor of this type triggers when the device face is tilted towards the user.
521 * The only allowed return value is 1.0.
522 * This sensor remains active until disabled.
523 *
524 * @hide This sensor is expected to only be used by the system ui
525 */
526 @SystemApi
527 public static final int TYPE_WRIST_TILT_GESTURE = 26;
528
529 /**
530 * A constant string describing a wrist tilt gesture sensor.
531 *
532 * @hide This sensor is expected to only be used by the system ui
533 * @see #TYPE_WRIST_TILT_GESTURE
534 */
535 @SystemApi
536 public static final String STRING_TYPE_WRIST_TILT_GESTURE = "android.sensor.wrist_tilt_gesture";
537
Jeff Brown241e70d2014-07-17 15:12:37 -0700538 /**
Trevor Bunker3c288e62016-01-08 11:11:13 -0800539 * The current orientation of the device.
540 * <p>
541 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
542 *
543 * @hide Expected to be used internally for auto-rotate and speaker rotation.
544 *
545 */
546 public static final int TYPE_DEVICE_ORIENTATION = 27;
547
548 /**
549 * A constant string describing a device orientation sensor type.
550 *
551 * @hide
552 * @see #TYPE_DEVICE_ORIENTATION
553 */
554 public static final String STRING_TYPE_DEVICE_ORIENTATION = "android.sensor.device_orientation";
555
Peng Xufb1c9412016-03-29 21:50:43 -0700556 /**
Ashutosh Joshi17efe022016-01-14 22:23:58 -0800557 * A constant describing a pose sensor with 6 degrees of freedom.
558 *
559 * Similar to {@link #TYPE_ROTATION_VECTOR}, with additional delta
560 * translation from an arbitrary reference point.
561 *
Ashutosh Joshiaf1a2662016-01-25 20:24:20 -0800562 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
563 *
Ashutosh Joshi17efe022016-01-14 22:23:58 -0800564 * Can use camera, depth sensor etc to compute output value.
565 *
566 * This is expected to be a high power sensor and expected only to be
567 * used when the screen is on.
568 *
569 * Expected to be more accurate than the rotation vector alone.
570 *
571 */
572 public static final int TYPE_POSE_6DOF = 28;
573
574 /**
575 * A constant string describing a pose sensor with 6 degrees of freedom.
576 *
577 * @see #TYPE_POSE_6DOF
578 */
579 public static final String STRING_TYPE_POSE_6DOF = "android.sensor.pose_6dof";
580
Peng Xufb1c9412016-03-29 21:50:43 -0700581 /**
Ashutosh Joshiaf1a2662016-01-25 20:24:20 -0800582 * A constant describing a stationary detect sensor.
583 *
584 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
585 *
586 */
587 public static final int TYPE_STATIONARY_DETECT = 29;
588
589 /**
590 * A constant string describing a stationary detection sensor.
591 *
592 * @see #TYPE_STATIONARY_DETECT
593 */
594 public static final String STRING_TYPE_STATIONARY_DETECT = "android.sensor.stationary_detect";
595
Peng Xufb1c9412016-03-29 21:50:43 -0700596 /**
Ashutosh Joshiaf1a2662016-01-25 20:24:20 -0800597 * A constant describing a motion detect sensor.
598 *
599 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
600 *
601 */
602 public static final int TYPE_MOTION_DETECT = 30;
603
604 /**
605 * A constant string describing a motion detection sensor.
606 *
607 * @see #TYPE_MOTION_DETECT
608 */
609 public static final String STRING_TYPE_MOTION_DETECT = "android.sensor.motion_detect";
610
Peng Xufb1c9412016-03-29 21:50:43 -0700611 /**
Ashutosh Joshiaf1a2662016-01-25 20:24:20 -0800612 * A constant describing a motion detect sensor.
613 *
614 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
615 *
616 */
617 public static final int TYPE_HEART_BEAT = 31;
618
619 /**
620 * A constant string describing a heart beat sensor.
621 *
622 * @see #TYPE_HEART_BEAT
623 */
624
625 public static final String STRING_TYPE_HEART_BEAT = "android.sensor.heart_beat";
Trevor Bunker3c288e62016-01-08 11:11:13 -0800626 /**
Peng Xua35b5532016-01-20 00:05:45 -0800627 * A constant describing a dynamic sensor meta event sensor.
628 *
629 * A sensor event of this type is received when a dynamic sensor is added to or removed from
630 * the system. This sensor type should always use special trigger report mode ({@code
631 * SensorManager.REPORTING_MODE_SPECIAL_TRIGGER}).
632 *
633 * @hide This sensor is expected to be used only by system services.
634 */
635 @SystemApi
636 public static final int TYPE_DYNAMIC_SENSOR_META = 32;
637
638 /**
639 * A constant string describing a dynamic sensor meta event sensor.
640 *
641 * @see #TYPE_DYNAMIC_SENSOR_META
642 *
643 * @hide This sensor is expected to only be used by the system service
644 */
645 @SystemApi
646 public static final String STRING_TYPE_DYNAMIC_SENSOR_META =
647 "android.sensor.dynamic_sensor_meta";
648
Ashutosh Joshie33eb922017-01-10 21:55:50 -0800649 /* TYPE_ADDITIONAL_INFO - defined as type 33 in the HAL is not exposed to
650 * applications. There are parts of the framework that require the sensors
651 * to be in the same order as the HAL. Skipping this sensor
652 */
653
Nick Vaccaro6432bf72017-01-12 21:09:37 -0800654 /**
655 * A constant describing a low latency off-body detect sensor.
656 *
657 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
658 *
Ashutosh Joshie33eb922017-01-10 21:55:50 -0800659 */
Nick Vaccaro6432bf72017-01-12 21:09:37 -0800660 public static final int TYPE_LOW_LATENCY_OFFBODY_DETECT = 34;
661
662
663 /**
664 * A constant string describing a low-latency offbody detector sensor.
665 *
666 * @see #TYPE_LOW_LATENCY_OFFBODY_DETECT
667 */
668 public static final String STRING_TYPE_LOW_LATENCY_OFFBODY_DETECT =
Ashutosh Joshia105c7b2017-04-15 05:50:40 -0700669 "android.sensor.low_latency_offbody_detect";
Ashutosh Joshie33eb922017-01-10 21:55:50 -0800670
671 /**
672 * A constant describing an uncalibrated accelerometer sensor.
673 *
674 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
675 *
676 */
677 public static final int TYPE_ACCELEROMETER_UNCALIBRATED = 35;
678
679 /**
680 * A constant string describing an uncalibrated accelerometer sensor.
681 *
682 * @see #TYPE_ACCELEROMETER_UNCALIBRATED
683 *
684 */
685 public static final String STRING_TYPE_ACCELEROMETER_UNCALIBRATED =
686 "android.sensor.accelerometer_uncalibrated";
Nick Vaccaro6432bf72017-01-12 21:09:37 -0800687
Peng Xua35b5532016-01-20 00:05:45 -0800688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 * A constant describing all sensor types.
690 */
Ashutosh Joshiaf1a2662016-01-25 20:24:20 -0800691
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700692 public static final int TYPE_ALL = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693
Peng Xu370985d2016-03-04 11:09:24 -0800694 /**
695 * The lowest sensor type vendor defined sensors can use.
696 *
697 * All vendor sensor types are greater than or equal to this constant.
698 *
699 */
700 public static final int TYPE_DEVICE_PRIVATE_BASE = 0x10000;
701
Aravind Akella27900352014-06-03 19:20:42 -0700702 // If this flag is set, the sensor defined as a wake up sensor. This field and REPORTING_MODE_*
703 // constants are defined as flags in sensors.h. Modify at both places if needed.
704 private static final int SENSOR_FLAG_WAKE_UP_SENSOR = 1;
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800705
Aravind Akella27900352014-06-03 19:20:42 -0700706 /**
707 * Events are reported at a constant rate which is set by the rate parameter of
708 * {@link SensorManager#registerListener(SensorEventListener, Sensor, int)}. Note: If other
709 * applications are requesting a higher rate, the sensor data might be delivered at faster rates
710 * than requested.
711 */
712 public static final int REPORTING_MODE_CONTINUOUS = 0;
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800713
Aravind Akella27900352014-06-03 19:20:42 -0700714 /**
715 * Events are reported only when the value changes. Event delivery rate can be limited by
716 * setting appropriate value for rate parameter of
717 * {@link SensorManager#registerListener(SensorEventListener, Sensor, int)} Note: If other
718 * applications are requesting a higher rate, the sensor data might be delivered at faster rates
719 * than requested.
720 */
721 public static final int REPORTING_MODE_ON_CHANGE = 1;
722
723 /**
724 * Events are reported in one-shot mode. Upon detection of an event, the sensor deactivates
725 * itself and then sends a single event. Sensors of this reporting mode must be registered to
726 * using {@link SensorManager#requestTriggerSensor(TriggerEventListener, Sensor)}.
727 */
728 public static final int REPORTING_MODE_ONE_SHOT = 2;
729
730 /**
731 * Events are reported as described in the description of the sensor. The rate passed to
732 * registerListener might not have an impact on the rate of event delivery. See the sensor
733 * definition for more information on when and how frequently the events are reported. For
734 * example, step detectors report events when a step is detected.
735 *
736 * @see SensorManager#registerListener(SensorEventListener, Sensor, int, int)
737 */
738 public static final int REPORTING_MODE_SPECIAL_TRIGGER = 3;
739
740 // Mask for the LSB 2nd, 3rd and fourth bits.
741 private static final int REPORTING_MODE_MASK = 0xE;
742 private static final int REPORTING_MODE_SHIFT = 1;
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800743
Aravind Akella18ebf732b2015-04-20 17:39:51 -0700744 // MASK for LSB fifth bit. Used to know whether the sensor supports data injection or not.
745 private static final int DATA_INJECTION_MASK = 0x10;
746 private static final int DATA_INJECTION_SHIFT = 4;
747
Peng Xuccf0c8e2017-01-17 22:16:21 -0800748 // MASK for dynamic sensor (sensor that added during runtime), bit 5.
Peng Xufb1c9412016-03-29 21:50:43 -0700749 private static final int DYNAMIC_SENSOR_MASK = 0x20;
750 private static final int DYNAMIC_SENSOR_SHIFT = 5;
751
Peng Xuccf0c8e2017-01-17 22:16:21 -0800752 // MASK for indication bit of sensor additional information support, bit 6.
Peng Xufb1c9412016-03-29 21:50:43 -0700753 private static final int ADDITIONAL_INFO_MASK = 0x40;
754 private static final int ADDITIONAL_INFO_SHIFT = 6;
755
Peng Xufa2672b2016-12-07 03:54:44 -0800756 // Mask for direct mode highest rate level, bit 7, 8, 9.
757 private static final int DIRECT_REPORT_MASK = 0x380;
758 private static final int DIRECT_REPORT_SHIFT = 7;
759
760 // Mask for supported direct channel, bit 10, 11
761 private static final int DIRECT_CHANNEL_MASK = 0xC00;
762 private static final int DIRECT_CHANNEL_SHIFT = 10;
763
Jaikumar Ganeshedb52422013-04-16 11:10:05 -0700764 // TODO(): The following arrays are fragile and error-prone. This needs to be refactored.
765
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800766 // Note: This needs to be updated, whenever a new sensor is added.
Mathias Agopianb25c0ef2013-07-03 15:38:29 -0700767 // Holds the reporting mode and maximum length of the values array
768 // associated with
769 // {@link SensorEvent} or {@link TriggerEvent} for the Sensor
770 private static final int[] sSensorReportingModes = {
Aravind Akella27900352014-06-03 19:20:42 -0700771 0, // padding because sensor types start at 1
772 3, // SENSOR_TYPE_ACCELEROMETER
773 3, // SENSOR_TYPE_GEOMAGNETIC_FIELD
774 3, // SENSOR_TYPE_ORIENTATION
775 3, // SENSOR_TYPE_GYROSCOPE
Ashutosh Joshicf189532017-03-22 21:25:13 -0700776 1, // SENSOR_TYPE_LIGHT
777 1, // SENSOR_TYPE_PRESSURE
778 1, // SENSOR_TYPE_TEMPERATURE
779 1, // SENSOR_TYPE_PROXIMITY
Aravind Akella27900352014-06-03 19:20:42 -0700780 3, // SENSOR_TYPE_GRAVITY
781 3, // SENSOR_TYPE_LINEAR_ACCELERATION
782 5, // SENSOR_TYPE_ROTATION_VECTOR
Ashutosh Joshicf189532017-03-22 21:25:13 -0700783 1, // SENSOR_TYPE_RELATIVE_HUMIDITY
784 1, // SENSOR_TYPE_AMBIENT_TEMPERATURE
Aravind Akella27900352014-06-03 19:20:42 -0700785 6, // SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED
786 4, // SENSOR_TYPE_GAME_ROTATION_VECTOR
787 6, // SENSOR_TYPE_GYROSCOPE_UNCALIBRATED
788 1, // SENSOR_TYPE_SIGNIFICANT_MOTION
789 1, // SENSOR_TYPE_STEP_DETECTOR
790 1, // SENSOR_TYPE_STEP_COUNTER
791 5, // SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR
792 1, // SENSOR_TYPE_HEART_RATE_MONITOR
Aravind Akella27900352014-06-03 19:20:42 -0700793 1, // SENSOR_TYPE_WAKE_UP_TILT_DETECTOR
794 1, // SENSOR_TYPE_WAKE_GESTURE
Jeff Brown241e70d2014-07-17 15:12:37 -0700795 1, // SENSOR_TYPE_GLANCE_GESTURE
Aravind Akella96eb62c2014-07-17 12:42:00 -0700796 1, // SENSOR_TYPE_PICK_UP_GESTURE
Nick Vaccarof5021f62015-01-23 15:09:45 -0800797 1, // SENSOR_TYPE_WRIST_TILT_GESTURE
Trevor Bunker3c288e62016-01-08 11:11:13 -0800798 1, // SENSOR_TYPE_DEVICE_ORIENTATION
Peng Xu62f2c872017-09-22 11:50:33 -0700799 16, // SENSOR_TYPE_POSE_6DOF
Peng Xua35b5532016-01-20 00:05:45 -0800800 1, // SENSOR_TYPE_STATIONARY_DETECT
801 1, // SENSOR_TYPE_MOTION_DETECT
802 1, // SENSOR_TYPE_HEART_BEAT
803 2, // SENSOR_TYPE_DYNAMIC_SENSOR_META
Peng Xu62f2c872017-09-22 11:50:33 -0700804 16, // skip over additional sensor info type
Nick Vaccaro6432bf72017-01-12 21:09:37 -0800805 1, // SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT
Ashutosh Joshie33eb922017-01-10 21:55:50 -0800806 6, // SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED
Mathias Agopianb25c0ef2013-07-03 15:38:29 -0700807 };
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700808
Aravind Akella27900352014-06-03 19:20:42 -0700809 /**
810 * Each sensor has exactly one reporting mode associated with it. This method returns the
811 * reporting mode constant for this sensor type.
812 *
813 * @return Reporting mode for the input sensor, one of REPORTING_MODE_* constants.
814 * @see #REPORTING_MODE_CONTINUOUS
815 * @see #REPORTING_MODE_ON_CHANGE
816 * @see #REPORTING_MODE_ONE_SHOT
817 * @see #REPORTING_MODE_SPECIAL_TRIGGER
818 */
819 public int getReportingMode() {
820 return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800821 }
822
Peng Xuccf0c8e2017-01-17 22:16:21 -0800823 /**
824 * Get the highest supported direct report mode rate level of the sensor.
825 *
826 * @return Highest direct report rate level of this sensor. If the sensor does not support
827 * direct report mode, this returns {@link SensorDirectChannel#RATE_STOP}.
828 * @see SensorDirectChannel#RATE_STOP
829 * @see SensorDirectChannel#RATE_NORMAL
830 * @see SensorDirectChannel#RATE_FAST
831 * @see SensorDirectChannel#RATE_VERY_FAST
832 */
833 @SensorDirectChannel.RateLevel
834 public int getHighestDirectReportRateLevel() {
Peng Xufa2672b2016-12-07 03:54:44 -0800835 int rateLevel = ((mFlags & DIRECT_REPORT_MASK) >> DIRECT_REPORT_SHIFT);
836 return rateLevel <= SensorDirectChannel.RATE_VERY_FAST
837 ? rateLevel : SensorDirectChannel.RATE_VERY_FAST;
Peng Xuccf0c8e2017-01-17 22:16:21 -0800838 }
839
840 /**
Peng Xu3c8c6a42017-04-06 18:37:56 -0700841 * Test if a sensor supports a specified direct channel type.
Peng Xuccf0c8e2017-01-17 22:16:21 -0800842 *
843 * @param sharedMemType type of shared memory used by direct channel.
Peng Xu3c8c6a42017-04-06 18:37:56 -0700844 * @return <code>true</code> if the specified shared memory type is supported.
845 * @see SensorDirectChannel#TYPE_MEMORY_FILE
Peng Xuccf0c8e2017-01-17 22:16:21 -0800846 * @see SensorDirectChannel#TYPE_HARDWARE_BUFFER
847 */
848 public boolean isDirectChannelTypeSupported(@SensorDirectChannel.MemoryType int sharedMemType) {
Peng Xufa2672b2016-12-07 03:54:44 -0800849 switch (sharedMemType) {
Peng Xu3c8c6a42017-04-06 18:37:56 -0700850 case SensorDirectChannel.TYPE_MEMORY_FILE:
Peng Xufa2672b2016-12-07 03:54:44 -0800851 return (mFlags & (1 << DIRECT_CHANNEL_SHIFT)) > 0;
852 case SensorDirectChannel.TYPE_HARDWARE_BUFFER:
853 return (mFlags & (1 << DIRECT_CHANNEL_SHIFT + 1)) > 0;
854 default:
855 return false;
856 }
Peng Xuccf0c8e2017-01-17 22:16:21 -0800857 }
858
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700859 static int getMaxLengthValuesArray(Sensor sensor, int sdkLevel) {
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700860 // RotationVector length has changed to 3 to 5 for API level 18
861 // Set it to 3 for backward compatibility.
Peng Xu62f2c872017-09-22 11:50:33 -0700862 if (sensor.mType == Sensor.TYPE_ROTATION_VECTOR
863 && sdkLevel <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Mathias Agopianb25c0ef2013-07-03 15:38:29 -0700864 return 3;
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700865 }
Aravind Akella27900352014-06-03 19:20:42 -0700866 int offset = sensor.mType;
Mathias Agopianb25c0ef2013-07-03 15:38:29 -0700867 if (offset >= sSensorReportingModes.length) {
Peng Xua35b5532016-01-20 00:05:45 -0800868 // we don't know about this sensor, so this is probably a vendor-defined sensor, in that
869 // case, we don't know how many value it has so we return the maximum and assume the app
870 // will know.
871 // FIXME: sensor HAL should advertise how much data is returned per sensor
Mathias Agopianb25c0ef2013-07-03 15:38:29 -0700872 return 16;
873 }
874 return sSensorReportingModes[offset];
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700875 }
876
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700877 /* Some of these fields are set only by the native bindings in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 * SensorManager.
879 */
880 private String mName;
881 private String mVendor;
882 private int mVersion;
883 private int mHandle;
884 private int mType;
885 private float mMaxRange;
886 private float mResolution;
887 private float mPower;
Mathias Agopian050b5622010-07-29 16:51:38 -0700888 private int mMinDelay;
Aravind Akellab4c76b12013-06-27 12:04:16 -0700889 private int mFifoReservedEventCount;
890 private int mFifoMaxEventCount;
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000891 private String mStringType;
892 private String mRequiredPermission;
Aravind Akella2f8b9142014-05-12 14:39:29 -0700893 private int mMaxDelay;
Aravind Akella27900352014-06-03 19:20:42 -0700894 private int mFlags;
Peng Xu716866d2016-05-25 21:10:00 -0700895 private int mId;
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 Sensor() {
898 }
899
900 /**
901 * @return name string of the sensor.
902 */
903 public String getName() {
904 return mName;
905 }
906
907 /**
908 * @return vendor string of this sensor.
909 */
910 public String getVendor() {
911 return mVendor;
912 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 /**
915 * @return generic type of this sensor.
916 */
917 public int getType() {
918 return mType;
919 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 /**
922 * @return version of the sensor's module.
923 */
924 public int getVersion() {
925 return mVersion;
926 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 /**
929 * @return maximum range of the sensor in the sensor's unit.
930 */
931 public float getMaximumRange() {
932 return mMaxRange;
933 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 /**
936 * @return resolution of the sensor in the sensor's unit.
937 */
938 public float getResolution() {
939 return mResolution;
940 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 /**
943 * @return the power in mA used by this sensor while in use
944 */
945 public float getPower() {
946 return mPower;
947 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700948
Mathias Agopian050b5622010-07-29 16:51:38 -0700949 /**
950 * @return the minimum delay allowed between two events in microsecond
951 * or zero if this sensor only returns a value when the data it's measuring
952 * changes.
953 */
954 public int getMinDelay() {
955 return mMinDelay;
956 }
957
Aravind Akellab4c76b12013-06-27 12:04:16 -0700958 /**
959 * @return Number of events reserved for this sensor in the batch mode FIFO. This gives a
Aravind Akellad405e0f2013-09-04 12:07:33 -0700960 * guarantee on the minimum number of events that can be batched.
Aravind Akellab4c76b12013-06-27 12:04:16 -0700961 */
962 public int getFifoReservedEventCount() {
963 return mFifoReservedEventCount;
964 }
965
966 /**
967 * @return Maximum number of events of this sensor that could be batched. If this value is zero
968 * it indicates that batch mode is not supported for this sensor. If other applications
969 * registered to batched sensors, the actual number of events that can be batched might be
970 * smaller because the hardware FiFo will be partially used to batch the other sensors.
Aravind Akellab4c76b12013-06-27 12:04:16 -0700971 */
972 public int getFifoMaxEventCount() {
973 return mFifoMaxEventCount;
974 }
975
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000976 /**
977 * @return The type of this sensor as a string.
978 */
979 public String getStringType() {
980 return mStringType;
981 }
982
983 /**
Peng Xu716866d2016-05-25 21:10:00 -0700984 * Do not use.
985 *
986 * This method throws an UnsupportedOperationException.
987 *
988 * Use getId() if you want a unique ID.
989 *
990 * @see getId
Peng Xufb1c9412016-03-29 21:50:43 -0700991 *
992 * @hide
Peng Xua35b5532016-01-20 00:05:45 -0800993 */
Peng Xufb1c9412016-03-29 21:50:43 -0700994 @SystemApi
Peng Xu716866d2016-05-25 21:10:00 -0700995 public java.util.UUID getUuid() {
996 throw new UnsupportedOperationException();
Peng Xua35b5532016-01-20 00:05:45 -0800997 }
998
999 /**
Peng Xu716866d2016-05-25 21:10:00 -07001000 * @return The sensor id that will be unique for the same app unless the device is factory
1001 * reset. Return value of 0 means this sensor does not support this function; return value of -1
1002 * means this sensor can be uniquely identified in system by combination of its type and name.
Peng Xufb1c9412016-03-29 21:50:43 -07001003 */
1004 public int getId() {
Peng Xu716866d2016-05-25 21:10:00 -07001005 return mId;
Peng Xufb1c9412016-03-29 21:50:43 -07001006 }
1007
1008 /**
Aravind Akellab613dba2014-05-21 14:14:45 -07001009 * @hide
Aravind Akellacd9a7bb2014-04-07 22:55:21 +00001010 * @return The permission required to access this sensor. If empty, no permission is required.
1011 */
1012 public String getRequiredPermission() {
1013 return mRequiredPermission;
1014 }
1015
Mathias Agopian7f84c062013-02-04 19:22:47 -08001016 /** @hide */
1017 public int getHandle() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 return mHandle;
1019 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -07001020
Aravind Akella2f8b9142014-05-12 14:39:29 -07001021 /**
Aravind Akellae57676b2014-09-04 16:24:41 -07001022 * This value is defined only for continuous and on-change sensors. It is the delay between two
1023 * sensor events corresponding to the lowest frequency that this sensor supports. When lower
1024 * frequencies are requested through registerListener() the events will be generated at this
1025 * frequency instead. It can be used to estimate when the batch FIFO may be full. Older devices
1026 * may set this value to zero. Ignore this value in case it is negative or zero.
Aravind Akella2f8b9142014-05-12 14:39:29 -07001027 *
1028 * @return The max delay for this sensor in microseconds.
1029 */
1030 public int getMaxDelay() {
1031 return mMaxDelay;
1032 }
1033
1034 /**
Aravind Akella1f9b32f2014-09-16 15:21:44 -07001035 * Returns true if the sensor is a wake-up sensor.
Aravind Akella2f8b9142014-05-12 14:39:29 -07001036 * <p>
Aravind Akella1f9b32f2014-09-16 15:21:44 -07001037 * <b>Application Processor Power modes</b> <p>
Peng Xu62f2c872017-09-22 11:50:33 -07001038 * Application Processor(AP), is the processor on which applications run. When no wake lock is
1039 * held and the user is not interacting with the device, this processor can enter a “Suspend”
1040 * mode, reducing the power consumption by 10 times or more.
Aravind Akella1f9b32f2014-09-16 15:21:44 -07001041 * </p>
Aravind Akella2f8b9142014-05-12 14:39:29 -07001042 * <p>
Aravind Akella1f9b32f2014-09-16 15:21:44 -07001043 * <b>Non-wake-up sensors</b> <p>
1044 * Non-wake-up sensors are sensors that do not wake the AP out of suspend to report data. While
1045 * the AP is in suspend mode, the sensors continue to function and generate events, which are
1046 * put in a hardware FIFO. The events in the FIFO are delivered to the application when the AP
1047 * wakes up. If the FIFO was too small to store all events generated while the AP was in
1048 * suspend mode, the older events are lost: the oldest data is dropped to accommodate the newer
1049 * data. In the extreme case where the FIFO is non-existent {@code maxFifoEventCount() == 0},
1050 * all events generated while the AP was in suspend mode are lost. Applications using
1051 * non-wake-up sensors should usually:
1052 * <ul>
1053 * <li>Either unregister from the sensors when they do not need them, usually in the activity’s
1054 * {@code onPause} method. This is the most common case.
1055 * <li>Or realize that the sensors are consuming some power while the AP is in suspend mode and
1056 * that even then, some events might be lost.
1057 * </ul>
1058 * </p>
Aravind Akella2f8b9142014-05-12 14:39:29 -07001059 * <p>
Aravind Akella1f9b32f2014-09-16 15:21:44 -07001060 * <b>Wake-up sensors</b> <p>
1061 * In opposition to non-wake-up sensors, wake-up sensors ensure that their data is delivered
1062 * independently of the state of the AP. While the AP is awake, the wake-up sensors behave
1063 * like non-wake-up-sensors. When the AP is asleep, wake-up sensors wake up the AP to deliver
1064 * events. That is, the AP will wake up and the sensor will deliver the events before the
1065 * maximum reporting latency is elapsed or the hardware FIFO gets full. See {@link
1066 * SensorManager#registerListener(SensorEventListener, Sensor, int, int)} for more details.
1067 * </p>
1068 *
1069 * @return <code>true</code> if this is a wake-up sensor, <code>false</code> otherwise.
Aravind Akella2f8b9142014-05-12 14:39:29 -07001070 */
1071 public boolean isWakeUpSensor() {
Aravind Akella27900352014-06-03 19:20:42 -07001072 return (mFlags & SENSOR_FLAG_WAKE_UP_SENSOR) != 0;
Aravind Akella2f8b9142014-05-12 14:39:29 -07001073 }
1074
Aravind Akella18ebf732b2015-04-20 17:39:51 -07001075 /**
Peng Xufb1c9412016-03-29 21:50:43 -07001076 * Returns true if the sensor is a dynamic sensor.
1077 *
1078 * @return <code>true</code> if the sensor is a dynamic sensor (sensor added at runtime).
1079 * @see SensorManager.DynamicSensorCallback
1080 */
1081 public boolean isDynamicSensor() {
1082 return (mFlags & DYNAMIC_SENSOR_MASK) != 0;
1083 }
1084
1085 /**
1086 * Returns true if the sensor supports sensor additional information API
1087 *
1088 * @return <code>true</code> if the sensor supports sensor additional information API
1089 * @see SensorAdditionalInfo
1090 */
1091 public boolean isAdditionalInfoSupported() {
1092 return (mFlags & ADDITIONAL_INFO_MASK) != 0;
1093 }
1094
1095 /**
Aravind Akella18ebf732b2015-04-20 17:39:51 -07001096 * Returns true if the sensor supports data injection when the
1097 * HAL is set to data injection mode.
1098 *
1099 * @return <code>true</code> if the sensor supports data
1100 * injection when the HAL is set in injection mode,
1101 * false otherwise.
1102 * @hide
1103 */
1104 @SystemApi
1105 public boolean isDataInjectionSupported() {
1106 return (((mFlags & DATA_INJECTION_MASK) >> DATA_INJECTION_SHIFT)) != 0;
1107 }
1108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 void setRange(float max, float res) {
1110 mMaxRange = max;
1111 mResolution = res;
1112 }
Jeff Brown6a2ef122012-08-10 18:19:10 -07001113
1114 @Override
1115 public String toString() {
1116 return "{Sensor name=\"" + mName + "\", vendor=\"" + mVendor + "\", version=" + mVersion
1117 + ", type=" + mType + ", maxRange=" + mMaxRange + ", resolution=" + mResolution
1118 + ", power=" + mPower + ", minDelay=" + mMinDelay + "}";
1119 }
destradaa9ba7c1c2015-02-10 15:04:43 -08001120
1121 /**
1122 * Sets the Type associated with the sensor.
1123 * NOTE: to be used only by native bindings in SensorManager.
1124 *
1125 * This allows interned static strings to be used across all representations of the Sensor. If
1126 * a sensor type is not referenced here, it will still be interned by the native SensorManager.
1127 *
1128 * @return {@code true} if the StringType was successfully set, {@code false} otherwise.
1129 */
1130 private boolean setType(int value) {
1131 mType = value;
1132 switch (mType) {
1133 case TYPE_ACCELEROMETER:
1134 mStringType = STRING_TYPE_ACCELEROMETER;
1135 return true;
1136 case TYPE_AMBIENT_TEMPERATURE:
1137 mStringType = STRING_TYPE_AMBIENT_TEMPERATURE;
1138 return true;
1139 case TYPE_GAME_ROTATION_VECTOR:
1140 mStringType = STRING_TYPE_GAME_ROTATION_VECTOR;
1141 return true;
1142 case TYPE_GEOMAGNETIC_ROTATION_VECTOR:
1143 mStringType = STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR;
1144 return true;
1145 case TYPE_GLANCE_GESTURE:
1146 mStringType = STRING_TYPE_GLANCE_GESTURE;
1147 return true;
1148 case TYPE_GRAVITY:
1149 mStringType = STRING_TYPE_GRAVITY;
1150 return true;
1151 case TYPE_GYROSCOPE:
1152 mStringType = STRING_TYPE_GYROSCOPE;
1153 return true;
1154 case TYPE_GYROSCOPE_UNCALIBRATED:
1155 mStringType = STRING_TYPE_GYROSCOPE_UNCALIBRATED;
1156 return true;
1157 case TYPE_HEART_RATE:
1158 mStringType = STRING_TYPE_HEART_RATE;
1159 return true;
1160 case TYPE_LIGHT:
1161 mStringType = STRING_TYPE_LIGHT;
1162 return true;
1163 case TYPE_LINEAR_ACCELERATION:
1164 mStringType = STRING_TYPE_LINEAR_ACCELERATION;
1165 return true;
1166 case TYPE_MAGNETIC_FIELD:
1167 mStringType = STRING_TYPE_MAGNETIC_FIELD;
1168 return true;
1169 case TYPE_MAGNETIC_FIELD_UNCALIBRATED:
1170 mStringType = STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
1171 return true;
1172 case TYPE_PICK_UP_GESTURE:
1173 mStringType = STRING_TYPE_PICK_UP_GESTURE;
1174 return true;
1175 case TYPE_PRESSURE:
1176 mStringType = STRING_TYPE_PRESSURE;
1177 return true;
1178 case TYPE_PROXIMITY:
1179 mStringType = STRING_TYPE_PROXIMITY;
1180 return true;
1181 case TYPE_RELATIVE_HUMIDITY:
1182 mStringType = STRING_TYPE_RELATIVE_HUMIDITY;
1183 return true;
1184 case TYPE_ROTATION_VECTOR:
1185 mStringType = STRING_TYPE_ROTATION_VECTOR;
1186 return true;
1187 case TYPE_SIGNIFICANT_MOTION:
1188 mStringType = STRING_TYPE_SIGNIFICANT_MOTION;
1189 return true;
1190 case TYPE_STEP_COUNTER:
1191 mStringType = STRING_TYPE_STEP_COUNTER;
1192 return true;
1193 case TYPE_STEP_DETECTOR:
1194 mStringType = STRING_TYPE_STEP_DETECTOR;
1195 return true;
1196 case TYPE_TILT_DETECTOR:
1197 mStringType = SENSOR_STRING_TYPE_TILT_DETECTOR;
1198 return true;
1199 case TYPE_WAKE_GESTURE:
1200 mStringType = STRING_TYPE_WAKE_GESTURE;
1201 return true;
1202 case TYPE_ORIENTATION:
1203 mStringType = STRING_TYPE_ORIENTATION;
1204 return true;
1205 case TYPE_TEMPERATURE:
1206 mStringType = STRING_TYPE_TEMPERATURE;
1207 return true;
Trevor Bunker3c288e62016-01-08 11:11:13 -08001208 case TYPE_DEVICE_ORIENTATION:
1209 mStringType = STRING_TYPE_DEVICE_ORIENTATION;
1210 return true;
Peng Xua35b5532016-01-20 00:05:45 -08001211 case TYPE_DYNAMIC_SENSOR_META:
1212 mStringType = STRING_TYPE_DYNAMIC_SENSOR_META;
1213 return true;
Nick Vaccaro6432bf72017-01-12 21:09:37 -08001214 case TYPE_LOW_LATENCY_OFFBODY_DETECT:
1215 mStringType = STRING_TYPE_LOW_LATENCY_OFFBODY_DETECT;
1216 return true;
Ashutosh Joshie33eb922017-01-10 21:55:50 -08001217 case TYPE_ACCELEROMETER_UNCALIBRATED:
1218 mStringType = STRING_TYPE_ACCELEROMETER_UNCALIBRATED;
1219 return true;
destradaa9ba7c1c2015-02-10 15:04:43 -08001220 default:
1221 return false;
1222 }
1223 }
Peng Xub85b1fe2016-04-20 21:28:33 -07001224
1225 /**
Peng Xu716866d2016-05-25 21:10:00 -07001226 * Sets the ID associated with the sensor.
1227 *
1228 * The method name is misleading; while this ID is based on the UUID,
1229 * we do not pass in the actual UUID.
Peng Xub85b1fe2016-04-20 21:28:33 -07001230 *
1231 * NOTE: to be used only by native bindings in SensorManager.
1232 *
Peng Xu716866d2016-05-25 21:10:00 -07001233 * @see #getId
Peng Xub85b1fe2016-04-20 21:28:33 -07001234 */
1235 private void setUuid(long msb, long lsb) {
Peng Xu716866d2016-05-25 21:10:00 -07001236 // TODO(b/29547335): Rename this method to setId.
Peng Xu62f2c872017-09-22 11:50:33 -07001237 mId = (int) msb;
Peng Xub85b1fe2016-04-20 21:28:33 -07001238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239}