From 0b992cbd967016f69999150a59968855f3fae30c Mon Sep 17 00:00:00 2001 From: Timo Tomasini Date: Thu, 7 Jul 2016 13:05:02 +0200 Subject: [PATCH] implemented missing java code --- .../com/esrlabs/geofence/CircleGeofence.java | 6 +- .../java/com/esrlabs/geofence/Geofence.java | 1 - .../com/esrlabs/geofence/GeofenceApp.java | 61 +++++++++++++++---- .../com/esrlabs/geofence/GeofenceAppTest.java | 3 +- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/CircleGeofence.java b/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/CircleGeofence.java index 4d5cfcf..aad38f4 100644 --- a/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/CircleGeofence.java +++ b/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/CircleGeofence.java @@ -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); } } diff --git a/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/Geofence.java b/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/Geofence.java index edb778b..af5d084 100644 --- a/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/Geofence.java +++ b/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/Geofence.java @@ -3,6 +3,5 @@ package com.esrlabs.geofence; import android.location.Location; public interface Geofence { - public boolean containsLocation(Location location); } diff --git a/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/GeofenceApp.java b/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/GeofenceApp.java index a8c398a..3142b44 100644 --- a/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/GeofenceApp.java +++ b/2016/android/Geofence/app/src/main/java/com/esrlabs/geofence/GeofenceApp.java @@ -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 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; } -} \ No newline at end of file +} diff --git a/2016/android/Geofence/app/src/test/java/com/esrlabs/geofence/GeofenceAppTest.java b/2016/android/Geofence/app/src/test/java/com/esrlabs/geofence/GeofenceAppTest.java index 1242bb5..8919d87 100644 --- a/2016/android/Geofence/app/src/test/java/com/esrlabs/geofence/GeofenceAppTest.java +++ b/2016/android/Geofence/app/src/test/java/com/esrlabs/geofence/GeofenceAppTest.java @@ -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