diff --git a/lost-sample/src/main/AndroidManifest.xml b/lost-sample/src/main/AndroidManifest.xml index 7a64379..edfe6c9 100644 --- a/lost-sample/src/main/AndroidManifest.xml +++ b/lost-sample/src/main/AndroidManifest.xml @@ -38,6 +38,7 @@ + - + diff --git a/lost-sample/src/main/java/com/example/lost/ListenerService.java b/lost-sample/src/main/java/com/example/lost/ListenerService.java new file mode 100644 index 0000000..7118819 --- /dev/null +++ b/lost-sample/src/main/java/com/example/lost/ListenerService.java @@ -0,0 +1,57 @@ +package com.example.lost; + +import com.mapzen.android.lost.api.LocationListener; +import com.mapzen.android.lost.api.LocationRequest; +import com.mapzen.android.lost.api.LocationServices; +import com.mapzen.android.lost.api.LostApiClient; + +import android.app.Service; +import android.content.Intent; +import android.location.Location; +import android.os.IBinder; +import android.support.annotation.Nullable; +import android.util.Log; + +/** + * Service running in separate process to request location updates. + */ +public class ListenerService extends Service { + + private static final String TAG = ListenerService.class.getSimpleName(); + + LostApiClient client; + LocationListener listener = new LocationListener() { + @Override public void onLocationChanged(Location location) { + Log.d(TAG, "Location Changed"); + } + }; + + @Nullable @Override public IBinder onBind(Intent intent) { + return null; + } + + @Override public void onCreate() { + super.onCreate(); + client = new LostApiClient.Builder(this).addConnectionCallbacks( + new LostApiClient.ConnectionCallbacks() { + @Override public void onConnected() { + LocationRequest request = LocationRequest.create(); + request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + request.setInterval(100); + + LocationServices.FusedLocationApi.requestLocationUpdates(client, request, listener); + } + + @Override public void onConnectionSuspended() { + + } + }).build(); + client.connect(); + } + + @Override public void onDestroy() { + super.onDestroy(); + LocationServices.FusedLocationApi.removeLocationUpdates(client, listener); + client.disconnect(); + } +} diff --git a/lost-sample/src/main/java/com/example/lost/MultipleProcessesActivity.java b/lost-sample/src/main/java/com/example/lost/MultipleProcessesActivity.java new file mode 100644 index 0000000..37b5bd9 --- /dev/null +++ b/lost-sample/src/main/java/com/example/lost/MultipleProcessesActivity.java @@ -0,0 +1,51 @@ +package com.example.lost; + +import com.mapzen.android.lost.api.LocationListener; +import com.mapzen.android.lost.api.LocationRequest; +import com.mapzen.android.lost.api.LocationServices; + +import android.content.Intent; +import android.location.Location; +import android.util.Log; +import android.widget.Toast; + +/** + * Demonstrates two different processes requesting location updates. + */ +public class MultipleProcessesActivity extends PendingIntentActivity { + + private static final String TAG = MultipleProcessesActivity.class.getSimpleName(); + + LocationListener listener = new LocationListener() { + @Override public void onLocationChanged(Location location) { + Log.d(TAG, "Location Changed"); + } + }; + + @Override protected void onResume() { + super.onResume(); + Intent intent = new Intent(this, ListenerService.class); + startService(intent); + } + + @Override protected void onPause() { + super.onPause(); + Intent intent = new Intent(this, ListenerService.class); + stopService(intent); + } + + void requestLocationUpdates() { + LocationRequest request = LocationRequest.create(); + request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + request.setInterval(100); + + LocationServices.FusedLocationApi.requestLocationUpdates(client, request, listener); + + Toast.makeText(this, R.string.requested, Toast.LENGTH_SHORT).show(); + } + + void unregisterAndDisconnectClient() { + LocationServices.FusedLocationApi.removeLocationUpdates(client, listener); + disconnect(); + } +} diff --git a/lost-sample/src/main/java/com/example/lost/PendingIntentActivity.java b/lost-sample/src/main/java/com/example/lost/PendingIntentActivity.java index 18f2cb1..74ffef8 100644 --- a/lost-sample/src/main/java/com/example/lost/PendingIntentActivity.java +++ b/lost-sample/src/main/java/com/example/lost/PendingIntentActivity.java @@ -49,13 +49,12 @@ private void setupDisconnectBtn() { Button disconnect = (Button) findViewById(R.id.disconnect); disconnect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - LocationServices.FusedLocationApi.removeLocationUpdates(client, pendingIntent); - disconnect(); + unregisterAndDisconnectClient(); } }); } - private void requestLocationUpdates() { + void requestLocationUpdates() { LocationRequest request = LocationRequest.create(); request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); request.setInterval(100); @@ -67,4 +66,9 @@ private void requestLocationUpdates() { Toast.makeText(PendingIntentActivity.this, R.string.requested, Toast.LENGTH_SHORT).show(); } + + void unregisterAndDisconnectClient() { + LocationServices.FusedLocationApi.removeLocationUpdates(client, pendingIntent); + disconnect(); + } } diff --git a/lost-sample/src/main/java/com/example/lost/SamplesList.java b/lost-sample/src/main/java/com/example/lost/SamplesList.java index 491c79b..9653776 100644 --- a/lost-sample/src/main/java/com/example/lost/SamplesList.java +++ b/lost-sample/src/main/java/com/example/lost/SamplesList.java @@ -28,6 +28,8 @@ private SamplesList() { MultipleLocationListenerSingleClientActivity.class), new Sample(R.string.sample_pending_intent_title, R.string.sample_pending_intent_description, PendingIntentActivity.class), + new Sample(R.string.sample_multiple_process_title, + R.string.sample_multiple_process_description, MultipleProcessesActivity.class), new Sample(R.string.sample_location_availability_title, R.string.sample_location_availability_description, LocationAvailabilityActivity.class) }; diff --git a/lost-sample/src/main/res/values/strings.xml b/lost-sample/src/main/res/values/strings.xml index 745d9cd..8932ebb 100644 --- a/lost-sample/src/main/res/values/strings.xml +++ b/lost-sample/src/main/res/values/strings.xml @@ -82,5 +82,7 @@ Demonstrates how a single client can request location updates at different fastest intervals Multiple Clients w/different request priorities Demonstrates multiple clients receiving location requests for different location priorities + Multiple processes w/different clients + Demonstrates using lost in the main app process as well as in a service running in its own process diff --git a/lost/src/main/aidl/com/mapzen/android/lost/internal/IFusedLocationProviderCallback.aidl b/lost/src/main/aidl/com/mapzen/android/lost/internal/IFusedLocationProviderCallback.aidl index 9cb989e..86cfcb1 100644 --- a/lost/src/main/aidl/com/mapzen/android/lost/internal/IFusedLocationProviderCallback.aidl +++ b/lost/src/main/aidl/com/mapzen/android/lost/internal/IFusedLocationProviderCallback.aidl @@ -4,6 +4,8 @@ import com.mapzen.android.lost.api.LocationAvailability; interface IFusedLocationProviderCallback { + long pid(); + void onLocationChanged(in Location location); void onLocationAvailabilityChanged(in LocationAvailability locationAvailability); diff --git a/lost/src/main/aidl/com/mapzen/android/lost/internal/IFusedLocationProviderService.aidl b/lost/src/main/aidl/com/mapzen/android/lost/internal/IFusedLocationProviderService.aidl index fc0292c..d6ba982 100644 --- a/lost/src/main/aidl/com/mapzen/android/lost/internal/IFusedLocationProviderService.aidl +++ b/lost/src/main/aidl/com/mapzen/android/lost/internal/IFusedLocationProviderService.aidl @@ -6,7 +6,9 @@ import com.mapzen.android.lost.internal.IFusedLocationProviderCallback; interface IFusedLocationProviderService { - void init(in IFusedLocationProviderCallback callback); + void add(in IFusedLocationProviderCallback callback); + + void remove(in IFusedLocationProviderCallback callback); Location getLastLocation(); diff --git a/lost/src/main/java/com/mapzen/android/lost/api/LocationRequest.java b/lost/src/main/java/com/mapzen/android/lost/api/LocationRequest.java index 12bb2fc..907ae0e 100644 --- a/lost/src/main/java/com/mapzen/android/lost/api/LocationRequest.java +++ b/lost/src/main/java/com/mapzen/android/lost/api/LocationRequest.java @@ -1,8 +1,12 @@ package com.mapzen.android.lost.api; +import com.mapzen.android.lost.internal.PidReader; + import android.os.Parcel; import android.os.Parcelable; +import static android.os.Process.myPid; + public final class LocationRequest implements Parcelable { public static final int PRIORITY_HIGH_ACCURACY = 0x00000064; public static final int PRIORITY_BALANCED_POWER_ACCURACY = 0x00000066; @@ -17,19 +21,40 @@ public final class LocationRequest implements Parcelable { private long fastestInterval = DEFAULT_FASTEST_INTERVAL_IN_MS; private float smallestDisplacement = DEFAULT_SMALLEST_DISPLACEMENT_IN_METERS; private int priority = PRIORITY_BALANCED_POWER_ACCURACY; + private PidReader pidReader = new PidReader() { + @Override public long getPid() { + return myPid(); + } + }; + long pid; private LocationRequest() { + commonInit(); + } + + private LocationRequest(PidReader reader) { + pidReader = reader; + commonInit(); + } + + private void commonInit() { + pid = pidReader.getPid(); } public static LocationRequest create() { return new LocationRequest(); } + public static LocationRequest create(PidReader reader) { + return new LocationRequest(reader); + } + public LocationRequest(LocationRequest incoming) { this.setInterval(incoming.getInterval()); this.setFastestInterval(incoming.getFastestInterval()); this.setSmallestDisplacement(incoming.getSmallestDisplacement()); this.setPriority(incoming.getPriority()); + this.pid = incoming.pid; } public long getInterval() { @@ -90,6 +115,9 @@ public LocationRequest setPriority(int priority) { LocationRequest that = (LocationRequest) o; + if (pid != that.pid) { + return false; + } if (interval != that.interval) { return false; } @@ -109,6 +137,7 @@ public LocationRequest setPriority(int priority) { 31 * result + (smallestDisplacement != +0.0f ? Float.floatToIntBits(smallestDisplacement) : 0); result = 31 * result + priority; + result = 31 * result + (int) pid; return result; } @@ -121,6 +150,7 @@ public LocationRequest setPriority(int priority) { dest.writeLong(this.fastestInterval); dest.writeFloat(this.smallestDisplacement); dest.writeInt(this.priority); + dest.writeLong(this.pid); } protected LocationRequest(Parcel in) { @@ -128,6 +158,7 @@ protected LocationRequest(Parcel in) { this.fastestInterval = in.readLong(); this.smallestDisplacement = in.readFloat(); this.priority = in.readInt(); + this.pid = in.readLong(); } public static final Parcelable.Creator CREATOR = diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderApiImpl.java b/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderApiImpl.java index 958a0b6..84798a3 100644 --- a/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderApiImpl.java +++ b/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderApiImpl.java @@ -26,6 +26,8 @@ import java.util.Map; import java.util.Set; +import static android.os.Process.myPid; + /** * Implementation of the {@link FusedLocationProviderApi}. */ @@ -41,8 +43,13 @@ public class FusedLocationProviderApiImpl extends ApiImpl IFusedLocationProviderService service; - private IFusedLocationProviderCallback.Stub remoteCallback + IFusedLocationProviderCallback.Stub remoteCallback = new IFusedLocationProviderCallback.Stub() { + + public long pid() throws RemoteException { + return myPid(); + } + public void onLocationChanged(final Location location) throws RemoteException { new Handler(Looper.getMainLooper()).post(new Runnable() { @@ -295,7 +302,7 @@ FusedLocationServiceConnectionManager getServiceConnectionManager() { void registerRemoteCallback() { if (service != null) { try { - service.init(remoteCallback); + service.add(remoteCallback); } catch (RemoteException e) { Log.e(TAG, "Error occurred trying to register remote callback", e); } @@ -305,7 +312,7 @@ void registerRemoteCallback() { void unregisterRemoteCallback() { if (service != null) { try { - service.init(null); + service.remove(remoteCallback); } catch (RemoteException e) { Log.e(TAG, "Error occurred trying to unregister remote callback", e); } diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderService.java b/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderService.java index 518e850..3e05f9c 100644 --- a/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderService.java +++ b/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderService.java @@ -21,8 +21,13 @@ public class FusedLocationProviderService extends Service { private final IFusedLocationProviderService.Stub binder = new IFusedLocationProviderService.Stub() { - @Override public void init(IFusedLocationProviderCallback callback) throws RemoteException { - delegate.init(callback); + @Override public void add(IFusedLocationProviderCallback callback) throws RemoteException { + delegate.add(callback); + } + + @Override public void remove(IFusedLocationProviderCallback callback) throws + RemoteException { + delegate.remove(callback); } @Override public Location getLastLocation() throws RemoteException { diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderServiceDelegate.java b/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderServiceDelegate.java index f742cef..8381d83 100644 --- a/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderServiceDelegate.java +++ b/lost/src/main/java/com/mapzen/android/lost/internal/FusedLocationProviderServiceDelegate.java @@ -10,10 +10,13 @@ import android.os.Looper; import android.os.RemoteException; import android.support.annotation.RequiresPermission; +import android.support.annotation.VisibleForTesting; import android.util.Log; import java.io.File; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static android.Manifest.permission.ACCESS_COARSE_LOCATION; import static android.Manifest.permission.ACCESS_FINE_LOCATION; @@ -25,15 +28,28 @@ public class FusedLocationProviderServiceDelegate implements LocationEngine.Call private boolean mockMode; private LocationEngine locationEngine; - private IFusedLocationProviderCallback callback; + private Map callbacks; public FusedLocationProviderServiceDelegate(Context context) { this.context = context; locationEngine = new FusionEngine(context, this); + callbacks = new HashMap<>(); } - public void init(IFusedLocationProviderCallback callback) { - this.callback = callback; + public void add(IFusedLocationProviderCallback callback) { + try { + callbacks.put(callback.pid(), callback); + } catch (RemoteException e) { + Log.e(TAG, "Error getting callback's unique id", e); + } + } + + public void remove(IFusedLocationProviderCallback callback) { + try { + callbacks.remove(callback.pid()); + } catch (RemoteException e) { + Log.e(TAG, "Error getting callback's unique id", e); + } } public Location getLastLocation() { @@ -74,7 +90,7 @@ public void setMockTrace(String path, String filename) { @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) public void reportLocation(Location location) { // Notify remote AIDL callback - if (callback != null) { + for (IFusedLocationProviderCallback callback : callbacks.values()) { try { callback.onLocationChanged(location); } catch (RemoteException e) { @@ -128,7 +144,7 @@ private void toggleMockMode() { @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) private void notifyLocationAvailabilityChanged() { final LocationAvailability availability = locationEngine.createLocationAvailability(); - if (callback != null) { + for (IFusedLocationProviderCallback callback : callbacks.values()) { try { callback.onLocationAvailabilityChanged(availability); } catch (RemoteException e) { @@ -136,4 +152,9 @@ private void notifyLocationAvailabilityChanged() { } } } + + @VisibleForTesting + Map getCallbacks() { + return callbacks; + } } diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/LocationEngine.java b/lost/src/main/java/com/mapzen/android/lost/internal/LocationEngine.java index e4641b8..b240352 100644 --- a/lost/src/main/java/com/mapzen/android/lost/internal/LocationEngine.java +++ b/lost/src/main/java/com/mapzen/android/lost/internal/LocationEngine.java @@ -51,7 +51,7 @@ public void addRequest(LocationRequest request) { /** * Disables the engine when no requests remain, otherwise updates the engine's configuration. * - * @param request Valid location request to enable. + * @param requests Valid location request to enable. */ public void removeRequests(List requests) { if (request != null) { diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/PidReader.java b/lost/src/main/java/com/mapzen/android/lost/internal/PidReader.java new file mode 100644 index 0000000..024a9ee --- /dev/null +++ b/lost/src/main/java/com/mapzen/android/lost/internal/PidReader.java @@ -0,0 +1,12 @@ +package com.mapzen.android.lost.internal; + +/** + * Generic interface for returning process id. + */ +public interface PidReader { + /** + * Gets the process id. + * @return + */ + long getPid(); +} diff --git a/lost/src/test/java/com/mapzen/android/lost/api/LocationRequestTest.java b/lost/src/test/java/com/mapzen/android/lost/api/LocationRequestTest.java index ea25b90..346e763 100644 --- a/lost/src/test/java/com/mapzen/android/lost/api/LocationRequestTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/api/LocationRequestTest.java @@ -94,5 +94,6 @@ public void setPriority_shouldRejectInvalidValues() throws Exception { assertThat(rehydrated.getFastestInterval()).isEqualTo(dehydrated.getFastestInterval()); assertThat(rehydrated.getSmallestDisplacement()) .isEqualTo(dehydrated.getSmallestDisplacement()); + assertThat(rehydrated.pid).isEqualTo(dehydrated.pid); } } diff --git a/lost/src/test/java/com/mapzen/android/lost/api/LocationSettingsRequestTest.java b/lost/src/test/java/com/mapzen/android/lost/api/LocationSettingsRequestTest.java index ae514d8..9d9c292 100644 --- a/lost/src/test/java/com/mapzen/android/lost/api/LocationSettingsRequestTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/api/LocationSettingsRequestTest.java @@ -1,5 +1,7 @@ package com.mapzen.android.lost.api; +import com.mapzen.android.lost.internal.TestPidReader; + import org.junit.Before; import org.junit.Test; @@ -15,13 +17,13 @@ public class LocationSettingsRequestTest { @Before public void setup() { requests = new ArrayList<>(); - LocationRequest highAccuracy = LocationRequest.create().setPriority( + LocationRequest highAccuracy = LocationRequest.create(new TestPidReader()).setPriority( LocationRequest.PRIORITY_HIGH_ACCURACY); //gps + wifi - LocationRequest balancedPowerAccuracy = LocationRequest.create().setPriority( + LocationRequest balancedPowerAccuracy = LocationRequest.create(new TestPidReader()).setPriority( LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); // wifi - LocationRequest lowPower = LocationRequest.create().setPriority( + LocationRequest lowPower = LocationRequest.create(new TestPidReader()).setPriority( LocationRequest.PRIORITY_LOW_POWER); // wifi - LocationRequest noPower = LocationRequest.create().setPriority( + LocationRequest noPower = LocationRequest.create(new TestPidReader()).setPriority( LocationRequest.PRIORITY_NO_POWER); // gps or wifi or none requests.add(highAccuracy); requests.add(balancedPowerAccuracy); diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/FusedLocationProviderApiImplTest.java b/lost/src/test/java/com/mapzen/android/lost/internal/FusedLocationProviderApiImplTest.java index 8e594fd..1969507 100644 --- a/lost/src/test/java/com/mapzen/android/lost/internal/FusedLocationProviderApiImplTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/internal/FusedLocationProviderApiImplTest.java @@ -284,7 +284,7 @@ public void setMockTrace_shouldThrowIfNotConnected() throws Exception { @Test public void onServiceConnected_shouldRegisterServiceCallbacks() throws Exception { TestServiceStub binder = new TestServiceStub(); api.onServiceConnected(binder); - assertThat(binder.callback).isNotNull(); + assertThat(binder.callbacks).isNotEmpty(); } @Test public void onDisconnect_shouldUnbindServiceIfBound() throws Exception { @@ -314,7 +314,7 @@ public void setMockTrace_shouldThrowIfNotConnected() throws Exception { Context context = mock(Context.class); api.onConnect(context); api.onDisconnect(); - verify(service).init(null); + verify(service).remove(api.remoteCallback); } @Test public void removeLocationUpdates_shouldReturnStatusSuccessIfListenerRemoved() { diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/FusedLocationProviderServiceDelegateTest.java b/lost/src/test/java/com/mapzen/android/lost/internal/FusedLocationProviderServiceDelegateTest.java index 8b21b44..853aac1 100644 --- a/lost/src/test/java/com/mapzen/android/lost/internal/FusedLocationProviderServiceDelegateTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/internal/FusedLocationProviderServiceDelegateTest.java @@ -87,7 +87,7 @@ public class FusedLocationProviderServiceDelegateTest extends BaseRobolectricTes @Test public void requestLocationUpdates_shouldNotifyOnLocationChangedGps() throws Exception { TestFusedLocationProviderCallback callback = new TestFusedLocationProviderCallback(); - delegate.init(callback); + delegate.add(callback); delegate.requestLocationUpdates(LocationRequest.create().setPriority(PRIORITY_HIGH_ACCURACY)); Location location = new Location(GPS_PROVIDER); @@ -97,7 +97,7 @@ public class FusedLocationProviderServiceDelegateTest extends BaseRobolectricTes @Test public void requestLocationUpdates_shouldNotifyOnLocationChangedNetwork() throws Exception { TestFusedLocationProviderCallback callback = new TestFusedLocationProviderCallback(); - delegate.init(callback); + delegate.add(callback); delegate.requestLocationUpdates(LocationRequest.create().setPriority(PRIORITY_HIGH_ACCURACY)); Location location = new Location(NETWORK_PROVIDER); @@ -107,7 +107,7 @@ public class FusedLocationProviderServiceDelegateTest extends BaseRobolectricTes @Test public void requestLocationUpdates_shouldPreferGpsIfMoreAccurate() throws Exception { TestFusedLocationProviderCallback callback = new TestFusedLocationProviderCallback(); - delegate.init(callback); + delegate.add(callback); delegate.requestLocationUpdates(LocationRequest.create().setPriority(PRIORITY_HIGH_ACCURACY)); Location gpsLocation = getTestLocation(GPS_PROVIDER, 0, 0, 0); @@ -122,7 +122,7 @@ public class FusedLocationProviderServiceDelegateTest extends BaseRobolectricTes @Test public void requestLocationUpdates_shouldPreferNetworkIfMoreAccurate() throws Exception { TestFusedLocationProviderCallback callback = new TestFusedLocationProviderCallback(); - delegate.init(callback); + delegate.add(callback); delegate.requestLocationUpdates(LocationRequest.create().setPriority(PRIORITY_HIGH_ACCURACY)); Location gpsLocation = getTestLocation(GPS_PROVIDER, 0, 0, 0); @@ -177,7 +177,7 @@ public class FusedLocationProviderServiceDelegateTest extends BaseRobolectricTes @Test public void setMockLocation_shouldCallbackOnce() throws Exception { IFusedLocationProviderCallback callback = mock(IFusedLocationProviderCallback.class); - delegate.init(callback); + delegate.add(callback); Location mockLocation = new Location("mock"); delegate.setMockMode(true); TestLocationListener listener = new TestLocationListener(); @@ -278,7 +278,7 @@ private void initTestGpxTrace() throws IOException { @Test public void requestLocationUpdates_shouldReportAvailability() { TestFusedLocationProviderCallback callback = new TestFusedLocationProviderCallback(); - delegate.init(callback); + delegate.add(callback); Location location = new Location("test"); shadowLocationManager.setLastKnownLocation(NETWORK_PROVIDER, location); LocationRequest request = LocationRequest.create(); @@ -353,11 +353,24 @@ private void initTestGpxTrace() throws IOException { @Test public void reportProviderEnabled_shouldNotifyAvailabilityChanged() throws Exception { TestFusedLocationProviderCallback callback = new TestFusedLocationProviderCallback(); - delegate.init(callback); + delegate.add(callback); delegate.reportProviderEnabled(GPS_PROVIDER); assertThat(callback.locationAvailability).isNotNull(); } + @Test public void addCallback_shouldIncreaseCallbacksCount() { + TestFusedLocationProviderCallback callback = new TestFusedLocationProviderCallback(); + delegate.add(callback); + assertThat(delegate.getCallbacks().size()).isEqualTo(1); + } + + @Test public void removeCallback_shouldDecreaseCallbacksCount() { + TestFusedLocationProviderCallback callback = new TestFusedLocationProviderCallback(); + delegate.add(callback); + delegate.remove(callback); + assertThat(delegate.getCallbacks().size()).isEqualTo(0); + } + public class TestService extends IntentService { public TestService() { super("test service"); @@ -380,6 +393,10 @@ public class TestFusedLocationProviderCallback implements IFusedLocationProvider private Location location; private LocationAvailability locationAvailability; + @Override public long pid() throws RemoteException { + return 1234; + } + @Override public void onLocationChanged(Location location) throws RemoteException { this.location = location; } diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/LocationSettingsResultRequestTest.java b/lost/src/test/java/com/mapzen/android/lost/internal/LocationSettingsResultRequestTest.java index 0129e5a..1fac59a 100644 --- a/lost/src/test/java/com/mapzen/android/lost/internal/LocationSettingsResultRequestTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/internal/LocationSettingsResultRequestTest.java @@ -41,7 +41,8 @@ ArrayList requests = new ArrayList<>(); LocationRequest highAccuracy = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //gps + wifi + LocationRequest.create(new TestPidReader()).setPriority( + LocationRequest.PRIORITY_HIGH_ACCURACY); //gps + wifi requests.add(highAccuracy); request = new LocationSettingsRequest.Builder().addAllLocationRequests(requests) .setNeedBle(true) @@ -166,9 +167,10 @@ ArrayList requests = new ArrayList<>(); LocationRequest balanced = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); + LocationRequest.create(new TestPidReader()).setPriority( + LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); LocationRequest lowPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_LOW_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_LOW_POWER); requests.add(balanced); requests.add(lowPower); LocationSettingsRequest settingsRequest = @@ -189,9 +191,10 @@ ArrayList requests = new ArrayList<>(); LocationRequest balanced = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); + LocationRequest.create(new TestPidReader()).setPriority( + LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); LocationRequest lowPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_LOW_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_LOW_POWER); requests.add(balanced); requests.add(lowPower); LocationSettingsRequest settingsRequest = @@ -213,9 +216,10 @@ ArrayList requests = new ArrayList<>(); LocationRequest balanced = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); + LocationRequest.create(new TestPidReader()).setPriority( + LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); LocationRequest lowPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_LOW_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_LOW_POWER); requests.add(balanced); requests.add(lowPower); LocationSettingsRequest settingsRequest = @@ -236,7 +240,7 @@ ArrayList requests = new ArrayList<>(); LocationRequest noPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_NO_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_NO_POWER); requests.add(noPower); LocationSettingsRequest settingsRequest = new LocationSettingsRequest.Builder().addAllLocationRequests(requests) @@ -256,7 +260,7 @@ ArrayList requests = new ArrayList<>(); LocationRequest noPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_NO_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_NO_POWER); requests.add(noPower); LocationSettingsRequest settingsRequest = new LocationSettingsRequest.Builder().addAllLocationRequests(requests) @@ -417,9 +421,10 @@ public void setResultCallback_balancedLowPower_noBle_shouldReturnStatusResolutio ArrayList requests = new ArrayList<>(); LocationRequest balanced = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); + LocationRequest.create(new TestPidReader()).setPriority( + LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); LocationRequest lowPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_LOW_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_LOW_POWER); requests.add(balanced); requests.add(lowPower); LocationSettingsRequest settingsRequest = @@ -444,9 +449,10 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut ArrayList requests = new ArrayList<>(); LocationRequest balanced = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); + LocationRequest.create(new TestPidReader()).setPriority( + LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); LocationRequest lowPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_LOW_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_LOW_POWER); requests.add(balanced); requests.add(lowPower); LocationSettingsRequest settingsRequest = @@ -471,9 +477,10 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut ArrayList requests = new ArrayList<>(); LocationRequest balanced = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); + LocationRequest.create(new TestPidReader()).setPriority( + LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); LocationRequest lowPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_LOW_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_LOW_POWER); requests.add(balanced); requests.add(lowPower); LocationSettingsRequest settingsRequest = @@ -497,7 +504,7 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut ArrayList requests = new ArrayList<>(); LocationRequest noPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_NO_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_NO_POWER); requests.add(noPower); LocationSettingsRequest settingsRequest = new LocationSettingsRequest.Builder().addAllLocationRequests(requests) @@ -520,7 +527,7 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut ArrayList requests = new ArrayList<>(); LocationRequest noPower = - LocationRequest.create().setPriority(LocationRequest.PRIORITY_NO_POWER); + LocationRequest.create(new TestPidReader()).setPriority(LocationRequest.PRIORITY_NO_POWER); requests.add(noPower); LocationSettingsRequest settingsRequest = new LocationSettingsRequest.Builder().addAllLocationRequests(requests) diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/LostRequestManagerTests.java b/lost/src/test/java/com/mapzen/android/lost/internal/LostRequestManagerTests.java index a507408..d9e5a49 100644 --- a/lost/src/test/java/com/mapzen/android/lost/internal/LostRequestManagerTests.java +++ b/lost/src/test/java/com/mapzen/android/lost/internal/LostRequestManagerTests.java @@ -25,7 +25,7 @@ public void setup() { @Test public void requestLocationUpdates_clientListener_oneRequest_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationListener listener = mock(LocationListener.class); requestManager.requestLocationUpdates(client, request, listener); @@ -35,10 +35,10 @@ public void requestLocationUpdates_clientListener_oneRequest_shouldUpdateMap() { @Test public void requestLocationUpdates_clientListener_twoRequests_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationListener listener = mock(LocationListener.class); requestManager.requestLocationUpdates(client, request, listener); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(client, anotherRequest, listener); assertThat(requestManager.getClientCallbackMap().size()).isEqualTo(1); @@ -51,11 +51,11 @@ public void requestLocationUpdates_clientListener_twoRequests_shouldUpdateMap() @Test public void requestLocationUpdates_twoClientListeners_twoRequests_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationListener listener = mock(LocationListener.class); requestManager.requestLocationUpdates(client, request, listener); LostApiClient anotherClient = mock(LostApiClient.class); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(anotherClient, anotherRequest, listener); assertThat(requestManager.getClientCallbackMap().size()).isEqualTo(2); @@ -72,7 +72,7 @@ public void requestLocationUpdates_twoClientListeners_twoRequests_shouldUpdateMa @Test public void requestLocationUpdates_clientPendingIntent_oneRequest_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); PendingIntent intent = mock(PendingIntent.class); requestManager.requestLocationUpdates(client, request, intent); @@ -82,10 +82,10 @@ public void requestLocationUpdates_clientPendingIntent_oneRequest_shouldUpdateMa @Test public void requestLocationUpdates_clientPendingIntent_twoRequests_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); PendingIntent intent = mock(PendingIntent.class); requestManager.requestLocationUpdates(client, request, intent); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(client, anotherRequest, intent); assertThat(requestManager.getClientCallbackMap().size()).isEqualTo(1); @@ -98,11 +98,11 @@ public void requestLocationUpdates_clientPendingIntent_twoRequests_shouldUpdateM @Test public void requestLocationUpdates_twoClientPendingIntents_twoRequests_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); PendingIntent intent = mock(PendingIntent.class); requestManager.requestLocationUpdates(client, request, intent); LostApiClient anotherClient = mock(LostApiClient.class); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(anotherClient, anotherRequest, intent); assertThat(requestManager.getClientCallbackMap().size()).isEqualTo(2); @@ -119,7 +119,7 @@ public void requestLocationUpdates_twoClientPendingIntents_twoRequests_shouldUpd @Test public void requestLocationUpdates_clientCallback_oneRequest_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationCallback callback = mock(LocationCallback.class); requestManager.requestLocationUpdates(client, request, callback); @@ -129,10 +129,10 @@ public void requestLocationUpdates_clientCallback_oneRequest_shouldUpdateMap() { @Test public void requestLocationUpdates_clientCallback_twoRequests_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationCallback callback = mock(LocationCallback.class); requestManager.requestLocationUpdates(client, request, callback); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(client, anotherRequest, callback); assertThat(requestManager.getClientCallbackMap().size()).isEqualTo(1); @@ -145,11 +145,11 @@ public void requestLocationUpdates_clientCallback_twoRequests_shouldUpdateMap() @Test public void requestLocationUpdates_twoClientCallbacks_twoRequests_shouldUpdateMap() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationCallback callback = mock(LocationCallback.class); requestManager.requestLocationUpdates(client, request, callback); LostApiClient anotherClient = mock(LostApiClient.class); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(anotherClient, anotherRequest, callback); assertThat(requestManager.getClientCallbackMap().size()).isEqualTo(2); @@ -166,7 +166,7 @@ public void requestLocationUpdates_twoClientCallbacks_twoRequests_shouldUpdateMa @Test public void removeLocationUpdates_clientListener_oneRequest_shouldRemoveAll() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationListener listener = mock(LocationListener.class); requestManager.requestLocationUpdates(client, request, listener); requestManager.removeLocationUpdates(client, listener); @@ -177,10 +177,10 @@ public void removeLocationUpdates_clientListener_oneRequest_shouldRemoveAll() { @Test public void removeLocationUpdates_clientListener_twoRequests_shouldRemoveAll() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationListener listener = mock(LocationListener.class); requestManager.requestLocationUpdates(client, request, listener); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(client, anotherRequest, listener); requestManager.removeLocationUpdates(client, listener); @@ -190,11 +190,11 @@ public void removeLocationUpdates_clientListener_twoRequests_shouldRemoveAll() { @Test public void removeLocationUpdates_twoClientListeners_twoRequests_shouldRemoveOne() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationListener listener = mock(LocationListener.class); requestManager.requestLocationUpdates(client, request, listener); LostApiClient anotherClient = mock(LostApiClient.class); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(anotherClient, anotherRequest, listener); requestManager.removeLocationUpdates(client, listener); @@ -207,7 +207,7 @@ public void removeLocationUpdates_twoClientListeners_twoRequests_shouldRemoveOne @Test public void removeLocationUpdates_clientPendingIntent_oneRequest_shouldRemoveAll() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); PendingIntent intent = mock(PendingIntent.class); requestManager.requestLocationUpdates(client, request, intent); requestManager.removeLocationUpdates(client, intent); @@ -218,10 +218,10 @@ public void removeLocationUpdates_clientPendingIntent_oneRequest_shouldRemoveAll @Test public void removeLocationUpdates_clientPendingIntent_twoRequests_shouldRemoveAll() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); PendingIntent intent = mock(PendingIntent.class); requestManager.requestLocationUpdates(client, request, intent); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(client, anotherRequest, intent); requestManager.removeLocationUpdates(client, intent); @@ -231,11 +231,11 @@ public void removeLocationUpdates_clientPendingIntent_twoRequests_shouldRemoveAl @Test public void removeLocationUpdates_twoClientPendingIntents_twoRequests_shouldRemoveOne() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); PendingIntent intent = mock(PendingIntent.class); requestManager.requestLocationUpdates(client, request, intent); LostApiClient anotherClient = mock(LostApiClient.class); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(anotherClient, anotherRequest, intent); requestManager.removeLocationUpdates(client, intent); @@ -248,7 +248,7 @@ public void removeLocationUpdates_twoClientPendingIntents_twoRequests_shouldRemo @Test public void removeLocationUpdates_clientCallback_oneRequest_shouldRemoveAll() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationCallback callback = mock(LocationCallback.class); requestManager.requestLocationUpdates(client, request, callback); requestManager.removeLocationUpdates(client, callback); @@ -259,10 +259,10 @@ public void removeLocationUpdates_clientCallback_oneRequest_shouldRemoveAll() { @Test public void removeLocationUpdates_clientCallback_twoRequests_shouldRemoveAll() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationCallback callback = mock(LocationCallback.class); requestManager.requestLocationUpdates(client, request, callback); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(client, anotherRequest, callback); requestManager.removeLocationUpdates(client, callback); @@ -272,11 +272,11 @@ public void removeLocationUpdates_clientCallback_twoRequests_shouldRemoveAll() { @Test public void removeLocationUpdates_twoClientCallbacks_twoRequests_shouldRemoveOne() { LostApiClient client = mock(LostApiClient.class); - LocationRequest request = LocationRequest.create(); + LocationRequest request = LocationRequest.create(new TestPidReader()); LocationCallback callback = mock(LocationCallback.class); requestManager.requestLocationUpdates(client, request, callback); LostApiClient anotherClient = mock(LostApiClient.class); - LocationRequest anotherRequest = LocationRequest.create(); + LocationRequest anotherRequest = LocationRequest.create(new TestPidReader()); requestManager.requestLocationUpdates(anotherClient, anotherRequest, callback); requestManager.removeLocationUpdates(client, callback); diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/TestPidReader.java b/lost/src/test/java/com/mapzen/android/lost/internal/TestPidReader.java new file mode 100644 index 0000000..b13ce46 --- /dev/null +++ b/lost/src/test/java/com/mapzen/android/lost/internal/TestPidReader.java @@ -0,0 +1,8 @@ +package com.mapzen.android.lost.internal; + +public class TestPidReader implements PidReader { + + @Override public long getPid() { + return 0; + } +} diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/TestServiceStub.java b/lost/src/test/java/com/mapzen/android/lost/internal/TestServiceStub.java index 6a3914a..82555d6 100644 --- a/lost/src/test/java/com/mapzen/android/lost/internal/TestServiceStub.java +++ b/lost/src/test/java/com/mapzen/android/lost/internal/TestServiceStub.java @@ -6,14 +6,19 @@ import android.location.Location; import android.os.RemoteException; +import java.util.ArrayList; import java.util.List; public class TestServiceStub extends IFusedLocationProviderService.Stub { - IFusedLocationProviderCallback callback; + List callbacks = new ArrayList<>(); - @Override public void init(IFusedLocationProviderCallback callback) throws RemoteException { - this.callback = callback; + @Override public void add(IFusedLocationProviderCallback callback) throws RemoteException { + this.callbacks.add(callback); + } + + @Override public void remove(IFusedLocationProviderCallback callback) throws RemoteException { + this.callbacks.remove(callback); } @Override public Location getLastLocation() throws RemoteException {