Sensor.java revision 6d0c1d78f121d4f1b72973740e8b120c7def1dc0
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. See 35 * {@link android.hardware.SensorEvent#values SensorEvent.values} for more 36 * details. 37 */ 38 public static final int TYPE_ACCELEROMETER = 1; 39 40 /** 41 * A constant describing a magnetic field sensor type. See 42 * {@link android.hardware.SensorEvent#values SensorEvent.values} for more 43 * details. 44 */ 45 public static final int TYPE_MAGNETIC_FIELD = 2; 46 47 /** 48 * A constant describing an orientation sensor type. See 49 * {@link android.hardware.SensorEvent#values SensorEvent.values} for more 50 * details. 51 * 52 * @deprecated use {@link android.hardware.SensorManager#getOrientation 53 * SensorManager.getOrientation()} instead. 54 */ 55 @Deprecated 56 public static final int TYPE_ORIENTATION = 3; 57 58 /** A constant describing a gyroscope sensor type */ 59 public static final int TYPE_GYROSCOPE = 4; 60 61 /** 62 * A constant describing a light sensor type. See 63 * {@link android.hardware.SensorEvent#values SensorEvent.values} for more 64 * details. 65 */ 66 public static final int TYPE_LIGHT = 5; 67 68 /** A constant describing a pressure sensor type */ 69 public static final int TYPE_PRESSURE = 6; 70 71 /** 72 * A constant describing a temperature sensor type 73 * 74 * @deprecated use 75 * {@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE 76 * Sensor.TYPE_AMBIENT_TEMPERATURE} instead. 77 */ 78 @Deprecated 79 public static final int TYPE_TEMPERATURE = 7; 80 81 /** 82 * A constant describing a proximity sensor type. See 83 * {@link android.hardware.SensorEvent#values SensorEvent.values} for more 84 * details. 85 */ 86 public static final int TYPE_PROXIMITY = 8; 87 88 /** 89 * A constant describing a gravity sensor type. 90 * See {@link android.hardware.SensorEvent SensorEvent} 91 * for more details. 92 */ 93 public static final int TYPE_GRAVITY = 9; 94 95 /** 96 * A constant describing a linear acceleration sensor type. 97 * See {@link android.hardware.SensorEvent SensorEvent} 98 * for more details. 99 */ 100 public static final int TYPE_LINEAR_ACCELERATION = 10; 101 102 /** 103 * A constant describing a rotation vector sensor type. 104 * See {@link android.hardware.SensorEvent SensorEvent} 105 * for more details. 106 */ 107 public static final int TYPE_ROTATION_VECTOR = 11; 108 109 /** 110 * A constant describing a relative humidity sensor type. 111 * See {@link android.hardware.SensorEvent SensorEvent} 112 * for more details. 113 */ 114 public static final int TYPE_RELATIVE_HUMIDITY = 12; 115 116 /** A constant describing an ambient temperature sensor type */ 117 public static final int TYPE_AMBIENT_TEMPERATURE = 13; 118 119 /** 120 * A constant describing a magnetic field uncalibrated sensor type. See 121 * {@link android.hardware.SensorEvent#values SensorEvent.values} for more 122 * details. 123 * <p> 124 * Similar to {@link #TYPE_MAGNETIC_FIELD} but the hard iron calibration (calibration 125 * due to distortions that arise from magnetized iron, steel or permanenet magnets 126 * on the device) is reported separately. No periodic calibration is performed 127 * (i.e. there are no discontinuities in the data stream while using this sensor). 128 * Assumptions that the magnetic field is due to the Earth's poles is avoided. 129 * Factory calibration and temperature compensation are still performed. 130 * </p> 131 */ 132 public static final int TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14; 133 134 /** 135 * Identical to {@link #TYPE_ROTATION_VECTOR} except that it doesn't 136 * use the geomagnetic field. Therefore the Y axis doesn't 137 * point north, but instead to some other reference, that reference is 138 * allowed to drift by the same order of magnitude as the gyroscope 139 * drift around the Z axis. 140 * <p> 141 * In the ideal case, a phone rotated and returning to the same real-world 142 * orientation should report the same game rotation vector 143 * (without using the earth's geomagnetic field). However, the orientation 144 * may drift somewhat over time. 145 * </p> 146 */ 147 148 public static final int TYPE_GAME_ROTATION_VECTOR = 15; 149 150 /** 151 * A constant describing a gyroscope uncalibrated sensor type. See 152 * {@link android.hardware.SensorEvent#values SensorEvent.values} for more 153 * details. 154 * <p> 155 * No gyro-drift compensation is performed. 156 * Factory calibration and temperature compensation is still applied 157 * to the rate of rotation (angular speeds). 158 * </p> 159 */ 160 public static final int TYPE_GYROSCOPE_UNCALIBRATED = 16; 161 162 /** 163 * A constant describing the significant motion trigger sensor. 164 * See {@link android.hardware.SensorEvent#values} for more details. 165 * <p> 166 * It triggers when an event occurs and then automatically disables 167 * itself. The sensor continues to operate while the device is asleep 168 * and will automatically wake the device to notify when significant 169 * motion is detected. The application does not need to hold any wake 170 * locks for this sensor to trigger. 171 * </p> 172 */ 173 public static final int TYPE_SIGNIFICANT_MOTION = 17; 174 175 /** 176 * A constant describing all sensor types. 177 */ 178 public static final int TYPE_ALL = -1; 179 180 /* Reporting mode constants for sensors. Each sensor will have exactly one 181 reporting mode associated with it. */ 182 // Events are reported at a constant rate. 183 static int REPORTING_MODE_CONTINUOUS = 1; 184 185 // Events are reported only when the value changes. 186 static int REPORTING_MODE_ON_CHANGE = 2; 187 188 // Upon detection of an event, the sensor deactivates itself and then sends a single event. 189 static int REPORTING_MODE_ONE_SHOT = 3; 190 191 // Note: This needs to be updated, whenever a new sensor is added. 192 private static int[] sSensorReportingModes = { 193 REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, 194 REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS, 195 REPORTING_MODE_ON_CHANGE, REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS, 196 REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ON_CHANGE, 197 REPORTING_MODE_ON_CHANGE, REPORTING_MODE_CONTINUOUS, REPORTING_MODE_CONTINUOUS, 198 REPORTING_MODE_CONTINUOUS, REPORTING_MODE_ONE_SHOT }; 199 200 // Note: This needs to be updated, whenever a new sensor is added. 201 // Holds the maximum length of the values array associated with {@link SensorEvent} or 202 // {@link TriggerEvent} for the Sensor 203 private static int[] sMaxLengthValuesArray = { 204 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 205 6, 4, 6, 1 }; 206 207 static int getReportingMode(Sensor sensor) { 208 // mType starts from offset 1. 209 return sSensorReportingModes[sensor.mType - 1]; 210 } 211 212 static int getMaxLengthValuesArray(Sensor sensor, int sdkLevel) { 213 // mType starts from offset 1. 214 int len = sMaxLengthValuesArray[sensor.mType - 1]; 215 216 // RotationVector length has changed to 3 to 5 for API level 18 217 // Set it to 3 for backward compatibility. 218 if (sensor.getType() == Sensor.TYPE_ROTATION_VECTOR && 219 sdkLevel <= Build.VERSION_CODES.JELLY_BEAN_MR1) { 220 len = 3; 221 } 222 return len; 223 } 224 225 /* Some of these fields are set only by the native bindings in 226 * SensorManager. 227 */ 228 private String mName; 229 private String mVendor; 230 private int mVersion; 231 private int mHandle; 232 private int mType; 233 private float mMaxRange; 234 private float mResolution; 235 private float mPower; 236 private int mMinDelay; 237 238 Sensor() { 239 } 240 241 /** 242 * @return name string of the sensor. 243 */ 244 public String getName() { 245 return mName; 246 } 247 248 /** 249 * @return vendor string of this sensor. 250 */ 251 public String getVendor() { 252 return mVendor; 253 } 254 255 /** 256 * @return generic type of this sensor. 257 */ 258 public int getType() { 259 return mType; 260 } 261 262 /** 263 * @return version of the sensor's module. 264 */ 265 public int getVersion() { 266 return mVersion; 267 } 268 269 /** 270 * @return maximum range of the sensor in the sensor's unit. 271 */ 272 public float getMaximumRange() { 273 return mMaxRange; 274 } 275 276 /** 277 * @return resolution of the sensor in the sensor's unit. 278 */ 279 public float getResolution() { 280 return mResolution; 281 } 282 283 /** 284 * @return the power in mA used by this sensor while in use 285 */ 286 public float getPower() { 287 return mPower; 288 } 289 290 /** 291 * @return the minimum delay allowed between two events in microsecond 292 * or zero if this sensor only returns a value when the data it's measuring 293 * changes. 294 */ 295 public int getMinDelay() { 296 return mMinDelay; 297 } 298 299 /** @hide */ 300 public int getHandle() { 301 return mHandle; 302 } 303 304 void setRange(float max, float res) { 305 mMaxRange = max; 306 mResolution = res; 307 } 308 309 @Override 310 public String toString() { 311 return "{Sensor name=\"" + mName + "\", vendor=\"" + mVendor + "\", version=" + mVersion 312 + ", type=" + mType + ", maxRange=" + mMaxRange + ", resolution=" + mResolution 313 + ", power=" + mPower + ", minDelay=" + mMinDelay + "}"; 314 } 315} 316