From 0176f37d011f837df577d88456543d9a78fd78f3 Mon Sep 17 00:00:00 2001 From: chteufleur Date: Fri, 5 Oct 2018 15:20:22 +0200 Subject: [PATCH] Move trace management into ServiceTrackingDog. --- .../mytrackingdog/MainActivity.java | 154 ++++---- .../mytrackingdog/models/Notification.java | 42 +++ .../mytrackingdog/services/ServiceGps.java | 210 +---------- .../services/ServiceTrackingDog.java | 331 ++++++++++++------ .../mytrackingdog/services/ServiceXmpp.java | 25 +- 5 files changed, 352 insertions(+), 410 deletions(-) create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/Notification.java diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java index c19c83d..c77192f 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java @@ -7,6 +7,7 @@ import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.graphics.Color; import android.hardware.GeomagneticField; +import android.location.Location; import android.location.LocationManager; import android.net.Uri; import android.os.Build; @@ -54,6 +55,7 @@ import java.util.List; import java.util.Observable; import java.util.Observer; +import fr.chteufleur.mytrackingdog.models.Notification; import fr.chteufleur.mytrackingdog.models.beans.MyLocation; import fr.chteufleur.mytrackingdog.models.beans.MyLocationArray; import fr.chteufleur.mytrackingdog.models.beans.WayPointLocation; @@ -174,6 +176,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu serviceTrackingDog.sendCommandStopTrail(); } } catch (XmppStringprepException | InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException ex) { + ex.printStackTrace(); } } }); @@ -197,6 +200,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu try { serviceTrackingDog.sendCommandObjectTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime()); } catch (Exception e) { + e.printStackTrace(); } } } else if (serviceTrackingDog.isDogActivated()) { @@ -421,7 +425,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu Log.i(TAG, "TEXT: "+text); } catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) { Log.e(TAG, "Fail send presence", e); - Toast.makeText(ctx, "Echec connexion avec le matériel", Toast.LENGTH_LONG).show();; + Toast.makeText(ctx, "Echec connexion avec le matériel", Toast.LENGTH_LONG).show(); } } break; @@ -461,7 +465,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu if (gpsspeed < 0.01) { GeomagneticField gf = new GeomagneticField(lat, lon, alt, timeOfFix); float trueNorth = orientationToMagneticNorth + gf.getDeclination(); - gf = null; if (trueNorth > 360.0f) { trueNorth = trueNorth - 360.0f; } @@ -637,93 +640,90 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu @Override public void update(Observable observable, Object o) { if (observable.getClass().getName().equals(ServiceGps.class.getName())) { - updateServiceGps(o); + updateServiceGps((Notification) o); } else if (observable.getClass().getName().equals(ServiceXmpp.class.getName())) { - updateServiceXmpp(o); + updateServiceXmpp((Notification) o); } } - public void updateServiceGps(Object o) { - if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) { - MyLocation loc = serviceTrackingDog.getCurrentLocation(); - onNewLocation(loc); - } - } - - public void updateServiceXmpp(Object o) { - if (o instanceof String) { - if (o.equals(ServiceXmpp.NOTIF_NEW_LOCATION)) { - runOnUiThread(new Runnable() { - @Override - public void run() { - if (serviceTrackingDog.isTraceurActivated()) { - MyLocation loc = serviceTrackingDog.getXmppCurrentLocation(); - onNewLocation(loc); - } - } - }); - } else if (o.equals(ServiceXmpp.NOTIF_NEW_OBJECT)){ - runOnUiThread(new Runnable() { - @Override - public void run() { - if (serviceTrackingDog.isTraceurActivated()) { - MyLocation locObj = serviceTrackingDog.getXmppLastObjectLocation(); - WayPointLocation loc = serviceTrackingDog.addPointObjectTrail(locObj); - GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); - addMarker(gp, loc.isFound()); - } - } - }); - } else if (o.equals(ServiceXmpp.NOTIF_START_TRAIL)){ - runOnUiThread(new Runnable() { - @Override - public void run() { - changeStatusTrace(); - } - }); - } else if (o.equals(ServiceXmpp.NOTIF_STOP_TRAIL)){ - runOnUiThread(new Runnable() { - @Override - public void run() { - changeStatusTrace(); - } - }); + public void updateServiceGps(Notification notification) { + if (notification.isAction(ServiceGps.NOTIF_NEW_LOCATION)) { + Location loc = (Location) notification.getExtra(ServiceGps.NOTIF_NEW_LOCATION_VALUE_LOCATION); + if (loc != null) { + onNewLocation(new MyLocation(loc)); } } } + public void updateServiceXmpp(Notification notification) { + if (notification.isAction(ServiceXmpp.NOTIF_NEW_LOCATION)) { + if (serviceTrackingDog.isTraceurActivated()) { + MyLocation loc = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_LOCATION_VALUE_LOCATION); + onNewLocation(loc); + } + } else if (notification.isAction(ServiceXmpp.NOTIF_NEW_OBJECT)){ + if (serviceTrackingDog.isTraceurActivated()) { + MyLocation locObj = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_OBJECT_VALUE_LOCATION); + WayPointLocation loc = serviceTrackingDog.addPointObjectTrail(locObj); + GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); + addMarker(gp, loc.isFound()); + } + } else if (notification.isAction(ServiceXmpp.NOTIF_START_TRAIL)){ + runOnUiThread(new Runnable() { + @Override + public void run() { + changeStatusTrace(); + } + }); + } else if (notification.isAction(ServiceXmpp.NOTIF_STOP_TRAIL)){ + runOnUiThread(new Runnable() { + @Override + public void run() { + changeStatusTrace(); + } + }); + } + } + + private MyLocation onNewLocation = null; private void onNewLocation(MyLocation loc) { if (loc != null) { - GeoPoint currentPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); - if (mLocationOverlay.isFollowLocationEnabled()) { - map.getController().setCenter(currentPoint); - } - if (serviceTrackingDog.isTraceurActivated()) { - updateTrailTrace(); - if (lastLocation != null) { - distance += loc.distanceTo(lastLocation); - updateDistance(); - } - lastLocation = loc; - } else if (serviceTrackingDog.isDogActivated()) { - updateDogTrace(); - if (lastLocation != null) { - distance += loc.distanceTo(lastLocation); - updateDistance(); - } - lastLocation = loc; - } + onNewLocation = loc; + runOnUiThread(new Runnable() { + @Override + public void run() { + GeoPoint currentPoint = new GeoPoint(onNewLocation.getLatitude(), onNewLocation.getLongitude(), onNewLocation.getAltitude()); + if (mLocationOverlay.isFollowLocationEnabled()) { + map.getController().setCenter(currentPoint); + } + if (serviceTrackingDog.isTraceurActivated()) { + updateTrailTrace(); + if (lastLocation != null) { + distance += onNewLocation.distanceTo(lastLocation); + updateDistance(); + } + lastLocation = onNewLocation; + } else if (serviceTrackingDog.isDogActivated()) { + updateDogTrace(); + if (lastLocation != null) { + distance += onNewLocation.distanceTo(lastLocation); + updateDistance(); + } + lastLocation = onNewLocation; + } - float orientation = serviceTrackingDog.getOrientation(deviceOrientation); - if (orientation >= 0) { - map.setMapOrientation(orientation); - } + float orientation = serviceTrackingDog.getOrientation(deviceOrientation); + if (orientation >= 0) { + map.setMapOrientation(orientation); + } - if (!zoomed) { - IMapController mapController = map.getController(); - mapController.setZoom(20.0); - zoomed = true; - } + if (!zoomed) { + IMapController mapController = map.getController(); + mapController.setZoom(20.0); + zoomed = true; + } + } + }); } } // diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/Notification.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/Notification.java new file mode 100644 index 0000000..256048f --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/Notification.java @@ -0,0 +1,42 @@ +package fr.chteufleur.mytrackingdog.models; + +import java.util.HashMap; +import java.util.Map; + +public class Notification { + + private final String action; + private final Map extras; + + public Notification(String action) { + this(action, new HashMap()); + } + + public Notification(String action, Map extras) { + this.action = action; + this.extras = extras; + } + + public boolean isAction(String action) { + boolean ret = false; + if (action != null) { + ret = this.action.equals(action); + } + return ret; + } + + public Notification addExtra(String param, Object value) { + if (extras != null) { + extras.put(param, value); + } + return this; + } + + public Object getExtra(String param) { + Object ret = null; + if (extras != null && extras.containsKey(param)) { + ret = extras.get(param); + } + return ret; + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceGps.java b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceGps.java index 225aef7..0069426 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceGps.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceGps.java @@ -1,62 +1,29 @@ package fr.chteufleur.mytrackingdog.services; -import android.content.SharedPreferences; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; -import android.os.Build; import android.os.Bundle; -import android.os.Environment; -import android.os.VibrationEffect; -import android.os.Vibrator; import android.util.Log; -import org.xmlpull.v1.XmlPullParserException; - -import java.io.File; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; import java.util.Observable; -import fr.chteufleur.mytrackingdog.models.ExportGpx; -import fr.chteufleur.mytrackingdog.models.Gpx; -import fr.chteufleur.mytrackingdog.models.ImportGpx; -import fr.chteufleur.mytrackingdog.models.Traces; -import fr.chteufleur.mytrackingdog.models.beans.MyLocation; -import fr.chteufleur.mytrackingdog.models.beans.MyLocationArray; -import fr.chteufleur.mytrackingdog.models.beans.TraceLocation; -import fr.chteufleur.mytrackingdog.models.beans.WayPointLocation; +import fr.chteufleur.mytrackingdog.models.Notification; public class ServiceGps extends Observable implements LocationListener { private static final String TAG = ServiceGps.class.getName(); public static final String NOTIF_NEW_LOCATION = ServiceGps.class.getName()+".newlocation"; - - private File lastExportedTrailFile = null; + public static final String NOTIF_NEW_LOCATION_VALUE_LOCATION = NOTIF_NEW_LOCATION+".value.location"; private LocationManager locationManager; - private MyLocation currentLocation = null; - private String appName = ""; - private final Traces traces = new Traces(); - public ServiceGps() { - } public void setLocationManager(LocationManager locationManager) { this.locationManager = locationManager; } - public void setAppName(String appName) { - this.appName = appName; - } - - public MyLocation getCurrentLocation() { - return currentLocation; - } public void start() { try { @@ -76,32 +43,12 @@ public class ServiceGps extends Observable implements LocationListener { Log.i(TAG, "Stop location"); } - public float getOrientation(int deviceOrientation) { - float ret = -1; - if (currentLocation != null && currentLocation.getSpeed() >= 0.01) { - ret = (360 - currentLocation.getBearing() - deviceOrientation); - if (ret < 0) { - ret += 360; - } - if (ret > 360) { - ret -= 360; - } - //help smooth everything out - ret = (int) ret; - ret = ret / 5; - ret = (int) ret; - ret = ret * 5; - } - return ret; - } - // @Override public void onLocationChanged(Location location) { Log.i(TAG, "onLocationChanged"); - currentLocation = new MyLocation(location); setChanged(); - notifyObservers(NOTIF_NEW_LOCATION); + notifyObservers(new Notification(NOTIF_NEW_LOCATION).addExtra(NOTIF_NEW_LOCATION_VALUE_LOCATION, location)); } @Override @@ -133,155 +80,4 @@ public class ServiceGps extends Observable implements LocationListener { } } // - - // - // - public void toggleTraceurActivation() { - traces.toggleTraceurActivation(); - if (!isTraceurActivated()) { - exportTrailTraceToGpx(); - } - } - public boolean isTraceurActivated() { - return traces.isTraceurActivated(); - } - - public void toggleDogActivation() { - traces.toggleDogActivation(); - if (!isDogActivated()) { - exportDogTraceToGpx(); - } - } - public boolean isDogActivated() { - return traces.isDogActivated(); - } - // - // - public WayPointLocation addPointObjectTrail() { - return addPointObjectTrail(currentLocation); - } - - public WayPointLocation addPointObjectTrail(MyLocation location) { - WayPointLocation wpl = null; - if (location != null) { - wpl = new WayPointLocation(location); - traces.addPointObjectTrail(wpl); - } - return wpl; - } - - public WayPointLocation addPointObjectDog() { - WayPointLocation wpl = null; - if (currentLocation != null) { - wpl = new WayPointLocation(currentLocation); - wpl.setFound(); - traces.addPointObjectDog(wpl); - } - return wpl; - } - - public MyLocationArray getListGeoPointObjectsTrail() { - return traces.getListPointObjectsTrail(); - } - public MyLocationArray getListGeoPointObjectsDog() { - return traces.getListPointObjectsDog(); - } - - public List foundNearObjects(int distance) { - List ret = new ArrayList<>(); - MyLocation curLoc = currentLocation; - for (MyLocation ml: getListGeoPointObjectsTrail()) { - if (curLoc.distanceTo(ml) < distance && ml instanceof WayPointLocation) { - ret.add((WayPointLocation) ml); - } - } - return ret; - } - public boolean isNearObjects() { - return !foundNearObjects(20).isEmpty(); - } - public List foundNearObjects() { - return foundNearObjects(10); - } - // - // - public void addPoint(MyLocation location) { - traces.addCurrentPoint(location); - } - public MyLocationArray getListGeoPointTraceur() { - return traces.getListPointTraceur(); - } - public MyLocationArray getListGeoPointDog() { - return traces.getListPointDog(); - } - public WayPointLocation getPointTrail(double lat, double lon) { - return traces.getPointObjectTrail(lat, lon); - } - public WayPointLocation getPointDog(double lat, double lon) { - return traces.getPointObjectDog(lat, lon); - } - // - // - - // - private String getFileName(String prefix) { - SimpleDateFormat formater = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); - String date = formater.format(new Date()); - return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" - + appName + "/" + date + "_" + prefix - + ".gpx"; - } - - private boolean exportDogTraceToGpx() { - File file = new File(getFileName(Gpx.DOG_TRACE_NAME)); - ExportGpx exportGpx = new ExportGpx(file, Gpx.DOG_TRACE_NAME); - exportGpx.setObjects(traces.getListPointObjectsDog()); - exportGpx.setTrace(traces.getListPointDog()); - return exportGpx.export(); - } - - private boolean exportTrailTraceToGpx() { - File file = new File(getFileName(Gpx.TRAIL_TRACE_NAME)); - ExportGpx exportGpx = new ExportGpx(file, Gpx.TRAIL_TRACE_NAME); - exportGpx.setObjects(traces.getListPointObjectsTrail()); - exportGpx.setTrace(traces.getListPointTraceur()); - lastExportedTrailFile = file; - return exportGpx.export(); - } - - public File getLastExportedTrailFile() { - return lastExportedTrailFile; - } - - public void importGpxTrace(File file) { - ImportGpx importGpx = new ImportGpx(file); - try { - List list = importGpx.parse(); - String traceName = importGpx.getTraceName(); - if (traceName.equals(Gpx.TRAIL_TRACE_NAME)) { - lastExportedTrailFile = file; - } - for (int i=0; i } diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java index 81a575d..9704f11 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java @@ -2,28 +2,45 @@ package fr.chteufleur.mytrackingdog.services; import android.content.SharedPreferences; import android.content.res.Resources; +import android.location.Location; import android.location.LocationManager; import android.os.Build; +import android.os.Environment; import android.os.VibrationEffect; import android.os.Vibrator; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPException; import org.jxmpp.stringprep.XmppStringprepException; +import org.xmlpull.v1.XmlPullParserException; import java.io.File; import java.io.IOException; import java.net.UnknownHostException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Observable; import java.util.Observer; +import fr.chteufleur.mytrackingdog.models.ExportGpx; +import fr.chteufleur.mytrackingdog.models.Gpx; +import fr.chteufleur.mytrackingdog.models.ImportGpx; +import fr.chteufleur.mytrackingdog.models.Notification; +import fr.chteufleur.mytrackingdog.models.Traces; import fr.chteufleur.mytrackingdog.models.beans.MyLocation; import fr.chteufleur.mytrackingdog.models.beans.MyLocationArray; +import fr.chteufleur.mytrackingdog.models.beans.TraceLocation; import fr.chteufleur.mytrackingdog.models.beans.WayPointLocation; public class ServiceTrackingDog implements Observer { + private enum LocationProvider { + GPS, + XMPP + } + // private final SharedPreferences preferences; private static final String PREF_VIBRATION_NEAR_OBJECT_ENABLED = "PREF_VIBRATION_NEAR_OBJECT_ENABLED"; @@ -31,8 +48,13 @@ public class ServiceTrackingDog implements Observer { private final ServiceGps serviceGps; private final ServiceXmpp serviceXmpp; + private final Traces traces = new Traces(); + private String appName = ""; + private LocationProvider currentLocationProvider = LocationProvider.GPS; + private MyLocation currentLocation = null; + public ServiceTrackingDog(Vibrator vibrator, SharedPreferences preferences, Resources resources) { this.vibrator = vibrator; @@ -56,7 +78,6 @@ public class ServiceTrackingDog implements Observer { public void setAppName(String appName) { this.appName = appName; - serviceGps.setAppName(appName); } public void addObserver(Observer observer, String name) { @@ -74,25 +95,137 @@ public class ServiceTrackingDog implements Observer { } } + // + public MyLocation getCurrentLocation() { + return currentLocation; + } + public float getOrientation(int deviceOrientation) { + float ret = -1; + if (currentLocation != null && currentLocation.getSpeed() >= 0.01) { + ret = (360 - currentLocation.getBearing() - deviceOrientation); + if (ret < 0) { + ret += 360; + } + if (ret > 360) { + ret -= 360; + } + //help smooth everything out + ret = (int) ret; + ret = ret / 5; + ret = (int) ret; + ret = ret * 5; + } + return ret; + } + // + + // + // + public void toggleTraceurActivation() { + traces.toggleTraceurActivation(); + if (!isTraceurActivated()) { + exportTrailTraceToGpx(); + } + } + public boolean isTraceurActivated() { + return traces.isTraceurActivated(); + } + public void toggleDogActivation() { + traces.toggleDogActivation(); + if (!isDogActivated()) { + exportDogTraceToGpx(); + } else { + currentLocationProvider = LocationProvider.GPS; + } + } + public boolean isDogActivated() { + return traces.isDogActivated(); + } + // + // + public WayPointLocation addPointObjectTrail() { + return addPointObjectTrail(currentLocation); + } + public WayPointLocation addPointObjectTrail(MyLocation location) { + WayPointLocation wpl = null; + if (location != null) { + wpl = new WayPointLocation(location); + traces.addPointObjectTrail(wpl); + } + return wpl; + } + public WayPointLocation addPointObjectDog() { + WayPointLocation wpl = null; + if (currentLocation != null) { + wpl = new WayPointLocation(currentLocation); + wpl.setFound(); + traces.addPointObjectDog(wpl); + } + return wpl; + } + + public MyLocationArray getListGeoPointObjectsTrail() { + return traces.getListPointObjectsTrail(); + } + public MyLocationArray getListGeoPointObjectsDog() { + return traces.getListPointObjectsDog(); + } + + private List foundNearObjects(int distance) { + List ret = new ArrayList<>(); + MyLocation curLoc = currentLocation; + for (MyLocation ml: getListGeoPointObjectsTrail()) { + if (curLoc.distanceTo(ml) < distance && ml instanceof WayPointLocation) { + ret.add((WayPointLocation) ml); + } + } + return ret; + } + public boolean isNearObjects() { + return !foundNearObjects(20).isEmpty(); + } + public List foundNearObjects() { + return foundNearObjects(10); + } + // + // + public void addPoint(MyLocation location) { + traces.addCurrentPoint(location); + } + public MyLocationArray getListGeoPointTraceur() { + return traces.getListPointTraceur(); + } + public MyLocationArray getListGeoPointDog() { + return traces.getListPointDog(); + } + public WayPointLocation getPointTrail(double lat, double lon) { + return traces.getPointObjectTrail(lat, lon); + } + public WayPointLocation getPointDog(double lat, double lon) { + return traces.getPointObjectDog(lat, lon); + } + // + // + // @Override public void update(Observable observable, Object o) { if (observable == serviceGps) { - updateGps(o); + updateGps((Notification) o); } else if (observable == serviceXmpp) { - updateXmpp(o); + updateXmpp((Notification) o); } } - public void updateGps(Object o) { - if (o != null && o instanceof String) { - String action = (String) o; - if (action.equals(ServiceGps.NOTIF_NEW_LOCATION)) { - MyLocation loc = onNewLocation(); + public void updateGps(Notification notification) { + if (notification.isAction(ServiceGps.NOTIF_NEW_LOCATION)) { + if (currentLocationProvider == LocationProvider.GPS) { + MyLocation location = new MyLocation((Location) notification.getExtra(ServiceGps.NOTIF_NEW_LOCATION_VALUE_LOCATION)); + onNewLocation(location); shouldVibrate(); - if (serviceXmpp != null && loc != null) { + if (serviceXmpp != null && location != null) { try { - serviceXmpp.sendCommandLocationTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime()); + serviceXmpp.sendCommandLocationTrail(location.getLatitude(), location.getLongitude(), location.getTime()); } catch (Exception ex) { } } @@ -100,22 +233,25 @@ public class ServiceTrackingDog implements Observer { } } - public void updateXmpp(Object o) { - if (o != null && o instanceof String) { - String action = (String) o; - if (o.equals(ServiceXmpp.NOTIF_NEW_LOCATION)) { - onNewLocation(); -// } else if (o.equals(ServiceXmpp.NOTIF_NEW_OBJECT)) { -// } else if (o.equals(ServiceXmpp.NOTIF_START_TRAIL)) { -// } else if (o.equals(ServiceXmpp.NOTIF_STOP_TRAIL)) { + public void updateXmpp(Notification notification) { + if (notification.isAction(ServiceXmpp.NOTIF_NEW_LOCATION)) { + if (currentLocationProvider == LocationProvider.XMPP) { + MyLocation location = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_LOCATION_VALUE_LOCATION); + onNewLocation(location); } +// } else if (notification.isAction(ServiceXmpp.NOTIF_NEW_OBJECT)) { + } else if (notification.isAction(ServiceXmpp.NOTIF_START_TRAIL)) { + currentLocationProvider = LocationProvider.XMPP; + } else if (notification.isAction(ServiceXmpp.NOTIF_STOP_TRAIL)) { + currentLocationProvider = LocationProvider.GPS; } } - public MyLocation onNewLocation() { - MyLocation loc = serviceGps.getCurrentLocation(); - serviceGps.addPoint(loc); - return loc; + public void onNewLocation(MyLocation location) { + if (location != null) { + currentLocation = location; + addPoint(currentLocation); + } } // @@ -151,8 +287,8 @@ public class ServiceTrackingDog implements Observer { private void shouldVibrate() { // Near object vibration check - if (serviceGps.isDogActivated()) { - if (vibrationNearObjectEnabled && serviceGps.isNearObjects()) { + if (isDogActivated()) { + if (vibrationNearObjectEnabled && isNearObjects()) { if (!nearObjectVibration) { nearObjectVibration = true; vibrate(TIME_VIBRATION_NEAR_OBJECT_MS); @@ -171,78 +307,6 @@ public class ServiceTrackingDog implements Observer { public void startLocation() { serviceGps.start(); } - public File getLastExportedTrailFile() { - return serviceGps.getLastExportedTrailFile(); - } - public MyLocation getCurrentLocation() { - return serviceGps.getCurrentLocation(); - } - public void importGpxTrace(File file) { - serviceGps.importGpxTrace(file); - } - public float getOrientation(int deviceOrientation) { - return serviceGps.getOrientation(deviceOrientation); - } - // - public void toggleTraceurActivation() { - serviceGps.toggleTraceurActivation(); - } - public boolean isTraceurActivated() { - return serviceGps.isTraceurActivated(); - } - - public void toggleDogActivation() { - serviceGps.toggleDogActivation(); - } - public boolean isDogActivated() { - return serviceGps.isDogActivated(); - } - // - // - public WayPointLocation addPointObjectTrail() { - return serviceGps.addPointObjectTrail(); - } - public WayPointLocation addPointObjectTrail(MyLocation location) { - return serviceGps.addPointObjectTrail(location); - } - public WayPointLocation addPointObjectDog() { - return serviceGps.addPointObjectDog(); - } - - public MyLocationArray getListGeoPointObjectsTrail() { - return serviceGps.getListGeoPointObjectsTrail(); - } - public MyLocationArray getListGeoPointObjectsDog() { - return serviceGps.getListGeoPointObjectsDog(); - } - - private List foundNearObjects(int distance) { - return serviceGps.foundNearObjects(distance); - } - public boolean isNearObjects() { - return serviceGps.isNearObjects(); - } - public List foundNearObjects() { - return serviceGps.foundNearObjects(); - } - // - // - public void addPoint(MyLocation location) { - serviceGps.addPoint(location); - } - public MyLocationArray getListGeoPointTraceur() { - return serviceGps.getListGeoPointTraceur(); - } - public MyLocationArray getListGeoPointDog() { - return serviceGps.getListGeoPointDog(); - } - public WayPointLocation getPointTrail(double lat, double lon) { - return serviceGps.getPointTrail(lat, lon); - } - public WayPointLocation getPointDog(double lat, double lon) { - return serviceGps.getPointDog(lat, lon); - } - // // // @@ -269,20 +333,6 @@ public class ServiceTrackingDog implements Observer { serviceXmpp.sendPresenceAvailable(); } } - public MyLocation getXmppCurrentLocation() { - MyLocation ret = null; - if (serviceXmpp != null) { - ret = serviceXmpp.getCurrentLocation(); - } - return ret; - } - public MyLocation getXmppLastObjectLocation() { - MyLocation ret = null; - if (serviceXmpp != null) { - ret = serviceXmpp.getLastObjectXmppLocation(); - } - return ret; - } // public void sendCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { if (serviceXmpp != null) { @@ -306,4 +356,65 @@ public class ServiceTrackingDog implements Observer { } // // + + // + private File lastExportedTrailFile = null; + private String getFileName(String prefix) { + SimpleDateFormat formater = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); + String date = formater.format(new Date()); + return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + + appName + "/" + date + "_" + prefix + + ".gpx"; + } + + public File getLastExportedTrailFile() { + return lastExportedTrailFile; + } + private boolean exportDogTraceToGpx() { + File file = new File(getFileName(Gpx.DOG_TRACE_NAME)); + ExportGpx exportGpx = new ExportGpx(file, Gpx.DOG_TRACE_NAME); + exportGpx.setObjects(traces.getListPointObjectsDog()); + exportGpx.setTrace(traces.getListPointDog()); + return exportGpx.export(); + } + + private boolean exportTrailTraceToGpx() { + File file = new File(getFileName(Gpx.TRAIL_TRACE_NAME)); + ExportGpx exportGpx = new ExportGpx(file, Gpx.TRAIL_TRACE_NAME); + exportGpx.setObjects(traces.getListPointObjectsTrail()); + exportGpx.setTrace(traces.getListPointTraceur()); + lastExportedTrailFile = file; + return exportGpx.export(); + } + public void importGpxTrace(File file) { + ImportGpx importGpx = new ImportGpx(file); + try { + List list = importGpx.parse(); + String traceName = importGpx.getTraceName(); + if (traceName.equals(Gpx.TRAIL_TRACE_NAME)) { + lastExportedTrailFile = file; + } + for (int i=0; i } diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java index 5524627..331615b 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java @@ -32,6 +32,7 @@ import java.net.UnknownHostException; import java.util.Observable; import fr.chteufleur.mytrackingdog.QRCodeGeneratorActivity; +import fr.chteufleur.mytrackingdog.models.Notification; import fr.chteufleur.mytrackingdog.models.beans.MyLocation; import fr.chteufleur.mytrackingdog.models.xmpp.commands.ObjectGeolocCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.StartTrailGeolocCommand; @@ -46,6 +47,8 @@ public class ServiceXmpp extends Observable implements PresenceEventListener { public static final String NOTIF_NEW_OBJECT = ServiceXmpp.class.getName()+".newobject"; public static final String NOTIF_START_TRAIL = ServiceXmpp.class.getName()+".starttrail"; public static final String NOTIF_STOP_TRAIL = ServiceXmpp.class.getName()+".stoptrail"; + public static final String NOTIF_NEW_OBJECT_VALUE_LOCATION = NOTIF_NEW_OBJECT+".value.location"; + public static final String NOTIF_NEW_LOCATION_VALUE_LOCATION = NOTIF_NEW_LOCATION+".value.location"; private static final String XMPP_NODE_TRAIL_GEOLOC = "trail_geoloc"; private static final String XMPP_NODE_OBJECT_GEOLOC = "object_geoloc"; @@ -202,8 +205,6 @@ public class ServiceXmpp extends Observable implements PresenceEventListener { // // - private MyLocation currentXmppLocation = null; - private MyLocation lastObjectXmppLocation = null; private void registerCommands() { commandManager.registerCommand(XMPP_NODE_TRAIL_GEOLOC, XMPP_NODE_TRAIL_GEOLOC, new LocalCommandFactory() { @Override @@ -234,35 +235,27 @@ public class ServiceXmpp extends Observable implements PresenceEventListener { public void startTrailGeoloc() { Log.i(TAG, "Start trail"); setChanged(); - notifyObservers(NOTIF_START_TRAIL); + notifyObservers(new Notification(NOTIF_START_TRAIL)); } public void stopTrailGeoloc() { Log.i(TAG, "Stop trail"); setChanged(); - notifyObservers(NOTIF_STOP_TRAIL); + notifyObservers(new Notification(NOTIF_STOP_TRAIL)); } public void addTrailGeoloc(double lat, double lon, long time) { Log.i(TAG, "Add location"); - currentXmppLocation = new MyLocation(lat, lon, time); + MyLocation location = new MyLocation(lat, lon, time); setChanged(); - notifyObservers(NOTIF_NEW_LOCATION); + notifyObservers(new Notification(NOTIF_NEW_LOCATION).addExtra(NOTIF_NEW_LOCATION_VALUE_LOCATION, location)); } public void addObjectGeoloc(double lat, double lon, long time) { Log.i(TAG, "Add object"); - lastObjectXmppLocation = new MyLocation(lat, lon, time); + MyLocation objectLocation = new MyLocation(lat, lon, time); setChanged(); - notifyObservers(NOTIF_NEW_OBJECT); - } - - public MyLocation getCurrentLocation() { - return currentXmppLocation; - } - - public MyLocation getLastObjectXmppLocation() { - return lastObjectXmppLocation; + notifyObservers(new Notification(NOTIF_NEW_OBJECT).addExtra(NOTIF_NEW_OBJECT_VALUE_LOCATION, objectLocation)); } public void sendCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {