Android Stuff

I write all my apps in Python using the SL4A software.  You need to install SL4A (here) before being able to use these apps.  If you'd like to develop with SL4A via the Android SDK and Eclispe, follow this tutorial. The instructions work for Linux as well.  The source code I provide needs to be pasted into script.py in those instructions.

Click my ads if you have a moment, I'm poor.

Battery Information, Weather Report and Take a Picture
    All the battery keywords are there with some new ones:  weather, picture, you.  Weather speaks the local weather report, picture takes a new picture - you can just ask for a new picture and it tells you the filename it saved it as, or you can use the keyword 'name' after 'picture' to specify a unique filename like:  "take a new picture with name flower".  You can also ask who/what this is with the 'you' keyword, just like Siri:  "who are you" or "what are you" and it speaks a basic string.  The APK is attached at the bottom of the page as SpeakAll.apk - this will be updated as I add more functions.




Source Code:


import android
import string
import urllib
import urllib2
from xml.dom import minidom

WEATHER_URL = 'http://www.google.com/ig/api?weather=%s&hl=%s'


def extract_value(dom, parent, child):
return dom.getElementsByTagName(parent)[0].getElementsByTagName(child)[0].getAttribute('data')


def fetch_weather(location, hl=''):
url = WEATHER_URL % (urllib.quote(location), hl)
handler = urllib2.urlopen(url)
data = handler.read()
dom = minidom.parseString(data)
handler.close()
data = {}
weather_dom = dom.getElementsByTagName('weather')[0]
data['city'] = extract_value(weather_dom, 'forecast_information','city')
data['temperature'] = extract_value(weather_dom, 'current_conditions','temp_f')
data['conditions'] = extract_value(weather_dom, 'current_conditions', 'condition')
dom.unlink()
return data



def say_weather(droid):
location = droid.getLastKnownLocation().result
if location['gps'] is not None:
 location = location['gps']
else:
 location = location['network']
addresses = droid.geocode(location['latitude'], location['longitude'])
zip = addresses.result[0]['postal_code']
if zip is None:
 msg = 'Failed to find location.'
else:
 result = fetch_weather(zip)
#  msg = '%(temperature)s degrees and %(conditions)s, in %(city)s.' % result
 msg = '%(temperature)s degrees and %(conditions)s.' % result
droid.ttsSpeak(msg)



if __name__ == '__main__':
droid = android.Android()
droid.batteryStartMonitoring()
lvl = droid.batteryGetLevel()
blevel = str(lvl[1])
vlt = droid.batteryGetVoltage()
volts = str(vlt[1])
r = volts[0] + "." + volts[1:]
bvolts = str(r)
tmp = droid.batteryGetTemperature()
temp = str(tmp[1])
r = temp[0:2]
degf = (int(r) * 9) / 5 + 32
btemp = str(degf)
hlt = droid.batteryGetHealth()
sts = droid.batteryGetStatus()
typ = droid.batteryGetPlugType()
droid.batteryStopMonitoring()
if hlt[1] == 1:
 bhealth = " health is unknown."
elif hlt[1] == 2:
 bhealth = " health is good."
elif hlt[1] == 3:
 bhealth = " health is overheat."
elif hlt[1] == 4:
 bhealth = " health is bad."
elif hlt[1] == 5:
 bhealth = " health is undervolt."
elif hlt[1] == 6:
 bhealth = " health failure."
if sts[1] == 1:
 bstatus = " status is unknown."
elif sts[1] == 2:
 bstatus = " status is charging."
elif sts[1] == 3:
 bstatus = " status is discharging."
elif sts[1] == 4:
 bstatus = " status is not charging."
elif sts[1] == 5:
 bstatus = " status error."
if typ[1] == 0:
 btype = "  device is unplugged."
elif typ[1] == 1:
 btype = "  device is on a c adapter."
elif typ[1] == 2:
 btype = "  device is on u s b cable."
blevel1 = " battery level is " + blevel + "percent."
bvolts1 = " " + bvolts + " volts."
btemp1 = " " + btemp + " degrees."
allbatt =   blevel1 + bvolts1 + btemp1 + bhealth + bstatus + btype
a = droid.recognizeSpeech()
a = str(a)
strt = a.find("result=u'")
endl = a.find(",", strt)
txt = a[strt+9:endl-1]
ask = txt.find("you")
if ask > 0:
droid.ttsSpeak("I am your assistant.")
ask = txt.find("weather")
if ask > 0:
say_weather(droid)
ask = txt.find("picture")
if ask > 0:
saveask = txt.find("name")
if saveask > 0:
nme1 = saveask + 5
saveloc = txt[nme1:]
mypicloc = "/mnt/sdcard/DCIM/Camera/" + str(saveloc) + ".jpg"
droid.cameraCapturePicture(mypicloc)
droid.ttsSpeak("picture saved to gallery as " + str(saveloc) + " dot j p g")
else:
droid.cameraCapturePicture('/mnt/sdcard/DCIM/Camera/photo.jpg')
droid.ttsSpeak("Picture saved to gallery as photo dot j p g")
else:
ask = txt.find("voltage")
if ask > 0:
 droid.ttsSpeak(bvolts1)
ask = txt.find("level")
if ask > 0:
 droid.ttsSpeak(blevel1)
ask = txt.find("temperature")
if ask > 0:
 droid.ttsSpeak(btemp1)
ask = txt.find("health")
if ask > 0:
 droid.ttsSpeak(bhealth)
ask = txt.find("status")
if ask > 0:
 droid.ttsSpeak(bstatus)
ask = txt.find("type")
if ask > 0:
 droid.ttsSpeak(btype)
ask = txt.find("all")
if ask > 0:
 droid.ttsSpeak(allbatt)



ċ
SpeakAll.apk
(477k)
Robert Svec,
Oct 14, 2011, 7:22 AM
Comments