@josuelmm/cordova-background-geolocation 3.0.2 → 3.1.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/CHANGELOG.md +24 -0
  2. package/README.md +114 -8
  3. package/RELEASE.MD +1 -1
  4. package/android/CDVBackgroundGeolocation/src/main/java/com/marianhello/bgloc/cordova/ConfigMapper.java +20 -0
  5. package/android/CDVBackgroundGeolocation/src/main/java/com/tenforwardconsulting/bgloc/cordova/BackgroundGeolocationPlugin.java +32 -2
  6. package/android/CDVBackgroundGeolocation/src/test/java/com/marianhello/ConfigMapperTest.java +12 -0
  7. package/android/common/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java +24 -1
  8. package/android/common/src/main/java/com/marianhello/bgloc/Config.java +87 -0
  9. package/android/common/src/main/java/com/marianhello/bgloc/HttpPostService.java +34 -1
  10. package/android/common/src/main/java/com/marianhello/bgloc/data/LocationDAO.java +5 -0
  11. package/android/common/src/main/java/com/marianhello/bgloc/data/provider/ContentProviderLocationDAO.java +11 -0
  12. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationContract.java +10 -0
  13. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationDAO.java +24 -0
  14. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationDAO.java +13 -0
  15. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteOpenHelper.java +13 -1
  16. package/android/common/src/main/java/com/marianhello/bgloc/sync/SyncAdapter.java +14 -9
  17. package/angular/background-geolocation.service.ts +14 -0
  18. package/angular/dist/background-geolocation.service.d.ts +2 -0
  19. package/angular/dist/esm2022/background-geolocation.service.mjs +7 -1
  20. package/angular/dist/fesm2022/josuelmm-cordova-background-geolocation.mjs +6 -0
  21. package/angular/dist/fesm2022/josuelmm-cordova-background-geolocation.mjs.map +1 -1
  22. package/ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.h +2 -0
  23. package/ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.m +16 -0
  24. package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.h +2 -0
  25. package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.m +22 -0
  26. package/ios/common/BackgroundGeolocation/MAURConfig.h +3 -0
  27. package/ios/common/BackgroundGeolocation/MAURConfig.m +20 -1
  28. package/ios/common/BackgroundGeolocation/MAURConfigurationContract.h +1 -0
  29. package/ios/common/BackgroundGeolocation/MAURConfigurationContract.m +1 -0
  30. package/ios/common/BackgroundGeolocation/MAURGeolocationOpenHelper.m +5 -1
  31. package/ios/common/BackgroundGeolocation/MAURPostLocationTask.m +4 -3
  32. package/ios/common/BackgroundGeolocation/MAURSQLiteConfigurationDAO.m +16 -9
  33. package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.h +2 -0
  34. package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.m +20 -0
  35. package/package.json +3 -2
  36. package/plugin.xml +1 -1
  37. package/www/BackgroundGeolocation.d.ts +65 -0
  38. package/www/BackgroundGeolocation.js +12 -0
@@ -33,6 +33,7 @@
33
33
  #define CC_COLUMN_NAME_URL "url"
34
34
  #define CC_COLUMN_NAME_SYNC_URL "sync_url"
35
35
  #define CC_COLUMN_NAME_SYNC_THRESHOLD "sync_threshold"
36
+ #define CC_COLUMN_NAME_SYNC_ENABLED "sync_enabled"
36
37
  #define CC_COLUMN_NAME_HEADERS "http_headers"
37
38
  #define CC_COLUMN_NAME_SAVE_BATTERY "save_battery"
38
39
  #define CC_COLUMN_NAME_MAX_LOCATIONS "max_locations"
@@ -37,6 +37,7 @@
37
37
  @{ @"name": @CC_COLUMN_NAME_URL, @"type": [SQLColumnType sqlColumnWithType: kText]},
38
38
  @{ @"name": @CC_COLUMN_NAME_SYNC_URL, @"type": [SQLColumnType sqlColumnWithType: kText]},
39
39
  @{ @"name": @CC_COLUMN_NAME_SYNC_THRESHOLD, @"type": [SQLColumnType sqlColumnWithType: kInteger]},
40
+ @{ @"name": @CC_COLUMN_NAME_SYNC_ENABLED, @"type": [SQLColumnType sqlColumnWithType: kInteger]},
40
41
  @{ @"name": @CC_COLUMN_NAME_HEADERS, @"type": [SQLColumnType sqlColumnWithType: kText]},
41
42
  @{ @"name": @CC_COLUMN_NAME_SAVE_BATTERY, @"type": [SQLColumnType sqlColumnWithType: kInteger]},
42
43
  @{ @"name": @CC_COLUMN_NAME_MAX_LOCATIONS, @"type": [SQLColumnType sqlColumnWithType: kInteger]},
@@ -14,7 +14,7 @@
14
14
  @implementation MAURGeolocationOpenHelper
15
15
 
16
16
  static NSString *const kDatabaseName = @"cordova_bg_geolocation.db";
17
- static NSInteger const kDatabaseVersion = 3;
17
+ static NSInteger const kDatabaseVersion = 4;
18
18
 
19
19
  - (instancetype)init
20
20
  {
@@ -81,6 +81,10 @@ static NSInteger const kDatabaseVersion = 3;
81
81
  [sql addObjectsFromArray: @[
82
82
  [MAURConfigurationContract createTableSQL]
83
83
  ]];
84
+ case 3:
85
+ [sql addObjectsFromArray: @[
86
+ [NSString stringWithFormat:@"ALTER TABLE %s ADD COLUMN %s INTEGER", CC_TABLE_NAME, CC_COLUMN_NAME_SYNC_ENABLED]
87
+ ]];
84
88
  break; // break only for previous db version (cascade statements)
85
89
  default:
86
90
  return;
@@ -101,7 +101,7 @@ static MAURLocationTransform s_locationTransform = nil;
101
101
  }
102
102
  }
103
103
 
104
- if ([self.config hasValidSyncUrl]) {
104
+ if ([self.config hasValidSyncUrl] && [self.config syncEnabled]) {
105
105
  NSNumber *locationsCount = [locationDAO getLocationsForSyncCount];
106
106
  NSInteger threshold = self.config.syncThreshold != nil ? self.config.syncThreshold.integerValue : 100;
107
107
  if (locationsCount && [locationsCount integerValue] >= threshold) {
@@ -212,9 +212,10 @@ static MAURLocationTransform s_locationTransform = nil;
212
212
 
213
213
  - (void) sync
214
214
  {
215
- if ([self.config hasValidSyncUrl]) {
216
- [uploader sync:self.config.syncUrl withTemplate:self.config._template withHttpHeaders:self.config.httpHeaders];
215
+ if (![self.config syncEnabled] || ![self.config hasValidSyncUrl]) {
216
+ return;
217
217
  }
218
+ [uploader sync:self.config.syncUrl withTemplate:self.config._template withHttpHeaders:self.config.httpHeaders];
218
219
  }
219
220
 
220
221
  #pragma mark - Location transform
@@ -81,13 +81,14 @@
81
81
  @COMMA_SEP @CC_COLUMN_NAME_URL
82
82
  @COMMA_SEP @CC_COLUMN_NAME_SYNC_URL
83
83
  @COMMA_SEP @CC_COLUMN_NAME_SYNC_THRESHOLD
84
+ @COMMA_SEP @CC_COLUMN_NAME_SYNC_ENABLED
84
85
  @COMMA_SEP @CC_COLUMN_NAME_HEADERS
85
86
  @COMMA_SEP @CC_COLUMN_NAME_SAVE_BATTERY
86
87
  @COMMA_SEP @CC_COLUMN_NAME_MAX_LOCATIONS
87
88
  @COMMA_SEP @CC_COLUMN_NAME_PAUSE_LOCATION_UPDATES
88
89
  @COMMA_SEP @CC_COLUMN_NAME_TEMPLATE
89
90
  @COMMA_SEP @CC_COLUMN_NAME_LAST_UPDATED_AT
90
- @") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,DateTime('now'))";
91
+ @") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,DateTime('now'))";
91
92
 
92
93
  [queue inDatabase:^(FMDatabase *database) {
93
94
  success = [database executeUpdate:sql,
@@ -113,6 +114,7 @@
113
114
  [config hasUrl] ? config.url : @CC_COLUMN_NAME_NULLABLE,
114
115
  [config hasSyncUrl] ? config.syncUrl : @CC_COLUMN_NAME_NULLABLE,
115
116
  [config hasSyncThreshold] ? config.syncThreshold : @CC_COLUMN_NAME_NULLABLE,
117
+ [config hasSyncEnabled] ? [NSNumber numberWithBool:[config syncEnabled]] : @CC_COLUMN_NAME_NULLABLE,
116
118
  (httpHeadersString != nil) ? httpHeadersString : @CC_COLUMN_NAME_NULLABLE,
117
119
  [config hasSaveBatteryOnBackground] ? config._saveBatteryOnBackground : @CC_COLUMN_NAME_NULLABLE,
118
120
  [config hasMaxLocations] ? config.maxLocations : @CC_COLUMN_NAME_NULLABLE,
@@ -157,6 +159,7 @@
157
159
  @COMMA_SEP @CC_COLUMN_NAME_URL
158
160
  @COMMA_SEP @CC_COLUMN_NAME_SYNC_URL
159
161
  @COMMA_SEP @CC_COLUMN_NAME_SYNC_THRESHOLD
162
+ @COMMA_SEP @CC_COLUMN_NAME_SYNC_ENABLED
160
163
  @COMMA_SEP @CC_COLUMN_NAME_HEADERS
161
164
  @COMMA_SEP @CC_COLUMN_NAME_SAVE_BATTERY
162
165
  @COMMA_SEP @CC_COLUMN_NAME_MAX_LOCATIONS
@@ -201,24 +204,28 @@
201
204
  if ([self isNonNull:rs columnIndex:21]) {
202
205
  config.syncThreshold = [NSNumber numberWithInt:[rs intForColumnIndex:21]];
203
206
  }
204
- if ([self isNonNull:rs columnIndex:22]) {
205
- NSString *httpHeadersString = [rs stringForColumnIndex:22];
207
+ id syncEnabledVal = [rs objectForColumnIndex:22];
208
+ if (syncEnabledVal != nil && syncEnabledVal != [NSNull null]) {
209
+ config.syncEnabled = [NSNumber numberWithBool:[rs intForColumnIndex:22] == 1 ? YES : NO];
210
+ }
211
+ if ([self isNonNull:rs columnIndex:23]) {
212
+ NSString *httpHeadersString = [rs stringForColumnIndex:23];
206
213
  if (httpHeadersString != nil) {
207
214
  NSData *jsonHttpHeaders = [httpHeadersString dataUsingEncoding:NSUTF8StringEncoding];
208
215
  config.httpHeaders = [NSJSONSerialization JSONObjectWithData:jsonHttpHeaders options:0 error:nil];
209
216
  }
210
217
  }
211
- if ([self isNonNull:rs columnIndex:23]) {
212
- config._saveBatteryOnBackground = [NSNumber numberWithBool:[rs intForColumnIndex:23] == 1 ? YES : NO];
213
- }
214
218
  if ([self isNonNull:rs columnIndex:24]) {
215
- config.maxLocations = [NSNumber numberWithInt:[rs intForColumnIndex:24]];
219
+ config._saveBatteryOnBackground = [NSNumber numberWithBool:[rs intForColumnIndex:24] == 1 ? YES : NO];
216
220
  }
217
221
  if ([self isNonNull:rs columnIndex:25]) {
218
- config._pauseLocationUpdates = [NSNumber numberWithBool:[rs intForColumnIndex:25] == 1 ? YES : NO];
222
+ config.maxLocations = [NSNumber numberWithInt:[rs intForColumnIndex:25]];
219
223
  }
220
224
  if ([self isNonNull:rs columnIndex:26]) {
221
- NSString *templateAsString = [rs stringForColumnIndex:26];
225
+ config._pauseLocationUpdates = [NSNumber numberWithBool:[rs intForColumnIndex:26] == 1 ? YES : NO];
226
+ }
227
+ if ([self isNonNull:rs columnIndex:27]) {
228
+ NSString *templateAsString = [rs stringForColumnIndex:27];
222
229
  if (templateAsString != nil) {
223
230
  NSData *jsonTemplate = [templateAsString dataUsingEncoding:NSUTF8StringEncoding];
224
231
  config._template = [NSJSONSerialization JSONObjectWithData:jsonTemplate options:0 error:nil];
@@ -26,6 +26,8 @@
26
26
  - (NSNumber*) persistLocation:(MAURLocation*)location limitRows:(NSInteger)maxRows;
27
27
  - (BOOL) deleteLocation:(NSNumber*)locationId error:(NSError * __autoreleasing *)outError;
28
28
  - (BOOL) deleteAllLocations:(NSError * __autoreleasing *)outError;
29
+ /** Mark all locations pending sync (PostPending) as deleted. Clears the sync queue without sending. */
30
+ - (BOOL) deletePendingSyncLocations:(NSError * __autoreleasing *)outError;
29
31
  - (BOOL) clearDatabase;
30
32
  - (NSString*) getDatabaseName;
31
33
  - (NSString*) getDatabasePath;
@@ -304,6 +304,26 @@
304
304
  return success;
305
305
  }
306
306
 
307
+ - (BOOL) deletePendingSyncLocations:(NSError * __autoreleasing *)outError
308
+ {
309
+ __block BOOL success = YES;
310
+ NSString *sql = @"UPDATE " @LC_TABLE_NAME @" SET " @LC_COLUMN_NAME_STATUS @" = ? WHERE " @LC_COLUMN_NAME_STATUS @" = ?";
311
+
312
+ [queue inDatabase:^(FMDatabase *database) {
313
+ if (![database executeUpdate:sql, [NSString stringWithFormat:@"%ld", MAURLocationDeleted], [NSString stringWithFormat:@"%ld", MAURLocationPostPending]]) {
314
+ int errorCode = [database lastErrorCode];
315
+ NSString *errorMessage = [database lastErrorMessage];
316
+ NSLog(@"deletePendingSyncLocations failed code: %d: message: %@", errorCode, errorMessage);
317
+ if (outError != NULL) {
318
+ *outError = [NSError errorWithDomain:Domain code:errorCode userInfo:@{ NSLocalizedDescriptionKey: errorMessage ?: @"" }];
319
+ }
320
+ success = NO;
321
+ }
322
+ }];
323
+
324
+ return success;
325
+ }
326
+
307
327
  - (BOOL) clearDatabase
308
328
  {
309
329
  __block BOOL success;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@josuelmm/cordova-background-geolocation",
3
- "version": "3.0.2",
3
+ "version": "3.1.0",
4
4
  "description": "Cordova Background Geolocation (fork actualizado)",
5
5
  "main": "./www/BackgroundGeolocation.js",
6
6
  "types": "./www/BackgroundGeolocation.d.ts",
@@ -29,7 +29,8 @@
29
29
  "scripts": {
30
30
  "build:angular": "npx ng-packagr -p angular/ng-package.json",
31
31
  "prepack": "npm run build:angular",
32
- "prepublishOnly": "npm run build:angular"
32
+ "pack": "npm pack",
33
+ "build-package": "npm run build:angular && npm pack"
33
34
  },
34
35
  "cordova": {
35
36
  "id": "cordova-background-geolocation",
package/plugin.xml CHANGED
@@ -2,7 +2,7 @@
2
2
  <plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
3
3
  xmlns:android="http://schemas.android.com/apk/res/android"
4
4
  id="cordova-background-geolocation"
5
- version="3.0.2">
5
+ version="3.1.0">
6
6
  <name>cordova-background-geolocation</name>
7
7
  <description>Cordova Background Geolocation Plugin</description>
8
8
  <license>Apache-2.0</license>
@@ -214,6 +214,33 @@ export interface ConfigureOptions {
214
214
  */
215
215
  notificationText?: string;
216
216
 
217
+ /**
218
+ * Title shown in the notification while locations are syncing to the server.
219
+ * Use this (and notificationSyncText, etc.) to localize sync notifications.
220
+ *
221
+ * Platform: Android
222
+ * @default "Syncing locations"
223
+ */
224
+ notificationSyncTitle?: string;
225
+
226
+ /**
227
+ * Text shown in the sync notification while upload is in progress.
228
+ * @default "Sync in progress"
229
+ */
230
+ notificationSyncText?: string;
231
+
232
+ /**
233
+ * Text shown when sync completes successfully.
234
+ * @default "Sync completed"
235
+ */
236
+ notificationSyncCompletedText?: string;
237
+
238
+ /**
239
+ * Text shown when sync fails (prefix before " (HTTP …)" or ": error").
240
+ * @default "Sync failed"
241
+ */
242
+ notificationSyncFailedText?: string;
243
+
217
244
  /**
218
245
  * The accent color (hex triplet) to use for notification.
219
246
  * Eg. <code>#4CAF50</code>.
@@ -301,6 +328,15 @@ export interface ConfigureOptions {
301
328
  */
302
329
  syncThreshold?: number;
303
330
 
331
+ /**
332
+ * Whether synchronization to syncUrl is enabled (automatic and forceSync).
333
+ * When false, no sync runs; locations are still stored and can be synced later by setting sync: true.
334
+ *
335
+ * Platform: Android, iOS
336
+ * @default true
337
+ */
338
+ sync?: boolean;
339
+
304
340
  /**
305
341
  * Optional HTTP headers sent along in HTTP request.
306
342
  *
@@ -710,6 +746,7 @@ export interface BackgroundGeolocationPlugin {
710
746
  /**
711
747
  * Force sync of pending locations.
712
748
  * Option <code>syncThreshold</code> will be ignored and all pending locations will be immediately posted to <code>syncUrl</code> in single batch.
749
+ * No-op if <code>sync</code> is false in config.
713
750
  *
714
751
  * Platform: Android, iOS
715
752
  *
@@ -721,6 +758,34 @@ export interface BackgroundGeolocationPlugin {
721
758
  fail?: (error: BackgroundGeolocationError) => void
722
759
  ): Promise<void>;
723
760
 
761
+ /**
762
+ * Clear the pending sync queue: discard all locations waiting to be sent to syncUrl.
763
+ * They will not be synced. Use when the user wants to discard pending locations.
764
+ *
765
+ * Platform: Android, iOS
766
+ *
767
+ * @param success
768
+ * @param fail
769
+ */
770
+ clearSync(
771
+ success?: () => void,
772
+ fail?: (error: BackgroundGeolocationError) => void
773
+ ): Promise<void>;
774
+
775
+ /**
776
+ * Get the number of locations pending to be synced (not yet sent to syncUrl).
777
+ * Use with forceSync() to sync on demand.
778
+ *
779
+ * Platform: Android, iOS
780
+ *
781
+ * @param success Called with the pending count (number).
782
+ * @param fail
783
+ */
784
+ getPendingSyncCount(
785
+ success?: (count: number) => void,
786
+ fail?: (error: BackgroundGeolocationError) => void
787
+ ): Promise<number>;
788
+
724
789
  /**
725
790
  * Get stored configuration options.
726
791
  *
@@ -221,6 +221,18 @@ var BackgroundGeolocation = {
221
221
  'forceSync');
222
222
  },
223
223
 
224
+ clearSync: function (success, failure) {
225
+ return execWithPromise(success,
226
+ failure,
227
+ 'clearSync');
228
+ },
229
+
230
+ getPendingSyncCount: function (success, failure) {
231
+ return execWithPromise(success,
232
+ failure,
233
+ 'getPendingSyncCount');
234
+ },
235
+
224
236
  on: function (event, callbackFn) {
225
237
  assert(this.events.indexOf(event) > -1, [TAG, '#on unknown event "' + event + '"']);
226
238
  if (!callbackFn) {