diff --git a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java index a16c42ed0..7ee7c7efa 100644 --- a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java +++ b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java @@ -24,7 +24,6 @@ import android.os.Handler; import android.os.Looper; import android.os.PowerManager.WakeLock; import android.util.Log; -import android.util.Pair; import androidx.annotation.Nullable; 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.TrackPoint; 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.handlers.TrackPointCreator; import de.dennisguse.opentracks.settings.PreferencesUtils; @@ -216,7 +214,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator. wakeLock = SystemUtils.acquireWakeLock(this, wakeLock); 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() { @@ -240,7 +238,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator. void stopSensors() { trackPointCreator.stop(); stopForeground(true); - notificationManager.cancelNotification(); + notificationManager.stop(); wakeLock = SystemUtils.releaseWakeLock(wakeLock); gpsStatusObservable.postValue(STATUS_GPS_DEFAULT); } @@ -284,7 +282,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator. Log.e(TAG, e.getMessage() + " " + writer); return; } - notificationManager.updateContent(getString(gpsStatusValue.message)); + notificationManager.updateGPSStatus(getString(gpsStatusValue.message)); gpsStatusObservable.postValue(gpsStatusValue); } diff --git a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingServiceNotificationManager.java b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingServiceNotificationManager.java index 6e767dcb2..c397cb28c 100644 --- a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingServiceNotificationManager.java +++ b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingServiceNotificationManager.java @@ -60,22 +60,35 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh .setSmallIcon(R.drawable.ic_logo_color_24dp); } - void stop() { - cancelNotification(); - } - @VisibleForTesting TrackRecordingServiceNotificationManager(NotificationManager notificationManager, NotificationCompat.Builder notificationBuilder) { this.notificationManager = notificationManager; 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); 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); DistanceFormatter formatter = DistanceFormatter.Builder() @@ -85,7 +98,7 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh if (trackPoint.hasHorizontalAccuracy()) { formattedAccuracy = formatter.formatDistance(trackPoint.getHorizontalAccuracy()); - boolean currentLocationWasAccurate = trackPoint.getHorizontalAccuracy().lessThan(recordingGpsAccuracy); + boolean currentLocationWasAccurate = trackPoint.getHorizontalAccuracy().lessThan(thresholdHorizontalAccuracy); boolean shouldAlert = !currentLocationWasAccurate && previousLocationWasAccurate; notificationBuilder.setOnlyAlertOnce(!shouldAlert); previousLocationWasAccurate = currentLocationWasAccurate; @@ -100,31 +113,10 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh notificationBuilder.setOnlyAlertOnce(true); } - Notification setGPSonlyStarted(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); - - updateContent(context.getString(R.string.gps_starting)); - - notificationBuilder.setContentIntent(pendingIntent); - updateNotification(); - - return getNotification(); - } - - void cancelNotification() { + void stop() { notificationManager.cancel(NOTIFICATION_ID); } - void setUnitSystem(UnitSystem unitSystem) { - this.unitSystem = unitSystem; - } - private void updateNotification() { notificationManager.notify(NOTIFICATION_ID, getNotification()); } @@ -133,6 +125,11 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh return notificationBuilder.build(); } + @VisibleForTesting + void setUnitSystem(UnitSystem unitSystem) { + this.unitSystem = unitSystem; + } + @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (PreferencesUtils.isKey(R.string.stats_units_key, key)) {