mirror of https://github.com/tasks/tasks
Logback-android, inject VoiceOutputAssistant
parent
d57a1a1acf
commit
f45cd0735d
@ -0,0 +1,18 @@
|
|||||||
|
<configuration>
|
||||||
|
<configuration>
|
||||||
|
<appender
|
||||||
|
name="LOGCAT"
|
||||||
|
class="ch.qos.logback.classic.android.LogcatAppender" >
|
||||||
|
<tagEncoder>
|
||||||
|
<pattern>%logger{0}</pattern>
|
||||||
|
</tagEncoder>
|
||||||
|
<encoder>
|
||||||
|
<pattern>[ %thread ] %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="WARN" >
|
||||||
|
<appender-ref ref="LOGCAT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
||||||
|
</configuration>
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 5.2 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 5.6 KiB |
@ -1,139 +0,0 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package com.todoroo.astrid.voice;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.media.AudioManager;
|
|
||||||
import android.speech.tts.TextToSpeech;
|
|
||||||
import android.speech.tts.TextToSpeech.OnInitListener;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import com.todoroo.andlib.service.ContextManager;
|
|
||||||
import com.todoroo.astrid.voice.VoiceOutputService.VoiceOutputAssistant;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Arne Jans
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class Api6VoiceOutputAssistant implements OnInitListener, VoiceOutputAssistant {
|
|
||||||
|
|
||||||
private static final int MY_DATA_CHECK_CODE = 2534;
|
|
||||||
private static final String TAG = "Astrid.VoiceOutputAssistant";
|
|
||||||
private final Context context;
|
|
||||||
private TextToSpeech mTts;
|
|
||||||
private boolean isTTSInitialized;
|
|
||||||
private boolean retryLastSpeak;
|
|
||||||
private String lastTextToSpeak;
|
|
||||||
private static final HashMap<String, String> ttsParams = new HashMap<>();
|
|
||||||
|
|
||||||
static {
|
|
||||||
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
|
|
||||||
String.valueOf(AudioManager.STREAM_NOTIFICATION));
|
|
||||||
}
|
|
||||||
|
|
||||||
Api6VoiceOutputAssistant() {
|
|
||||||
this.context = ContextManager.getContext().getApplicationContext();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void checkIsTTSInstalled() {
|
|
||||||
if (!isTTSInitialized && context instanceof Activity) {
|
|
||||||
Intent checkIntent = new Intent();
|
|
||||||
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
|
|
||||||
((Activity) context).startActivityForResult(checkIntent,
|
|
||||||
MY_DATA_CHECK_CODE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleActivityResult(int requestCode, int resultCode) {
|
|
||||||
if (requestCode == MY_DATA_CHECK_CODE) {
|
|
||||||
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
|
|
||||||
// success, create the TTS instance
|
|
||||||
initTTS();
|
|
||||||
} else {
|
|
||||||
// missing data, install it
|
|
||||||
Intent installIntent = new Intent();
|
|
||||||
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
|
|
||||||
context.startActivity(installIntent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initTTS() {
|
|
||||||
mTts = new TextToSpeech(context, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void queueSpeak(String textToSpeak) {
|
|
||||||
if (mTts != null && isTTSInitialized) {
|
|
||||||
mTts.speak(textToSpeak, TextToSpeech.QUEUE_ADD, ttsParams);
|
|
||||||
while (mTts.isSpeaking()) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(50);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
retryLastSpeak = true;
|
|
||||||
this.lastTextToSpeak = textToSpeak;
|
|
||||||
initTTS();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onInit(int status) {
|
|
||||||
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
|
|
||||||
if (status == TextToSpeech.SUCCESS && mTts != null) {
|
|
||||||
// Set preferred language to US english.
|
|
||||||
// Note that a language may not be available, and the result will indicate this.
|
|
||||||
int result = mTts.setLanguage(Locale.getDefault());
|
|
||||||
// Try this someday for some interesting results.
|
|
||||||
// int result mTts.setLanguage(Locale.FRANCE);
|
|
||||||
if (result == TextToSpeech.LANG_MISSING_DATA ||
|
|
||||||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
|
|
||||||
// Language data is missing or the language is not supported.
|
|
||||||
Log.e(TAG, "Language is not available.");
|
|
||||||
} else {
|
|
||||||
// Check the documentation for other possible result codes.
|
|
||||||
// For example, the language may be available for the locale,
|
|
||||||
// but not for the specified country and variant.
|
|
||||||
|
|
||||||
mTts.speak("", 0, null);
|
|
||||||
|
|
||||||
// The TTS engine has been successfully initialized.
|
|
||||||
isTTSInitialized = true;
|
|
||||||
// if this request came from queueSpeak, then speak it and reset the memento
|
|
||||||
if (retryLastSpeak && this.lastTextToSpeak != null) {
|
|
||||||
this.queueSpeak(this.lastTextToSpeak);
|
|
||||||
retryLastSpeak = false;
|
|
||||||
lastTextToSpeak = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Initialization failed.
|
|
||||||
Log.e(TAG, "Could not initialize TextToSpeech.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Has to be called in onDestroy of the activity that uses this instance of VoiceOutputAssistant.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
if (mTts != null && isTTSInitialized) {
|
|
||||||
mTts.shutdown();
|
|
||||||
mTts = null;
|
|
||||||
isTTSInitialized = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,118 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.todoroo.astrid.voice;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.media.AudioManager;
|
||||||
|
import android.speech.tts.TextToSpeech;
|
||||||
|
import android.speech.tts.TextToSpeech.OnInitListener;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.tasks.injection.ForApplication;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Arne Jans
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class VoiceOutputAssistant implements OnInitListener {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(VoiceOutputAssistant.class);
|
||||||
|
|
||||||
|
private final Context context;
|
||||||
|
|
||||||
|
private TextToSpeech mTts;
|
||||||
|
private boolean isTTSInitialized;
|
||||||
|
private String lastTextToSpeak;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public VoiceOutputAssistant(@ForApplication Context context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTTSInitialized() {
|
||||||
|
return isTTSInitialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initTTS() {
|
||||||
|
if(mTts == null) {
|
||||||
|
mTts = new TextToSpeech(context, this);
|
||||||
|
log.debug("Inititalized {}", mTts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void speak(String textToSpeak) {
|
||||||
|
if (mTts != null && isTTSInitialized) {
|
||||||
|
final String id = UUID.randomUUID().toString();
|
||||||
|
log.debug("{}: {} ({})", this, textToSpeak, id);
|
||||||
|
mTts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {
|
||||||
|
@Override
|
||||||
|
public void onUtteranceCompleted(String utteranceId) {
|
||||||
|
log.debug("{}: onUtteranceCompleted {}", utteranceId);
|
||||||
|
if(utteranceId.equals(id)) {
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mTts.speak(textToSpeak, TextToSpeech.QUEUE_ADD, new HashMap<String, String>() {{
|
||||||
|
put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
|
||||||
|
put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, id);
|
||||||
|
}});
|
||||||
|
} else {
|
||||||
|
lastTextToSpeak = textToSpeak;
|
||||||
|
initTTS();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onInit(int status) {
|
||||||
|
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
|
||||||
|
if (status == TextToSpeech.SUCCESS && mTts != null) {
|
||||||
|
// Set preferred language to US english.
|
||||||
|
// Note that a language may not be available, and the result will indicate this.
|
||||||
|
int result = mTts.setLanguage(Locale.getDefault());
|
||||||
|
// Try this someday for some interesting results.
|
||||||
|
// int result mTts.setLanguage(Locale.FRANCE);
|
||||||
|
if (result == TextToSpeech.LANG_MISSING_DATA) {
|
||||||
|
log.error("Language data missing");
|
||||||
|
} else if(result == TextToSpeech.LANG_NOT_SUPPORTED) {
|
||||||
|
log.error("Language not supported");
|
||||||
|
} else {
|
||||||
|
// Check the documentation for other possible result codes.
|
||||||
|
// For example, the language may be available for the locale,
|
||||||
|
// but not for the specified country and variant.
|
||||||
|
|
||||||
|
mTts.speak("", 0, null);
|
||||||
|
|
||||||
|
// The TTS engine has been successfully initialized.
|
||||||
|
isTTSInitialized = true;
|
||||||
|
// if this request came from speak, then speak it and reset the memento
|
||||||
|
if (lastTextToSpeak != null) {
|
||||||
|
speak(lastTextToSpeak);
|
||||||
|
lastTextToSpeak = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("Could not initialize TextToSpeech.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() {
|
||||||
|
if (mTts != null && isTTSInitialized) {
|
||||||
|
try {
|
||||||
|
mTts.shutdown();
|
||||||
|
log.debug("Shutdown {}", mTts);
|
||||||
|
mTts = null;
|
||||||
|
isTTSInitialized = false;
|
||||||
|
} catch(VerifyError e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,73 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2012 Todoroo Inc
|
|
||||||
*
|
|
||||||
* See the file "LICENSE" for the full license governing this code.
|
|
||||||
*/
|
|
||||||
package com.todoroo.astrid.voice;
|
|
||||||
|
|
||||||
|
|
||||||
import com.todoroo.andlib.utility.AndroidUtilities;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All API versions-friendly voice input / output.
|
|
||||||
* @author Tim Su <tim@todoroo.com>
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class VoiceOutputService {
|
|
||||||
|
|
||||||
private static final int MIN_TTS_VERSION = 6;
|
|
||||||
private static VoiceOutputAssistant outputAssistant;
|
|
||||||
|
|
||||||
// --- voice output
|
|
||||||
|
|
||||||
public interface VoiceOutputAssistant {
|
|
||||||
public void checkIsTTSInstalled();
|
|
||||||
|
|
||||||
public void handleActivityResult(int requestCode, int resultCode);
|
|
||||||
|
|
||||||
public void queueSpeak(String textToSpeak);
|
|
||||||
|
|
||||||
public void onDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class NullVoiceOutputAssistant implements VoiceOutputAssistant {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void checkIsTTSInstalled() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleActivityResult(int requestCode, int resultCode) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void queueSpeak(String textToSpeak) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static VoiceOutputAssistant getVoiceOutputInstance() {
|
|
||||||
if(AndroidUtilities.getSdkVersion() >= MIN_TTS_VERSION) {
|
|
||||||
if (outputAssistant == null) {
|
|
||||||
outputAssistant = new Api6VoiceOutputAssistant();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(outputAssistant == null) {
|
|
||||||
outputAssistant = new NullVoiceOutputAssistant();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return outputAssistant;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue