Cleanup: TrackRecordingServiceConnection has now an execute method (avoid instantiation).

This commit is contained in:
Dennis Guse
2023-12-26 11:54:38 +01:00
parent 59aeec4fbc
commit 9a0f927182
6 changed files with 21 additions and 45 deletions
@@ -80,7 +80,7 @@ public class TrackRecordingServiceConnection {
public void bind(@NonNull Context context) {
if (trackRecordingService != null) {
// Service is already started and bound.
callback.onConnected(trackRecordingService, this);
return;
}
@@ -89,32 +89,6 @@ public class TrackRecordingServiceConnection {
context.bindService(new Intent(context, TrackRecordingService.class), serviceConnection, flags);
}
public void startAndBind(Context context) {
if (trackRecordingService != null) {
// Service is already started and bound.
return;
}
ContextCompat.startForegroundService(context, new Intent(context, TrackRecordingService.class));
bind(context);
}
//TODO There should be a better way to implement this.
/**
* Triggers the onConnected() callback even if already connected.
*/
//TODO Check if this is actually needed as it is used to re-connect from Activities in onResume by using a LiveData; might be obsolete. If not, there should be a better way to implement this.
@Deprecated
public void startAndBindWithCallback(Context context) {
if (trackRecordingService == null) {
startAndBind(context);
return;
}
callback.onConnected(trackRecordingService, this);
}
/**
* Unbinds the service (but leave it running).
*/
@@ -178,4 +152,15 @@ public class TrackRecordingServiceConnection {
public interface Callback {
void onConnected(TrackRecordingService service, TrackRecordingServiceConnection self);
}
public static void execute(Context context, Callback callback) {
Callback withUnbind = (service, connection) -> {
callback.onConnected(service, connection);
connection.unbind(context);
};
new TrackRecordingServiceConnection(withUnbind)
.bind(context);
ContextCompat.startForegroundService(context, new Intent(context, TrackRecordingService.class));
}
}