refactoring of GeofenceApp
This commit is contained in:
parent
dd79e93a09
commit
3c1c1546b5
4 changed files with 38 additions and 135 deletions
|
@ -7,7 +7,7 @@
|
|||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name">
|
||||
<service
|
||||
android:name="com.esrlabs.geofence.MyService"
|
||||
android:name="com.esrlabs.geofence.GeofenceApp"
|
||||
android:enabled="true"
|
||||
android:exported="true" >
|
||||
</service>
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
||||
}
|
|
@ -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<String> 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) {}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue