From bec6bda2f6c19c802fc7cffd3f18ccba9ecb8388 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 2 Oct 2022 01:31:47 +0200 Subject: [PATCH] Wait for service init on start, reuse existing service --- sbapp/main.py | 16 +++++++++------- sbapp/services/sidebandservice.py | 1 + sbapp/sideband/core.py | 13 +++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/sbapp/main.py b/sbapp/main.py index 38ee203..d1e8a84 100644 --- a/sbapp/main.py +++ b/sbapp/main.py @@ -105,25 +105,27 @@ class SidebandApp(MDApp): if self.sideband.first_run: self.guide_action() - self.request_notifications_permission() + self.request_permissions() else: self.open_conversations() self.app_state = SidebandApp.ACTIVE def start_service(self): - RNS.log("Launching platform service for RNS and LXMF") + RNS.log("Launching platform-specific service for RNS and LXMF") if RNS.vendor.platformutils.get_platform() == "android": - # TODO: Check if service is running and start as necessary self.android_service = autoclass('io.unsigned.sideband.ServiceSidebandservice') mActivity = autoclass('org.kivy.android.PythonActivity').mActivity argument = self.app_dir self.android_service.start(mActivity, argument) - # TODO: Remove and add real service started check here - RNS.log("Service instance: "+str(self.android_service)) - RNS.log("Waiting for service start") - time.sleep(7) + # Wait a little extra for user to react to permissions prompt + if self.sideband.first_run: + time.sleep(6) + + # Wait for service to become available + while not self.sideband.service_available(): + time.sleep(0.20) # Start local core instance self.sideband.start() diff --git a/sbapp/services/sidebandservice.py b/sbapp/services/sidebandservice.py index 53aa0fe..1d4a661 100644 --- a/sbapp/services/sidebandservice.py +++ b/sbapp/services/sidebandservice.py @@ -145,6 +145,7 @@ class SidebandService(): def run(self): while self.should_run: + self.sideband.setstate("service.heartbeat", time.time()) time.sleep(1) self.release_locks() diff --git a/sbapp/sideband/core.py b/sbapp/sideband/core.py index f36eae1..a2c68b8 100644 --- a/sbapp/sideband/core.py +++ b/sbapp/sideband/core.py @@ -375,6 +375,19 @@ class SidebandCore(): else: return [] + def service_available(self): + service_heartbeat = self.getstate("service.heartbeat") + if not service_heartbeat: + return False + else: + try: + if time.time() - service_heartbeat > 2.5: + return False + else: + return True + except: + return False + def gui_foreground(self): return self._db_getstate("app.foreground")