diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java index 8bab6aa..ecc1c27 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java @@ -6,6 +6,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.os.Build; import android.os.Bundle; @@ -50,6 +51,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu public static final String TAG = MainActivity.class.getName(); + public static String appName = ""; + private MyLocationNewOverlay mLocationOverlay; private CompassOverlay mCompassOverlay; private IOrientationProvider compass = null; @@ -68,6 +71,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu private FloatingActionButton start_stop_trace; private FloatingActionButton start_stop_dog_trace; + private FloatingActionButton add_object; @RequiresApi(api = Build.VERSION_CODES.M) protected void checkPermissions() { @@ -81,6 +85,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + appName = getString(R.string.app_name); //handle permissions first, before map is created. not depicted here if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { checkPermissions(); @@ -148,7 +153,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu changeStatusDogTrace(); } }); - FloatingActionButton add_object = findViewById(R.id.add_object); + add_object = findViewById(R.id.add_object); + add_object.setVisibility(View.GONE); add_object.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -166,8 +172,12 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu serviceGps.toggleTraceurActivation(); if (serviceGps.isTraceurActivated()) { this.start_stop_trace.setImageResource(R.drawable.ic_menu_offline); + this.add_object.setVisibility(View.VISIBLE); + this.start_stop_dog_trace.setVisibility(View.GONE); } else { this.start_stop_trace.setImageResource(R.drawable.ic_menu_mylocation); + this.add_object.setVisibility(View.GONE); + this.start_stop_dog_trace.setVisibility(View.VISIBLE); } } @@ -175,8 +185,12 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu serviceGps.toggleDogActivation(); if (serviceGps.isDogActivated()) { this.start_stop_dog_trace.setImageResource(R.drawable.ic_menu_offline); + this.add_object.setVisibility(View.GONE); + this.start_stop_trace.setVisibility(View.GONE); } else { this.start_stop_dog_trace.setImageResource(R.drawable.ic_menu_mylocation); + this.add_object.setVisibility(View.VISIBLE); + this.start_stop_trace.setVisibility(View.VISIBLE); } } @@ -225,6 +239,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu } serviceGps.setLocationManager((LocationManager) getSystemService(Context.LOCATION_SERVICE)); + serviceGps.setAppName(appName); serviceGps.start(); mLocationOverlay.enableFollowLocation(); @@ -359,43 +374,41 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu @Override public void update(Observable observable, Object o) { - Log.i(TAG, "update"); if (observable == serviceGps) { - Log.i(TAG, "service GPS"); if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) { - Log.i(TAG, "new location"); - GeoPoint currentPoint = serviceGps.getCurrentGeoPoint(); - Polyline line = null; - if (serviceGps.isTraceurActivated()) { - Log.i(TAG, "polyline traceur"); - map.getController().setCenter(currentPoint); + Location loc = serviceGps.getCurrentLocation(); + if (loc != null) { + GeoPoint currentPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); + Polyline line = null; + if (serviceGps.isTraceurActivated()) { + map.getController().setCenter(currentPoint); - line = new Polyline(map); - line.setTitle("Traceur"); - line.setColor(Color.RED); - line.setPoints(convertListLocation(serviceGps.getListGeoPointTraceur())); + line = new Polyline(map); + line.setTitle("Trail"); + line.setColor(Color.RED); + line.setPoints(convertListLocation(serviceGps.getListGeoPointTraceur())); - } else if (serviceGps.isDogActivated()) { - Log.i(TAG, "polyline dog"); - map.getController().setCenter(currentPoint); + } else if (serviceGps.isDogActivated()) { + map.getController().setCenter(currentPoint); - line = new Polyline(map); - line.setTitle("Dog"); - line.setColor(Color.BLUE); - line.setPoints(convertListLocation(serviceGps.getListGeoPointDog())); - } + line = new Polyline(map); + line.setTitle("Dog"); + line.setColor(Color.BLUE); + line.setPoints(convertListLocation(serviceGps.getListGeoPointDog())); + } - if (line != null) { - line.setSubDescription(Polyline.class.getCanonicalName()); - line.setWidth(LINE_WIDTH_BIG); - line.setGeodesic(true); - line.setInfoWindow(new BasicInfoWindow(R.layout.bonuspack_bubble, map)); - map.getOverlayManager().add(line); - map.invalidate(); - } - float orientation = serviceGps.getOrientation(deviceOrientation); - if (orientation >= 0) { - map.setMapOrientation(orientation); + if (line != null) { + line.setSubDescription(Polyline.class.getCanonicalName()); + line.setWidth(LINE_WIDTH_BIG); + line.setGeodesic(true); + line.setInfoWindow(new BasicInfoWindow(R.layout.bonuspack_bubble, map)); + map.getOverlayManager().add(line); + map.invalidate(); + } + float orientation = serviceGps.getOrientation(deviceOrientation); + if (orientation >= 0) { + map.setMapOrientation(orientation); + } } } } diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/ExportGpx.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/ExportGpx.java new file mode 100644 index 0000000..13fff5f --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/ExportGpx.java @@ -0,0 +1,83 @@ +package fr.chteufleur.mytrackingdog.models; + +import android.os.Environment; +import android.util.Log; + +import java.io.File; +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.List; + +import fr.chteufleur.mytrackingdog.models.beans.MyLocation; + +public class ExportGpx extends Gpx { + + private static final String TAG = Traces.class.getName(); + + private static final String XML_HEADER = ""; + private static final String GPX_HEADER = ""; + private static final String GPX_FOOTER = ""; + + private final String traceName; + + private final List listObjects = new ArrayList<>(); + private final List listTracePoint = new ArrayList<>(); + + public ExportGpx(File filePath, String traceName) { + super(filePath); + this.traceName = traceName; + } + + + public void setObjects(List objects) { + this.listObjects.clear(); + this.listObjects.addAll(objects); + } + + public void setTrace(List points) { + this.listTracePoint.clear(); + this.listTracePoint.addAll(points); + } + + public boolean export() { + if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { + Log.e(TAG, "External media not mounted."); + return false; + } + + File parent = filePath.getParentFile(); + if (!parent.exists()) { + if (parent.mkdir()) { + Log.i(TAG, "Create directory : "+parent.getAbsolutePath()); + } else { + Log.e(TAG, "Fail to create dir : "+parent.getAbsolutePath()); + } + + } + + boolean ret = false; + try { + FileWriter writer = new FileWriter(filePath); + writer.write(XML_HEADER+"\n"+GPX_HEADER); + for (MyLocation loc: listObjects) { + writer.write("\n"+loc.toWayPoint()); + } + + writer.write("\n"); + writer.write("\n"); + writer.write("\n"+this.traceName+""); + for (MyLocation loc: listTracePoint) { + writer.write("\n"+loc.toTracePoint()); + } + writer.write("\n"); + writer.write("\n"); + writer.append("\n"+GPX_FOOTER); + writer.close(); + ret = true; + Log.i(TAG, "Export successful to : "+filePath.getAbsolutePath()); + } catch (Exception e) { + Log.e(TAG, "Failed to export to GPX file : "+filePath.getAbsolutePath(), e); + } + return ret; + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/Gpx.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/Gpx.java new file mode 100644 index 0000000..eeedcbc --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/Gpx.java @@ -0,0 +1,12 @@ +package fr.chteufleur.mytrackingdog.models; + +import java.io.File; + +public class Gpx { + + protected final File filePath; + + public Gpx(File filePath) { + this.filePath = filePath; + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/Traces.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/Traces.java index 0eeb84b..b3cd513 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/models/Traces.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/Traces.java @@ -28,7 +28,7 @@ public class Traces { listPointDog.add(point); } public void addPointObject(MyLocation point) { - Log.i(TAG, "add point object"); + Log.i(TAG, "add point object ("+point.toString()+")"); listPointObjects.add(point); } public void addCurrentPoint(MyLocation point) { diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/beans/MyLocation.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/beans/MyLocation.java index d356053..fe07ad6 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/models/beans/MyLocation.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/beans/MyLocation.java @@ -7,4 +7,23 @@ public class MyLocation extends Location { public MyLocation(Location l) { super(l); } + + + public String toWayPoint() { + String ret = ""; + ret += ""; + ret += ""; + return ret; + } + + public String toTracePoint() { + String ret = ""; + ret += ""; + ret += ""; + return ret; + } + @Override + public String toString() { + return String.format("lat=%,4f ; lon=%,4f ; time=%d", getLatitude(), getLongitude(), getTime()); + } } 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 6b0a7c0..775c14f 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceGps.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceGps.java @@ -4,13 +4,16 @@ import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; +import android.os.Environment; import android.util.Log; -import org.osmdroid.util.GeoPoint; - +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; import java.util.Observable; +import fr.chteufleur.mytrackingdog.models.ExportGpx; import fr.chteufleur.mytrackingdog.models.Traces; import fr.chteufleur.mytrackingdog.models.beans.MyLocation; @@ -21,12 +24,16 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe private LocationManager locationManager; private MyLocation currentLocation = null; + private String appName = ""; private final Traces traces = new Traces(); public void setLocationManager(LocationManager locationManager) { this.locationManager = locationManager; } + public void setAppName(String appName) { + this.appName = appName; + } public MyLocation getCurrentLocation() { return currentLocation; @@ -89,15 +96,11 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe stop(); } - public GeoPoint getCurrentGeoPoint() { - if (currentLocation == null) { - return null; - } - return new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude(), currentLocation.getAltitude()); - } - public void toggleTraceurActivation() { traces.toggleTraceurActivation(); + if (!isTraceurActivated()) { + exportTrailTraceToGpx(); + } } public boolean isTraceurActivated() { return traces.isTraceurActivated(); @@ -105,6 +108,9 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe public void toggleDogActivation() { traces.toggleDogActivation(); + if (!isDogActivated()) { + exportDogTraceToGpx(); + } } public boolean isDogActivated() { return traces.isDogActivated(); @@ -126,4 +132,29 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe public List getListGeoPointObjects() { return traces.getListPointObjects(); } + + public 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 + "/" + appName + "_" + date + "_" + prefix + + ".gpx"; + } + + public boolean exportDogTraceToGpx() { + File file = new File(getFileName("dog")); + ExportGpx exportGpx = new ExportGpx(file, "dog"); + exportGpx.setTrace(traces.getListPointDog()); + return exportGpx.export(); + } + + public boolean exportTrailTraceToGpx() { + SimpleDateFormat formater = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); + String date = formater.format(new Date()); + File file = new File(getFileName("trail")); + ExportGpx exportGpx = new ExportGpx(file, "trail"); + exportGpx.setObjects(traces.getListPointObjects()); + exportGpx.setTrace(traces.getListPointTraceur()); + return exportGpx.export(); + } }