This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* throw IllegalStateException * move service callback code into separate class & add tests * add back final * add documentation
- Loading branch information
1 parent
ce4bf79
commit f2757a6
Showing
6 changed files
with
207 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
68 changes: 68 additions & 0 deletions
68
lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationServiceCallbackManager.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.mapzen.android.lost.internal; | ||
|
||
import com.mapzen.android.lost.api.LocationAvailability; | ||
import com.mapzen.android.lost.api.LocationResult; | ||
|
||
import android.content.Context; | ||
import android.location.Location; | ||
import android.os.RemoteException; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Handles callbacks received in {@link FusedLocationProviderApiImpl} from | ||
* {@link FusedLocationProviderService}. | ||
*/ | ||
public class FusedLocationServiceCallbackManager { | ||
|
||
/** | ||
* Called when a new location has been received. This method handles dispatching changes to all | ||
* {@link com.mapzen.android.lost.api.LocationListener}s, {@link android.app.PendingIntent}s, and | ||
* {@link com.mapzen.android.lost.api.LocationCallback}s which are registered. If the | ||
* {@link IFusedLocationProviderService} is null, an {@link IllegalStateException} will be thrown. | ||
* @param context | ||
* @param location | ||
* @param clientManager | ||
* @param service | ||
*/ | ||
void onLocationChanged(Context context, Location location, LostClientManager clientManager, | ||
IFusedLocationProviderService service) { | ||
if (service == null) { | ||
throw new IllegalStateException("Location update received after client was " | ||
+ "disconnected. Did you forget to unregister location updates before " | ||
+ "disconnecting?"); | ||
} | ||
|
||
ReportedChanges changes = clientManager.reportLocationChanged(location); | ||
|
||
LocationAvailability availability; | ||
try { | ||
availability = service.getLocationAvailability(); | ||
} catch (RemoteException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
ArrayList<Location> locations = new ArrayList<>(); | ||
locations.add(location); | ||
final LocationResult result = LocationResult.create(locations); | ||
ReportedChanges pendingIntentChanges = clientManager.sendPendingIntent( | ||
context, location, availability, result); | ||
|
||
ReportedChanges callbackChanges = clientManager.reportLocationResult(location, result); | ||
|
||
changes.putAll(pendingIntentChanges); | ||
changes.putAll(callbackChanges); | ||
clientManager.updateReportedValues(changes); | ||
} | ||
|
||
/** | ||
* Handles notifying all registered {@link LocationCallback}s that {@link LocationAvailability} | ||
* has changed. | ||
* @param locationAvailability | ||
* @param clientManager | ||
*/ | ||
void onLocationAvailabilityChanged(LocationAvailability locationAvailability, | ||
LostClientManager clientManager) { | ||
clientManager.notifyLocationAvailability(locationAvailability); | ||
} | ||
} |
This file contains 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
91 changes: 91 additions & 0 deletions
91
...c/test/java/com/mapzen/android/lost/internal/FusedLocationServiceCallbackManagerTest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.mapzen.android.lost.internal; | ||
|
||
import com.mapzen.android.lost.api.LocationAvailability; | ||
import com.mapzen.android.lost.api.LocationResult; | ||
|
||
import org.junit.Test; | ||
|
||
import android.content.Context; | ||
import android.location.Location; | ||
import android.os.RemoteException; | ||
|
||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class FusedLocationServiceCallbackManagerTest { | ||
|
||
FusedLocationServiceCallbackManager callbackManager = | ||
new FusedLocationServiceCallbackManager(); | ||
|
||
@Test(expected = IllegalStateException.class) | ||
public void onLocationChanged_shouldThrowIfServiceDisconnected() { | ||
callbackManager.onLocationChanged(mock(Context.class), mock(Location.class), | ||
mock(LostClientManager.class), null); | ||
} | ||
|
||
@Test public void onLocationChanged_shouldReportLocationChanged() { | ||
LostClientManager clientManager = mock(LostClientManager.class); | ||
Location location = mock(Location.class); | ||
when(clientManager.reportLocationChanged(any(Location.class))). | ||
thenReturn(mock(ReportedChanges.class)); | ||
callbackManager.onLocationChanged(mock(Context.class), location, clientManager, | ||
mock(IFusedLocationProviderService.class)); | ||
verify(clientManager).reportLocationChanged(location); | ||
} | ||
|
||
@Test public void onLocationChanged_shouldSendPendingIntent() { | ||
LostClientManager clientManager = mock(LostClientManager.class); | ||
when(clientManager.reportLocationChanged(any(Location.class))). | ||
thenReturn(mock(ReportedChanges.class)); | ||
IFusedLocationProviderService service = mock(IFusedLocationProviderService.class); | ||
LocationAvailability locationAvailability = mock(LocationAvailability.class); | ||
try { | ||
when(service.getLocationAvailability()).thenReturn(locationAvailability); | ||
} catch (RemoteException e) { | ||
e.printStackTrace(); | ||
} | ||
Context context = mock(Context.class); | ||
Location location = mock(Location.class); | ||
callbackManager.onLocationChanged(context, location, clientManager, service); | ||
verify(clientManager).sendPendingIntent(eq(context), eq(location), eq(locationAvailability), | ||
any(LocationResult.class)); | ||
} | ||
|
||
@Test public void onLocationChanged_shouldReportLocationResult() { | ||
LostClientManager clientManager = mock(LostClientManager.class); | ||
when(clientManager.reportLocationChanged(any(Location.class))). | ||
thenReturn(mock(ReportedChanges.class)); | ||
IFusedLocationProviderService service = mock(IFusedLocationProviderService.class); | ||
Location location = mock(Location.class); | ||
callbackManager.onLocationChanged(mock(Context.class), location, clientManager, service); | ||
verify(clientManager).reportLocationResult(eq(location), any(LocationResult.class)); | ||
} | ||
|
||
@Test public void onLocationChanged_shouldUpdateReportedValues() { | ||
LostClientManager clientManager = mock(LostClientManager.class); | ||
ReportedChanges changes = mock(ReportedChanges.class); | ||
when(clientManager.reportLocationChanged(any(Location.class))).thenReturn(changes); | ||
ReportedChanges pendingIntentChanges = mock(ReportedChanges.class); | ||
when(clientManager.sendPendingIntent(any(Context.class), any(Location.class), | ||
any(LocationAvailability.class), any(LocationResult.class))).thenReturn( | ||
pendingIntentChanges); | ||
ReportedChanges callbackChanges = mock(ReportedChanges.class); | ||
when(clientManager.reportLocationResult(any(Location.class), any(LocationResult.class))). | ||
thenReturn(callbackChanges); | ||
callbackManager.onLocationChanged(mock(Context.class), mock(Location.class), clientManager, | ||
mock(IFusedLocationProviderService.class)); | ||
verify(changes).putAll(pendingIntentChanges); | ||
verify(changes).putAll(callbackChanges); | ||
verify(clientManager).updateReportedValues(changes); | ||
} | ||
|
||
@Test public void onLocationAvailabilityChanged_shouldNotifyLocationAvailability() { | ||
LostClientManager clientManager = mock(LostClientManager.class); | ||
LocationAvailability locationAvailability = mock(LocationAvailability.class); | ||
callbackManager.onLocationAvailabilityChanged(locationAvailability, clientManager); | ||
verify(clientManager).notifyLocationAvailability(locationAvailability); | ||
} | ||
} |