| 1 |
from android.runnable import run_on_ui_thread # noqa |
| 2 |
from android.activity import _activity as act # noqa |
| 3 |
from webview.platforms.android.base import EventLoop |
| 4 |
from webview.platforms.android.event import EventDispatcher |
| 5 |
|
| 6 |
|
| 7 |
class App(EventDispatcher): |
| 8 |
# Return the current running App instance |
| 9 |
_running_app = None |
| 10 |
|
| 11 |
def __init__(self, **kwargs): |
| 12 |
App._running_app = self |
| 13 |
super().__init__(**kwargs) |
| 14 |
self._eventloop = EventLoop() |
| 15 |
|
| 16 |
#: The *root* widget returned by the :meth:`build_view` |
| 17 |
self.root = None |
| 18 |
|
| 19 |
def build_view(self): |
| 20 |
"""Initializes the application; it will be called only once. |
| 21 |
If this method returns a widget (tree), it will be used as the root |
| 22 |
widget and added to the window. |
| 23 |
|
| 24 |
:return: |
| 25 |
None or a root :class:`~android.widget.RelativeLayout` instance |
| 26 |
if no self.root exists.""" |
| 27 |
return self.root |
| 28 |
|
| 29 |
@run_on_ui_thread |
| 30 |
def set_content_view(self, root): |
| 31 |
act.setContentView(root) |
| 32 |
|
| 33 |
def on_create(self, activity, saved_instance_state): |
| 34 |
"""Event handler for the `on_create` event which is fired after |
| 35 |
initialization (after build() has been called) but before the |
| 36 |
application has started running. |
| 37 |
""" |
| 38 |
|
| 39 |
def on_pause(self, activity): |
| 40 |
"""Event handler called when Pause mode is requested""" |
| 41 |
|
| 42 |
def on_destroy(self, activity): |
| 43 |
"""Event handler for the `on_destroy` event which is fired when the |
| 44 |
application has finished running (i.e. the window is about to be |
| 45 |
closed). |
| 46 |
""" |
| 47 |
|
| 48 |
def on_resume(self, activity): |
| 49 |
pass |
| 50 |
|
| 51 |
def on_start(self, activity): |
| 52 |
pass |
| 53 |
|
| 54 |
def on_stop(self, activity): |
| 55 |
pass |
| 56 |
|
| 57 |
def run(self): |
| 58 |
self.build_view() |
| 59 |
self._eventloop.status = 'created' |
| 60 |
self._eventloop.mainloop() |
| 61 |
|
| 62 |
@staticmethod |
| 63 |
def get_running_app(): |
| 64 |
"""Return the currently running application instance.""" |
| 65 |
return App._running_app |
| 66 |
|
| 67 |
def pause(self): |
| 68 |
"""Pause the application. |
| 69 |
|
| 70 |
On Android set OS state to pause, Kivy app state follows. |
| 71 |
No functionality on other OS. |
| 72 |
.. versionadded:: 2.2.0 |
| 73 |
""" |
| 74 |
act.moveTaskToBack(True) |
| 75 |
|
| 76 |
def stop(self): |
| 77 |
self.dispatch('on_destroy') |
| 78 |
self._eventloop.close() |
| 79 |
App._running_app = None |