Skip to content

Commit

Permalink
Merge pull request #1797 from OpenTracksApp/refactoring#1708
Browse files Browse the repository at this point in the history
Refactoring TrackRecordingServiceConnection
  • Loading branch information
dennisguse authored Dec 27, 2023
2 parents 8d7a871 + 9a0f927 commit eaad33c
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 160 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ dependencies {
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core:1.12.0'
implementation 'androidx.core:core-splashscreen:1.0.1'
implementation 'androidx.mediarouter:mediarouter:1.6.0'

Expand Down
15 changes: 0 additions & 15 deletions src/main/java/de/dennisguse/opentracks/TrackEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.databinding.TrackEditBinding;
import de.dennisguse.opentracks.fragments.ChooseActivityTypeDialogFragment;
import de.dennisguse.opentracks.services.TrackRecordingServiceConnection;
import de.dennisguse.opentracks.util.TrackUtils;

/**
Expand All @@ -44,7 +43,6 @@ public class TrackEditActivity extends AbstractActivity implements ChooseActivit

private static final String ICON_VALUE_KEY = "icon_value_key";

private TrackRecordingServiceConnection trackRecordingServiceConnection;
private ContentProviderUtils contentProviderUtils;
private Track track;
private ActivityType activityType;
Expand All @@ -55,7 +53,6 @@ public class TrackEditActivity extends AbstractActivity implements ChooseActivit
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);

trackRecordingServiceConnection = new TrackRecordingServiceConnection();
Track.Id trackId = getIntent().getParcelableExtra(EXTRA_TRACK_ID);
if (trackId == null) {
Log.e(TAG, "invalid trackId");
Expand Down Expand Up @@ -114,18 +111,6 @@ protected void onCreate(Bundle bundle) {
setSupportActionBar(viewBinding.bottomAppBarLayout.bottomAppBar);
}

@Override
protected void onStart() {
super.onStart();
trackRecordingServiceConnection.startConnection(this);
}

@Override
protected void onStop() {
super.onStop();
trackRecordingServiceConnection.unbind(this);
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/de/dennisguse/opentracks/TrackListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,9 @@ protected void onCreate(Bundle savedInstanceState) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
} else {
if (gpsStatusValue.isGpsStarted()) {
recordingStatusConnection.unbindAndStop(this);
recordingStatusConnection.startConnection(this); //TODO We need to stay listening!
recordingStatusConnection.stopService(this);
} else {
new TrackRecordingServiceConnection((service, connection) -> {
service.tryStartSensors();

connection.unbind(this);
}).startAndBindWithCallback(this);
TrackRecordingServiceConnection.execute(this, (service, connection) -> service.tryStartSensors());
}
}
});
Expand All @@ -183,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, true);
});
});
viewBinding.trackListFabAction.setOnLongClickListener((view) -> {
if (!recordingStatus.isRecording()) {
Expand Down Expand Up @@ -220,7 +213,7 @@ protected void onStart() {
super.onStart();

PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
recordingStatusConnection.startConnection(this);
recordingStatusConnection.bind(this);
}

@Override
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, true);
});
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ protected void onStart() {

PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);

trackRecordingServiceConnection.startConnection(this);
trackRecordingServiceConnection.bind(this);
trackDataHub.start();
}

Expand All @@ -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, true);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void onResume() {

PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);

trackRecordingServiceConnection.startConnection(getContext());
trackRecordingServiceConnection.bind(getContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

if (PreferencesUtils.isPublicAPIenabled()) {
Log.i(TAG, "Received and trying to execute requested action.");
new TrackRecordingServiceConnection(serviceConnectedCallback)
.startAndBind(this, isStartServiceForeground());
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.");
finish();
}
}

protected boolean isStartServiceForeground() {
return false;
}

protected abstract void execute(TrackRecordingService service);

protected abstract boolean isPostExecuteStopService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,4 @@ private void startDashboardAPI(@NonNull Track.Id trackId, @NonNull Bundle bundle
protected boolean isPostExecuteStopService() {
return false;
}

@Override
protected boolean isStartServiceForeground() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.app.ServiceCompat;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

Expand Down Expand Up @@ -111,7 +112,7 @@ public void onCreate() {
recordingDataObservable = new MutableLiveData<>(NOT_RECORDING);

trackPointCreator = new TrackPointCreator(this);
trackRecordingManager = new TrackRecordingManager(this, trackPointCreator, this , handler);
trackRecordingManager = new TrackRecordingManager(this, trackPointCreator, this, handler);

voiceAnnouncementManager = new VoiceAnnouncementManager(this);
notificationManager = new TrackRecordingServiceNotificationManager(this);
Expand Down Expand Up @@ -213,19 +214,15 @@ private synchronized void startSensors() {
Log.i(TAG, "startSensors");
wakeLock = SystemUtils.acquireWakeLock(this, wakeLock);
trackPointCreator.start(this, handler);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (!PermissionRequester.RECORDING.hasPermission(this)) {
Toast.makeText(this, R.string.permission_recording_failed, Toast.LENGTH_LONG).show();
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (!PermissionRequester.RECORDING.hasPermission(this)) {
Toast.makeText(this, R.string.permission_recording_failed, Toast.LENGTH_LONG).show();
return;
}

startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE);
} else {
startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this));
}

ServiceCompat.startForeground(this, TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE);
}

public void endCurrentTrack() {
Expand Down
Loading

0 comments on commit eaad33c

Please sign in to comment.