Skip to content

Commit

Permalink
Fix for the "OK, Google" problem http://stackoverflow.com/questions/3…
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Skachkov committed Mar 17, 2016
1 parent b7eae46 commit b09724c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ailib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {
mavenCentral()
}
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:support-v4:23.2.1'
compile 'com.google.code.gson:gson:2.3'
compile 'commons-io:commons-io:2.4'
testCompile "junit:junit:4.12"
Expand Down
5 changes: 1 addition & 4 deletions ailib/src/main/java/ai/api/AIDataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,13 @@ public AIResponse request(@NonNull final AIRequest request, @Nullable final Requ
}

final String queryData = gson.toJson(request);

Log.d(TAG, "Request json: " + queryData);

final String response = doTextRequest(config.getQuestionUrl(), queryData, additionalHeaders);

if (TextUtils.isEmpty(response)) {
throw new AIServiceException("Empty response from ai service. Please check configuration and Internet connection.");
}

Log.d(TAG, "Response json: " + response);
Log.d(TAG, "Response json: " + response.replaceAll("[\r\n]+", " "));

final AIResponse aiResponse = gson.fromJson(response, AIResponse.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ protected void initializeRecognizer() {
}

protected void clearRecognizer() {
Log.d(TAG, "clearRecognizer");
if (speechRecognizer != null) {
synchronized (speechRecognizerLock) {
if (speechRecognizer != null) {
Expand Down Expand Up @@ -175,18 +176,7 @@ public void startListening(final RequestExtras requestExtras) {

recognitionActive = true;

final Intent sttIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

final String language = config.getLanguage().replace('-', '_');

sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);
sttIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);

// WORKAROUND for https://code.google.com/p/android/issues/detail?id=75347
sttIntent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{});
final Intent sttIntent = createRecognitionIntent();

try {
wasReadyForSpeech = false;
Expand All @@ -202,6 +192,23 @@ public void startListening(final RequestExtras requestExtras) {
}
}

private Intent createRecognitionIntent() {
final Intent sttIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

final String language = config.getLanguage().replace('-', '_');

sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);
sttIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
sttIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());

// WORKAROUND for https://code.google.com/p/android/issues/detail?id=75347
sttIntent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{language});
return sttIntent;
}

@Override
public void stopListening() {
synchronized (speechRecognizerLock) {
Expand All @@ -224,6 +231,25 @@ public void cancel() {
}
}

private void restartRecognition(){
recognitionActive = false;

synchronized (speechRecognizerLock) {
try {
if (speechRecognizer != null) {
speechRecognizer.cancel();

final Intent intent = createRecognitionIntent();
wasReadyForSpeech = false;
speechRecognizer.startListening(intent);
recognitionActive = true;
}
} catch (Exception e) {
stopListening();
}
}
}

/**
* This method must be called from UI thread
*/
Expand Down Expand Up @@ -285,6 +311,8 @@ public void onEndOfSpeech() {
@Override
public void onError(final int error) {
if (error == SpeechRecognizer.ERROR_NO_MATCH && !wasReadyForSpeech) {
Log.d(TAG, "SpeechRecognizer.ERROR_NO_MATCH, restartRecognition()");
restartRecognition();
return;
}

Expand Down
2 changes: 1 addition & 1 deletion apiAISampleApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repositories {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.google.code.gson:gson:2.3'
compile 'commons-io:commons-io:2.4'
//compile 'ai.api:sdk:1.8.0@aar'
Expand Down

0 comments on commit b09724c

Please sign in to comment.