2017-03-20 16:38:14 -04:00
import { Injectable } from '@angular/core' ;
import { Cordova , Plugin } from '@ionic-native/core' ;
2017-03-18 18:33:01 -04:00
import { Observable } from 'rxjs/Observable' ;
2016-05-13 01:26:05 +02:00
declare var window ;
2016-12-06 08:44:42 -05:00
export interface BackgroundGeolocationResponse {
2016-05-13 01:26:05 +02:00
/**
* ID of location as stored in DB (or null)
*/
locationId : number ;
/**
* Service provider
*/
serviceProvider : string ;
/**
* true if location recorded as part of debug
*/
2016-06-01 06:56:04 -04:00
debug : boolean ;
2016-05-13 01:26:05 +02:00
/**
* UTC time of this fix, in milliseconds since January 1, 1970.
*/
time : number ;
/**
* latitude, in degrees.
*/
latitude : number ;
/**
* longitude, in degrees.
*/
longitude : number ;
/**
* estimated accuracy of this location, in meters.
*/
accuracy : number ;
/**
* speed if it is available, in meters/second over ground.
*/
speed : number ;
/**
* altitude if available, in meters above the WGS 84 reference ellipsoid.
*/
altitude : number ;
2016-12-29 23:47:51 -06:00
2016-12-29 16:39:16 +07:00
/**
* accuracy of the altitude if available.
*/
altitudeAccuracy : number ;
2016-05-13 01:26:05 +02:00
/**
* bearing, in degrees.
*/
bearing : number ;
/**
* A Coordinates object defining the current location
*/
coords : Coordinates ;
/**
* A timestamp representing the time at which the location was retrieved.
*/
timestamp : number ;
}
2016-12-06 08:44:42 -05:00
export interface BackgroundGeolocationConfig {
2016-05-13 01:26:05 +02:00
/**
* Desired accuracy in meters. Possible values [0, 10, 100, 1000]. The lower
* the number, the more power devoted to GeoLocation resulting in higher
* accuracy readings. 1000 results in lowest power drain and least accurate
* readings. @see Apple docs (https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instp/CLLocationManager/desiredAccuracy)
*/
desiredAccuracy : number ;
/**
* Stationary radius in meters. When stopped, the minimum distance the device
* must move beyond the stationary location for aggressive background-tracking
* to engage.
*/
stationaryRadius : number ;
/**
* When enabled, the plugin will emit sounds for life-cycle events of
* background-geolocation! See debugging sounds table.
*/
2016-08-01 16:41:59 -05:00
debug? : boolean ;
2016-05-13 01:26:05 +02:00
/**
* The minimum distance (measured in meters) a device must move horizontally
* before an update event is generated. @see Apple docs. (https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instp/CLLocationManager/distanceFilter)
*/
distanceFilter : number ;
/**
* IOS, ANDROID ONLY
* Enable this in order to force a stop() when the application terminated
2016-08-01 16:41:59 -05:00
* (e.g. on iOS, double-tap home button, swipe away the app).o
*
* Defaults to true
2016-05-13 01:26:05 +02:00
*/
stopOnTerminate? : boolean ;
2016-09-20 12:36:56 +02:00
/**
* ANDROID ONLY
* Start background service on device boot.
*
* Defaults to false
*/
startOnBoot? : boolean ;
/**
* ANDROID ONLY
* If false location service will not be started in foreground and no notification will be shown.
*
* Defaults to true
*/
startForeground? : boolean ;
2016-05-13 01:26:05 +02:00
/**
* ANDROID, WP8 ONLY
2017-01-30 05:45:42 -05:00
* When using BackgroundGeolocation.LocationProvider.ANDROID_DISTANCE_FILTER_PROVIDER:
* The minimum time interval between location updates in milliseconds.
2016-05-13 01:26:05 +02:00
* @see Android docs (http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long,%20float,%20android.location.Criteria,%20android.app.PendingIntent))
* and the MS doc (http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geolocator.reportinterval)
* for more information
2017-01-30 05:45:42 -05:00
* When using BackgroundGeolocation.LocationProvider.ANDROID_ACTIVITY_PROVIDER:
* Rate in milliseconds at which your app prefers to receive location updates.
* @see Android docs (https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#getInterval())
2016-05-13 01:26:05 +02:00
*/
2016-08-01 16:41:59 -05:00
interval? : number ;
2016-05-13 01:26:05 +02:00
/**
* ANDROID ONLY
* Custom notification title in the drawer.
*/
notificationTitle? : string ;
/**
* ANDROID ONLY
* Custom notification text in the drawer.
*/
notificationText? : string ;
/**
* ANDROID ONLY
* The accent color to use for notification. Eg. #4CAF50.
*/
notificationIconColor? : string ;
2016-09-20 12:36:56 +02:00
/**
* ANDROID ONLY
* The filename of a custom notification icon. See android quirks.
* NOTE: Only available for API Level >=21.
*/
notificationIconLarge? : string ;
/**
* ANDROID ONLY
* The filename of a custom notification icon. See android quirks.
* NOTE: Only available for API Level >=21.
2016-05-13 01:26:05 +02:00
*/
2016-09-20 12:36:56 +02:00
notificationIconSmall? : string ;
2016-05-13 01:26:05 +02:00
/**
* ANDROID ONLY
* Set location service provider @see wiki (https://github.com/mauron85/cordova-plugin-background-geolocation/wiki/Android-providers)
*/
2016-08-01 16:41:59 -05:00
locationProvider? : number ;
2016-05-13 01:26:05 +02:00
/**
* IOS ONLY
* [AutomotiveNavigation, OtherNavigation, Fitness, Other] Presumably,
* this affects iOS GPS algorithm. @see Apple docs for more information
* (https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instp/CLLocationManager/activityType)
*/
activityType? : string ;
2016-09-20 12:36:56 +02:00
/**
* IOS ONLY
* Pauses location updates when app is paused
*
* Defaults to true
*/
pauseLocationUpdates? : boolean ;
/**
* Server url where to send HTTP POST with recorded locations
* @see https://github.com/mauron85/cordova-plugin-background-geolocation#http-locations-posting
*/
url? : string ;
/**
* Server url where to send fail to post locations
* @see https://github.com/mauron85/cordova-plugin-background-geolocation#http-locations-posting
*/
syncUrl? : string ;
/**
* Specifies how many previously failed locations will be sent to server at once
*
* Defaults to 100
*/
syncThreshold? : number ;
/**
* Optional HTTP headers sent along in HTTP request
*/
httpHeaders? : any ;
/**
* IOS ONLY
* Switch to less accurate significant changes and region monitory when in background (default)
*
* Defaults to 100
*/
saveBatteryOnBackground? : boolean ;
/**
* Limit maximum number of locations stored into db
*
* Defaults to 10000
*/
maxLocations? : number ;
2017-01-30 05:45:42 -05:00
/**
* ANDROID ONLY with BackgroundGeolocation.LocationProvider.ANDROID_ACTIVITY_PROVIDER
*
* Fastest rate in milliseconds at which your app can handle location updates.
* @see Android docs (https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#getFastestInterval())
*/
fastestInterval? : number ;
/**
* ANDROID ONLY with BackgroundGeolocation.LocationProvider.ANDROID_ACTIVITY_PROVIDER
*
* Rate in milliseconds at which activity recognition occurs. Larger values will result in fewer activity detections while improving battery life.
*/
activitiesInterval? : number ;
/**
* ANDROID ONLY with BackgroundGeolocation.LocationProvider.ANDROID_ACTIVITY_PROVIDER
*
* stop() is forced, when the STILL activity is detected (default is true)
*/
stopOnStillActivity? : boolean ;
2016-05-13 01:26:05 +02:00
}
/**
2017-03-20 16:38:14 -04:00
* @name Background Geolocation
2016-05-13 01:26:05 +02:00
* @description
* This plugin provides foreground and background geolocation with battery-saving "circular region monitoring" and "stop detection". For
* more detail, please see https://github.com/mauron85/cordova-plugin-background-geolocation
*
* @usage
*
2017-03-20 16:38:14 -04:00
* BackgroundGeolocation must be called within app.ts and or before Geolocation. Otherwise the platform will not ask you for background tracking permission.
*
2016-07-20 17:17:09 +02:00
* ```typescript
2017-03-20 16:38:14 -04:00
* import { BackgroundGeolocation, BackgroundGeolocationConfig } from '@ionic-native/background-geolocation';
2016-05-13 01:26:05 +02:00
*
2017-03-20 16:38:14 -04:00
* constructor(private backgroundGeolocation: BackgroundGeolocation) { }
2016-05-13 01:26:05 +02:00
*
2017-03-20 16:38:14 -04:00
* ...
2016-10-14 06:43:02 -04:00
*
2017-03-20 16:38:14 -04:00
* const config: BackgroundGeolocationConfig = {
2016-06-09 08:35:03 +02:00
* desiredAccuracy: 10,
* stationaryRadius: 20,
* distanceFilter: 30,
* debug: true, // enable this hear sounds for background-geolocation life-cycle.
* stopOnTerminate: false, // enable this to clear background location settings when the app terminates
* };
2016-07-08 00:34:43 +02:00
*
2017-03-20 16:38:14 -04:00
* this.backgroundGeolocation.configure(config)
* .subscribe((location: BackgroundGeolocationResponse) => {
*
* console.log(location);
*
* // IMPORTANT: You must execute the finish method here to inform the native plugin that you're finished,
* // and the background-task may be completed. You must do this regardless if your HTTP request is successful or not.
* // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
2017-03-21 21:37:42 +01:00
* this.backgroundGeolocation.finish(); // FOR IOS ONLY
2017-03-20 16:38:14 -04:00
*
* });
2016-05-13 01:26:05 +02:00
*
2017-03-20 16:38:14 -04:00
* // start recording location
* this.backgroundGeolocation.start();
2016-05-13 01:26:05 +02:00
*
* // If you wish to turn OFF background-tracking, call the #stop method.
2017-03-20 16:38:14 -04:00
* this.backgroundGeolocation.stop();
2016-05-13 01:26:05 +02:00
*
* ```
2016-10-05 21:33:13 -03:00
* @interfaces
2016-12-06 08:44:42 -05:00
* BackgroundGeolocationResponse
* BackgroundGeolocationConfig
2016-05-13 01:26:05 +02:00
*/
@Plugin ( {
2016-10-27 12:48:50 -05:00
pluginName : 'BackgroundGeolocation' ,
2016-05-13 01:26:05 +02:00
plugin : 'cordova-plugin-mauron85-background-geolocation' ,
2016-07-11 17:42:11 -04:00
pluginRef : 'backgroundGeolocation' ,
2016-06-11 10:49:03 -04:00
repo : 'https://github.com/mauron85/cordova-plugin-background-geolocation' ,
platforms : [ 'iOS' , 'Android' , 'Windows Phone 8' ]
2016-05-13 01:26:05 +02:00
} )
2017-03-20 16:38:14 -04:00
@Injectable ( )
2016-05-13 15:58:43 +02:00
export class BackgroundGeolocation {
2016-05-13 01:26:05 +02:00
2016-09-20 12:36:56 +02:00
/**
* Set location service provider @see https://github.com/mauron85/cordova-plugin-background-geolocation/wiki/Android-providers
*
* Possible values:
* ANDROID_DISTANCE_FILTER_PROVIDER: 0,
* ANDROID_ACTIVITY_PROVIDER: 1
*
* @enum {number}
*/
2017-03-20 16:38:14 -04:00
LocationProvider : any = {
2016-09-20 13:07:01 +02:00
ANDROID_DISTANCE_FILTER_PROVIDER : 0 ,
ANDROID_ACTIVITY_PROVIDER : 1
2016-09-20 12:36:56 +02:00
} ;
/**
* Desired accuracy in meters. Possible values [0, 10, 100, 1000].
* The lower the number, the more power devoted to GeoLocation resulting in higher accuracy readings.
* 1000 results in lowest power drain and least accurate readings.
*
* Possible values:
* HIGH: 0
* MEDIUM: 10
* LOW: 100
* PASSIVE: 1000
*
* enum {number}
*/
2017-03-20 16:38:14 -04:00
Accuracy : any = {
2016-09-20 13:07:01 +02:00
HIGH : 0 ,
MEDIUM : 10 ,
LOW : 100 ,
PASSIVE : 1000
2016-09-20 12:36:56 +02:00
} ;
/**
* Used in the switchMode function
*
* Possible values:
* BACKGROUND: 0
* FOREGROUND: 1
*
* @enum {number}
*/
2017-03-20 16:38:14 -04:00
Mode : any = {
2016-09-20 13:07:01 +02:00
BACKGROUND : 0 ,
FOREGROUND : 1
2016-09-20 12:36:56 +02:00
} ;
2016-05-13 01:26:05 +02:00
/**
* Configure the plugin.
2016-09-20 12:36:56 +02:00
*
2017-03-04 08:15:21 -05:00
* @param options {BackgroundGeolocationConfig} options An object of type Config
2017-03-20 16:38:14 -04:00
* @return {Observable<BackgroundGeolocationResponse>}
2016-05-13 01:26:05 +02:00
*/
2016-05-15 04:47:35 +02:00
@Cordova ( {
2017-03-18 18:33:01 -04:00
callbackOrder : 'reverse' ,
observable : true
2016-05-15 04:47:35 +02:00
} )
2017-03-20 16:38:14 -04:00
configure ( options : BackgroundGeolocationConfig ) : Observable < BackgroundGeolocationResponse > { return ; }
2016-05-13 15:58:43 +02:00
/**
2016-05-15 03:15:51 +02:00
* Turn ON the background-geolocation system.
2016-05-13 15:58:43 +02:00
* The user will be tracked whenever they suspend the app.
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-05-13 15:58:43 +02:00
*/
2016-05-15 03:15:51 +02:00
@Cordova ( )
2017-03-20 16:38:14 -04:00
start ( ) : Promise < any > { return ; }
2016-05-13 15:58:43 +02:00
/**
* Turn OFF background-tracking
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-05-13 15:58:43 +02:00
*/
2016-05-15 03:15:51 +02:00
@Cordova ( )
2017-03-20 16:38:14 -04:00
stop ( ) : Promise < any > { return ; }
2016-05-15 03:15:51 +02:00
2016-05-13 15:58:43 +02:00
/**
* Inform the native plugin that you're finished, the background-task may be completed
2017-03-01 19:50:40 -05:00
* @returns {Promise<any>}
2016-05-13 15:58:43 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'iOS' , 'Windows Phone' ]
} )
2017-03-20 16:38:14 -04:00
finish ( ) : Promise < any > { return ; }
2016-05-13 15:58:43 +02:00
/**
* Force the plugin to enter "moving" or "stationary" state
2017-03-04 08:15:21 -05:00
* @param isMoving {boolean}
2017-03-01 19:53:50 -05:00
* @returns {Promise<any>}
2016-05-13 15:58:43 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'iOS' , 'Windows Phone' ]
} )
2017-03-20 16:38:14 -04:00
changePace ( isMoving : boolean ) : Promise < any > { return ; }
2016-05-13 15:58:43 +02:00
/**
* Setup configuration
2017-03-04 08:15:21 -05:00
* @param options {BackgroundGeolocationConfig}
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-05-13 15:58:43 +02:00
*/
2016-05-15 04:47:35 +02:00
@Cordova ( {
callbackOrder : 'reverse'
} )
2017-03-20 16:38:14 -04:00
setConfig ( options : BackgroundGeolocationConfig ) : Promise < any > { return ; }
2016-05-13 15:58:43 +02:00
2016-05-17 23:34:14 +02:00
/**
* Returns current stationaryLocation if available. null if not
2016-11-29 16:40:50 -06:00
* @returns {Promise<Location>}
2016-05-17 23:34:14 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'iOS' , 'Windows Phone' ]
} )
2017-03-20 16:38:14 -04:00
getStationaryLocation ( ) : Promise < BackgroundGeolocationResponse > { return ; }
2016-05-13 15:58:43 +02:00
2016-05-17 23:34:14 +02:00
/**
* Add a stationary-region listener. Whenever the devices enters "stationary-mode",
* your #success callback will be executed with #location param containing #radius of region
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-05-17 23:34:14 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'iOS' , 'Windows Phone' ]
} )
2017-03-20 16:38:14 -04:00
onStationary ( ) : Promise < any > { return ; }
2016-05-15 03:15:51 +02:00
2016-05-13 15:58:43 +02:00
/**
2016-05-15 03:15:51 +02:00
* Check if location is enabled on the device
2016-05-15 04:47:35 +02:00
* @returns {Promise<number>} Returns a promise with int argument that takes values 0, 1 (true).
2016-05-13 15:58:43 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'Android' ]
} )
2017-03-20 16:38:14 -04:00
isLocationEnabled ( ) : Promise < number > { return ; }
2016-05-13 15:58:43 +02:00
2016-09-19 18:18:53 -04:00
/**
* Display app settings to change permissions
*/
2016-09-20 12:36:56 +02:00
@Cordova ( { sync : true } )
2017-03-20 16:38:14 -04:00
showAppSettings ( ) : void { }
2016-09-19 18:18:53 -04:00
2016-05-15 03:15:51 +02:00
/**
* Display device location settings
*/
2016-09-20 12:36:56 +02:00
@Cordova ( { sync : true } )
2017-03-20 16:38:14 -04:00
showLocationSettings ( ) : void { }
2016-05-13 15:58:43 +02:00
2016-05-17 23:34:14 +02:00
/**
* Method can be used to detect user changes in location services settings.
* If user enable or disable location services then success callback will be executed.
* In case or error (SettingNotFoundException) fail callback will be executed.
2016-11-29 16:40:50 -06:00
* @returns {Promise<boolean>}
2016-05-17 23:34:14 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'Android' ]
} )
2017-03-20 16:38:14 -04:00
watchLocationMode ( ) : Promise < boolean > { return ; }
2016-05-13 15:58:43 +02:00
2016-05-17 23:34:14 +02:00
/**
* Stop watching for location mode changes.
2017-03-01 19:53:50 -05:00
* @returns {Promise<any>}
2016-05-17 23:34:14 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'Android' ]
} )
2017-03-20 16:38:14 -04:00
stopWatchingLocationMode ( ) : Promise < any > { return ; }
2016-05-15 03:15:51 +02:00
2016-05-17 23:34:14 +02:00
/**
* Method will return all stored locations.
* Locations are stored when:
* - config.stopOnTerminate is false and main activity was killed
* by the system
* or
* - option.debug is true
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-05-17 23:34:14 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'Android' ]
} )
2017-03-20 16:38:14 -04:00
getLocations ( ) : Promise < any > { return ; }
2016-09-20 12:36:56 +02:00
/**
* Method will return locations, which has not been yet posted to server. NOTE: Locations does contain locationId.
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-09-20 12:36:56 +02:00
*/
@Cordova ( )
2017-03-20 16:38:14 -04:00
getValidLocations ( ) : Promise < any > { return ; }
2016-05-15 03:15:51 +02:00
2016-05-17 23:34:14 +02:00
/**
* Delete stored location by given locationId.
2017-03-04 08:15:21 -05:00
* @param locationId {number}
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-05-17 23:34:14 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'Android' ]
} )
2017-03-20 16:38:14 -04:00
deleteLocation ( locationId : number ) : Promise < any > { return ; }
2016-05-15 03:15:51 +02:00
2016-05-15 04:47:35 +02:00
/**
2016-05-17 23:34:14 +02:00
* Delete all stored locations.
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-05-15 04:47:35 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'Android' ]
} )
2017-03-20 16:38:14 -04:00
deleteAllLocations ( ) : Promise < any > { return ; }
2016-09-20 12:36:56 +02:00
/**
* Normally plugin will handle switching between BACKGROUND and FOREGROUND mode itself.
* Calling switchMode you can override plugin behavior and force plugin to switch into other mode.
*
* In FOREGROUND mode plugin uses iOS local manager to receive locations and behavior is affected by option.desiredAccuracy and option.distanceFilter.
* In BACKGROUND mode plugin uses significant changes and region monitoring to receive locations and uses option.stationaryRadius only.
*
* BackgroundGeolocation.Mode.FOREGROUND
* BackgroundGeolocation.Mode.BACKGROUND
2017-03-01 19:53:50 -05:00
**
2017-03-04 08:15:21 -05:00
* @param modeId {number}
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-09-20 12:36:56 +02:00
*/
2017-03-01 19:53:50 -05:00
@Cordova ( {
platforms : [ 'iOS' ]
} )
2017-03-20 16:38:14 -04:00
switchMode ( modeId : number ) : Promise < any > { return ; }
2016-05-15 03:15:51 +02:00
2016-09-20 12:36:56 +02:00
/**
* Return all logged events. Useful for plugin debugging. Parameter limit limits number of returned entries.
* @see https://github.com/mauron85/cordova-plugin-background-geolocation/tree/v2.2.1#debugging for more information.
*
2017-03-04 08:15:21 -05:00
* @param limit {number} Limits the number of entries
2016-11-29 16:40:50 -06:00
* @returns {Promise<any>}
2016-09-20 12:36:56 +02:00
*/
@Cordova ( )
2017-03-20 16:38:14 -04:00
getLogEntries ( limit : number ) : Promise < any > { return ; }
2016-12-06 08:02:00 -05:00
2016-05-13 01:26:05 +02:00
}