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 27, 2023
1 parent 7c54009 commit d8c6d7f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 45 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.bind(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 @@ -37,8 +37,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

if (PreferencesUtils.isPublicAPIenabled()) {
Log.i(TAG, "Received and trying to execute requested action.");
new TrackRecordingServiceConnection(serviceConnectedCallback)
.startAndBind(this);
TrackRecordingServiceConnection.execute(this, serviceConnectedCallback);
} else {
Toast.makeText(this, getString(R.string.settings_public_api_disabled_toast), Toast.LENGTH_LONG).show();
Log.w(TAG, "Public API is disabled; ignoring request.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public TrackRecordingServiceConnection(@NonNull Callback callback) {

public void bind(@NonNull Context context) {
if (trackRecordingService != null) {
// Service is already started and bound.
callback.onConnected(trackRecordingService, this);
return;
}

Expand All @@ -89,32 +89,6 @@ public void bind(@NonNull Context context) {
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).
*/
Expand Down Expand Up @@ -178,4 +152,15 @@ 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)
.bind(context);

ContextCompat.startForegroundService(context, new Intent(context, TrackRecordingService.class));
}
}

0 comments on commit d8c6d7f

Please sign in to comment.