This repository was archived by the owner on Jul 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathRNProximityModule.java
More file actions
113 lines (92 loc) · 3.52 KB
/
RNProximityModule.java
File metadata and controls
113 lines (92 loc) · 3.52 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
package com.RNProximity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.annotation.Nullable;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.util.HashMap;
import java.util.Map;
public class RNProximityModule extends ReactContextBaseJavaModule implements SensorEventListener {
private static final String TAG = "RNProximityModule";
private static final String KEY_PROXIMITY = "proximity";
private static final String KEY_DISTANCE = "distance";
private static final String KEY_EVENT_ON_SENSOR_CHANGE = "EVENT_ON_SENSOR_CHANGE";
private static final String EVENT_ON_SENSOR_CHANGE = "onSensorChanged";
private final ReactApplicationContext reactContext;
private PowerManager.WakeLock wakeLock;
private SensorManager mSensorManager;
private Sensor mProximity;
public RNProximityModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
mSensorManager = (SensorManager) reactContext.getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
public void sendEvent(String eventName, @Nullable WritableMap params) {
if (this.reactContext.hasActiveCatalystInstance()) {
this.reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
} else {
Log.i(TAG, "Waiting for CatalystInstance");
}
}
private PowerManager.WakeLock getWakeLock(){
Context context = this.reactContext.getApplicationContext();
PowerManager powerManager = context.getSystemService(PowerManager.class);
PowerManager.WakeLock wl;
if (powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
return powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "WAKE_LOCK:" + TAG);
}
return null;
}
@ReactMethod
public void addListener() {
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}
@ReactMethod
public void removeListener() {
mSensorManager.unregisterListener(this);
}
@ReactMethod
public void proximityEnabled(boolean enable) {
if(this.wakeLock != null){
if(enable){
this.wakeLock.acquire();
} else
this.wakeLock.release();
}
}
@Override
public String getName() {
return "RNProximity";
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put(KEY_EVENT_ON_SENSOR_CHANGE, EVENT_ON_SENSOR_CHANGE);
return constants;
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
WritableMap params = Arguments.createMap();
double distance = sensorEvent.values[0];
double maximumRange = mProximity.getMaximumRange();
boolean isNearDevice = distance < maximumRange;
params.putBoolean(KEY_PROXIMITY, isNearDevice);
params.putDouble(KEY_DISTANCE, distance);
sendEvent(EVENT_ON_SENSOR_CHANGE, params);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}