Skip to content

Commit

Permalink
Bugfix: PublicAPI stop should not start TrackRecordingService in fore…
Browse files Browse the repository at this point in the history
…ground.

Actually, TrackRecordingService will only be start in foreground if needed.

Fixes #1824.
  • Loading branch information
dennisguse committed Feb 13, 2024
1 parent cb9b947 commit 1bd3d66
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.dennisguse.opentracks.publicapi;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.GrantPermissionRule;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.TestUtil;
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.util.IntentUtils;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class PublicApiTest {

@Rule
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();

private final Context context = ApplicationProvider.getApplicationContext();

@Test
public void StartTest() {
PreferencesUtils.setBoolean(R.string.publicapi_enabled_key, true);

context.startActivity(IntentUtils.newIntent(context, StartRecording.class));
}

@Test
public void StartStopTest() throws InterruptedException {
PreferencesUtils.setBoolean(R.string.publicapi_enabled_key, true);

context.startActivity(IntentUtils.newIntent(context, StartRecording.class));

Thread.sleep(5000);

context.startActivity(IntentUtils.newIntent(context, StopRecording.class));
}

@Test
public void StopAndWait() throws InterruptedException {
PreferencesUtils.setBoolean(R.string.publicapi_enabled_key, true);

context.startActivity(IntentUtils.newIntent(context, StopRecording.class));

Thread.sleep(10000);

//No ForegroundServiceDidNotStartInTimeException should be happening.
}
}
4 changes: 2 additions & 2 deletions src/main/java/de/dennisguse/opentracks/TrackListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected void onCreate(Bundle savedInstanceState) {
if (gpsStatusValue.isGpsStarted()) {
recordingStatusConnection.stopService(this);
} else {
TrackRecordingServiceConnection.execute(this, (service, connection) -> service.tryStartSensors());
TrackRecordingServiceConnection.executeForeground(this, (service, connection) -> service.tryStartSensors());
}
}
});
Expand All @@ -178,7 +178,7 @@ protected void onCreate(Bundle savedInstanceState) {
// Not Recording -> Recording
Log.i(TAG, "Starting recording");
updateGpsMenuItem(false, true);
TrackRecordingServiceConnection.execute(this, (service, connection) -> {
TrackRecordingServiceConnection.executeForeground(this, (service, connection) -> {
Track.Id trackId = service.startNewTrack();

Intent newIntent = IntentUtils.newIntent(TrackListActivity.this, TrackRecordingActivity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

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

Intent newIntent = IntentUtils.newIntent(TrackRecordedActivity.this, TrackRecordingActivity.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void onChooseActivityTypeDone(ActivityType activityType) {
}

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

Intent newIntent = IntentUtils.newIntent(TrackStoppedActivity.this, TrackRecordingActivity.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class TrackRecordingServiceConnection {

private static final String TAG = TrackRecordingServiceConnection.class.getSimpleName();

private static final int SERVICE_BIND_FLAG = BuildConfig.DEBUG ? Context.BIND_DEBUG_UNBIND : 0;

private final Callback callback;

private TrackRecordingService trackRecordingService;
Expand Down Expand Up @@ -81,8 +83,19 @@ public void bind(@NonNull Context context) {
}

Log.i(TAG, "Binding the service.");
int flags = BuildConfig.DEBUG ? Context.BIND_DEBUG_UNBIND : 0;
context.bindService(new Intent(context, TrackRecordingService.class), serviceConnection, flags);

context.bindService(new Intent(context, TrackRecordingService.class), serviceConnection, SERVICE_BIND_FLAG);
}

public void bindWithStart(@NonNull Context context) {
if (trackRecordingService != null) {
callback.onConnected(trackRecordingService, this);
return;
}

Log.i(TAG, "Binding and starting the service (not in foreground).");

context.bindService(new Intent(context, TrackRecordingService.class), serviceConnection, Context.BIND_AUTO_CREATE + SERVICE_BIND_FLAG);
}

/**
Expand Down Expand Up @@ -135,13 +148,22 @@ public interface Callback {
}

public static void execute(Context context, Callback callback) {
Callback withUnbind = (service, connection) -> {
callback.onConnected(service, connection);
connection.unbind(context);
};
new TrackRecordingServiceConnection(withUnbind)
.bindWithStart(context);
}

public static void executeForeground(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));
// ContextCompat.startForegroundService(context, new Intent(context, TrackRecordingService.class));
}
}

0 comments on commit 1bd3d66

Please sign in to comment.