Wednesday, November 17, 2010

SMSSender with User Interface using SL4A WebView

Screenshots

Start of the application
Preview of CSV file
Load text from a file
Merge CSV text and pick messages to send

Using Android dialog while sending

Report

Along the way

While working on this project which is my first script that provides a User Interface via WebView, I encountered some challenges. Here are a short list and how I tried to handle them.

Nothing works in WebView

At some point nothing worked in my HTML page. This was due to a small problem in the javascript which simply crashed the whole JS side. One in particular that got me for a while was that the JS implementation does not like non-norm tag attributes. <a badattribute="hello" > would make JS crash upon calling element.getProperty("badattribute"); So if the entire HTML page is just unresponsive, I'll look for JS error (sometimes easy to find by opening the page in Chrome and looking at the console).

WebView not showing anymore

Description of the problem: script starts, you can see some python output but the WebView does not show.
Cause: a previous crash of the script happened while JS was waiting for a particular event.
Solution: restart the script, use the home button, go to running applications and end SL4A altogether. Then the WebView should be back.
Preventive solution: Give a timeout parameter to waitForEvent: waitForEvent("name",30000), but this is sometimes only applicable during debugging.

Can't import CSS or an external script

Surprisingly, in the HTML file, "./" isn't the folder where the HTML resides but the one where the python script is. So, if the script is in /sdcard/sl4a/scripts, the html file is in sdcard/sl4a/scripts/html and the CSS file is /sdcard/sl4a/scripts/css, importing the css will be done by:
<link type="text/css" rel="stylesheet" href="./css/yourfile.css">

CSS is not being updated

We're using a WebView therefore CSS should work and it does, very well actually. It's just that sometimes it takes a while (several restarts) before taking effect. I'm guessing that the CSS is cached when using webview. For that same reason, so far I'm putting all the JS code direct into the HTML file, although I haven't yet tested the issue with external JS.

Exiting

So far I haven't found a solution. Having the python script exit, does not force the webview to exit. When showWebView is called it returns a Result object with an id but I'm not sure if/how that can be used. Edit: When compiled into an APK app, exiting the webview appears to exit the whole app.

Why chat between Python and Javascript?

Actually if you look at the example I created, the SMSSender with UI, the things that the application does could probably be handled by JS alone. Using the Android layer, we can call intents, etc from JS just as well as Python. So we could've written the whole thing in JS. But, Python is just so full of nice libraries that I couldn't resist. CSV handled in one line, ConfigParser,etc.
To be frank, my first reason to go back to Python was that, for some reason, AJAX calls to a distant location from within the WebView didn't work - not totally surprising though, form local to another server. So I chose to get back to urllib, urllib2 and co from Python to handle connections to external server. Not using that feature right now though.

What's the result of a positive/negative dialog?

An alert dialog where you click the positive or negative button will return a Result with a result attribute which has a "which" key. So in JS, read it with
r=droid.dialogGetResponse();
clicked = r.result.which; //clicked will be "positive" or "negative"

No comments:

Post a Comment