-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompassManager.java
140 lines (110 loc) · 3.36 KB
/
CompassManager.java
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
import java.lang.reflect.*;
import java.util.List;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class CompassManager {
private Sensor sensor;
private SensorManager sensorManager;
Method compassEventMethod;
Method directionEventMethod;
private Boolean supported;
private boolean running = false;
Context context;
public CompassManager(Context parent) {
this.context = parent;
try {
compassEventMethod =
parent.getClass().getMethod("compassEvent", new Class[] { Float.TYPE, Float.TYPE, Float.TYPE });
} catch (Exception e) {
// no such method, or an error.. which is fine, just ignore
}
try {
directionEventMethod =
parent.getClass().getMethod("directionEvent", new Class[] { Float.TYPE });
} catch (Exception e) {
// no such method, or an error.. which is fine, just ignore
}
// System.out.println("directionEventMethod is " + directionEventMethod);
resume();
}
public void resume() {
if (isSupported()) {
startListening();
}
}
public void pause() {
if (isListening()) {
stopListening();
}
}
/**
* Returns true if the manager is listening to orientation changes
*/
public boolean isListening() {
return running;
}
/**
* Unregisters listeners
*/
public void stopListening() {
running = false;
try {
if (sensorManager != null && sensorEventListener != null) {
sensorManager.unregisterListener(sensorEventListener);
}
}
catch (Exception e) {
}
}
/**
* Returns true if at least one Accelerometer sensor is available
*/
public boolean isSupported() {
if (supported == null) {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
supported = new Boolean(sensors.size() > 0);
}
return supported;
}
public void startListening() {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors.size() > 0) {
sensor = sensors.get(0);
running = sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME);
}
}
/**
* The listener that listen to events from the accelerometer listener
*/
private SensorEventListener sensorEventListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// ignored for now
}
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (compassEventMethod != null) {
try {
compassEventMethod.invoke(context, new Object[] { x, y, z });
} catch (Exception e) {
e.printStackTrace();
compassEventMethod = null;
}
}
if (directionEventMethod != null) {
try {
directionEventMethod.invoke(context, new Object[] { (float) (-x * Math.PI / 180) });
} catch (Exception e) {
e.printStackTrace();
directionEventMethod = null;
}
}
}
};
}