Skip to content

Commit

Permalink
Merge pull request #8 from HWJFish/65-settings-voice-announcements-an…
Browse files Browse the repository at this point in the history
…nounce-avg-speed-at-end-of-each-recording

65 settings voice announcements announce avg speed at end of each recording
  • Loading branch information
HWJFish authored Apr 9, 2024
2 parents 1a528fb + 20cb8c0 commit 37e96b7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ public void announceAfterRecording(@NonNull Track track) {
return;
}
// add other check with and here
if (!PreferencesUtils.shouldVoiceAnnounceMaxSpeedRecording() && !PreferencesUtils.shouldVoiceAnnounceMaxSlope() && !PreferencesUtils.shouldVoiceAnnounceAveragesloperecording() ) {

if (!PreferencesUtils.shouldVoiceAnnounceMaxSpeedRecording() && !PreferencesUtils.shouldVoiceAnnounceMaxSlope() && !PreferencesUtils.shouldVoiceAnnounceAveragesloperecording()
&& !PreferencesUtils.shouldVoiceAnnounceAverageSpeedRecording()) {
return;
}
voiceAnnouncement.announce(VoiceAnnouncementUtils.createAfterRecording(context,track.getTrackStatistics(),PreferencesUtils.getUnitSystem()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import static de.dennisguse.opentracks.settings.PreferencesUtils.shouldVoiceAnnounceRunAverageSpeed;
import static de.dennisguse.opentracks.settings.PreferencesUtils.shouldVoiceAnnounceMaxSlope;
import static de.dennisguse.opentracks.settings.PreferencesUtils.shouldVoiceAnnounceAveragesloperecording;

import static de.dennisguse.opentracks.settings.PreferencesUtils.shouldVoiceAnnounceMaxSpeedRecording;
import static de.dennisguse.opentracks.settings.PreferencesUtils.shouldVoiceAnnounceAverageSpeedRecording;

import android.content.Context;
import android.icu.text.MessageFormat;
Expand All @@ -38,17 +38,19 @@ class VoiceAnnouncementUtils {

private VoiceAnnouncementUtils() {
}

static double calculateMaxSlope() {

// This method should return the calculated maximum slope.
return 0.0;
return 0.0;
}
static double CalculateAverageSlope(){

static double CalculateAverageSlope() {
// This is dummy methods to fetch or calculate the average slope.
return 10.0;
}


static Spannable createIdle(Context context) {
return new SpannableStringBuilder()
.append(context.getString(R.string.voiceIdle));
Expand All @@ -57,6 +59,7 @@ static Spannable createIdle(Context context) {
static Spannable createAfterRecording(Context context, TrackStatistics trackStatistics, UnitSystem unitSystem) {
SpannableStringBuilder builder = new SpannableStringBuilder();
Speed maxSpeed = trackStatistics.getMaxSpeed();
Speed avgSpeed = trackStatistics.getAverageSpeed();

int perUnitStringId;
int distanceId;
Expand Down Expand Up @@ -92,7 +95,7 @@ static Spannable createAfterRecording(Context context, TrackStatistics trackStat
if (shouldVoiceAnnounceMaxSpeedRecording()) {
double speedInUnit = maxSpeed.to(unitSystem);
builder.append(" ")
.append(context.getString(R.string.speed));
.append(context.getString(R.string.stats_max_speed));
String template = context.getResources().getString(speedId);
appendDecimalUnit(builder, MessageFormat.format(template, Map.of("n", speedInUnit)), speedInUnit, 1, unitSpeedTTS);
builder.append(".");
Expand All @@ -102,22 +105,34 @@ static Spannable createAfterRecording(Context context, TrackStatistics trackStat
double maxSlope = calculateMaxSlope(); // Calculate the maximum slope based on elevation data
if (!Double.isNaN(maxSlope)) {
builder.append(" ")
.append(context.getString(R.string.settings_announcements_max_slope))
.append(": ")
.append(String.format("%.2f%%", maxSlope)) // Format the slope value
.append(".");
.append(context.getString(R.string.settings_announcements_max_slope))
.append(": ")
.append(String.format("%.2f%%", maxSlope)) // Format the slope value
.append(".");
}
}
if (shouldVoiceAnnounceAveragesloperecording()) {
double avgSlope = CalculateAverageSlope();
if (!Double.isNaN(avgSlope)) {
builder.append(" ")
.append("Average slope")
.append(": ")
.append(String.format("%.2f%%", avgSlope))
.append(".");
}

if (shouldVoiceAnnounceAverageSpeedRecording()) {

double speedInUnit = avgSpeed.to(unitSystem);
builder.append(" ")
.append(context.getString(R.string.speed));
String template = context.getResources().getString(speedId);
appendDecimalUnit(builder, MessageFormat.format(template, Map.of("n", speedInUnit)), speedInUnit, 1, unitSpeedTTS);
builder.append(".");
}


if (shouldVoiceAnnounceAveragesloperecording()) {
double avgSlope = CalculateAverageSlope();
if (!Double.isNaN(avgSlope)) {
builder.append(" ")
.append("Average slope")
.append(": ")
.append(String.format("%.2f%%", avgSlope))
.append(".");
}
}
return builder;
}

Expand Down Expand Up @@ -147,21 +162,20 @@ static Spannable createRunStatistics(Context context, TrackStatistics trackStati
}



//Announce Average Speed
if (shouldVoiceAnnounceRunAverageSpeed()) {
double speedInUnit = averageMovingSpeed.to(unitSystem);
builder.append(" ")
.append(context.getString(R.string.speed));
String template = context.getResources().getString(speedId);
appendDecimalUnit(builder, MessageFormat.format(template, Map.of("n", speedInUnit)), speedInUnit, 1, unitSpeedTTS);
builder.append(".");
}
if (shouldVoiceAnnounceRunAverageSpeed()) {
double speedInUnit = averageMovingSpeed.to(unitSystem);
builder.append(" ")
.append(context.getString(R.string.speed));
String template = context.getResources().getString(speedId);
appendDecimalUnit(builder, MessageFormat.format(template, Map.of("n", speedInUnit)), speedInUnit, 1, unitSpeedTTS);
builder.append(".");
}

if (shouldVoiceAnnounceMaxSpeedRun()) {
double speedInUnit = maxSpeed.to(unitSystem);
builder.append(" ")
.append(context.getString(R.string.settings_announcements_max_speed_run));
.append(context.getString(R.string.stats_max_speed));
String template = context.getResources().getString(speedId);
appendDecimalUnit(builder, MessageFormat.format(template, Map.of("n", speedInUnit)), speedInUnit, 1, unitSpeedTTS);
builder.append(".");
Expand Down Expand Up @@ -287,7 +301,6 @@ static Spannable createStatistics(Context context, TrackStatistics trackStatisti
appendCardinal(builder, context.getString(R.string.sensor_state_heart_rate_value, currentHeartRate), currentHeartRate);
builder.append(".");
}



return builder;
Expand Down Expand Up @@ -315,7 +328,7 @@ private static void appendDuration(@NonNull Context context, @NonNull SpannableS
/**
* Speaks as: 98.14 [UNIT] - ninety eight point one four [UNIT with correct plural form]
*
* @param number The number to speak
* @param number The number to speak
* @param precision The number of decimal places to announce
*/
private static void appendDecimalUnit(@NonNull SpannableStringBuilder builder, @NonNull String localizedText, double number, int precision, @NonNull String unit) {
Expand All @@ -327,7 +340,7 @@ private static void appendDecimalUnit(@NonNull SpannableStringBuilder builder, @
long integerPart = (long) roundedNumber;

if (precision == 0 || (roundedNumber - integerPart) == 0) {
measureBuilder.setNumber((long)number);
measureBuilder.setNumber((long) number);
} else {
// Extract the decimal part
String fractionalPart = String.format("%." + precision + "f", (roundedNumber - integerPart)).substring(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,14 @@ public static void setVoiceAnnounceAveragesloperecording(boolean value) {

}

public static boolean shouldVoiceAnnounceAverageSpeedRecording() {
return getBoolean(R.string.voice_announce_average_speed_recording_key, true);
}
@VisibleForTesting
public static void setVoiceAnnounceAverageSpeedRecording(boolean value) {
setBoolean(R.string.voice_announce_average_speed_recording_key, value);
}

public static Distance getRecordingDistanceInterval() {
return Distance.of(getInt(R.string.recording_distance_interval_key, getRecordingDistanceIntervalDefaultInternal()));
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/res/values/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@
<string name="voice_announce_max_speed_recording_key">voiceAnnounceMaxSpeedRecording</string>
<bool name="voice_announce_max_speed_recording_default" translatable="false">true</bool>


<string name="voice_announce_average_speed_recording_key">voiceAnnounceAverageSlopeRecording</string>
<bool name="voice_announce_average_speed_recording_default" translatable="false">true</bool>

<string name="voice_announce_average_slope_recording_key">voiceAnnounceAverageSlopeRecording</string>
<bool name="voice_announce_average_slope_recording_default" translatable="false">true</bool>

Expand All @@ -279,6 +283,7 @@
<bool name="voice_announce_max_speed_run_default" translatable="false">true</bool>



<!-- See TrackFileFormat -->
<string name="export_trackfileformat_key" translatable="false">exportTrackFileFormat</string>
<string name="export_trackfileformat_default" translatable="false">KMZ_WITH_TRACKDETAIL_AND_SENSORDATA_AND_PICTURES</string>
Expand Down
3 changes: 3 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ limitations under the License.

<!-- Recording Related Data -->
<string name="settings_announcements_max_speed_recording">Max speed/recording</string>

<string name="settings_announcements_average_speed_recording">Average speed/recording</string>

<string name="settings_announcements_max_slope">Max slope</string>
<string name="settings_announcements_average_slope_recording">Average slope</string>

Expand Down
5 changes: 5 additions & 0 deletions src/main/res/xml/settings_announcements.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
android:key="@string/voice_announce_max_speed_recording_key"
android:title="@string/settings_announcements_max_speed_recording" />

<SwitchPreference
android:defaultValue="@bool/voice_announce_max_speed_recording_default"
android:title="@string/settings_announcements_average_speed_recording"
app:key="@string/voice_announce_average_speed_recording_key" />

<SwitchPreferenceCompat
android:defaultValue="@bool/voice_announce_max_slope_default"
android:key="@string/voice_announce_max_slope_key"
Expand Down

0 comments on commit 37e96b7

Please sign in to comment.