1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware;
import android.os.Handler;
import android.util.Log;
import android.util.SparseArray;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* <p>
* SensorManager lets you access the device's {@link android.hardware.Sensor
* sensors}. Get an instance of this class by calling
* {@link android.content.Context#getSystemService(java.lang.String)
* Context.getSystemService()} with the argument
* {@link android.content.Context#SENSOR_SERVICE}.
* </p>
* <p>
* Always make sure to disable sensors you don't need, especially when your
* activity is paused. Failing to do so can drain the battery in just a few
* hours. Note that the system will <i>not</i> disable sensors automatically when
* the screen turns off.
* </p>
* <p class="note">
* Note: Don't use this mechanism with a Trigger Sensor, have a look
* at {@link TriggerEventListener}. {@link Sensor#TYPE_SIGNIFICANT_MOTION}
* is an example of a trigger sensor.
* </p>
* <pre class="prettyprint">
* public class SensorActivity extends Activity, implements SensorEventListener {
* private final SensorManager mSensorManager;
* private final Sensor mAccelerometer;
*
* public SensorActivity() {
* mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
* mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
* }
*
* protected void onResume() {
* super.onResume();
* mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
* }
*
* protected void onPause() {
* super.onPause();
* mSensorManager.unregisterListener(this);
* }
*
* public void onAccuracyChanged(Sensor sensor, int accuracy) {
* }
*
* public void onSensorChanged(SensorEvent event) {
* }
* }
* </pre>
*
* @see SensorEventListener
* @see SensorEvent
* @see Sensor
*
*/
public abstract class SensorManager {
/** @hide */
protected static final String TAG = "SensorManager";
private static final float[] mTempMatrix = new float[16];
// Cached lists of sensors by type. Guarded by mSensorListByType.
private final SparseArray<List<Sensor>> mSensorListByType =
new SparseArray<List<Sensor>>();
// Legacy sensor manager implementation. Guarded by mSensorListByType during initialization.
private LegacySensorManager mLegacySensorManager;
/* NOTE: sensor IDs must be a power of 2 */
/**
* A constant describing an orientation sensor. See
* {@link android.hardware.SensorListener SensorListener} for more details.
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_ORIENTATION = 1 << 0;
/**
* A constant describing an accelerometer. See
* {@link android.hardware.SensorListener SensorListener} for more details.
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_ACCELEROMETER = 1 << 1;
/**
* A constant describing a temperature sensor See
* {@link android.hardware.SensorListener SensorListener} for more details.
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_TEMPERATURE = 1 << 2;
/**
* A constant describing a magnetic sensor See
* {@link android.hardware.SensorListener SensorListener} for more details.
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_MAGNETIC_FIELD = 1 << 3;
/**
* A constant describing an ambient light sensor See
* {@link android.hardware.SensorListener SensorListener} for more details.
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_LIGHT = 1 << 4;
/**
* A constant describing a proximity sensor See
* {@link android.hardware.SensorListener SensorListener} for more details.
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_PROXIMITY = 1 << 5;
/**
* A constant describing a Tricorder See
* {@link android.hardware.SensorListener SensorListener} for more details.
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_TRICORDER = 1 << 6;
/**
* A constant describing an orientation sensor. See
* {@link android.hardware.SensorListener SensorListener} for more details.
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_ORIENTATION_RAW = 1 << 7;
/**
* A constant that includes all sensors
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_ALL = 0x7F;
/**
* Smallest sensor ID
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_MIN = SENSOR_ORIENTATION;
/**
* Largest sensor ID
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int SENSOR_MAX = ((SENSOR_ALL + 1)>>1);
/**
* Index of the X value in the array returned by
* {@link android.hardware.SensorListener#onSensorChanged}
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int DATA_X = 0;
/**
* Index of the Y value in the array returned by
* {@link android.hardware.SensorListener#onSensorChanged}
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int DATA_Y = 1;
/**
* Index of the Z value in the array returned by
* {@link android.hardware.SensorListener#onSensorChanged}
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int DATA_Z = 2;
/**
* Offset to the untransformed values in the array returned by
* {@link android.hardware.SensorListener#onSensorChanged}
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int RAW_DATA_INDEX = 3;
/**
* Index of the untransformed X value in the array returned by
* {@link android.hardware.SensorListener#onSensorChanged}
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int RAW_DATA_X = 3;
/**
* Index of the untransformed Y value in the array returned by
* {@link android.hardware.SensorListener#onSensorChanged}
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int RAW_DATA_Y = 4;
/**
* Index of the untransformed Z value in the array returned by
* {@link android.hardware.SensorListener#onSensorChanged}
*
* @deprecated use {@link android.hardware.Sensor Sensor} instead.
*/
@Deprecated
public static final int RAW_DATA_Z = 5;
/** Standard gravity (g) on Earth. This value is equivalent to 1G */
public static final float STANDARD_GRAVITY = 9.80665f;
/** Sun's gravity in SI units (m/s^2) */
public static final float GRAVITY_SUN = 275.0f;
/** Mercury's gravity in SI units (m/s^2) */
public static final float GRAVITY_MERCURY = 3.70f;
/** Venus' gravity in SI units (m/s^2) */
public static final float GRAVITY_VENUS = 8.87f;
/** Earth's gravity in SI units (m/s^2) */
public static final float GRAVITY_EARTH = 9.80665f;
/** The Moon's gravity in SI units (m/s^2) */
public static final float GRAVITY_MOON = 1.6f;
/** Mars' gravity in SI units (m/s^2) */
public static final float GRAVITY_MARS = 3.71f;
/** Jupiter's gravity in SI units (m/s^2) */
public static final float GRAVITY_JUPITER = 23.12f;
/** Saturn's gravity in SI units (m/s^2) */
public static final float GRAVITY_SATURN = 8.96f;
/** Uranus' gravity in SI units (m/s^2) */
public static final float GRAVITY_URANUS = 8.69f;
/** Neptune's gravity in SI units (m/s^2) */
public static final float GRAVITY_NEPTUNE = 11.0f;
/** Pluto's gravity in SI units (m/s^2) */
public static final float GRAVITY_PLUTO = 0.6f;
/** Gravity (estimate) on the first Death Star in Empire units (m/s^2) */
public static final float GRAVITY_DEATH_STAR_I = 0.000000353036145f;
/** Gravity on the island */
public static final float GRAVITY_THE_ISLAND = 4.815162342f;
/** Maximum magnetic field on Earth's surface */
public static final float MAGNETIC_FIELD_EARTH_MAX = 60.0f;
/** Minimum magnetic field on Earth's surface */
public static final float MAGNETIC_FIELD_EARTH_MIN = 30.0f;
/** Standard atmosphere, or average sea-level pressure in hPa (millibar) */
public static final float PRESSURE_STANDARD_ATMOSPHERE = 1013.25f;
/** Maximum luminance of sunlight in lux */
public static final float LIGHT_SUNLIGHT_MAX = 120000.0f;
/** luminance of sunlight in lux */
public static final float LIGHT_SUNLIGHT = 110000.0f;
/** luminance in shade in lux */
public static final float LIGHT_SHADE = 20000.0f;
/** luminance under an overcast sky in lux */
public static final float LIGHT_OVERCAST = 10000.0f;
/** luminance at sunrise in lux */
public static final float LIGHT_SUNRISE = 400.0f;
/** luminance under a cloudy sky in lux */
public static final float LIGHT_CLOUDY = 100.0f;
/** luminance at night with full moon in lux */
public static final float LIGHT_FULLMOON = 0.25f;
/** luminance at night with no moon in lux*/
public static final float LIGHT_NO_MOON = 0.001f;
/** get sensor data as fast as possible */
public static final int SENSOR_DELAY_FASTEST = 0;
/** rate suitable for games */
public static final int SENSOR_DELAY_GAME = 1;
/** rate suitable for the user interface */
public static final int SENSOR_DELAY_UI = 2;
/** rate (default) suitable for screen orientation changes */
public static final int SENSOR_DELAY_NORMAL = 3;
/**
* The values returned by this sensor cannot be trusted, calibration is
* needed or the environment doesn't allow readings
*/
public static final int SENSOR_STATUS_UNRELIABLE = 0;
/**
* This sensor is reporting data with low accuracy, calibration with the
* environment is needed
*/
public static final int SENSOR_STATUS_ACCURACY_LOW = 1;
/**
* This sensor is reporting data with an average level of accuracy,
* calibration with the environment may improve the readings
*/
public static final int SENSOR_STATUS_ACCURACY_MEDIUM = 2;
/** This sensor is reporting data with maximum accuracy */
public static final int SENSOR_STATUS_ACCURACY_HIGH = 3;
/** see {@link #remapCoordinateSystem} */
public static final int AXIS_X = 1;
/** see {@link #remapCoordinateSystem} */
public static final int AXIS_Y = 2;
/** see {@link #remapCoordinateSystem} */
public static final int AXIS_Z = 3;
/** see {@link #remapCoordinateSystem} */
public static final int AXIS_MINUS_X = AXIS_X | 0x80;
/** see {@link #remapCoordinateSystem} */
public static final int AXIS_MINUS_Y = AXIS_Y | 0x80;
/** see {@link #remapCoordinateSystem} */
public static final int AXIS_MINUS_Z = AXIS_Z | 0x80;
/**
* {@hide}
*/
public SensorManager() {
}
/**
* Gets the full list of sensors that are available.
* @hide
*/
protected abstract List<Sensor> getFullSensorList();
/**
* @return available sensors.
* @deprecated This method is deprecated, use
* {@link SensorManager#getSensorList(int)} instead
*/
@Deprecated
public int getSensors() {
return getLegacySensorManager().getSensors();
}
/**
* Use this method to get the list of available sensors of a certain type.
* Make multiple calls to get sensors of different types or use
* {@link android.hardware.Sensor#TYPE_ALL Sensor.TYPE_ALL} to get all the
* sensors.
*
* @param type
* of sensors requested
*
* @return a list of sensors matching the asked type.
*
* @see #getDefaultSensor(int)
* @see Sensor
*/
public List<Sensor> getSensorList(int type) {
// cache the returned lists the first time
List<Sensor> list;
final List<Sensor> fullList = getFullSensorList();
synchronized (mSensorListByType) {
list = mSensorListByType.get(type);
if (list == null) {
if (type == Sensor.TYPE_ALL) {
list = fullList;
} else {
list = new ArrayList<Sensor>();
for (Sensor i : fullList) {
if (i.getType() == type)
list.add(i);
}
}
list = Collections.unmodifiableList(list);
mSensorListByType.append(type, list);
}
}
return list;
}
/**
* Use this method to get the default sensor for a given type. Note that the
* returned sensor could be a composite sensor, and its data could be
* averaged or filtered. If you need to access the raw sensors use
* {@link SensorManager#getSensorList(int) getSensorList}.
*
* @param type
* of sensors requested
*
* @return the default sensors matching the asked type.
*
* @see #getSensorList(int)
* @see Sensor
*/
public Sensor getDefaultSensor(int type) {
// TODO: need to be smarter, for now, just return the 1st sensor
List<Sensor> l = getSensorList(type);
return l.isEmpty() ? null : l.get(0);
}
/**
* Registers a listener for given sensors.
*
* @deprecated This method is deprecated, use
* {@link SensorManager#registerListener(SensorEventListener, Sensor, int)}
* instead.
*
* @param listener
* sensor listener object
*
* @param sensors
* a bit masks of the sensors to register to
*
* @return <code>true</code> if the sensor is supported and successfully
* enabled
*/
@Deprecated
public boolean registerListener(SensorListener listener, int sensors) {
return registerListener(listener, sensors, SENSOR_DELAY_NORMAL);
}
/**
* Registers a SensorListener for given sensors.
*
* @deprecated This method is deprecated, use
* {@link SensorManager#registerListener(SensorEventListener, Sensor, int)}
* instead.
*
* @param listener
* sensor listener object
*
* @param sensors
* a bit masks of the sensors to register to
*
* @param rate
* rate of events. This is only a hint to the system. events may be
* received faster or slower than the specified rate. Usually events
* are received faster. The value must be one of
* {@link #SENSOR_DELAY_NORMAL}, {@link #SENSOR_DELAY_UI},
* {@link #SENSOR_DELAY_GAME}, or {@link #SENSOR_DELAY_FASTEST}.
*
* @return <code>true</code> if the sensor is supported and successfully
* enabled
*/
@Deprecated
public boolean registerListener(SensorListener listener, int sensors, int rate) {
return getLegacySensorManager().registerListener(listener, sensors, rate);
}
/**
* Unregisters a listener for all sensors.
*
* @deprecated This method is deprecated, use
* {@link SensorManager#unregisterListener(SensorEventListener)}
* instead.
*
* @param listener
* a SensorListener object
*/
@Deprecated
public void unregisterListener(SensorListener listener) {
unregisterListener(listener, SENSOR_ALL | SENSOR_ORIENTATION_RAW);
}
/**
* Unregisters a listener for the sensors with which it is registered.
*
* @deprecated This method is deprecated, use
* {@link SensorManager#unregisterListener(SensorEventListener, Sensor)}
* instead.
*
* @param listener
* a SensorListener object
*
* @param sensors
* a bit masks of the sensors to unregister from
*/
@Deprecated
public void unregisterListener(SensorListener listener, int sensors) {
getLegacySensorManager().unregisterListener(listener, sensors);
}
/**
* Unregisters a listener for the sensors with which it is registered.
*
* <p class="note"></p>
* Note: Don't use this method with a one shot trigger sensor such as
* {@link Sensor#TYPE_SIGNIFICANT_MOTION}.
* Use {@link #cancelTriggerSensor(TriggerEventListener, Sensor)} instead.
* </p>
*
* @param listener
* a SensorEventListener object
*
* @param sensor
* the sensor to unregister from
*
* @see #unregisterListener(SensorEventListener)
* @see #registerListener(SensorEventListener, Sensor, int)
*
* @throws IllegalArgumentException when sensor is a trigger sensor.
*/
public void unregisterListener(SensorEventListener listener, Sensor sensor) {
if (listener == null || sensor == null) {
return;
}
unregisterListenerImpl(listener, sensor);
}
/**
* Unregisters a listener for all sensors.
*
* @param listener
* a SensorListener object
*
* @see #unregisterListener(SensorEventListener, Sensor)
* @see #registerListener(SensorEventListener, Sensor, int)
*
*/
public void unregisterListener(SensorEventListener listener) {
if (listener == null) {
return;
}
unregisterListenerImpl(listener, null);
}
/** @hide */
protected abstract void unregisterListenerImpl(SensorEventListener listener, Sensor sensor);
/**
* Registers a {@link android.hardware.SensorEventListener
* SensorEventListener} for the given sensor.
*
* <p class="note"></p>
* Note: Don't use this method with a one shot trigger sensor such as
* {@link Sensor#TYPE_SIGNIFICANT_MOTION}.
* Use {@link #requestTriggerSensor(TriggerEventListener, Sensor)} instead.
* </p>
*
* @param listener
* A {@link android.hardware.SensorEventListener SensorEventListener}
* object.
*
* @param sensor
* The {@link android.hardware.Sensor Sensor} to register to.
*
* @param rate
* The rate {@link android.hardware.SensorEvent sensor events} are
* delivered at. This is only a hint to the system. Events may be
* received faster or slower than the specified rate. Usually events
* are received faster. The value must be one of
* {@link #SENSOR_DELAY_NORMAL}, {@link #SENSOR_DELAY_UI},
* {@link #SENSOR_DELAY_GAME}, or {@link #SENSOR_DELAY_FASTEST}
* or, the desired delay between events in microseconds.
* Specifying the delay in microseconds only works from Android
* 2.3 (API level 9) onwards. For earlier releases, you must use
* one of the {@code SENSOR_DELAY_*} constants.
*
* @return <code>true</code> if the sensor is supported and successfully
* enabled.
*
* @see #registerListener(SensorEventListener, Sensor, int, Handler)
* @see #unregisterListener(SensorEventListener)
* @see #unregisterListener(SensorEventListener, Sensor)
*
* @throws IllegalArgumentException when sensor is null or a trigger sensor
*/
public boolean registerListener(SensorEventListener listener, Sensor sensor, int rate) {
return registerListener(listener, sensor, rate, null);
}
/**
* Registers a {@link android.hardware.SensorEventListener
* SensorEventListener} for the given sensor.
*
* <p class="note"></p>
* Note: Don't use this method with a one shot trigger sensor such as
* {@link Sensor#TYPE_SIGNIFICANT_MOTION}.
* Use {@link #requestTriggerSensor(TriggerEventListener, Sensor)} instead.
* </p>
*
* @param listener
* A {@link android.hardware.SensorEventListener SensorEventListener}
* object.
*
* @param sensor
* The {@link android.hardware.Sensor Sensor} to register to.
*
* @param rate
* The rate {@link android.hardware.SensorEvent sensor events} are
* delivered at. This is only a hint to the system. Events may be
* received faster or slower than the specified rate. Usually events
* are received faster. The value must be one of
* {@link #SENSOR_DELAY_NORMAL}, {@link #SENSOR_DELAY_UI},
* {@link #SENSOR_DELAY_GAME}, or {@link #SENSOR_DELAY_FASTEST}.
* or, the desired delay between events in microseconds.
* Specifying the delay in microseconds only works from Android
* 2.3 (API level 9) onwards. For earlier releases, you must use
* one of the {@code SENSOR_DELAY_*} constants.
*
* @param handler
* The {@link android.os.Handler Handler} the
* {@link android.hardware.SensorEvent sensor events} will be
* delivered to.
*
* @return true if the sensor is supported and successfully enabled.
*
* @see #registerListener(SensorEventListener, Sensor, int)
* @see #unregisterListener(SensorEventListener)
* @see #unregisterListener(SensorEventListener, Sensor)
*
* @throws IllegalArgumentException when sensor is null or a trigger sensor
*/
public boolean registerListener(SensorEventListener listener, Sensor sensor, int rate,
Handler handler) {
if (listener == null || sensor == null) {
return false;
}
int delay = -1;
switch (rate) {
case SENSOR_DELAY_FASTEST:
delay = 0;
break;
case SENSOR_DELAY_GAME:
delay = 20000;
break;
case SENSOR_DELAY_UI:
delay = 66667;
break;
case SENSOR_DELAY_NORMAL:
delay = 200000;
break;
default:
delay = rate;
break;
}
return registerListenerImpl(listener, sensor, delay, handler);
}
/** @hide */
protected abstract boolean registerListenerImpl(SensorEventListener listener, Sensor sensor,
int delay, Handler handler);
/**
* <p>
* Computes the inclination matrix <b>I</b> as well as the rotation matrix
* <b>R</b> transforming a vector from the device coordinate system to the
* world's coordinate system which is defined as a direct orthonormal basis,
* where:
* </p>
*
* <ul>
* <li>X is defined as the vector product <b>Y.Z</b> (It is tangential to
* the ground at the device's current location and roughly points East).</li>
* <li>Y is tangential to the ground at the device's current location and
* points towards the magnetic North Pole.</li>
* <li>Z points towards the sky and is perpendicular to the ground.</li>
* </ul>
*
* <p>
* <center><img src="../../../images/axis_globe.png"
* alt="World coordinate-system diagram." border="0" /></center>
* </p>
*
* <p>
* <hr>
* <p>
* By definition:
* <p>
* [0 0 g] = <b>R</b> * <b>gravity</b> (g = magnitude of gravity)
* <p>
* [0 m 0] = <b>I</b> * <b>R</b> * <b>geomagnetic</b> (m = magnitude of
* geomagnetic field)
* <p>
* <b>R</b> is the identity matrix when the device is aligned with the
* world's coordinate system, that is, when the device's X axis points
* toward East, the Y axis points to the North Pole and the device is facing
* the sky.
*
* <p>
* <b>I</b> is a rotation matrix transforming the geomagnetic vector into
* the same coordinate space as gravity (the world's coordinate space).
* <b>I</b> is a simple rotation around the X axis. The inclination angle in
* radians can be computed with {@link #getInclination}.
* <hr>
*
* <p>
* Each matrix is returned either as a 3x3 or 4x4 row-major matrix depending
* on the length of the passed array:
* <p>
* <u>If the array length is 16:</u>
*
* <pre>
* / M[ 0] M[ 1] M[ 2] M[ 3] \
* | M[ 4] M[ 5] M[ 6] M[ 7] |
* | M[ 8] M[ 9] M[10] M[11] |
* \ M[12] M[13] M[14] M[15] /
*</pre>
*
* This matrix is ready to be used by OpenGL ES's
* {@link javax.microedition.khronos.opengles.GL10#glLoadMatrixf(float[], int)
* glLoadMatrixf(float[], int)}.
* <p>
* Note that because OpenGL matrices are column-major matrices you must
* transpose the matrix before using it. However, since the matrix is a
* rotation matrix, its transpose is also its inverse, conveniently, it is
* often the inverse of the rotation that is needed for rendering; it can
* therefore be used with OpenGL ES directly.
* <p>
* Also note that the returned matrices always have this form:
*
* <pre>
* / M[ 0] M[ 1] M[ 2] 0 \
* | M[ 4] M[ 5] M[ 6] 0 |
* | M[ 8] M[ 9] M[10] 0 |
* \ 0 0 0 1 /
*</pre>
*
* <p>
* <u>If the array length is 9:</u>
*
* <pre>
* / M[ 0] M[ 1] M[ 2] \
* | M[ 3] M[ 4] M[ 5] |
* \ M[ 6] M[ 7] M[ 8] /
*</pre>
*
* <hr>
* <p>
* The inverse of each matrix can be computed easily by taking its
* transpose.
*
* <p>
* The matrices returned by this function are meaningful only when the
* device is not free-falling and it is not close to the magnetic north. If
* the device is accelerating, or placed into a strong magnetic field, the
* returned matrices may be inaccurate.
*
* @param R
* is an array of 9 floats holding the rotation matrix <b>R</b> when
* this function returns. R can be null.
* <p>
*
* @param I
* is an array of 9 floats holding the rotation matrix <b>I</b> when
* this function returns. I can be null.
* <p>
*
* @param gravity
* is an array of 3 floats containing the gravity vector expressed in
* the device's coordinate. You can simply use the
* {@link android.hardware.SensorEvent#values values} returned by a
* {@link android.hardware.SensorEvent SensorEvent} of a
* {@link android.hardware.Sensor Sensor} of type
* {@link android.hardware.Sensor#TYPE_ACCELEROMETER
* TYPE_ACCELEROMETER}.
* <p>
*
* @param geomagnetic
* is an array of 3 floats containing the geomagnetic vector
* expressed in the device's coordinate. You can simply use the
* {@link android.hardware.SensorEvent#values values} returned by a
* {@link android.hardware.SensorEvent SensorEvent} of a
* {@link android.hardware.Sensor Sensor} of type
* {@link android.hardware.Sensor#TYPE_MAGNETIC_FIELD
* TYPE_MAGNETIC_FIELD}.
*
* @return <code>true</code> on success, <code>false</code> on failure (for
* instance, if the device is in free fall). On failure the output
* matrices are not modified.
*
* @see #getInclination(float[])
* @see #getOrientation(float[], float[])
* @see #remapCoordinateSystem(float[], int, int, float[])
*/
public static boolean getRotationMatrix(float[] R, float[] I,
float[] gravity, float[] geomagnetic) {
// TODO: move this to native code for efficiency
float Ax = gravity[0];
float Ay = gravity[1];
float Az = gravity[2];
final float Ex = geomagnetic[0];
final float Ey = geomagnetic[1];
final float Ez = geomagnetic[2];
float Hx = Ey*Az - Ez*Ay;
float Hy = Ez*Ax - Ex*Az;
float Hz = Ex*Ay - Ey*Ax;
final float normH = (float)Math.sqrt(Hx*Hx + Hy*Hy + Hz*Hz);
if (normH < 0.1f) {
// device is close to free fall (or in space?), or close to
// magnetic north pole. Typical values are > 100.
return false;
}
final float invH = 1.0f / normH;
Hx *= invH;
Hy *= invH;
Hz *= invH;
final float invA = 1.0f / (float)Math.sqrt(Ax*Ax + Ay*Ay + Az*Az);
Ax *= invA;
Ay *= invA;
Az *= invA;
final float Mx = Ay*Hz - Az*Hy;
final float My = Az*Hx - Ax*Hz;
final float Mz = Ax*Hy - Ay*Hx;
if (R != null) {
if (R.length == 9) {
R[0] = Hx; R[1] = Hy; R[2] = Hz;
R[3] = Mx; R[4] = My; R[5] = Mz;
R[6] = Ax; R[7] = Ay; R[8] = Az;
} else if (R.length == 16) {
R[0] = Hx; R[1] = Hy; R[2] = Hz; R[3] = 0;
R[4] = Mx; R[5] = My; R[6] = Mz; R[7] = 0;
R[8] = Ax; R[9] = Ay; R[10] = Az; R[11] = 0;
R[12] = 0; R[13] = 0; R[14] = 0; R[15] = 1;
}
}
if (I != null) {
// compute the inclination matrix by projecting the geomagnetic
// vector onto the Z (gravity) and X (horizontal component
// of geomagnetic vector) axes.
final float invE = 1.0f / (float)Math.sqrt(Ex*Ex + Ey*Ey + Ez*Ez);
final float c = (Ex*Mx + Ey*My + Ez*Mz) * invE;
final float s = (Ex*Ax + Ey*Ay + Ez*Az) * invE;
if (I.length == 9) {
I[0] = 1; I[1] = 0; I[2] = 0;
I[3] = 0; I[4] = c; I[5] = s;
I[6] = 0; I[7] =-s; I[8] = c;
} else if (I.length == 16) {
I[0] = 1; I[1] = 0; I[2] = 0;
I[4] = 0; I[5] = c; I[6] = s;
I[8] = 0; I[9] =-s; I[10]= c;
I[3] = I[7] = I[11] = I[12] = I[13] = I[14] = 0;
I[15] = 1;
}
}
return true;
}
/**
* Computes the geomagnetic inclination angle in radians from the
* inclination matrix <b>I</b> returned by {@link #getRotationMatrix}.
*
* @param I
* inclination matrix see {@link #getRotationMatrix}.
*
* @return The geomagnetic inclination angle in radians.
*
* @see #getRotationMatrix(float[], float[], float[], float[])
* @see #getOrientation(float[], float[])
* @see GeomagneticField
*
*/
public static float getInclination(float[] I) {
if (I.length == 9) {
return (float)Math.atan2(I[5], I[4]);
} else {
return (float)Math.atan2(I[6], I[5]);
}
}
/**
* <p>
* Rotates the supplied rotation matrix so it is expressed in a different
* coordinate system. This is typically used when an application needs to
* compute the three orientation angles of the device (see
* {@link #getOrientation}) in a different coordinate system.
* </p>
*
* <p>
* When the rotation matrix is used for drawing (for instance with OpenGL
* ES), it usually <b>doesn't need</b> to be transformed by this function,
* unless the screen is physically rotated, in which case you can use
* {@link android.view.Display#getRotation() Display.getRotation()} to
* retrieve the current rotation of the screen. Note that because the user
* is generally free to rotate their screen, you often should consider the
* rotation in deciding the parameters to use here.
* </p>
*
* <p>
* <u>Examples:</u>
* <p>
*
* <ul>
* <li>Using the camera (Y axis along the camera's axis) for an augmented
* reality application where the rotation angles are needed:</li>
*
* <p>
* <ul>
* <code>remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);</code>
* </ul>
* </p>
*
* <li>Using the device as a mechanical compass when rotation is
* {@link android.view.Surface#ROTATION_90 Surface.ROTATION_90}:</li>
*
* <p>
* <ul>
* <code>remapCoordinateSystem(inR, AXIS_Y, AXIS_MINUS_X, outR);</code>
* </ul>
* </p>
*
* Beware of the above example. This call is needed only to account for a
* rotation from its natural orientation when calculating the rotation
* angles (see {@link #getOrientation}). If the rotation matrix is also used
* for rendering, it may not need to be transformed, for instance if your
* {@link android.app.Activity Activity} is running in landscape mode.
* </ul>
*
* <p>
* Since the resulting coordinate system is orthonormal, only two axes need
* to be specified.
*
* @param inR
* the rotation matrix to be transformed. Usually it is the matrix
* returned by {@link #getRotationMatrix}.
*
* @param X
* defines on which world axis and direction the X axis of the device
* is mapped.
*
* @param Y
* defines on which world axis and direction the Y axis of the device
* is mapped.
*
* @param outR
* the transformed rotation matrix. inR and outR can be the same
* array, but it is not recommended for performance reason.
*
* @return <code>true</code> on success. <code>false</code> if the input
* parameters are incorrect, for instance if X and Y define the same
* axis. Or if inR and outR don't have the same length.
*
* @see #getRotationMatrix(float[], float[], float[], float[])
*/
public static boolean remapCoordinateSystem(float[] inR, int X, int Y,
float[] outR)
{
if (inR == outR) {
final float[] temp = mTempMatrix;
synchronized(temp) {
// we don't expect to have a lot of contention
if (remapCoordinateSystemImpl(inR, X, Y, temp)) {
final int size = outR.length;
for (int i=0 ; i<size ; i++)
outR[i] = temp[i];
return true;
}
}
}
return remapCoordinateSystemImpl(inR, X, Y, outR);
}
private static boolean remapCoordinateSystemImpl(float[] inR, int X, int Y,
float[] outR)
{
/*
* X and Y define a rotation matrix 'r':
*
* (X==1)?((X&0x80)?-1:1):0 (X==2)?((X&0x80)?-1:1):0 (X==3)?((X&0x80)?-1:1):0
* (Y==1)?((Y&0x80)?-1:1):0 (Y==2)?((Y&0x80)?-1:1):0 (Y==3)?((X&0x80)?-1:1):0
* r[0] ^ r[1]
*
* where the 3rd line is the vector product of the first 2 lines
*
*/
final int length = outR.length;
if (inR.length != length)
return false; // invalid parameter
if ((X & 0x7C)!=0 || (Y & 0x7C)!=0)
return false; // invalid parameter
if (((X & 0x3)==0) || ((Y & 0x3)==0))
return false; // no axis specified
if ((X & 0x3) == (Y & 0x3))
return false; // same axis specified
// Z is "the other" axis, its sign is either +/- sign(X)*sign(Y)
// this can be calculated by exclusive-or'ing X and Y; except for
// the sign inversion (+/-) which is calculated below.
int Z = X ^ Y;
// extract the axis (remove the sign), offset in the range 0 to 2.
final int x = (X & 0x3)-1;
final int y = (Y & 0x3)-1;
final int z = (Z & 0x3)-1;
// compute the sign of Z (whether it needs to be inverted)
final int axis_y = (z+1)%3;
final int axis_z = (z+2)%3;
if (((x^axis_y)|(y^axis_z)) != 0)
Z ^= 0x80;
final boolean sx = (X>=0x80);
final boolean sy = (Y>=0x80);
final boolean sz = (Z>=0x80);
// Perform R * r, in avoiding actual muls and adds.
final int rowLength = ((length==16)?4:3);
for (int j=0 ; j<3 ; j++) {
final int offset = j*rowLength;
for (int i=0 ; i<3 ; i++) {
if (x==i) outR[offset+i] = sx ? -inR[offset+0] : inR[offset+0];
if (y==i) outR[offset+i] = sy ? -inR[offset+1] : inR[offset+1];
if (z==i) outR[offset+i] = sz ? -inR[offset+2] : inR[offset+2];
}
}
if (length == 16) {
outR[3] = outR[7] = outR[11] = outR[12] = outR[13] = outR[14] = 0;
outR[15] = 1;
}
return true;
}
/**
* Computes the device's orientation based on the rotation matrix.
* <p>
* When it returns, the array values is filled with the result:
* <ul>
* <li>values[0]: <i>azimuth</i>, rotation around the Z axis.</li>
* <li>values[1]: <i>pitch</i>, rotation around the X axis.</li>
* <li>values[2]: <i>roll</i>, rotation around the Y axis.</li>
* </ul>
* <p>The reference coordinate-system used is different from the world
* coordinate-system defined for the rotation matrix:</p>
* <ul>
* <li>X is defined as the vector product <b>Y.Z</b> (It is tangential to
* the ground at the device's current location and roughly points West).</li>
* <li>Y is tangential to the ground at the device's current location and
* points towards the magnetic North Pole.</li>
* <li>Z points towards the center of the Earth and is perpendicular to the ground.</li>
* </ul>
*
* <p>
* <center><img src="../../../images/axis_globe_inverted.png"
* alt="Inverted world coordinate-system diagram." border="0" /></center>
* </p>
* <p>
* All three angles above are in <b>radians</b> and <b>positive</b> in the
* <b>counter-clockwise</b> direction.
*
* @param R
* rotation matrix see {@link #getRotationMatrix}.
*
* @param values
* an array of 3 floats to hold the result.
*
* @return The array values passed as argument.
*
* @see #getRotationMatrix(float[], float[], float[], float[])
* @see GeomagneticField
*/
public static float[] getOrientation(float[] R, float values[]) {
/*
* 4x4 (length=16) case:
* / R[ 0] R[ 1] R[ 2] 0 \
* | R[ 4] R[ 5] R[ 6] 0 |
* | R[ 8] R[ 9] R[10] 0 |
* \ 0 0 0 1 /
*
* 3x3 (length=9) case:
* / R[ 0] R[ 1] R[ 2] \
* | R[ 3] R[ 4] R[ 5] |
* \ R[ 6] R[ 7] R[ 8] /
*
*/
if (R.length == 9) {
values[0] = (float)Math.atan2(R[1], R[4]);
values[1] = (float)Math.asin(-R[7]);
values[2] = (float)Math.atan2(-R[6], R[8]);
} else {
values[0] = (float)Math.atan2(R[1], R[5]);
values[1] = (float)Math.asin(-R[9]);
values[2] = (float)Math.atan2(-R[8], R[10]);
}
return values;
}
/**
* Computes the Altitude in meters from the atmospheric pressure and the
* pressure at sea level.
* <p>
* Typically the atmospheric pressure is read from a
* {@link Sensor#TYPE_PRESSURE} sensor. The pressure at sea level must be
* known, usually it can be retrieved from airport databases in the
* vicinity. If unknown, you can use {@link #PRESSURE_STANDARD_ATMOSPHERE}
* as an approximation, but absolute altitudes won't be accurate.
* </p>
* <p>
* To calculate altitude differences, you must calculate the difference
* between the altitudes at both points. If you don't know the altitude
* as sea level, you can use {@link #PRESSURE_STANDARD_ATMOSPHERE} instead,
* which will give good results considering the range of pressure typically
* involved.
* </p>
* <p>
* <code><ul>
* float altitude_difference =
* getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_at_point2)
* - getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_at_point1);
* </ul></code>
* </p>
*
* @param p0 pressure at sea level
* @param p atmospheric pressure
* @return Altitude in meters
*/
public static float getAltitude(float p0, float p) {
final float coef = 1.0f / 5.255f;
return 44330.0f * (1.0f - (float)Math.pow(p/p0, coef));
}
/** Helper function to compute the angle change between two rotation matrices.
* Given a current rotation matrix (R) and a previous rotation matrix
* (prevR) computes the rotation around the z,x, and y axes which
* transforms prevR to R.
* outputs a 3 element vector containing the z,x, and y angle
* change at indexes 0, 1, and 2 respectively.
* <p> Each input matrix is either as a 3x3 or 4x4 row-major matrix
* depending on the length of the passed array:
* <p>If the array length is 9, then the array elements represent this matrix
* <pre>
* / R[ 0] R[ 1] R[ 2] \
* | R[ 3] R[ 4] R[ 5] |
* \ R[ 6] R[ 7] R[ 8] /
*</pre>
* <p>If the array length is 16, then the array elements represent this matrix
* <pre>
* / R[ 0] R[ 1] R[ 2] R[ 3] \
* | R[ 4] R[ 5] R[ 6] R[ 7] |
* | R[ 8] R[ 9] R[10] R[11] |
* \ R[12] R[13] R[14] R[15] /
*</pre>
* @param R current rotation matrix
* @param prevR previous rotation matrix
* @param angleChange an an array of floats (z, x, and y) in which the angle change is stored
*/
public static void getAngleChange( float[] angleChange, float[] R, float[] prevR) {
float rd1=0,rd4=0, rd6=0,rd7=0, rd8=0;
float ri0=0,ri1=0,ri2=0,ri3=0,ri4=0,ri5=0,ri6=0,ri7=0,ri8=0;
float pri0=0, pri1=0, pri2=0, pri3=0, pri4=0, pri5=0, pri6=0, pri7=0, pri8=0;
if(R.length == 9) {
ri0 = R[0];
ri1 = R[1];
ri2 = R[2];
ri3 = R[3];
ri4 = R[4];
ri5 = R[5];
ri6 = R[6];
ri7 = R[7];
ri8 = R[8];
} else if(R.length == 16) {
ri0 = R[0];
ri1 = R[1];
ri2 = R[2];
ri3 = R[4];
ri4 = R[5];
ri5 = R[6];
ri6 = R[8];
ri7 = R[9];
ri8 = R[10];
}
if(prevR.length == 9) {
pri0 = prevR[0];
pri1 = prevR[1];
pri2 = prevR[2];
pri3 = prevR[3];
pri4 = prevR[4];
pri5 = prevR[5];
pri6 = prevR[6];
pri7 = prevR[7];
pri8 = prevR[8];
} else if(prevR.length == 16) {
pri0 = prevR[0];
pri1 = prevR[1];
pri2 = prevR[2];
pri3 = prevR[4];
pri4 = prevR[5];
pri5 = prevR[6];
pri6 = prevR[8];
pri7 = prevR[9];
pri8 = prevR[10];
}
// calculate the parts of the rotation difference matrix we need
// rd[i][j] = pri[0][i] * ri[0][j] + pri[1][i] * ri[1][j] + pri[2][i] * ri[2][j];
rd1 = pri0 * ri1 + pri3 * ri4 + pri6 * ri7; //rd[0][1]
rd4 = pri1 * ri1 + pri4 * ri4 + pri7 * ri7; //rd[1][1]
rd6 = pri2 * ri0 + pri5 * ri3 + pri8 * ri6; //rd[2][0]
rd7 = pri2 * ri1 + pri5 * ri4 + pri8 * ri7; //rd[2][1]
rd8 = pri2 * ri2 + pri5 * ri5 + pri8 * ri8; //rd[2][2]
angleChange[0] = (float)Math.atan2(rd1, rd4);
angleChange[1] = (float)Math.asin(-rd7);
angleChange[2] = (float)Math.atan2(-rd6, rd8);
}
/** Helper function to convert a rotation vector to a rotation matrix.
* Given a rotation vector (presumably from a ROTATION_VECTOR sensor), returns a
* 9 or 16 element rotation matrix in the array R. R must have length 9 or 16.
* If R.length == 9, the following matrix is returned:
* <pre>
* / R[ 0] R[ 1] R[ 2] \
* | R[ 3] R[ 4] R[ 5] |
* \ R[ 6] R[ 7] R[ 8] /
*</pre>
* If R.length == 16, the following matrix is returned:
* <pre>
* / R[ 0] R[ 1] R[ 2] 0 \
* | R[ 4] R[ 5] R[ 6] 0 |
* | R[ 8] R[ 9] R[10] 0 |
* \ 0 0 0 1 /
*</pre>
* @param rotationVector the rotation vector to convert
* @param R an array of floats in which to store the rotation matrix
*/
public static void getRotationMatrixFromVector(float[] R, float[] rotationVector) {
float q0;
float q1 = rotationVector[0];
float q2 = rotationVector[1];
float q3 = rotationVector[2];
if (rotationVector.length == 4) {
q0 = rotationVector[3];
} else {
q0 = 1 - q1*q1 - q2*q2 - q3*q3;
q0 = (q0 > 0) ? (float)Math.sqrt(q0) : 0;
}
float sq_q1 = 2 * q1 * q1;
float sq_q2 = 2 * q2 * q2;
float sq_q3 = 2 * q3 * q3;
float q1_q2 = 2 * q1 * q2;
float q3_q0 = 2 * q3 * q0;
float q1_q3 = 2 * q1 * q3;
float q2_q0 = 2 * q2 * q0;
float q2_q3 = 2 * q2 * q3;
float q1_q0 = 2 * q1 * q0;
if(R.length == 9) {
R[0] = 1 - sq_q2 - sq_q3;
R[1] = q1_q2 - q3_q0;
R[2] = q1_q3 + q2_q0;
R[3] = q1_q2 + q3_q0;
R[4] = 1 - sq_q1 - sq_q3;
R[5] = q2_q3 - q1_q0;
R[6] = q1_q3 - q2_q0;
R[7] = q2_q3 + q1_q0;
R[8] = 1 - sq_q1 - sq_q2;
} else if (R.length == 16) {
R[0] = 1 - sq_q2 - sq_q3;
R[1] = q1_q2 - q3_q0;
R[2] = q1_q3 + q2_q0;
R[3] = 0.0f;
R[4] = q1_q2 + q3_q0;
R[5] = 1 - sq_q1 - sq_q3;
R[6] = q2_q3 - q1_q0;
R[7] = 0.0f;
R[8] = q1_q3 - q2_q0;
R[9] = q2_q3 + q1_q0;
R[10] = 1 - sq_q1 - sq_q2;
R[11] = 0.0f;
R[12] = R[13] = R[14] = 0.0f;
R[15] = 1.0f;
}
}
/** Helper function to convert a rotation vector to a normalized quaternion.
* Given a rotation vector (presumably from a ROTATION_VECTOR sensor), returns a normalized
* quaternion in the array Q. The quaternion is stored as [w, x, y, z]
* @param rv the rotation vector to convert
* @param Q an array of floats in which to store the computed quaternion
*/
public static void getQuaternionFromVector(float[] Q, float[] rv) {
if (rv.length == 4) {
Q[0] = rv[3];
} else {
Q[0] = 1 - rv[0]*rv[0] - rv[1]*rv[1] - rv[2]*rv[2];
Q[0] = (Q[0] > 0) ? (float)Math.sqrt(Q[0]) : 0;
}
Q[1] = rv[0];
Q[2] = rv[1];
Q[3] = rv[2];
}
/**
* Requests receiving trigger events for a trigger sensor.
*
* <p>
* When the sensor detects a trigger event condition, such as significant motion in
* the case of the {@link Sensor#TYPE_SIGNIFICANT_MOTION}, the provided trigger listener
* will be invoked once and then its request to receive trigger events will be canceled.
* To continue receiving trigger events, the application must request to receive trigger
* events again.
* </p>
*
* @param listener The listener on which the
* {@link TriggerEventListener#onTrigger(TriggerEvent)} will be delivered.
* @param sensor The sensor to be enabled.
*
* @return true if the sensor was successfully enabled.
*
* @throws IllegalArgumentException when sensor is null or not a trigger sensor.
*/
public boolean requestTriggerSensor(TriggerEventListener listener, Sensor sensor) {
return requestTriggerSensorImpl(listener, sensor);
}
/**
* @hide
*/
protected abstract boolean requestTriggerSensorImpl(TriggerEventListener listener,
Sensor sensor);
/**
* Cancels receiving trigger events for a trigger sensor.
*
* <p>
* Note that a Trigger sensor will be auto disabled if
* {@link TriggerEventListener#onTrigger(TriggerEvent)} has triggered.
* This method is provided in case the user wants to explicitly cancel the request
* to receive trigger events.
* </p>
*
* @param listener The listener on which the
* {@link TriggerEventListener#onTrigger(TriggerEvent)}
* is delivered.It should be the same as the one used
* in {@link #requestTriggerSensor(TriggerEventListener, Sensor)}
* @param sensor The sensor for which the trigger request should be canceled.
* If null, it cancels receiving trigger for all sensors associated
* with the listener.
*
* @return true if successfully canceled.
*
* @throws IllegalArgumentException when sensor is a trigger sensor.
*/
public boolean cancelTriggerSensor(TriggerEventListener listener, Sensor sensor) {
return cancelTriggerSensorImpl(listener, sensor, true);
}
/**
* @hide
*/
protected abstract boolean cancelTriggerSensorImpl(TriggerEventListener listener,
Sensor sensor, boolean disable);
private LegacySensorManager getLegacySensorManager() {
synchronized (mSensorListByType) {
if (mLegacySensorManager == null) {
Log.i(TAG, "This application is using deprecated SensorManager API which will "
+ "be removed someday. Please consider switching to the new API.");
mLegacySensorManager = new LegacySensorManager(this);
}
return mLegacySensorManager;
}
}
}
|