Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect installed languages for offline recognition

It is possible to determine via code which language packages are currently installed on a device? Tried this:

    Intent detailsIntent =  new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
    sendOrderedBroadcast(detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);

 

    public class LanguageDetailsChecker extends BroadcastReceiver  {

    private List<String> supportedLanguages;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle results = getResultExtras(true);
        if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
        {
            supportedLanguages =results.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
            Log.d("TAG","languages: " + supportedLanguages);
        }
    }
}

However, the output shows me tons of languages, while I only have en_UK, en_US and es_ES installed. Any idea?

like image 539
JesusS Avatar asked Jul 23 '13 07:07

JesusS


1 Answers

If you have root(sorry), you can do it this way:

public static boolean isOfflineDictionaryPresent(String language) {
    if (locale == null) locale = Locale.US;
    String dir = "/system/usr/srec/config/" +
            language.replace('_', '.').toLowerCase();
    if ((new File(dir)).isDirectory()) return true;
    return false;
}

This was ripped from the Android 4.2.2 Recognizer.java source and modified:

  • returns a simple boolean instead of the dictionary directory
  • takes a String input(ex. "en_US") instead of a Locale

I'd get the full list just as you are, and loop through them to check which ones are available offline. I've checked the /system/usr/srec/config/ folder on two devices, and they both match the dictionaries I have installed.

Of course, the down side is that it only works for root, so I'm not sure how helpful this will be to you in the end. I'm really not sure what to say for non-root, I can't find anything.


Edit: Out of curiosity, though, what does EXTRA_SUPPORTED_LANGUAGES contain if you are offline? If it returns correctly, you might just have to fake out the network manager.

like image 122
Geobits Avatar answered Nov 07 '22 13:11

Geobits