Monday, January 21, 2013

My first Python app for Android, and it works!

I tried making a simple app for controlling the RaspberryPi lights controller i made earlier. It was to be a simple script, only really making a POST to a web-interface hosted on my LAN. However, simple tasks get really complex when the only tool you have is the most alien-ass backwards developing studio kit-SDK i have ever laid my eyes on. Yes im talking about AndroidADT/Eclipse.

God, please explain to me why a simple project template that only displays a blank screen need to consist of 131 Files divided across 66 Folders?!


And why does my 4 core computer, sporting 8Gb of ram and another gig of VRAM load for an amount of time that surely involves the Avogadro constant before it's able to run my simple app (displaying a blank screen). Yea, it... aint great.

So after Android-ADT and Eclipse just pissed me off to no end i decided Java could go fuck itself; my time is better spent on Python.

To run and develop Python on Android you need a Python interpreter installed on your phone. This was the main thing putting me off Python for phones in the first place, as it adds some complexity for the end user. But in my case that's usually just me anyways. Pluss it's really just an app that you need to download:
QPython+ and it's on google play store (for free).

QPython+ has a nice, no bullshit userinterface, and comes with some preinstalled libs, a text-editor and a debugger. Everything you need to develop, debug and run Python on your phone.

This below is my app in Python; 1 file, 0 folders. If my math is correct the complexity dropped by exactly half an infinity:

import urllib
# you need urllib to connect to the url
import androidhelper
# androidhelper is included in QPython+
# gives easy access to lots of android stuff, like GUI

droid = androidhelper.Android() 

def turnLights():
 # This displays a dialog with 3 options
 # Program loops untill user wants to exit
 while True:  
     title = 'Light switch:'
     droid.dialogCreateAlert(title)
     droid.dialogSetItems(['On', 'Off', 'Exit'])
     droid.dialogShow() 
     response = droid.dialogGetResponse().result['item']
     if response == 0:
            # The app connects to one of two url's
            # The web server at that url determines what
            # to do based on the page requested
            urlon = "http://192.168.1.6:8080/?turn=on"
            sock = urllib.urlopen(urlon)
            content = sock.read() 
            sock.close()
            print content
     elif response == 1:
            urloff = "http://192.168.1.6:8080/?turn=off"
            sock = urllib.urlopen(urloff)
            content = sock.read() 
            sock.close()
            print content
     elif response == 2: 
            # The user pressed Exit
            break 
# Run the app    
turnLights()

Python for Android is just awesome. Each app starts out as just one simple .py file. Easy as that. My app takes up 699 bytes. And i did bug-fixes and debugging on my phone and used the on-screen keyboard to add a couple of missing indents.

 My simple, beautiful app; a functioning light switch:


1 comment:

  1. https://www.facebook.com/photo.php?v=10202960866251504

    ReplyDelete