RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS in Oreo
Asked Answered
F

1

14

In most Android devices, the RecognitionService will be supplied by Google's native 'Now/Assistant' application.

Up until Android Oreo, I was able to query the languages supported by the Google Recognizer with the following simple code:

final Intent vrIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);

// vrIntent.setPackage("com.google.android.googlequicksearchbox");

getContext().sendOrderedBroadcast(vrIntent, null, new BroadcastReceiver() {

    @Override
    public void onReceive(final Context context, final Intent intent) {

                // final Bundle bundle = intent.getExtras();
                final Bundle bundle = getResultExtras(true);

                if (bundle != null) {

                    if (bundle.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
                        Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");

                        final ArrayList<String> vrStringLocales = bundle.getStringArrayList(
                                RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);

                        Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES size: " + vrStringLocales.size());

                    } else {
                        Log.w("TAG", "onReceive: missing EXTRA_SUPPORTED_LANGUAGES");
                    }

                } else {
                    Log.w("TAG", "onReceive: Bundle null");
                }

}, null, 1234, null, null);

However, since 8.0+ the extra RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES is no longer contained in the response.

Before I attempt to file this as a bug, I wanted to firstly see if others could replicate - but also check if there has been an Ordered Broadcast behavioural change in API 26 I've somehow overlooked, which could be the cause of this.

Thanks in advance.

Feeze answered 29/1, 2018 at 11:21 Comment(19)
Have you tried using getVoiceDetailsIntent()?Budgie
@Budgie Thanks. I have - it just returns the shown intent of RecognizerIntent.ACTION_GET_LANGUAGE_DETAILSFeeze
Could you do simple debugging and verify, whether Broadcast is completed successfully by checking getResultCode() == Activity.RESULT_OK or if getResultData() contains any value at the beginning of receive() function?Ego
@R.Zagórski the result code matches the request code, in the above example 1234. The result data is null, but the documentation does state "This is often null". This behaviour is identical to pre-Oreo, which does contain the required language extra.Feeze
I've tested your code on Android Emulator with API 26 and I reach this Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");. compileSdkVersion 27 buildToolsVersion "27.0.3" defaultConfig { applicationId "it.versionestabile.stackover001" minSdkVersion 19 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } Do you say instead not reaching that code?Northwest
@Northwest thanks for testing. It doesn't reach that for me. Could you confirm you are running the most up to date version of Google Now/Assistant in the emulator please? It could be a versioning issue...Feeze
@Feeze that's my version: com.google.android.googlequicksearchbox, 7.2.29.21.x86Northwest
@Feeze PackageManager packageManager = getPackageManager(); for (PackageInfo packageInfo: packageManager.getInstalledPackages(0)) { if (packageInfo.packageName.contains("com.google.android.googlequicksearchbox")) Log.d("AAA", packageInfo.packageName + ", " + packageInfo.versionName); }Northwest
@Northwest thank you. Could you confirm the emulator config you are using please? If I can replicate, I'll let you know, so you can put your findings as an answer.Feeze
@Feeze Name: Nexus_5X_API_26_No_Proxy CPU/ABI: Google Play Intel Atom (x86) Path: C:\Users\***\.android\avd\Nexus_5X_API_26_No_Proxy.avd Target: google_apis_playstore [Google Play] (API level 26) Skin: nexus_5x SD Card: 100M hw.dPad: no runtime.network.speed: full hw.accelerometer: yes hw.device.name: Nexus 5X vm.heapSize: 256 skin.dynamic: yes hw.device.manufacturer: Google hw.gps: yes hw.initialOrientation: Portrait image.androidVersion.api: 26Northwest
@Feeze hw.audioInput: yes image.sysdir.1: system-images\android-26\google_apis_playstore\x86\ tag.id: google_apis_playstore showDeviceFrame: yes hw.camera.back: emulated hw.mainKeys: no AvdId: Nexus_5X_API_26_No_Proxy hw.camera.front: emulated hw.lcd.density: 420 avd.ini.displayname: Nexus 5X API 26 No Proxy hw.gpu.mode: auto hw.device.hash2: MD5:1be89bc42ec9644d4b77968b23474980 hw.ramSize: 1536 hw.trackBall: no PlayStore.enabled: true hw.battery: yes hw.cpu.ncore: 4 hw.sdCard: yes tag.display: Google Play runtime.network.latency: none hw.keyboard: yesNorthwest
@Feeze hw.sensors.proximity: yes disk.dataPartition.size: 800M hw.sensors.orientation: yes avd.ini.encoding: UTF-8 hw.gpu.enabled: yes and Android Studio 2.3.3Northwest
@Feeze github.com/shadowsheep1/googlenowpackageNorthwest
@Northwest really appreciate this. I'll come back to you when I've tested.Feeze
@Feeze you are welcome!Northwest
@Northwest I can replicate! Thank you. Delving deeper into my code, it's a threading issue in Oreo. When I solve the exact cause, I'll let you know so you can post it as an answer and I'll award you the bounty!Feeze
@Northwest Could you confirm that if you comment out vrIntent.setPackage("com.google.android.googlequicksearchbox"); it then fails?Feeze
@Feeze I confirm. Comment out vrIntent.setPackage("com.google.android.googlequicksearchbox"); it fails. I've tested also on a Mac OSX with Android Studio 3.0.1 and an emulator with API 27. Same behaviour. With the above line: OK. With the line commented out: KO.Northwest
Let us continue this discussion in chat.Feeze
N
15

So, I could't replicate, but further to the comments, if you don't set the package name

vrIntent.setPackage("com.google.android.googlequicksearchbox");

then it fails, otherwise all works fine to me.

That's the basic activity I've used to test it.

package it.versionestabile.stackover001;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;

import static java.security.AccessController.getContext;

/**
 * https://mcmap.net/q/817362/-recognizerintent-action_get_language_details-in-oreo
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Intent vrIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
        vrIntent.setPackage("com.google.android.googlequicksearchbox");

        PackageManager packageManager = getPackageManager();

        for (PackageInfo packageInfo: packageManager.getInstalledPackages(0)) {
            if (packageInfo.packageName.contains("com.google.android.googlequicksearchbox"))
                Log.d("AAA", packageInfo.packageName + ", "  + packageInfo.versionName);
        }

        this.sendOrderedBroadcast(vrIntent, null, new BroadcastReceiver() {

            @Override
            public void onReceive(final Context context, final Intent intent) {

                // final Bundle bundle = intent.getExtras();
                final Bundle bundle = getResultExtras(true);

                if (bundle != null) {

                    if (bundle.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
                        Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");

                        final ArrayList<String> vrStringLocales = bundle.getStringArrayList(
                                RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);

                        Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES size: " + vrStringLocales.size());

                    } else {
                        Log.w("TAG", "onReceive: missing EXTRA_SUPPORTED_LANGUAGES");
                    }

                } else {
                    Log.w("TAG", "onReceive: Bundle null");
                }
            }

            }, null, 1234, null, null);
    }
}

I've tested it both on Android Studio 2.3 and 3.0.1 and on emulator with API 26 and 27.

All works fine with the above code.

But if you comment out this line:

vrIntent.setPackage("com.google.android.googlequicksearchbox");

on Oreo it doesn't work.

And I still suggest to check the presence of Google Now with Package Manager in a way like this:

PackageManager packageManager = getPackageManager();

for (PackageInfo packageInfo: packageManager.getInstalledPackages(0)) {
    if (packageInfo.packageName.contains("com.google.android.googlequicksearchbox"))
        Log.d("AAA", packageInfo.packageName + ", "  + packageInfo.versionName);
// TODO - set a boolean value to discriminate the precence of google now
}

In order to decide if you have the right version of Google Now.

Hope it helps!

Northwest answered 6/2, 2018 at 22:45 Comment(4)
I'll be posting another question to try and work out why in Oreo you need to specify the package name... Thank you once again!Feeze
For comparison, how should it look like prior to OREO ? thanksFlense
@Flense prior to Oreo it works also without explicitly setting the package to the intent.Northwest
Best solution, I tested in Android 9 and it works. I have only added the line "setPackage("com.google.android.googlequicksearchbox")" to my Intent and it worked. ThanksHeteroecious

© 2022 - 2024 — McMap. All rights reserved.