@capgo/background-geolocation 8.0.6 → 8.0.8

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.
@@ -16,6 +16,8 @@
16
16
  <!-- Android SDK 33+ requires the POST_NOTIFICATIONS runtime permission to
17
17
  display the foreground service notification. -->
18
18
  <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
19
+ <!-- See https://github.com/Cap-go/capacitor-background-geolocation/issues/12 for more details-->
20
+ <uses-permission android:name="android.permission.WAKE_LOCK" />
19
21
  <uses-feature android:name="android.hardware.location.gps" />
20
22
  </manifest>
21
23
 
@@ -14,7 +14,10 @@ import android.location.LocationManager;
14
14
  import android.media.MediaPlayer;
15
15
  import android.os.Binder;
16
16
  import android.os.Build;
17
+ import android.os.Handler;
17
18
  import android.os.IBinder;
19
+ import android.os.Looper;
20
+ import android.os.PowerManager;
18
21
  import androidx.localbroadcastmanager.content.LocalBroadcastManager;
19
22
  import com.getcapacitor.Logger;
20
23
 
@@ -40,6 +43,12 @@ public class BackgroundGeolocationService extends Service {
40
43
  private double distanceThreshold;
41
44
  private boolean isOffRoute;
42
45
 
46
+ private Handler watchdogHandler = new Handler(Looper.getMainLooper());
47
+ private Runnable watchdogRunnable;
48
+ private Runnable restartRunnable;
49
+ private float currentDistanceFilter;
50
+ private PowerManager.WakeLock wakeLock;
51
+
43
52
  @Override
44
53
  public IBinder onBind(Intent intent) {
45
54
  return binder;
@@ -55,6 +64,8 @@ public class BackgroundGeolocationService extends Service {
55
64
  client.removeUpdates(locationCallback);
56
65
  }
57
66
  releaseMediaPlayer();
67
+ releaseWakeLock();
68
+ stopWatchdog();
58
69
  stopSelf();
59
70
  return false;
60
71
  }
@@ -66,6 +77,8 @@ public class BackgroundGeolocationService extends Service {
66
77
  }
67
78
  super.onDestroy();
68
79
  releaseMediaPlayer();
80
+ releaseWakeLock();
81
+ stopWatchdog();
69
82
  }
70
83
 
71
84
  private void releaseMediaPlayer() {
@@ -83,15 +96,87 @@ public class BackgroundGeolocationService extends Service {
83
96
  mediaPlayer = null;
84
97
  }
85
98
 
99
+ private void acquireWakeLock() {
100
+ if (wakeLock != null && wakeLock.isHeld()) {
101
+ return;
102
+ }
103
+ try {
104
+ PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
105
+ wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "BackgroundGeolocation::LocationWakeLock");
106
+ wakeLock.acquire();
107
+ Logger.info("Wake lock acquired");
108
+ } catch (Exception e) {
109
+ Logger.error("Error acquiring wake lock", e);
110
+ }
111
+ }
112
+
113
+ private void releaseWakeLock() {
114
+ if (wakeLock == null) {
115
+ return;
116
+ }
117
+ try {
118
+ if (wakeLock.isHeld()) {
119
+ wakeLock.release();
120
+ Logger.info("Wake lock released");
121
+ }
122
+ } catch (Exception e) {
123
+ Logger.error("Error releasing wake lock", e);
124
+ }
125
+ wakeLock = null;
126
+ }
127
+
128
+ private void restartLocationUpdates() {
129
+ Logger.debug("Location watchdog timed out, restarting updates");
130
+ if (client == null || locationCallback == null) {
131
+ return;
132
+ }
133
+ client.removeUpdates(locationCallback);
134
+ if (restartRunnable != null) {
135
+ watchdogHandler.removeCallbacks(restartRunnable);
136
+ }
137
+ restartRunnable = () -> {
138
+ if (client == null || locationCallback == null) {
139
+ return;
140
+ }
141
+ try {
142
+ client.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, currentDistanceFilter, locationCallback);
143
+ } catch (SecurityException ignore) {
144
+ // Permission issues are handled in the start() method
145
+ }
146
+ startWatchdog();
147
+ };
148
+ watchdogHandler.postDelayed(restartRunnable, 10000);
149
+ }
150
+
151
+ private void startWatchdog() {
152
+ stopWatchdog();
153
+ if (watchdogRunnable == null) {
154
+ watchdogRunnable = this::restartLocationUpdates;
155
+ }
156
+ watchdogHandler.postDelayed(watchdogRunnable, 60000);
157
+ }
158
+
159
+ private void stopWatchdog() {
160
+ if (watchdogRunnable != null) {
161
+ watchdogHandler.removeCallbacks(watchdogRunnable);
162
+ }
163
+ if (restartRunnable != null) {
164
+ watchdogHandler.removeCallbacks(restartRunnable);
165
+ }
166
+ }
167
+
86
168
  // Handles requests from the activity.
87
169
  public class LocalBinder extends Binder {
88
170
 
89
171
  void start(final String id, final String notificationTitle, final String notificationMessage, float distanceFilter) {
90
172
  releaseMediaPlayer();
173
+ acquireWakeLock();
91
174
  client = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
92
175
  callbackId = id;
176
+ currentDistanceFilter = distanceFilter;
93
177
 
94
178
  locationCallback = (location) -> {
179
+ startWatchdog();
95
180
  if (mediaPlayer != null) {
96
181
  double[] point = { location.getLongitude(), location.getLatitude() };
97
182
  var offRoute = distancePointToRoute(point) > distanceThreshold;
@@ -139,10 +224,12 @@ public class BackgroundGeolocationService extends Service {
139
224
  }
140
225
 
141
226
  String stop() {
227
+ stopWatchdog();
142
228
  client.removeUpdates(locationCallback);
143
229
  stopForeground(true);
144
230
  stopSelf();
145
231
  releaseMediaPlayer();
232
+ releaseWakeLock();
146
233
  return callbackId;
147
234
  }
148
235
 
@@ -38,7 +38,7 @@ func formatLocation(_ location: CLLocation) -> PluginCallResultData {
38
38
  @objc(BackgroundGeolocation)
39
39
  // swiftlint:disable:next type_body_length
40
40
  public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate, CAPBridgedPlugin {
41
- private let pluginVersion: String = "8.0.6"
41
+ private let pluginVersion: String = "8.0.8"
42
42
  public let identifier = "BackgroundGeolocationPlugin"
43
43
  public let jsName = "BackgroundGeolocation"
44
44
  public let pluginMethods: [CAPPluginMethod] = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/background-geolocation",
3
- "version": "8.0.6",
3
+ "version": "8.0.8",
4
4
  "description": "Receive accurate geolocation updates even while the app is in the background.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",