Skip to content

Commit

Permalink
feat: Android 11 support and enhanced getting list of supported langu…
Browse files Browse the repository at this point in the history
…ages
  • Loading branch information
gotev committed Nov 3, 2020
1 parent 2d34330 commit ddfce0c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ public void onClick(DialogInterface dialogInterface, int i) {
.create()
.show();
}

@Override
public void onNotSupported(UnsupportedReason reason) {
switch (reason) {
case GOOGLE_APP_NOT_FOUND:
showSpeechNotSupportedDialog();
break;

case EMPTY_SUPPORTED_LANGUAGES:
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.set_stt_langs)
.setMessage(R.string.no_langs)
.setPositiveButton("OK", null)
.show();
break;
}
}
});
}

Expand Down
1 change: 1 addition & 0 deletions examples/demoapp/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<string name="no">No</string>
<string name="set_stt_langs">Set Speech To Text Language</string>
<string name="set_tts_voices">Set Text To Speech Voice</string>
<string name="no_langs">No languages are currently available on this device</string>
<string name="no_tts_voices">No supported voices or you\'re running on Android API &lt; 23, which does not support querying for voices.</string>
</resources>
4 changes: 2 additions & 2 deletions manifest.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ ext {
library_licenses = ["Apache-2.0"]
library_licenses_url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
library_project_group = 'net.gotev'
library_version = '1.5.0'
library_version = '1.6.0'
version_code = 1
min_sdk = 18
target_sdk = 28
target_sdk = 30
demo_app_id = 'net.gotev.speechdemo'

// Gradle classpath dependencies versions
Expand Down
8 changes: 8 additions & 0 deletions speech/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<!-- Android 11 queries declaration -->
<queries>
<package android:name="com.google.android.googlequicksearchbox" />
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>

</manifest>
48 changes: 41 additions & 7 deletions speech/src/main/java/net/gotev/speech/Speech.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.Voice;
Expand All @@ -15,10 +18,7 @@
import net.gotev.speech.engine.TextToSpeechEngine;
import net.gotev.speech.ui.SpeechProgressView;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.*;

/**
* Helper class to easily work with Android speech recognition.
Expand All @@ -28,6 +28,7 @@
public class Speech {

private static Speech instance = null;
protected static String GOOGLE_APP_PACKAGE = "com.google.android.googlequicksearchbox";

private Context mContext;

Expand Down Expand Up @@ -334,18 +335,51 @@ public Speech setAudioStream(final int audioStream) {
return this;
}

private boolean isGoogleAppInstalled() {
PackageManager packageManager = mContext.getPackageManager();

for (PackageInfo packageInfo: packageManager.getInstalledPackages(0)) {
if (packageInfo.packageName.contains(GOOGLE_APP_PACKAGE)) {
return true;
}
}

return false;
}

/**
* Gets the list of the supported speech to text languages on this device
* @param listener listner which will receive the results
*/
public void getSupportedSpeechToTextLanguages(SupportedLanguagesListener listener) {
if (!isGoogleAppInstalled()) {
listener.onNotSupported(UnsupportedReason.GOOGLE_APP_NOT_FOUND);
return;
}

Intent intent = RecognizerIntent.getVoiceDetailsIntent(mContext);

if (intent == null) {
intent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
intent.setPackage(GOOGLE_APP_PACKAGE);
}

mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
List<String> languages = getResultExtras(true).getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
Collections.sort(languages);
listener.onSupportedLanguages(languages);
Bundle extras = getResultExtras(true);

if (extras != null && extras.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
List<String> languages = extras.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
if (languages == null || languages.isEmpty()) {
listener.onNotSupported(UnsupportedReason.EMPTY_SUPPORTED_LANGUAGES);
} else {
Collections.sort(languages);
listener.onSupportedLanguages(languages);
}
} else {
listener.onNotSupported(UnsupportedReason.EMPTY_SUPPORTED_LANGUAGES);
}
}
}, null, Activity.RESULT_OK, null, null);
}
Expand Down
2 changes: 1 addition & 1 deletion speech/src/main/java/net/gotev/speech/SpeechUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ private SpeechUtil() {}
*/
public static void redirectUserToGoogleAppOnPlayStore(Context context) {
context.startActivity(new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("market://details?id=com.google.android.googlequicksearchbox")));
.setData(Uri.parse("market://details?id=" + Speech.GOOGLE_APP_PACKAGE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

public interface SupportedLanguagesListener {
void onSupportedLanguages(List<String> supportedLanguages);
void onNotSupported(UnsupportedReason reason);
}
6 changes: 6 additions & 0 deletions speech/src/main/java/net/gotev/speech/UnsupportedReason.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.gotev.speech;

public enum UnsupportedReason {
GOOGLE_APP_NOT_FOUND,
EMPTY_SUPPORTED_LANGUAGES
}

0 comments on commit ddfce0c

Please sign in to comment.