Move WakeLock to SensorManager (was TrackRecordingService).

This commit is contained in:
Dennis Guse
2025-11-01 09:56:31 +01:00
parent 65c8e8542e
commit c687308d97
3 changed files with 12 additions and 12 deletions
@@ -3,6 +3,7 @@ package de.dennisguse.opentracks.sensors;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -15,6 +16,7 @@ import de.dennisguse.opentracks.sensors.sensorData.Aggregator;
import de.dennisguse.opentracks.sensors.sensorData.Raw; import de.dennisguse.opentracks.sensors.sensorData.Raw;
import de.dennisguse.opentracks.sensors.sensorData.SensorDataSet; import de.dennisguse.opentracks.sensors.sensorData.SensorDataSet;
import de.dennisguse.opentracks.services.handlers.TrackPointCreator; import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
import de.dennisguse.opentracks.util.SystemUtils;
public class SensorManager implements SharedPreferences.OnSharedPreferenceChangeListener { public class SensorManager implements SharedPreferences.OnSharedPreferenceChangeListener {
@@ -24,6 +26,8 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
@VisibleForTesting @VisibleForTesting
public SensorDataSet sensorDataSet; public SensorDataSet sensorDataSet;
private PowerManager.WakeLock wakeLock;
private final TrackPointCreator observer; private final TrackPointCreator observer;
private final SensorDataChangedObserver listener = new SensorDataChangedObserver() { private final SensorDataChangedObserver listener = new SensorDataChangedObserver() {
@@ -71,6 +75,8 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
throw new RuntimeException("SensorManager cannot be started twice; stop first."); throw new RuntimeException("SensorManager cannot be started twice; stop first.");
} }
wakeLock = SystemUtils.acquireWakeLock(context, wakeLock);
gpsManager = new GpsManager(observer, listener); gpsManager = new GpsManager(observer, listener);
altitudeSumManager = new GainManager(listener); altitudeSumManager = new GainManager(listener);
bluetoothSensorManager = new BluetoothRemoteSensorManager(context, handler, listener); bluetoothSensorManager = new BluetoothRemoteSensorManager(context, handler, listener);
@@ -83,6 +89,8 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
} }
public void stop(Context context) { public void stop(Context context) {
wakeLock = SystemUtils.releaseWakeLock(wakeLock);
bluetoothSensorManager.stop(context); bluetoothSensorManager.stop(context);
bluetoothSensorManager = null; bluetoothSensorManager = null;
@@ -22,7 +22,6 @@ import android.content.SharedPreferences;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.PowerManager.WakeLock;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -44,7 +43,6 @@ import de.dennisguse.opentracks.sensors.GpsStatusValue;
import de.dennisguse.opentracks.services.announcement.VoiceAnnouncementManager; import de.dennisguse.opentracks.services.announcement.VoiceAnnouncementManager;
import de.dennisguse.opentracks.services.handlers.TrackPointCreator; import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
import de.dennisguse.opentracks.settings.PreferencesUtils; import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.util.SystemUtils;
public class TrackRecordingService extends Service implements TrackPointCreator.Callback, SharedPreferences.OnSharedPreferenceChangeListener, TrackRecordingManager.IdleObserver { public class TrackRecordingService extends Service implements TrackPointCreator.Callback, SharedPreferences.OnSharedPreferenceChangeListener, TrackRecordingManager.IdleObserver {
@@ -89,7 +87,6 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
private MutableLiveData<RecordingData> recordingDataObservable; private MutableLiveData<RecordingData> recordingDataObservable;
// The following variables are set when recording: // The following variables are set when recording:
private WakeLock wakeLock; //TODO Move to SensorManager
private Handler handler; private Handler handler;
private TrackPointCreator trackPointCreator; private TrackPointCreator trackPointCreator;
@@ -125,7 +122,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
if (isRecording()) { if (isRecording()) {
endCurrentTrack(); endCurrentTrack();
} }
if (isSensorStarted()) { if (trackPointCreator != null && trackPointCreator.isStarted()) {
stopSensors(); stopSensors();
} }
@@ -200,7 +197,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
} }
public void tryStartSensors() { public void tryStartSensors() {
if (isSensorStarted()) return; if (trackPointCreator.isStarted()) return;
Log.i(TAG, "tryStartSensors"); Log.i(TAG, "tryStartSensors");
@@ -208,12 +205,11 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
} }
private synchronized void startSensors() { private synchronized void startSensors() {
if (isSensorStarted()) { if (trackPointCreator.isStarted()) {
Log.i(TAG, "sensors already started; skipping"); Log.i(TAG, "sensors already started; skipping");
return; return;
} }
Log.i(TAG, "startSensors"); Log.i(TAG, "startSensors");
wakeLock = SystemUtils.acquireWakeLock(this, wakeLock);
trackPointCreator.start(this, handler); trackPointCreator.start(this, handler);
ServiceCompat.startForeground(this, TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.onSensorsStarted(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE); ServiceCompat.startForeground(this, TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.onSensorsStarted(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE);
@@ -241,7 +237,6 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
trackPointCreator.stop(); trackPointCreator.stop();
stopForeground(true); stopForeground(true);
notificationManager.stop(); notificationManager.stop();
wakeLock = SystemUtils.releaseWakeLock(wakeLock);
gpsStatusObservable.postValue(STATUS_GPS_DEFAULT); gpsStatusObservable.postValue(STATUS_GPS_DEFAULT);
} }
@@ -347,9 +342,6 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
return recordingStatus.isRecording(); return recordingStatus.isRecording();
} }
private boolean isSensorStarted() {
return wakeLock != null;
}
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key) {
@@ -47,7 +47,7 @@ public class TrackPointCreator implements SharedPreferences.OnSharedPreferenceCh
sensorManager.start(context, handler); sensorManager.start(context, handler);
} }
private boolean isStarted() { public boolean isStarted() {
return context != null; return context != null;
} }