Reorganization of ServiceGps code.

This commit is contained in:
chteufleur 2018-10-04 16:30:13 +02:00
parent b856f3f4e1
commit 2dd8f75822
2 changed files with 67 additions and 65 deletions

View File

@ -1,8 +0,0 @@
package fr.chteufleur.mytrackingdog.services;
public interface IServiceGps {
public void start();
public void stop();
}

View File

@ -31,7 +31,7 @@ import fr.chteufleur.mytrackingdog.models.beans.MyLocationArray;
import fr.chteufleur.mytrackingdog.models.beans.TraceLocation; import fr.chteufleur.mytrackingdog.models.beans.TraceLocation;
import fr.chteufleur.mytrackingdog.models.beans.WayPointLocation; import fr.chteufleur.mytrackingdog.models.beans.WayPointLocation;
public class ServiceGps extends Observable implements IServiceGps, LocationListener { public class ServiceGps extends Observable implements LocationListener {
private static final String TAG = ServiceGps.class.getName(); private static final String TAG = ServiceGps.class.getName();
public static final String NOTIF_NEW_LOCATION = ServiceGps.class.getName()+".newlocation"; public static final String NOTIF_NEW_LOCATION = ServiceGps.class.getName()+".newlocation";
@ -67,6 +67,24 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
return currentLocation; return currentLocation;
} }
public void start() {
try {
// if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
Log.i(TAG, "Start location");
// } else {
// Log.i(TAG, "Start location failed (provider not enabled)");
// }
} catch (SecurityException ex) {
Log.e(TAG, "Location permission is not activated.", ex);
}
}
public void stop() {
this.locationManager.removeUpdates(this);
Log.i(TAG, "Stop location");
}
public float getOrientation(int deviceOrientation) { public float getOrientation(int deviceOrientation) {
float ret = -1; float ret = -1;
if (currentLocation != null && currentLocation.getSpeed() >= 0.01) { if (currentLocation != null && currentLocation.getSpeed() >= 0.01) {
@ -86,26 +104,7 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
return ret; return ret;
} }
@Override //<editor-fold defaultstate="collapsed" desc="Vibration management">
public void start() {
try {
// if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
Log.i(TAG, "Start location");
// } else {
// Log.i(TAG, "Start location failed (provider not enabled)");
// }
} catch (SecurityException ex) {
Log.e(TAG, "Location permission is not activated.", ex);
}
}
@Override
public void stop() {
this.locationManager.removeUpdates(this);
Log.i(TAG, "Stop location");
}
private final Vibrator vibrator; private final Vibrator vibrator;
private boolean vibrationNearObjectEnabled = false; private boolean vibrationNearObjectEnabled = false;
private boolean nearObjectVibration = false; private boolean nearObjectVibration = false;
@ -123,9 +122,18 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
return this.vibrationNearObjectEnabled; return this.vibrationNearObjectEnabled;
} }
public void addPoint(MyLocation location) { private void vibrate(int timeVibrationMs) {
traces.addCurrentPoint(location); if (this.vibrator != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.vibrator.vibrate(VibrationEffect.createOneShot(timeVibrationMs, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
this.vibrator.vibrate(timeVibrationMs);
}
}
} }
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="GPS Callback">
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged"); Log.i(TAG, "onLocationChanged");
@ -145,16 +153,6 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
} }
} }
private void vibrate(int timeVibrationMs) {
if (this.vibrator != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.vibrator.vibrate(VibrationEffect.createOneShot(timeVibrationMs, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
this.vibrator.vibrate(timeVibrationMs);
}
}
}
@Override @Override
public void onStatusChanged(String s, int i, Bundle bundle) { public void onStatusChanged(String s, int i, Bundle bundle) {
Log.i(TAG, "onStatusChanged("+s+", "+i+", null)"); Log.i(TAG, "onStatusChanged("+s+", "+i+", null)");
@ -183,7 +181,10 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
stop(); stop();
} }
} }
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Trace management">
//<editor-fold defaultstate="collapsed" desc="Trace activation">
public void toggleTraceurActivation() { public void toggleTraceurActivation() {
traces.toggleTraceurActivation(); traces.toggleTraceurActivation();
if (!isTraceurActivated()) { if (!isTraceurActivated()) {
@ -203,7 +204,8 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
public boolean isDogActivated() { public boolean isDogActivated() {
return traces.isDogActivated(); return traces.isDogActivated();
} }
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Objects">
public WayPointLocation addPointObjectTrail() { public WayPointLocation addPointObjectTrail() {
return addPointObjectTrail(currentLocation); return addPointObjectTrail(currentLocation);
} }
@ -227,33 +229,14 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
return wpl; return wpl;
} }
public MyLocationArray getListGeoPointTraceur() {
return traces.getListPointTraceur();
}
public MyLocationArray getListGeoPointDog() {
return traces.getListPointDog();
}
public MyLocationArray getListGeoPointObjectsTrail() { public MyLocationArray getListGeoPointObjectsTrail() {
return traces.getListPointObjectsTrail(); return traces.getListPointObjectsTrail();
} }
public MyLocationArray getListGeoPointObjectsDog() { public MyLocationArray getListGeoPointObjectsDog() {
return traces.getListPointObjectsDog(); return traces.getListPointObjectsDog();
} }
public WayPointLocation getPointTrail(double lat, double lon) {
return traces.getPointObjectTrail(lat, lon);
}
public WayPointLocation getPointDog(double lat, double lon) {
return traces.getPointObjectDog(lat, lon);
}
public boolean isNearObjects() { private List<WayPointLocation> foundNearObjects(int distance) {
return !foundNearObjects(20).isEmpty();
}
public List<WayPointLocation> foundNearObjects() {
return foundNearObjects(10);
}
public List<WayPointLocation> foundNearObjects(int distance) {
List<WayPointLocation> ret = new ArrayList<>(); List<WayPointLocation> ret = new ArrayList<>();
MyLocation curLoc = currentLocation; MyLocation curLoc = currentLocation;
for (MyLocation ml: getListGeoPointObjectsTrail()) { for (MyLocation ml: getListGeoPointObjectsTrail()) {
@ -263,8 +246,34 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
} }
return ret; return ret;
} }
private boolean isNearObjects() {
return !foundNearObjects(20).isEmpty();
}
public List<WayPointLocation> foundNearObjects() {
return foundNearObjects(10);
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Traces">
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);
}
//</editor-fold>
//</editor-fold>
public String getFileName(String prefix) { //<editor-fold defaultstate="collapsed" desc="Export file management">
private String getFileName(String prefix) {
SimpleDateFormat formater = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); SimpleDateFormat formater = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String date = formater.format(new Date()); String date = formater.format(new Date());
return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" return Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
@ -272,7 +281,7 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
+ ".gpx"; + ".gpx";
} }
public boolean exportDogTraceToGpx() { private boolean exportDogTraceToGpx() {
File file = new File(getFileName(Gpx.DOG_TRACE_NAME)); File file = new File(getFileName(Gpx.DOG_TRACE_NAME));
ExportGpx exportGpx = new ExportGpx(file, Gpx.DOG_TRACE_NAME); ExportGpx exportGpx = new ExportGpx(file, Gpx.DOG_TRACE_NAME);
exportGpx.setObjects(traces.getListPointObjectsDog()); exportGpx.setObjects(traces.getListPointObjectsDog());
@ -280,7 +289,7 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
return exportGpx.export(); return exportGpx.export();
} }
public boolean exportTrailTraceToGpx() { private boolean exportTrailTraceToGpx() {
File file = new File(getFileName(Gpx.TRAIL_TRACE_NAME)); File file = new File(getFileName(Gpx.TRAIL_TRACE_NAME));
ExportGpx exportGpx = new ExportGpx(file, Gpx.TRAIL_TRACE_NAME); ExportGpx exportGpx = new ExportGpx(file, Gpx.TRAIL_TRACE_NAME);
exportGpx.setObjects(traces.getListPointObjectsTrail()); exportGpx.setObjects(traces.getListPointObjectsTrail());
@ -323,4 +332,5 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
e.printStackTrace(); e.printStackTrace();
} }
} }
//</editor-fold>
} }