@@ -81,6 +81,88 @@ public static float[] accelerometerReading() {
81
81
}
82
82
}
83
83
84
+ static SensorEvent lastOrientationEvent = null ;
85
+
86
+ static class OrientationListener implements SensorEventListener {
87
+ public void onSensorChanged (SensorEvent ev ) {
88
+ lastOrientationEvent = ev ;
89
+ }
90
+
91
+ public void onAccuracyChanged (Sensor sensor , int accuracy ) {
92
+ }
93
+ }
94
+
95
+ static OrientationListener orientationListener = new OrientationListener ();
96
+
97
+ /**
98
+ * Enable or Disable the OrientationSensor.
99
+ */
100
+ public static void orientationSensorEnable (boolean enable ) {
101
+ SensorManager sm = (SensorManager ) context .getSystemService (Context .SENSOR_SERVICE );
102
+ Sensor orientationSensor = sm .getDefaultSensor (Sensor .TYPE_ORIENTATION );
103
+
104
+ if (orientationSensor == null ) {
105
+ return ;
106
+ }
107
+
108
+ if (enable ) {
109
+ sm .registerListener (orientationListener , orientationSensor , SensorManager .SENSOR_DELAY_NORMAL );
110
+ } else {
111
+ sm .unregisterListener (orientationListener , orientationSensor );
112
+ }
113
+ }
114
+
115
+ public static float [] orientationSensorReading () {
116
+ if (lastOrientationEvent != null ) {
117
+ return lastOrientationEvent .values ;
118
+ } else {
119
+ float rv [] = { 0f , 0f , 0f };
120
+ return rv ;
121
+ }
122
+ }
123
+
124
+ static SensorEvent lastMagneticFieldEvent = null ;
125
+
126
+ static class MagneticFieldListener implements SensorEventListener {
127
+ public void onSensorChanged (SensorEvent ev ) {
128
+ lastMagneticFieldEvent = ev ;
129
+ }
130
+
131
+ public void onAccuracyChanged (Sensor sensor , int accuracy ) {
132
+ }
133
+ }
134
+
135
+ static MagneticFieldListener magneticFieldListener = new MagneticFieldListener ();
136
+
137
+ /**
138
+ * Enable or Disable the MagneticFieldSensor.
139
+ */
140
+ public static void magneticFieldSensorEnable (boolean enable ) {
141
+ SensorManager sm = (SensorManager ) context .getSystemService (Context .SENSOR_SERVICE );
142
+ Sensor magneticFieldSensor = sm .getDefaultSensor (Sensor .TYPE_MAGNETIC_FIELD );
143
+
144
+ if (magneticFieldSensor == null ) {
145
+ return ;
146
+ }
147
+
148
+ if (enable ) {
149
+ sm .registerListener (magneticFieldListener , magneticFieldSensor , SensorManager .SENSOR_DELAY_NORMAL );
150
+ } else {
151
+ sm .unregisterListener (magneticFieldListener , magneticFieldSensor );
152
+ }
153
+ }
154
+
155
+
156
+ public static float [] magneticFieldSensorReading () {
157
+ if (lastMagneticFieldEvent != null ) {
158
+ return lastMagneticFieldEvent .values ;
159
+ } else {
160
+ float rv [] = { 0f , 0f , 0f };
161
+ return rv ;
162
+ }
163
+ }
164
+
165
+
84
166
static DisplayMetrics metrics = new DisplayMetrics ();
85
167
86
168
/**
0 commit comments