-
Notifications
You must be signed in to change notification settings - Fork 373
/
LocationManager.js
560 lines (499 loc) · 20.4 KB
/
LocationManager.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
var exec = require('cordova/exec');
var _ = require('com.unarin.cordova.beacon.underscorejs');
var Q = require('com.unarin.cordova.beacon.Q');
var Regions = require('com.unarin.cordova.beacon.Regions');
var Delegate = require('com.unarin.cordova.beacon.Delegate');
var Region = require('com.unarin.cordova.beacon.Region');
var CircularRegion = require('com.unarin.cordova.beacon.CircularRegion');
var BeaconRegion = require('com.unarin.cordova.beacon.BeaconRegion');
/**
* Creates an instance of the plugin.
*
* Important note: Creating multiple instances is expected to break the delegate
* callback mechanism, as the native layer can only handle one callback ID at a
* time.
*
* @constructor {LocationManager}
*/
function LocationManager (){
this.delegate = new Delegate();
this._registerDelegateCallbackId();
this.bindMethodContexts();
}
/**
* Binds the contexts of instance methods to the actual {LocationManager}
* instance.
* The goal of this is to make the caller code clean of binding calls when
* the promise functions are chained for example.
*
* @returns {undefined}
*/
LocationManager.prototype.bindMethodContexts = function() {
this.disableDebugLogs = _.bind(this.disableDebugLogs, this);
this.enableDebugLogs = _.bind(this.enableDebugLogs, this);
};
LocationManager.prototype.getDelegate = function() {
return this.delegate;
};
LocationManager.prototype.setDelegate = function(newDelegate) {
if (!(newDelegate instanceof Delegate)) {
console.error('newDelegate parameter has to be an instance of Delegate.');
return;
}
this.delegate = newDelegate;
this.onDomDelegateReady();
return this.delegate;
};
/**
* Calls the method 'registerDelegateCallbackId' in the native layer which
* saves the callback ID for later use.
*
* The saved callback ID will be used when the native layer wants to notify
* the DOM asynchronously about an event of it's own, for example entering
* into a region.
*
* The same callback will be used for success and fail handling since the
* handling is the same.
*
* @returns {Q.Promise}
*/
LocationManager.prototype._registerDelegateCallbackId = function () {
this.appendToDeviceLog('registerDelegateCallbackId()');
var d = Q.defer();
exec(_.bind(this._onDelegateCallback, this, d), _.bind(this._onDelegateCallback, this, d), "LocationManager",
"registerDelegateCallbackId", []);
return d.promise;
};
/**
* Handles asynchronous calls from the native layer. In this context async
* means that message is not a response to a request of the DOM.
*
* @param {type} deferred {promise, resolve, reject} object.
*
* @param {type} pluginResult The PluginResult object constructed by the
* native layer as the payload of the message it wishes to send to the DOM
* asynchronously.
*
* @returns {undefined}
*/
LocationManager.prototype._onDelegateCallback = function (deferred, pluginResult) {
this.appendToDeviceLog('_onDelegateCallback() ' + JSON.stringify(pluginResult));
if (pluginResult && _.isString(pluginResult['eventType'])) { // The native layer calling the DOM with a delegate event.
this._mapDelegateCallback(pluginResult);
} else if (Q.isPending(deferred.promise)) { // The callback ID registration finished, runs only once.
deferred.resolve();
} else { // The native layer calls back the delegate without specifying an event, coding error.
console.error('Delegate registration promise is already been resolved, all subsequent callbacks should provide an "eventType" field.');
}
};
/**
* Routes async messages arriving from the native layer to the appropriate
* delegate methods.
*
* @param {type} pluginResult The PluginResult object constructed by the
* native layer as the payload of the message it wishes to send to the DOM
*
* @returns {undefined}
*/
LocationManager.prototype._mapDelegateCallback = function (pluginResult) {
var eventType = pluginResult['eventType']; // the Objective-C selector's name
this.appendToDeviceLog('_mapDelegateCallback() found eventType ' + eventType);
if (_.isFunction(this.delegate[eventType])) {
this.delegate[eventType](pluginResult);
} else {
console.error('Delegate unable to handle eventType: ' + eventType);
}
};
/**
* Goes through the provided pre-processors *in order* adn applies them to
* [pluginResult].
* When the pre-processing is done, [resolve] is called with the pre-
* processed results. The raw input is discarded.
*
* @param {Function} resolve A callback which will get called upon completion.
*
* @param {Array} pluginResult The PluginResult object constructed by the
* native layer as the payload of the message it wishes to send to the DOM.
* This function expects the [pluginResult] to be an array of elements.
*
* @param {Array} preProcessors An array of {Function}s which will be applied
* to [pluginResult], in order.
*
* @returns {undefined}
*/
LocationManager.prototype._preProcessorExecutor = function (resolve, pluginResult, preProcessors) {
_.each(preProcessors, function (preProcessor) {
pluginResult = preProcessor(pluginResult);
});
resolve(pluginResult);
};
/**
* Wraps a Cordova exec call into a promise, allowing the client code to
* operate with those promises instead of callbacks.
*
* @param {String} method The name of the method in the native layer to be
* called by Cordova.
*
* @param {Array} commandArgs An array of arguments to be passed for the
* native layer. Defaults to an empty array if omitted.
*
* @param {Array} preProcessors An array of callback functions all of which
* takes an iterable (array) as it's parameter and applies a certain
* operation to the elements of that iterable.
*
* @returns {Q.Promise}
*/
LocationManager.prototype._promisedExec = function (method, commandArgs, preProcessors) {
var self = this;
commandArgs = _.isArray(commandArgs) ? commandArgs : [];
preProcessors = _.isArray(preProcessors) ? preProcessors : [];
preProcessors = _.filter(preProcessors, function(preProcessor) {
return _.isFunction(preProcessor);
});
var d = Q.defer();
var resolveWrap = function(pluginResult) {
self._preProcessorExecutor(d.resolve, pluginResult, preProcessors);
};
exec(resolveWrap, d.reject, "LocationManager", method, commandArgs);
return d.promise;
};
/**
* Signals the native layer that the client side is ready to consume messages.
* Readiness here means that it has a {Delegate} set by the consumer javascript
* code.
* <p>
* The {LocationManager.setDelegate()} will implicitly call this method as well,
* therefore the only case when you have to call this manually is if you don't
* wish to specify a {Delegate} of yours.
* <p>
* The purpose of this signaling mechanism is to make the events work when the
* app is being woken up by the Operating System to give it a chance to handle
* region monitoring events for example.
* <p>
* If you don't set a {Delegate} and don't call this method manually, an error
* message get emitted in the native runtime and the DOM as well after a certain
* period of time.
*
* @return {Q.Promise} Returns a promise which is resolved as soon as the
* native layer acknowledged the request and started to send events.
*/
LocationManager.prototype.onDomDelegateReady = function() {
return this._promisedExec('onDomDelegateReady', [], []);
};
/**
* Determines if bluetooth is switched on, according to the native layer.
* @returns {Q.Promise} Returns a promise which is resolved with a {Boolean}
* indicating whether bluetooth is active.
*/
LocationManager.prototype.isBluetoothEnabled = function() {
return this._promisedExec('isBluetoothEnabled', [], []);
};
/**
* Enables Bluetooth using the native Layer. (ANDROID ONLY)
*
* @returns {Q.Promise} Returns a promise which is resolved when Bluetooth
* could be enabled. If not, the promise will be rejected with an error.
*/
LocationManager.prototype.enableBluetooth = function() {
return this._promisedExec('enableBluetooth', [], []);
};
/**
* Disables Bluetooth using the native Layer. (ANDROID ONLY)
*
* @returns {Q.Promise} Returns a promise which is resolved when Bluetooth
* could be enabled. If not, the promise will be rejected with an error.
*/
LocationManager.prototype.disableBluetooth = function() {
return this._promisedExec('disableBluetooth', [], []);
};
/**
* Start monitoring the specified region.
*
* If a region of the same type with the same identifier is already being
* monitored for this application,
* it will be removed from monitoring. For circular regions, the region
* monitoring service will prioritize
* regions by their size, favoring smaller regions over larger regions.
*
* This is done asynchronously and may not be immediately reflected in monitoredRegions.
*
* @param {Region} region An instance of {Region} which will be monitored
* by the operating system.
*
* @return {Q.Promise} Returns a promise which is resolved as soon as the
* native layer acknowledged the dispatch of the monitoring request.
*/
LocationManager.prototype.startMonitoringForRegion = function(region) {
Regions.checkRegionType(region);
return this._promisedExec('startMonitoringForRegion', [region], []);
};
/**
* Stop monitoring the specified region. It is valid to call
* stopMonitoringForRegion: for a region that was registered for monitoring
* with a different location manager object, during this or previous
* launches of your application.
*
* This is done asynchronously and may not be immediately reflected in monitoredRegions.
*
* @param {Region} region An instance of {Region} which will be monitored
* by the operating system.
*
* @return {Q.Promise} Returns a promise which is resolved as soon as the
* native layer acknowledged the dispatch of the request to stop monitoring.
*/
LocationManager.prototype.stopMonitoringForRegion = function(region) {
Regions.checkRegionType(region);
return this._promisedExec('stopMonitoringForRegion', [region], []);
};
/**
* Request state the for specified region. When result is ready
* didDetermineStateForRegion is triggered. This can be any region,
* also those which is not currently monitored.
*
* This is done asynchronously and may not be immediately reflected in monitoredRegions.
*
* @param {Region} region An instance of {Region} which will be monitored
* by the operating system.
*
* @return {Q.Promise} Returns a promise which is resolved as soon as the
* native layer acknowledged the dispatch of the request to stop monitoring.
*/
LocationManager.prototype.requestStateForRegion = function(region) {
Regions.checkRegionType(region);
return this._promisedExec('requestStateForRegion', [region], []);
};
/**
* Start ranging the specified beacon region.
*
* If a region of the same type with the same identifier is already being
* monitored for this application, it will be removed from monitoring.
*
* This is done asynchronously and may not be immediately reflected in rangedRegions.
*
* @param {Region} region An instance of {BeaconRegion} which will be monitored
* by the operating system.
*
* @return {Q.Promise} Returns a promise which is resolved as soon as the
* native layer acknowledged the dispatch of the monitoring request.
*/
LocationManager.prototype.startRangingBeaconsInRegion = function(region) {
if (!Regions.isBeaconRegion(region))
throw new TypeError('The region parameter has to be an instance of BeaconRegion');
return this._promisedExec('startRangingBeaconsInRegion', [region], []);
};
/**
* Stop ranging the specified region. It is valid to call
* stopMonitoringForRegion: for a region that was registered for ranging
* with a different location manager object, during this or previous
* launches of your application.
*
* This is done asynchronously and may not be immediately reflected in rangedRegions.
*
* @param {Region} region An instance of {BeaconRegion} which will be monitored
* by the operating system.
*
* @return {Q.Promise} Returns a promise which is resolved as soon as the
* native layer acknowledged the dispatch of the request to stop monitoring.
*/
LocationManager.prototype.stopRangingBeaconsInRegion = function(region) {
if (!Regions.isBeaconRegion(region))
throw new TypeError('The region parameter has to be an instance of BeaconRegion');
return this._promisedExec('stopRangingBeaconsInRegion', [region], []);
};
/**
* Queries the native layer to determine the current authorization in effect.
*
* @returns {Q.Promise} Returns a promise which is resolved with the
* requested authorization status.
*/
LocationManager.prototype.getAuthorizationStatus = function() {
return this._promisedExec('getAuthorizationStatus', [], []);
};
/**
* For iOS 8 and above only. The permission model has changed by Apple in iOS 8, making it necessary for apps to
* explicitly request permissions via methods like these:
* <a href="https://developer.apple.com/library/prerelease/iOS/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestWhenInUseAuthorization">requestWhenInUseAuthorization</a>
* <a href="https://developer.apple.com/library/prerelease/iOS/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestAlwaysAuthorization">requestAlwaysAuthorization</a>
*
* If you are using this plugin on Android devices only, you will never have to use this, nor {@code requestAlwaysAuthorization}
* @returns {Q.Promise}
*/
LocationManager.prototype.requestWhenInUseAuthorization = function() {
return this._promisedExec('requestWhenInUseAuthorization', [], []);
};
/**
* See the docuemntation of {@code requestWhenInUseAuthorization} for further details.
*
* @returns {Q.Promise}
*/
LocationManager.prototype.requestAlwaysAuthorization = function() {
return this._promisedExec('requestAlwaysAuthorization', [], []);
};
/**
*
* @returns {Q.Promise} Returns a promise which is resolved with an {Array}
* of {Region} instances that are being monitored by the native layer.
*/
LocationManager.prototype.getMonitoredRegions = function() {
var preProcessors = [Regions.fromJsonArray];
return this._promisedExec('getMonitoredRegions', [], preProcessors);
};
/**
*
* @returns {Q.Promise} Returns a promise which is resolved with an {Array}
* of {Region} instances that are being ranged by the native layer.
*/
LocationManager.prototype.getRangedRegions = function() {
var preProcessors = [Regions.fromJsonArray];
return this._promisedExec('getRangedRegions', [], preProcessors);
};
/**
* Determines if ranging is available or not, according to the native layer.
* @returns {Q.Promise} Returns a promise which is resolved with a {Boolean}
* indicating whether ranging is available or not.
*/
LocationManager.prototype.isRangingAvailable = function() {
return this._promisedExec('isRangingAvailable', [], []);
};
/**
* Determines if region type is supported or not, according to the native layer.
*
* @param {Region} region An instance of {Region} which will be checked
* by the operating system.
*
* @returns {Q.Promise} Returns a promise which is resolved with a {Boolean}
* indicating whether the region type is supported or not.
*/
LocationManager.prototype.isMonitoringAvailableForClass = function(region) {
Regions.checkRegionType(region);
return this._promisedExec('isMonitoringAvailableForClass', [region], []);
};
/**
* Start advertising the specified region.
*
* If a region a different identifier is already being advertised for
* this application, it will be replaced with the new identifier.
*
* This call will accept a valid beacon even when no BlueTooth is available,
* and will start when BlueTooth is powered on. See {Delegate.}
*
* @param {Region} region An instance of {Region} which will be advertised
* by the operating system.
* @param {Integer} measuredPower: Optional parameter, if left empty, the device will
* use it's own default value.
*
* @return {Q.Promise} Returns a promise which is resolved as soon as the
* native layer acknowledged the dispatch of the advertising request.
*/
LocationManager.prototype.startAdvertising = function(region, measuredPower) {
Regions.checkRegionType(region);
if (measuredPower)
return this._promisedExec('startAdvertising', [region, measuredPower], []);
else
return this._promisedExec('startAdvertising', [region], []);
};
/**
* Stop advertising as a beacon.
*
* This is done asynchronously and may not be immediately reflected in isAdvertising.
*
* @return {Q.Promise} Returns a promise which is resolved as soon as the
* native layer acknowledged the dispatch of the request to stop advertising.
*/
LocationManager.prototype.stopAdvertising = function() {
return this._promisedExec('stopAdvertising', [], []);
};
/**
* Determines if advertising is available or not, according to the native layer.
* @returns {Q.Promise} Returns a promise which is resolved with a {Boolean}
* indicating whether advertising is available or not.
*/
LocationManager.prototype.isAdvertisingAvailable = function() {
return this._promisedExec('isAdvertisingAvailable', [], []);
};
/**
* Determines if advertising is currently active, according to the native layer.
* @returns {Q.Promise} Returns a promise which is resolved with a {Boolean}
* indicating whether advertising is active.
*/
LocationManager.prototype.isAdvertising = function() {
return this._promisedExec('isAdvertising', [], []);
};
/**
* Disables debug logging in the native layer. Use this method if you want
* to prevent this plugin from writing to the device logs.
*
* @returns {Q.Promise} Returns a promise which is resolved as soon as the
* native layer has set the logging level accordingly.
*/
LocationManager.prototype.disableDebugLogs = function() {
return this._promisedExec('disableDebugLogs', [], []);
};
/**
* Enables the posting of debug notifications in the native layer. Use this method if you want
* to allow the plugin the posting local notifications.
* This can be very helpful when debugging how to apps behave when launched into the background.
*
* @returns {Q.Promise} Returns a promise which is resolved as soon as the
* native layer has set the flag to enabled.
*/
LocationManager.prototype.enableDebugNotifications = function() {
return this._promisedExec('enableDebugNotifications', [], []);
};
/**
* Disables the posting of debug notifications in the native layer. Use this method if you want
* to prevent the plugin from posting local notifications.
*
* @returns {Q.Promise} Returns a promise which is resolved as soon as the
* native layer has set the flag to disabled.
*/
LocationManager.prototype.disableDebugNotifications = function() {
return this._promisedExec('disableDebugNotifications', [], []);
};
/**
* Enables debug logging in the native layer. Use this method if you want
* a debug the inner workings of this plugin.
*
* @returns {Q.Promise} Returns a promise which is resolved as soon as the
* native layer has set the logging level accordingly.
*/
LocationManager.prototype.enableDebugLogs = function() {
return this._promisedExec('enableDebugLogs', [], []);
};
/**
* Appends the provided [message] to the device logs.
* Note: If debug logging is turned off, this won't do anything.
*
* @param {String} message The message to append to the device logs.
*
* @returns {Q.Promise} Returns a promise which is resolved with the log
* message received by the native layer for appending. The returned message
* is expected to be equivalent to the one provided in the original call.
*/
LocationManager.prototype.appendToDeviceLog = function(message) {
return this._promisedExec('appendToDeviceLog', [message], []);
};
var locationManager = new LocationManager();
locationManager.Regions = Regions;
locationManager.Region = Region;
locationManager.CircularRegion = CircularRegion;
locationManager.BeaconRegion = BeaconRegion;
locationManager.Delegate = Delegate;
module.exports.LocationManager = LocationManager;
module.exports.locationManager = locationManager;