Add ServiceTrackingDog for better services management.
This commit is contained in:
parent
1361473eee
commit
83342b5be9
|
|
@ -58,6 +58,7 @@ import fr.chteufleur.mytrackingdog.models.beans.MyLocation;
|
|||
import fr.chteufleur.mytrackingdog.models.beans.MyLocationArray;
|
||||
import fr.chteufleur.mytrackingdog.models.beans.WayPointLocation;
|
||||
import fr.chteufleur.mytrackingdog.services.ServiceGps;
|
||||
import fr.chteufleur.mytrackingdog.services.ServiceTrackingDog;
|
||||
import fr.chteufleur.mytrackingdog.services.ServiceXmpp;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements IOrientationConsumer, Observer {
|
||||
|
|
@ -72,8 +73,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
private MyLocationNewOverlay mLocationOverlay;
|
||||
private IOrientationProvider compass = null;
|
||||
private ServiceGps serviceGps = null;
|
||||
private ServiceXmpp serviceXmpp = null;
|
||||
private ServiceTrackingDog serviceTrackingDog = null;
|
||||
|
||||
private Context ctx = null;
|
||||
private MapView map = null;
|
||||
|
|
@ -88,7 +89,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
private Button start_stop_trace;
|
||||
private Button start_stop_dog_trace;
|
||||
private Button add_object;
|
||||
private Button center_button;
|
||||
private TextView textViewCurrentLocation;
|
||||
|
||||
private MyLocation lastLocation = null;
|
||||
|
|
@ -141,7 +141,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
});
|
||||
|
||||
center_button = findViewById(R.id.center_button);
|
||||
Button center_button = findViewById(R.id.center_button);
|
||||
center_button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
|
|
@ -169,16 +169,12 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
public void onClick(View view) {
|
||||
changeStatusTrace();
|
||||
try {
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
if (serviceTrackingDog.isTraceurActivated()) {
|
||||
serviceXmpp.sendCommandStartTrail();
|
||||
} else {
|
||||
serviceXmpp.sendCommandStopTrail();
|
||||
}
|
||||
} catch (XmppStringprepException ex) {
|
||||
} catch (InterruptedException e) {
|
||||
} catch (SmackException.NoResponseException e) {
|
||||
} catch (SmackException.NotConnectedException e) {
|
||||
} catch (XMPPException.XMPPErrorException e) {
|
||||
} catch (XmppStringprepException | InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException ex) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -194,8 +190,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
add_object.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
WayPointLocation loc = serviceGps.addPointObjectTrail();
|
||||
if (serviceTrackingDog.isTraceurActivated()) {
|
||||
WayPointLocation loc = serviceTrackingDog.addPointObjectTrail();
|
||||
if (loc != null) {
|
||||
GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
|
||||
addMarker(gp, loc.isFound());
|
||||
|
|
@ -204,7 +200,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
} else if (serviceGps.isDogActivated()) {
|
||||
} else if (serviceTrackingDog.isDogActivated()) {
|
||||
markAsFound();
|
||||
}
|
||||
}
|
||||
|
|
@ -213,12 +209,13 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
textViewCurrentLocation = findViewById(R.id.textViewCurrentLocation);
|
||||
textViewCurrentLocation.setVisibility(View.GONE);
|
||||
|
||||
if (serviceGps == null) {
|
||||
serviceGps = new ServiceGps(
|
||||
if (serviceTrackingDog == null) {
|
||||
serviceTrackingDog = new ServiceTrackingDog(
|
||||
(Vibrator) getSystemService(Context.VIBRATOR_SERVICE),
|
||||
PreferenceManager.getDefaultSharedPreferences(ctx)
|
||||
PreferenceManager.getDefaultSharedPreferences(ctx),
|
||||
getResources()
|
||||
);
|
||||
serviceGps.addObserver(this);
|
||||
serviceTrackingDog.addObserver(this, ServiceGps.class.getName());
|
||||
}
|
||||
|
||||
if (serviceXmpp == null) {
|
||||
|
|
@ -270,9 +267,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
setRequestedOrientation(orientation);
|
||||
}
|
||||
|
||||
serviceGps.setLocationManager((LocationManager) getSystemService(Context.LOCATION_SERVICE));
|
||||
serviceGps.setAppName(appName);
|
||||
serviceGps.start();
|
||||
serviceTrackingDog.setLocationManager((LocationManager) getSystemService(Context.LOCATION_SERVICE));
|
||||
serviceTrackingDog.setAppName(appName);
|
||||
serviceTrackingDog.startLocation();
|
||||
|
||||
mLocationOverlay.enableFollowLocation();
|
||||
mLocationOverlay.enableMyLocation();
|
||||
|
|
@ -310,15 +307,15 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (serviceGps.isDogActivated() || serviceGps.isTraceurActivated()) {
|
||||
if (serviceTrackingDog.isDogActivated() || serviceTrackingDog.isTraceurActivated()) {
|
||||
Toast.makeText(ctx, "Une trace est en cours d'enregistrement.", Toast.LENGTH_LONG).show();
|
||||
return ;
|
||||
}
|
||||
|
||||
super.onBackPressed();
|
||||
pause();
|
||||
if (serviceGps != null) {
|
||||
serviceGps.stop();
|
||||
if (serviceTrackingDog != null) {
|
||||
serviceTrackingDog.close();
|
||||
}
|
||||
if (serviceXmpp != null) {
|
||||
serviceXmpp.close();
|
||||
|
|
@ -334,7 +331,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
||||
|
||||
MenuItem vibrationObjectMenuItem = menu.findItem(R.id.action_active_vibration_object);
|
||||
vibrationObjectMenuItem.setChecked(serviceGps.isVibrationNearObjectEnabled());
|
||||
vibrationObjectMenuItem.setChecked(serviceTrackingDog.isVibrationNearObjectEnabled());
|
||||
|
||||
MenuItem xmppObjectMenuItem = menu.findItem(R.id.action_active_xmpp);
|
||||
xmppObjectMenuItem.setChecked(serviceXmpp.isEnabled());
|
||||
|
|
@ -364,7 +361,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
startActivityForResult(intent, ACTIVITY_QR_CODE_READER);
|
||||
|
||||
} else if (id == R.id.action_send_gpx_trail) {
|
||||
File trailFile = serviceGps.getLastExportedTrailFile();
|
||||
File trailFile = serviceTrackingDog.getLastExportedTrailFile();
|
||||
if (trailFile != null) {
|
||||
Intent shareIntent = new Intent();
|
||||
shareIntent.setAction(Intent.ACTION_SEND);
|
||||
|
|
@ -380,7 +377,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
} else if (id == R.id.action_active_vibration_object) {
|
||||
boolean checked = item.isChecked();
|
||||
item.setChecked(!checked);
|
||||
serviceGps.setVibrationNearObjectEnabled(!checked);
|
||||
serviceTrackingDog.setVibrationNearObjectEnabled(!checked);
|
||||
return true;
|
||||
|
||||
} else if (id == R.id.action_active_xmpp) {
|
||||
|
|
@ -413,23 +410,23 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
switch (requestCode) {
|
||||
case ACTIVITY_REQUEST_PICK_FILE:
|
||||
if (data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {
|
||||
serviceGps.importGpxTrace(new File(data.getStringExtra(FilePicker.EXTRA_FILE_PATH)));
|
||||
serviceTrackingDog.importGpxTrace(new File(data.getStringExtra(FilePicker.EXTRA_FILE_PATH)));
|
||||
updateDogTrace();
|
||||
updateTrailTrace();
|
||||
calculTrailDistance();
|
||||
for (MyLocation loc: serviceGps.getListGeoPointObjectsTrail()) {
|
||||
for (MyLocation loc: serviceTrackingDog.getListGeoPointObjectsTrail()) {
|
||||
addMarker(new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()), false);
|
||||
}
|
||||
for (MyLocation loc: serviceGps.getListGeoPointObjectsDog()) {
|
||||
for (MyLocation loc: serviceTrackingDog.getListGeoPointObjectsDog()) {
|
||||
boolean isFound = false;
|
||||
if (loc instanceof WayPointLocation) {
|
||||
isFound = ((WayPointLocation) loc).isFound();
|
||||
}
|
||||
addMarker(new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()), isFound);
|
||||
}
|
||||
if (!serviceGps.getListGeoPointTraceur().isEmpty()) {
|
||||
if (!serviceTrackingDog.getListGeoPointTraceur().isEmpty()) {
|
||||
updateDistance(getTextTraceur());
|
||||
} else if (!serviceGps.getListGeoPointDog().isEmpty()) {
|
||||
} else if (!serviceTrackingDog.getListGeoPointDog().isEmpty()) {
|
||||
updateDistance(getTextDog());
|
||||
}
|
||||
}
|
||||
|
|
@ -472,7 +469,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...
|
||||
MyLocation location = serviceGps.getCurrentLocation();
|
||||
MyLocation location = serviceTrackingDog.getCurrentLocation();
|
||||
if (location == null) {
|
||||
return ;
|
||||
}
|
||||
|
|
@ -512,8 +509,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
//<editor-fold defaultstate="collapsed" desc="Map management">
|
||||
private void changeStatusTrace() {
|
||||
serviceGps.toggleTraceurActivation();
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
serviceTrackingDog.toggleTraceurActivation();
|
||||
if (serviceTrackingDog.isTraceurActivated()) {
|
||||
this.start_stop_trace.setText(R.string.trail_stop);
|
||||
this.add_object.setVisibility(View.VISIBLE);
|
||||
this.add_object.setText(R.string.trail_object);
|
||||
|
|
@ -527,8 +524,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
|
||||
private void changeStatusDogTrace() {
|
||||
serviceGps.toggleDogActivation();
|
||||
if (serviceGps.isDogActivated()) {
|
||||
serviceTrackingDog.toggleDogActivation();
|
||||
if (serviceTrackingDog.isDogActivated()) {
|
||||
this.start_stop_dog_trace.setText(R.string.dog_stop);
|
||||
this.add_object.setVisibility(View.VISIBLE);
|
||||
this.add_object.setText(R.string.dog_object);
|
||||
|
|
@ -552,9 +549,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
marker.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
|
||||
@Override
|
||||
public boolean onMarkerClick(Marker marker, MapView mapView) {
|
||||
if (serviceGps.isDogActivated()) {
|
||||
if (serviceTrackingDog.isDogActivated()) {
|
||||
GeoPoint gp = marker.getPosition();
|
||||
WayPointLocation wpl = serviceGps.getPointTrail(gp.getLatitude(), gp.getLongitude());
|
||||
WayPointLocation wpl = serviceTrackingDog.getPointTrail(gp.getLatitude(), gp.getLongitude());
|
||||
if (wpl != null) {
|
||||
wpl.setFound();
|
||||
marker.setIcon(getResources().getDrawable(R.drawable.ic_marker_blue));
|
||||
|
|
@ -568,7 +565,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
|
||||
private void markAsFound() {
|
||||
List<WayPointLocation> wpls = serviceGps.foundNearObjects();
|
||||
List<WayPointLocation> wpls = serviceTrackingDog.foundNearObjects();
|
||||
boolean findMarker = false;
|
||||
for (WayPointLocation wpl : wpls) {
|
||||
if (wpl != null && !wpl.isFound()) {
|
||||
|
|
@ -584,21 +581,21 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
}
|
||||
}
|
||||
serviceGps.addPointObjectDog();
|
||||
serviceTrackingDog.addPointObjectDog();
|
||||
findMarker = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (wpls.isEmpty() || !findMarker) {
|
||||
// if no object is near
|
||||
WayPointLocation wpl = serviceGps.addPointObjectDog();
|
||||
WayPointLocation wpl = serviceTrackingDog.addPointObjectDog();
|
||||
GeoPoint gp = new GeoPoint(wpl.getLatitude(), wpl.getLongitude(), wpl.getAltitude());
|
||||
addMarker(gp, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void calculTrailDistance() {
|
||||
List<MyLocation> listLoc = serviceGps.getListGeoPointTraceur();
|
||||
List<MyLocation> listLoc = serviceTrackingDog.getListGeoPointTraceur();
|
||||
MyLocation last = null;
|
||||
distance = 0;
|
||||
for (MyLocation loc: listLoc) {
|
||||
|
|
@ -613,7 +610,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
|
||||
private void updateDogTrace() {
|
||||
MyLocationArray listLoc = serviceGps.getListGeoPointDog();
|
||||
MyLocationArray listLoc = serviceTrackingDog.getListGeoPointDog();
|
||||
if (listLoc.isEmpty()) {
|
||||
return ;
|
||||
}
|
||||
|
|
@ -631,7 +628,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
|
||||
private void updateTrailTrace() {
|
||||
MyLocationArray listLoc = serviceGps.getListGeoPointTraceur();
|
||||
MyLocationArray listLoc = serviceTrackingDog.getListGeoPointTraceur();
|
||||
if (listLoc.isEmpty()) {
|
||||
return ;
|
||||
}
|
||||
|
|
@ -662,7 +659,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
//<editor-fold defaultstate="collapsed" desc="Service callback">
|
||||
@Override
|
||||
public void update(Observable observable, Object o) {
|
||||
if (observable == serviceGps) {
|
||||
if (observable.getClass().getName().equals(ServiceGps.class.getName())) {
|
||||
updateServiceGps(o);
|
||||
} else if (observable == serviceXmpp) {
|
||||
updateServiceXmpp(o);
|
||||
|
|
@ -671,7 +668,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
public void updateServiceGps(Object o) {
|
||||
if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) {
|
||||
MyLocation loc = serviceGps.getCurrentLocation();
|
||||
MyLocation loc = serviceTrackingDog.getCurrentLocation();
|
||||
onNewLocation(loc);
|
||||
try {
|
||||
serviceXmpp.sendCommandLocationTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime());
|
||||
|
|
@ -686,7 +683,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
if (serviceTrackingDog.isTraceurActivated()) {
|
||||
MyLocation loc = serviceXmpp.getCurrentLocation();
|
||||
onNewLocation(loc);
|
||||
}
|
||||
|
|
@ -696,9 +693,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
if (serviceTrackingDog.isTraceurActivated()) {
|
||||
MyLocation locObj = serviceXmpp.getLastObjectXmppLocation();
|
||||
WayPointLocation loc = serviceGps.addPointObjectTrail(locObj);
|
||||
WayPointLocation loc = serviceTrackingDog.addPointObjectTrail(locObj);
|
||||
GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
|
||||
addMarker(gp, loc.isFound());
|
||||
}
|
||||
|
|
@ -724,19 +721,19 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
private void onNewLocation(MyLocation loc) {
|
||||
if (loc != null) {
|
||||
serviceGps.addPoint(loc);
|
||||
//serviceGps.addPoint(loc);
|
||||
GeoPoint currentPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
|
||||
if (mLocationOverlay.isFollowLocationEnabled()) {
|
||||
map.getController().setCenter(currentPoint);
|
||||
}
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
if (serviceTrackingDog.isTraceurActivated()) {
|
||||
updateTrailTrace();
|
||||
if (lastLocation != null) {
|
||||
distance += loc.distanceTo(lastLocation);
|
||||
updateDistance();
|
||||
}
|
||||
lastLocation = loc;
|
||||
} else if (serviceGps.isDogActivated()) {
|
||||
} else if (serviceTrackingDog.isDogActivated()) {
|
||||
updateDogTrace();
|
||||
if (lastLocation != null) {
|
||||
distance += loc.distanceTo(lastLocation);
|
||||
|
|
@ -745,7 +742,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
lastLocation = loc;
|
||||
}
|
||||
|
||||
float orientation = serviceGps.getOrientation(deviceOrientation);
|
||||
float orientation = serviceTrackingDog.getOrientation(deviceOrientation);
|
||||
if (orientation >= 0) {
|
||||
map.setMapOrientation(orientation);
|
||||
}
|
||||
|
|
@ -762,22 +759,22 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
//<editor-fold defaultstate="collapsed" desc="Placeholder management">
|
||||
private void updateDistance() {
|
||||
String text = null;
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
if (serviceTrackingDog.isTraceurActivated()) {
|
||||
text = getTextTraceur();
|
||||
} else if (serviceGps.isDogActivated()) {
|
||||
} else if (serviceTrackingDog.isDogActivated()) {
|
||||
text = getTextDog();
|
||||
}
|
||||
updateDistance(text);
|
||||
}
|
||||
|
||||
private String getTextTraceur() {
|
||||
return String.format("Distance: %,dm\t\t\t\t\t\t\t\tObjets: %d", ((int) distance), serviceGps.getListGeoPointObjectsTrail().size());
|
||||
return String.format("Distance: %,dm\t\t\t\t\t\t\t\tObjets: %d", ((int) distance), serviceTrackingDog.getListGeoPointObjectsTrail().size());
|
||||
}
|
||||
|
||||
private String getTextDog() {
|
||||
String text = "";
|
||||
MyLocation firstLoc = serviceGps.getListGeoPointDog().getFirstLocation();
|
||||
MyLocation lastLoc = serviceGps.getListGeoPointDog().getLastLocation();
|
||||
MyLocation firstLoc = serviceTrackingDog.getListGeoPointDog().getFirstLocation();
|
||||
MyLocation lastLoc = serviceTrackingDog.getListGeoPointDog().getLastLocation();
|
||||
if (firstLoc != null) {
|
||||
long time = lastLoc.getTime() - firstLoc.getTime();
|
||||
time /= 1_000;
|
||||
|
|
@ -789,12 +786,12 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
text = String.format("Time: %02d:%02d:%02d", h, m, s);
|
||||
|
||||
int nbFoundObject = 0;
|
||||
for (MyLocation loc: serviceGps.getListGeoPointObjectsDog()) {
|
||||
for (MyLocation loc: serviceTrackingDog.getListGeoPointObjectsDog()) {
|
||||
if (loc instanceof WayPointLocation && ((WayPointLocation) loc).isFound()) {
|
||||
nbFoundObject++;
|
||||
}
|
||||
}
|
||||
text += "\t\t\t\t\t\t\t\tObjets: "+nbFoundObject+"/"+serviceGps.getListGeoPointObjectsTrail().size();
|
||||
text += "\t\t\t\t\t\t\t\tObjets: "+nbFoundObject+"/"+serviceTrackingDog.getListGeoPointObjectsTrail().size();
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,8 +36,6 @@ public class ServiceGps extends Observable implements LocationListener {
|
|||
private static final String TAG = ServiceGps.class.getName();
|
||||
public static final String NOTIF_NEW_LOCATION = ServiceGps.class.getName()+".newlocation";
|
||||
|
||||
private static final String PREF_VIBRATION_NEAR_OBJECT_ENABLED = "PREF_VIBRATION_NEAR_OBJECT_ENABLED";
|
||||
|
||||
private File lastExportedTrailFile = null;
|
||||
|
||||
private LocationManager locationManager;
|
||||
|
|
@ -45,15 +43,8 @@ public class ServiceGps extends Observable implements LocationListener {
|
|||
private String appName = "";
|
||||
|
||||
private final Traces traces = new Traces();
|
||||
private final SharedPreferences preferences;
|
||||
|
||||
public ServiceGps(Vibrator vibrator, SharedPreferences preferences) {
|
||||
this.vibrator = vibrator;
|
||||
this.preferences = preferences;
|
||||
|
||||
if (preferences != null) {
|
||||
this.vibrationNearObjectEnabled = preferences.getBoolean(PREF_VIBRATION_NEAR_OBJECT_ENABLED, false);
|
||||
}
|
||||
public ServiceGps() {
|
||||
}
|
||||
|
||||
public void setLocationManager(LocationManager locationManager) {
|
||||
|
|
@ -104,35 +95,6 @@ public class ServiceGps extends Observable implements LocationListener {
|
|||
return ret;
|
||||
}
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="Vibration management">
|
||||
private final Vibrator vibrator;
|
||||
private boolean vibrationNearObjectEnabled = false;
|
||||
private boolean nearObjectVibration = false;
|
||||
|
||||
public void setVibrationNearObjectEnabled(boolean b) {
|
||||
this.vibrationNearObjectEnabled = b;
|
||||
if (preferences != null) {
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
editor.putBoolean(PREF_VIBRATION_NEAR_OBJECT_ENABLED, b);
|
||||
editor.commit();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isVibrationNearObjectEnabled() {
|
||||
return this.vibrationNearObjectEnabled;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="GPS Callback">
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
|
|
@ -140,17 +102,6 @@ public class ServiceGps extends Observable implements LocationListener {
|
|||
currentLocation = new MyLocation(location);
|
||||
setChanged();
|
||||
notifyObservers(NOTIF_NEW_LOCATION);
|
||||
|
||||
if (isDogActivated()) {
|
||||
if (vibrationNearObjectEnabled && isNearObjects()) {
|
||||
if (!nearObjectVibration) {
|
||||
nearObjectVibration = true;
|
||||
vibrate(1_000);
|
||||
}
|
||||
} else {
|
||||
nearObjectVibration = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -159,9 +110,9 @@ public class ServiceGps extends Observable implements LocationListener {
|
|||
if (s != null && s.equals(LocationManager.GPS_PROVIDER)) {
|
||||
if (i == LocationProvider.AVAILABLE) {
|
||||
start();
|
||||
} else if (i == LocationProvider.TEMPORARILY_UNAVAILABLE) {
|
||||
} else if (i == LocationProvider.OUT_OF_SERVICE) {
|
||||
stop();
|
||||
// } else if (i == LocationProvider.TEMPORARILY_UNAVAILABLE) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -236,7 +187,7 @@ public class ServiceGps extends Observable implements LocationListener {
|
|||
return traces.getListPointObjectsDog();
|
||||
}
|
||||
|
||||
private List<WayPointLocation> foundNearObjects(int distance) {
|
||||
public List<WayPointLocation> foundNearObjects(int distance) {
|
||||
List<WayPointLocation> ret = new ArrayList<>();
|
||||
MyLocation curLoc = currentLocation;
|
||||
for (MyLocation ml: getListGeoPointObjectsTrail()) {
|
||||
|
|
@ -246,7 +197,7 @@ public class ServiceGps extends Observable implements LocationListener {
|
|||
}
|
||||
return ret;
|
||||
}
|
||||
private boolean isNearObjects() {
|
||||
public boolean isNearObjects() {
|
||||
return !foundNearObjects(20).isEmpty();
|
||||
}
|
||||
public List<WayPointLocation> foundNearObjects() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,227 @@
|
|||
package fr.chteufleur.mytrackingdog.services;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Resources;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Build;
|
||||
import android.os.VibrationEffect;
|
||||
import android.os.Vibrator;
|
||||
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
import fr.chteufleur.mytrackingdog.models.beans.MyLocation;
|
||||
import fr.chteufleur.mytrackingdog.models.beans.MyLocationArray;
|
||||
import fr.chteufleur.mytrackingdog.models.beans.WayPointLocation;
|
||||
|
||||
public class ServiceTrackingDog implements Observer {
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="Preferences">
|
||||
private final SharedPreferences preferences;
|
||||
private static final String PREF_VIBRATION_NEAR_OBJECT_ENABLED = "PREF_VIBRATION_NEAR_OBJECT_ENABLED";
|
||||
//</editor-fold>
|
||||
|
||||
private final ServiceGps serviceGps;
|
||||
private final ServiceXmpp serviceXmpp;
|
||||
private String appName = "";
|
||||
|
||||
|
||||
public ServiceTrackingDog(Vibrator vibrator, SharedPreferences preferences, Resources resources) {
|
||||
this.vibrator = vibrator;
|
||||
this.preferences = preferences;
|
||||
|
||||
if (preferences != null) {
|
||||
this.vibrationNearObjectEnabled = preferences.getBoolean(PREF_VIBRATION_NEAR_OBJECT_ENABLED, false);
|
||||
}
|
||||
|
||||
this.serviceGps = new ServiceGps();
|
||||
this.serviceGps.addObserver(this);
|
||||
ServiceXmpp sx = null;
|
||||
try {
|
||||
sx = new ServiceXmpp(resources);
|
||||
} catch (UnknownHostException | XmppStringprepException ex) {
|
||||
sx = null;
|
||||
}
|
||||
this.serviceXmpp = sx;
|
||||
}
|
||||
|
||||
public void setAppName(String appName) {
|
||||
this.appName = appName;
|
||||
serviceGps.setAppName(appName);
|
||||
}
|
||||
|
||||
public void addObserver(Observer observer, String name) {
|
||||
if (name != null) {
|
||||
if (name.equals(ServiceGps.class.getName())) {
|
||||
serviceGps.addObserver(observer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
serviceGps.stop();
|
||||
if (serviceXmpp != null) {
|
||||
serviceXmpp.close();
|
||||
}
|
||||
}
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="Observer Callback">
|
||||
@Override
|
||||
public void update(Observable observable, Object o) {
|
||||
if (observable == serviceGps) {
|
||||
updateGps(o);
|
||||
} else if (observable == serviceXmpp) {
|
||||
updateXmpp(o);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateGps(Object o) {
|
||||
if (o != null && o instanceof String) {
|
||||
String action = (String) o;
|
||||
if (action.equals(ServiceGps.NOTIF_NEW_LOCATION)) {
|
||||
onNewLocation();
|
||||
shouldVibrate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateXmpp(Object o) {
|
||||
}
|
||||
|
||||
public void onNewLocation() {
|
||||
MyLocation loc = serviceGps.getCurrentLocation();
|
||||
serviceGps.addPoint(loc);
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="Vibration management">
|
||||
private static final int TIME_VIBRATION_NEAR_OBJECT_MS = 1_000;
|
||||
private final Vibrator vibrator;
|
||||
private boolean vibrationNearObjectEnabled = false;
|
||||
private boolean nearObjectVibration = false;
|
||||
|
||||
public void setVibrationNearObjectEnabled(boolean b) {
|
||||
this.vibrationNearObjectEnabled = b;
|
||||
if (preferences != null) {
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
editor.putBoolean(PREF_VIBRATION_NEAR_OBJECT_ENABLED, b);
|
||||
editor.commit();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isVibrationNearObjectEnabled() {
|
||||
return this.vibrationNearObjectEnabled;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void shouldVibrate() {
|
||||
// Near object vibration check
|
||||
if (serviceGps.isDogActivated()) {
|
||||
if (vibrationNearObjectEnabled && serviceGps.isNearObjects()) {
|
||||
if (!nearObjectVibration) {
|
||||
nearObjectVibration = true;
|
||||
vibrate(TIME_VIBRATION_NEAR_OBJECT_MS);
|
||||
}
|
||||
} else {
|
||||
nearObjectVibration = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="Service GPS delegation">
|
||||
public void setLocationManager(LocationManager locationManager) {
|
||||
serviceGps.setLocationManager(locationManager);
|
||||
}
|
||||
public void startLocation() {
|
||||
serviceGps.start();
|
||||
}
|
||||
public File getLastExportedTrailFile() {
|
||||
return serviceGps.getLastExportedTrailFile();
|
||||
}
|
||||
public MyLocation getCurrentLocation() {
|
||||
return serviceGps.getCurrentLocation();
|
||||
}
|
||||
public void importGpxTrace(File file) {
|
||||
serviceGps.importGpxTrace(file);
|
||||
}
|
||||
public float getOrientation(int deviceOrientation) {
|
||||
return serviceGps.getOrientation(deviceOrientation);
|
||||
}
|
||||
//<editor-fold defaultstate="collapsed" desc="Trace activation">
|
||||
public void toggleTraceurActivation() {
|
||||
serviceGps.toggleTraceurActivation();
|
||||
}
|
||||
public boolean isTraceurActivated() {
|
||||
return serviceGps.isTraceurActivated();
|
||||
}
|
||||
|
||||
public void toggleDogActivation() {
|
||||
serviceGps.toggleDogActivation();
|
||||
}
|
||||
public boolean isDogActivated() {
|
||||
return serviceGps.isDogActivated();
|
||||
}
|
||||
//</editor-fold>
|
||||
//<editor-fold defaultstate="collapsed" desc="Objects">
|
||||
public WayPointLocation addPointObjectTrail() {
|
||||
return serviceGps.addPointObjectTrail();
|
||||
}
|
||||
public WayPointLocation addPointObjectTrail(MyLocation location) {
|
||||
return serviceGps.addPointObjectTrail(location);
|
||||
}
|
||||
public WayPointLocation addPointObjectDog() {
|
||||
return serviceGps.addPointObjectDog();
|
||||
}
|
||||
|
||||
public MyLocationArray getListGeoPointObjectsTrail() {
|
||||
return serviceGps.getListGeoPointObjectsTrail();
|
||||
}
|
||||
public MyLocationArray getListGeoPointObjectsDog() {
|
||||
return serviceGps.getListGeoPointObjectsDog();
|
||||
}
|
||||
|
||||
private List<WayPointLocation> foundNearObjects(int distance) {
|
||||
return serviceGps.foundNearObjects(distance);
|
||||
}
|
||||
public boolean isNearObjects() {
|
||||
return serviceGps.isNearObjects();
|
||||
}
|
||||
public List<WayPointLocation> foundNearObjects() {
|
||||
return serviceGps.foundNearObjects();
|
||||
}
|
||||
//</editor-fold>
|
||||
//<editor-fold defaultstate="collapsed" desc="Traces">
|
||||
public void addPoint(MyLocation location) {
|
||||
serviceGps.addPoint(location);
|
||||
}
|
||||
public MyLocationArray getListGeoPointTraceur() {
|
||||
return serviceGps.getListGeoPointTraceur();
|
||||
}
|
||||
public MyLocationArray getListGeoPointDog() {
|
||||
return serviceGps.getListGeoPointDog();
|
||||
}
|
||||
public WayPointLocation getPointTrail(double lat, double lon) {
|
||||
return serviceGps.getPointTrail(lat, lon);
|
||||
}
|
||||
public WayPointLocation getPointDog(double lat, double lon) {
|
||||
return serviceGps.getPointDog(lat, lon);
|
||||
}
|
||||
//</editor-fold>
|
||||
//</editor-fold>
|
||||
}
|
||||
Loading…
Reference in New Issue