forked from williambout/react-native-proximity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNProximityModule.java
More file actions
56 lines (45 loc) · 1.6 KB
/
RNProximityModule.java
File metadata and controls
56 lines (45 loc) · 1.6 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
package com.RNProximity;
import android.os.PowerManager;
import android.util.Log;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import static android.content.Context.POWER_SERVICE;
public class RNProximityModule extends ReactContextBaseJavaModule {
private static final String TAG = "RNProximityModule";
private final ReactApplicationContext reactContext;
private final PowerManager.WakeLock wakeLock;
public RNProximityModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
PowerManager powerManager = (PowerManager) reactContext.getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG + ":wakelocktag");
}
public void sendEvent(String eventName, WritableMap params) {
if (this.reactContext.hasActiveCatalystInstance()) {
this.reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
} else {
Log.i(TAG, "Waiting for CatalystInstance");
}
}
@ReactMethod
public void addListener() {
if(!wakeLock.isHeld()) {
wakeLock.acquire(10*60*1000L /*10 minutes*/);
}
}
@ReactMethod
public void removeListener() {
if(wakeLock.isHeld()) {
wakeLock.release();
}
}
@Override
public String getName() {
return "RNProximity";
}
}