-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
623 lines (599 loc) · 19.4 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
'use strict'
import {
Platform,
AppRegistry
} from "react-native"
import NativeModule from './NativeModule';
const TAG = "BackgroundGeolocation";
const LOG_LEVEL_OFF = 0;
const LOG_LEVEL_ERROR = 1;
const LOG_LEVEL_WARNING = 2;
const LOG_LEVEL_INFO = 3;
const LOG_LEVEL_DEBUG = 4;
const LOG_LEVEL_VERBOSE = 5;
const DESIRED_ACCURACY_NAVIGATION = -2;
const DESIRED_ACCURACY_HIGH = -1;
const DESIRED_ACCURACY_MEDIUM = 10;
const DESIRED_ACCURACY_LOW = 100;
const DESIRED_ACCURACY_VERY_LOW = 1000;
const DESIRED_ACCURACY_LOWEST = 3000;
const AUTHORIZATION_STATUS_NOT_DETERMINED = 0;
const AUTHORIZATION_STATUS_RESTRICTED = 1;
const AUTHORIZATION_STATUS_DENIED = 2;
const AUTHORIZATION_STATUS_ALWAYS = 3;
const AUTHORIZATION_STATUS_WHEN_IN_USE = 4;
const NOTIFICATION_PRIORITY_DEFAULT = 0;
const NOTIFICATION_PRIORITY_HIGH = 1;
const NOTIFICATION_PRIORITY_LOW =-1;
const NOTIFICATION_PRIORITY_MAX = 2;
const NOTIFICATION_PRIORITY_MIN =-2;
const emptyFn = function() {}
class BackgroundGeolocation {
static get LOG_LEVEL_OFF() { return LOG_LEVEL_OFF; }
static get LOG_LEVEL_ERROR() { return LOG_LEVEL_ERROR; }
static get LOG_LEVEL_WARNING() { return LOG_LEVEL_WARNING; }
static get LOG_LEVEL_INFO() { return LOG_LEVEL_INFO; }
static get LOG_LEVEL_DEBUG() { return LOG_LEVEL_DEBUG; }
static get LOG_LEVEL_VERBOSE() { return LOG_LEVEL_VERBOSE; }
static get DESIRED_ACCURACY_NAVIGATION() { return DESIRED_ACCURACY_NAVIGATION; }
static get DESIRED_ACCURACY_HIGH() { return DESIRED_ACCURACY_HIGH; }
static get DESIRED_ACCURACY_MEDIUM() { return DESIRED_ACCURACY_MEDIUM; }
static get DESIRED_ACCURACY_LOW() { return DESIRED_ACCURACY_LOW; }
static get DESIRED_ACCURACY_VERY_LOW() { return DESIRED_ACCURACY_VERY_LOW; }
static get DESIRED_ACCURACY_LOWEST() { return DESIRED_ACCURACY_LOWEST; }
static get AUTHORIZATION_STATUS_NOT_DETERMINED() { return AUTHORIZATION_STATUS_NOT_DETERMINED; }
static get AUTHORIZATION_STATUS_RESTRICTED() { return AUTHORIZATION_STATUS_RESTRICTED; }
static get AUTHORIZATION_STATUS_DENIED() { return AUTHORIZATION_STATUS_DENIED; }
static get AUTHORIZATION_STATUS_ALWAYS() { return AUTHORIZATION_STATUS_ALWAYS; }
static get AUTHORIZATION_STATUS_WHEN_IN_USE() { return AUTHORIZATION_STATUS_WHEN_IN_USE; }
static get NOTIFICATION_PRIORITY_DEFAULT() { return NOTIFICATION_PRIORITY_DEFAULT; }
static get NOTIFICATION_PRIORITY_HIGH() { return NOTIFICATION_PRIORITY_HIGH; }
static get NOTIFICATION_PRIORITY_LOW() { return NOTIFICATION_PRIORITY_LOW; }
static get NOTIFICATION_PRIORITY_MAX() { return NOTIFICATION_PRIORITY_MAX; }
static get NOTIFICATION_PRIORITY_MIN() { return NOTIFICATION_PRIORITY_MIN; }
/**
* Register HeadlessTask
*/
static registerHeadlessTask(task) {
AppRegistry.registerHeadlessTask(TAG, () => task);
}
/**
* Core Plugin Control Methods
*/
static ready(config, success, failure) {
if (arguments.length <= 1) {
return NativeModule.ready(config||{});
} else {
NativeModule.ready(config).then(success).catch(failure);
}
}
/**
* Reset plugin confg to default
*/
static reset(config, success, failure) {
if ((typeof(config) == 'function') || (typeof(success) === 'function')) {
if (typeof(config) === 'function') {
success = config;
}
NativeModule.reset(config).then(success).catch(failure);
} else {
return NativeModule.reset(config);
}
}
/**
* Perform initial configuration of plugin. Reset config to default before applying supplied configuration
*/
static configure(config, success, failure) {
if (arguments.length == 1) {
return NativeModule.configure(config);
} else {
NativeModule.configure(config).then(success).catch(failure);
}
}
/**
* Listen to a plugin event
*/
static addListener(event, success, failure) {
if (typeof(event) != 'string') { throw "BackgroundGeolocation#on must be provided a {String} event as 1st argument." }
if (NativeModule.EVENTS.indexOf(event) < 0) { throw "BackgroundGeolocation#on - Unknown event '" + event + "'" }
if (typeof(success) != 'function') { throw "BackgroundGeolocation#on must be provided a callback as 2nd argument. If you're attempting to use the Promise API to listen to an event, it won't work, since a Promise can only evaluate once, while the callback function must be executed for each event." }
NativeModule.addListener.apply(NativeModule, arguments);
}
// @alias #removeListener
static on(event, success, failure) {
this.addListener.apply(this, arguments);
}
/**
* Remove a single plugin event-listener, supplying a reference to the handler initially supplied to #un
*/
static removeListener(event, handler, success, failure) {
if (typeof(event) != 'string') { throw "BackgroundGeolocation#un must be provided a {String} event as 1st argument" }
if (NativeModule.EVENTS.indexOf(event) < 0) { throw "BackgroundGeolocation#un - Unknown event '" + event + "'" }
NativeModule.removeListener.apply(NativeModule, arguments);
}
// @alias #removeListener
static un(event, handler, success, failiure) {
this.removeListener.apply(this, arguments);
}
/**
* Remove all event listeners
*/
static removeListeners(success, failure) {
if (!arguments.length) {
return NativeModule.removeListeners();
} else {
NativeModule.removeListeners().then(success).catch(failure);
}
}
// @alias -> #removeListeners
static removeAllListeners(success, failure) {
this.removeListeners.apply(this, arguments);
}
/**
* Fetch current plugin configuration
*/
static getState(success, failure) {
if (!arguments.length) {
return NativeModule.getState();
} else {
NativeModule.getState().then(success).catch(failure);
}
}
/**
* Start the plugin
*/
static start(success, failure) {
if (!arguments.length) {
return NativeModule.start();
} else {
NativeModule.start().then(success).catch(failure);
}
}
/**
* Stop the plugin
*/
static stop(success, failure) {
if (!arguments.length) {
return NativeModule.stop();
} else {
NativeModule.stop().then(success).catch(failure);
}
}
/**
* Start the scheduler
*/
static startSchedule(success, failure) {
if (!arguments.length) {
return NativeModule.startSchedule();
} else {
NativeModule.startSchedule().then(success).catch(failure);
}
}
/**
* Stop the scheduler
*/
static stopSchedule(success, failure) {
if (!arguments.length) {
return NativeModule.stopSchedule();
} else {
NativeModule.stopSchedule().then(success).catch(failure);
}
}
/**
* Initiate geofences-only mode
*/
static startGeofences(success, failure) {
if (!arguments.length) {
return NativeModule.startGeofences();
} else {
NativeModule.startGeofences().then(success).catch(failure);
}
}
/**
* Start an iOS background-task, provding 180s of background running time
*/
static startBackgroundTask(success, failure) {
if (!arguments.length) {
return NativeModule.startBackgroundTask();
} else {
if (typeof(success) !== 'function') {
throw TAG + "#startBackgroundTask must be provided with a callback to recieve the taskId";
}
NativeModule.startBackgroundTask().then(success).catch(failure);
}
}
/**
* Signal to iOS that your background-task from #startBackgroundTask is complete
*/
static finish(taskId, success, failure) {
// No taskId? Ignore it.
if (arguments.length == 1) {
return NativeModule.finish(taskId);
} else {
NativeModule.finish(taskId).then(success).catch(failure);
}
}
/**
* Toggle motion-state between stationary <-> moving
*/
static changePace(isMoving, success, failure) {
if (arguments.length == 1) {
return NativeModule.changePace(isMoving);
} else {
NativeModule.changePace(isMoving).then(success).catch(failure);
}
}
/**
* Provide new configuration to the plugin. This configuration will be *merged* to current configuration
*/
static setConfig(config, success, failure) {
if (arguments.length == 1) {
return NativeModule.setConfig(config);
} else {
NativeModule.setConfig(config).then(success).catch(failure);
}
}
/**
* HTTP & Persistence
*
*/
static getLocations(success, failure) {
if (!arguments.length) {
return NativeModule.getLocations();
} else {
NativeModule.getLocations().then(success).catch(failure);
}
}
/**
* Fetch the current count of location records in database
*/
static getCount(success, failure) {
if (!arguments.length) {
return NativeModule.getCount();
} else {
NativeModule.getCount().then(success).catch(failure);
}
}
/**
* Destroy all records in locations database
*/
static destroyLocations(success, failure) {
if (!arguments.length) {
return NativeModule.destroyLocations();
} else {
NativeModule.destroyLocations().then(success).catch(failure);
}
}
// @deprecated
static clearDatabase(success, failure) {
return this.destroyLocations.apply(this, arguments);
}
/**
* Insert a single record into locations database
*/
static insertLocation(location, success, failure) {
if (arguments.length == 1) {
return NativeModule.insertLocation(location);
} else {
NativeModule.insertLocation(location).then(success).catch(failure);
}
}
/**
* Manually initiate an HTTP sync operation
*/
static sync(success, failure) {
if (!arguments.length) {
return NativeModule.sync();
} else {
NativeModule.sync().then(success).catch(failure);
}
}
/**
* Fetch the current value of odometer
*/
static getOdometer(success, failure) {
if (!arguments.length) {
return NativeModule.getOdometer();
} else {
NativeModule.getOdometer().then(success).catch(failure);
}
}
/**
* Set the value of the odometer
*/
static setOdometer(value, success, failure) {
if (arguments.length == 1) {
return NativeModule.setOdometer(value);
} else {
NativeModule.setOdometer(value).then(success).catch(failure);
}
}
/**
* Reset the value of odometer to 0
*/
static resetOdometer(success, failure) {
if (!arguments.length) {
return NativeModule.setOdometer(0);
} else {
NativeModule.setOdometer(0).then(success).catch(failure);
}
}
/**
* Geofencing Methods
*/
/**
* Add a single geofence
*/
static addGeofence(config, success, failure) {
if (arguments.length == 1) {
return NativeModule.addGeofence(config);
} else {
NativeModule.addGeofence(config).then(success).catch(failure);
}
}
/**
* Remove a single geofence by identifier
*/
static removeGeofence(identifier, success, failure) {
if (arguments.length == 1) {
return NativeModule.removeGeofence(identifier);
} else {
NativeModule.removeGeofence(identifier).then(success).catch(failure);
}
}
/**
* Add a list of geofences
*/
static addGeofences(geofences, success, failure) {
if (arguments.length == 1) {
return NativeModule.addGeofences(geofences);
} else {
NativeModule.addGeofences(geofences).then(success).catch(failure);
}
}
/**
* Remove geofences. You may either supply an array of identifiers or nothing to destroy all geofences.
* 1. removeGeofences() <-- Promise
* 2. removeGeofences(['foo']) <-- Promise
*
* 3. removeGeofences(success, [failure])
* 4. removeGeofences(['foo'], success, [failure])
*/
static removeGeofences(success, failure) {
if (!arguments.length) {
return NativeModule.removeGeofences();
} else {
NativeModule.removeGeofences().then(success).catch(failure);
}
}
/**
* Fetch all geofences from database
*/
static getGeofences(success, failure) {
if (!arguments.length) {
return NativeModule.getGeofences();
} else {
NativeModule.getGeofences().then(success).catch(failure);
}
}
/**
* Fetch the current position from location-services
*/
static getCurrentPosition(success, failure, options) {
if (typeof(success) == 'function') {
if (typeof(failure) == 'object') {
options = failure;
failure = emptyFn;
}
NativeModule.getCurrentPosition(options).then(success).catch(failure);
} else {
return NativeModule.getCurrentPosition.apply(NativeModule, arguments);
}
}
/**
* Begin watching a stream of locations
*/
static watchPosition(success, failure, options) {
if (typeof(success) == 'object') {
throw TAG + '#watchPosition cannot use Promises since a Promise can only evaluate once while the supplied callback must be executed for each location';
}
if (typeof(failure) == 'object') {
options = failure;
failure = emptyFn;
}
NativeModule.watchPosition(success, failure, options||{});
}
/**
* Stop watching location
*/
static stopWatchPosition(success, failure) {
if (!arguments.length) {
return NativeModule.stopWatchPosition();
} else {
NativeModule.stopWatchPosition().then(success).catch(failure);
}
}
/**
* Set the logLevel. This is just a helper method for setConfig({logLevel: level})
*/
static setLogLevel(value, success, failure) {
if (arguments.length == 1) {
return NativeModule.setLogLevel(value);
} else {
NativeModule.setLogLevel(value).then(success).catch(failure);
}
}
/**
* Fetch the entire contents of log database returned as a String
*/
static getLog(success, failure) {
if (!arguments.length) {
return NativeModule.getLog();
} else {
NativeModule.getLog().then(success).catch(failure);
}
}
/**
* Destroy all contents of log database
*/
static destroyLog(success, failure) {
if (!arguments.length) {
return NativeModule.destroyLog();
} else {
NativeModule.destroyLog().then(success).catch(failure);
}
}
/**
* Open deafult email client on device to email the contents of log database attached as a compressed file attachement
*/
static emailLog(email, success, failure) {
if (typeof(email) != 'string') { throw TAG + "#emailLog requires an email address as 1st argument"}
if (arguments.length == 1) {
return NativeModule.emailLog(email);
} else {
NativeModule.emailLog(email).then(success).catch(failure);
}
}
/**
* Has device OS initiated power-saving mode?
*/
static isPowerSaveMode(success, failure) {
if (!arguments.length) {
return NativeModule.isPowerSaveMode();
} else {
NativeModule.isPowerSaveMode().then(success).catch(failure);
}
}
/**
* Fetch the state of this device's available motion-sensors
*/
static getSensors(success, failure) {
if (!arguments.length) {
return NativeModule.getSensors();
} else {
NativeModule.getSensors().then(success).catch(failure);
}
}
/**
* Play a system sound via the plugin's Sound API
*/
static playSound(soundId, success, failure) {
if (arguments.length == 1) {
return NativeModule.playSound(soundId);
} else {
NativeModule.playSound(soundId).then(success).catch(failure);
}
}
/**
* Insert a log message into the plugin's log database
*/
static get logger() { return NativeModule.logger; }
/**
* Iterate and execute API methods to test validity of method signature for both Standard and Promise API
*/
static test(delay) {
test(this, delay);
}
}
export default BackgroundGeolocation;
/**
* Iterate and execute API methods to test validity of method signature for both Standard and Promise API
*/
var test = function(bgGeo, delay) {
delay = delay || 250;
var methods = [
['reset', {debug: true, logLevel: 5}],
['setConfig', {distanceFilter: 50}],
['setLogLevel', 5],
['getLog', null],
['emailLog', 'foo@bar.com'],
['on', 'location'],
['ready', {}],
['configure', {debug: true, logLevel: 5, schedule: ['1-7 00:00-23:59']}],
['getState', null],
['startSchedule', null],
['stopSchedule', null],
['startGeofences', null],
['stop', null],
['start', null],
['startBackgroundTask', null],
['finish', 0],
['changePace', true],
['getLocations', null],
['insertLocation', {}],
['sync', null],
['getOdometer', null],
['setOdometer', 0],
['resetOdometer'],
['addGeofence', {identifier: 'test-geofence-1', radius: 100, latitude: 0, longitude:0, notifyOnEntry:true}],
['addGeofences', [{identifier: 'test-geofence-2', radius: 100, latitude: 0, longitude:0, notifyOnEntry:true}, {identifier: 'test-geofence-3', radius: 100, latitude: 0, longitude:0, notifyOnEntry:true}]],
['getGeofences', null],
['removeGeofence', 'test-geofence-1'],
['removeGeofences', null],
['getCurrentPosition', {}],
['watchPosition', {}],
['stopWatchPosition', null],
['isPowerSaveMode', null],
['getSensors', null],
['playSound', 1509],
['destroyLocations', null],
['clearDatabase', null],
['destroyLog', null],
['removeListeners', null]
];
var createCallback = function(type, method, params) {
return function(result) {
console.log('- ' + method + '(' + params + ') - ' + type + ': ', result);
}
}
var executeMethod = function(record) {
console.log('* Execute method: ', record)
var method = '' + record[0];
var params = record[1];
var success = createCallback('success', method, params);
var failure = createCallback('failure', method, params);
// Execute Standard API
try {
console.log('- Standard API: ' + method);
if (params == null) {
bgGeo[method](success, failure);
} else {
// Adjust params for different signatures.
switch (method) {
case 'watchPosition':
case 'getCurrentPosition':
bgGeo[method](success, failure, params);
break;
default:
bgGeo[method](params, success, failure);
break;
}
}
} catch (e) {
console.warn(e);
}
// Execute Promise API
setTimeout(function() {
console.log('- Promise API: ' + method);
try {
if (params == null) {
bgGeo[method]().then(success).catch(failure);
} else {
bgGeo[method](params).then(success).catch(failure);
}
} catch (e) {
console.warn(e);
}
}, 10);
}
// Begin fetching methods.
var intervalId = setInterval(function() {
var record = methods.shift();
if (!record || !methods.length) {
clearInterval(intervalId);
console.log('*** TEST COMPLETE ***');
return;
}
executeMethod(record);
}, delay);
}