mirror of
https://github.com/LBRYFoundation/lbry-sdk.git
synced 2025-08-23 17:27:25 +00:00
68 lines
No EOL
2.3 KiB
Python
68 lines
No EOL
2.3 KiB
Python
from lbrynet.extras.daemon.Daemon import Daemon
|
|
from jnius import PythonJavaClass, autoclass, java_method
|
|
JAVA_NAMESPACE = 'org.kivy.android'
|
|
JNI_NAMESPACE = 'org/kivy/android'
|
|
|
|
_activity = autoclass(JAVA_NAMESPACE + '.PythonActivity').mActivity
|
|
|
|
_callbacks = {
|
|
'on_new_intent': [],
|
|
'on_activity_result': [],
|
|
}
|
|
|
|
|
|
class NewIntentListener(PythonJavaClass):
|
|
__javainterfaces__ = [JNI_NAMESPACE + '/PythonActivity$NewIntentListener']
|
|
__javacontext__ = 'app'
|
|
|
|
def __init__(self, callback, **kwargs):
|
|
super(NewIntentListener, self).__init__(**kwargs)
|
|
self.callback = callback
|
|
|
|
@java_method('(Landroid/content/Intent;)V')
|
|
def onNewIntent(self, intent):
|
|
self.callback(intent)
|
|
|
|
|
|
class ActivityResultListener(PythonJavaClass):
|
|
__javainterfaces__ = [JNI_NAMESPACE + '/PythonActivity$ActivityResultListener']
|
|
__javacontext__ = 'app'
|
|
|
|
def __init__(self, callback):
|
|
super(ActivityResultListener, self).__init__()
|
|
self.callback = callback
|
|
|
|
@java_method('(IILandroid/content/Intent;)V')
|
|
def onActivityResult(self, requestCode, resultCode, intent):
|
|
self.callback(requestCode, resultCode, intent)
|
|
|
|
|
|
def bind(**kwargs):
|
|
for event, callback in kwargs.items():
|
|
if event not in _callbacks:
|
|
raise Exception('Unknown {!r} event'.format(event))
|
|
elif event == 'on_new_intent':
|
|
listener = NewIntentListener(callback)
|
|
_activity.registerNewIntentListener(listener)
|
|
_callbacks[event].append(listener)
|
|
elif event == 'on_activity_result':
|
|
listener = ActivityResultListener(callback)
|
|
_activity.registerActivityResultListener(listener)
|
|
_callbacks[event].append(listener)
|
|
|
|
|
|
def unbind(**kwargs):
|
|
for event, callback in kwargs.items():
|
|
if event not in _callbacks:
|
|
raise Exception('Unknown {!r} event'.format(event))
|
|
else:
|
|
for listener in _callbacks[event][:]:
|
|
if listener.callback == callback:
|
|
_callbacks[event].remove(listener)
|
|
if event == 'on_new_intent':
|
|
_activity.unregisterNewIntentListener(listener)
|
|
elif event == 'on_activity_result':
|
|
_activity.unregisterActivityResultListener(listener)
|
|
|
|
|
|
print('YEAH! Done all the things!!! WOOOHOOO!!!!') |