📜 ⬆️ ⬇️

Check habrahabr.ru user’s karma using Python on Android. Part 2 - GUI

In the first part, I described how to configure an Android smartphone to work with SL4A (Scripting Layer for Android), showed how to call system pop-up windows, get user-entered information from them and display the result of work in them. In the same part I will talk about building the interface to the application using WebViews . In short, WebViews are a way to build an application interface using HTML, JavaScript, and CSS. If we consider that in Android smartphones full-featured webkit, then building the interface is not such a problem task.

Source

habr.py
# -*- coding: utf-8 -*- import android, os, sys, urllib2 from xml.etree import ElementTree def show_karma_value(value): return value droid = android.Android() droid.webViewShow(os.path.dirname(sys.argv[0]) + '/html/habr.html') #  ,    -  /html/habr.html while True: user = droid.eventWaitFor('login').result if user: droid.dialogCreateSpinnerProgress("", " ") #         droid.dialogShow() try: feed = urllib2.urlopen('http://habrahabr.ru/api/profile/' + user['data']) #  XML XML = ElementTree.XML(feed.read()) #   XML try: #    <error> XML.find("error").text value = '  ' except: #   <error>  ,      value = XML.find("karma").text except: value = '  ' droid.dialogDismiss() #    droid.eventPost('show_karma_value', show_karma_value(value)) #    show_karma_value    droid.vibrate() #      


habr.html
 <html> <head> <title>Sensor Monitor</title> <style> .logo{ text-align: center; width: 110px; height: 110px; display: block; margin: 10px auto; background: url(http://habrahabr.ru/i/bg-multilogo.png) no-repeat 50% -144px; } #karma { color: #6DA3BD; } </style> </head> <body> <span class="logo"></span> <form onsubmit="speak(); return false;"> <label for="login"> %username%</label> <input type="text" id="login" /> <input type="submit" value="!" /> </form> <h2 id="karma" style="display: inline;" /></h2> <script> var droid = new Android(); var speak = function() { droid.eventPost("login", document.getElementById("login").value); } var show_karma_value = function(data) { document.getElementById("karma").innerHTML = ' ' + document.getElementById("login").value + ': ' + data.data; } droid.registerCallback("show_karma_value", show_karma_value); </script> </body> </html> 

')
Parse the source code

Interaction with the program is carried out using the Android class. This class is completely identical to the same class for Python, that is, from the JS code you can call any system functions.
 var droid = new Android(); 

If you look at the code of the habr.py file, you can see that in comparison with the previous part it has become less code. This is because another file is now responsible for the interface. Communicating the Python program with its interface occurs through "events" (events):
In the html file we register the event with the command
 droid.registerCallback("show_karma_value", show_karma_value); 

The first argument is the name of the event, and the second argument is the variable that reacts to this event. The variable itself may contain a function; therefore, such a functional is sufficient for any tasks. If we do not need to wait for a response from the program, but only call some function or simply transfer the data to it, we use
 droid.eventPost("login", document.getElementById("login").value); 

In the program, we expect user input
 while True: user = droid.eventWaitFor('login').result 

And only if the user triggered a login event, and passed a non-empty value to the program, we execute the rest of the code, in which we load and parse information about the user's karma, generate various error messages just in case. Further, in the program itself, we give the html event of the template the value of a variable that was calculated inside the program itself.
 droid.eventPost('show_karma_value', value) 

Our template already knows what to do with this data thanks to JS code
 var show_karma_value = function(data) { document.getElementById("karma").innerHTML = ' ' + document.getElementById("login").value + ': ' + data.data; } 


Also in the program for clarity, a pop-up window is implemented during the download and a short vibro after the end of the data download.

Conclusion

As a conclusion, it can be said that the development of Python programs for Android is not at all difficult, and even if the requirement for a program is a normal GUI is not a problem thanks to WebViews. Even after the first topic, it became clear that the topic was interesting to the habra people, so I will try to continue writing articles.

Source: https://habr.com/ru/post/122281/


All Articles