/
runnable.py
49 lines (37 loc) · 1.24 KB
/
runnable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'''
Runnable
========
'''
from jnius import PythonJavaClass, java_method, autoclass
from android.config import JAVA_NAMESPACE
# reference to the activity
_PythonActivity = autoclass(JAVA_NAMESPACE + '.PythonActivity')
class Runnable(PythonJavaClass):
'''Wrapper around Java Runnable class. This class can be used to schedule a
call of a Python function into the PythonActivity thread.
'''
__javainterfaces__ = ['java/lang/Runnable']
__runnables__ = []
def __init__(self, func):
super(Runnable, self).__init__()
self.func = func
def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
Runnable.__runnables__.append(self)
_PythonActivity.mActivity.runOnUiThread(self)
@java_method('()V')
def run(self):
try:
self.func(*self.args, **self.kwargs)
except:
import traceback
traceback.print_exc()
Runnable.__runnables__.remove(self)
def run_on_ui_thread(f):
'''Decorator to create automatically a :class:`Runnable` object with the
function. The function will be delayed and call into the Activity thread.
'''
def f2(*args, **kwargs):
Runnable(f)(*args, **kwargs)
return f2