Stuff about Python, multitouch, optical tracking and audio

Main menu

Post navigation

Kivy is organizing its second application development contest!. This is a great chance for users both new and experienced to show off their skills and compete for prizes. Entries will be judged on a range of criteria accessible to both new and experienced programmers, so don’t be afraid to dive in!

Only 2 hours of coding where needed for the first published version of the game. Then i learned on the Google Play Game services APIs for integrating a leaderboard and achievements. And after a couple of hours during the night, what i can say is: Be patient! Even if you follow the documentation carefully, you need to wait few hours before your app_id works on their server, even in test mode. There never warn us about it.

There is a little more code around that, but globally, using the new Play Games services APIs is now easy. I hope this little piece of code will help peoples to integrate it into their app. Somebody need to start a Python library for managing Google Play API and Game Center for iOS.

Have a look at the source code for an example of the integration with the Play Games services APIs 🙂

For another museum project, Arnaud asked me to see if we could integrate Facebook in an app on Android. The usuallibraries are made for desktop, and manually open a webbrowser to handle the user authorization. But that’s not really nice for us, it would be nicer to have a native integration instead. Let’s see how we can use the official Facebook Android SDK.

Get the example

Register a Facebook application

Fill the information “App Name”. Don’t put “FB” or “Facebook” in the title, or your application name will be considered as Invalid 🙂

Pass the captcha

Write down your application id somewhere

Now, you need to activate Native Android App. The package name is the same name as the one we will pass in --package when building the APK. The Class Name is the packagename/activity. Activity class is always the same for all the applications compiled with python-for-android.

Right now, the Facebook Android SDK will not authorize our android application to be used, because Facebook want to know the signature hash that will be used by our generated APK. To generate the key hashes, you need to use the keytool. Here is an example for the android “debug” key:

1

2

3

4

5

$keytool-exportcert-aliasandroiddebugkey\

-keystore~/.android/debug.keystore|\

openssl sha1-binary|openssl base64

Enter keystore password:android

u+bzQmG87L298C4KGM8yODi3W/4=

Copy the generated key hash, and paste it to the field:

Setup the our application

Go into p4a-facebook-example, and edit the main.py

Search for FACEBOOK_APP_ID, and replace with your own Application Id

Go into python-for-android, and create a distribution:

1

$./distribute.sh-m'kivy'

Go into the dist/default

Generate the APK one time (for generating all the file we need to customize). Replace /path/to/p4a-facebook-example:

1

2

3

./build.py--name"Facebook test"--packageorg.test.facebook\

--version1--private/path/to/p4a-facebook-example/\

--window--permission INTERNET debug

Add a new reference to project.properties to include the Facebook SDK. The path to the Facebook SDK MUST be a relative path, otherwise you’ll get issues during the next build.

1

android.library.reference.1=../../facebook-android-sdk-3.0.2/facebook

Edit the templates/AndroidManifest.tmpl.xml to include the Facebook login activity, just under the <application>:

Test your application

The Facebook SDK 3.0 require to separate read and publish permissions. To the user, it means you’ll have 2 dialog to accept. Even if you just want to publish to the application stream, you need to have the basic permission (read) accepted by the user before publish permissions. This is already implemented in the example app.

When you start the application the first time, you’ll have:

Depending if you have the Native Facebook application or not, you’ll have 2 kinds of authentification boxes.

Without Facebook installed on the device:

And with Facebook installed on the device:

After authentication, the user will need to accept our permissions:

It’s done!

The application have now the right to post 🙂 The example post in the user stream as the application itself, not as the user. It’s called “publish_actions”.

Getting further

Obviously, when you use the Facebook SDK itself, you feel the pain of an API designed for Java. For every callback that Facebook want to call, you need to implement a Java class and define the callback method. Python is really simpler and fun to use.

See for yourself: we want to make a call of “Request.newStatusUpdateRequest“. Se weed first to implement a GraphUserCallback class, in Java. Thanks to Pyjnius, we can do it directly in Python:

This Python/Java class will call our own python callback when the Java callback onCompleted will be called. Then:

1

2

3

4

5

@run_on_ui_thread

defpost(self,text,callback):

req=Request.newStatusUpdateRequest(

self._session,text,_FacebookRequestCallback(callback))

req.executeAsync()

All you have to do at the end, is to call the post method:

1

2

3

4

5

deffb_post(self,text):

defcallback(*args):

fromtimeimporttime

self.post_status='message posted at {}'.format(time())

self.facebook.post(text,callback=callback)

I don’t provide a wrapper around all the possible Request method you can do with Facebook. I just have an example for “post” and “me”. Both wrap “Request.newStatusUpdateRequest” and “Request.newMeRequest“. Please note that every request call must happen in the UI thread. Use the python-for-android/runnable module for that, with @run_on_ui_thread decorator.

The end

It was not easy to get it right, and it still complex to make all the pieces together. I’ve tried to use their own LoginButton (because they said that’s how they want it, everywhere), but i’ve not be able to use it in our app. Mostly because the LoginButton is a pure android widget, and because it doesn’t want to bring back the Activity after login. I was stuck too much time on it, and preferred to go in another way.
Please note that you should not share an application with a fake Facebook button, the design / look-and-feel must be the same as the original one.

ScrollView has been re-factored with new physics calculations. To be exact, the scrollview doesn’t calculate anything now, it just pass the touch position to an Scroll effect. This class calculate the movement’s velocity, and the over-scroll’s distance (means how far you scrolled out of the bounds). Then, we implemented 2 visual effects that use the over-scroll to make the scrollview act as a Damped spring, and/or to fade out the scrollview if you over-scroll too much.

Garden: a separated organization for centralize user’s widgets and addons. You can create garden packages very easily, and import it in the source code with just “kivy.garden.packagename“. Few garden packages are already available!

We found others libraries such as JPype or Py4j, but we weren’t happy with the design and the usability. Using Jython wasn’t an option either, because we wanted to use the library within our python for android project.

Let me show you how simple it is to use Pyjnius:

1

2

3

4

5

6

7

8

9

>>>fromjnius importautoclass

>>>Stack=autoclass('java.util.Stack')

>>>stack=Stack()

>>>stack.push('hello')

>>>stack.push('world')

>>>stack.pop()

'world'

>>>stack.pop()

'hello'

What we demonstrate here is the autoclass function that create kind-of proxy that reflect all the methods and fields available from the Java class java.util.Stack.

Ok, maybe you want an Android-related example? Just have a look:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

fromjnius importautoclass

fromtimeimportsleep

MediaRecorder=autoclass('android.media.MediaRecorder')

AudioSource=autoclass('android.media.MediaRecorder$AudioSource')

OutputFormat=autoclass('android.media.MediaRecorder$OutputFormat')

AudioEncoder=autoclass('android.media.MediaRecorder$AudioEncoder')

# Record the Microphone with a 3GP recorder

mRecorder=MediaRecorder()

mRecorder.setAudioSource(AudioSource.MIC)

mRecorder.setOutputFormat(OutputFormat.THREE_GPP)

mRecorder.setOutputFile('/sdcard/testrecorder.3gp')

mRecorder.setAudioEncoder(AudioEncoder.ARM_NB)

mRecorder.prepare()

# Record 5 seconds

mRecorder.start()

sleep(5)

mRecorder.stop()

mRecorder.release()

More examples are available in the documentation. We have a mapping between Java/Python type, native arrays, support for methods with multiple signatures, and a lot more. We are using Cython + JNI internally. The performance cost is minimal.

For the full story on the multiple Apple limitations we are working with, i want to share the hardest part for Python integration: Apple’s prohibition on using dlopen() to load dynamic libraries.
In a typical case, a compiled Python library’s extension is a “.so”, and the “.so” is dlopen() at import.

That being said, as we did for the python-for-android project published in January, we are redirecting the compilation object to create static libraries. Theses libraries are included in the final application binary. But it’s not enough: we must also tell to Python to look for the library entry point in the application binary, instead of dlopen(). So in the python dynload loader:

1

return(dl_funcptr)dlsym(RTLD_MAIN_ONLY,funcname)

This way, Python will always look at the application binary, and never need to use dlopen().

If you are worried that Apple would reject a Python-based application, or even using Kivy altogether, we have tested it for you: the game that won the Kivy contest has been packaged for IOS, submitted to Apple… and accepted. You can found Deflectouch on iTunes (source code).

During January, we hosted a contest for promoting the Kivy framework. The goal was simple: create a game using Kivy, without external non-pure python dependencies. All the entries have been submitted on our sponsor Github (and thanks to NotionInk), under a compatible OSS licence. The contest registered 21 entries, and 11 submissions was valid.

The project is under the umbrella of Kivy organization, but is not designed to be limited to Kivy only. Read the documentation to correctly install the NDK/SDK Android, and set the needed environment variables.

The packaging is done in 4 steps:
1. Ensure you have Android SDK/NDK downloaded and correctly installed
2. Ensure you have some environment set
3. Create a Python distribution containing the selected modules
4. Use that distribution to build an APK of your Python application

Creating the python distribution is as simple as that:

1

2

3

4

5

# create a simple distribution with python + PIL + Kivy

./distribute.sh-m"pil kivy"

# create a distribution with python + openssl + pil + kivy

./distribute.sh-m"openssl pil kivy"

A directory dist/default will be created, including the result of the whole arm compilation.
Available libraries as for today: jpeg pil png sdl sqlite3 pygame kivy android libxml2 libxslt lxml ffmpeg openssl.

The second step is a little bit harder, since you need to provide more information for Android:

1

2

3

4

5

6

7

8

cddist/default

./build.py--packageorg.test.touchtracer--name touchtracer\

--version1.0--dir~/code/kivy/examples/demo/touchtracer debug installd

# --package: java name of your application

# --name: title of your application

# --version: version of your application

# --dir: location of your application containing the main.py

Then you’ll get a nicely bin/touchtracer-1.0-debug.apk

Pro:

A blacklist.txt file that can be used to exclude files in the final APK

Reusable distribution for other applications

Modular recipes architecture

Be able to build independents python distributions

Cons:

You need a main.py file that will be used for starting your application

Only one java bootstrap available, using OpenGL ES 2.0.

Only Kivy toolkit is working. I’m sure that other people can enhance it to add other toolkit recipes. But for example, pygame is not gonna to work because the android project is OpenGL ES 2.0: pygame drawing will not work.

I hope you’ll like it 🙂

We would like to thank Renpy / PGS4A for its initial pygame for android project

Kivy, an open source project for building cross platform applications and user interfaces, is announcing its 1st programming contest. The contest will start with the publication of official rules on January 1st. Registrations will be accepted through January 25th, and submissions will be accepted through January 31st. Prizes include Nvidia Tegra 2 android tablets from NotionInk, One-year “bronze” plans and t-shirts.

The goal of the contest is to encourage interested developers to learn more about Kivy, which is a cross platform Application and User Interface software library for the python programming language. Kivy applications run on Linux, Windows, OSX, Android (and iOS). The framework supports most inputs protocols and devices like WM_Touch, WM_Pen, Mac OS X Trackpad and Magic Mouse, single/multi touch screens, Mtdev, Linux Kernel HID, TUIO, and uses pure hardware accelerated rendering to enable developers to really take user interfaces to a new level.

The contest is sponsored by NotionInk, an indian company that designs tablets PCs, and Github, a web-based hosting service for software developments projects.