-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathBackgroundTimerModule.java
More file actions
107 lines (88 loc) · 3.25 KB
/
BackgroundTimerModule.java
File metadata and controls
107 lines (88 loc) · 3.25 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
package com.ocetnik.timer;
import android.os.Handler;
import android.os.PowerManager;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.lang.Runnable;
public class BackgroundTimerModule extends ReactContextBaseJavaModule {
private Handler handler;
private ReactContext reactContext;
private Runnable runnable;
private PowerManager powerManager;
private PowerManager.WakeLock wakeLock;
private final LifecycleEventListener listener = new LifecycleEventListener(){
@Override
public void onHostResume() {}
@Override
public void onHostPause() {}
@Override
public void onHostDestroy() {
if (wakeLock.isHeld()) wakeLock.release();
}
};
public BackgroundTimerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
this.powerManager = (PowerManager) getReactApplicationContext().getSystemService(reactContext.POWER_SERVICE);
this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "rohit_bg_wakelock");
reactContext.addLifecycleEventListener(listener);
}
@Override
public String getName() {
return "RNBackgroundTimer";
}
@ReactMethod
public void start(final int delay) {
if (!wakeLock.isHeld()) wakeLock.acquire();
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
sendEvent(reactContext, "backgroundTimer");
}
};
handler.post(runnable);
}
@ReactMethod
public void stop() {
if (wakeLock.isHeld()) wakeLock.release();
// avoid null pointer exceptio when stop is called without start
if (handler != null) handler.removeCallbacks(runnable);
}
private void sendEvent(ReactContext reactContext, String eventName) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, null);
}
@ReactMethod
public void setTimeout(final int id, final double timeout) {
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
if (getReactApplicationContext().hasActiveCatalystInstance()) {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("backgroundTimer.timeout", id);
}
}
}, (long) timeout);
}
@ReactMethod
public void addListener(String eventName) {
// Keep: Required for RN built in Event Emitter Calls.
}
@ReactMethod
public void removeListeners(Integer count) {
// Keep: Required for RN built in Event Emitter Calls.
}
/*@ReactMethod
public void clearTimeout(final int id) {
// todo one day..
// not really neccessary to have
}*/
}