blob: 5cc11505d6b9c4cbb950f24e624846f0e9bba9dc [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
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -070020import android.os.Build;
21
Mathias Agopian74cde2c2010-06-16 18:55:46 -070022/**
23 * Class representing a sensor. Use {@link SensorManager#getSensorList} to get
24 * the list of available Sensors.
Mathias Agopian0f791a72010-06-22 21:55:01 -070025 *
26 * @see SensorManager
27 * @see SensorEventListener
28 * @see SensorEvent
29 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 */
Jeff Brown6a2ef122012-08-10 18:19:10 -070031public final class Sensor {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Mathias Agopian74cde2c2010-06-16 18:55:46 -070033 /**
Scott Main92053f82013-06-13 16:32:50 -070034 * A constant describing an accelerometer sensor type.
35 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
36 * for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 */
Mathias Agopian74cde2c2010-06-16 18:55:46 -070038 public static final int TYPE_ACCELEROMETER = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Mathias Agopian74cde2c2010-06-16 18:55:46 -070040 /**
Scott Main92053f82013-06-13 16:32:50 -070041 * A constant describing a magnetic field sensor type.
42 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
43 * for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 */
45 public static final int TYPE_MAGNETIC_FIELD = 2;
Mathias Agopian74cde2c2010-06-16 18:55:46 -070046
47 /**
Scott Main92053f82013-06-13 16:32:50 -070048 * A constant describing an orientation sensor type.
49 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
50 * for more details.
Mathias Agopian74cde2c2010-06-16 18:55:46 -070051 *
Mathias Agopian61a44932010-02-26 18:06:26 -080052 * @deprecated use {@link android.hardware.SensorManager#getOrientation
Mathias Agopian74cde2c2010-06-16 18:55:46 -070053 * SensorManager.getOrientation()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 */
Mathias Agopian61a44932010-02-26 18:06:26 -080055 @Deprecated
Mathias Agopian74cde2c2010-06-16 18:55:46 -070056 public static final int TYPE_ORIENTATION = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Scott Main92053f82013-06-13 16:32:50 -070058 /** A constant describing a gyroscope sensor type.
59 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
60 * for more details. */
Mathias Agopian74cde2c2010-06-16 18:55:46 -070061 public static final int TYPE_GYROSCOPE = 4;
62
Mike Lockwood4115c512009-11-03 10:35:43 -050063 /**
Scott Main92053f82013-06-13 16:32:50 -070064 * A constant describing a light sensor type.
65 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
66 * for more details.
Mike Lockwood4115c512009-11-03 10:35:43 -050067 */
Mathias Agopian74cde2c2010-06-16 18:55:46 -070068 public static final int TYPE_LIGHT = 5;
Mathias Agopian210fc912010-02-26 13:51:39 -080069
Scott Main92053f82013-06-13 16:32:50 -070070 /** A constant describing a pressure sensor type.
71 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
72 * for more details. */
Mathias Agopian74cde2c2010-06-16 18:55:46 -070073 public static final int TYPE_PRESSURE = 6;
Mathias Agopian210fc912010-02-26 13:51:39 -080074
Mathias Agopian462db222011-03-22 18:44:26 -070075 /**
76 * A constant describing a temperature sensor type
77 *
78 * @deprecated use
79 * {@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE
80 * Sensor.TYPE_AMBIENT_TEMPERATURE} instead.
81 */
82 @Deprecated
Mathias Agopian74cde2c2010-06-16 18:55:46 -070083 public static final int TYPE_TEMPERATURE = 7;
Mathias Agopian210fc912010-02-26 13:51:39 -080084
Mike Lockwood4115c512009-11-03 10:35:43 -050085 /**
Scott Main92053f82013-06-13 16:32:50 -070086 * A constant describing a proximity sensor type.
87 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
88 * for more details.
Mike Lockwood4115c512009-11-03 10:35:43 -050089 */
Mathias Agopian74cde2c2010-06-16 18:55:46 -070090 public static final int TYPE_PROXIMITY = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Mathias Agopian74cde2c2010-06-16 18:55:46 -070092 /**
Kevin Powellf35fd952010-07-19 19:10:40 -070093 * A constant describing a gravity sensor type.
Scott Main92053f82013-06-13 16:32:50 -070094 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Kevin Powellf35fd952010-07-19 19:10:40 -070095 * for more details.
96 */
97 public static final int TYPE_GRAVITY = 9;
98
99 /**
100 * A constant describing a linear acceleration sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700101 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Kevin Powellf35fd952010-07-19 19:10:40 -0700102 * for more details.
103 */
104 public static final int TYPE_LINEAR_ACCELERATION = 10;
105
106 /**
107 * A constant describing a rotation vector sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700108 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Kevin Powellf35fd952010-07-19 19:10:40 -0700109 * for more details.
110 */
111 public static final int TYPE_ROTATION_VECTOR = 11;
112
Urs Fleisch58190512010-12-29 17:02:02 +0100113 /**
114 * A constant describing a relative humidity sensor type.
Scott Main92053f82013-06-13 16:32:50 -0700115 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
Urs Fleisch58190512010-12-29 17:02:02 +0100116 * for more details.
117 */
118 public static final int TYPE_RELATIVE_HUMIDITY = 12;
119
Scott Main92053f82013-06-13 16:32:50 -0700120 /** A constant describing an ambient temperature sensor type.
121 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values}
122 * for more details. */
Mathias Agopian462db222011-03-22 18:44:26 -0700123 public static final int TYPE_AMBIENT_TEMPERATURE = 13;
124
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800125 /**
Scott Main92053f82013-06-13 16:32:50 -0700126 * A constant describing an uncalibrated magnetic field sensor type.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800127 * <p>
Scott Main92053f82013-06-13 16:32:50 -0700128 * Similar to {@link #TYPE_MAGNETIC_FIELD} but the hard iron calibration (device calibration
129 * due to distortions that arise from magnetized iron, steel or permanent magnets on the
130 * device) is not considered in the given sensor values. However, such hard iron bias values
131 * are returned to you separately in the result {@link android.hardware.SensorEvent#values}
132 * so you may use them for custom calibrations.
133 * <p>Also, no periodic calibration is performed
134 * (i.e. there are no discontinuities in the data stream while using this sensor) and
135 * assumptions that the magnetic field is due to the Earth's poles is avoided, but
136 * factory calibration and temperature compensation have been performed.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800137 * </p>
Scott Main92053f82013-06-13 16:32:50 -0700138 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
139 * details.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800140 */
141 public static final int TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14;
142
143 /**
Scott Main92053f82013-06-13 16:32:50 -0700144 * A constant describing an uncalibrated rotation vector sensor type.
145 * <p>Identical to {@link #TYPE_ROTATION_VECTOR} except that it doesn't
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800146 * use the geomagnetic field. Therefore the Y axis doesn't
147 * point north, but instead to some other reference, that reference is
148 * allowed to drift by the same order of magnitude as the gyroscope
149 * drift around the Z axis.
150 * <p>
151 * In the ideal case, a phone rotated and returning to the same real-world
152 * orientation should report the same game rotation vector
153 * (without using the earth's geomagnetic field). However, the orientation
154 * may drift somewhat over time.
155 * </p>
Scott Main92053f82013-06-13 16:32:50 -0700156 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
157 * details.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800158 */
159
160 public static final int TYPE_GAME_ROTATION_VECTOR = 15;
161
162 /**
Scott Main92053f82013-06-13 16:32:50 -0700163 * A constant describing an uncalibrated gyroscope sensor type.
164 * <p>Similar to {@link #TYPE_GYROSCOPE} but no gyro-drift compensation has been performed
165 * to adjust the given sensor values. However, such gyro-drift bias values
166 * are returned to you separately in the result {@link android.hardware.SensorEvent#values}
167 * so you may use them for custom calibrations.
168 * <p>Factory calibration and temperature compensation is still applied
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800169 * to the rate of rotation (angular speeds).
170 * </p>
Scott Main92053f82013-06-13 16:32:50 -0700171 * <p> See {@link android.hardware.SensorEvent#values SensorEvent.values} for more
172 * details.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800173 */
174 public static final int TYPE_GYROSCOPE_UNCALIBRATED = 16;
175
176 /**
177 * A constant describing the significant motion trigger sensor.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800178 * <p>
179 * It triggers when an event occurs and then automatically disables
180 * itself. The sensor continues to operate while the device is asleep
181 * and will automatically wake the device to notify when significant
182 * motion is detected. The application does not need to hold any wake
183 * locks for this sensor to trigger.
Scott Main92053f82013-06-13 16:32:50 -0700184 * <p>See {@link TriggerEvent} for more details.
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800185 */
186 public static final int TYPE_SIGNIFICANT_MOTION = 17;
187
188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * A constant describing all sensor types.
190 */
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700191 public static final int TYPE_ALL = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800193 /* Reporting mode constants for sensors. Each sensor will have exactly one
194 reporting mode associated with it. */
195 // Events are reported at a constant rate.
196 static int REPORTING_MODE_CONTINUOUS = 1;
197
198 // Events are reported only when the value changes.
199 static int REPORTING_MODE_ON_CHANGE = 2;
200
201 // Upon detection of an event, the sensor deactivates itself and then sends a single event.
202 static int REPORTING_MODE_ONE_SHOT = 3;
203
Jaikumar Ganeshedb52422013-04-16 11:10:05 -0700204 // TODO(): The following arrays are fragile and error-prone. This needs to be refactored.
205
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800206 // Note: This needs to be updated, whenever a new sensor is added.
207 private static int[] sSensorReportingModes = {
208 REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS,
209 REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS,
210 REPORTING_MODE_ON_CHANGE, REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS,
211 REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ON_CHANGE,
212 REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS,
213 REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ONE_SHOT };
214
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700215 // Note: This needs to be updated, whenever a new sensor is added.
216 // Holds the maximum length of the values array associated with {@link SensorEvent} or
217 // {@link TriggerEvent} for the Sensor
218 private static int[] sMaxLengthValuesArray = {
Jaikumar Ganeshedb52422013-04-16 11:10:05 -0700219 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3,
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700220 6, 4, 6, 1 };
221
Jaikumar Ganesh9a8df4d2013-02-12 16:31:32 -0800222 static int getReportingMode(Sensor sensor) {
223 // mType starts from offset 1.
224 return sSensorReportingModes[sensor.mType - 1];
225 }
226
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700227 static int getMaxLengthValuesArray(Sensor sensor, int sdkLevel) {
228 // mType starts from offset 1.
229 int len = sMaxLengthValuesArray[sensor.mType - 1];
230
231 // RotationVector length has changed to 3 to 5 for API level 18
232 // Set it to 3 for backward compatibility.
233 if (sensor.getType() == Sensor.TYPE_ROTATION_VECTOR &&
234 sdkLevel <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
235 len = 3;
236 }
237 return len;
238 }
239
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700240 /* Some of these fields are set only by the native bindings in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 * SensorManager.
242 */
243 private String mName;
244 private String mVendor;
245 private int mVersion;
246 private int mHandle;
247 private int mType;
248 private float mMaxRange;
249 private float mResolution;
250 private float mPower;
Mathias Agopian050b5622010-07-29 16:51:38 -0700251 private int mMinDelay;
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 Sensor() {
254 }
255
256 /**
257 * @return name string of the sensor.
258 */
259 public String getName() {
260 return mName;
261 }
262
263 /**
264 * @return vendor string of this sensor.
265 */
266 public String getVendor() {
267 return mVendor;
268 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * @return generic type of this sensor.
272 */
273 public int getType() {
274 return mType;
275 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * @return version of the sensor's module.
279 */
280 public int getVersion() {
281 return mVersion;
282 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * @return maximum range of the sensor in the sensor's unit.
286 */
287 public float getMaximumRange() {
288 return mMaxRange;
289 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
292 * @return resolution of the sensor in the sensor's unit.
293 */
294 public float getResolution() {
295 return mResolution;
296 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * @return the power in mA used by this sensor while in use
300 */
301 public float getPower() {
302 return mPower;
303 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700304
Mathias Agopian050b5622010-07-29 16:51:38 -0700305 /**
306 * @return the minimum delay allowed between two events in microsecond
307 * or zero if this sensor only returns a value when the data it's measuring
308 * changes.
309 */
310 public int getMinDelay() {
311 return mMinDelay;
312 }
313
Mathias Agopian7f84c062013-02-04 19:22:47 -0800314 /** @hide */
315 public int getHandle() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 return mHandle;
317 }
Mathias Agopian74cde2c2010-06-16 18:55:46 -0700318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 void setRange(float max, float res) {
320 mMaxRange = max;
321 mResolution = res;
322 }
Jeff Brown6a2ef122012-08-10 18:19:10 -0700323
324 @Override
325 public String toString() {
326 return "{Sensor name=\"" + mName + "\", vendor=\"" + mVendor + "\", version=" + mVersion
327 + ", type=" + mType + ", maxRange=" + mMaxRange + ", resolution=" + mResolution
328 + ", power=" + mPower + ", minDelay=" + mMinDelay + "}";
329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330}