Add menu to enable/disable vibration on approaching.

This commit is contained in:
chteufleur 2018-09-28 16:01:31 +02:00
parent afbd353f71
commit fd2947d9b8
4 changed files with 47 additions and 4 deletions

View File

@ -182,7 +182,10 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
textViewCurrentLocation.setVisibility(View.GONE); textViewCurrentLocation.setVisibility(View.GONE);
if (serviceGps == null) { if (serviceGps == null) {
serviceGps = new ServiceGps((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)); serviceGps = new ServiceGps(
(Vibrator) getSystemService(Context.VIBRATOR_SERVICE),
PreferenceManager.getDefaultSharedPreferences(ctx)
);
serviceGps.addObserver(this); serviceGps.addObserver(this);
} }
} }
@ -369,6 +372,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu); getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem vibrationObjectMenuItem = menu.findItem(R.id.action_active_vibration_object);
vibrationObjectMenuItem.setChecked(serviceGps.isVibrationNearObjectEnabled());
return true; return true;
} }
@ -399,6 +405,12 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
Toast.makeText(ctx, "Aucune trace enregistré.", Toast.LENGTH_LONG).show(); Toast.makeText(ctx, "Aucune trace enregistré.", Toast.LENGTH_LONG).show();
} }
return true; return true;
} else if (id == R.id.action_active_vibration_object) {
boolean checked = item.isChecked();
item.setChecked(!checked);
serviceGps.setVibrationNearObjectEnabled(!checked);
return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);

View File

@ -1,6 +1,6 @@
package fr.chteufleur.mytrackingdog.services; package fr.chteufleur.mytrackingdog.services;
import android.content.res.Resources; import android.content.SharedPreferences;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
@ -36,6 +36,8 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
private static final String TAG = ServiceGps.class.getName(); 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 static final String PREF_VIBRATION_NEAR_OBJECT_ENABLED = "PREF_VIBRATION_NEAR_OBJECT_ENABLED";
private File lastExportedTrailFile = null; private File lastExportedTrailFile = null;
private LocationManager locationManager; private LocationManager locationManager;
@ -43,9 +45,15 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
private String appName = ""; private String appName = "";
private final Traces traces = new Traces(); private final Traces traces = new Traces();
private final SharedPreferences preferences;
public ServiceGps(Vibrator vibrator) { public ServiceGps(Vibrator vibrator, SharedPreferences preferences) {
this.vibrator = vibrator; this.vibrator = vibrator;
this.preferences = preferences;
if (preferences != null) {
this.vibrationNearObjectEnabled = preferences.getBoolean(PREF_VIBRATION_NEAR_OBJECT_ENABLED, false);
}
} }
public void setLocationManager(LocationManager locationManager) { public void setLocationManager(LocationManager locationManager) {
@ -99,8 +107,22 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
} }
private final Vibrator vibrator; private final Vibrator vibrator;
private boolean vibrationNearObjectEnabled = false;
private boolean nearObjectVibration = 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;
}
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged"); Log.i(TAG, "onLocationChanged");
@ -110,7 +132,7 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
notifyObservers(NOTIF_NEW_LOCATION); notifyObservers(NOTIF_NEW_LOCATION);
if (isDogActivated()) { if (isDogActivated()) {
if (isNearObjects()) { if (vibrationNearObjectEnabled && isNearObjects()) {
if (!nearObjectVibration) { if (!nearObjectVibration) {
nearObjectVibration = true; nearObjectVibration = true;
vibrate(1_000); vibrate(1_000);

View File

@ -12,4 +12,12 @@
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/action_send_gpx_trail" android:title="@string/action_send_gpx_trail"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_active_vibration_object"
android:checkable="true"
android:orderInCategory="100"
android:title="@string/action_active_vibration_object"
app:showAsAction="never" />
</menu> </menu>

View File

@ -10,4 +10,5 @@
<string name="action_import_gpx">Import GPX</string> <string name="action_import_gpx">Import GPX</string>
<string name="action_send_gpx_trail">Envoyer trace du traceur</string> <string name="action_send_gpx_trail">Envoyer trace du traceur</string>
<string name="action_send_to">Envoyer par</string> <string name="action_send_to">Envoyer par</string>
<string name="action_active_vibration_object">Active vibration objets</string>
</resources> </resources>