| 1 |
__all__ = ('ValueCallback', 'DownloadListener') |
| 2 |
|
| 3 |
from jnius import PythonJavaClass, java_method |
| 4 |
|
| 5 |
|
| 6 |
class ValueCallback(PythonJavaClass): |
| 7 |
""" |
| 8 |
Represents a callback to receive a value from Java code. |
| 9 |
|
| 10 |
This class implements the Android ValueCallback interface in order to receive |
| 11 |
values from a Java environment. It's particularly useful when working with |
| 12 |
Android-related tasks where results from Java methods are passed back to Python. |
| 13 |
|
| 14 |
Attributes: |
| 15 |
on_receive_value: Callable function to handle the received value from the Java |
| 16 |
environment. |
| 17 |
|
| 18 |
Methods: |
| 19 |
onReceiveValue: This method is invoked when a value is received from Java, |
| 20 |
passing the value to the Python callback function. |
| 21 |
""" |
| 22 |
|
| 23 |
__javacontext__ = 'app' |
| 24 |
__javainterfaces__ = ['android/webkit/ValueCallback'] |
| 25 |
|
| 26 |
def __init__(self, on_receive_value): |
| 27 |
self.on_receive_value = on_receive_value |
| 28 |
|
| 29 |
@java_method('(Ljava/lang/Object;)V') |
| 30 |
def onReceiveValue(self, value): |
| 31 |
""" |
| 32 |
Handles the receiving of a single value. This method is invoked to process or |
| 33 |
handle a given object value when required. |
| 34 |
|
| 35 |
Args: |
| 36 |
value: The object received which needs to be processed. |
| 37 |
""" |
| 38 |
self.on_receive_value(value) |
| 39 |
|
| 40 |
|
| 41 |
class DownloadListener(PythonJavaClass): |
| 42 |
""" |
| 43 |
Handles download events initiated through a WebView in an Android application. |
| 44 |
|
| 45 |
This class serves as a bridge for the Android `DownloadListener` interface, |
| 46 |
facilitating the handling of download requests generated during WebView interactions. |
| 47 |
By implementing this interface, it allows the developer to intercept and handle |
| 48 |
download events directly using the provided callback. |
| 49 |
|
| 50 |
Attributes: |
| 51 |
on_download_start (callable): A callback function that is triggered when a download |
| 52 |
starts. The function should accept five parameters: url, user_agent, content_disposition, |
| 53 |
mimetype, and content_length. |
| 54 |
""" |
| 55 |
|
| 56 |
__javacontext__ = 'app' |
| 57 |
__javainterfaces__ = ['android/webkit/DownloadListener'] |
| 58 |
|
| 59 |
def __init__(self, on_download_start): |
| 60 |
self.on_download_start = on_download_start |
| 61 |
|
| 62 |
@java_method('(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;J)V') |
| 63 |
def onDownloadStart(self, url, user_agent, content_disposition, mimetype, content_length): |
| 64 |
self.on_download_start(url, user_agent, content_disposition, mimetype, content_length) |