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; import android.location.Location;
public class CircleGeofence implements Geofence { public class CircleGeofence implements Geofence {
final Location center; final Location center;
final float radiusInMeters; final float radiusInMeters;
@ -14,7 +13,8 @@ public class CircleGeofence implements Geofence {
@Override @Override
public boolean containsLocation(Location location) { public boolean containsLocation(Location location) {
// TODO float distanceInMeters = center.distanceTo(location);
return false;
return (distanceInMeters <= radiusInMeters);
} }
} }

View file

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

View file

@ -5,6 +5,7 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.location.Criteria;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
@ -32,13 +33,18 @@ import static com.esrlabs.geofence.Utils.location;
* - that the notification appears in the emulator * - that the notification appears in the emulator
*/ */
public class GeofenceApp extends Service implements LocationListener { public class GeofenceApp extends Service implements LocationListener {
public static final String TAG = "GeofenceApp"; public static final String TAG = "GeofenceApp";
private LocationManager locationManager; private LocationManager locationManager;
private Geofence geofence; 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 @Override
public void onCreate() { public void onCreate() {
@ -47,8 +53,7 @@ public class GeofenceApp extends Service implements LocationListener {
Log.d(TAG, "onCreate"); Log.d(TAG, "onCreate");
if (locationManager == null) { if (locationManager == null) {
locationManager = (LocationManager) locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
this.getSystemService(Context.LOCATION_SERVICE);
} }
initLocationListener(); initLocationListener();
@ -61,12 +66,36 @@ public class GeofenceApp extends Service implements LocationListener {
} }
} }
private void initHeadUnitService() { 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() { private void initLocationListener() {
// TODO List<String> locationProviders = locationManager.getAllProviders();
for(String provider : locationProviders) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
} }
@Override @Override
@ -76,7 +105,17 @@ public class GeofenceApp extends Service implements LocationListener {
@Override @Override
public void onLocationChanged(Location location) { 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 @Override
@ -94,9 +133,7 @@ public class GeofenceApp extends Service implements LocationListener {
} }
public Location latestLocation() public Location latestLocation() {
{ return mLatestLocation;
// TODO
return null;
} }
} }

View file

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