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 18 package android.hardware; 19 20 import 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 */ 31 public 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 last 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 sensor of this type generates an event each time a tilt event is detected. A tilt event 410 * is generated if the direction of the 2-seconds window average gravity changed by at 411 * least 35 degrees since the activation of the sensor. It is a wake up sensor. 412 * 413 * @hide 414 * @see #isWakeUpSensor() 415 */ 416 public static final int TYPE_TILT_DETECTOR = 22; 417 418 /** 419 * A constant string describing a wake up tilt detector sensor type. 420 * 421 * @hide 422 * @see #TYPE_WAKE_UP_TILT_DETECTOR 423 */ 424 public static final String SENSOR_STRING_TYPE_TILT_DETECTOR = 425 "android.sensor.tilt_detector"; 426 427 /** 428 * A constant describing a wake gesture sensor. 429 * <p> 430 * Wake gesture sensors enable waking up the device based on a device specific motion. 431 * <p> 432 * When this sensor triggers, the device behaves as if the power button was pressed, turning the 433 * screen on. This behavior (turning on the screen when this sensor triggers) might be 434 * deactivated by the user in the device settings. Changes in settings do not impact the 435 * behavior of the sensor: only whether the framework turns the screen on when it triggers. 436 * <p> 437 * The actual gesture to be detected is not specified, and can be chosen by the manufacturer of 438 * the device. This sensor must be low power, as it is likely to be activated 24/7. 439 * Values of events created by this sensors should not be used. 440 * 441 * @see #isWakeUpSensor() 442 * @hide This sensor is expected to only be used by the system ui 443 */ 444 public static final int TYPE_WAKE_GESTURE = 23; 445 446 /** 447 * A constant string describing a wake gesture sensor. 448 * 449 * @hide This sensor is expected to only be used by the system ui 450 * @see #TYPE_WAKE_GESTURE 451 */ 452 public static final String STRING_TYPE_WAKE_GESTURE = "android.sensor.wake_gesture"; 453 454 /** 455 * A constant describing a wake gesture sensor. 456 * <p> 457 * A sensor enabling briefly turning the screen on to enable the user to 458 * glance content on screen based on a specific motion. The device should 459 * turn the screen off after a few moments. 460 * <p> 461 * When this sensor triggers, the device turns the screen on momentarily 462 * to allow the user to glance notifications or other content while the 463 * device remains locked in a non-interactive state (dozing). This behavior 464 * (briefly turning on the screen when this sensor triggers) might be deactivated 465 * by the user in the device settings. Changes in settings do not impact the 466 * behavior of the sensor: only whether the framework briefly turns the screen on 467 * when it triggers. 468 * <p> 469 * The actual gesture to be detected is not specified, and can be chosen by the manufacturer of 470 * the device. This sensor must be low power, as it is likely to be activated 24/7. 471 * Values of events created by this sensors should not be used. 472 * 473 * @see #isWakeUpSensor() 474 * @hide This sensor is expected to only be used by the system ui 475 */ 476 public static final int TYPE_GLANCE_GESTURE = 24; 477 478 /** 479 * A constant string describing a wake gesture sensor. 480 * 481 * @hide This sensor is expected to only be used by the system ui 482 * @see #TYPE_GLANCE_GESTURE 483 */ 484 public static final String STRING_TYPE_GLANCE_GESTURE = "android.sensor.glance_gesture"; 485 486 /** 487 * A constant describing a pick up sensor. 488 * 489 * A sensor of this type triggers when the device is picked up regardless of wherever it was 490 * before (desk, pocket, bag). The only allowed return value is 1.0. This sensor deactivates 491 * itself immediately after it triggers. 492 * 493 * @hide Expected to be used internally for always on display. 494 */ 495 public static final int TYPE_PICK_UP_GESTURE = 25; 496 497 /** 498 * A constant string describing a pick up sensor. 499 * 500 * @hide This sensor is expected to be used internally for always on display. 501 * @see #TYPE_PICK_UP_GESTURE 502 */ 503 public static final String STRING_TYPE_PICK_UP_GESTURE = "android.sensor.pick_up_gesture"; 504 505 /** 506 * A constant describing all sensor types. 507 */ 508 public static final int TYPE_ALL = -1; 509 510 // If this flag is set, the sensor defined as a wake up sensor. This field and REPORTING_MODE_* 511 // constants are defined as flags in sensors.h. Modify at both places if needed. 512 private static final int SENSOR_FLAG_WAKE_UP_SENSOR = 1; 513 514 /** 515 * Events are reported at a constant rate which is set by the rate parameter of 516 * {@link SensorManager#registerListener(SensorEventListener, Sensor, int)}. Note: If other 517 * applications are requesting a higher rate, the sensor data might be delivered at faster rates 518 * than requested. 519 */ 520 public static final int REPORTING_MODE_CONTINUOUS = 0; 521 522 /** 523 * Events are reported only when the value changes. Event delivery rate can be limited by 524 * setting appropriate value for rate parameter of 525 * {@link SensorManager#registerListener(SensorEventListener, Sensor, int)} Note: If other 526 * applications are requesting a higher rate, the sensor data might be delivered at faster rates 527 * than requested. 528 */ 529 public static final int REPORTING_MODE_ON_CHANGE = 1; 530 531 /** 532 * Events are reported in one-shot mode. Upon detection of an event, the sensor deactivates 533 * itself and then sends a single event. Sensors of this reporting mode must be registered to 534 * using {@link SensorManager#requestTriggerSensor(TriggerEventListener, Sensor)}. 535 */ 536 public static final int REPORTING_MODE_ONE_SHOT = 2; 537 538 /** 539 * Events are reported as described in the description of the sensor. The rate passed to 540 * registerListener might not have an impact on the rate of event delivery. See the sensor 541 * definition for more information on when and how frequently the events are reported. For 542 * example, step detectors report events when a step is detected. 543 * 544 * @see SensorManager#registerListener(SensorEventListener, Sensor, int, int) 545 */ 546 public static final int REPORTING_MODE_SPECIAL_TRIGGER = 3; 547 548 // Mask for the LSB 2nd, 3rd and fourth bits. 549 private static final int REPORTING_MODE_MASK = 0xE; 550 private static final int REPORTING_MODE_SHIFT = 1; 551 552 // TODO(): The following arrays are fragile and error-prone. This needs to be refactored. 553 554 // Note: This needs to be updated, whenever a new sensor is added. 555 // Holds the reporting mode and maximum length of the values array 556 // associated with 557 // {@link SensorEvent} or {@link TriggerEvent} for the Sensor 558 private static final int[] sSensorReportingModes = { 559 0, // padding because sensor types start at 1 560 3, // SENSOR_TYPE_ACCELEROMETER 561 3, // SENSOR_TYPE_GEOMAGNETIC_FIELD 562 3, // SENSOR_TYPE_ORIENTATION 563 3, // SENSOR_TYPE_GYROSCOPE 564 3, // SENSOR_TYPE_LIGHT 565 3, // SENSOR_TYPE_PRESSURE 566 3, // SENSOR_TYPE_TEMPERATURE 567 3, // SENSOR_TYPE_PROXIMITY 568 3, // SENSOR_TYPE_GRAVITY 569 3, // SENSOR_TYPE_LINEAR_ACCELERATION 570 5, // SENSOR_TYPE_ROTATION_VECTOR 571 3, // SENSOR_TYPE_RELATIVE_HUMIDITY 572 3, // SENSOR_TYPE_AMBIENT_TEMPERATURE 573 6, // SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED 574 4, // SENSOR_TYPE_GAME_ROTATION_VECTOR 575 6, // SENSOR_TYPE_GYROSCOPE_UNCALIBRATED 576 1, // SENSOR_TYPE_SIGNIFICANT_MOTION 577 1, // SENSOR_TYPE_STEP_DETECTOR 578 1, // SENSOR_TYPE_STEP_COUNTER 579 5, // SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR 580 1, // SENSOR_TYPE_HEART_RATE_MONITOR 581 1, // SENSOR_TYPE_WAKE_UP_TILT_DETECTOR 582 1, // SENSOR_TYPE_WAKE_GESTURE 583 1, // SENSOR_TYPE_GLANCE_GESTURE 584 1, // SENSOR_TYPE_PICK_UP_GESTURE 585 }; 586 587 /** 588 * Each sensor has exactly one reporting mode associated with it. This method returns the 589 * reporting mode constant for this sensor type. 590 * 591 * @return Reporting mode for the input sensor, one of REPORTING_MODE_* constants. 592 * @see #REPORTING_MODE_CONTINUOUS 593 * @see #REPORTING_MODE_ON_CHANGE 594 * @see #REPORTING_MODE_ONE_SHOT 595 * @see #REPORTING_MODE_SPECIAL_TRIGGER 596 */ 597 public int getReportingMode() { 598 return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT); 599 } 600 601 static int getMaxLengthValuesArray(Sensor sensor, int sdkLevel) { 602 // RotationVector length has changed to 3 to 5 for API level 18 603 // Set it to 3 for backward compatibility. 604 if (sensor.mType == Sensor.TYPE_ROTATION_VECTOR && 605 sdkLevel <= Build.VERSION_CODES.JELLY_BEAN_MR1) { 606 return 3; 607 } 608 int offset = sensor.mType; 609 if (offset >= sSensorReportingModes.length) { 610 // we don't know about this sensor, so this is probably a 611 // vendor-defined sensor, in that case, we don't know how many value 612 // it has 613 // so we return the maximum and assume the app will know. 614 // FIXME: sensor HAL should advertise how much data is returned per 615 // sensor 616 return 16; 617 } 618 return sSensorReportingModes[offset]; 619 } 620 621 /* Some of these fields are set only by the native bindings in 622 * SensorManager. 623 */ 624 private String mName; 625 private String mVendor; 626 private int mVersion; 627 private int mHandle; 628 private int mType; 629 private float mMaxRange; 630 private float mResolution; 631 private float mPower; 632 private int mMinDelay; 633 private int mFifoReservedEventCount; 634 private int mFifoMaxEventCount; 635 private String mStringType; 636 private String mRequiredPermission; 637 private int mMaxDelay; 638 private int mFlags; 639 640 Sensor() { 641 } 642 643 /** 644 * @return name string of the sensor. 645 */ 646 public String getName() { 647 return mName; 648 } 649 650 /** 651 * @return vendor string of this sensor. 652 */ 653 public String getVendor() { 654 return mVendor; 655 } 656 657 /** 658 * @return generic type of this sensor. 659 */ 660 public int getType() { 661 return mType; 662 } 663 664 /** 665 * @return version of the sensor's module. 666 */ 667 public int getVersion() { 668 return mVersion; 669 } 670 671 /** 672 * @return maximum range of the sensor in the sensor's unit. 673 */ 674 public float getMaximumRange() { 675 return mMaxRange; 676 } 677 678 /** 679 * @return resolution of the sensor in the sensor's unit. 680 */ 681 public float getResolution() { 682 return mResolution; 683 } 684 685 /** 686 * @return the power in mA used by this sensor while in use 687 */ 688 public float getPower() { 689 return mPower; 690 } 691 692 /** 693 * @return the minimum delay allowed between two events in microsecond 694 * or zero if this sensor only returns a value when the data it's measuring 695 * changes. 696 */ 697 public int getMinDelay() { 698 return mMinDelay; 699 } 700 701 /** 702 * @return Number of events reserved for this sensor in the batch mode FIFO. This gives a 703 * guarantee on the minimum number of events that can be batched. 704 */ 705 public int getFifoReservedEventCount() { 706 return mFifoReservedEventCount; 707 } 708 709 /** 710 * @return Maximum number of events of this sensor that could be batched. If this value is zero 711 * it indicates that batch mode is not supported for this sensor. If other applications 712 * registered to batched sensors, the actual number of events that can be batched might be 713 * smaller because the hardware FiFo will be partially used to batch the other sensors. 714 */ 715 public int getFifoMaxEventCount() { 716 return mFifoMaxEventCount; 717 } 718 719 /** 720 * @return The type of this sensor as a string. 721 */ 722 public String getStringType() { 723 return mStringType; 724 } 725 726 /** 727 * @hide 728 * @return The permission required to access this sensor. If empty, no permission is required. 729 */ 730 public String getRequiredPermission() { 731 return mRequiredPermission; 732 } 733 734 /** @hide */ 735 public int getHandle() { 736 return mHandle; 737 } 738 739 /** 740 * This value is defined only for continuous and on-change sensors. It is the delay between two 741 * sensor events corresponding to the lowest frequency that this sensor supports. When lower 742 * frequencies are requested through registerListener() the events will be generated at this 743 * frequency instead. It can be used to estimate when the batch FIFO may be full. Older devices 744 * may set this value to zero. Ignore this value in case it is negative or zero. 745 * 746 * @return The max delay for this sensor in microseconds. 747 */ 748 public int getMaxDelay() { 749 return mMaxDelay; 750 } 751 752 /** 753 * Returns whether this sensor is a wake-up sensor. 754 * <p> 755 * Wake up sensors wake the application processor up when they have events to deliver. When a 756 * wake up sensor is registered to without batching enabled, each event will wake the 757 * application processor up. 758 * <p> 759 * When a wake up sensor is registered to with batching enabled, it 760 * wakes the application processor up when maxReportingLatency has elapsed or when the hardware 761 * FIFO storing the events from wake up sensors is getting full. 762 * <p> 763 * Non-wake up sensors never wake the application processor up. Their events are only reported 764 * when the application processor is awake, for example because the application holds a wake 765 * lock, or another source woke the application processor up. 766 * <p> 767 * When a non-wake up sensor is registered to without batching enabled, the measurements made 768 * while the application processor is asleep might be lost and never returned. 769 * <p> 770 * When a non-wake up sensor is registered to with batching enabled, the measurements made while 771 * the application processor is asleep are stored in the hardware FIFO for non-wake up sensors. 772 * When this FIFO gets full, new events start overwriting older events. When the application 773 * then wakes up, the latest events are returned, and some old events might be lost. The number 774 * of events actually returned depends on the hardware FIFO size, as well as on what other 775 * sensors are activated. If losing sensor events is not acceptable during batching, you must 776 * use the wake-up version of the sensor. 777 * @return true if this is a wake up sensor, false otherwise. 778 */ 779 public boolean isWakeUpSensor() { 780 return (mFlags & SENSOR_FLAG_WAKE_UP_SENSOR) != 0; 781 } 782 783 void setRange(float max, float res) { 784 mMaxRange = max; 785 mResolution = res; 786 } 787 788 @Override 789 public String toString() { 790 return "{Sensor name=\"" + mName + "\", vendor=\"" + mVendor + "\", version=" + mVersion 791 + ", type=" + mType + ", maxRange=" + mMaxRange + ", resolution=" + mResolution 792 + ", power=" + mPower + ", minDelay=" + mMinDelay + "}"; 793 } 794 } 795