implemented missing java code

This commit is contained in:
Timo Tomasini 2016-07-07 13:05:02 +02:00
parent 1b87782260
commit 0b992cbd96
4 changed files with 54 additions and 17 deletions

View File

@ -3,7 +3,6 @@ package com.esrlabs.geofence;
import android.location.Location;
public class CircleGeofence implements Geofence {
final Location center;
final float radiusInMeters;
@ -14,7 +13,8 @@ public class CircleGeofence implements Geofence {
@Override
public boolean containsLocation(Location location) {
// TODO
return false;
float distanceInMeters = center.distanceTo(location);
return (distanceInMeters <= radiusInMeters);
}
}

View File

@ -3,6 +3,5 @@ package com.esrlabs.geofence;
import android.location.Location;
public interface Geofence {
public boolean containsLocation(Location location);
}

View File

@ -5,6 +5,7 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
@ -32,13 +33,18 @@ import static com.esrlabs.geofence.Utils.location;
* - that the notification appears in the emulator
*/
public class GeofenceApp extends Service implements LocationListener {
public static final String TAG = "GeofenceApp";
private LocationManager locationManager;
private Geofence geofence;
private ServiceConnection mConnection;
private HeadUnit mOurHeadUnitService;
private Location mLatestLocation;
// TODO: create constructor
public GeofenceApp(LocationManager aLocationManager, Geofence aGeofence) {
locationManager = aLocationManager;
geofence = aGeofence;
}
@Override
public void onCreate() {
@ -47,8 +53,7 @@ public class GeofenceApp extends Service implements LocationListener {
Log.d(TAG, "onCreate");
if (locationManager == null) {
locationManager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}
initLocationListener();
@ -61,12 +66,36 @@ public class GeofenceApp extends Service implements LocationListener {
}
}
private void initHeadUnitService() {
// TODO
Intent headUnitServiceIntent = new Intent(HeadUnit.class.getName());
headUnitServiceIntent.setPackage("com.esrlabs.headunitservice");
mConnection = new ServiceConnection() {
// Called when the connection with the service is established
public void onServiceConnected(ComponentName className, IBinder service) {
// Following the example above for an AIDL interface,
// this gets an instance of the IRemoteInterface, which we can use to call on the service
mOurHeadUnitService = HeadUnit.Stub.asInterface(service);
};
// Called when the connection with the service disconnects unexpectedly
public void onServiceDisconnected(ComponentName className) {
Log.e(TAG, "Service has unexpectedly disconnected");
mOurHeadUnitService = null;
}
};
bindService(headUnitServiceIntent, mConnection, BIND_AUTO_CREATE);
}
private void initLocationListener() {
// TODO
List<String> locationProviders = locationManager.getAllProviders();
for(String provider : locationProviders) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
}
@Override
@ -76,7 +105,17 @@ public class GeofenceApp extends Service implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO
mLatestLocation = location;
try {
if(geofence.containsLocation(location)) {
mOurHeadUnitService.hideAllNotifications();
} else {
mOurHeadUnitService.showNotification("Outside Geofence!");
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
@ -94,9 +133,7 @@ public class GeofenceApp extends Service implements LocationListener {
}
public Location latestLocation()
{
// TODO
return null;
public Location latestLocation() {
return mLatestLocation;
}
}
}

View File

@ -82,7 +82,8 @@ public class GeofenceAppTest extends TestCase {
@Test
public void shouldReceiveTheLatestLocation() throws Exception {
simulateNewLocation(someLocation);
assertTrue(someLocation.equals(geofenceApp.latestLocation()));
Location testLocation = geofenceApp.latestLocation();
assertTrue(someLocation.equals(testLocation));
}
@Test