/
DeviceMotionModule.java
297 lines (253 loc) · 9.68 KB
/
DeviceMotionModule.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
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
package expo.modules.sensors.modules;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener2;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Choreographer;
import android.view.Surface;
import android.view.WindowManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.Math;
import org.unimodules.core.ExportedModule;
import org.unimodules.core.ModuleRegistry;
import org.unimodules.core.Promise;
import org.unimodules.core.interfaces.ExpoMethod;
import org.unimodules.core.interfaces.services.EventEmitter;
import org.unimodules.core.interfaces.services.UIManager;
import org.unimodules.interfaces.sensors.SensorService;
import org.unimodules.interfaces.sensors.SensorServiceSubscription;
import org.unimodules.interfaces.sensors.services.AccelerometerService;
import org.unimodules.interfaces.sensors.services.GravitySensorService;
import org.unimodules.interfaces.sensors.services.GyroscopeService;
import org.unimodules.interfaces.sensors.services.LinearAccelerationSensorService;
import org.unimodules.interfaces.sensors.services.RotationVectorSensorService;
public class DeviceMotionModule extends ExportedModule implements SensorEventListener2 {
private long mLastUpdate = 0;
private float mUpdateInterval = 1.0f / 60.0f;
private float[] mRotationMatrix = new float[9];
private float[] mRotationResult = new float[3];
private SensorEvent mAccelerationEvent;
private SensorEvent mAccelerationIncludingGravityEvent;
private SensorEvent mRotationEvent;
private SensorEvent mRotationRateEvent;
private SensorEvent mGravityEvent;
private List<SensorServiceSubscription> mServiceSubscriptions = null;
private UIManager mUiManager = null;
private ModuleRegistry mModuleRegistry = null;
public DeviceMotionModule(Context context) {
super(context);
}
@Override
public String getName() {
return "ExponentDeviceMotion";
}
@Override
public Map<String, Object> getConstants() {
return Collections.unmodifiableMap(new HashMap<String, Object>() {
{
// Gravity on the planet this module supports (currently just Earth) represented as m/s^2.
put("Gravity", 9.80665);
}
});
}
@ExpoMethod
public void setUpdateInterval(int updateInterval, Promise promise) {
mUpdateInterval = updateInterval;
promise.resolve(null);
}
@ExpoMethod
public void startObserving(Promise promise) {
if (mServiceSubscriptions == null) {
mServiceSubscriptions = new ArrayList<>();
for (SensorService kernelService : getSensorKernelServices()) {
SensorServiceSubscription subscription = kernelService.createSubscriptionForListener(this);
// We want handle update interval on our own,
// because we need to coordinate updates from multiple sensor services.
subscription.setUpdateInterval(0);
mServiceSubscriptions.add(subscription);
}
}
for (SensorServiceSubscription subscription : mServiceSubscriptions) {
subscription.start();
}
promise.resolve(null);
}
@ExpoMethod
public void stopObserving(final Promise promise) {
mUiManager.runOnUiQueueThread(new Runnable() {
@Override
public void run() {
for (SensorServiceSubscription subscription : mServiceSubscriptions) {
subscription.stop();
}
mCurrentFrameCallback.stop();
promise.resolve(null);
}
});
}
@ExpoMethod
public void isAvailableAsync(Promise promise) {
SensorManager mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
ArrayList<Integer> sensorTypes = new ArrayList<>(Arrays.asList(Sensor.TYPE_GYROSCOPE, Sensor.TYPE_ACCELEROMETER, Sensor.TYPE_LINEAR_ACCELERATION, Sensor.TYPE_ROTATION_VECTOR, Sensor.TYPE_GRAVITY));
for (Integer type : sensorTypes) {
if (mSensorManager.getDefaultSensor(type) == null) {
promise.resolve(false);
return;
}
}
promise.resolve(true);
}
@Override
public void onCreate(ModuleRegistry moduleRegistry) {
mEventEmitter = moduleRegistry.getModule(EventEmitter.class);
mUiManager = moduleRegistry.getModule(UIManager.class);
mModuleRegistry = moduleRegistry;
}
private List<SensorService> getSensorKernelServices() {
return Arrays.asList(
mModuleRegistry.getModule(GyroscopeService.class),
mModuleRegistry.getModule(LinearAccelerationSensorService.class),
mModuleRegistry.getModule(AccelerometerService.class),
mModuleRegistry.getModule(RotationVectorSensorService.class),
mModuleRegistry.getModule(GravitySensorService.class)
);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor sensor = sensorEvent.sensor;
if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
mRotationRateEvent = sensorEvent;
} else if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mAccelerationIncludingGravityEvent = sensorEvent;
} else if (sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
mAccelerationEvent = sensorEvent;
} else if (sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
mRotationEvent = sensorEvent;
} else if (sensor.getType() == Sensor.TYPE_GRAVITY) {
mGravityEvent = sensorEvent;
}
mCurrentFrameCallback.maybePostFromNonUI();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// do nothing
}
@Override
public void onFlushCompleted(Sensor sensor) {
// do nothing
}
private ScheduleDispatchFrameCallback mCurrentFrameCallback = new ScheduleDispatchFrameCallback();
private DispatchEventRunnable mDispatchEventRunnable = new DispatchEventRunnable();
private EventEmitter mEventEmitter;
private class ScheduleDispatchFrameCallback implements Choreographer.FrameCallback {
private volatile boolean mIsPosted = false;
private boolean mShouldStop = false;
public void doFrame(long frameTimeNanos) {
if (mShouldStop) {
mIsPosted = false;
} else {
post();
}
long curTime = System.currentTimeMillis();
if ((curTime - mLastUpdate) > mUpdateInterval) {
mUiManager.runOnClientCodeQueueThread(mDispatchEventRunnable);
mLastUpdate = curTime;
}
}
public void stop() {
mShouldStop = true;
}
public void maybePost() {
if (!mIsPosted) {
mIsPosted = true;
post();
}
}
private void post() {
Choreographer.getInstance().postFrameCallback(mCurrentFrameCallback);
}
public void maybePostFromNonUI() {
if (mIsPosted) {
return;
}
mUiManager.runOnUiQueueThread(new Runnable() {
@Override
public void run() {
maybePost();
}
});
}
}
private class DispatchEventRunnable implements Runnable {
@Override
public void run() {
mEventEmitter.emit("deviceMotionDidUpdate", eventsToMap());
}
}
private Bundle eventsToMap() {
Bundle map = new Bundle();
Bundle acceleration = new Bundle();
Bundle accelerationIncludingGravity = new Bundle();
Bundle rotation = new Bundle();
Bundle rotationRate = new Bundle();
double interval = 0;
if (mAccelerationEvent != null) {
acceleration.putDouble("x", mAccelerationEvent.values[0]);
acceleration.putDouble("y", mAccelerationEvent.values[1]);
acceleration.putDouble("z", mAccelerationEvent.values[2]);
map.putBundle("acceleration", acceleration);
interval = mAccelerationEvent.timestamp;
}
if (mAccelerationIncludingGravityEvent != null && mGravityEvent != null) {
accelerationIncludingGravity.putDouble("x", mAccelerationIncludingGravityEvent.values[0] - 2 * mGravityEvent.values[0]);
accelerationIncludingGravity.putDouble("y", mAccelerationIncludingGravityEvent.values[1] - 2 * mGravityEvent.values[1]);
accelerationIncludingGravity.putDouble("z", mAccelerationIncludingGravityEvent.values[2] - 2 * mGravityEvent.values[2]);
map.putBundle("accelerationIncludingGravity", accelerationIncludingGravity);
interval = mAccelerationIncludingGravityEvent.timestamp;
}
if (mRotationRateEvent != null) {
rotationRate.putDouble("alpha", Math.toDegrees(mRotationRateEvent.values[0]));
rotationRate.putDouble("beta", Math.toDegrees(mRotationRateEvent.values[1]));
rotationRate.putDouble("gamma", Math.toDegrees(mRotationRateEvent.values[2]));
map.putBundle("rotationRate", rotationRate);
interval = mRotationRateEvent.timestamp;
}
if (mRotationEvent != null) {
SensorManager.getRotationMatrixFromVector(mRotationMatrix, mRotationEvent.values);
SensorManager.getOrientation(mRotationMatrix, mRotationResult);
rotation.putDouble("alpha", -mRotationResult[0]);
rotation.putDouble("beta", -mRotationResult[1]);
rotation.putDouble("gamma", mRotationResult[2]);
map.putBundle("rotation", rotation);
interval = mRotationEvent.timestamp;
}
map.putDouble("interval", interval);
map.putInt("orientation", getOrientation());
return map;
}
private int getOrientation() {
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
switch (windowManager.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return -90;
default:
// pass to global return
}
}
return 0;
}
}