Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Dec 05, 2017 14:06

Some tips on how to use Google Assistant without voice injection.

Start Google Assistant

Special Key

Android has a special key code for starting Assistant applications: 219

driver.pressKeyCode(219);

This will start the default Assistant application.

Activity

It is possible to start the Google Assistant application implicitly with it's activity.

Map<String, Object> params = new HashMap<>();
params.put("package", "com.google.android.googlequicksearchbox");
params.put("activity", "com.google.android.apps.gsa.staticplugins.opa.EnterOpaActivityFromDeeplink");
params.put("action", "android.intent.action.VOICE_COMMAND");
driver.executeScript("mobile:activity:open", params);

This will start Google Assistant.

Interacting with Google Assistant

Queries

Although Google Assistant activity doesn't support queries, it is possible to get a similar result with web search. You can start Google Web Search with a text query and get the same result as with Google Assistant voice command.

Map<String, Object> params = new HashMap<>();
params.put("package", "com.google.android.googlequicksearchbox");
params.put("activity", "com.google.android.googlequicksearchbox.SearchActivity");
params.put("action", "android.intent.action.WEB_SEARCH");
params.put("arguments", "-e query send-email-hi-to-john");
driver.executeScript("mobile:activity:open", params);

This command executes the query "send email hi to john". It will open a screen that asks to confirm sending email "Hi" to John.

You can confirm and proceed by clicking the arrow button with the following id: com.google.android.googlequicksearchbox:id/execute_button_highlight

driver.findElementByXPath("//*[@resource-id=\"com.google.android.googlequicksearchbox:id/execute_button_highlight\"]").click();

Talk to Google Assistant

Turn on Keyboard Input

Google Assistant does support text input. If you want to interact with it by typing, you should enable it in Google Assistant settings.

Go to Google Assistant settings → Phone → Preferred input → Keyboard

Or you can press the keyboard icon inside Google Assistant.

Talk to Google Assistant

Now when keyboard input is on, you can interact with Google Assistant by typing sentences to it's text field.

driver.findElementByXPath("//*[@resource-id=\"com.google.android.googlequicksearchbox:id/input_text\"]").sendKeys("talk to today's word");
driver.findElementByXPath("//*[@resource-id=\"com.google.android.googlequicksearchbox:id/send_button\"]").click();

This code will select Google Assistant text field, type the "talk to today's word" sentence and press the "send" button. You can run this code after opening Google Assistant Activity as shown above.