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 50 51 52 53 54 55 56
|
from threading import Semaphore
from android.activity import register_activity_lifecycle_callbacks # noqa
from android.runnable import run_on_ui_thread # noqa
from webview.platforms.android.event import EventDispatcher
from webview.platforms.android.jclass.view import Choreographer
from webview.platforms.android.jinterface.view import FrameCallback
class EventLoop(EventDispatcher):
def __init__(self):
super(EventLoop, self).__init__()
from webview.platforms.android.app import App
self.app = App.get_running_app()
self.quit = False
self.status = 'idle'
self.resumed = False
self.destroyed = False
self.paused = False
register_activity_lifecycle_callbacks(
onActivityCreated=self.app.on_create,
onActivityPaused=self.app.on_pause,
onActivityDestroyed=self.app.on_destroy,
onActivityResumed=self.app.on_resume,
onActivityStarted=self.app.on_start,
onActivityStopped=self.app.on_stop,
)
def mainloop(self):
choreographer = None
frame_callback = None
while not self.quit and self.status == 'created':
def do_frame(_):
lock.release()
@run_on_ui_thread
def post_frame():
nonlocal choreographer, frame_callback
if not choreographer:
choreographer = Choreographer.getInstance()
if not frame_callback:
frame_callback = FrameCallback(do_frame)
choreographer.postFrameCallback(frame_callback)
lock = Semaphore(0)
post_frame()
lock.acquire()
def close(self):
self.quit = True
self.status = 'destroyed'
|