From f7f58141ded1e2091436124b85a9c067f8ab0cf8 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sun, 29 Sep 2024 16:56:34 +0200 Subject: [PATCH] Added settings for disabling idle functionality. --- .../services/TrackRecordingManager.java | 18 ++++++---- .../opentracks/settings/PreferencesUtils.java | 36 ++++++++++--------- src/main/res/values/settings.xml | 2 ++ 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java index d2fd761cf9..0ccc4616a7 100644 --- a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java +++ b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java @@ -170,8 +170,7 @@ synchronized boolean onNewTrackPoint(@NonNull TrackPoint trackPoint) { if (trackPoint.hasLocation() && lastStoredTrackPointWithLocation == null) { insertTrackPoint(trackPoint, true); - handler.removeCallbacks(ON_IDLE); - handler.postDelayed(ON_IDLE, idleDuration.toMillis()); + scheduleNewIdleTimeout(); return true; } @@ -199,16 +198,14 @@ synchronized boolean onNewTrackPoint(@NonNull TrackPoint trackPoint) { trackPoint.setType(TrackPoint.Type.SEGMENT_START_AUTOMATIC); insertTrackPoint(trackPoint, true); - handler.removeCallbacks(ON_IDLE); - handler.postDelayed(ON_IDLE, idleDuration.toMillis()); + scheduleNewIdleTimeout(); return true; } if (distanceToLastStoredTrackPoint.greaterOrEqualThan(recordingDistanceInterval)) { insertTrackPoint(trackPoint, false); - handler.removeCallbacks(ON_IDLE); - handler.postDelayed(ON_IDLE, idleDuration.toMillis()); + scheduleNewIdleTimeout(); return true; } @@ -218,6 +215,15 @@ synchronized boolean onNewTrackPoint(@NonNull TrackPoint trackPoint) { return false; } + private void scheduleNewIdleTimeout() { + if (idleDuration.isZero()) { + Log.d(TAG, "idle functionality is disabled"); + return; + } + handler.removeCallbacks(ON_IDLE); + handler.postDelayed(ON_IDLE, idleDuration.toMillis()); + } + TrackStatistics getTrackStatistics() { return trackStatisticsUpdater == null ? null : trackStatisticsUpdater.getTrackStatistics(); } diff --git a/src/main/java/de/dennisguse/opentracks/settings/PreferencesUtils.java b/src/main/java/de/dennisguse/opentracks/settings/PreferencesUtils.java index 28d10b8ddd..f999e9d31b 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/PreferencesUtils.java +++ b/src/main/java/de/dennisguse/opentracks/settings/PreferencesUtils.java @@ -344,17 +344,17 @@ public static Duration getVoiceAnnouncementFrequency() { } static String[] getVoiceAnnouncementFrequencyEntries() { - String[] values = resources.getStringArray(R.array.voice_announcement_frequency_values); - String[] options = new String[values.length]; - for (int i = 0; i < values.length; i++) { - if (resources.getString(R.string.announcement_off).equals(values[i])) { - options[i] = resources.getString(R.string.value_off); + String[] entryValues = resources.getStringArray(R.array.voice_announcement_frequency_values); + String[] entries = new String[entryValues.length]; + for (int i = 0; i < entryValues.length; i++) { + if (resources.getString(R.string.announcement_off).equals(entryValues[i])) { + entries[i] = resources.getString(R.string.value_off); } else { - int value = Integer.parseInt(values[i]); - options[i] = resources.getString(R.string.value_integer_minute, Duration.ofSeconds(value).toMinutes()); + int value = Integer.parseInt(entryValues[i]); + entries[i] = resources.getString(R.string.value_integer_minute, Duration.ofSeconds(value).toMinutes()); } } - return options; + return entries; } /** @@ -370,23 +370,23 @@ public static Distance getVoiceAnnouncementDistance() { * @return Result depends on getUnitSystem */ static String[] getVoiceAnnouncementDistanceEntries() { - String[] values = resources.getStringArray(R.array.voice_announcement_distance_values); - String[] options = new String[values.length]; + String[] entryValues = resources.getStringArray(R.array.voice_announcement_distance_values); + String[] entries = new String[entryValues.length]; UnitSystem unitSystem = getUnitSystem(); DistanceFormatter formatter = DistanceFormatter.Builder() .setDecimalCount(0) .setUnit(unitSystem) .build(resources); - for (int i = 0; i < values.length; i++) { - if (resources.getString(R.string.announcement_off).equals(values[i])) { - options[i] = resources.getString(R.string.value_off); + for (int i = 0; i < entryValues.length; i++) { + if (resources.getString(R.string.announcement_off).equals(entryValues[i])) { + entries[i] = resources.getString(R.string.value_off); } else { - Distance distance = Distance.one(unitSystem).multipliedBy(Double.parseDouble(values[i])); - options[i] = formatter.formatDistance(distance); + Distance distance = Distance.one(unitSystem).multipliedBy(Double.parseDouble(entryValues[i])); + entries[i] = formatter.formatDistance(distance); } } - return options; + return entries; } public static float getVoiceSpeedRate() { @@ -642,7 +642,9 @@ static String[] getIdleDurationEntries() { for (int i = 0; i < entryValues.length; i++) { int value = Integer.parseInt(entryValues[i]); - if (value == idleDurationDefault) { + if (resources.getString(R.string.announcement_off).equals(entryValues[i])) { + entries[i] = resources.getString(R.string.value_off); + } else if (value == idleDurationDefault) { entries[i] = resources.getString(R.string.value_int_seconds, value); } else { entries[i] = value < 60 ? resources.getString(R.string.value_integer_second, value) : resources.getString(R.string.value_integer_minute, value / 60); diff --git a/src/main/res/values/settings.xml b/src/main/res/values/settings.xml index a3276d42fd..58f4b63413 100644 --- a/src/main/res/values/settings.xml +++ b/src/main/res/values/settings.xml @@ -127,7 +127,9 @@ idleSpeedDuration 10 + 0 + @string/idle_duration_off 5 @string/idle_duration_default 15