This commit is contained in:
Rodrigo Damazio
2010-11-01 02:05:42 -02:00
2 changed files with 46 additions and 4 deletions
@@ -27,6 +27,8 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.util.Locale;
@@ -63,6 +65,26 @@ public class StatusAnnouncerTask implements PeriodicTask {
*/
private boolean ready = false;
/**
* Whether we're allowed to speak right now.
*/
private boolean speechAllowed;
/**
* Listener which updates {@link #speechAllowed} when the phone state changes.
*/
private final PhoneStateListener phoneListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
speechAllowed = (state == TelephonyManager.CALL_STATE_IDLE);
if (!speechAllowed && tts.isSpeaking()) {
// Stop speaking if we are
tts.stop();
}
}
};
/**
* Constructs the announcer and start the TTS engine.
*/
@@ -116,6 +138,12 @@ public class StatusAnnouncerTask implements PeriodicTask {
return;
}
if (!speechAllowed) {
Log.i(MyTracksConstants.TAG,
"Not making announcement - not allowed at this time");
return;
}
if (service == null || service.getTripStatistics() == null) {
Log.e(MyTracksConstants.TAG, "StatusAnnouncer stats not initialized.");
return;
@@ -193,12 +221,23 @@ public class StatusAnnouncerTask implements PeriodicTask {
context.getString(speedLabel));
}
public void shutdown() {
tts.shutdown();
@Override
public void start() {
// Register ourselves as a listener so we won't speak during a call.
speechAllowed = true;
TelephonyManager telephony =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public void start() {
public void shutdown() {
// Stop listening to phone state
TelephonyManager telephony =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_NONE);
tts.shutdown();
}
public static int getVolumeStream() {