Cleanup: simplify TrackServiceConnection.

This commit is contained in:
Dennis Guse
2023-12-25 21:36:19 +01:00
parent 8d7a8719bc
commit 41da2f5b53
3 changed files with 13 additions and 26 deletions
@@ -126,7 +126,7 @@ public class TrackRecordedActivity extends AbstractTrackDeleteActivity implement
trackDataHub.loadTrack(trackId); trackDataHub.loadTrack(trackId);
} }
trackRecordingServiceConnection.bind(this); trackRecordingServiceConnection.startConnection(this);
} }
@Override @Override
@@ -58,13 +58,6 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
this.callback = callback; this.callback = callback;
} }
public void bind(@NonNull Context context) {
if (trackRecordingService != null) {
return;
}
context.bindService(new Intent(context, TrackRecordingService.class), this, 0);
}
/** /**
* Starts and binds the service. * Starts and binds the service.
* *
@@ -132,11 +125,6 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
context.stopService(new Intent(context, TrackRecordingService.class)); context.stopService(new Intent(context, TrackRecordingService.class));
} }
@Nullable
public TrackRecordingService getServiceIfBound() {
return trackRecordingService;
}
private void setTrackRecordingService(TrackRecordingService value) { private void setTrackRecordingService(TrackRecordingService value) {
trackRecordingService = value; trackRecordingService = value;
if (callback != null) { if (callback != null) {
@@ -173,19 +161,19 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
@Nullable @Nullable
public Marker.Id addMarker(Context context, String name, String category, String description, String photoUrl) { public Marker.Id addMarker(Context context, String name, String category, String description, String photoUrl) {
TrackRecordingService trackRecordingService = getServiceIfBound();
if (trackRecordingService == null) { if (trackRecordingService == null) {
Log.d(TAG, "Unable to add marker, no track recording service"); Log.d(TAG, "Unable to add marker, no track recording service");
} else { return null;
try { }
Marker.Id marker = trackRecordingService.insertMarker(name, category, description, photoUrl);
if (marker != null) { try {
Toast.makeText(context, R.string.marker_add_success, Toast.LENGTH_SHORT).show(); Marker.Id marker = trackRecordingService.insertMarker(name, category, description, photoUrl);
return marker; if (marker != null) {
} Toast.makeText(context, R.string.marker_add_success, Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) { return marker;
Log.e(TAG, "Unable to add marker.", e);
} }
} catch (IllegalStateException e) {
Log.e(TAG, "Unable to add marker.", e);
} }
Toast.makeText(context, R.string.marker_add_error, Toast.LENGTH_LONG).show(); Toast.makeText(context, R.string.marker_add_error, Toast.LENGTH_LONG).show();
@@ -193,7 +181,6 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
} }
public void stopRecording(@NonNull Context context) { public void stopRecording(@NonNull Context context) {
TrackRecordingService trackRecordingService = getServiceIfBound();
if (trackRecordingService == null) { if (trackRecordingService == null) {
Log.e(TAG, "TrackRecordingService not connected."); Log.e(TAG, "TrackRecordingService not connected.");
} else { } else {
@@ -203,7 +190,7 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
} }
public interface Callback { public interface Callback {
void onConnected(TrackRecordingService service, TrackRecordingServiceConnection connection); void onConnected(TrackRecordingService service, TrackRecordingServiceConnection self);
default void onDisconnected() { default void onDisconnected() {
} }
@@ -125,7 +125,7 @@ public class MarkerListActivity extends AbstractActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
trackRecordingServiceConnection.bind(this); trackRecordingServiceConnection.startConnection(this);
this.invalidateOptionsMenu(); this.invalidateOptionsMenu();
loadData(); loadData();
} }