Add GPX export.
This commit is contained in:
parent
e883f7eeba
commit
95bd039122
|
|
@ -6,6 +6,7 @@ import android.content.pm.ActivityInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.hardware.GeomagneticField;
|
import android.hardware.GeomagneticField;
|
||||||
|
import android.location.Location;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
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 final String TAG = MainActivity.class.getName();
|
||||||
|
|
||||||
|
public static String appName = "";
|
||||||
|
|
||||||
private MyLocationNewOverlay mLocationOverlay;
|
private MyLocationNewOverlay mLocationOverlay;
|
||||||
private CompassOverlay mCompassOverlay;
|
private CompassOverlay mCompassOverlay;
|
||||||
private IOrientationProvider compass = null;
|
private IOrientationProvider compass = null;
|
||||||
|
|
@ -68,6 +71,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
||||||
|
|
||||||
private FloatingActionButton start_stop_trace;
|
private FloatingActionButton start_stop_trace;
|
||||||
private FloatingActionButton start_stop_dog_trace;
|
private FloatingActionButton start_stop_dog_trace;
|
||||||
|
private FloatingActionButton add_object;
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||||
protected void checkPermissions() {
|
protected void checkPermissions() {
|
||||||
|
|
@ -81,6 +85,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
appName = getString(R.string.app_name);
|
||||||
//handle permissions first, before map is created. not depicted here
|
//handle permissions first, before map is created. not depicted here
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
checkPermissions();
|
checkPermissions();
|
||||||
|
|
@ -148,7 +153,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
||||||
changeStatusDogTrace();
|
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() {
|
add_object.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
|
|
@ -166,8 +172,12 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
||||||
serviceGps.toggleTraceurActivation();
|
serviceGps.toggleTraceurActivation();
|
||||||
if (serviceGps.isTraceurActivated()) {
|
if (serviceGps.isTraceurActivated()) {
|
||||||
this.start_stop_trace.setImageResource(R.drawable.ic_menu_offline);
|
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 {
|
} else {
|
||||||
this.start_stop_trace.setImageResource(R.drawable.ic_menu_mylocation);
|
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();
|
serviceGps.toggleDogActivation();
|
||||||
if (serviceGps.isDogActivated()) {
|
if (serviceGps.isDogActivated()) {
|
||||||
this.start_stop_dog_trace.setImageResource(R.drawable.ic_menu_offline);
|
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 {
|
} else {
|
||||||
this.start_stop_dog_trace.setImageResource(R.drawable.ic_menu_mylocation);
|
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.setLocationManager((LocationManager) getSystemService(Context.LOCATION_SERVICE));
|
||||||
|
serviceGps.setAppName(appName);
|
||||||
serviceGps.start();
|
serviceGps.start();
|
||||||
|
|
||||||
mLocationOverlay.enableFollowLocation();
|
mLocationOverlay.enableFollowLocation();
|
||||||
|
|
@ -359,43 +374,41 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Observable observable, Object o) {
|
public void update(Observable observable, Object o) {
|
||||||
Log.i(TAG, "update");
|
|
||||||
if (observable == serviceGps) {
|
if (observable == serviceGps) {
|
||||||
Log.i(TAG, "service GPS");
|
|
||||||
if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) {
|
if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) {
|
||||||
Log.i(TAG, "new location");
|
Location loc = serviceGps.getCurrentLocation();
|
||||||
GeoPoint currentPoint = serviceGps.getCurrentGeoPoint();
|
if (loc != null) {
|
||||||
Polyline line = null;
|
GeoPoint currentPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
|
||||||
if (serviceGps.isTraceurActivated()) {
|
Polyline line = null;
|
||||||
Log.i(TAG, "polyline traceur");
|
if (serviceGps.isTraceurActivated()) {
|
||||||
map.getController().setCenter(currentPoint);
|
map.getController().setCenter(currentPoint);
|
||||||
|
|
||||||
line = new Polyline(map);
|
line = new Polyline(map);
|
||||||
line.setTitle("Traceur");
|
line.setTitle("Trail");
|
||||||
line.setColor(Color.RED);
|
line.setColor(Color.RED);
|
||||||
line.setPoints(convertListLocation(serviceGps.getListGeoPointTraceur()));
|
line.setPoints(convertListLocation(serviceGps.getListGeoPointTraceur()));
|
||||||
|
|
||||||
} else if (serviceGps.isDogActivated()) {
|
} else if (serviceGps.isDogActivated()) {
|
||||||
Log.i(TAG, "polyline dog");
|
map.getController().setCenter(currentPoint);
|
||||||
map.getController().setCenter(currentPoint);
|
|
||||||
|
|
||||||
line = new Polyline(map);
|
line = new Polyline(map);
|
||||||
line.setTitle("Dog");
|
line.setTitle("Dog");
|
||||||
line.setColor(Color.BLUE);
|
line.setColor(Color.BLUE);
|
||||||
line.setPoints(convertListLocation(serviceGps.getListGeoPointDog()));
|
line.setPoints(convertListLocation(serviceGps.getListGeoPointDog()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (line != null) {
|
if (line != null) {
|
||||||
line.setSubDescription(Polyline.class.getCanonicalName());
|
line.setSubDescription(Polyline.class.getCanonicalName());
|
||||||
line.setWidth(LINE_WIDTH_BIG);
|
line.setWidth(LINE_WIDTH_BIG);
|
||||||
line.setGeodesic(true);
|
line.setGeodesic(true);
|
||||||
line.setInfoWindow(new BasicInfoWindow(R.layout.bonuspack_bubble, map));
|
line.setInfoWindow(new BasicInfoWindow(R.layout.bonuspack_bubble, map));
|
||||||
map.getOverlayManager().add(line);
|
map.getOverlayManager().add(line);
|
||||||
map.invalidate();
|
map.invalidate();
|
||||||
}
|
}
|
||||||
float orientation = serviceGps.getOrientation(deviceOrientation);
|
float orientation = serviceGps.getOrientation(deviceOrientation);
|
||||||
if (orientation >= 0) {
|
if (orientation >= 0) {
|
||||||
map.setMapOrientation(orientation);
|
map.setMapOrientation(orientation);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 = "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>";
|
||||||
|
private static final String GPX_HEADER = "<gpx xmlns=\"http://www.topografix.com/GPX/1/0\" version=\"1.0\" creator=\"fr.chteufleur.mytrackingdog\">";
|
||||||
|
private static final String GPX_FOOTER = "</gpx>";
|
||||||
|
|
||||||
|
private final String traceName;
|
||||||
|
|
||||||
|
private final List<MyLocation> listObjects = new ArrayList<>();
|
||||||
|
private final List<MyLocation> listTracePoint = new ArrayList<>();
|
||||||
|
|
||||||
|
public ExportGpx(File filePath, String traceName) {
|
||||||
|
super(filePath);
|
||||||
|
this.traceName = traceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setObjects(List<MyLocation> objects) {
|
||||||
|
this.listObjects.clear();
|
||||||
|
this.listObjects.addAll(objects);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTrace(List<MyLocation> 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<trk>");
|
||||||
|
writer.write("\n<trkseg>");
|
||||||
|
writer.write("\n<name>"+this.traceName+"</name>");
|
||||||
|
for (MyLocation loc: listTracePoint) {
|
||||||
|
writer.write("\n"+loc.toTracePoint());
|
||||||
|
}
|
||||||
|
writer.write("\n</trkseg>");
|
||||||
|
writer.write("\n</trk>");
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -28,7 +28,7 @@ public class Traces {
|
||||||
listPointDog.add(point);
|
listPointDog.add(point);
|
||||||
}
|
}
|
||||||
public void addPointObject(MyLocation point) {
|
public void addPointObject(MyLocation point) {
|
||||||
Log.i(TAG, "add point object");
|
Log.i(TAG, "add point object ("+point.toString()+")");
|
||||||
listPointObjects.add(point);
|
listPointObjects.add(point);
|
||||||
}
|
}
|
||||||
public void addCurrentPoint(MyLocation point) {
|
public void addCurrentPoint(MyLocation point) {
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,23 @@ public class MyLocation extends Location {
|
||||||
public MyLocation(Location l) {
|
public MyLocation(Location l) {
|
||||||
super(l);
|
super(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String toWayPoint() {
|
||||||
|
String ret = "<wpt lat=\""+getLatitude()+"\" lon=\""+getLongitude()+"\">";
|
||||||
|
ret += "<time>"+getTime()+"</time>";
|
||||||
|
ret += "</wpt>";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toTracePoint() {
|
||||||
|
String ret = "<trkpt lat=\""+getLatitude()+"\" lon=\""+getLongitude()+"\">";
|
||||||
|
ret += "<time>"+getTime()+"</time>";
|
||||||
|
ret += "</trkpt>";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("lat=%,4f ; lon=%,4f ; time=%d", getLatitude(), getLongitude(), getTime());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,16 @@ import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Environment;
|
||||||
import android.util.Log;
|
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.List;
|
||||||
import java.util.Observable;
|
import java.util.Observable;
|
||||||
|
|
||||||
|
import fr.chteufleur.mytrackingdog.models.ExportGpx;
|
||||||
import fr.chteufleur.mytrackingdog.models.Traces;
|
import fr.chteufleur.mytrackingdog.models.Traces;
|
||||||
import fr.chteufleur.mytrackingdog.models.beans.MyLocation;
|
import fr.chteufleur.mytrackingdog.models.beans.MyLocation;
|
||||||
|
|
||||||
|
|
@ -21,12 +24,16 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
||||||
|
|
||||||
private LocationManager locationManager;
|
private LocationManager locationManager;
|
||||||
private MyLocation currentLocation = null;
|
private MyLocation currentLocation = null;
|
||||||
|
private String appName = "";
|
||||||
|
|
||||||
private final Traces traces = new Traces();
|
private final Traces traces = new Traces();
|
||||||
|
|
||||||
public void setLocationManager(LocationManager locationManager) {
|
public void setLocationManager(LocationManager locationManager) {
|
||||||
this.locationManager = locationManager;
|
this.locationManager = locationManager;
|
||||||
}
|
}
|
||||||
|
public void setAppName(String appName) {
|
||||||
|
this.appName = appName;
|
||||||
|
}
|
||||||
|
|
||||||
public MyLocation getCurrentLocation() {
|
public MyLocation getCurrentLocation() {
|
||||||
return currentLocation;
|
return currentLocation;
|
||||||
|
|
@ -89,15 +96,11 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public GeoPoint getCurrentGeoPoint() {
|
|
||||||
if (currentLocation == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude(), currentLocation.getAltitude());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toggleTraceurActivation() {
|
public void toggleTraceurActivation() {
|
||||||
traces.toggleTraceurActivation();
|
traces.toggleTraceurActivation();
|
||||||
|
if (!isTraceurActivated()) {
|
||||||
|
exportTrailTraceToGpx();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public boolean isTraceurActivated() {
|
public boolean isTraceurActivated() {
|
||||||
return traces.isTraceurActivated();
|
return traces.isTraceurActivated();
|
||||||
|
|
@ -105,6 +108,9 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
||||||
|
|
||||||
public void toggleDogActivation() {
|
public void toggleDogActivation() {
|
||||||
traces.toggleDogActivation();
|
traces.toggleDogActivation();
|
||||||
|
if (!isDogActivated()) {
|
||||||
|
exportDogTraceToGpx();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public boolean isDogActivated() {
|
public boolean isDogActivated() {
|
||||||
return traces.isDogActivated();
|
return traces.isDogActivated();
|
||||||
|
|
@ -126,4 +132,29 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
||||||
public List<MyLocation> getListGeoPointObjects() {
|
public List<MyLocation> getListGeoPointObjects() {
|
||||||
return traces.getListPointObjects();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue