forked from upstream-mirrors/OpenTracks
Cleanup: TrackRecordingServiceConnection has now an execute method (avoid instantiation).
This commit is contained in:
+12
-27
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user