Skip to content

Commit

Permalink
Cleanup: TrackRecordingServiceConnection has now an execute method (a…
Browse files Browse the repository at this point in the history
…void instantiation).
  • Loading branch information
dennisguse committed Dec 26, 2023
1 parent 7c54009 commit 08ce689
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 35 deletions.
12 changes: 3 additions & 9 deletions src/main/java/de/dennisguse/opentracks/TrackListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,7 @@ protected void onCreate(Bundle savedInstanceState) {
if (gpsStatusValue.isGpsStarted()) {
recordingStatusConnection.stopService(this);
} else {
new TrackRecordingServiceConnection((service, connection) -> {
service.tryStartSensors();

connection.unbind(this);
}).startAndBindWithCallback(this);
TrackRecordingServiceConnection.execute(this, (service, connection) -> service.tryStartSensors());
}
}
});
Expand All @@ -182,15 +178,13 @@ protected void onCreate(Bundle savedInstanceState) {
// Not Recording -> Recording
Log.i(TAG, "Starting recording");
updateGpsMenuItem(false, true);
new TrackRecordingServiceConnection((service, connection) -> {
TrackRecordingServiceConnection.execute(this, (service, connection) -> {
Track.Id trackId = service.startNewTrack();

Intent newIntent = IntentUtils.newIntent(TrackListActivity.this, TrackRecordingActivity.class);
newIntent.putExtra(TrackRecordingActivity.EXTRA_TRACK_ID, trackId);
startActivity(newIntent);

connection.unbind(this);
}).startAndBind(this);
});
});
viewBinding.trackListFabAction.setOnLongClickListener((view) -> {
if (!recordingStatus.isRecording()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

if (item.getItemId() == R.id.track_detail_resume_track) {
new TrackRecordingServiceConnection((service, connection) -> {
TrackRecordingServiceConnection.execute(this, (service, connection) -> {
service.resumeTrack(trackId);

Intent newIntent = IntentUtils.newIntent(TrackRecordedActivity.this, TrackRecordingActivity.class)
.putExtra(TrackRecordingActivity.EXTRA_TRACK_ID, trackId);
startActivity(newIntent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

connection.unbind(this);
finish();
}).startAndBind(this);
});
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected void onResume() {
trackDataHub.setRecordingStatus(recordingStatus);
}

trackRecordingServiceConnection.startAndBindWithCallback(this);
trackRecordingServiceConnection.startAndBind(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,16 @@ public void onChooseActivityTypeDone(ActivityType activityType) {
}

private void resumeTrackAndFinish() {
new TrackRecordingServiceConnection((service, connection) -> {
TrackRecordingServiceConnection.execute(this, (service, connection) -> {
service.resumeTrack(trackId);

Intent newIntent = IntentUtils.newIntent(TrackStoppedActivity.this, TrackRecordingActivity.class)
.putExtra(TrackRecordingActivity.EXTRA_TRACK_ID, trackId);
startActivity(newIntent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

connection.unbind(this);
finish();
}).startAndBind(this);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public TrackRecordingServiceConnection(@NonNull Callback callback) {

public void bind(@NonNull Context context) {
if (trackRecordingService != null) {
// Service is already started and bound.
return;
throw new RuntimeException("Already bound to service");
}

Log.i(TAG, "Binding the service.");
Expand All @@ -91,30 +90,14 @@ public void bind(@NonNull Context context) {

public void startAndBind(Context context) {
if (trackRecordingService != null) {
// Service is already started and bound.
return;
throw new RuntimeException("Already bound to service");
}

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).
*/
Expand Down Expand Up @@ -178,4 +161,13 @@ public void stopRecording(@NonNull Context context) {
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)
.startAndBind(context);
}
}

0 comments on commit 08ce689

Please sign in to comment.