-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.js
123 lines (101 loc) · 3.2 KB
/
index.js
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
//@flow
'use strict'
type ListenerFunc = (status: number) => void
type GPSStateType = {
NOT_DETERMINED: number,
RESTRICTED: number,
DENIED: number,
AUTHORIZED: number,
AUTHORIZED_ALWAYS: number,
AUTHORIZED_WHENINUSE: number,
openAppDetails: () => void,
openLocationSettings: () => void,
isMarshmallowOrAbove: () => boolean,
isAuthorized: () => boolean,
isDenied: () => boolean,
isNotDetermined: () => boolean,
addListener: (callback: ListenerFunc) => void,
removeListener: () => void,
getStatus: () => Promise<number>,
requestAuthorization: (authType: number) => void,
}
const { NativeModules, NativeEventEmitter, Platform } = require('react-native')
const GPSStateNative = NativeModules.GPSState
const isDroid = Platform.OS == 'android'
const isIOS = Platform.OS == 'ios'
const gpsStateEmitter = new NativeEventEmitter(GPSStateNative)
var _subscription
var _listener: ListenerFunc | null = null
var _isListening: boolean = true
var _currentStatus: number = GPSStateNative.NOT_DETERMINED
var _isMarshmallowOrAbove: boolean = false
var _isopend = false
var _isClosed = false
GPSStateNative.getStatus().then((status: number) => _currentStatus = status)
if (isDroid) {
GPSStateNative.isMarshmallowOrAbove().then((isM: boolean) => _isMarshmallowOrAbove = isM)
}
_subscription = gpsStateEmitter.addListener('OnStatusChange', (response) => {
var status: number
if (isIOS) {
status = response
} else {
status = response.status
}
_currentStatus = status
if (_listener && status && _isListening) {
_listener(status)
}
})
const GPSState: GPSStateType = {
NOT_DETERMINED: GPSStateNative.NOT_DETERMINED,
RESTRICTED: GPSStateNative.RESTRICTED,
DENIED: GPSStateNative.DENIED,
AUTHORIZED: GPSStateNative.AUTHORIZED,
AUTHORIZED_ALWAYS: GPSStateNative.AUTHORIZED_ALWAYS,
AUTHORIZED_WHENINUSE: GPSStateNative.AUTHORIZED_WHENINUSE,
openAppDetails: () => {
GPSStateNative.openSettings(true)
},
openLocationSettings: () => {
GPSStateNative.openSettings(false)
},
isMarshmallowOrAbove: () => {
return _isMarshmallowOrAbove
},
isAuthorized: () => (isPermissionEquals(GPSStateNative.AUTHORIZED_WHENINUSE) || isPermissionEquals(GPSStateNative.AUTHORIZED_ALWAYS)),
isDenied: () => isPermissionEquals(GPSStateNative.DENIED),
isRestricted: () => isPermissionEquals(GPSStateNative.RESTRICTED),
isNotDetermined: () => isPermissionEquals(GPSStateNative.NOT_DETERMINED),
addListener: (callback: ListenerFunc) => {
if (typeof callback == 'function') {
_isListening = true
_listener = callback
GPSStateNative.startListen()
}
},
removeListener: () => {
_isListening = false
_listener = null
GPSStateNative.stopListen()
},
getStatus: () => GPSStateNative.getStatus(),
requestAuthorization: (authType) => {
if (isIOS) {
var type = parseInt(authType)
var min = GPSStateNative.STATUS_NOT_DETERMINED
var max = GPSStateNative.STATUS_AUTHORIZED_WHENINUSE
var inRange = (type >= min && type <= max)
if (isNaN(type) || !inRange) {
type = GPSStateNative.AUTHORIZED_WHENINUSE
}
GPSStateNative.requestAuthorization(type)
} else {
GPSStateNative.requestAuthorization()
}
},
}
function isPermissionEquals(expectedPerm) {
return _currentStatus == expectedPerm;
}
module.exports = GPSState