From fd4bad0ff2365966ab192da9fb93c04fbbaf4b66 Mon Sep 17 00:00:00 2001 From: SivaNagireddy97 <84583182+SivaNagireddy97@users.noreply.github.com> Date: Mon, 27 Nov 2023 12:35:42 -0500 Subject: [PATCH] Implementing End of Workout Announcement At the End of the workout, the user will be announced a motivation message. --- .../announcement/VoiceAnnouncement.java | 30 +++++++++++++++++++ .../VoiceAnnouncementManager.java | 2 +- .../announcement/VoiceAnnouncementUtils.java | 7 +++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncement.java b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncement.java index 34b8c4991..211c1aa6e 100644 --- a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncement.java +++ b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncement.java @@ -142,6 +142,36 @@ public void start() { } } + public void announceatTheEndOfTrack(){ + 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.atTheEndAnnounce(); + tts.speak(announcement, TextToSpeech.QUEUE_FLUSH, null, "not used"); + } public void announceMotivation(){ synchronized (this) { if (!ttsReady) { diff --git a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementManager.java b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementManager.java index 5bd10d7b9..8c1878055 100644 --- a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementManager.java +++ b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementManager.java @@ -104,7 +104,6 @@ public void update(@NonNull Context context, @NonNull Track track) { updateNextDuration(); announce = true; } - if (announce) { Random random = new Random(); float p = random.nextFloat(); @@ -120,6 +119,7 @@ public void update(@NonNull Context context, @NonNull Track track) { public void stop() { + voiceAnnouncement.announceatTheEndOfTrack(); if (voiceAnnouncement != null) { voiceAnnouncement.stop(); voiceAnnouncement = null; diff --git a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementUtils.java b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementUtils.java index 5a5835c2e..e5a146fbf 100644 --- a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementUtils.java +++ b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementUtils.java @@ -167,6 +167,13 @@ static Spannable getAnnouncement(Context context, TrackStatistics trackStatistic return builder; } + static Spannable atTheEndAnnounce(){ + String ma="Your session as ended.Remember! Consistency is the key to success"; + SpannableStringBuilder motivator = new SpannableStringBuilder(); + return (motivator.append(ma)); + } + + static Spannable getMotivationalAnnouncements(){ MotivationalAnnouncements ma = new MotivationalAnnouncements(); Random random = new Random();