Vibration when approaching object.
This commit is contained in:
parent
928cd65dba
commit
afbd353f71
|
|
@ -30,4 +30,6 @@
|
|||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
</manifest>
|
||||
|
|
@ -12,6 +12,7 @@ import android.net.Uri;
|
|||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Vibrator;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.RequiresApi;
|
||||
|
|
@ -181,7 +182,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
textViewCurrentLocation.setVisibility(View.GONE);
|
||||
|
||||
if (serviceGps == null) {
|
||||
serviceGps = new ServiceGps();
|
||||
serviceGps = new ServiceGps((Vibrator) getSystemService(Context.VIBRATOR_SERVICE));
|
||||
serviceGps.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
package fr.chteufleur.mytrackingdog.services;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.location.LocationProvider;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.VibrationEffect;
|
||||
import android.os.Vibrator;
|
||||
import android.util.Log;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
|
@ -40,6 +44,10 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
|||
|
||||
private final Traces traces = new Traces();
|
||||
|
||||
public ServiceGps(Vibrator vibrator) {
|
||||
this.vibrator = vibrator;
|
||||
}
|
||||
|
||||
public void setLocationManager(LocationManager locationManager) {
|
||||
this.locationManager = locationManager;
|
||||
}
|
||||
|
|
@ -90,6 +98,8 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
|||
Log.i(TAG, "Stop location");
|
||||
}
|
||||
|
||||
private final Vibrator vibrator;
|
||||
private boolean nearObjectVibration = false;
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
|
|
@ -98,6 +108,27 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
|||
traces.addCurrentPoint(currentLocation);
|
||||
setChanged();
|
||||
notifyObservers(NOTIF_NEW_LOCATION);
|
||||
|
||||
if (isDogActivated()) {
|
||||
if (isNearObjects()) {
|
||||
if (!nearObjectVibration) {
|
||||
nearObjectVibration = true;
|
||||
vibrate(1_000);
|
||||
}
|
||||
} else {
|
||||
nearObjectVibration = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
|
@ -187,11 +218,18 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
|||
return traces.getPointObjectDog(lat, lon);
|
||||
}
|
||||
|
||||
public boolean isNearObjects() {
|
||||
return !foundNearObjects(20).isEmpty();
|
||||
}
|
||||
public List<WayPointLocation> foundNearObjects() {
|
||||
return foundNearObjects(10);
|
||||
}
|
||||
|
||||
public List<WayPointLocation> foundNearObjects(int distance) {
|
||||
List<WayPointLocation> ret = new ArrayList<>();
|
||||
MyLocation curLoc = currentLocation;
|
||||
for (MyLocation ml: getListGeoPointObjectsTrail()) {
|
||||
if (curLoc.distanceTo(ml) < 10 && ml instanceof WayPointLocation) {
|
||||
if (curLoc.distanceTo(ml) < distance && ml instanceof WayPointLocation) {
|
||||
ret.add((WayPointLocation) ml);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue