PeriodTaskFactory: refactor interfaces.

This commit is contained in:
Dennis Guse
2021-08-24 06:20:52 +02:00
parent 29a1daa177
commit b32e87e10b
7 changed files with 35 additions and 72 deletions
@@ -50,8 +50,8 @@ import de.dennisguse.opentracks.services.handlers.EGM2008CorrectionManager;
import de.dennisguse.opentracks.services.handlers.GpsStatusValue;
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
import de.dennisguse.opentracks.services.tasks.AnnouncementPeriodicTask;
import de.dennisguse.opentracks.services.tasks.PeriodicTask;
import de.dennisguse.opentracks.services.tasks.PeriodicTaskExecutor;
import de.dennisguse.opentracks.services.tasks.PeriodicTaskFactory;
import de.dennisguse.opentracks.settings.SettingsActivity;
import de.dennisguse.opentracks.stats.TrackStatistics;
import de.dennisguse.opentracks.util.ExportUtils;
@@ -209,7 +209,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
return trackRecordingManager.insertMarker(name, category, description, photoUrl);
}
public void run(@NonNull PeriodicTask periodicTask) {
public void run(@NonNull PeriodicTaskFactory.Task periodicTask) {
periodicTask.run(recordingStatus.getTrackId(), trackRecordingManager.getTrackStatistics());
}
@@ -29,8 +29,6 @@ public class TrackPointCreator {
private Context context;
private final Callback service;
// Disabled to simplify testing and implementation of #822
// private ExecutorService serviceExecutor;
@NonNull
private Clock clock = Clock.systemUTC();
@@ -52,7 +50,6 @@ public class TrackPointCreator {
public void start(@NonNull Context context) {
this.context = context;
// serviceExecutor = Executors.newSingleThreadExecutor();
SharedPreferences sharedPreferences = PreferencesUtils.getSharedPreferences(context);
locationHandler.onStart(context, sharedPreferences);
@@ -100,11 +97,6 @@ public class TrackPointCreator {
public void stop() {
locationHandler.onStop();
// if (serviceExecutor != null) {
// serviceExecutor.shutdownNow();
// }
// serviceExecutor = null;
if (remoteSensorManager != null) {
remoteSensorManager.stop();
remoteSensorManager = null;
@@ -128,13 +120,8 @@ public class TrackPointCreator {
}
public void onNewTrackPoint(TrackPoint trackPoint, Distance thresholdHorizontalAccuracy) {
// if (serviceExecutor == null || serviceExecutor.isTerminated() || serviceExecutor.isShutdown()) {
// return;
// }
fillAndReset(trackPoint);
// serviceExecutor.execute(() -> service.newTrackPoint(trackPoint, thresholdHorizontalAccuracy));
service.newTrackPoint(trackPoint, thresholdHorizontalAccuracy);
}
@@ -42,7 +42,7 @@ import de.dennisguse.opentracks.viewmodels.IntervalStatistics;
*
* @author Sandor Dornbush
*/
public class AnnouncementPeriodicTask implements PeriodicTask {
public class AnnouncementPeriodicTask implements PeriodicTaskFactory.Task {
private static final String TAG = AnnouncementPeriodicTask.class.getSimpleName();
@@ -209,7 +209,7 @@ public class AnnouncementPeriodicTask implements PeriodicTask {
@Override
@NonNull
public PeriodicTask create(Context context) {
public Task create(Context context) {
return new AnnouncementPeriodicTask(context);
}
}
@@ -1,46 +0,0 @@
/*
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package de.dennisguse.opentracks.services.tasks;
import androidx.annotation.NonNull;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.stats.TrackStatistics;
/**
* This is interface for a task that will be executed on some schedule.
*
* @author Sandor Dornbush
*/
public interface PeriodicTask {
/**
* Sets up this task for subsequent calls to the run method.
*/
void start();
/**
* This method will be called periodically.
*/
@Deprecated
void run(@NonNull Track.Id trackId, @NonNull TrackStatistics trackStatistics);
/**
* Shuts down this task and clean up resources.
*/
void shutdown();
}
@@ -49,7 +49,7 @@ public class PeriodicTaskExecutor {
*/
private int taskFrequency;
private PeriodicTask periodicTask;
private PeriodicTaskFactory.Task periodicTask;
// Time periodic task executor
private TimerTaskExecutor timerTaskExecutor = null;
@@ -20,18 +20,40 @@ import android.content.Context;
import androidx.annotation.NonNull;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.stats.TrackStatistics;
/**
* An interface for classes that can create {@link PeriodicTask}.
* An interface for classes that can create {@link Task}.
*
* @author Sandor Dornbush
*/
interface PeriodicTaskFactory {
public interface PeriodicTaskFactory {
@NonNull
Task create(Context context);
/**
* Creates a {@link PeriodicTask}.
* This is interface for a task that will be executed on some schedule.
*
* @return the task, or null if the task is not supported
* @author Sandor Dornbush
*/
@NonNull
PeriodicTask create(Context context);
interface Task {
/**
* Sets up this task for subsequent calls to the run method.
*/
void start();
/**
* This method will be called periodically.
*/
@Deprecated
void run(@NonNull Track.Id trackId, @NonNull TrackStatistics trackStatistics);
/**
* Shuts down this task and clean up resources.
*/
void shutdown();
}
}
@@ -30,13 +30,13 @@ import de.dennisguse.opentracks.services.TrackRecordingService;
*/
class TimerTaskExecutor {
private final PeriodicTask periodicTask;
private final PeriodicTaskFactory.Task periodicTask;
private final TrackRecordingService trackRecordingService;
private TimerTask timerTask;
private Timer timer;
public TimerTaskExecutor(PeriodicTask periodicTask, TrackRecordingService trackRecordingService) {
public TimerTaskExecutor(PeriodicTaskFactory.Task periodicTask, TrackRecordingService trackRecordingService) {
this.periodicTask = periodicTask;
this.trackRecordingService = trackRecordingService;
}