Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Watcher> watchers = new HashSet<Watcher>();
private HashSet<Watcher> watchers = new HashSet<>();

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Expand All @@ -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<Watcher>();
stopSelf();
Expand All @@ -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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it the GPS provider is high accuracy by default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup.

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);

Expand All @@ -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) {}

Expand All @@ -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);
Expand All @@ -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) { }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is duplicated from earlier, can it be extracted as a helper method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ab6a04a

}
}

Expand Down