@josuelmm/cordova-background-geolocation 2.3.5 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/.npmignore +9 -0
  2. package/CHANGELOG.md +16 -0
  3. package/README.md +223 -57
  4. package/RELEASE.MD +1 -1
  5. package/android/CDVBackgroundGeolocation/src/main/java/com/marianhello/bgloc/cordova/ConfigMapper.java +4 -0
  6. package/android/CDVBackgroundGeolocation/src/main/java/com/tenforwardconsulting/bgloc/cordova/BackgroundGeolocationPlugin.java +7 -0
  7. package/android/common/src/main/java/com/marianhello/bgloc/BootCompletedReceiver.java +11 -0
  8. package/android/common/src/main/java/com/marianhello/bgloc/Config.java +18 -0
  9. package/android/common/src/main/java/com/marianhello/bgloc/HttpPostService.java +72 -27
  10. package/android/common/src/main/java/com/marianhello/bgloc/service/LocationServiceImpl.java +85 -1
  11. package/android/common/src/main/java/com/marianhello/bgloc/service/LocationServiceProxy.java +12 -1
  12. package/angular/background-geolocation-enums.ts +63 -0
  13. package/angular/background-geolocation-events.ts +17 -0
  14. package/angular/background-geolocation.module.ts +29 -0
  15. package/angular/background-geolocation.service.ts +228 -0
  16. package/angular/dist/background-geolocation-enums.d.ts +54 -0
  17. package/angular/dist/background-geolocation-events.d.ts +17 -0
  18. package/angular/dist/background-geolocation.module.d.ts +16 -0
  19. package/angular/dist/background-geolocation.service.d.ts +67 -0
  20. package/angular/dist/esm2022/background-geolocation-enums.mjs +64 -0
  21. package/angular/dist/esm2022/background-geolocation-events.mjs +19 -0
  22. package/angular/dist/esm2022/background-geolocation.module.mjs +43 -0
  23. package/angular/dist/esm2022/background-geolocation.service.mjs +148 -0
  24. package/angular/dist/esm2022/josuelmm-cordova-background-geolocation.mjs +5 -0
  25. package/angular/dist/esm2022/public-api.mjs +9 -0
  26. package/angular/dist/fesm2022/josuelmm-cordova-background-geolocation.mjs +283 -0
  27. package/angular/dist/fesm2022/josuelmm-cordova-background-geolocation.mjs.map +1 -0
  28. package/angular/dist/index.d.ts +5 -0
  29. package/angular/dist/public-api.d.ts +9 -0
  30. package/angular/tsconfig.json +18 -0
  31. package/ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.m +8 -0
  32. package/ios/common/BackgroundGeolocation/MAURLocation.h +2 -0
  33. package/ios/common/BackgroundGeolocation/MAURLocation.m +16 -2
  34. package/ios/common/README.md +1 -5
  35. package/package.json +32 -2
  36. package/plugin.xml +1 -1
  37. package/www/BackgroundGeolocation.d.ts +149 -2
  38. package/www/BackgroundGeolocation.js +13 -0
package/.npmignore CHANGED
@@ -7,6 +7,15 @@
7
7
  !/plugin.xml
8
8
  !/package.json
9
9
  !/www
10
+ !/angular
11
+ # Only ship ng-packagr output for ./angular, not old tsc .js/.d.ts
12
+ /angular/*.js
13
+ /angular/*.d.ts
14
+ /angular/public-api.ts
15
+ /angular/ng-package.json
16
+ /angular/tsconfig.lib.json
17
+ /angular/package.json
18
+ !/angular/dist
10
19
  !/android
11
20
  /android/*
12
21
  !/android/dependencies.gradle
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## [3.0.0](https://github.com/josuelmm/cordova-background-geolocation/tree/3.0.0) (2026-02-20)
4
+
5
+ ### Added
6
+ - Android: Wake lock (PARTIAL_WAKE_LOCK) during tracking
7
+ - Android: Optional location watchdog (`enableWatchdog`) to restart provider if no update in ~60s
8
+ - `openSettings()` — convenience alias for `showAppSettings()`
9
+ - `getPluginVersion()` — returns plugin version from native
10
+ - iOS: `simulated` field in location payload (iOS 15+, from `sourceInformation.isSimulatedBySoftware`)
11
+ - Config option `enableWatchdog` (Android, default `false`)
12
+ - Angular: full documentation in docs/angular.md
13
+
14
+ ### Changed
15
+ - Version bump to 3.0.0
16
+
17
+ [Full Changelog](https://github.com/josuelmm/cordova-background-geolocation/compare/2.3.4...3.0.0)
18
+
3
19
  ## [2.3.4](https://github.com/josuelmm/cordova-background-geolocation/tree/2.3.4) (2026-02-20)
4
20
 
5
21
  [Full Changelog](https://github.com/josuelmm/cordova-background-geolocation/compare/v2.0.4...2.3.4)
package/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Cordova Background Geolocation Plugin
1
+ # Cordova Background Geolocation
2
+ # Capacitor Background Geolocation
3
+ # Background Geolocation
2
4
 
3
5
  [![npm](https://img.shields.io/npm/v/@josuelmm/cordova-background-geolocation?style=flat-square)](https://www.npmjs.com/package/@josuelmm/cordova-background-geolocation)
4
6
  ![npm downloads](https://img.shields.io/npm/dm/@josuelmm/cordova-background-geolocation?style=flat-square)
@@ -7,98 +9,262 @@
7
9
  [![GitHub stars](https://img.shields.io/github/stars/josuelmm/cordova-background-geolocation?style=flat-square)](https://github.com/josuelmm/cordova-background-geolocation/stargazers)
8
10
  ![GitHub last commit](https://img.shields.io/github/last-commit/josuelmm/cordova-background-geolocation?style=flat-square)
9
11
 
10
- ## Introduction
12
+ ## What it does
11
13
 
12
- *Cross-platform geolocation for Cordova and Capacitor with battery-saving "circular region monitoring" and "stop detection"*
14
+ This plugin provides **background and foreground geolocation** for **Cordova**, **Capacitor**, and **Ionic** apps. It is tested and works with current versions of Capacitor and Ionic. It is more battery- and data-efficient than the HTML5 Geolocation API and is designed to keep tracking location even when the app is in the background or the screen is off.
13
15
 
14
- This plugin can be used for geolocation when the app is running in the foreground or background. It is more battery and data efficient than html5 geolocation. It can be used side by side with other geolocation providers (eg. html5 navigator.geolocation).
16
+ **Features:**
15
17
 
16
- This project is based on [@mauron85/cordova-plugin-background-geolocation](https://github.com/mauron85/cordova-plugin-background-geolocation), which in turn was based on the original [cordova-background-geolocation plugin](https://github.com/christocracy/cordova-plugin-background-geolocation) by [christocracy](https://github.com/christocracy). This independent fork is maintained at [josuelmm/cordova-background-geolocation](https://github.com/josuelmm/cordova-background-geolocation). If you have any fixes, features or updates that you would like included, please do raise a PR or issue on the GitHub repository.
18
+ - **Circular region monitoring** and **stop detection** to reduce battery use
19
+ - **Activity-based provider** (e.g. walking, driving, still) for smarter updates
20
+ - **Configurable distance/interval** filtering and optional **HTTP posting** of locations to your server
21
+ - **Foreground service** (Android) with a persistent notification so the OS does not kill the tracker
22
+ - Works alongside other geolocation sources (e.g. `navigator.geolocation`)
17
23
 
18
- **ATENTION:** This project changes the package name from it's parents. The words are the same but in a different order, which is easy to miss and can cause some confusion
24
+ **Self-contained:** This plugin works on its own. You install it, call `BackgroundGeolocation.configure()`, `start()`, etc. directly. TypeScript definitions (`.d.ts`) are included. You do **not** need any wrapper or extra package for Capacitor or Cordova.
19
25
 
20
- We are also looking to maintainers to help with this, so that the project does not end up orphaned. If you are interested in helping out with maintaining the project, please open an [issue or discussion](https://github.com/josuelmm/cordova-background-geolocation/issues) on GitHub.
26
+ **Capacitor & Ionic:** Use the plugin in a Capacitor app (with or without Ionic). Install the package, run `npx cap sync`, then use the same JavaScript API. The plugin is compatible with recent Capacitor (e.g. 6.x, 7.x) and Ionic (7.x, 8.x) versions.
21
27
 
22
- The NPM package can be found at [@josuelmm/cordova-background-geolocation](https://www.npmjs.com/package/@josuelmm/cordova-background-geolocation).
28
+ ---
23
29
 
24
- **Recent (v2.3.4):** Sync and location providers were hardened for production: null-safe config/manager checks, HTTP timeouts so the sync notification cannot stay stuck, migration of the Android activity provider to `FusedLocationProviderClient` / `ActivityRecognitionClient`, and PendingIntent/requestCode fixes on the distance-filter provider. See [CHANGELOG](CHANGELOG.md) for details.
30
+ ## Installation
25
31
 
26
- <font size="4">[Documentation](https://josuelmm.github.io/cordova-background-geolocation/)</font> (original docs; API is compatible)
32
+ ### npm (Capacitor or modern Cordova)
27
33
 
28
- ### Installing the plugin
29
-
30
- Este fork se publica en npm como **`@josuelmm/cordova-background-geolocation`**. Para publicar tú mismo o automatizar desde GitHub, ver [NPM_PUBLISH.md](NPM_PUBLISH.md).
31
-
32
- **Note:** for non AndroidX project please use version 1.x of this plugin. Version 2.x and on will support AndroidX.
33
-
34
- **Note:** this plugin can be installed on a Capacitor project and it is tested to be working as expected, some configuration may need to be done differently than below according to how Capacitor configuration is implemented.
35
-
36
- **Note:** for Android 14+ there's a need to let Google know why the app needs to use the location and have a video link when uploading for the first time to play console. It takes some time to approve and after that there's no need to do it again. This is related to `FOREGROUND_SERVICE_LOCATION` premission.
37
-
38
- **Note:** for Android 13+ there's a need for runtime `POST_NOTIFICATION` permission request in order to show the icon.
34
+ ```bash
35
+ npm install @josuelmm/cordova-background-geolocation
36
+ ```
39
37
 
38
+ For Capacitor, sync native projects after installing:
40
39
 
41
40
  ```bash
42
- npm install @josuelmm/cordova-background-geolocation
43
41
  npx cap sync
44
42
  ```
45
43
 
44
+ ### Cordova CLI
45
+
46
46
  ```bash
47
47
  cordova plugin add @josuelmm/cordova-background-geolocation
48
48
  ```
49
49
 
50
- You may also want to change default iOS permission prompts and set specific google play version and android support library version for compatibility with other plugins.
51
-
52
- **Note:** Always consult documentation of other plugins to figure out compatible versions.
50
+ Optional: set variables for Google Play Services and iOS permission strings:
53
51
 
54
52
  ```bash
55
53
  cordova plugin add @josuelmm/cordova-background-geolocation \
56
54
  --variable GOOGLE_PLAY_SERVICES_VERSION=17+ \
57
- --variable ALWAYS_USAGE_DESCRIPTION="App requires ..." \
58
- --variable MOTION_USAGE_DESCRIPTION="App requires motion detection"
55
+ --variable ALWAYS_USAGE_DESCRIPTION="Your app needs location for ..." \
56
+ --variable MOTION_USAGE_DESCRIPTION="Your app uses motion for ..."
57
+ ```
58
+
59
+ **Notes:**
60
+
61
+ - **AndroidX:** Use plugin version 2.x. For non-AndroidX projects use 1.x.
62
+ - **Android 14+:** You may need to justify foreground location usage (e.g. in Play Console) for `FOREGROUND_SERVICE_LOCATION`.
63
+ - **Android 13+:** Request runtime `POST_NOTIFICATION` permission if you want the tracking notification to show.
64
+ - After changing plugin options, remove and reinstall the plugin for changes to take effect.
65
+
66
+ ---
67
+
68
+ ## Usage (with or without Angular)
69
+
70
+ You can use the plugin in two ways:
71
+
72
+ - **Without Angular** — Use the global `BackgroundGeolocation` object (Cordova/Capacitor injects it after `deviceready`). Same in plain JS, React, Vue, or any framework.
73
+ - **With Angular (Ionic Angular)** — Import the Angular service and inject it; same API, better testability and no global. See [Angular (Ionic Angular)](#angular-ionic-angular) below.
74
+
75
+ The following steps use the global API. If you use Angular, call the same methods on the injected service instead.
76
+
77
+ ### TypeScript imports
78
+
79
+ You can use either the **native** type names or the **Awesome Cordova Plugins–style** aliases.
80
+
81
+ **Option A — Named import (same style as @awesome-cordova-plugins):**
82
+
83
+ ```ts
84
+ import {
85
+ BackgroundGeolocation,
86
+ BackgroundGeolocationConfig,
87
+ BackgroundGeolocationEvents,
88
+ BackgroundGeolocationResponse
89
+ } from '@josuelmm/cordova-background-geolocation';
90
+
91
+ // After deviceready (use the global object, not injection):
92
+ BackgroundGeolocation.configure({ distanceFilter: 50 } as BackgroundGeolocationConfig);
93
+ BackgroundGeolocation.on('location', (loc: BackgroundGeolocationResponse) => { ... });
94
+ ```
95
+
96
+ **Angular/Ionic:** One import for service and types: use `BackgroundGeolocationService` (and types like `BackgroundGeolocationConfig`, `BackgroundGeolocationResponse`) from `@josuelmm/cordova-background-geolocation/angular`. See [Angular (Ionic Angular)](#angular-ionic-angular).
97
+
98
+ **Option B — Default export + native type names:**
99
+
100
+ ```ts
101
+ import BackgroundGeolocation from '@josuelmm/cordova-background-geolocation';
102
+ import type {
103
+ ConfigureOptions,
104
+ Location,
105
+ LocationOptions,
106
+ ServiceStatus,
107
+ Activity,
108
+ BackgroundGeolocationError,
109
+ LogEntry
110
+ } from '@josuelmm/cordova-background-geolocation';
111
+
112
+ // After deviceready:
113
+ BackgroundGeolocation.configure({ distanceFilter: 50 } as ConfigureOptions);
114
+ BackgroundGeolocation.on('location', (loc: Location) => { ... });
59
115
  ```
60
116
 
61
- **Note:** To apply changes, you must remove and reinstall plugin.
117
+ **Type aliases / compatibility:** `BackgroundGeolocationConfig` = `ConfigureOptions`, `BackgroundGeolocationResponse` = `Location`. `BackgroundGeolocationEvents` is an enum (e.g. `BackgroundGeolocationEvents.location`). Enums and types match [@awesome-cordova-plugins/background-geolocation](https://github.com/danielsogl/awesome-cordova-plugins/blob/master/src/%40awesome-cordova-plugins/plugins/background-geolocation/index.ts) where applicable; **accuracy values** in this plugin are `0, 100, 1000, 10000` (use `BackgroundGeolocation.HIGH_ACCURACY` etc. or the `BackgroundGeolocationAccuracy` enum from the types).
118
+
119
+ **Constants** (accuracy, provider, mode) are on the plugin object:
62
120
 
63
- ### Usage
121
+ ```ts
122
+ BackgroundGeolocation.HIGH_ACCURACY
123
+ BackgroundGeolocation.ACTIVITY_PROVIDER
124
+ BackgroundGeolocation.BACKGROUND_MODE
125
+ BackgroundGeolocation.FOREGROUND_MODE
126
+ ```
127
+
128
+ ### 1. Configure
64
129
 
65
- First, configure the plugin with the settings you require.
130
+ Set your preferred provider, accuracy, intervals, and optional server URL:
66
131
 
67
132
  ```js
68
133
  BackgroundGeolocation.configure({
69
- locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
70
- desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
71
- stationaryRadius: 50,
72
- distanceFilter: 50,
73
- notificationTitle: 'Background tracking',
74
- notificationText: 'enabled',
75
- debug: true,
76
- interval: 10000,
77
- fastestInterval: 5000,
78
- activitiesInterval: 10000,
79
- url: 'http://192.168.81.15:3000/location',
80
- httpHeaders: {
81
- 'X-FOO': 'bar'
82
- },
83
- // customize post properties
84
- postTemplate: {
85
- lat: '@latitude',
86
- lon: '@longitude',
87
- foo: 'bar' // you can also add your own properties
88
- }
134
+ locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
135
+ desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
136
+ stationaryRadius: 50,
137
+ distanceFilter: 50,
138
+ notificationTitle: 'Background tracking',
139
+ notificationText: 'enabled',
140
+ debug: true,
141
+ interval: 10000,
142
+ fastestInterval: 5000,
143
+ activitiesInterval: 10000,
144
+ url: 'https://yourserver.com/location',
145
+ // HTTP headers can be sent in two ways:
146
+ // 1) Static: set httpHeaders here — same headers on every POST/sync request (e.g. API key, Content-Type).
147
+ httpHeaders: {
148
+ 'X-FOO': 'bar',
149
+ 'Authorization': 'Bearer YOUR_TOKEN'
150
+ },
151
+ postTemplate: {
152
+ lat: '@latitude',
153
+ lon: '@longitude'
154
+ }
155
+ });
156
+ ```
157
+
158
+ ### 2. Start tracking
159
+
160
+ ```js
161
+ BackgroundGeolocation.start();
162
+ ```
163
+
164
+ ### 3. Listen for events
165
+
166
+ ```js
167
+ BackgroundGeolocation.on('location', function (location) {
168
+ console.log(location.latitude, location.longitude);
169
+ });
170
+
171
+ BackgroundGeolocation.on('error', function (error) {
172
+ console.warn(error.code, error.message);
173
+ });
174
+ ```
175
+
176
+ **HTTP headers (second way — dynamic):** When your server responds with **401 Unauthorized**, the plugin emits the `http_authorization` event. You can then refresh your token and reconfigure headers so the next request uses them (e.g. a new `Authorization` header):
177
+
178
+ ```js
179
+ BackgroundGeolocation.on('http_authorization', function () {
180
+ // e.g. get a new token from your auth API, then:
181
+ BackgroundGeolocation.configure({
182
+ httpHeaders: { 'Authorization': 'Bearer ' + newToken }
89
183
  });
184
+ });
185
+ ```
186
+
187
+ ### 4. Stop tracking
188
+
189
+ ```js
190
+ BackgroundGeolocation.stop();
191
+ ```
192
+
193
+ More options (stationary, activity, start/stop events, headless task) are in the [documentation](https://josuelmm.github.io/cordova-background-geolocation/). For **Angular** (service, methods, events, example), see [Angular (Ionic Angular)](https://josuelmm.github.io/cordova-background-geolocation/angular).
194
+
195
+ ---
196
+
197
+ ## Angular (Ionic Angular)
198
+
199
+ The package includes an **Angular integration**: an injectable service and optional NgModule. You can use the plugin **without it** (global `BackgroundGeolocation`) or **with it** (inject the service). Both use the same native plugin.
200
+
201
+ ### Install (same as above)
202
+
203
+ ```bash
204
+ npm install @josuelmm/cordova-background-geolocation
205
+ npx cap sync
206
+ ```
207
+
208
+ ### How to import and use
209
+
210
+ **One import** — Service and the most used types are exported from the `/angular` entry, so you do **not** need a second import from the main package:
211
+
212
+ ```ts
213
+ import {
214
+ BackgroundGeolocationService,
215
+ BackgroundGeolocationConfig,
216
+ BackgroundGeolocationEvents,
217
+ BackgroundGeolocationResponse
218
+ } from '@josuelmm/cordova-background-geolocation/angular';
219
+
220
+ @Injectable({ ... })
221
+ export class MyService {
222
+ constructor(private bg: BackgroundGeolocationService) {}
223
+
224
+ startTracking() {
225
+ this.bg.configure({ distanceFilter: 50, url: 'https://...' } as BackgroundGeolocationConfig)
226
+ .then(() => this.bg.start());
227
+ }
228
+
229
+ onLocation() {
230
+ return this.bg.on('location', (loc: BackgroundGeolocationResponse) => console.log(loc));
231
+ // subscription.unsubscribe() when done
232
+ }
233
+ }
90
234
  ```
91
235
 
92
- Then call `start()` to start location tracking.
236
+ **You must import `BackgroundGeolocationModule`** in your `AppModule` (or feature module) so the service is provided and AOT builds work. Then inject `BackgroundGeolocationService` as in the example above. See [docs/angular.md](docs/angular.md) for the full snippet.
237
+
238
+ **Migrating from @awesome-cordova-plugins/background-geolocation:** there you inject a class named `BackgroundGeolocation`. In this package, `BackgroundGeolocation` is the **global plugin object**, not an injectable class. Use `BackgroundGeolocationService` instead (same API). See [docs/angular.md](docs/angular.md) for details.
239
+
240
+ ### Summary
241
+
242
+ | Use case | What to do |
243
+ |-----------------------|------------|
244
+ | **Without Angular** | Use global `BackgroundGeolocation` after `deviceready`. Types: main package or Awesome-style aliases (see [TypeScript imports](#typescript-imports) above). |
245
+ | **With Angular** | Import from `@josuelmm/cordova-background-geolocation/angular`: add `BackgroundGeolocationModule` to your module `imports`, then inject `BackgroundGeolocationService`. Do **not** inject the global `BackgroundGeolocation`. |
246
+
247
+ No extra wrapper (e.g. Awesome Cordova Plugins) is required.
248
+
249
+ ---
250
+
251
+ ## Compatibility
252
+
253
+ | Plugin version | Cordova CLI | Cordova Android | Cordova iOS |
254
+ |----------------|-------------|-----------------|-------------|
255
+ | 1.x | ≥ 8.0.0 | ≥ 8.0.0 | ≥ 6.0.0 |
256
+ | 2.x | ≥ 10.0.0 | ≥ 10.0.0 | ≥ 6.0.0 |
257
+
258
+ ---
259
+
260
+ ## Documentation and changelog
93
261
 
94
- A more comprehensive example can be found in the [Documentation](https://josuelmm.github.io/cordova-background-geolocation/example) (original docs).
262
+ - [Documentation](https://josuelmm.github.io/cordova-background-geolocation/) (API, options, examples)
263
+ - [CHANGELOG](CHANGELOG.md) for version history
95
264
 
96
- ### Compatibility
265
+ This project is based on [@mauron85/cordova-plugin-background-geolocation](https://github.com/mauron85/cordova-plugin-background-geolocation) and the original by [christocracy](https://github.com/christocracy). Maintained at [josuelmm/cordova-background-geolocation](https://github.com/josuelmm/cordova-background-geolocation). Issues and PRs welcome.
97
266
 
98
- | Plugin version | Cordova CLI | Cordova Platform Android | Cordova Platform iOS |
99
- |------------------|-------------------|--------------------------|----------------------|
100
- | >1.0.0 | 8.0.0 | 8.0.0 | 6.0.0 |
101
- | >2.0.0 | 10.0.0 | 10.0.0 | 6.0.0 |
267
+ ---
102
268
 
103
269
  ## Licence
104
270
 
package/RELEASE.MD CHANGED
@@ -1,5 +1,5 @@
1
1
  In order to release a version the following actions are needed:
2
2
  1. Update the version in the package.json, lock and plugin.xml files
3
3
  2. Go to [releases and create a new release](https://github.com/josuelmm/cordova-background-geolocation/releases/new) in GitHub
4
- 3. Make sure to create a tag too with the version name - v2.3.4
4
+ 3. Make sure to create a tag too with the version name (e.g. v3.0.0)
5
5
  4. Click create. Github actions will publish the package to npm
@@ -98,6 +98,9 @@ public class ConfigMapper {
98
98
  config.setTemplate(LocationTemplateFactory.fromJSON(postTemplate));
99
99
  }
100
100
  }
101
+ if (jObject.has("enableWatchdog")) {
102
+ config.setEnableWatchdog(jObject.getBoolean("enableWatchdog"));
103
+ }
101
104
 
102
105
  return config;
103
106
  }
@@ -127,6 +130,7 @@ public class ConfigMapper {
127
130
  json.put("syncThreshold", config.getSyncThreshold());
128
131
  json.put("httpHeaders", new JSONObject(config.getHttpHeaders()));
129
132
  json.put("maxLocations", config.getMaxLocations());
133
+ json.put("enableWatchdog", Boolean.TRUE.equals(config.getEnableWatchdog()));
130
134
  LocationTemplate tpl = config.getTemplate();
131
135
  Object template = JSONObject.NULL;
132
136
  if (tpl instanceof HashMapLocationTemplate) {
@@ -71,6 +71,10 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Plugin
71
71
  public static final String ACTION_END_TASK = "endTask";
72
72
  public static final String ACTION_REGISTER_HEADLESS_TASK = "registerHeadlessTask";
73
73
  public static final String ACTION_FORCE_SYNC = "forceSync";
74
+ public static final String ACTION_GET_PLUGIN_VERSION = "getPluginVersion";
75
+
76
+ /** Plugin version; keep in sync with plugin.xml. */
77
+ public static final String PLUGIN_VERSION = "3.0.0";
74
78
 
75
79
  private BackgroundGeolocationFacade facade;
76
80
 
@@ -365,6 +369,9 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Plugin
365
369
  logger.debug("Forced location sync requested");
366
370
  facade.forceSync();
367
371
  return true;
372
+ } else if (ACTION_GET_PLUGIN_VERSION.equals(action)) {
373
+ callbackContext.success(PLUGIN_VERSION);
374
+ return true;
368
375
  }
369
376
 
370
377
  return false;
@@ -9,9 +9,11 @@ This is a new class
9
9
 
10
10
  package com.marianhello.bgloc;
11
11
 
12
+ import android.Manifest;
12
13
  import android.content.BroadcastReceiver;
13
14
  import android.content.Context;
14
15
  import android.content.Intent;
16
+ import android.content.pm.PackageManager;
15
17
  import android.os.Build;
16
18
  import android.util.Log;
17
19
 
@@ -44,6 +46,10 @@ public class BootCompletedReceiver extends BroadcastReceiver {
44
46
  Log.d(TAG, "Boot completed " + config.toString());
45
47
 
46
48
  if (config.getStartOnBoot()) {
49
+ if (!hasLocationPermission(context)) {
50
+ Log.w(TAG, "Skipping start on boot: ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION not granted");
51
+ return;
52
+ }
47
53
  Log.i(TAG, "Starting service after boot");
48
54
  Intent locationServiceIntent = new Intent(context, LocationServiceImpl.class);
49
55
  locationServiceIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
@@ -56,4 +62,9 @@ public class BootCompletedReceiver extends BroadcastReceiver {
56
62
  }
57
63
  }
58
64
  }
65
+
66
+ private static boolean hasLocationPermission(Context context) {
67
+ return context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
68
+ || context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
69
+ }
59
70
  }
@@ -63,6 +63,7 @@ public class Config implements Parcelable
63
63
  private HashMap httpHeaders;
64
64
  private Integer maxLocations;
65
65
  private LocationTemplate template;
66
+ private Boolean enableWatchdog;
66
67
 
67
68
  public Config () {
68
69
  }
@@ -92,6 +93,7 @@ public class Config implements Parcelable
92
93
  this.syncThreshold = config.syncThreshold;
93
94
  this.httpHeaders = CloneHelper.deepCopy(config.httpHeaders);
94
95
  this.maxLocations = config.maxLocations;
96
+ this.enableWatchdog = config.enableWatchdog;
95
97
  if (config.template instanceof AbstractLocationTemplate) {
96
98
  this.template = ((AbstractLocationTemplate)config.template).clone();
97
99
  }
@@ -120,6 +122,7 @@ public class Config implements Parcelable
120
122
  setSyncUrl(in.readString());
121
123
  setSyncThreshold(in.readInt());
122
124
  setMaxLocations(in.readInt());
125
+ setEnableWatchdog((Boolean) in.readValue(null));
123
126
  Bundle bundle = in.readBundle();
124
127
  setHttpHeaders((HashMap<String, String>) bundle.getSerializable("httpHeaders"));
125
128
  setTemplate((LocationTemplate) bundle.getSerializable(AbstractLocationTemplate.BUNDLE_KEY));
@@ -151,6 +154,7 @@ public class Config implements Parcelable
151
154
  config.httpHeaders = null;
152
155
  config.maxLocations = 10000;
153
156
  config.template = null;
157
+ config.enableWatchdog = false;
154
158
 
155
159
  return config;
156
160
  }
@@ -183,6 +187,7 @@ public class Config implements Parcelable
183
187
  out.writeString(getSyncUrl());
184
188
  out.writeInt(getSyncThreshold());
185
189
  out.writeInt(getMaxLocations());
190
+ out.writeValue(getEnableWatchdog());
186
191
  Bundle bundle = new Bundle();
187
192
  bundle.putSerializable("httpHeaders", getHttpHeaders());
188
193
  bundle.putSerializable(AbstractLocationTemplate.BUNDLE_KEY, (AbstractLocationTemplate) getTemplate());
@@ -520,6 +525,19 @@ public class Config implements Parcelable
520
525
  this.template = template;
521
526
  }
522
527
 
528
+ public boolean hasEnableWatchdog() {
529
+ return enableWatchdog != null;
530
+ }
531
+
532
+ @Nullable
533
+ public Boolean getEnableWatchdog() {
534
+ return enableWatchdog;
535
+ }
536
+
537
+ public void setEnableWatchdog(Boolean enableWatchdog) {
538
+ this.enableWatchdog = enableWatchdog;
539
+ }
540
+
523
541
  @Override
524
542
  public String toString () {
525
543
  return new StringBuffer()