-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSensorManager.java
More file actions
2081 lines (1922 loc) · 75.7 KB
/
SensorManager.java
File metadata and controls
2081 lines (1922 loc) · 75.7 KB
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
/*
* 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.Looper;
import android.os.Process;
import android.os.RemoteException;
import android.os.Handler;
import android.os.Message;
import android.os.ServiceManager;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.view.IRotationWatcher;
import android.view.IWindowManager;
import android.view.Surface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
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>
*
* <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 class SensorManager
{
private static final String TAG = "SensorManager";
private static final float[] mTempMatrix = new float[16];
/* 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;
/*-----------------------------------------------------------------------*/
Looper mMainLooper;
@SuppressWarnings("deprecation")
private HashMap<SensorListener, LegacyListener> mLegacyListenersMap =
new HashMap<SensorListener, LegacyListener>();
/*-----------------------------------------------------------------------*/
private static final int SENSOR_DISABLE = -1;
private static boolean sSensorModuleInitialized = false;
private static ArrayList<Sensor> sFullSensorsList = new ArrayList<Sensor>();
private static SparseArray<List<Sensor>> sSensorListByType = new SparseArray<List<Sensor>>();
private static IWindowManager sWindowManager;
private static int sRotation = Surface.ROTATION_0;
/* The thread and the sensor list are global to the process
* but the actual thread is spawned on demand */
private static SensorThread sSensorThread;
private static int sQueue;
// Used within this module from outside SensorManager, don't make private
static SparseArray<Sensor> sHandleToSensor = new SparseArray<Sensor>();
static final ArrayList<ListenerDelegate> sListeners =
new ArrayList<ListenerDelegate>();
/*-----------------------------------------------------------------------*/
private class SensorEventPool {
private final int mPoolSize;
private final SensorEvent mPool[];
private int mNumItemsInPool;
private SensorEvent createSensorEvent() {
// maximal size for all legacy events is 3
return new SensorEvent(3);
}
SensorEventPool(int poolSize) {
mPoolSize = poolSize;
mNumItemsInPool = poolSize;
mPool = new SensorEvent[poolSize];
}
SensorEvent getFromPool() {
SensorEvent t = null;
synchronized (this) {
if (mNumItemsInPool > 0) {
// remove the "top" item from the pool
final int index = mPoolSize - mNumItemsInPool;
t = mPool[index];
mPool[index] = null;
mNumItemsInPool--;
}
}
if (t == null) {
// the pool was empty or this item was removed from the pool for
// the first time. In any case, we need to create a new item.
t = createSensorEvent();
}
return t;
}
void returnToPool(SensorEvent t) {
synchronized (this) {
// is there space left in the pool?
if (mNumItemsInPool < mPoolSize) {
// if so, return the item to the pool
mNumItemsInPool++;
final int index = mPoolSize - mNumItemsInPool;
mPool[index] = t;
}
}
}
}
private static SensorEventPool sPool;
/*-----------------------------------------------------------------------*/
static private class SensorThread {
Thread mThread;
boolean mSensorsReady;
SensorThread() {
}
@Override
protected void finalize() {
}
// must be called with sListeners lock
boolean startLocked() {
try {
if (mThread == null) {
mSensorsReady = false;
SensorThreadRunnable runnable = new SensorThreadRunnable();
Thread thread = new Thread(runnable, SensorThread.class.getName());
thread.start();
synchronized (runnable) {
while (mSensorsReady == false) {
runnable.wait();
}
}
mThread = thread;
}
} catch (InterruptedException e) {
}
return mThread == null ? false : true;
}
private class SensorThreadRunnable implements Runnable {
SensorThreadRunnable() {
}
private boolean open() {
// NOTE: this cannot synchronize on sListeners, since
// it's held in the main thread at least until we
// return from here.
sQueue = sensors_create_queue();
return true;
}
public void run() {
//Log.d(TAG, "entering main sensor thread");
final float[] values = new float[3];
final int[] status = new int[1];
final long timestamp[] = new long[1];
Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
if (!open()) {
return;
}
synchronized (this) {
// we've open the driver, we're ready to open the sensors
mSensorsReady = true;
this.notify();
}
while (true) {
// wait for an event
final int sensor = sensors_data_poll(sQueue, values, status, timestamp);
int accuracy = status[0];
synchronized (sListeners) {
if (sensor == -1 || sListeners.isEmpty()) {
// we lost the connection to the event stream. this happens
// when the last listener is removed or if there is an error
if (sensor == -1 && !sListeners.isEmpty()) {
// log a warning in case of abnormal termination
Log.e(TAG, "_sensors_data_poll() failed, we bail out: sensors=" + sensor);
}
// we have no more listeners or polling failed, terminate the thread
sensors_destroy_queue(sQueue);
sQueue = 0;
mThread = null;
break;
}
final Sensor sensorObject = sHandleToSensor.get(sensor);
if (sensorObject != null) {
// report the sensor event to all listeners that
// care about it.
final int size = sListeners.size();
for (int i=0 ; i<size ; i++) {
ListenerDelegate listener = sListeners.get(i);
if (listener.hasSensor(sensorObject)) {
// this is asynchronous (okay to call
// with sListeners lock held).
listener.onSensorChangedLocked(sensorObject,
values, timestamp, accuracy);
}
}
}
}
}
//Log.d(TAG, "exiting main sensor thread");
}
}
}
/*-----------------------------------------------------------------------*/
private class ListenerDelegate {
private final SensorEventListener mSensorEventListener;
private final ArrayList<Sensor> mSensorList = new ArrayList<Sensor>();
private final Handler mHandler;
public SparseBooleanArray mSensors = new SparseBooleanArray();
public SparseBooleanArray mFirstEvent = new SparseBooleanArray();
public SparseIntArray mSensorAccuracies = new SparseIntArray();
ListenerDelegate(SensorEventListener listener, Sensor sensor, Handler handler) {
mSensorEventListener = listener;
Looper looper = (handler != null) ? handler.getLooper() : mMainLooper;
// currently we create one Handler instance per listener, but we could
// have one per looper (we'd need to pass the ListenerDelegate
// instance to handleMessage and keep track of them separately).
mHandler = new Handler(looper) {
@Override
public void handleMessage(Message msg) {
final SensorEvent t = (SensorEvent)msg.obj;
final int handle = t.sensor.getHandle();
switch (t.sensor.getType()) {
// Only report accuracy for sensors that support it.
case Sensor.TYPE_MAGNETIC_FIELD:
case Sensor.TYPE_ORIENTATION:
// call onAccuracyChanged() only if the value changes
final int accuracy = mSensorAccuracies.get(handle);
if ((t.accuracy >= 0) && (accuracy != t.accuracy)) {
mSensorAccuracies.put(handle, t.accuracy);
mSensorEventListener.onAccuracyChanged(t.sensor, t.accuracy);
}
break;
default:
// For other sensors, just report the accuracy once
if (mFirstEvent.get(handle) == false) {
mFirstEvent.put(handle, true);
mSensorEventListener.onAccuracyChanged(
t.sensor, SENSOR_STATUS_ACCURACY_HIGH);
}
break;
}
mSensorEventListener.onSensorChanged(t);
sPool.returnToPool(t);
}
};
addSensor(sensor);
}
Object getListener() {
return mSensorEventListener;
}
void addSensor(Sensor sensor) {
mSensors.put(sensor.getHandle(), true);
mSensorList.add(sensor);
}
int removeSensor(Sensor sensor) {
mSensors.delete(sensor.getHandle());
mSensorList.remove(sensor);
return mSensors.size();
}
boolean hasSensor(Sensor sensor) {
return mSensors.get(sensor.getHandle());
}
List<Sensor> getSensors() {
return mSensorList;
}
void onSensorChangedLocked(Sensor sensor, float[] values, long[] timestamp, int accuracy) {
SensorEvent t = sPool.getFromPool();
final float[] v = t.values;
v[0] = values[0];
v[1] = values[1];
v[2] = values[2];
t.timestamp = timestamp[0];
t.accuracy = accuracy;
t.sensor = sensor;
Message msg = Message.obtain();
msg.what = 0;
msg.obj = t;
mHandler.sendMessage(msg);
}
}
/**
* {@hide}
*/
public SensorManager(Looper mainLooper) {
mMainLooper = mainLooper;
synchronized(sListeners) {
if (!sSensorModuleInitialized) {
sSensorModuleInitialized = true;
nativeClassInit();
sWindowManager = IWindowManager.Stub.asInterface(
ServiceManager.getService("window"));
if (sWindowManager != null) {
// if it's null we're running in the system process
// which won't get the rotated values
try {
sRotation = sWindowManager.watchRotation(
new IRotationWatcher.Stub() {
public void onRotationChanged(int rotation) {
SensorManager.this.onRotationChanged(rotation);
}
}
);
} catch (RemoteException e) {
}
}
// initialize the sensor list
sensors_module_init();
final ArrayList<Sensor> fullList = sFullSensorsList;
int i = 0;
do {
Sensor sensor = new Sensor();
i = sensors_module_get_next_sensor(sensor, i);
if (i>=0) {
//Log.d(TAG, "found sensor: " + sensor.getName() +
// ", handle=" + sensor.getHandle());
sensor.setLegacyType(getLegacySensorType(sensor.getType()));
fullList.add(sensor);
sHandleToSensor.append(sensor.getHandle(), sensor);
}
} while (i>0);
sPool = new SensorEventPool( sFullSensorsList.size()*2 );
sSensorThread = new SensorThread();
}
}
}
private int getLegacySensorType(int type) {
switch (type) {
case Sensor.TYPE_ACCELEROMETER:
return SENSOR_ACCELEROMETER;
case Sensor.TYPE_MAGNETIC_FIELD:
return SENSOR_MAGNETIC_FIELD;
case Sensor.TYPE_ORIENTATION:
return SENSOR_ORIENTATION_RAW;
case Sensor.TYPE_TEMPERATURE:
return SENSOR_TEMPERATURE;
case Sensor.TYPE_LIGHT: //Howard
return SENSOR_LIGHT;
}
return 0;
}
/**
* @return available sensors.
* @deprecated This method is deprecated, use
* {@link SensorManager#getSensorList(int)} instead
*/
@Deprecated
public int getSensors() {
int result = 0;
final ArrayList<Sensor> fullList = sFullSensorsList;
for (Sensor i : fullList) {
switch (i.getType()) {
case Sensor.TYPE_ACCELEROMETER:
result |= SensorManager.SENSOR_ACCELEROMETER;
break;
case Sensor.TYPE_MAGNETIC_FIELD:
result |= SensorManager.SENSOR_MAGNETIC_FIELD;
break;
case Sensor.TYPE_ORIENTATION:
result |= SensorManager.SENSOR_ORIENTATION |
SensorManager.SENSOR_ORIENTATION_RAW;
break;
case Sensor.TYPE_LIGHT: //Howard
result |= SensorManager.SENSOR_LIGHT;
break;
}
}
return result;
}
/**
* 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 ArrayList<Sensor> fullList = sFullSensorsList;
synchronized(fullList) {
list = sSensorListByType.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);
sSensorListByType.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) {
if (listener == null) {
return false;
}
boolean result = false;
result = registerLegacyListener(SENSOR_ACCELEROMETER, Sensor.TYPE_ACCELEROMETER,
listener, sensors, rate) || result;
result = registerLegacyListener(SENSOR_MAGNETIC_FIELD, Sensor.TYPE_MAGNETIC_FIELD,
listener, sensors, rate) || result;
result = registerLegacyListener(SENSOR_ORIENTATION_RAW, Sensor.TYPE_ORIENTATION,
listener, sensors, rate) || result;
result = registerLegacyListener(SENSOR_ORIENTATION, Sensor.TYPE_ORIENTATION,
listener, sensors, rate) || result;
result = registerLegacyListener(SENSOR_TEMPERATURE, Sensor.TYPE_TEMPERATURE,
listener, sensors, rate) || result;
result = registerLegacyListener(SENSOR_LIGHT, Sensor.TYPE_LIGHT, //Howard
listener, sensors, rate) || result;
return result;
}
@SuppressWarnings("deprecation")
private boolean registerLegacyListener(int legacyType, int type,
SensorListener listener, int sensors, int rate)
{
if (listener == null) {
return false;
}
boolean result = false;
// Are we activating this legacy sensor?
if ((sensors & legacyType) != 0) {
// if so, find a suitable Sensor
Sensor sensor = getDefaultSensor(type);
if (sensor != null) {
// If we don't already have one, create a LegacyListener
// to wrap this listener and process the events as
// they are expected by legacy apps.
LegacyListener legacyListener = null;
synchronized (mLegacyListenersMap) {
legacyListener = mLegacyListenersMap.get(listener);
if (legacyListener == null) {
// we didn't find a LegacyListener for this client,
// create one, and put it in our list.
legacyListener = new LegacyListener(listener);
mLegacyListenersMap.put(listener, legacyListener);
}
}
// register this legacy sensor with this legacy listener
legacyListener.registerSensor(legacyType);
// and finally, register the legacy listener with the new apis
result = registerListener(legacyListener, sensor, rate);
}
}
return result;
}
/**
* 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) {
unregisterLegacyListener(SENSOR_ACCELEROMETER, Sensor.TYPE_ACCELEROMETER,
listener, sensors);
unregisterLegacyListener(SENSOR_MAGNETIC_FIELD, Sensor.TYPE_MAGNETIC_FIELD,
listener, sensors);
unregisterLegacyListener(SENSOR_ORIENTATION_RAW, Sensor.TYPE_ORIENTATION,
listener, sensors);
unregisterLegacyListener(SENSOR_ORIENTATION, Sensor.TYPE_ORIENTATION,
listener, sensors);
unregisterLegacyListener(SENSOR_TEMPERATURE, Sensor.TYPE_TEMPERATURE,
listener, sensors);
unregisterLegacyListener(SENSOR_LIGHT, Sensor.TYPE_LIGHT, //Howard
listener, sensors);
}
@SuppressWarnings("deprecation")
private void unregisterLegacyListener(int legacyType, int type,
SensorListener listener, int sensors)
{
if (listener == null) {
return;
}
// do we know about this listener?
LegacyListener legacyListener = null;
synchronized (mLegacyListenersMap) {
legacyListener = mLegacyListenersMap.get(listener);
}
if (legacyListener != null) {
// Are we deactivating this legacy sensor?
if ((sensors & legacyType) != 0) {
// if so, find the corresponding Sensor
Sensor sensor = getDefaultSensor(type);
if (sensor != null) {
// unregister this legacy sensor and if we don't
// need the corresponding Sensor, unregister it too
if (legacyListener.unregisterSensor(legacyType)) {
// corresponding sensor not needed, unregister
unregisterListener(legacyListener, sensor);
// finally check if we still need the legacyListener
// in our mapping, if not, get rid of it too.
synchronized(sListeners) {
boolean found = false;
for (ListenerDelegate i : sListeners) {
if (i.getListener() == legacyListener) {
found = true;
break;
}
}
if (!found) {
synchronized (mLegacyListenersMap) {
mLegacyListenersMap.remove(listener);
}
}
}
}
}
}
}
}
/**
* 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.
*
* @param listener
* a SensorEventListener object
*
* @param sensor
* the sensor to unregister from
*
* @see #unregisterListener(SensorEventListener)
* @see #registerListener(SensorEventListener, Sensor, int)
*
*/
public void unregisterListener(SensorEventListener listener, Sensor sensor) {
unregisterListener((Object)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) {
unregisterListener((Object)listener);
}
/**
* Registers a {@link android.hardware.SensorEventListener
* SensorEventListener} for the given sensor.