Re-organize data flow.
This commit is contained in:
parent
52e2689603
commit
95ad89a181
|
|
@ -11,9 +11,11 @@ import android.location.LocationManager;
|
|||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
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.MyLocationNewOverlay;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
import fr.chteufleur.mytrackingdog.services.IServiceGps;
|
||||
import fr.chteufleur.mytrackingdog.services.ServiceGps;
|
||||
|
||||
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 CompassOverlay mCompassOverlay;
|
||||
private IOrientationProvider compass = null;
|
||||
private IServiceGps serviceGps = null;
|
||||
private ServiceGps serviceGps = null;
|
||||
|
||||
private Context ctx = null;
|
||||
private MapView map = null;
|
||||
private ImageButton btFollowMe;
|
||||
|
||||
private Float trueNorth = 0f;
|
||||
private int deviceOrientation = 0;
|
||||
private boolean zoomed = false;
|
||||
|
||||
private final int REQUEST_CODE_ASK_PERMISSION = 123;
|
||||
|
||||
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_dog_trace;
|
||||
private FloatingActionButton add_object;
|
||||
private boolean traceurActivated = false;
|
||||
private boolean dogActivated = false;
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
protected void checkPermissions() {
|
||||
|
|
@ -100,7 +92,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
// 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);
|
||||
|
||||
// Add the ability to zoom with 2 fingers
|
||||
|
|
@ -126,7 +118,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
mLocationOverlay.setOptionsMenuEnabled(true);
|
||||
|
||||
// Follow me
|
||||
btFollowMe = (ImageButton) findViewById(R.id.ic_follow_me);
|
||||
btFollowMe = findViewById(R.id.ic_follow_me);
|
||||
btFollowMe.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
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() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
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() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
changeStatusDogTrace();
|
||||
}
|
||||
});
|
||||
add_object = (FloatingActionButton) findViewById(R.id.add_object);
|
||||
FloatingActionButton add_object = findViewById(R.id.add_object);
|
||||
add_object.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
|
|
@ -164,13 +156,13 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
if (serviceGps == null) {
|
||||
serviceGps = new ServiceGps();
|
||||
((Observable) serviceGps).addObserver(this);
|
||||
serviceGps.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void changeStatusTrace() {
|
||||
this.traceurActivated = !this.traceurActivated;
|
||||
if (this.traceurActivated) {
|
||||
serviceGps.toggleTraceurActivation();
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
this.start_stop_trace.setImageResource(R.drawable.ic_menu_offline);
|
||||
} else {
|
||||
this.start_stop_trace.setImageResource(R.drawable.ic_menu_mylocation);
|
||||
|
|
@ -178,14 +170,25 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
|
||||
private void changeStatusDogTrace() {
|
||||
this.dogActivated = !this.dogActivated;
|
||||
if (this.dogActivated) {
|
||||
serviceGps.toggleDogActivation();
|
||||
if (serviceGps.isDogActivated()) {
|
||||
this.start_stop_dog_trace.setImageResource(R.drawable.ic_menu_offline);
|
||||
} else {
|
||||
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
|
||||
public void onResume(){
|
||||
super.onResume();
|
||||
|
|
@ -194,8 +197,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
if (!"Android-x86".equalsIgnoreCase(Build.BRAND)) {
|
||||
//lock the device in current screen orientation
|
||||
int orientation;
|
||||
int rotation = ((WindowManager) getSystemService(
|
||||
Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
|
||||
int rotation = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
|
||||
switch (rotation) {
|
||||
case Surface.ROTATION_0:
|
||||
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
|
||||
|
|
@ -209,6 +211,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
this.deviceOrientation = 180;
|
||||
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
|
||||
break;
|
||||
case Surface.ROTATION_270:
|
||||
default:
|
||||
this.deviceOrientation = 270;
|
||||
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
|
||||
|
|
@ -218,7 +221,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
setRequestedOrientation(orientation);
|
||||
}
|
||||
|
||||
((ServiceGps) serviceGps).setLocationManager((LocationManager) getSystemService(ctx.LOCATION_SERVICE));
|
||||
serviceGps.setLocationManager((LocationManager) getSystemService(Context.LOCATION_SERVICE));
|
||||
serviceGps.start();
|
||||
|
||||
mLocationOverlay.enableFollowLocation();
|
||||
|
|
@ -292,10 +295,10 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
switch (requestCode) {
|
||||
case REQUEST_CODE_ASK_PERMISSION:
|
||||
Toast.makeText(ctx, "Permissions granted", Toast.LENGTH_LONG);
|
||||
Toast.makeText(ctx, "Permissions granted", Toast.LENGTH_LONG).show();
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
@ -308,7 +311,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
@Override
|
||||
public void onOrientationChanged(float orientationToMagneticNorth, IOrientationProvider source) {
|
||||
//note, on devices without a compass this never fires...
|
||||
Location location = ((ServiceGps) serviceGps).getCurrentLocation();
|
||||
Location location = serviceGps.getCurrentLocation();
|
||||
if (location == null) {
|
||||
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
|
||||
if (gpsspeed < 0.01) {
|
||||
GeomagneticField gf = new GeomagneticField(lat, lon, alt, timeOfFix);
|
||||
trueNorth = orientationToMagneticNorth + gf.getDeclination();
|
||||
float trueNorth = orientationToMagneticNorth + gf.getDeclination();
|
||||
gf = null;
|
||||
synchronized (trueNorth) {
|
||||
if (trueNorth > 360.0f) {
|
||||
trueNorth = trueNorth - 360.0f;
|
||||
}
|
||||
float actualHeading = 0f;
|
||||
if (trueNorth > 360.0f) {
|
||||
trueNorth = trueNorth - 360.0f;
|
||||
}
|
||||
|
||||
//this part adjusts the desired map rotation based on device orientation and compass heading
|
||||
float t = (360 - trueNorth - this.deviceOrientation);
|
||||
if (t < 0) {
|
||||
t += 360;
|
||||
}
|
||||
if (t > 360) {
|
||||
t -= 360;
|
||||
}
|
||||
actualHeading = t;
|
||||
//help smooth everything out
|
||||
t = (int) t;
|
||||
t = t / 5;
|
||||
t = (int) t;
|
||||
t = t * 5;
|
||||
map.setMapOrientation(t);
|
||||
if (!zoomed) {
|
||||
IMapController mapController = map.getController();
|
||||
mapController.setZoom(20.0);
|
||||
zoomed = true;
|
||||
}
|
||||
//this part adjusts the desired map rotation based on device orientation and compass heading
|
||||
float t = (360 - trueNorth - this.deviceOrientation);
|
||||
if (t < 0) {
|
||||
t += 360;
|
||||
}
|
||||
if (t > 360) {
|
||||
t -= 360;
|
||||
}
|
||||
//help smooth everything out
|
||||
t = (int) t;
|
||||
t = t / 5;
|
||||
t = (int) t;
|
||||
t = t * 5;
|
||||
map.setMapOrientation(t);
|
||||
if (!zoomed) {
|
||||
IMapController mapController = map.getController();
|
||||
mapController.setZoom(20.0);
|
||||
zoomed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -357,27 +356,30 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
@Override
|
||||
public void update(Observable observable, Object o) {
|
||||
Log.i(TAG, "update");
|
||||
if (observable == serviceGps) {
|
||||
if (o instanceof String && (String) o == ServiceGps.NOTIF_NEW_LOCATION) {
|
||||
GeoPoint currentPoint = ((ServiceGps) serviceGps).getCurrentGeoPoint();
|
||||
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 (traceurActivated) {
|
||||
lGeoPointTraceur.add(((ServiceGps) serviceGps).getCurrentGeoPoint());
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
Log.i(TAG, "polyline traceur");
|
||||
map.getController().setCenter(currentPoint);
|
||||
|
||||
line = new Polyline(map);
|
||||
line.setTitle("Traceur");
|
||||
line.setColor(Color.RED);
|
||||
line.setPoints(lGeoPointTraceur);
|
||||
line.setPoints(serviceGps.getListGeoPointTraceur());
|
||||
|
||||
} else if (dogActivated) {
|
||||
lGeoPointDog.add(((ServiceGps) serviceGps).getCurrentGeoPoint());
|
||||
} else if (serviceGps.isDogActivated()) {
|
||||
Log.i(TAG, "polyline dog");
|
||||
map.getController().setCenter(currentPoint);
|
||||
|
||||
line = new Polyline(map);
|
||||
line.setTitle("Dog");
|
||||
line.setColor(Color.BLUE);
|
||||
line.setPoints(lGeoPointDog);
|
||||
line.setPoints(serviceGps.getListGeoPointDog());
|
||||
}
|
||||
|
||||
if (line != null) {
|
||||
|
|
@ -388,23 +390,11 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
map.getOverlayManager().add(line);
|
||||
map.invalidate();
|
||||
}
|
||||
float orientation = ((ServiceGps) serviceGps).getOrientation(deviceOrientation);
|
||||
float orientation = serviceGps.getOrientation(deviceOrientation);
|
||||
if (orientation >= 0) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,18 +8,21 @@ import android.util.Log;
|
|||
|
||||
import org.osmdroid.util.GeoPoint;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
|
||||
import fr.chteufleur.mytrackingdog.MainActivity;
|
||||
import fr.chteufleur.mytrackingdog.models.Traces;
|
||||
|
||||
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";
|
||||
|
||||
private LocationManager locationManager;
|
||||
private Location currentLocation = null;
|
||||
|
||||
private final Traces traces = new Traces();
|
||||
|
||||
public void setLocationManager(LocationManager locationManager) {
|
||||
this.locationManager = locationManager;
|
||||
}
|
||||
|
|
@ -65,6 +68,7 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
|||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
currentLocation = location;
|
||||
traces.addCurrentPoint(getCurrentGeoPoint());
|
||||
setChanged();
|
||||
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());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue