-
Notifications
You must be signed in to change notification settings - Fork 70
Update engine's location requests #215
Changes from 1 commit
6deadaa
42b8e74
d0d1712
cf85c3f
c10762d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,10 @@ | |
|
||
import android.app.PendingIntent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Manages requests for {@link ClientCallbackWrapper}s so that the system can properly remove | ||
|
@@ -19,7 +19,7 @@ | |
public class LostRequestManager implements RequestManager { | ||
|
||
private static LostRequestManager instance; | ||
private Map<ClientCallbackWrapper, Set<LocationRequest>> clientCallbackToLocationRequests; | ||
private Map<ClientCallbackWrapper, List<LocationRequest>> clientCallbackToLocationRequests; | ||
|
||
LostRequestManager() { | ||
clientCallbackToLocationRequests = new HashMap<>(); | ||
|
@@ -50,19 +50,19 @@ public static LostRequestManager shared() { | |
registerRequest(wrapper, request); | ||
} | ||
|
||
@Override public Set<LocationRequest> removeLocationUpdates(LostApiClient client, | ||
@Override public List<LocationRequest> removeLocationUpdates(LostApiClient client, | ||
LocationListener listener) { | ||
ClientCallbackWrapper wrapper = getWrapper(client, listener); | ||
return getRequestOnlyUsedBy(wrapper); | ||
} | ||
|
||
@Override public Set<LocationRequest> removeLocationUpdates(LostApiClient client, | ||
@Override public List<LocationRequest> removeLocationUpdates(LostApiClient client, | ||
PendingIntent callbackIntent) { | ||
ClientCallbackWrapper wrapper = getWrapper(client, callbackIntent); | ||
return getRequestOnlyUsedBy(wrapper); | ||
} | ||
|
||
@Override public Set<LocationRequest> removeLocationUpdates(LostApiClient client, | ||
@Override public List<LocationRequest> removeLocationUpdates(LostApiClient client, | ||
LocationCallback callback) { | ||
ClientCallbackWrapper wrapper = getWrapper(client, callback); | ||
return getRequestOnlyUsedBy(wrapper); | ||
|
@@ -73,28 +73,25 @@ private <T> ClientCallbackWrapper getWrapper(LostApiClient client, T callback) { | |
} | ||
|
||
private void registerRequest(ClientCallbackWrapper wrapper, LocationRequest request) { | ||
Set<LocationRequest> requests = clientCallbackToLocationRequests.get(wrapper); | ||
List<LocationRequest> requests = clientCallbackToLocationRequests.get(wrapper); | ||
if (requests == null) { | ||
requests = new HashSet<>(); | ||
requests = new ArrayList(); | ||
clientCallbackToLocationRequests.put(wrapper, requests); | ||
} | ||
requests.add(request); | ||
LocationRequest r = new LocationRequest(request); | ||
requests.add(r); | ||
} | ||
|
||
private Set<LocationRequest> getRequestOnlyUsedBy(ClientCallbackWrapper wrapper) { | ||
Set<LocationRequest> requestsToRemove = clientCallbackToLocationRequests.get(wrapper); | ||
private List<LocationRequest> getRequestOnlyUsedBy(ClientCallbackWrapper wrapper) { | ||
List<LocationRequest> requestsToRemove = clientCallbackToLocationRequests.get(wrapper); | ||
if (requestsToRemove == null) { | ||
return requestsToRemove; | ||
return null; | ||
} | ||
clientCallbackToLocationRequests.remove(wrapper); | ||
for (ClientCallbackWrapper w : clientCallbackToLocationRequests.keySet()) { | ||
Set<LocationRequest> requests = clientCallbackToLocationRequests.get(w); | ||
requestsToRemove.removeAll(requests); | ||
} | ||
return requestsToRemove; | ||
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. I guess we can't rule out the possibility of more than one client registering the same request but that seems kind of gross. What if a 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. I'm not sure this would simplify the logic because I'd still have to iterate over the request to unique id map to ensure the request id was the one corresponding to the client wrapper I wanted to remove requests for. I would also have to add a step to update request ids for remaining client wrappers after this step. Am I missing something though? Is this how you imagine it be used? 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. Mainly I was looking for a way to not have to search all other clients to see if another one is using any of the What if we just made a copy of each incoming request so two clients could have equal requests without it being the same instance? |
||
} | ||
|
||
Map<ClientCallbackWrapper, Set<LocationRequest>> getClientCallbackMap() { | ||
Map<ClientCallbackWrapper, List<LocationRequest>> getClientCallbackMap() { | ||
return clientCallbackToLocationRequests; | ||
} | ||
} |
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.
This is great should really cut down on thrashing of the
LocationManager
listeners when multiple requests are removed!