openhab/openhab.android — pull request #28 by Prom42 (2014-11-02 13:45:42+00:00)
Title: Refactoring of microphone widget from activity into own service

This pull request fixes the following issues mentioned in #5 :
- HABdroid is not opened anymore when the microphone widget is used
- Text overlay (Toast) is displayed with the recognized command
- Voice recognition works regardless of whether HABdroid is active on the background or not

It does not address the following suggestions (maybe a seperate ticket can be created for these):
- Do not include two on-click functions (execute voice recognition & openHAB) in the widget
- Enable widget for lock screen

A few words about the implementation:
Previously, the voice command functionality was included in the OpenHABMainActivity, which was called by an Intent. That's probably why the HABdroid GUI was always opened, when the voice widget was used. I couldn't find a way to execute voice recognition without the GUI being opened. Extracting the voice command functionality into an service (OpenHABVoiceService.java) solved this problem however.

The OpenHABVoiceService is called from two places: The microphone menu icon inside HABdroid, and the microphone widget.
- In case the service is called from the menu icon, the openHAB URL can be passed in the Intent.
- In case the service is called from the widget, it uses the OpenHABTracker to determine the URL of the openHAB Server. This was a little bit tricky because OpenHABTracker uses asynchronous autodiscovery if the openHAB URL is not configured. (For this reason, the ContinuingIntentService buffers the voice commands until the openHAB URL is discovered.)

I also had to adjust the AsyncServiceResolver a little (besides converting tabs to spaces) because it is called indirectly also from the service. This cast would then not work anymore: ((Activity) mCtx).runOnUiThread(), as the mCtx is not an activity.

I'd be happy to hear about any feedback.