-
Notifications
You must be signed in to change notification settings - Fork 75
Replace fused location provide with GPS #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HarelM
wants to merge
2
commits into
capacitor-community:master
Choose a base branch
from
HarelM:gps-provider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Watcher> watchers = new HashSet<Watcher>(); | ||
| private HashSet<Watcher> 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<Watcher>(); | ||
| 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) { } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in ab6a04a |
||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup.