Android Deep Dive
Into the depths of the Android SDK
In this section, we will look at some of the advanced features provided by Slang's Android SDK and how they can be used by apps to get the most out of the Slang platform.
Advanced Actions
Asynchronous handling of intent actions
Normally when handling an intent, the "action" method is expected to be synchronous, i.e., Slang expects the action to be completed when the method returns. And based on the return value (SUCCESS or FAILURE) it will trigger the Completion prompt as described below. But if the action needs to perform asynchronous processing (such as a network fetch) before the action can be completed, it can make use of the session helper methods - waitForActionCompletion & notifyActionCompleted to yield control back to Slang and asynchronously notify it of the completion status.
Customizing Prompts
Completion Prompts
When an intent is detected by Slang, it will call the "action" method of the registered SlangIntentAction implementation. After the "action" method is done with its handling, Slang will speak out the Completion prompt associated with this intent if the handler returns SUCCESS.
By default it will use the Completion prompt configured in the Slang Console for that intent. The "action" method can override the prompt if necessary.
Setting the Greeting and Clarification prompt
By default Slang will show a prompt on its surface when the Slang Trigger is pressed ("Hi! How may I help you"). Similarly when Slang does not understand what the user is saying, it will ask for the user to retry with a default prompt ("Sorry, I didn't understand that, could you please say it again?). These prompts can be overridden in the Slang Console.
Prompting for missing information
Sometimes, when a user speaks an utterance that corresponds to an intent, it is possible that some important information could be left out of the utterance. For example, for flight ticket booking intent, the required pieces of information would be the source city, the destination city and the date of travel. However, the user could end up speaking an utterance with incomplete information, like this:
"Show me flights from Bangalore to Delhi"
In this case, the user has not specified the date of travel, which is required information for processing the intent and the app should prompt the user to provide the missing information. There are two ways in which this can be achieved through Slang:
1. Configure entity prompts in Slang Console (Recommended): In this method, the app's configuration in the Slang Console should contain appropriate messages to prompt the user for each entity. You can configure these messages for each entity under the Prompts section in the Console as shown below.
These values can be accessed in the app and used to prompt the users to provide the missing information in the following way:
2. Override completion statement fully within the app: With this method, the app can fully override the completion statement without relying on the app configuration in Slang Console. Here is how this can be done:
Error Handling
Handling Slang errors
When Slang is initialized in the application (typically in the onCreate method the Application class), the SDK performs a handshake with our servers and verifies that the Buddy credentials are valid and then retrieve details about the Buddy. But if things go wrong during this process (eg the Buddy credentials are invalid or the network is unreachable), then the app needs to be notified of this. This is done via the SlangBuddy.Listener handler. To register the Listener, instantiate one and pass it to the SlangBuddyOptions at initialization time.
Handling unresolved utterances
By default, Slang processes all user utterance and attempts to detect the intent. On successful intent detection, it calls the "action" method of the registered SlangIntentAction instance. For utterances that did not resolve into intents, Slang will ask the user to retry. But sometimes the app may want to get notified about these unresolved utterances (and also all utterances). To get notified, pass an instance of the SlangUtteranceAction class to the SlangBuddyOptions at initialization time.
Customizing the UI
Changing the spoken voice (Talkback)
By default the voice spoken is a female voice that best suits the current locale
. To change the voice to a male voice instead, use the below API
Turning on/off Slang Trigger
Sometimes it might be required to turn off Slang (for example, when in the login page). To do this, you can use the "show/hide" APIs. Note that it's important to make sure that Slang has initialized before calling these APIs.
Customizing default Slang Trigger/Surface
By default, Slang uses a green theme for its assets and use a green mic button and Slang branded icon. Use the SlangBuiltInUI object (which can be retrieved using SlangBuddy.getBuiltinUI()) to customize it.
Note that some of these customizations might be available only under the paid plan. In such cases, Slang will throw a SlangBuddy.InsufficientPrivilegeException if used under free plans.