mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
Fixed ControlRecordingService: only unbind if connected.
This commit is contained in:
+8
-6
@@ -25,7 +25,9 @@ import androidx.test.rule.ServiceTestRule;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
@@ -40,16 +42,17 @@ import static org.mockito.Mockito.when;
|
||||
*
|
||||
* @author Youtao Liu
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ControlRecordingServiceTest {
|
||||
|
||||
private Context context = ApplicationProvider.getApplicationContext();
|
||||
|
||||
@Rule
|
||||
public final ServiceTestRule mServiceRule = new ServiceTestRule();
|
||||
|
||||
@Rule
|
||||
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
|
||||
|
||||
private Context context = ApplicationProvider.getApplicationContext();
|
||||
|
||||
@Mock
|
||||
private ITrackRecordingService iTrackRecordingServiceMock;
|
||||
|
||||
@@ -68,7 +71,7 @@ public class ControlRecordingServiceTest {
|
||||
|
||||
when(iTrackRecordingServiceMock.startNewTrack()).thenReturn(1L);
|
||||
subject.onHandleIntent(intent, iTrackRecordingServiceMock);
|
||||
verify(iTrackRecordingServiceMock);
|
||||
verify(iTrackRecordingServiceMock).startNewTrack();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,10 +82,9 @@ public class ControlRecordingServiceTest {
|
||||
@Test
|
||||
public void testStopRecording() throws TimeoutException {
|
||||
Intent intent = startControlRecordingService(context.getString(R.string.track_action_end));
|
||||
iTrackRecordingServiceMock.endCurrentTrack();
|
||||
|
||||
subject.onHandleIntent(intent, iTrackRecordingServiceMock);
|
||||
verify(iTrackRecordingServiceMock);
|
||||
verify(iTrackRecordingServiceMock).endCurrentTrack();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,7 +96,7 @@ public class ControlRecordingServiceTest {
|
||||
Intent intent = new Intent(context, ControlRecordingService.class);
|
||||
intent.setAction(action);
|
||||
mServiceRule.startService(intent);
|
||||
subject = ((ControlRecordingService) mServiceRule.bindService(intent));
|
||||
subject = ((ControlRecordingService.LocalBinder) mServiceRule.bindService(intent)).getService();
|
||||
return intent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,12 +20,14 @@ import android.app.IntentService;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import de.dennisguse.opentracks.widgets.TrackWidgetProvider;
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.widgets.TrackWidgetProvider;
|
||||
|
||||
/**
|
||||
* A service to control starting and stopping of a recording. This service,
|
||||
@@ -57,6 +59,39 @@ public class ControlRecordingService extends IntentService implements ServiceCon
|
||||
bindService(newIntent, this, 0);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return new LocalBinder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the intent to start or stop a recording.
|
||||
*
|
||||
* @param intent to be handled
|
||||
* @param service the trackRecordingService
|
||||
*/
|
||||
@VisibleForTesting
|
||||
void onHandleIntent(Intent intent, ITrackRecordingService service) {
|
||||
String action = intent.getAction();
|
||||
if (action != null) {
|
||||
if (action.equals(getString(R.string.track_action_start))) {
|
||||
service.startNewTrack();
|
||||
} else if (action.equals(getString(R.string.track_action_end))) {
|
||||
service.endCurrentTrack();
|
||||
} else if (action.equals(getString(R.string.track_action_pause))) {
|
||||
service.pauseCurrentTrack();
|
||||
} else if (action.equals(getString(R.string.track_action_resume))) {
|
||||
service.resumeCurrentTrack();
|
||||
}
|
||||
}
|
||||
if (connected) {
|
||||
unbindService(this);
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
trackRecordingService = (ITrackRecordingService) service;
|
||||
@@ -97,31 +132,14 @@ public class ControlRecordingService extends IntentService implements ServiceCon
|
||||
onHandleIntent(intent, trackRecordingService);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles the intent to start or stop a recording.
|
||||
*
|
||||
* @param intent to be handled
|
||||
* @param service the trackRecordingService
|
||||
*/
|
||||
@VisibleForTesting
|
||||
void onHandleIntent(Intent intent, ITrackRecordingService service) {
|
||||
String action = intent.getAction();
|
||||
if (action != null) {
|
||||
if (action.equals(getString(R.string.track_action_start))) {
|
||||
service.startNewTrack();
|
||||
} else if (action.equals(getString(R.string.track_action_end))) {
|
||||
service.endCurrentTrack();
|
||||
} else if (action.equals(getString(R.string.track_action_pause))) {
|
||||
service.pauseCurrentTrack();
|
||||
} else if (action.equals(getString(R.string.track_action_resume))) {
|
||||
service.resumeCurrentTrack();
|
||||
}
|
||||
public class LocalBinder extends Binder {
|
||||
ControlRecordingService getService() {
|
||||
return ControlRecordingService.this;
|
||||
}
|
||||
unbindService(this);
|
||||
connected = false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
Reference in New Issue
Block a user