| 1 |
from threading import Semaphore |
| 2 |
|
| 3 |
from android.activity import register_activity_lifecycle_callbacks # noqa |
| 4 |
from android.runnable import run_on_ui_thread # noqa |
| 5 |
|
| 6 |
from webview.platforms.android.event import EventDispatcher |
| 7 |
from webview.platforms.android.jclass.view import Choreographer |
| 8 |
from webview.platforms.android.jinterface.view import FrameCallback |
| 9 |
|
| 10 |
|
| 11 |
class EventLoop(EventDispatcher): |
| 12 |
def __init__(self): |
| 13 |
super().__init__() |
| 14 |
from webview.platforms.android.app import App |
| 15 |
|
| 16 |
self.app = App.get_running_app() |
| 17 |
self.quit = False |
| 18 |
self.status = 'idle' |
| 19 |
self.resumed = False |
| 20 |
self.destroyed = False |
| 21 |
self.paused = False |
| 22 |
register_activity_lifecycle_callbacks( |
| 23 |
onActivityCreated=self.app.on_create, |
| 24 |
onActivityPaused=self.app.on_pause, |
| 25 |
onActivityDestroyed=self.app.on_destroy, |
| 26 |
onActivityResumed=self.app.on_resume, |
| 27 |
onActivityStarted=self.app.on_start, |
| 28 |
onActivityStopped=self.app.on_stop, |
| 29 |
) |
| 30 |
|
| 31 |
def mainloop(self): |
| 32 |
choreographer = None |
| 33 |
frame_callback = None |
| 34 |
while not self.quit and self.status == 'created': |
| 35 |
|
| 36 |
def do_frame(_): |
| 37 |
lock.release() |
| 38 |
|
| 39 |
@run_on_ui_thread |
| 40 |
def post_frame(): |
| 41 |
nonlocal choreographer, frame_callback |
| 42 |
|
| 43 |
if not choreographer: |
| 44 |
choreographer = Choreographer.getInstance() |
| 45 |
if not frame_callback: |
| 46 |
frame_callback = FrameCallback(do_frame) |
| 47 |
|
| 48 |
choreographer.postFrameCallback(frame_callback) |
| 49 |
|
| 50 |
lock = Semaphore(0) |
| 51 |
post_frame() |
| 52 |
lock.acquire() |
| 53 |
|
| 54 |
def close(self): |
| 55 |
self.quit = True |
| 56 |
self.status = 'destroyed' |