Skip to content

Instantly share code, notes, and snippets.

@matiboy
Created December 6, 2010 01:25
UIHandler class for SL4A
class UIHandler():
def __init__(self, waitfor=None, post=None):
# Name of the event to wait for - default python
if waitfor is None:
self.waitfor = "python"
else:
self.waitfor = waitfor
# Name of the event to post
if post is None:
self.post = "javascript"
else:
self.post = post
self.droid = android.Android()
self.dispatch = {}
def wait(self):
droid = self.droid
event = droid.waitForEvent(self.waitfor)
# This is currently needed because if the waitForEvent is too fast, the event is missed
start = time.time()
data = event.result["data"]
if type(data) in [str, unicode]:
data = str(data)
try:
data = json.loads(data)
print "ok...", data
except:
self.log("Unable to parse Json data upon receiving event. Data passed: %s" % data)
data = {}
else:
self.log("Data received from event is not a string, using empty dict")
data = {}
feedback = self.dispatch[data["task"]](data)
passing = json.dumps(feedback)
self.log(passing)
howlong = time.time() - start
min = 0.2
if howlong < min:
time.sleep(min - howlong)
self.log("Had to wait cause process was only %f second" % howlong)
droid.postEvent(self.post, passing)
def log(self, message):
print message
def startLoad(self, title="", message=""):
droid = self.droid
droid.dialogCreateSpinnerProgress(title, message)
droid.dialogShow()
def stopLoad(self):
self.droid.dialogDismiss()
@anarchyonline
Copy link

Hi matiboy!

I realize this is nearly 3 years down the line but I'd like to ask you about the postEvent glitch that SL4A had with Javascript. I noticed you made a post about it back then on the SL4A board and someone advized you to file a bug report. Have you made any progress with this issue of Javascript not catching a Python response event if it is too fast? I'm stuck one the same problem and you are the only person I could find that experienced something similar.
Hope you'll see this!

AO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment