Re-organize data flow.

This commit is contained in:
Chteufleur 2018-08-13 22:43:42 +02:00
parent 52e2689603
commit 95ad89a181
3 changed files with 167 additions and 77 deletions

View File

@ -11,9 +11,11 @@ import android.location.LocationManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.Surface; import android.view.Surface;
@ -37,43 +39,33 @@ import org.osmdroid.views.overlay.infowindow.BasicInfoWindow;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import fr.chteufleur.mytrackingdog.services.IServiceGps;
import fr.chteufleur.mytrackingdog.services.ServiceGps; import fr.chteufleur.mytrackingdog.services.ServiceGps;
public class MainActivity extends AppCompatActivity implements IOrientationConsumer, Observer { public class MainActivity extends AppCompatActivity implements IOrientationConsumer, Observer {
public static final String TAG = "MainActivity"; public static final String TAG = MainActivity.class.getName();
private MyLocationNewOverlay mLocationOverlay; private MyLocationNewOverlay mLocationOverlay;
private CompassOverlay mCompassOverlay; private CompassOverlay mCompassOverlay;
private IOrientationProvider compass = null; private IOrientationProvider compass = null;
private IServiceGps serviceGps = null; private ServiceGps serviceGps = null;
private Context ctx = null; private Context ctx = null;
private MapView map = null; private MapView map = null;
private ImageButton btFollowMe; private ImageButton btFollowMe;
private Float trueNorth = 0f;
private int deviceOrientation = 0; private int deviceOrientation = 0;
private boolean zoomed = false; private boolean zoomed = false;
private final int REQUEST_CODE_ASK_PERMISSION = 123; private final int REQUEST_CODE_ASK_PERMISSION = 123;
private static final float LINE_WIDTH_BIG = 12; private static final float LINE_WIDTH_BIG = 12;
private List<GeoPoint> lGeoPointTraceur = new ArrayList<>();
private List<GeoPoint> lGeoPointDog = new ArrayList<>();
private List<GeoPoint> lGeoPointObjects = new ArrayList<>();
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;
private boolean traceurActivated = false;
private boolean dogActivated = false;
@RequiresApi(api = Build.VERSION_CODES.M) @RequiresApi(api = Build.VERSION_CODES.M)
protected void checkPermissions() { protected void checkPermissions() {
@ -100,7 +92,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
// Keep screen ON // Keep screen ON
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
map = (MapView) findViewById(R.id.map); map = findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK); map.setTileSource(TileSourceFactory.MAPNIK);
// Add the ability to zoom with 2 fingers // Add the ability to zoom with 2 fingers
@ -126,7 +118,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
mLocationOverlay.setOptionsMenuEnabled(true); mLocationOverlay.setOptionsMenuEnabled(true);
// Follow me // Follow me
btFollowMe = (ImageButton) findViewById(R.id.ic_follow_me); btFollowMe = findViewById(R.id.ic_follow_me);
btFollowMe.setOnClickListener(new View.OnClickListener() { btFollowMe.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -140,21 +132,21 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
} }
}); });
start_stop_trace = (FloatingActionButton) findViewById(R.id.start_stop_trace); start_stop_trace = findViewById(R.id.start_stop_trace);
start_stop_trace.setOnClickListener(new View.OnClickListener() { start_stop_trace.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
changeStatusTrace(); changeStatusTrace();
} }
}); });
start_stop_dog_trace = (FloatingActionButton) findViewById(R.id.start_stop_dog_trace); start_stop_dog_trace = findViewById(R.id.start_stop_dog_trace);
start_stop_dog_trace.setOnClickListener(new View.OnClickListener() { start_stop_dog_trace.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
changeStatusDogTrace(); changeStatusDogTrace();
} }
}); });
add_object = (FloatingActionButton) findViewById(R.id.add_object); FloatingActionButton add_object = findViewById(R.id.add_object);
add_object.setOnClickListener(new View.OnClickListener() { add_object.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@ -164,13 +156,13 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
if (serviceGps == null) { if (serviceGps == null) {
serviceGps = new ServiceGps(); serviceGps = new ServiceGps();
((Observable) serviceGps).addObserver(this); serviceGps.addObserver(this);
} }
} }
private void changeStatusTrace() { private void changeStatusTrace() {
this.traceurActivated = !this.traceurActivated; serviceGps.toggleTraceurActivation();
if (this.traceurActivated) { if (serviceGps.isTraceurActivated()) {
this.start_stop_trace.setImageResource(R.drawable.ic_menu_offline); this.start_stop_trace.setImageResource(R.drawable.ic_menu_offline);
} else { } else {
this.start_stop_trace.setImageResource(R.drawable.ic_menu_mylocation); this.start_stop_trace.setImageResource(R.drawable.ic_menu_mylocation);
@ -178,14 +170,25 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
} }
private void changeStatusDogTrace() { private void changeStatusDogTrace() {
this.dogActivated = !this.dogActivated; serviceGps.toggleDogActivation();
if (this.dogActivated) { 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);
} else { } else {
this.start_stop_dog_trace.setImageResource(R.drawable.ic_menu_mylocation); this.start_stop_dog_trace.setImageResource(R.drawable.ic_menu_mylocation);
} }
} }
private void addMarker() {
GeoPoint gp = serviceGps.addPointObject();
Marker marker = new Marker(map);
marker.setPosition(gp);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
marker.setTitle("Object");
marker.setDraggable(false);
map.getOverlays().add(marker);
}
@Override @Override
public void onResume(){ public void onResume(){
super.onResume(); super.onResume();
@ -194,8 +197,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
if (!"Android-x86".equalsIgnoreCase(Build.BRAND)) { if (!"Android-x86".equalsIgnoreCase(Build.BRAND)) {
//lock the device in current screen orientation //lock the device in current screen orientation
int orientation; int orientation;
int rotation = ((WindowManager) getSystemService( int rotation = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
switch (rotation) { switch (rotation) {
case Surface.ROTATION_0: case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
@ -209,6 +211,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
this.deviceOrientation = 180; this.deviceOrientation = 180;
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break; break;
case Surface.ROTATION_270:
default: default:
this.deviceOrientation = 270; this.deviceOrientation = 270;
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
@ -218,7 +221,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
setRequestedOrientation(orientation); setRequestedOrientation(orientation);
} }
((ServiceGps) serviceGps).setLocationManager((LocationManager) getSystemService(ctx.LOCATION_SERVICE)); serviceGps.setLocationManager((LocationManager) getSystemService(Context.LOCATION_SERVICE));
serviceGps.start(); serviceGps.start();
mLocationOverlay.enableFollowLocation(); mLocationOverlay.enableFollowLocation();
@ -292,10 +295,10 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) { switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSION: case REQUEST_CODE_ASK_PERMISSION:
Toast.makeText(ctx, "Permissions granted", Toast.LENGTH_LONG); Toast.makeText(ctx, "Permissions granted", Toast.LENGTH_LONG).show();
break; break;
default: default:
@ -308,7 +311,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
@Override @Override
public void onOrientationChanged(float orientationToMagneticNorth, IOrientationProvider source) { public void onOrientationChanged(float orientationToMagneticNorth, IOrientationProvider source) {
//note, on devices without a compass this never fires... //note, on devices without a compass this never fires...
Location location = ((ServiceGps) serviceGps).getCurrentLocation(); Location location = serviceGps.getCurrentLocation();
if (location == null) { if (location == null) {
return ; return ;
} }
@ -322,34 +325,30 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
//only use the compass bit if we aren't moving, since gps is more accurate when we are moving //only use the compass bit if we aren't moving, since gps is more accurate when we are moving
if (gpsspeed < 0.01) { if (gpsspeed < 0.01) {
GeomagneticField gf = new GeomagneticField(lat, lon, alt, timeOfFix); GeomagneticField gf = new GeomagneticField(lat, lon, alt, timeOfFix);
trueNorth = orientationToMagneticNorth + gf.getDeclination(); float trueNorth = orientationToMagneticNorth + gf.getDeclination();
gf = null; gf = null;
synchronized (trueNorth) { if (trueNorth > 360.0f) {
if (trueNorth > 360.0f) { trueNorth = trueNorth - 360.0f;
trueNorth = trueNorth - 360.0f; }
}
float actualHeading = 0f;
//this part adjusts the desired map rotation based on device orientation and compass heading //this part adjusts the desired map rotation based on device orientation and compass heading
float t = (360 - trueNorth - this.deviceOrientation); float t = (360 - trueNorth - this.deviceOrientation);
if (t < 0) { if (t < 0) {
t += 360; t += 360;
} }
if (t > 360) { if (t > 360) {
t -= 360; t -= 360;
} }
actualHeading = t; //help smooth everything out
//help smooth everything out t = (int) t;
t = (int) t; t = t / 5;
t = t / 5; t = (int) t;
t = (int) t; t = t * 5;
t = t * 5; map.setMapOrientation(t);
map.setMapOrientation(t); if (!zoomed) {
if (!zoomed) { IMapController mapController = map.getController();
IMapController mapController = map.getController(); mapController.setZoom(20.0);
mapController.setZoom(20.0); zoomed = true;
zoomed = true;
}
} }
} }
} }
@ -357,27 +356,30 @@ 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) {
if (o instanceof String && (String) o == ServiceGps.NOTIF_NEW_LOCATION) { Log.i(TAG, "service GPS");
GeoPoint currentPoint = ((ServiceGps) serviceGps).getCurrentGeoPoint(); if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) {
Log.i(TAG, "new location");
GeoPoint currentPoint = serviceGps.getCurrentGeoPoint();
Polyline line = null; Polyline line = null;
if (traceurActivated) { if (serviceGps.isTraceurActivated()) {
lGeoPointTraceur.add(((ServiceGps) serviceGps).getCurrentGeoPoint()); Log.i(TAG, "polyline traceur");
map.getController().setCenter(currentPoint); map.getController().setCenter(currentPoint);
line = new Polyline(map); line = new Polyline(map);
line.setTitle("Traceur"); line.setTitle("Traceur");
line.setColor(Color.RED); line.setColor(Color.RED);
line.setPoints(lGeoPointTraceur); line.setPoints(serviceGps.getListGeoPointTraceur());
} else if (dogActivated) { } else if (serviceGps.isDogActivated()) {
lGeoPointDog.add(((ServiceGps) serviceGps).getCurrentGeoPoint()); 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(lGeoPointDog); line.setPoints(serviceGps.getListGeoPointDog());
} }
if (line != null) { if (line != null) {
@ -388,23 +390,11 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
map.getOverlayManager().add(line); map.getOverlayManager().add(line);
map.invalidate(); map.invalidate();
} }
float orientation = ((ServiceGps) serviceGps).getOrientation(deviceOrientation); float orientation = serviceGps.getOrientation(deviceOrientation);
if (orientation >= 0) { if (orientation >= 0) {
map.setMapOrientation(orientation); map.setMapOrientation(orientation);
} }
} }
} }
} }
private void addMarker() {
GeoPoint gp = ((ServiceGps) serviceGps).getCurrentGeoPoint();
lGeoPointObjects.add(gp);
Marker marker = new Marker(map);
marker.setPosition(gp);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
marker.setTitle("Object");
marker.setDraggable(false);
map.getOverlays().add(marker);
}
} }

View File

@ -0,0 +1,66 @@
package fr.chteufleur.mytrackingdog.models;
import android.util.Log;
import org.osmdroid.util.GeoPoint;
import java.util.ArrayList;
import java.util.List;
public class Traces {
private static final String TAG = Traces.class.getName();
private List<GeoPoint> lGeoPointTraceur = new ArrayList<>();
private List<GeoPoint> lGeoPointDog = new ArrayList<>();
private List<GeoPoint> lGeoPointObjects = new ArrayList<>();
private boolean traceurActivated = false;
private boolean dogActivated = false;
public void addPointTraceur(GeoPoint point) {
Log.i(TAG, "add point traceur");
lGeoPointTraceur.add(point);
}
public void addPointDog(GeoPoint point) {
Log.i(TAG, "add point dog");
lGeoPointDog.add(point);
}
public void addPointObject(GeoPoint point) {
Log.i(TAG, "add point object");
lGeoPointObjects.add(point);
}
public void addCurrentPoint(GeoPoint point) {
if (traceurActivated) {
addPointTraceur(point);
} else if (dogActivated) {
addPointDog(point);
}
}
public List<GeoPoint> getListGeoPointTraceur() {
return lGeoPointTraceur;
}
public List<GeoPoint> getListGeoPointDog() {
return lGeoPointDog;
}
public List<GeoPoint> getListGeoPointObjects() {
return lGeoPointObjects;
}
public void toggleTraceurActivation() {
this.traceurActivated = !this.traceurActivated;
Log.i(TAG, "set traceur activated to "+traceurActivated);
}
public boolean isTraceurActivated() {
return this.traceurActivated;
}
public void toggleDogActivation() {
this.dogActivated = !this.dogActivated;
Log.i(TAG, "set dog activated to "+dogActivated);
}
public boolean isDogActivated() {
return this.dogActivated;
}
}

View File

@ -8,18 +8,21 @@ import android.util.Log;
import org.osmdroid.util.GeoPoint; import org.osmdroid.util.GeoPoint;
import java.util.List;
import java.util.Observable; import java.util.Observable;
import fr.chteufleur.mytrackingdog.MainActivity; import fr.chteufleur.mytrackingdog.models.Traces;
public class ServiceGps extends Observable implements IServiceGps, LocationListener { public class ServiceGps extends Observable implements IServiceGps, LocationListener {
private static final String TAG = "ServiceGps"; private static final String TAG = ServiceGps.class.getName();
public static final String NOTIF_NEW_LOCATION = "fr.chteufleur.mytrackingdog.services.servicegps.newlocation"; public static final String NOTIF_NEW_LOCATION = "fr.chteufleur.mytrackingdog.services.servicegps.newlocation";
private LocationManager locationManager; private LocationManager locationManager;
private Location currentLocation = null; private Location currentLocation = null;
private final Traces traces = new Traces();
public void setLocationManager(LocationManager locationManager) { public void setLocationManager(LocationManager locationManager) {
this.locationManager = locationManager; this.locationManager = locationManager;
} }
@ -65,6 +68,7 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
currentLocation = location; currentLocation = location;
traces.addCurrentPoint(getCurrentGeoPoint());
setChanged(); setChanged();
notifyObservers(NOTIF_NEW_LOCATION); notifyObservers(NOTIF_NEW_LOCATION);
} }
@ -90,4 +94,34 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
} }
return new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude(), currentLocation.getAltitude()); return new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude(), currentLocation.getAltitude());
} }
public void toggleTraceurActivation() {
traces.toggleTraceurActivation();
}
public boolean isTraceurActivated() {
return traces.isTraceurActivated();
}
public void toggleDogActivation() {
traces.toggleDogActivation();
}
public boolean isDogActivated() {
return traces.isDogActivated();
}
public GeoPoint addPointObject() {
GeoPoint point = getCurrentGeoPoint();
traces.addPointObject(point);
return point;
}
public List<GeoPoint> getListGeoPointTraceur() {
return traces.getListGeoPointTraceur();
}
public List<GeoPoint> getListGeoPointDog() {
return traces.getListGeoPointDog();
}
public List<GeoPoint> getListGeoPointObjects() {
return traces.getListGeoPointObjects();
}
} }