-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathLocationService.js
259 lines (225 loc) · 8.32 KB
/
LocationService.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import BackgroundGeolocation from '@mauron85/react-native-background-geolocation';
import { NativeModules, Platform } from 'react-native';
import PushNotification from 'react-native-push-notification';
import { CROSSED_PATHS } from '../constants/storage';
import { GetStoreData } from '../helpers/General';
import languages from '../locales/languages';
import { Colors } from '../styles';
let isBackgroundGeolocationConfigured = false;
const LOCATION_DISABLED_NOTIFICATION_ID = '55';
// Time (in milliseconds) between location information polls
// 5 minutes
export const MIN_LOCATION_UPDATE_MS = 300000;
//Device Location Statuses
/**
* Location services are disabled for the device
*/
export const DEVICE_LOCATION_OFF = 'DEVICE_LOCATION_OFF';
/**
* Location services are disabled for this app
*/
export const APP_NOT_AUTHORIZED = 'APP_NOT_AUTHORIZED';
/**
* User has granted location tracking permissions
* to the app, and device location services are running
*/
export const ALL_CONDITIONS_MET = 'ALL_CONDITIONS_MET';
/*
On Android: isAppGpsEnabled = 0 means Never Use Location
On IOS: isAppGpsEnabled = 0 means Never Use Location. isAppGpsEnabled = 99 means Ask Next Time
*/
export const NEVER_USE_LOCATION_STATUS = 0;
export const ASK_NEXT_TIME_STATUS = 99;
export default class LocationServices {
static async start() {
// handles edge cases around Android where start might get called again even though
// the service is already created. Make sure the listeners are still bound and exit
if (isBackgroundGeolocationConfigured) {
BackgroundGeolocation.start();
return;
}
BackgroundGeolocation.configure({
maxLocations: 0,
desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
stationaryRadius: 5,
distanceFilter: 5,
notificationTitle: languages.t('label.location_enabled_title'),
notificationText: languages.t('label.location_enabled_message'),
enableAutoStartDialogueTitle: languages.t('home.gps.auto_start_header'),
enableAutoStartDialogueText: languages.t('home.gps.auto_start_subheader'),
enableLocationDialogueTitle: languages.t('home.gps.tracing_off_header'),
enableLocationDialogueText: languages.t('home.gps.tracing_off_subheader'),
debug: false,
startOnBoot: true,
stopOnTerminate: false,
locationProvider: Platform.select({
ios: BackgroundGeolocation.CONTINUOUS_RAW_PROVIDER,
android: BackgroundGeolocation.DISTANCE_FILTER_PROVIDER,
default: BackgroundGeolocation.DISTANCE_FILTER_PROVIDER,
}),
interval: MIN_LOCATION_UPDATE_MS,
fastestInterval: MIN_LOCATION_UPDATE_MS,
activitiesInterval: MIN_LOCATION_UPDATE_MS,
activityType: 'AutomotiveNavigation',
pauseLocationUpdates: false,
saveBatteryOnBackground: false,
stopOnStillActivity: false,
});
BackgroundGeolocation.on('error', (error) => {
console.log('[ERROR] BackgroundGeolocation error:', error);
});
BackgroundGeolocation.on('start', () => {
console.log('[INFO] BackgroundGeolocation service has been started');
});
BackgroundGeolocation.on('authorization', (status) => {
console.log(
'[INFO] BackgroundGeolocation authorization status: ' + status,
);
if (status === BackgroundGeolocation.AUTHORIZED) {
// TODO: this should not restart if user opted out
BackgroundGeolocation.start(); // force running, if not already running
BackgroundGeolocation.checkStatus(({ locationServicesEnabled }) => {
if (!locationServicesEnabled) {
PushNotification.localNotification({
id: LOCATION_DISABLED_NOTIFICATION_ID,
title: languages.t('label.location_disabled_title'),
message: languages.t('label.location_disabled_message'),
smallIcon: 'ic_notificationicon',
color: Colors.primaryBlue,
});
} else {
PushNotification.cancelLocalNotifications({
id: LOCATION_DISABLED_NOTIFICATION_ID,
});
}
});
}
});
BackgroundGeolocation.on('background', () => {
console.log('[INFO] App is in background');
});
BackgroundGeolocation.on('foreground', () => {
console.log('[INFO] App is in foreground');
});
BackgroundGeolocation.on('abort_requested', () => {
console.log('[INFO] Server responded with 285 Updates Not Required');
// Here we can decide whether we want stop the updates or not.
// If you've configured the server to return 285, then it means the server does not require further update.
// So the normal thing to do here would be to `BackgroundGeolocation.stop()`.
// But you might be counting on it to receive location updates in the UI, so you could just reconfigure and set `url` to null.
});
BackgroundGeolocation.on('http_authorization', () => {
console.log('[INFO] App needs to authorize the http requests');
});
BackgroundGeolocation.on('stop', () => {
PushNotification.localNotification({
title: languages.t('label.location_disabled_title'),
message: languages.t('label.location_disabled_message'),
smallIcon: 'ic_notificationicon',
color: Colors.primaryBlue,
});
console.log('[INFO] stop');
});
BackgroundGeolocation.on('stationary', () => {
console.log('[INFO] stationary');
});
const {
authorization,
isRunning,
locationServicesEnabled,
} = await this.getBackgroundGeoStatus();
console.log('[INFO] BackgroundGeolocation service is running', isRunning);
console.log(
'[INFO] BackgroundGeolocation services enabled',
locationServicesEnabled,
);
console.log('[INFO] BackgroundGeolocation auth status: ' + authorization);
BackgroundGeolocation.start(); //triggers start on start event
isBackgroundGeolocationConfigured = true;
}
static async stop() {
PushNotification.localNotification({
title: languages.t('label.location_disabled_title'),
message: languages.t('label.location_disabled_message'),
smallIcon: 'ic_notificationicon',
color: Colors.primaryBlue,
});
BackgroundGeolocation.removeAllListeners();
BackgroundGeolocation.stop();
isBackgroundGeolocationConfigured = false;
}
static async getHasPotentialExposure() {
const dayBin = await GetStoreData(CROSSED_PATHS, false);
return !!dayBin && dayBin.some((exposure) => exposure > 0);
}
static async getBackgroundGeoStatus() {
return new Promise((resolve, reject) => {
BackgroundGeolocation.checkStatus(
(status) => resolve(status),
(e) => reject(e),
);
});
}
static async checkStatus() {
const hasPotentialExposure = await this.getHasPotentialExposure();
const {
authorization: isAppGpsEnabled,
isRunning,
locationServicesEnabled: isDeviceGpsEnabled,
} = await this.getBackgroundGeoStatus();
if (!isDeviceGpsEnabled) {
return {
canTrack: false,
reason: DEVICE_LOCATION_OFF,
hasPotentialExposure,
isRunning,
};
}
if (
isAppGpsEnabled === NEVER_USE_LOCATION_STATUS ||
isAppGpsEnabled === ASK_NEXT_TIME_STATUS
) {
return {
canTrack: false,
reason: APP_NOT_AUTHORIZED,
hasPotentialExposure,
isRunning,
};
}
return {
canTrack: true,
reason: ALL_CONDITIONS_MET,
hasPotentialExposure,
isRunning,
};
}
/**
* Like checkStatus, but it also tries to start/stop the service as
* appropriate.
*
* - If the user has opted out or permissions are not available, stop.
* - If the user has opted in and perissions are available, start.
*/
static async checkStatusAndStartOrStop() {
const status = await this.checkStatus();
const { canTrack, isRunning } = status;
if (canTrack && !isRunning) {
this.start();
}
if (!canTrack && isRunning) {
this.stop();
}
return status;
}
static async getLocationData() {
return NativeModules.SecureStorageManager.getLocations();
}
/**
* Returns the most recent point of location data for a user.
* This is the last item in the location data array.
*/
static async getMostRecentUserGps() {
const locData = await LocationServices.getLocationData();
return locData[locData.length - 1];
}
}