GPS Status: TrackListActivity uses the value of gps status from the service to select appropiate icon.

This commit is contained in:
Román
2020-08-04 15:13:38 +02:00
committed by Román Martínez
parent e2b741dd08
commit 9837680540
6 changed files with 55 additions and 19 deletions
@@ -83,6 +83,7 @@ public class TrackListActivity extends AbstractListActivity implements ConfirmDe
private TrackController trackController;
private ListView listView;
private ResourceCursorAdapter resourceCursorAdapter;
private GpsStatusValue gpsStatusValue;
private final LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {
@Override
@@ -175,25 +176,28 @@ public class TrackListActivity extends AbstractListActivity implements ConfirmDe
// After binding changes (e.g., becomes available), update the total time in trackController.
runOnUiThread(() -> trackController.update(isRecording, recordingTrackPaused));
if (!startGps) {
return;
}
TrackRecordingServiceInterface service = trackRecordingServiceConnection.getServiceIfBound();
if (service == null) {
Log.d(TAG, "service not available to start gps or a new recording");
return;
}
if (startGps) {
service.startGps();
startGps = false;
}
service.setListener(new BoundServiceListener() {
@Override
public void onGpsStatusChange(GpsStatusValue newStatus) {
updateGpsMenuItem(true, isRecording, newStatus.icon);
}
// Get GPS status and listen GPS status changes.
gpsStatusValue = service.getGpsStatus();
updateGpsMenuItem(true, isRecording);
service.setListener(newStatus -> {
gpsStatusValue = newStatus;
updateGpsMenuItem(true, isRecording);
});
if (!startGps) {
return;
}
service.startGps();
startGps = false;
gpsStatusValue = GpsStatusValue.GPS_ENABLED;
updateGpsMenuItem(true, isRecording);
}
};
@@ -222,6 +226,8 @@ public class TrackListActivity extends AbstractListActivity implements ConfirmDe
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gpsStatusValue = GpsStatusValue.GPS_NONE;
recordingTrackPaused = PreferencesUtils.isRecordingTrackPausedDefault(this);
contentProviderUtils = new ContentProviderUtils(this);
@@ -448,22 +454,21 @@ public class TrackListActivity extends AbstractListActivity implements ConfirmDe
* @param isRecording true if recording
*/
private void updateMenuItems(boolean isGpsStarted, boolean isRecording) {
updateGpsMenuItem(isGpsStarted, isRecording, R.drawable.ic_gps_not_fixed_24dp);
updateGpsMenuItem(isGpsStarted, isRecording);
}
/**
* Updates the menu items with the icon specified.
*
* @param isGpsStarted true if gps is started
* @param isRecording true if recording
* @param icon the icon drawable value
* @param isGpsStarted true if gps is started
* @param isRecording true if recording
*/
private void updateGpsMenuItem(boolean isGpsStarted, boolean isRecording, int icon) {
private void updateGpsMenuItem(boolean isGpsStarted, boolean isRecording) {
if (startGpsMenuItem != null) {
startGpsMenuItem.setVisible(!isRecording);
if (!isRecording) {
startGpsMenuItem.setTitle(isGpsStarted ? R.string.menu_stop_gps : R.string.menu_start_gps);
startGpsMenuItem.setIcon(isGpsStarted ? icon : R.drawable.ic_gps_off_24dp);
startGpsMenuItem.setIcon(isGpsStarted ? gpsStatusValue.icon : R.drawable.ic_gps_off_24dp);
}
}
}
@@ -698,4 +698,8 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
public void setRemoteSensorManager(BluetoothRemoteSensorManager remoteSensorManager) {
this.remoteSensorManager = remoteSensorManager;
}
public GpsStatusValue getGpsStatus() {
return handlerServer.getGpsStatus();
}
}
@@ -4,6 +4,7 @@ import androidx.annotation.VisibleForTesting;
import de.dennisguse.opentracks.content.data.TrackPoint;
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.services.handlers.GpsStatusValue;
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
/**
@@ -27,6 +28,11 @@ class TrackRecordingServiceBinder extends android.os.Binder implements TrackReco
trackRecordingService.addListener(listener);
}
@Override
public GpsStatusValue getGpsStatus() {
return trackRecordingService.getGpsStatus();
}
@Override
public void startGps() {
trackRecordingService.tryStartGps();
@@ -19,6 +19,7 @@ import androidx.annotation.VisibleForTesting;
import de.dennisguse.opentracks.content.data.TrackPoint;
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.services.handlers.GpsStatusValue;
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
/**
@@ -112,4 +113,6 @@ public interface TrackRecordingServiceInterface {
void newTrackPoint(TrackPoint trackPoint, int recordingGpsAccuracy);
void setListener(BoundServiceListener listener);
GpsStatusValue getGpsStatus();
}
@@ -60,6 +60,10 @@ public class HandlerServer {
service.newGpsStatus(gpsStatusValue);
}
public GpsStatusValue getGpsStatus() {
return locationHandler.getGpsStatus();
}
public interface HandlerServerInterface {
void newTrackPoint(TrackPoint trackPoint, int gpsAccuracy);
void newGpsStatus(GpsStatusValue gpsStatusValue);
@@ -90,6 +90,10 @@ class LocationHandler implements HandlerServer.Handler, LocationListener, GpsSta
*/
@Override
public void onLocationChanged(@NonNull Location location) {
if (gpsStatus != null) {
gpsStatus.onLocationChanged(location);
}
if (!LocationUtils.isValidLocation(location)) {
Log.w(TAG, "Ignore newTrackPoint. location is invalid.");
return;
@@ -122,10 +126,16 @@ class LocationHandler implements HandlerServer.Handler, LocationListener, GpsSta
@Override
public void onProviderEnabled(@NonNull String provider) {
if (gpsStatus != null) {
gpsStatus.onGpsEnabled();
}
}
@Override
public void onProviderDisabled(@NonNull String provider) {
if (gpsStatus != null) {
gpsStatus.onGpsDisabled();
}
}
private void registerLocationListener() {
@@ -161,4 +171,8 @@ class LocationHandler implements HandlerServer.Handler, LocationListener, GpsSta
public void onGpsStatusChanged(GpsStatusValue prevStatus, GpsStatusValue currentStatus) {
handlerServer.sendGpsStatus(currentStatus);
}
public GpsStatusValue getGpsStatus() {
return gpsStatus != null ? gpsStatus.getGpsStatus() : GpsStatusValue.GPS_NONE;
}
}