From ac5f7457bd224ae0d9395a8623921fa0ebfb05d2 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Thu, 9 Jul 2020 11:01:38 +0200 Subject: [PATCH] Ensure to clean up location requests on update --- .../internal/LocationRequestUpdateData.java | 6 ++-- .../gms/location/GoogleLocationManager.java | 29 ++++++++++++++----- .../gms/location/LocationRequestHelper.java | 8 ++--- .../gms/location/RealLocationProvider.java | 9 +++++- .../gms/location/UnifiedLocationProvider.kt | 7 +++++ 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/play-services-location-api/src/main/java/com/google/android/gms/location/internal/LocationRequestUpdateData.java b/play-services-location-api/src/main/java/com/google/android/gms/location/internal/LocationRequestUpdateData.java index f3b6742a..6da81cc3 100644 --- a/play-services-location-api/src/main/java/com/google/android/gms/location/internal/LocationRequestUpdateData.java +++ b/play-services-location-api/src/main/java/com/google/android/gms/location/internal/LocationRequestUpdateData.java @@ -56,10 +56,10 @@ public class LocationRequestUpdateData extends AutoSafeParcelable { return "LocationRequestUpdateData{" + "opCode=" + opCode + ", request=" + request + - ", listener=" + listener + + ", listener=" + (listener != null ? listener.asBinder() : null) + ", pendingIntent=" + pendingIntent + - ", callback=" + callback + - ", fusedLocationProviderCallback=" + fusedLocationProviderCallback + + ", callback=" + (callback != null ? callback.asBinder() : null) + + ", fusedLocationProviderCallback=" + (fusedLocationProviderCallback != null ? fusedLocationProviderCallback.asBinder() : null) + '}'; } diff --git a/play-services-location-core/src/main/java/org/microg/gms/location/GoogleLocationManager.java b/play-services-location-core/src/main/java/org/microg/gms/location/GoogleLocationManager.java index 6d936476..ae767666 100644 --- a/play-services-location-core/src/main/java/org/microg/gms/location/GoogleLocationManager.java +++ b/play-services-location-core/src/main/java/org/microg/gms/location/GoogleLocationManager.java @@ -116,12 +116,27 @@ public class GoogleLocationManager implements LocationChangeListener { } private void requestLocationUpdates(LocationRequestHelper request) { + LocationRequestHelper old = null; + for (LocationRequestHelper req : currentRequests) { + if (req.respondsTo(request.pendingIntent) || req.respondsTo(request.listener) || req.respondsTo(request.callback)) { + old = req; + break; + } + } + if (old != null) { + currentRequests.remove(old); + } currentRequests.add(request); if (gpsProvider != null && request.hasFinePermission() && request.locationRequest.getPriority() == PRIORITY_HIGH_ACCURACY) { gpsProvider.addRequest(request); + } else if (gpsProvider != null && old != null) { + gpsProvider.removeRequest(old); } - if (networkProvider != null && request.hasCoarsePermission() && request.locationRequest.getPriority() != PRIORITY_NO_POWER) + if (networkProvider != null && request.hasCoarsePermission() && request.locationRequest.getPriority() != PRIORITY_NO_POWER) { networkProvider.addRequest(request); + } else if (networkProvider != null && old != null) { + networkProvider.removeRequest(old); + } } public void requestLocationUpdates(LocationRequest request, ILocationListener listener, String packageName) { @@ -162,12 +177,6 @@ public class GoogleLocationManager implements LocationChangeListener { packageName = PackageUtils.packageFromPendingIntent(data.pendingIntent); if (data.opCode == LocationRequestUpdateData.REQUEST_UPDATES) { requestLocationUpdates(new LocationRequestHelper(context, packageName, Binder.getCallingUid(), data)); - if (data.fusedLocationProviderCallback != null) { - try { - data.fusedLocationProviderCallback.onFusedLocationProviderResult(FusedLocationProviderResult.SUCCESS); - } catch (RemoteException ignored) { - } - } } else if (data.opCode == LocationRequestUpdateData.REMOVE_UPDATES) { for (int i = 0; i < currentRequests.size(); i++) { if (currentRequests.get(i).respondsTo(data.listener) @@ -178,6 +187,12 @@ public class GoogleLocationManager implements LocationChangeListener { } } } + if (data.fusedLocationProviderCallback != null) { + try { + data.fusedLocationProviderCallback.onFusedLocationProviderResult(FusedLocationProviderResult.SUCCESS); + } catch (RemoteException ignored) { + } + } } public void setMockMode(boolean mockMode) { diff --git a/play-services-location-core/src/main/java/org/microg/gms/location/LocationRequestHelper.java b/play-services-location-core/src/main/java/org/microg/gms/location/LocationRequestHelper.java index 4ba3ec5f..ad67ae31 100644 --- a/play-services-location-core/src/main/java/org/microg/gms/location/LocationRequestHelper.java +++ b/play-services-location-core/src/main/java/org/microg/gms/location/LocationRequestHelper.java @@ -53,9 +53,9 @@ public class LocationRequestHelper { public final String packageName; public final int uid; private final boolean selfHasAppOpsRights; - private ILocationListener listener; - private PendingIntent pendingIntent; - private ILocationCallback callback; + public ILocationListener listener; + public PendingIntent pendingIntent; + public ILocationCallback callback; private Location lastReport; private int numReports = 0; @@ -105,7 +105,7 @@ public class LocationRequestHelper { } lastReport = new Location(location); lastReport.setProvider("fused"); - Log.d(TAG, "sending Location: " + location); + Log.d(TAG, "sending Location: " + location + " to " + packageName); if (listener != null) { try { listener.onLocationChanged(lastReport); diff --git a/play-services-location-core/src/main/java/org/microg/gms/location/RealLocationProvider.java b/play-services-location-core/src/main/java/org/microg/gms/location/RealLocationProvider.java index 4958626d..0ae75148 100644 --- a/play-services-location-core/src/main/java/org/microg/gms/location/RealLocationProvider.java +++ b/play-services-location-core/src/main/java/org/microg/gms/location/RealLocationProvider.java @@ -87,6 +87,13 @@ public class RealLocationProvider { public void addRequest(LocationRequestHelper request) { Log.d(TAG, name + ": addRequest " + request); + for (int i = 0; i < requests.size(); i++) { + LocationRequestHelper req = requests.get(i); + if (req.respondsTo(request.pendingIntent) || req.respondsTo(request.listener) || req.respondsTo(request.callback)) { + requests.remove(i); + i--; + } + } requests.add(request); updateConnection(); } @@ -109,7 +116,7 @@ public class RealLocationProvider { for (LocationRequestHelper request : requests) { minTime = Math.min(request.locationRequest.getInterval(), minTime); minDistance = Math.min(request.locationRequest.getSmallestDesplacement(), minDistance); - if (sb.length() == 0) sb.append(", "); + if (sb.length() != 0) sb.append(", "); sb.append(request.packageName).append(":").append(request.locationRequest.getInterval()).append("ms"); } Log.d(TAG, name + ": requesting location updates with interval " + minTime + "ms (" + sb + "), minDistance=" + minDistance); diff --git a/play-services-location-core/src/main/java/org/microg/gms/location/UnifiedLocationProvider.kt b/play-services-location-core/src/main/java/org/microg/gms/location/UnifiedLocationProvider.kt index c62bdfb5..51cbfbf5 100644 --- a/play-services-location-core/src/main/java/org/microg/gms/location/UnifiedLocationProvider.kt +++ b/play-services-location-core/src/main/java/org/microg/gms/location/UnifiedLocationProvider.kt @@ -31,6 +31,13 @@ class UnifiedLocationProvider(context: Context?, changeListener: LocationChangeL fun addRequest(request: LocationRequestHelper) { Log.d(TAG, "unified network: addRequest $request") + for (i in 0..requests.size) { + if (i >= requests.size) break + val req = requests[i] + if (req.respondsTo(request.pendingIntent) || req.respondsTo(request.listener) || req.respondsTo(request.callback)) { + requests.removeAt(i) + } + } requests.add(request) updateConnection() }