This commit is contained in:
Dennis Guse
2025-10-22 22:28:57 +02:00
parent 94595f2a1f
commit 26e7059af2
2 changed files with 29 additions and 34 deletions
@@ -24,7 +24,6 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.PowerManager.WakeLock; import android.os.PowerManager.WakeLock;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
@@ -42,7 +41,6 @@ import de.dennisguse.opentracks.data.models.Marker;
import de.dennisguse.opentracks.data.models.Track; import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.data.models.TrackPoint; import de.dennisguse.opentracks.data.models.TrackPoint;
import de.dennisguse.opentracks.sensors.GpsStatusValue; import de.dennisguse.opentracks.sensors.GpsStatusValue;
import de.dennisguse.opentracks.sensors.sensorData.SensorDataSet;
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;
@@ -216,7 +214,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
wakeLock = SystemUtils.acquireWakeLock(this, wakeLock); wakeLock = SystemUtils.acquireWakeLock(this, wakeLock);
trackPointCreator.start(this, handler); trackPointCreator.start(this, handler);
ServiceCompat.startForeground(this, TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(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);
} }
public void endCurrentTrack() { public void endCurrentTrack() {
@@ -240,7 +238,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
void stopSensors() { void stopSensors() {
trackPointCreator.stop(); trackPointCreator.stop();
stopForeground(true); stopForeground(true);
notificationManager.cancelNotification(); notificationManager.stop();
wakeLock = SystemUtils.releaseWakeLock(wakeLock); wakeLock = SystemUtils.releaseWakeLock(wakeLock);
gpsStatusObservable.postValue(STATUS_GPS_DEFAULT); gpsStatusObservable.postValue(STATUS_GPS_DEFAULT);
} }
@@ -284,7 +282,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
Log.e(TAG, e.getMessage() + " " + writer); Log.e(TAG, e.getMessage() + " " + writer);
return; return;
} }
notificationManager.updateContent(getString(gpsStatusValue.message)); notificationManager.updateGPSStatus(getString(gpsStatusValue.message));
gpsStatusObservable.postValue(gpsStatusValue); gpsStatusObservable.postValue(gpsStatusValue);
} }
@@ -60,22 +60,35 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh
.setSmallIcon(R.drawable.ic_logo_color_24dp); .setSmallIcon(R.drawable.ic_logo_color_24dp);
} }
void stop() {
cancelNotification();
}
@VisibleForTesting @VisibleForTesting
TrackRecordingServiceNotificationManager(NotificationManager notificationManager, NotificationCompat.Builder notificationBuilder) { TrackRecordingServiceNotificationManager(NotificationManager notificationManager, NotificationCompat.Builder notificationBuilder) {
this.notificationManager = notificationManager; this.notificationManager = notificationManager;
this.notificationBuilder = notificationBuilder; this.notificationBuilder = notificationBuilder;
} }
void updateContent(String content) { Notification onSensorsStarted(Context context) {
Intent intent = IntentUtils.newIntent(context, TrackListActivity.class);
int pendingIntentFlags = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
PendingIntent pendingIntent = TaskStackBuilder.create(context)
.addParentStack(TrackListActivity.class)
.addNextIntent(intent)
.getPendingIntent(0, pendingIntentFlags);
updateGPSStatus(context.getString(R.string.gps_starting));
notificationBuilder.setContentIntent(pendingIntent);
updateNotification();
return getNotification();
}
void updateGPSStatus(String content) {
notificationBuilder.setSubText(content); notificationBuilder.setSubText(content);
updateNotification(); updateNotification();
} }
void updateTrackPoint(Context context, TrackStatistics trackStatistics, TrackPoint trackPoint, Distance recordingGpsAccuracy) { void updateTrackPoint(Context context, TrackStatistics trackStatistics, TrackPoint trackPoint, Distance thresholdHorizontalAccuracy) {
String formattedAccuracy = context.getString(R.string.value_none); String formattedAccuracy = context.getString(R.string.value_none);
DistanceFormatter formatter = DistanceFormatter.Builder() DistanceFormatter formatter = DistanceFormatter.Builder()
@@ -85,7 +98,7 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh
if (trackPoint.hasHorizontalAccuracy()) { if (trackPoint.hasHorizontalAccuracy()) {
formattedAccuracy = formatter.formatDistance(trackPoint.getHorizontalAccuracy()); formattedAccuracy = formatter.formatDistance(trackPoint.getHorizontalAccuracy());
boolean currentLocationWasAccurate = trackPoint.getHorizontalAccuracy().lessThan(recordingGpsAccuracy); boolean currentLocationWasAccurate = trackPoint.getHorizontalAccuracy().lessThan(thresholdHorizontalAccuracy);
boolean shouldAlert = !currentLocationWasAccurate && previousLocationWasAccurate; boolean shouldAlert = !currentLocationWasAccurate && previousLocationWasAccurate;
notificationBuilder.setOnlyAlertOnce(!shouldAlert); notificationBuilder.setOnlyAlertOnce(!shouldAlert);
previousLocationWasAccurate = currentLocationWasAccurate; previousLocationWasAccurate = currentLocationWasAccurate;
@@ -100,31 +113,10 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh
notificationBuilder.setOnlyAlertOnce(true); notificationBuilder.setOnlyAlertOnce(true);
} }
Notification setGPSonlyStarted(Context context) { void stop() {
Intent intent = IntentUtils.newIntent(context, TrackListActivity.class);
int pendingIntentFlags = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
PendingIntent pendingIntent = TaskStackBuilder.create(context)
.addParentStack(TrackListActivity.class)
.addNextIntent(intent)
.getPendingIntent(0, pendingIntentFlags);
updateContent(context.getString(R.string.gps_starting));
notificationBuilder.setContentIntent(pendingIntent);
updateNotification();
return getNotification();
}
void cancelNotification() {
notificationManager.cancel(NOTIFICATION_ID); notificationManager.cancel(NOTIFICATION_ID);
} }
void setUnitSystem(UnitSystem unitSystem) {
this.unitSystem = unitSystem;
}
private void updateNotification() { private void updateNotification() {
notificationManager.notify(NOTIFICATION_ID, getNotification()); notificationManager.notify(NOTIFICATION_ID, getNotification());
} }
@@ -133,6 +125,11 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh
return notificationBuilder.build(); return notificationBuilder.build();
} }
@VisibleForTesting
void setUnitSystem(UnitSystem unitSystem) {
this.unitSystem = unitSystem;
}
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (PreferencesUtils.isKey(R.string.stats_units_key, key)) { if (PreferencesUtils.isKey(R.string.stats_units_key, key)) {