Cleanning code.

This commit is contained in:
chteufleur 2018-10-05 16:08:55 +02:00
parent 0176f37d01
commit d253dbd765
3 changed files with 64 additions and 53 deletions

View File

@ -1,6 +1,7 @@
package fr.chteufleur.mytrackingdog; package fr.chteufleur.mytrackingdog;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
@ -132,6 +133,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
map.setBuiltInZoomControls(false); map.setBuiltInZoomControls(false);
map.setMultiTouchControls(true); map.setMultiTouchControls(true);
map.setOnTouchListener(new View.OnTouchListener() { map.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override @Override
public boolean onTouch(View view, MotionEvent motionEvent) { public boolean onTouch(View view, MotionEvent motionEvent) {
map.onTouchEvent(motionEvent); map.onTouchEvent(motionEvent);
@ -169,15 +171,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
@Override @Override
public void onClick(View view) { public void onClick(View view) {
changeStatusTrace(); changeStatusTrace();
try {
if (serviceTrackingDog.isTraceurActivated()) {
serviceTrackingDog.sendCommandStartTrail();
} else {
serviceTrackingDog.sendCommandStopTrail();
}
} catch (XmppStringprepException | InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException ex) {
ex.printStackTrace();
}
} }
}); });
start_stop_dog_trace = findViewById(R.id.start_stop_dog_trace); start_stop_dog_trace = findViewById(R.id.start_stop_dog_trace);
@ -197,11 +190,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
if (loc != null) { if (loc != null) {
GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
addMarker(gp, loc.isFound()); addMarker(gp, loc.isFound());
try {
serviceTrackingDog.sendCommandObjectTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime());
} catch (Exception e) {
e.printStackTrace();
}
} }
} else if (serviceTrackingDog.isDogActivated()) { } else if (serviceTrackingDog.isDogActivated()) {
markAsFound(); markAsFound();
@ -398,7 +386,13 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
serviceTrackingDog.importGpxTrace(new File(data.getStringExtra(FilePicker.EXTRA_FILE_PATH))); serviceTrackingDog.importGpxTrace(new File(data.getStringExtra(FilePicker.EXTRA_FILE_PATH)));
updateDogTrace(); updateDogTrace();
updateTrailTrace(); updateTrailTrace();
calculTrailDistance();
// Update distance
distance = serviceTrackingDog.calculTrailDistance();
if (distance != 0) {
updateDistance();
}
for (MyLocation loc: serviceTrackingDog.getListGeoPointObjectsTrail()) { for (MyLocation loc: serviceTrackingDog.getListGeoPointObjectsTrail()) {
addMarker(new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()), false); addMarker(new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()), false);
} }
@ -410,9 +404,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
addMarker(new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()), isFound); addMarker(new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()), isFound);
} }
if (!serviceTrackingDog.getListGeoPointTraceur().isEmpty()) { if (!serviceTrackingDog.getListGeoPointTraceur().isEmpty()) {
updateDistance(getTextTraceur()); updatePlaceholder(getTextTraceur());
} else if (!serviceTrackingDog.getListGeoPointDog().isEmpty()) { } else if (!serviceTrackingDog.getListGeoPointDog().isEmpty()) {
updateDistance(getTextDog()); updatePlaceholder(getTextDog());
} }
} }
break; break;
@ -567,28 +561,13 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
} }
} }
if (wpls.isEmpty() || !findMarker) { if (wpls.isEmpty() || !findMarker) {
// if no object is near // if no object is near -> add new found object
WayPointLocation wpl = serviceTrackingDog.addPointObjectDog(); WayPointLocation wpl = serviceTrackingDog.addPointObjectDog();
GeoPoint gp = new GeoPoint(wpl.getLatitude(), wpl.getLongitude(), wpl.getAltitude()); GeoPoint gp = new GeoPoint(wpl.getLatitude(), wpl.getLongitude(), wpl.getAltitude());
addMarker(gp, true); addMarker(gp, true);
} }
} }
private void calculTrailDistance() {
List<MyLocation> listLoc = serviceTrackingDog.getListGeoPointTraceur();
MyLocation last = null;
distance = 0;
for (MyLocation loc: listLoc) {
if (last != null) {
distance += loc.distanceTo(last);
}
last = loc;
}
if (distance != 0) {
updateDistance();
}
}
private void updateDogTrace() { private void updateDogTrace() {
MyLocationArray listLoc = serviceTrackingDog.getListGeoPointDog(); MyLocationArray listLoc = serviceTrackingDog.getListGeoPointDog();
if (listLoc.isEmpty()) { if (listLoc.isEmpty()) {
@ -661,21 +640,20 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
MyLocation loc = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_LOCATION_VALUE_LOCATION); MyLocation loc = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_LOCATION_VALUE_LOCATION);
onNewLocation(loc); onNewLocation(loc);
} }
} else if (notification.isAction(ServiceXmpp.NOTIF_NEW_OBJECT)){ } else if (notification.isAction(ServiceXmpp.NOTIF_NEW_OBJECT)) {
if (serviceTrackingDog.isTraceurActivated()) { if (serviceTrackingDog.isTraceurActivated()) {
MyLocation locObj = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_OBJECT_VALUE_LOCATION); MyLocation locObj = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_OBJECT_VALUE_LOCATION);
WayPointLocation loc = serviceTrackingDog.addPointObjectTrail(locObj); GeoPoint gp = new GeoPoint(locObj.getLatitude(), locObj.getLongitude(), locObj.getAltitude());
GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); addMarker(gp, false);
addMarker(gp, loc.isFound());
} }
} else if (notification.isAction(ServiceXmpp.NOTIF_START_TRAIL)){ } else if (notification.isAction(ServiceXmpp.NOTIF_START_TRAIL)) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
changeStatusTrace(); changeStatusTrace();
} }
}); });
} else if (notification.isAction(ServiceXmpp.NOTIF_STOP_TRAIL)){ } else if (notification.isAction(ServiceXmpp.NOTIF_STOP_TRAIL)) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -719,7 +697,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
if (!zoomed) { if (!zoomed) {
IMapController mapController = map.getController(); IMapController mapController = map.getController();
mapController.setZoom(20.0); mapController.setZoom(19.0);
zoomed = true; zoomed = true;
} }
} }
@ -736,7 +714,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
} else if (serviceTrackingDog.isDogActivated()) { } else if (serviceTrackingDog.isDogActivated()) {
text = getTextDog(); text = getTextDog();
} }
updateDistance(text); updatePlaceholder(text);
} }
private String getTextTraceur() { private String getTextTraceur() {
@ -768,7 +746,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
return text; return text;
} }
private void updateDistance(String text) { private void updatePlaceholder(String text) {
if (text != null) { if (text != null) {
this.textViewCurrentLocation.setText(text); this.textViewCurrentLocation.setText(text);
this.textViewCurrentLocation.setVisibility(View.VISIBLE); this.textViewCurrentLocation.setVisibility(View.VISIBLE);

View File

@ -10,10 +10,10 @@ public class Traces {
private static final String TAG = Traces.class.getName(); private static final String TAG = Traces.class.getName();
private MyLocationArray listPointTraceur = new MyLocationArray(); private final MyLocationArray listPointTraceur = new MyLocationArray();
private MyLocationArray listPointDog = new MyLocationArray(); private final MyLocationArray listPointDog = new MyLocationArray();
private MyLocationArray listPointObjectsTrail = new MyLocationArray(); private final MyLocationArray listPointObjectsTrail = new MyLocationArray();
private MyLocationArray listPointObjectsDog = new MyLocationArray(); private final MyLocationArray listPointObjectsDog = new MyLocationArray();
private boolean traceurActivated = false; private boolean traceurActivated = false;
private boolean dogActivated = false; private boolean dogActivated = false;

View File

@ -66,7 +66,7 @@ public class ServiceTrackingDog implements Observer {
this.serviceGps = new ServiceGps(); this.serviceGps = new ServiceGps();
this.serviceGps.addObserver(this); this.serviceGps.addObserver(this);
ServiceXmpp sx = null; ServiceXmpp sx;
try { try {
sx = new ServiceXmpp(resources); sx = new ServiceXmpp(resources);
sx.addObserver(this); sx.addObserver(this);
@ -117,6 +117,9 @@ public class ServiceTrackingDog implements Observer {
} }
return ret; return ret;
} }
public LocationProvider getCurrentLocationProvider() {
return currentLocationProvider;
}
//</editor-fold> //</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Trace management"> //<editor-fold defaultstate="collapsed" desc="Trace management">
@ -125,6 +128,17 @@ public class ServiceTrackingDog implements Observer {
traces.toggleTraceurActivation(); traces.toggleTraceurActivation();
if (!isTraceurActivated()) { if (!isTraceurActivated()) {
exportTrailTraceToGpx(); exportTrailTraceToGpx();
try {
sendXmppCommandStopTrail();
} catch (XmppStringprepException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException | SmackException.NoResponseException e) {
e.printStackTrace();
}
} else {
try {
sendXmppCommandStartTrail();
} catch (XmppStringprepException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException | SmackException.NoResponseException e) {
e.printStackTrace();
}
} }
} }
public boolean isTraceurActivated() { public boolean isTraceurActivated() {
@ -144,9 +158,14 @@ public class ServiceTrackingDog implements Observer {
//</editor-fold> //</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Objects"> //<editor-fold defaultstate="collapsed" desc="Objects">
public WayPointLocation addPointObjectTrail() { public WayPointLocation addPointObjectTrail() {
try {
sendXmppCommandObjectTrail(currentLocation.getLatitude(), currentLocation.getLongitude(), currentLocation.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return addPointObjectTrail(currentLocation); return addPointObjectTrail(currentLocation);
} }
public WayPointLocation addPointObjectTrail(MyLocation location) { private WayPointLocation addPointObjectTrail(MyLocation location) {
WayPointLocation wpl = null; WayPointLocation wpl = null;
if (location != null) { if (location != null) {
wpl = new WayPointLocation(location); wpl = new WayPointLocation(location);
@ -204,6 +223,18 @@ public class ServiceTrackingDog implements Observer {
public WayPointLocation getPointDog(double lat, double lon) { public WayPointLocation getPointDog(double lat, double lon) {
return traces.getPointObjectDog(lat, lon); return traces.getPointObjectDog(lat, lon);
} }
public float calculTrailDistance() {
List<MyLocation> listLoc = getListGeoPointTraceur();
MyLocation last = null;
float distance = 0;
for (MyLocation loc: getListGeoPointTraceur()) {
if (last != null) {
distance += loc.distanceTo(last);
}
last = loc;
}
return distance;
}
//</editor-fold> //</editor-fold>
//</editor-fold> //</editor-fold>
@ -239,7 +270,9 @@ public class ServiceTrackingDog implements Observer {
MyLocation location = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_LOCATION_VALUE_LOCATION); MyLocation location = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_LOCATION_VALUE_LOCATION);
onNewLocation(location); onNewLocation(location);
} }
// } else if (notification.isAction(ServiceXmpp.NOTIF_NEW_OBJECT)) { } else if (notification.isAction(ServiceXmpp.NOTIF_NEW_OBJECT)) {
MyLocation location = (MyLocation) notification.getExtra(ServiceXmpp.NOTIF_NEW_OBJECT_VALUE_LOCATION);
addPointObjectTrail(location);
} else if (notification.isAction(ServiceXmpp.NOTIF_START_TRAIL)) { } else if (notification.isAction(ServiceXmpp.NOTIF_START_TRAIL)) {
currentLocationProvider = LocationProvider.XMPP; currentLocationProvider = LocationProvider.XMPP;
} else if (notification.isAction(ServiceXmpp.NOTIF_STOP_TRAIL)) { } else if (notification.isAction(ServiceXmpp.NOTIF_STOP_TRAIL)) {
@ -334,22 +367,22 @@ public class ServiceTrackingDog implements Observer {
} }
} }
//<editor-fold defaultstate="collapsed" desc="Commands"> //<editor-fold defaultstate="collapsed" desc="Commands">
public void sendCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { private void sendXmppCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
if (serviceXmpp != null) { if (serviceXmpp != null) {
serviceXmpp.sendCommandStartTrail(); serviceXmpp.sendCommandStartTrail();
} }
} }
public void sendCommandStopTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { private void sendXmppCommandStopTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
if (serviceXmpp != null) { if (serviceXmpp != null) {
serviceXmpp.sendCommandStopTrail(); serviceXmpp.sendCommandStopTrail();
} }
} }
public void sendCommandObjectTrail(double lat, double lon, long time) throws Exception { private void sendXmppCommandObjectTrail(double lat, double lon, long time) throws Exception {
if (serviceXmpp != null) { if (serviceXmpp != null) {
serviceXmpp.sendCommandObjectTrail(lat, lon, time); serviceXmpp.sendCommandObjectTrail(lat, lon, time);
} }
} }
public void sendCommandLocationTrail(double lat, double lon, long time) throws Exception { private void sendXmppCommandLocationTrail(double lat, double lon, long time) throws Exception {
if (serviceXmpp != null) { if (serviceXmpp != null) {
serviceXmpp.sendCommandLocationTrail(lat, lon, time); serviceXmpp.sendCommandLocationTrail(lat, lon, time);
} }