From 3c1c1546b59cafcae32fa24eee6cc577791aeb80 Mon Sep 17 00:00:00 2001 From: Andrei Bechet Date: Thu, 18 Jun 2015 16:40:53 +0200 Subject: [PATCH] refactoring of GeofenceApp --- .../Geofence/app/src/main/AndroidManifest.xml | 2 +- .../com/esrlabs/geofence/GeofenceApp.java | 37 ++++++++++ .../java/com/esrlabs/geofence/MyService.java | 65 ----------------- .../com/esrlabs/geofence/MyServiceTest.java | 69 ------------------- 4 files changed, 38 insertions(+), 135 deletions(-) create mode 100644 2015/android/Geofence/app/src/main/java/com/esrlabs/geofence/GeofenceApp.java delete mode 100644 2015/android/Geofence/app/src/main/java/com/esrlabs/geofence/MyService.java delete mode 100644 2015/android/Geofence/app/src/test/java/com/esrlabs/geofence/MyServiceTest.java diff --git a/2015/android/Geofence/app/src/main/AndroidManifest.xml b/2015/android/Geofence/app/src/main/AndroidManifest.xml index c84fdfd..99e3992 100644 --- a/2015/android/Geofence/app/src/main/AndroidManifest.xml +++ b/2015/android/Geofence/app/src/main/AndroidManifest.xml @@ -7,7 +7,7 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name"> diff --git a/2015/android/Geofence/app/src/main/java/com/esrlabs/geofence/GeofenceApp.java b/2015/android/Geofence/app/src/main/java/com/esrlabs/geofence/GeofenceApp.java new file mode 100644 index 0000000..2ade4cc --- /dev/null +++ b/2015/android/Geofence/app/src/main/java/com/esrlabs/geofence/GeofenceApp.java @@ -0,0 +1,37 @@ +package com.esrlabs.geofence; + +import android.app.Service; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Bundle; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Log; + +import com.esrlabs.headunitinterface.HeadUnit; + +import java.util.List; + +/** + * Steps: + * - register to the location provider + * - connect to the HeadUnitService + * - build geofence object + * - implement the exercise logic (when the user is outside of the geofence show popup; hide it otherwise) + * + * See: + * - that tests are green + * - that the notification appears in the emulator + */ +public class GeofenceApp extends Service implements LocationListener { + + public static final String TAG = "GeofenceApp"; + public static final String CAN_PROVIDER = "CanLocationProvider"; + + +} \ No newline at end of file diff --git a/2015/android/Geofence/app/src/main/java/com/esrlabs/geofence/MyService.java b/2015/android/Geofence/app/src/main/java/com/esrlabs/geofence/MyService.java deleted file mode 100644 index 3c221e3..0000000 --- a/2015/android/Geofence/app/src/main/java/com/esrlabs/geofence/MyService.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.esrlabs.geofence; - -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.os.Bundle; -import android.os.IBinder; - -import java.util.List; - -public class MyService extends Service implements LocationListener { - public static final String CAN_PROVIDER = "CanLocationProvider"; - - private LocationManager locationManager; - private Location latestLocation; - - public MyService(LocationManager locationManager) { - this.locationManager = locationManager; - } - - @Override - public void onCreate() { - super.onCreate(); - - if (locationManager == null) { - locationManager = (LocationManager) - this.getSystemService(Context.LOCATION_SERVICE); - } - - initLocationListener(); - } - - @Override - public IBinder onBind(Intent intent) { - throw new UnsupportedOperationException("Not yet implemented"); - } - - private void initLocationListener() { - List allProviders = locationManager.getAllProviders(); - for (String provider : allProviders) { - locationManager.requestLocationUpdates(provider, 0, 0, this); - } - - locationManager.requestLocationUpdates(CAN_PROVIDER, 0, 0, this); - } - - @Override - public void onLocationChanged(Location location) { - latestLocation = location; - } - - public Location latestLocation() { - return latestLocation; - } - - @Override - public void onStatusChanged(String s, int i, Bundle bundle) {} - @Override - public void onProviderDisabled(String s) {} - @Override - public void onProviderEnabled(String s) {} -} \ No newline at end of file diff --git a/2015/android/Geofence/app/src/test/java/com/esrlabs/geofence/MyServiceTest.java b/2015/android/Geofence/app/src/test/java/com/esrlabs/geofence/MyServiceTest.java deleted file mode 100644 index dacf07b..0000000 --- a/2015/android/Geofence/app/src/test/java/com/esrlabs/geofence/MyServiceTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.esrlabs.geofence; - -import static com.esrlabs.geofence.MyService.CAN_PROVIDER; - -import android.content.Context; -import android.location.Location; -import android.location.LocationManager; -import android.test.ServiceTestCase; - -import com.esrlabs.geofence.MyService; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.Shadows; -import org.robolectric.annotation.Config; -import org.robolectric.shadows.ShadowLocationManager; - -import esrlabs.com.geofence.BuildConfig; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(constants = BuildConfig.class, emulateSdk = 17) -public class MyServiceTest extends ServiceTestCase { - - public final LocationManager locationManager = (LocationManager) - RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE); - Location someLocation = location(CAN_PROVIDER, 12.0, 20.0); - - public MyServiceTest() { - super(MyService.class); - } - - @Before - public void setUp() throws Exception { } - - @After - public void tearDown() throws Exception { } - - @Test - public void testLatestLocation() throws Exception { - MyService mainService = setupMyService(locationManager); - simulateNewLocation(locationManager); - - assertTrue(someLocation.equals(mainService.latestLocation())); - } - - private void simulateNewLocation(LocationManager locationManager) { - ShadowLocationManager shadowLocationManager = Shadows.shadowOf(locationManager); - shadowLocationManager.simulateLocation(someLocation); - } - - private MyService setupMyService(LocationManager locationManager) { - MyService service = new MyService(locationManager); - service.onCreate(); - return service; - } - - private Location location(String provider, double latitude, double longitude) { - Location location = new Location(provider); - location.setLatitude(latitude); - location.setLongitude(longitude); - location.setTime(System.currentTimeMillis()); - return location; - } -} \ No newline at end of file