Ensure to clean up location requests on update

This commit is contained in:
Marvin W 2020-07-09 11:01:38 +02:00
parent 5146559f89
commit ac5f7457bd
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
5 changed files with 44 additions and 15 deletions

View File

@ -56,10 +56,10 @@ public class LocationRequestUpdateData extends AutoSafeParcelable {
return "LocationRequestUpdateData{" + return "LocationRequestUpdateData{" +
"opCode=" + opCode + "opCode=" + opCode +
", request=" + request + ", request=" + request +
", listener=" + listener + ", listener=" + (listener != null ? listener.asBinder() : null) +
", pendingIntent=" + pendingIntent + ", pendingIntent=" + pendingIntent +
", callback=" + callback + ", callback=" + (callback != null ? callback.asBinder() : null) +
", fusedLocationProviderCallback=" + fusedLocationProviderCallback + ", fusedLocationProviderCallback=" + (fusedLocationProviderCallback != null ? fusedLocationProviderCallback.asBinder() : null) +
'}'; '}';
} }

View File

@ -116,12 +116,27 @@ public class GoogleLocationManager implements LocationChangeListener {
} }
private void requestLocationUpdates(LocationRequestHelper request) { 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); currentRequests.add(request);
if (gpsProvider != null && request.hasFinePermission() && request.locationRequest.getPriority() == PRIORITY_HIGH_ACCURACY) { if (gpsProvider != null && request.hasFinePermission() && request.locationRequest.getPriority() == PRIORITY_HIGH_ACCURACY) {
gpsProvider.addRequest(request); 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); networkProvider.addRequest(request);
} else if (networkProvider != null && old != null) {
networkProvider.removeRequest(old);
}
} }
public void requestLocationUpdates(LocationRequest request, ILocationListener listener, String packageName) { public void requestLocationUpdates(LocationRequest request, ILocationListener listener, String packageName) {
@ -162,12 +177,6 @@ public class GoogleLocationManager implements LocationChangeListener {
packageName = PackageUtils.packageFromPendingIntent(data.pendingIntent); packageName = PackageUtils.packageFromPendingIntent(data.pendingIntent);
if (data.opCode == LocationRequestUpdateData.REQUEST_UPDATES) { if (data.opCode == LocationRequestUpdateData.REQUEST_UPDATES) {
requestLocationUpdates(new LocationRequestHelper(context, packageName, Binder.getCallingUid(), data)); 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) { } else if (data.opCode == LocationRequestUpdateData.REMOVE_UPDATES) {
for (int i = 0; i < currentRequests.size(); i++) { for (int i = 0; i < currentRequests.size(); i++) {
if (currentRequests.get(i).respondsTo(data.listener) 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) { public void setMockMode(boolean mockMode) {

View File

@ -53,9 +53,9 @@ public class LocationRequestHelper {
public final String packageName; public final String packageName;
public final int uid; public final int uid;
private final boolean selfHasAppOpsRights; private final boolean selfHasAppOpsRights;
private ILocationListener listener; public ILocationListener listener;
private PendingIntent pendingIntent; public PendingIntent pendingIntent;
private ILocationCallback callback; public ILocationCallback callback;
private Location lastReport; private Location lastReport;
private int numReports = 0; private int numReports = 0;
@ -105,7 +105,7 @@ public class LocationRequestHelper {
} }
lastReport = new Location(location); lastReport = new Location(location);
lastReport.setProvider("fused"); lastReport.setProvider("fused");
Log.d(TAG, "sending Location: " + location); Log.d(TAG, "sending Location: " + location + " to " + packageName);
if (listener != null) { if (listener != null) {
try { try {
listener.onLocationChanged(lastReport); listener.onLocationChanged(lastReport);

View File

@ -87,6 +87,13 @@ public class RealLocationProvider {
public void addRequest(LocationRequestHelper request) { public void addRequest(LocationRequestHelper request) {
Log.d(TAG, name + ": addRequest " + 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); requests.add(request);
updateConnection(); updateConnection();
} }
@ -109,7 +116,7 @@ public class RealLocationProvider {
for (LocationRequestHelper request : requests) { for (LocationRequestHelper request : requests) {
minTime = Math.min(request.locationRequest.getInterval(), minTime); minTime = Math.min(request.locationRequest.getInterval(), minTime);
minDistance = Math.min(request.locationRequest.getSmallestDesplacement(), minDistance); 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"); 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); Log.d(TAG, name + ": requesting location updates with interval " + minTime + "ms (" + sb + "), minDistance=" + minDistance);

View File

@ -31,6 +31,13 @@ class UnifiedLocationProvider(context: Context?, changeListener: LocationChangeL
fun addRequest(request: LocationRequestHelper) { fun addRequest(request: LocationRequestHelper) {
Log.d(TAG, "unified network: addRequest $request") 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) requests.add(request)
updateConnection() updateConnection()
} }