altosdroid: Use single object to pass data to UI
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosVoice.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.AltosDroid;
20
21 import android.speech.tts.TextToSpeech;
22 import android.speech.tts.TextToSpeech.OnInitListener;
23
24 import org.altusmetrum.altoslib_5.*;
25
26 public class AltosVoice {
27
28         private TextToSpeech tts         = null;
29         private boolean      tts_enabled = false;
30
31         private IdleThread   idle_thread = null;
32
33         private AltosState   old_state   = null;
34
35         public AltosVoice(AltosDroid a) {
36
37                 tts = new TextToSpeech(a, new OnInitListener() {
38                         public void onInit(int status) {
39                                 if (status == TextToSpeech.SUCCESS) tts_enabled = true;
40                                 if (tts_enabled) {
41                                         idle_thread = new IdleThread();
42                                 }
43                         }
44                 });
45
46         }
47
48         public synchronized void speak(String s) {
49                 if (!tts_enabled) return;
50                 tts.speak(s, TextToSpeech.QUEUE_ADD, null);
51         }
52
53         public void stop() {
54                 if (tts != null) tts.shutdown();
55                 if (idle_thread != null) {
56                         idle_thread.interrupt();
57                         idle_thread = null;
58                 }
59         }
60
61         public void tell(AltosState state, AltosGreatCircle from_receiver) {
62                 if (!tts_enabled) return;
63
64                 boolean spoke = false;
65                 if (old_state == null || old_state.state != state.state) {
66                         if (state.state != AltosLib.ao_flight_stateless)
67                                 speak(state.state_name());
68                         if ((old_state == null || old_state.state <= AltosLib.ao_flight_boost) &&
69                             state.state > AltosLib.ao_flight_boost) {
70                                 if (state.max_speed() != AltosLib.MISSING)
71                                         speak(String.format("max speed: %d meters per second.", (int) (state.max_speed() + 0.5)));
72                                 spoke = true;
73                         } else if ((old_state == null || old_state.state < AltosLib.ao_flight_drogue) &&
74                                    state.state >= AltosLib.ao_flight_drogue) {
75                                 if (state.max_height() != AltosLib.MISSING)
76                                         speak(String.format("max height: %d meters.", (int) (state.max_height() + 0.5)));
77                                 spoke = true;
78                         }
79                 }
80                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
81                         if (state.gps_ready) {
82                                 speak("GPS ready");
83                                 spoke = true;
84                         } else if (old_state != null) {
85                                 speak("GPS lost");
86                                 spoke = true;
87                         }
88                 }
89                 old_state = state;
90                 if (idle_thread != null)
91                         idle_thread.notice(state, from_receiver, spoke);
92         }
93
94
95         class IdleThread extends Thread {
96                 boolean            started;
97                 private AltosState state;
98                 private AltosGreatCircle from_receiver;
99                 int                reported_landing;
100                 int                report_interval;
101                 long               report_time;
102
103                 public synchronized void report(boolean last) {
104                         if (state == null)
105                                 return;
106
107                         /* reset the landing count once we hear about a new flight */
108                         if (state.state < AltosLib.ao_flight_drogue)
109                                 reported_landing = 0;
110
111                         /* Shut up once the rocket is on the ground */
112                         if (reported_landing > 2) {
113                                 return;
114                         }
115
116                         /* If the rocket isn't on the pad, then report location */
117                         if ((AltosLib.ao_flight_drogue <= state.state &&
118                               state.state < AltosLib.ao_flight_landed) ||
119                              state.state == AltosLib.ao_flight_stateless)
120                         {
121                                 AltosGreatCircle        position;
122
123                                 if (from_receiver != null)
124                                         position = from_receiver;
125                                 else
126                                         position = state.from_pad;
127
128                                 if (position != null) {
129                                         speak(String.format("Height %d, bearing %s %d, elevation %d, range %d.\n",
130                                                             (int) (state.height() + 0.5),
131                                                             position.bearing_words(
132                                                                     AltosGreatCircle.BEARING_VOICE),
133                                                             (int) (position.bearing + 0.5),
134                                                             (int) (position.elevation + 0.5),
135                                                             (int) (position.range + 0.5)));
136                                 }
137                         } else if (state.state > AltosLib.ao_flight_pad) {
138                                 if (state.height() != AltosLib.MISSING)
139                                         speak(String.format("%d meters", (int) (state.height() + 0.5)));
140                         } else {
141                                 reported_landing = 0;
142                         }
143
144                         /* If the rocket is coming down, check to see if it has landed;
145                          * either we've got a landed report or we haven't heard from it in
146                          * a long time
147                          */
148                         if (state.state >= AltosLib.ao_flight_drogue &&
149                             (last ||
150                              System.currentTimeMillis() - state.received_time >= 15000 ||
151                              state.state == AltosLib.ao_flight_landed))
152                         {
153                                 if (Math.abs(state.speed()) < 20 && state.height() < 100)
154                                         speak("rocket landed safely");
155                                 else
156                                         speak("rocket may have crashed");
157                                 if (state.from_pad != null)
158                                         speak(String.format("Bearing %d degrees, range %d meters.",
159                                                             (int) (state.from_pad.bearing + 0.5),
160                                                             (int) (state.from_pad.distance + 0.5)));
161                                 ++reported_landing;
162                         }
163                 }
164
165                 long now () {
166                         return System.currentTimeMillis();
167                 }
168
169                 void set_report_time() {
170                         report_time = now() + report_interval;
171                 }
172
173                 public void run () {
174                         try {
175                                 for (;;) {
176                                         set_report_time();
177                                         for (;;) {
178                                                 synchronized (this) {
179                                                         long sleep_time = report_time - now();
180                                                         if (sleep_time <= 0)
181                                                                 break;
182                                                         wait(sleep_time);
183                                                 }
184                                         }
185                                         report(false);
186                                 }
187                         } catch (InterruptedException ie) {
188                         }
189                 }
190
191                 public synchronized void notice(AltosState new_state, AltosGreatCircle new_from_receiver, boolean spoken) {
192                         AltosState old_state = state;
193                         state = new_state;
194                         from_receiver = new_from_receiver;
195                         if (!started && state.state > AltosLib.ao_flight_pad) {
196                                 started = true;
197                                 start();
198                         }
199
200                         if (state.state < AltosLib.ao_flight_drogue)
201                                 report_interval = 10000;
202                         else
203                                 report_interval = 20000;
204                         if (old_state != null && old_state.state != state.state) {
205                                 report_time = now();
206                                 this.notify();
207                         } else if (spoken)
208                                 set_report_time();
209                 }
210
211                 public IdleThread() {
212                         state = null;
213                         reported_landing = 0;
214                         report_interval = 10000;
215                 }
216         }
217
218 }