Sensor.java revision 27900358f2f5b89e8b01516b28a916d16c99ba9b
1/*
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
20import android.os.Build;
21
22/**
23 * Class representing a sensor. Use {@link SensorManager#getSensorList} to get
24 * the list of available Sensors.
25 *
26 * @see SensorManager
27 * @see SensorEventListener
28 * @see SensorEvent
29 *
30 */
31public final class Sensor {
32
33    /**
34     * A constant describing an accelerometer sensor type.
35     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
36     * for more details.
37     */
38    public static final int TYPE_ACCELEROMETER = 1;
39
40    /**
41     * A constant string describing an accelerometer sensor type.
42     *
43     * @see #TYPE_ACCELEROMETER
44     */
45    public static final String STRING_TYPE_ACCELEROMETER = "android.sensor.accelerometer";
46
47    /**
48     * A constant describing a magnetic field sensor type.
49     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
50     * for more details.
51     */
52    public static final int TYPE_MAGNETIC_FIELD = 2;
53
54    /**
55     * A constant string describing a magnetic field sensor type.
56     *
57     * @see #TYPE_MAGNETIC_FIELD
58     */
59    public static final String STRING_TYPE_MAGNETIC_FIELD = "android.sensor.magnetic_field";
60
61    /**
62     * A constant describing an orientation sensor type.
63     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
64     * for more details.
65     *
66     * @deprecated use {@link android.hardware.SensorManager#getOrientation
67     *             SensorManager.getOrientation()} instead.
68     */
69    @Deprecated
70    public static final int TYPE_ORIENTATION = 3;
71
72    /**
73     * A constant string describing an orientation sensor type.
74     *
75     * @see #TYPE_ORIENTATION
76     * @deprecated use {@link android.hardware.SensorManager#getOrientation
77     *             SensorManager.getOrientation()} instead.
78     */
79    @Deprecated
80    public static final String STRING_TYPE_ORIENTATION = "android.sensor.orientation";
81
82    /**
83     * A constant describing a gyroscope sensor type.
84     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
85     * for more details. */
86    public static final int TYPE_GYROSCOPE = 4;
87
88    /**
89     * A constant string describing a gyroscope sensor type.
90     *
91     * @see #TYPE_GYROSCOPE
92     */
93    public static final String STRING_TYPE_GYROSCOPE = "android.sensor.gyroscope";
94
95    /**
96     * A constant describing a light sensor type.
97     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
98     * for more details.
99     */
100    public static final int TYPE_LIGHT = 5;
101
102    /**
103     * A constant string describing a light sensor type.
104     *
105     * @see #TYPE_LIGHT
106     */
107    public static final String STRING_TYPE_LIGHT = "android.sensor.light";
108
109    /**
110     * A constant describing a pressure sensor type.
111     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
112     * for more details.
113     */
114    public static final int TYPE_PRESSURE = 6;
115
116    /**
117     * A constant string describing a pressure sensor type.
118     *
119     * @see #TYPE_PRESSURE
120     */
121    public static final String STRING_TYPE_PRESSURE = "android.sensor.pressure";
122
123    /**
124     * A constant describing a temperature sensor type
125     *
126     * @deprecated use
127     *             {@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE
128     *             Sensor.TYPE_AMBIENT_TEMPERATURE} instead.
129     */
130    @Deprecated
131    public static final int TYPE_TEMPERATURE = 7;
132
133    /**
134     * A constant string describing a temperature sensor type
135     *
136     * @see #TYPE_TEMPERATURE
137     * @deprecated use
138     *             {@link android.hardware.Sensor#STRING_TYPE_AMBIENT_TEMPERATURE
139     *             Sensor.STRING_TYPE_AMBIENT_TEMPERATURE} instead.
140     */
141    @Deprecated
142    public static final String STRING_TYPE_TEMPERATURE = "android.sensor.temperature";
143
144    /**
145     * A constant describing a proximity sensor type. This is a wake up sensor.
146     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
147     * for more details.
148     * @see #isWakeUpSensor()
149     */
150    public static final int TYPE_PROXIMITY = 8;
151
152    /**
153     * A constant string describing a proximity sensor type.
154     *
155     * @see #TYPE_PROXIMITY
156     */
157    public static final String STRING_TYPE_PROXIMITY = "android.sensor.proximity";
158
159    /**
160     * A constant describing a gravity sensor type.
161     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
162     * for more details.
163     */
164    public static final int TYPE_GRAVITY = 9;
165
166    /**
167     * A constant string describing a gravity sensor type.
168     *
169     * @see #TYPE_GRAVITY
170     */
171    public static final String STRING_TYPE_GRAVITY = "android.sensor.gravity";
172
173    /**
174     * A constant describing a linear acceleration sensor type.
175     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
176     * for more details.
177     */
178    public static final int TYPE_LINEAR_ACCELERATION = 10;
179
180    /**
181     * A constant string describing a linear acceleration sensor type.
182     *
183     * @see #TYPE_LINEAR_ACCELERATION
184     */
185    public static final String STRING_TYPE_LINEAR_ACCELERATION =
186        "android.sensor.linear_acceleration";
187
188    /**
189     * A constant describing a rotation vector sensor type.
190     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
191     * for more details.
192     */
193    public static final int TYPE_ROTATION_VECTOR = 11;
194
195    /**
196     * A constant string describing a rotation vector sensor type.
197     *
198     * @see #TYPE_ROTATION_VECTOR
199     */
200    public static final String STRING_TYPE_ROTATION_VECTOR = "android.sensor.rotation_vector";
201
202    /**
203     * A constant describing a relative humidity sensor type.
204     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
205     * for more details.
206     */
207    public static final int TYPE_RELATIVE_HUMIDITY = 12;
208
209    /**
210     * A constant string describing a relative humidity sensor type
211     *
212     * @see #TYPE_RELATIVE_HUMIDITY
213     */
214    public static final String STRING_TYPE_RELATIVE_HUMIDITY = "android.sensor.relative_humidity";
215
216    /**
217     * A constant describing an ambient temperature sensor type.
218     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
219     * for more details.
220     */
221    public static final int TYPE_AMBIENT_TEMPERATURE = 13;
222
223    /**
224     * A constant string describing an ambient temperature sensor type.
225     *
226     * @see #TYPE_AMBIENT_TEMPERATURE
227     */
228    public static final String STRING_TYPE_AMBIENT_TEMPERATURE =
229        "android.sensor.ambient_temperature";
230
231    /**
232     * A constant describing an uncalibrated magnetic field sensor type.
233     * <p>
234     * Similar to {@link #TYPE_MAGNETIC_FIELD} but the hard iron calibration (device calibration
235     * due to distortions that arise from magnetized iron, steel or permanent magnets on the
236     * device) is not considered in the given sensor values. However, such hard iron bias values
237     * are returned to you separately in the result {@link android.hardware.SensorEvent#values}
238     * so you may use them for custom calibrations.
239     * <p>Also, no periodic calibration is performed
240     * (i.e. there are no discontinuities in the data stream while using this sensor) and
241     * assumptions that the magnetic field is due to the Earth's poles is avoided, but
242     * factory calibration and temperature compensation have been performed.
243     * </p>
244     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
245     * details.
246     */
247    public static final int TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14;
248    /**
249     * A constant string describing an uncalibrated magnetic field sensor type.
250     *
251     * @see #TYPE_MAGNETIC_FIELD_UNCALIBRATED
252     */
253    public static final String STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED =
254        "android.sensor.magnetic_field_uncalibrated";
255
256    /**
257     * A constant describing an uncalibrated rotation vector sensor type.
258     * <p>Identical to {@link #TYPE_ROTATION_VECTOR} except that it doesn't
259     * use the geomagnetic field. Therefore the Y axis doesn't
260     * point north, but instead to some other reference, that reference is
261     * allowed to drift by the same order of magnitude as the gyroscope
262     * drift around the Z axis.
263     * <p>
264     * In the ideal case, a phone rotated and returning to the same real-world
265     * orientation should report the same game rotation vector
266     * (without using the earth's geomagnetic field). However, the orientation
267     * may drift somewhat over time.
268     * </p>
269     * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
270     * details.
271     */
272    public static final int TYPE_GAME_ROTATION_VECTOR = 15;
273
274    /**
275     * A constant string describing an uncalibrated rotation vector sensor type.
276     *
277     * @see #TYPE_GAME_ROTATION_VECTOR
278     */
279    public static final String STRING_TYPE_GAME_ROTATION_VECTOR =
280        "android.sensor.game_rotation_vector";
281
282    /**
283     * A constant describing an uncalibrated gyroscope sensor type.
284     * <p>Similar to {@link #TYPE_GYROSCOPE} but no gyro-drift compensation has been performed
285     * to adjust the given sensor values. However, such gyro-drift bias values
286     * are returned to you separately in the result {@link android.hardware.SensorEvent#values}
287     * so you may use them for custom calibrations.
288     * <p>Factory calibration and temperature compensation is still applied
289     * to the rate of rotation (angular speeds).
290     * </p>
291     * <p> See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
292     * details.
293     */
294    public static final int TYPE_GYROSCOPE_UNCALIBRATED = 16;
295
296    /**
297     * A constant string describing an uncalibrated gyroscope sensor type.
298     *
299     * @see #TYPE_GYROSCOPE_UNCALIBRATED
300     */
301    public static final String STRING_TYPE_GYROSCOPE_UNCALIBRATED =
302        "android.sensor.gyroscope_uncalibrated";
303
304    /**
305     * A constant describing a significant motion trigger sensor.
306     * <p>
307     * It triggers when an event occurs and then automatically disables
308     * itself. The sensor continues to operate while the device is asleep
309     * and will automatically wake the device to notify when significant
310     * motion is detected. The application does not need to hold any wake
311     * locks for this sensor to trigger. This is a wake up sensor.
312     * <p>See {@link TriggerEvent} for more details.
313     *
314     * @see #isWakeUpSensor()
315     */
316    public static final int TYPE_SIGNIFICANT_MOTION = 17;
317
318    /**
319     * A constant string describing a significant motion trigger sensor.
320     *
321     * @see #TYPE_SIGNIFICANT_MOTION
322     */
323    public static final String STRING_TYPE_SIGNIFICANT_MOTION =
324        "android.sensor.significant_motion";
325
326    /**
327     * A constant describing a step detector sensor.
328     * <p>
329     * A sensor of this type triggers an event each time a step is taken by the user. The only
330     * allowed value to return is 1.0 and an event is generated for each step. Like with any other
331     * event, the timestamp indicates when the event (here the step) occurred, this corresponds to
332     * when the foot hit the ground, generating a high variation in acceleration.
333     * <p>
334     * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
335     */
336    public static final int TYPE_STEP_DETECTOR = 18;
337
338    /**
339     * A constant string describing a step detector sensor.
340     *
341     * @see #TYPE_STEP_DETECTOR
342     */
343    public static final String STRING_TYPE_STEP_DETECTOR = "android.sensor.step_detector";
344
345    /**
346     * A constant describing a step counter sensor.
347     * <p>
348     * A sensor of this type returns the number of steps taken by the user since the last reboot
349     * while activated. The value is returned as a float (with the fractional part set to zero) and
350     * is reset to zero only on a system reboot. The timestamp of the event is set to the time when
351     * the first step for that event was taken. This sensor is implemented in hardware and is
352     * expected to be low power.
353     * <p>
354     * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
355     */
356    public static final int TYPE_STEP_COUNTER = 19;
357
358    /**
359     * A constant string describing a step counter sensor.
360     *
361     * @see #TYPE_STEP_COUNTER
362     */
363    public static final String STRING_TYPE_STEP_COUNTER = "android.sensor.step_counter";
364
365    /**
366     * A constant describing a geo-magnetic rotation vector.
367     * <p>
368     * Similar to {@link #TYPE_ROTATION_VECTOR}, but using a magnetometer instead of using a
369     * gyroscope. This sensor uses lower power than the other rotation vectors, because it doesn't
370     * use the gyroscope. However, it is more noisy and will work best outdoors.
371     * <p>
372     * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details.
373     */
374    public static final int TYPE_GEOMAGNETIC_ROTATION_VECTOR = 20;
375
376    /**
377     * A constant string describing a geo-magnetic rotation vector.
378     *
379     * @see #TYPE_GEOMAGNETIC_ROTATION_VECTOR
380     */
381    public static final String STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR =
382        "android.sensor.geomagnetic_rotation_vector";
383
384    /**
385     * A constant describing a heart rate monitor.
386     * <p>
387     * The reported value is the heart rate in beats per minute.
388     * <p>
389     * The reported accuracy represents the status of the monitor during the reading. See the
390     * {@code SENSOR_STATUS_*} constants in {@link android.hardware.SensorManager SensorManager}
391     * for more details on accuracy/status values. In particular, when the accuracy is
392     * {@code SENSOR_STATUS_UNRELIABLE} or {@code SENSOR_STATUS_NO_CONTACT}, the heart rate
393     * value should be discarded.
394     * <p>
395     * This sensor requires permission {@code android.permission.BODY_SENSORS}.
396     * It will not be returned by {@code SensorManager.getSensorsList} nor
397     * {@code SensorManager.getDefaultSensor} if the application doesn't have this permission.
398     */
399    public static final int TYPE_HEART_RATE = 21;
400
401    /**
402     * A constant string describing a heart rate monitor.
403     *
404     * @see #TYPE_HEART_RATE
405     */
406    public static final String STRING_TYPE_HEART_RATE = "android.sensor.heart_rate";
407
408    /**
409     * A non-wake up variant of proximity sensor.
410     *
411     * @see #TYPE_PROXIMITY
412     */
413    public static final int TYPE_NON_WAKE_UP_PROXIMITY_SENSOR = 22;
414
415    /**
416     * A constant string describing a non-wake up proximity sensor type.
417     *
418     * @see #TYPE_NON_WAKE_UP_PROXIMITY_SENSOR
419     */
420    public static final String SENSOR_STRING_TYPE_NON_WAKE_UP_PROXIMITY_SENSOR =
421            "android.sensor.non_wake_up_proximity_sensor";
422
423    /**
424     * A constant describing a wake up variant of accelerometer sensor type.
425     *
426     * @see #isWakeUpSensor()
427     * @see #TYPE_ACCELEROMETER
428     */
429    public static final int TYPE_WAKE_UP_ACCELEROMETER = 23;
430
431    /**
432     * A constant string describing a wake up accelerometer.
433     *
434     * @see #TYPE_WAKE_UP_ACCELEROMETER
435     */
436    public static final String STRING_TYPE_WAKE_UP_ACCELEROMETER =
437            "android.sensor.wake_up_accelerometer";
438
439    /**
440     * A constant describing a wake up variant of a magnetic field sensor type.
441     *
442     * @see #isWakeUpSensor()
443     * @see #TYPE_MAGNETIC_FIELD
444     */
445    public static final int TYPE_WAKE_UP_MAGNETIC_FIELD = 24;
446
447    /**
448     * A constant string describing a wake up magnetic field sensor.
449     *
450     * @see #TYPE_WAKE_UP_MAGNETIC_FIELD
451     */
452    public static final String STRING_TYPE_WAKE_UP_MAGNETIC_FIELD =
453            "android.sensor.wake_up_magnetic_field";
454
455    /**
456     * A constant describing a wake up variant of an orientation sensor type.
457     *
458     * @see #isWakeUpSensor()
459     * @see #TYPE_ORIENTATION
460     */
461    public static final int TYPE_WAKE_UP_ORIENTATION = 25;
462
463    /**
464     * A constant string describing a wake up orientation sensor.
465     *
466     * @see #TYPE_WAKE_UP_ORIENTATION
467     */
468    public static final String STRING_TYPE_WAKE_UP_ORIENTATION =
469            "android.sensor.wake_up_orientation";
470
471    /**
472     * A constant describing a wake up variant of a gyroscope sensor type.
473     *
474     * @see #isWakeUpSensor()
475     * @see #TYPE_GYROSCOPE
476     */
477    public static final int TYPE_WAKE_UP_GYROSCOPE = 26;
478
479    /**
480     * A constant string describing a wake up gyroscope sensor type.
481     *
482     * @see #TYPE_WAKE_UP_GYROSCOPE
483     */
484    public static final String STRING_TYPE_WAKE_UP_GYROSCOPE = "android.sensor.wake_up_gyroscope";
485
486    /**
487     * A constant describing a wake up variant of a light sensor type.
488     *
489     * @see #isWakeUpSensor()
490     * @see #TYPE_LIGHT
491     */
492    public static final int TYPE_WAKE_UP_LIGHT = 27;
493
494    /**
495     * A constant string describing a wake up light sensor type.
496     *
497     * @see #TYPE_WAKE_UP_LIGHT
498     */
499    public static final String STRING_TYPE_WAKE_UP_LIGHT = "android.sensor.wake_up_light";
500
501    /**
502     * A constant describing a wake up variant of a pressure sensor type.
503     *
504     * @see #isWakeUpSensor()
505     * @see #TYPE_PRESSURE
506     */
507    public static final int TYPE_WAKE_UP_PRESSURE = 28;
508
509    /**
510     * A constant string describing a wake up pressure sensor type.
511     *
512     * @see #TYPE_WAKE_UP_PRESSURE
513     */
514    public static final String STRING_TYPE_WAKE_UP_PRESSURE = "android.sensor.wake_up_pressure";
515
516    /**
517     * A constant describing a wake up variant of a gravity sensor type.
518     *
519     * @see #isWakeUpSensor()
520     * @see #TYPE_GRAVITY
521     */
522    public static final int TYPE_WAKE_UP_GRAVITY = 29;
523
524    /**
525     * A constant string describing a wake up gravity sensor type.
526     *
527     * @see #TYPE_WAKE_UP_GRAVITY
528     */
529    public static final String STRING_TYPE_WAKE_UP_GRAVITY = "android.sensor.wake_up_gravity";
530
531    /**
532     * A constant describing a wake up variant of a linear acceleration sensor type.
533     *
534     * @see #isWakeUpSensor()
535     * @see #TYPE_LINEAR_ACCELERATION
536     */
537    public static final int TYPE_WAKE_UP_LINEAR_ACCELERATION = 30;
538
539    /**
540     * A constant string describing a wake up linear acceleration sensor type.
541     *
542     * @see #TYPE_WAKE_UP_LINEAR_ACCELERATION
543     */
544    public static final String STRING_TYPE_WAKE_UP_LINEAR_ACCELERATION =
545            "android.sensor.wake_up_linear_acceleration";
546
547    /**
548     * A constant describing a wake up variant of a rotation vector sensor type.
549     *
550     * @see #isWakeUpSensor()
551     * @see #TYPE_ROTATION_VECTOR
552     */
553    public static final int TYPE_WAKE_UP_ROTATION_VECTOR = 31;
554
555    /**
556     * A constant string describing a wake up rotation vector sensor type.
557     *
558     * @see #TYPE_WAKE_UP_ROTATION_VECTOR
559     */
560    public static final String STRING_TYPE_WAKE_UP_ROTATION_VECTOR =
561            "android.sensor.wake_up_rotation_vector";
562
563    /**
564     * A constant describing a wake up variant of a relative humidity sensor type.
565     *
566     * @see #isWakeUpSensor()
567     * @see #TYPE_RELATIVE_HUMIDITY
568     */
569    public static final int TYPE_WAKE_UP_RELATIVE_HUMIDITY = 32;
570
571    /**
572     * A constant string describing a wake up relative humidity sensor type.
573     *
574     * @see #TYPE_WAKE_UP_RELATIVE_HUMIDITY
575     */
576    public static final String STRING_TYPE_WAKE_UP_RELATIVE_HUMIDITY =
577            "android.sensor.wake_up_relative_humidity";
578
579    /**
580     * A constant describing a wake up variant of an ambient temperature sensor type.
581     *
582     * @see #isWakeUpSensor()
583     * @see #TYPE_AMBIENT_TEMPERATURE
584     */
585    public static final int TYPE_WAKE_UP_AMBIENT_TEMPERATURE = 33;
586
587    /**
588     * A constant string describing a wake up ambient temperature sensor type.
589     *
590     * @see #TYPE_WAKE_UP_AMBIENT_TEMPERATURE
591     */
592    public static final String STRING_TYPE_WAKE_UP_AMBIENT_TEMPERATURE =
593            "android.sensor.wake_up_ambient_temperature";
594
595    /**
596     * A constant describing a wake up variant of an uncalibrated magnetic field sensor type.
597     *
598     * @see #isWakeUpSensor()
599     * @see #TYPE_MAGNETIC_FIELD_UNCALIBRATED
600     */
601    public static final int TYPE_WAKE_UP_MAGNETIC_FIELD_UNCALIBRATED = 34;
602
603    /**
604     * A constant string describing a wake up uncalibrated magnetic field sensor type.
605     *
606     * @see #TYPE_WAKE_UP_MAGNETIC_FIELD_UNCALIBRATED
607     */
608    public static final String STRING_TYPE_WAKE_UP_MAGNETIC_FIELD_UNCALIBRATED =
609            "android.sensor.wake_up_magnetic_field_uncalibrated";
610
611    /**
612     * A constant describing a wake up variant of a game rotation vector sensor type.
613     *
614     * @see #isWakeUpSensor()
615     * @see #TYPE_GAME_ROTATION_VECTOR
616     */
617    public static final int TYPE_WAKE_UP_GAME_ROTATION_VECTOR = 35;
618
619    /**
620     * A constant string describing a wake up game rotation vector sensor type.
621     *
622     * @see #TYPE_WAKE_UP_GAME_ROTATION_VECTOR
623     */
624    public static final String STRING_TYPE_WAKE_UP_GAME_ROTATION_VECTOR =
625            "android.sensor.wake_up_game_rotation_vector";
626
627    /**
628     * A constant describing a wake up variant of an uncalibrated gyroscope sensor type.
629     *
630     * @see #isWakeUpSensor()
631     * @see #TYPE_GYROSCOPE_UNCALIBRATED
632     */
633    public static final int TYPE_WAKE_UP_GYROSCOPE_UNCALIBRATED = 36;
634
635    /**
636     * A constant string describing a wake up uncalibrated gyroscope sensor type.
637     *
638     * @see #TYPE_WAKE_UP_GYROSCOPE_UNCALIBRATED
639     */
640    public static final String STRING_TYPE_WAKE_UP_GYROSCOPE_UNCALIBRATED =
641            "android.sensor.wake_up_gyroscope_uncalibrated";
642
643    /**
644     * A constant describing a wake up variant of a step detector sensor type.
645     *
646     * @see #isWakeUpSensor()
647     * @see #TYPE_STEP_DETECTOR
648     */
649    public static final int TYPE_WAKE_UP_STEP_DETECTOR = 37;
650
651    /**
652     * A constant string describing a wake up step detector sensor type.
653     *
654     * @see #TYPE_WAKE_UP_STEP_DETECTOR
655     */
656    public static final String STRING_TYPE_WAKE_UP_STEP_DETECTOR =
657            "android.sensor.wake_up_step_detector";
658
659    /**
660     * A constant describing a wake up variant of a step counter sensor type.
661     *
662     * @see #isWakeUpSensor()
663     * @see #TYPE_STEP_COUNTER
664     */
665    public static final int TYPE_WAKE_UP_STEP_COUNTER = 38;
666
667    /**
668     * A constant string describing a wake up step counter sensor type.
669     *
670     * @see #TYPE_WAKE_UP_STEP_COUNTER
671     */
672    public static final String STRING_TYPE_WAKE_UP_STEP_COUNTER =
673            "android.sensor.wake_up_step_counter";
674
675    /**
676     * A constant describing a wake up variant of a geomagnetic rotation vector sensor type.
677     *
678     * @see #isWakeUpSensor()
679     * @see #TYPE_GEOMAGNETIC_ROTATION_VECTOR
680     */
681    public static final int TYPE_WAKE_UP_GEOMAGNETIC_ROTATION_VECTOR = 39;
682
683    /**
684     * A constant string describing a wake up geomagnetic rotation vector sensor type.
685     *
686     * @see #TYPE_WAKE_UP_GEOMAGNETIC_ROTATION_VECTOR
687     */
688    public static final String STRING_TYPE_WAKE_UP_GEOMAGNETIC_ROTATION_VECTOR =
689            "android.sensor.wake_up_geomagnetic_rotation_vector";
690
691    /**
692     * A constant describing a wake up variant of a heart rate sensor type.
693     *
694     * @see #isWakeUpSensor()
695     * @see #TYPE_HEART_RATE
696     */
697    public static final int TYPE_WAKE_UP_HEART_RATE = 40;
698
699    /**
700     * A constant string describing a wake up heart rate sensor type.
701     *
702     * @see #TYPE_WAKE_UP_HEART_RATE
703     */
704    public static final String STRING_TYPE_WAKE_UP_HEART_RATE = "android.sensor.wake_up_heart_rate";
705
706    /**
707     * A sensor of this type generates an event each time a tilt event is detected. A tilt event
708     * is generated if the direction of the 2-seconds window average gravity changed by at
709     * least 35 degrees since the activation of the sensor. It is a wake up sensor.
710     *
711     * @hide
712     * @see #isWakeUpSensor()
713     */
714    public static final int TYPE_WAKE_UP_TILT_DETECTOR = 41;
715
716    /**
717     * A constant string describing a wake up tilt detector sensor type.
718     *
719     * @hide
720     * @see #TYPE_WAKE_UP_TILT_DETECTOR
721     */
722    public static final String SENSOR_STRING_TYPE_WAKE_UP_TILT_DETECTOR =
723            "android.sensor.wake_up_tilt_detector";
724
725    /**
726     * A constant describing a wake gesture sensor.
727     * <p>
728     * Wake gesture sensors enable waking up the device based on a device specific motion.
729     * <p>
730     * When this sensor triggers, the device behaves as if the power button was pressed, turning the
731     * screen on. This behavior (turning on the screen when this sensor triggers) might be
732     * deactivated by the user in the device settings. Changes in settings do not impact the
733     * behavior of the sensor: only whether the framework turns the screen on when it triggers.
734     * <p>
735     * The actual gesture to be detected is not specified, and can be chosen by the manufacturer of
736     * the device. This sensor must be low power, as it is likely to be activated 24/7.
737     * Values of events created by this sensors should not be used.
738     *
739     * @see #isWakeUpSensor()
740     * @hide This sensor is expected to only be used by the power manager
741     */
742    public static final int TYPE_WAKE_GESTURE = 42;
743
744    /**
745     * A constant string describing a wake gesture sensor.
746     *
747     * @hide This sensor is expected to only be used by the power manager
748     * @see #TYPE_WAKE_GESTURE
749     */
750    public static final String STRING_TYPE_WAKE_GESTURE = "android.sensor.wake_gesture";
751
752    /**
753     * A constant describing all sensor types.
754     */
755    public static final int TYPE_ALL = -1;
756
757    // If this flag is set, the sensor defined as a wake up sensor. This field and REPORTING_MODE_*
758    // constants are defined as flags in sensors.h. Modify at both places if needed.
759    private static final int SENSOR_FLAG_WAKE_UP_SENSOR = 1;
760
761    /**
762     * Events are reported at a constant rate which is set by the rate parameter of
763     * {@link SensorManager#registerListener(SensorEventListener, Sensor, int)}. Note: If other
764     * applications are requesting a higher rate, the sensor data might be delivered at faster rates
765     * than requested.
766     */
767    public static final int REPORTING_MODE_CONTINUOUS = 0;
768
769    /**
770     * Events are reported only when the value changes. Event delivery rate can be limited by
771     * setting appropriate value for rate parameter of
772     * {@link SensorManager#registerListener(SensorEventListener, Sensor, int)} Note: If other
773     * applications are requesting a higher rate, the sensor data might be delivered at faster rates
774     * than requested.
775     */
776    public static final int REPORTING_MODE_ON_CHANGE = 1;
777
778    /**
779     * Events are reported in one-shot mode. Upon detection of an event, the sensor deactivates
780     * itself and then sends a single event. Sensors of this reporting mode must be registered to
781     * using {@link SensorManager#requestTriggerSensor(TriggerEventListener, Sensor)}.
782     */
783    public static final int REPORTING_MODE_ONE_SHOT = 2;
784
785    /**
786     * Events are reported as described in the description of the sensor. The rate passed to
787     * registerListener might not have an impact on the rate of event delivery. See the sensor
788     * definition for more information on when and how frequently the events are reported. For
789     * example, step detectors report events when a step is detected.
790     *
791     * @see SensorManager#registerListener(SensorEventListener, Sensor, int, int)
792     */
793    public static final int REPORTING_MODE_SPECIAL_TRIGGER = 3;
794
795    // Mask for the LSB 2nd, 3rd and fourth bits.
796    private static final int REPORTING_MODE_MASK = 0xE;
797    private static final int REPORTING_MODE_SHIFT = 1;
798
799    // TODO(): The following arrays are fragile and error-prone. This needs to be refactored.
800
801    // Note: This needs to be updated, whenever a new sensor is added.
802    // Holds the reporting mode and maximum length of the values array
803    // associated with
804    // {@link SensorEvent} or {@link TriggerEvent} for the Sensor
805    private static final int[] sSensorReportingModes = {
806            0, // padding because sensor types start at 1
807            3, // SENSOR_TYPE_ACCELEROMETER
808            3, // SENSOR_TYPE_GEOMAGNETIC_FIELD
809            3, // SENSOR_TYPE_ORIENTATION
810            3, // SENSOR_TYPE_GYROSCOPE
811            3, // SENSOR_TYPE_LIGHT
812            3, // SENSOR_TYPE_PRESSURE
813            3, // SENSOR_TYPE_TEMPERATURE
814            3, // SENSOR_TYPE_PROXIMITY
815            3, // SENSOR_TYPE_GRAVITY
816            3, // SENSOR_TYPE_LINEAR_ACCELERATION
817            5, // SENSOR_TYPE_ROTATION_VECTOR
818            3, // SENSOR_TYPE_RELATIVE_HUMIDITY
819            3, // SENSOR_TYPE_AMBIENT_TEMPERATURE
820            6, // SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED
821            4, // SENSOR_TYPE_GAME_ROTATION_VECTOR
822            6, // SENSOR_TYPE_GYROSCOPE_UNCALIBRATED
823            1, // SENSOR_TYPE_SIGNIFICANT_MOTION
824            1, // SENSOR_TYPE_STEP_DETECTOR
825            1, // SENSOR_TYPE_STEP_COUNTER
826            5, // SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR
827            1, // SENSOR_TYPE_HEART_RATE_MONITOR
828            3, // SENSOR_TYPE_NON_WAKE_UP_PROXIMITY_SENSOR
829            // wake up variants of base sensors
830            3, // SENSOR_TYPE_WAKE_UP_ACCELEROMETER
831            3, // SENSOR_TYPE_WAKE_UP_MAGNETIC_FIELD
832            3, // SENSOR_TYPE_WAKE_UP_ORIENTATION
833            3, // SENSOR_TYPE_WAKE_UP_GYROSCOPE
834            3, // SENSOR_TYPE_WAKE_UP_LIGHT
835            3, // SENSOR_TYPE_WAKE_UP_PRESSURE
836            3, // SENSOR_TYPE_WAKE_UP_GRAVITY
837            3, // SENSOR_TYPE_WAKE_UP_LINEAR_ACCELERATION
838            5, // SENSOR_TYPE_WAKE_UP_ROTATION_VECTOR
839            3, // SENSOR_TYPE_WAKE_UP_RELATIVE_HUMIDITY
840            3, // SENSOR_TYPE_WAKE_UP_AMBIENT_TEMPERATURE
841            6, // SENSOR_TYPE_WAKE_UP_MAGNETIC_FIELD_UNCALIBRATED
842            4, // SENSOR_TYPE_WAKE_UP_GAME_ROTATION_VECTOR
843            6, // SENSOR_TYPE_WAKE_UP_GYROSCOPE_UNCALIBRATED
844            1, // SENSOR_TYPE_WAKE_UP_STEP_DETECTOR
845            1, // SENSOR_TYPE_WAKE_UP_STEP_COUNTER
846            5, // SENSOR_TYPE_WAKE_UP_GEOMAGNETIC_ROTATION_VECTOR
847            1, // SENSOR_TYPE_WAKE_UP_HEART_RATE_MONITOR
848            1, // SENSOR_TYPE_WAKE_UP_TILT_DETECTOR
849            1, // SENSOR_TYPE_WAKE_GESTURE
850    };
851
852    /**
853     * Each sensor has exactly one reporting mode associated with it. This method returns the
854     * reporting mode constant for this sensor type.
855     *
856     * @return Reporting mode for the input sensor, one of REPORTING_MODE_* constants.
857     * @see #REPORTING_MODE_CONTINUOUS
858     * @see #REPORTING_MODE_ON_CHANGE
859     * @see #REPORTING_MODE_ONE_SHOT
860     * @see #REPORTING_MODE_SPECIAL_TRIGGER
861     */
862    public int getReportingMode() {
863        return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
864    }
865
866    static int getMaxLengthValuesArray(Sensor sensor, int sdkLevel) {
867        // RotationVector length has changed to 3 to 5 for API level 18
868        // Set it to 3 for backward compatibility.
869        if (sensor.mType == Sensor.TYPE_ROTATION_VECTOR &&
870                sdkLevel <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
871            return 3;
872        }
873        int offset = sensor.mType;
874        if (offset >= sSensorReportingModes.length) {
875            // we don't know about this sensor, so this is probably a
876            // vendor-defined sensor, in that case, we don't know how many value
877            // it has
878            // so we return the maximum and assume the app will know.
879            // FIXME: sensor HAL should advertise how much data is returned per
880            // sensor
881            return 16;
882        }
883        return sSensorReportingModes[offset];
884    }
885
886    /* Some of these fields are set only by the native bindings in
887     * SensorManager.
888     */
889    private String  mName;
890    private String  mVendor;
891    private int     mVersion;
892    private int     mHandle;
893    private int     mType;
894    private float   mMaxRange;
895    private float   mResolution;
896    private float   mPower;
897    private int     mMinDelay;
898    private int     mFifoReservedEventCount;
899    private int     mFifoMaxEventCount;
900    private String  mStringType;
901    private String  mRequiredPermission;
902    private int     mMaxDelay;
903    private int     mFlags;
904
905    Sensor() {
906    }
907
908    /**
909     * @return name string of the sensor.
910     */
911    public String getName() {
912        return mName;
913    }
914
915    /**
916     * @return vendor string of this sensor.
917     */
918    public String getVendor() {
919        return mVendor;
920    }
921
922    /**
923     * @return generic type of this sensor.
924     */
925    public int getType() {
926        return mType;
927    }
928
929    /**
930     * @return version of the sensor's module.
931     */
932    public int getVersion() {
933        return mVersion;
934    }
935
936    /**
937     * @return maximum range of the sensor in the sensor's unit.
938     */
939    public float getMaximumRange() {
940        return mMaxRange;
941    }
942
943    /**
944     * @return resolution of the sensor in the sensor's unit.
945     */
946    public float getResolution() {
947        return mResolution;
948    }
949
950    /**
951     * @return the power in mA used by this sensor while in use
952     */
953    public float getPower() {
954        return mPower;
955    }
956
957    /**
958     * @return the minimum delay allowed between two events in microsecond
959     * or zero if this sensor only returns a value when the data it's measuring
960     * changes.
961     */
962    public int getMinDelay() {
963        return mMinDelay;
964    }
965
966    /**
967     * @return Number of events reserved for this sensor in the batch mode FIFO. This gives a
968     * guarantee on the minimum number of events that can be batched.
969     */
970    public int getFifoReservedEventCount() {
971        return mFifoReservedEventCount;
972    }
973
974    /**
975     * @return Maximum number of events of this sensor that could be batched. If this value is zero
976     * it indicates that batch mode is not supported for this sensor. If other applications
977     * registered to batched sensors, the actual number of events that can be batched might be
978     * smaller because the hardware FiFo will be partially used to batch the other sensors.
979     */
980    public int getFifoMaxEventCount() {
981        return mFifoMaxEventCount;
982    }
983
984    /**
985     * @return The type of this sensor as a string.
986     */
987    public String getStringType() {
988        return mStringType;
989    }
990
991    /**
992     * @hide
993     * @return The permission required to access this sensor. If empty, no permission is required.
994     */
995    public String getRequiredPermission() {
996        return mRequiredPermission;
997    }
998
999    /** @hide */
1000    public int getHandle() {
1001        return mHandle;
1002    }
1003
1004    /**
1005     * This value is defined only for continuous mode sensors. It is the delay between two
1006     * sensor events corresponding to the lowest frequency that this sensor supports. When
1007     * lower frequencies are requested through registerListener() the events will be generated
1008     * at this frequency instead. It can be used to estimate when the batch FIFO may be full.
1009     * Older devices may set this value to zero. Ignore this value in case it is negative
1010     * or zero.
1011     *
1012     * @return The max delay for this sensor in microseconds.
1013     */
1014    public int getMaxDelay() {
1015        return mMaxDelay;
1016    }
1017
1018    /**
1019     * Returns whether this sensor is a wake-up sensor.
1020     * <p>
1021     * Wake up sensors wake the application processor up when they have events to deliver. When a
1022     * wake up sensor is registered to without batching enabled, each event will wake the
1023     * application processor up.
1024     * <p>
1025     * When a wake up sensor is registered to with batching enabled, it
1026     * wakes the application processor up when maxReportingLatency has elapsed or when the hardware
1027     * FIFO storing the events from wake up sensors is getting full.
1028     * <p>
1029     * Non-wake up sensors never wake the application processor up. Their events are only reported
1030     * when the application processor is awake, for example because the application holds a wake
1031     * lock, or another source woke the application processor up.
1032     * <p>
1033     * When a non-wake up sensor is registered to without batching enabled, the measurements made
1034     * while the application processor is asleep might be lost and never returned.
1035     * <p>
1036     * When a non-wake up sensor is registered to with batching enabled, the measurements made while
1037     * the application processor is asleep are stored in the hardware FIFO for non-wake up sensors.
1038     * When this FIFO gets full, new events start overwriting older events. When the application
1039     * then wakes up, the latest events are returned, and some old events might be lost. The number
1040     * of events actually returned depends on the hardware FIFO size, as well as on what other
1041     * sensors are activated. If losing sensor events is not acceptable during batching, you must
1042     * use the wake-up version of the sensor.
1043     * @return true if this is a wake up sensor, false otherwise.
1044     */
1045    public boolean isWakeUpSensor() {
1046        return (mFlags & SENSOR_FLAG_WAKE_UP_SENSOR) != 0;
1047    }
1048
1049    void setRange(float max, float res) {
1050        mMaxRange = max;
1051        mResolution = res;
1052    }
1053
1054    @Override
1055    public String toString() {
1056        return "{Sensor name=\"" + mName + "\", vendor=\"" + mVendor + "\", version=" + mVersion
1057                + ", type=" + mType + ", maxRange=" + mMaxRange + ", resolution=" + mResolution
1058                + ", power=" + mPower + ", minDelay=" + mMinDelay + "}";
1059    }
1060}
1061