-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (70 loc) · 1.83 KB
/
index.js
File metadata and controls
74 lines (70 loc) · 1.83 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
import {
NativeEventEmitter,
NativeModules,
DeviceEventEmitter,
Platform,
AppState
} from 'react-native';
const isIOS = Platform.OS == 'ios';
const ScreenShotDetect = NativeModules.RNScreenShotDetect;
const screenshotDetectEmitter = new NativeEventEmitter(ScreenShotDetect);
let subscription = null;
let isListening = false;
let emmitTimer = null;
function addIOSEventListener(handler) {
// 开始监听,初始化
removeAllListener();
ScreenShotDetect.startListener();
subscription = screenshotDetectEmitter.addListener('ScreenShotDetected', (result) => {
result.timeStamp = result.timeStamp * 1000;
handler(result);
})
return subscription;
}
function removeAllListener() {
if (subscription) {
try {
subscription.remove();
subscription = null;
} catch (error) {
}
}
}
function startListen() {
if (!isIOS && !isListening) {
isListening = true;
ScreenShotDetect.startListen();
}
}
function stopListen() {
if (!isIOS && isListening) {
isListening = false;
ScreenShotDetect.stopListen();
}
}
function clearTimer() {
if (emmitTimer) clearTimeout(emmitTimer);
}
function addAndroidEventListener(handler) {
removeAllListener();
startListen();
subscription = DeviceEventEmitter.addListener('ScreenShotDetected', (result) => {
clearTimer()
emmitTimer = setTimeout(() => {
handler(result);
}, 1000);
});
return subscription;
}
export default {
addEventListener: function (handler) {
if (isIOS) {
return addIOSEventListener(handler);
} else {
return addAndroidEventListener(handler)
}
},
startListener: startListen,
stopListener: stopListen,
clearListener: removeAllListener,
}