Skip to content

Commit 646bd83

Browse files
committed
new android.activity module, that provide bind/unbind() method for on_new_intent / on_activity_result events.
1 parent ee52b38 commit 646bd83

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

docs/source/android.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,50 @@ Android (``android``)
3232
keycodes are available as constants in the android module.
3333

3434

35+
Activity (``android.activity``)
36+
-------------------------------
37+
38+
.. module:: android.activity
39+
40+
The default PythonActivity have a observer pattern for `onActivityResult <http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)>`_ and `onNewIntent <http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)>`_.
41+
42+
.. function:: bind(eventname=callback, ...)
43+
44+
This allows you to bind a callback to an Android event:
45+
- ``on_new_intent`` is the event associated to the onNewIntent java call
46+
- ``on_activity_result`` is the event associated to the onActivityResult java call
47+
48+
.. function:: unbind(eventname=callback, ...)
49+
50+
Unregister a previously registered callback with :func:`bind`.
51+
52+
Example::
53+
54+
# this example is a snippet from an NFC p2p app, and are located into a
55+
# kivy App class implementation
56+
57+
from android import activity
58+
59+
def on_new_intent(self, intent):
60+
if intent.getAction() != NfcAdapter.ACTION_NDEF_DISCOVERED:
61+
return
62+
rawmsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
63+
if not rawmsgs:
64+
return
65+
for message in rawmsgs:
66+
message = cast(NdefMessage, message)
67+
payload = message.getRecords()[0].getPayload()
68+
print 'payload: {}'.format(''.join(map(chr, payload)))
69+
70+
def nfc_enable(self):
71+
activity.bind(on_new_intent=self.on_new_intent)
72+
# ...
73+
74+
def nfc_disable(self):
75+
activity.unbind(on_new_intent=self.on_new_intent)
76+
# ...
77+
78+
3579
Billing (``android.billing``)
3680
-----------------------------
3781

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from jnius import PythonJavaClass, java_method
2+
3+
_callbacks = {
4+
'on_new_intent': [],
5+
'on_activity_result': [] }
6+
7+
class NewIntentListener(PythonJavaClass):
8+
__javainterfaces__ = ['org/renpy/android/PythonActivity$NewIntentListener']
9+
__javacontext__ = 'app'
10+
11+
def __init__(self, callback):
12+
super(NewIntentListener, self).__init__()
13+
self.callback = callback
14+
15+
@java_method('(Landroid/content/Intent;)V')
16+
def onNewIntent(self, intent):
17+
self.callback(intent)
18+
19+
20+
class ActivityResultListener(PythonJavaClass):
21+
__javainterfaces__ = ['org/renpy/android/PythonActivity$ActivityResultListener']
22+
__javacontext__ = 'app'
23+
24+
def __init__(self, callback):
25+
super(ActivityResultListener, self).__init__()
26+
self.callback = callback
27+
28+
@java_method('(IILandroid/content/Intent;)V')
29+
def onActivityResult(self, requestCode, resultCode, intent):
30+
self.callback(requestCode, resultCode, intent)
31+
32+
33+
def bind(**kwargs):
34+
for event, callback in kwargs.items():
35+
if event not in _callbacks:
36+
raise Exception('Unknown {!r} event'.format(event))
37+
elif event == 'on_new_intent':
38+
listener = NewIntentListener(callback)
39+
_callbacks[event].append(listener)
40+
elif event == 'on_activity_result':
41+
listener = ActivityResultListener(callback)
42+
_callbacks[event].append(callback)
43+
44+
def unbind(**kwargs):
45+
for event, callback in kwargs.items():
46+
if event not in _callbacks:
47+
raise Exception('Unknown {!r} event'.format(event))
48+
else:
49+
for listener in _callbacks[event][:]:
50+
if listener.callback is callback:
51+
_callbacks[event].remove(listener)
52+

0 commit comments

Comments
 (0)