From 8feb8366721c474105021a1cbd8809cd7ea54450 Mon Sep 17 00:00:00 2001 From: HarelM Date: Wed, 23 Jul 2025 08:51:29 +0300 Subject: [PATCH 1/2] Replace fused locaiton provide with GPS --- .../BackgroundGeolocationService.java | 90 ++++++++----------- 1 file changed, 35 insertions(+), 55 deletions(-) diff --git a/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java b/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java index bcb0dc3..766ba01 100644 --- a/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java +++ b/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java @@ -2,21 +2,14 @@ import android.app.Notification; import android.app.Service; +import android.content.Context; import android.content.Intent; -import android.content.pm.ServiceInfo; -import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.os.Binder; -import android.os.Build; import android.os.IBinder; import com.getcapacitor.Logger; -import com.google.android.gms.location.FusedLocationProviderClient; -import com.google.android.gms.location.LocationAvailability; -import com.google.android.gms.location.LocationCallback; -import com.google.android.gms.location.LocationRequest; -import com.google.android.gms.location.LocationResult; -import com.google.android.gms.location.LocationServices; - import java.util.HashSet; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -33,14 +26,14 @@ public class BackgroundGeolocationService extends Service { // Must be unique for this application. private static final int NOTIFICATION_ID = 28351; - private class Watcher { + private static class Watcher { public String id; - public FusedLocationProviderClient client; - public LocationRequest locationRequest; - public LocationCallback locationCallback; + public LocationManager client; + public float distanceFilter; + public LocationListener locationCallback; public Notification backgroundNotification; } - private HashSet watchers = new HashSet(); + private HashSet watchers = new HashSet<>(); @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -59,7 +52,7 @@ public IBinder onBind(Intent intent) { @Override public boolean onUnbind(Intent intent) { for (Watcher watcher : watchers) { - watcher.client.removeLocationUpdates(watcher.locationCallback); + watcher.client.removeUpdates(watcher.locationCallback); } watchers = new HashSet(); stopSelf(); @@ -82,39 +75,22 @@ void addWatcher( Notification backgroundNotification, float distanceFilter ) { - FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient( - BackgroundGeolocationService.this - ); - LocationRequest locationRequest = new LocationRequest(); - locationRequest.setMaxWaitTime(1000); - locationRequest.setInterval(1000); - locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); - locationRequest.setSmallestDisplacement(distanceFilter); - - LocationCallback callback = new LocationCallback(){ - @Override - public void onLocationResult(LocationResult locationResult) { - Location location = locationResult.getLastLocation(); - Intent intent = new Intent(ACTION_BROADCAST); - intent.putExtra("location", location); - intent.putExtra("id", id); - LocalBroadcastManager.getInstance( - getApplicationContext() - ).sendBroadcast(intent); - } - @Override - public void onLocationAvailability(LocationAvailability availability) { - if (!availability.isLocationAvailable()) { - Logger.debug("Location not available"); - } - } + LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); + + LocationListener listener = location -> { + Intent intent = new Intent(ACTION_BROADCAST); + intent.putExtra("location", location); + intent.putExtra("id", id); + LocalBroadcastManager.getInstance( + getApplicationContext() + ).sendBroadcast(intent); }; Watcher watcher = new Watcher(); watcher.id = id; - watcher.client = client; - watcher.locationRequest = locationRequest; - watcher.locationCallback = callback; + watcher.client = locationManager; + watcher.distanceFilter = distanceFilter; + watcher.locationCallback = listener; watcher.backgroundNotification = backgroundNotification; watchers.add(watcher); @@ -123,9 +99,10 @@ public void onLocationAvailability(LocationAvailability availability) { // we simply ignore the exception. try { watcher.client.requestLocationUpdates( - watcher.locationRequest, - watcher.locationCallback, - null + LocationManager.GPS_PROVIDER, + 1000, + watcher.distanceFilter, + watcher.locationCallback ); } catch (SecurityException ignore) {} @@ -150,7 +127,7 @@ public void onLocationAvailability(LocationAvailability availability) { void removeWatcher(String id) { for (Watcher watcher : watchers) { if (watcher.id.equals(id)) { - watcher.client.removeLocationUpdates(watcher.locationCallback); + watcher.client.removeUpdates(watcher.locationCallback); watchers.remove(watcher); if (getNotification() == null) { stopForeground(true); @@ -164,12 +141,15 @@ void onPermissionsGranted() { // If permissions were granted while the app was in the background, for example in // the Settings app, the watchers need restarting. for (Watcher watcher : watchers) { - watcher.client.removeLocationUpdates(watcher.locationCallback); - watcher.client.requestLocationUpdates( - watcher.locationRequest, - watcher.locationCallback, - null - ); + watcher.client.removeUpdates(watcher.locationCallback); + try { + watcher.client.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + 1000, + watcher.distanceFilter, + watcher.locationCallback + ); + } catch (SecurityException ignore) { } } } From ab6a04a31c3a7b6bdf3b45a1b4e2ae5392b26900 Mon Sep 17 00:00:00 2001 From: HarelM Date: Thu, 24 Jul 2025 12:43:16 +0300 Subject: [PATCH 2/2] Move duplicate code to a single method --- .../BackgroundGeolocationService.java | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java b/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java index 766ba01..0c26ff9 100644 --- a/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java +++ b/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java @@ -59,6 +59,21 @@ public boolean onUnbind(Intent intent) { return false; } + private void requestLocationUpdates(Watcher watcher) { + try { + watcher.client.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + 1000, + watcher.distanceFilter, + watcher.locationCallback + ); + } catch (SecurityException ignore) { + // According to Android Studio, this method can throw a Security Exception if + // permissions are not yet granted. Rather than check the permissions, which is fiddly, + // we simply ignore the exception. + } + } + Notification getNotification() { for (Watcher watcher : watchers) { if (watcher.backgroundNotification != null) { @@ -94,17 +109,7 @@ void addWatcher( watcher.backgroundNotification = backgroundNotification; watchers.add(watcher); - // According to Android Studio, this method can throw a Security Exception if - // permissions are not yet granted. Rather than check the permissions, which is fiddly, - // we simply ignore the exception. - try { - watcher.client.requestLocationUpdates( - LocationManager.GPS_PROVIDER, - 1000, - watcher.distanceFilter, - watcher.locationCallback - ); - } catch (SecurityException ignore) {} + requestLocationUpdates(watcher); // Promote the service to the foreground if necessary. // Ideally we would only call 'startForeground' if the service is not already @@ -142,14 +147,7 @@ void onPermissionsGranted() { // the Settings app, the watchers need restarting. for (Watcher watcher : watchers) { watcher.client.removeUpdates(watcher.locationCallback); - try { - watcher.client.requestLocationUpdates( - LocationManager.GPS_PROVIDER, - 1000, - watcher.distanceFilter, - watcher.locationCallback - ); - } catch (SecurityException ignore) { } + requestLocationUpdates(watcher); } }