Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Added Speed Anouncement #305

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,36 @@ public void announceMotivation(){
Spannable announcement = VoiceAnnouncementUtils.getMotivationalAnnouncements();
tts.speak(announcement, TextToSpeech.QUEUE_FLUSH, null, "not used");
}
public void announceMotivationForIncreasedSpeed(){
synchronized (this) {
if (!ttsReady) {
ttsReady = ttsInitStatus == TextToSpeech.SUCCESS;
if (ttsReady) {
onTtsReady();
}
}
}

if (Arrays.asList(AudioManager.MODE_IN_CALL, AudioManager.MODE_IN_COMMUNICATION)
.contains(audioManager.getMode())) {
Log.i(TAG, "Announcement is not allowed at this time.");
return;
}

if (!ttsReady) {
if (ttsFallback == null) {
Log.w(TAG, "MediaPlayer for ttsFallback was not created.");
} else {
Log.i(TAG, "TTS not ready/available, just generating a tone.");
ttsFallback.seekTo(0);
ttsFallback.start();
}
return;
}

Spannable announcement = VoiceAnnouncementUtils.getMotivationalAnnouncementsForSpeedIncreased();
tts.speak(announcement, TextToSpeech.QUEUE_FLUSH, null, "not used");
}

public void announce(@NonNull Track track) {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Speed;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.services.TrackRecordingService;
import de.dennisguse.opentracks.settings.PreferencesUtils;
Expand Down Expand Up @@ -77,6 +78,12 @@ void update(@Nullable TrackStatistics trackStatistics) {
updateNextDuration();
updateNextTaskDistance();
}
boolean isSignificantSpeedIncreased(Speed max, Speed avg) {
// Define your criteria for a significant speed change (e.g., double or reduce by a certain percentage)
double speedChangeThreshold = 0.5; // Example: 50%

return Math.abs(max.toMPH() - avg.toMPH()) / max.toMPH() >= speedChangeThreshold;
}

public void update(@NonNull Context context, @NonNull Track track) {
if (voiceAnnouncement == null) {
Expand All @@ -92,18 +99,43 @@ public void update(@NonNull Context context, @NonNull Track track) {
return;
}



boolean announce = false;
boolean announceForSpeed =false;
boolean finalannounce=false;
this.trackStatistics = track.getTrackStatistics();

if (trackStatistics.getTotalDistance().greaterThan(nextTotalDistance)) {
updateNextTaskDistance();
announce = true;
announceForSpeed=true;
}

if (!trackStatistics.getTotalTime().minus(nextTotalTime).isNegative()) {
if (!trackStatistics.getTotalTime().minus(nextTotalTime).isNegative() && announceForSpeed) {
updateNextDuration();
announce = true;
announceForSpeed=true;
}

Speed maxSpeed = track.getTrackStatistics().getMaxSpeed();
Speed avgSpeed =track.getTrackStatistics().getAverageSpeed();
Speed current=Speed.of(trackStatistics.getTotalDistance(),trackStatistics.getTotalTime());
if (isSignificantSpeedIncreased(maxSpeed, avgSpeed) && announceForSpeed) {
// Make a motivational announcement
finalannounce = true;

}

if (finalannounce) {

voiceAnnouncement.announceMotivationForIncreasedSpeed();
}





if (announce) {
Random random = new Random();
float p = random.nextFloat();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ static Spannable getMotivationalAnnouncements(){
SpannableStringBuilder motivator = new SpannableStringBuilder();
return (motivator.append(ma.motivations[i]));
}
static Spannable getMotivationalAnnouncementsForSpeedIncreased(){
MotivationalAnnouncements ma = new MotivationalAnnouncements();

SpannableStringBuilder motivator = new SpannableStringBuilder();
return (motivator.append(ma.speedIncreased_motivations));
}

private static void appendDuration(@NonNull Context context, @NonNull SpannableStringBuilder builder, @NonNull Duration duration) {
int hours = (int) (duration.toHours());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class MotivationalAnnouncements {
public String []motivations = {"You are doing great","Kudos","Go Hard or Go Home","Yippee","Faster if you can","Its okay,You can do this","I believe in you",
"You're the best sportsperson I have ever seen","Keep going"};

public String speedIncreased_motivations = "Hurray you have doubled up your speed";



Expand Down