Voice enabled Chatter search app for Android
As part of my session on Android development at Cloudforce NY, I had demoed an application that lets you search Chatter by speaking the search word or phrase instead of typing it. I've posted the code here and you can download it, peruse it and try it out on a tricked out Android device of your choice!
Setup : Download and unzip the package in your local machine. Create a new Android project in Eclipse (File–>New–>Android Project) and chose the 'Create Project from existing source' option. Browse to the directory where you unzipped the package in the 'Location' field, select an Android Build target (2.2 or above) and finish the import. Next, download the Force.com toolkit for Android and import it into the Eclipse project. You can do so by going to Project –> Properties–> Java Build Path–> Source–> Link Source. Choose the folder where you unzipped the toolkit (e.g. C:Tempdeveloperforce-Force.com-…Sforce-Android-Toolkit) and give a Folder Name (e.g. 'Sforce-Android-Toolkit'). Next, in order to test the application you'll need to setup OAuth in your target Org. This can be done by creating a new Remote Access Application (Setup–>Develop–>Remote Access) and updating the 'consumerKey' and 'callbackUrl' parameters in the res/values/strings.xml file to their respective values.
Note: Since the application requires a microphone input, you can't test it in the Android Emulator in Eclipse. If you want to take the app for a spin you'll have to try it out on an actual Android phone. Instructions on how to upload the application to an Android phone can be found here.
Application walk-through: Here's the basic anatomy of the application :
1) First, the application uses the 'loginOAuth' method of the Force.com toolkit for Android to perform OAuth 2.0 authentication with Force.com (this initialization code can be found in the SfdcLogin.java class). Using OAuth instead of username/password for authentication is recommended for mobile devices since OAuth does not require the client device (i.e. the phone/tablet) to capture the username/password.
2) The user can then search Chatter by clicking the microphone button and speaking the word or phase (say 'test' or 'Acme Inc'). The app uses the Google Voice Search application that comes bundled with most Android devices to perform the speech-to-text translation. There are of course other speech-to-text APIs that you could use (like the Dragon Mobile SDK), but Google Voice Search is pre-installed on most Android devices and is free – that's a hard combination to beat!
3) Next, the app uses the REST API to perform a SOSL search on all Chatter posts in the Org for the specified search phrase (this logic can be found in the SfdcSpeechRecognition.java class). The reason for using the REST API to perform the SOSL search instead of the Android toolkit is simple – the toolkit currently does not support the SOSL search function (though it supports the more commonly used SOQL query function). Why use SOSL and not SOQL? That's because you can only query the FeedItem object (the 'master' table that stores all Chatter posts – whether they are from an entity feed, UserProfileFeed or NewsFeed) with a 'Id' where clause filter. There is no such restriction on performing a SOSL search on FeedItem. In addition, you could search the FeedComment object with the same SOSL query if you wanted to (the current app doesn't do that) and so using SOSL is a better option for this use case.
4) The app then performs a SOQL query using the 'query' method of the Android toolkit to retrieve the URL's for the Chatter profile pics. Displaying a user's Chatter profile pic in a mobile device can be tricky and so I use the pattern described in this post to display the pics in the search result list. Also, the application manages the actual download of the user pics asynchronously for a better user experience. You don't want to block the application while you wait for all the pics to download and so the download happens asynchronously from the main UI thread with the screen getting updated as each pic finishes downloading (this logic is available in the ImageDownloader.java class).
A final note. Though this sample application was developed for searching Chatter, you can just as easily re-purpose it to perform voice enabled search on any data in Force.com (Accounts, Contacts, Custom Object etc.). In fact, that was the whole point of developing and posting this app – to act as a basic template for other, more innovative voice enabled mobile applications. We're living in a Cloud 2 world – its open, social and mobile. And now its also voice activated.