2022-09-17 08:00:55 -06:00
|
|
|
import time
|
2022-09-20 09:28:39 -06:00
|
|
|
import RNS
|
|
|
|
from os import environ
|
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
from kivy.logger import Logger, LOG_LEVELS
|
|
|
|
# TODO: Reset
|
|
|
|
Logger.setLevel(LOG_LEVELS["debug"])
|
|
|
|
# Logger.setLevel(LOG_LEVELS["error"])
|
2022-09-17 08:00:55 -06:00
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
if RNS.vendor.platformutils.get_platform() == "android":
|
|
|
|
from jnius import autoclass, cast
|
|
|
|
Context = autoclass('android.content.Context')
|
|
|
|
from sideband.core import SidebandCore
|
2022-09-28 05:12:14 -06:00
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
else:
|
|
|
|
from sbapp.sideband.core import SidebandCore
|
2022-09-28 05:12:14 -06:00
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
class AppProxy():
|
|
|
|
def __init__(self):
|
2022-09-28 05:12:14 -06:00
|
|
|
pass
|
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
class SidebandService():
|
2022-09-17 08:00:55 -06:00
|
|
|
|
|
|
|
def __init__(self):
|
2022-09-20 09:28:39 -06:00
|
|
|
self.argument = environ.get('PYTHON_SERVICE_ARGUMENT', '')
|
2022-10-01 03:18:14 -06:00
|
|
|
self.app_dir = self.argument
|
2022-09-20 09:28:39 -06:00
|
|
|
self.multicast_lock = None
|
|
|
|
self.wake_lock = None
|
2022-10-01 03:18:14 -06:00
|
|
|
self.should_run = False
|
2022-09-20 09:28:39 -06:00
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
self.app_proxy = AppProxy()
|
|
|
|
|
|
|
|
self.android_service = None
|
|
|
|
self.app_context = None
|
|
|
|
self.wifi_manager = None
|
|
|
|
|
|
|
|
if RNS.vendor.platformutils.get_platform() == "android":
|
|
|
|
self.android_service = autoclass('org.kivy.android.PythonService').mService
|
|
|
|
self.app_context = self.android_service.getApplication().getApplicationContext()
|
|
|
|
self.wifi_manager = self.app_context.getSystemService(Context.WIFI_SERVICE)
|
|
|
|
# The returned instance is an android.net.wifi.WifiManager
|
2022-09-20 09:28:39 -06:00
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
# TODO: Remove?
|
|
|
|
RNS.log("Sideband background service created, starting core...", RNS.LOG_DEBUG)
|
|
|
|
self.sideband = SidebandCore(self.app_proxy, is_service=True, android_app_dir=self.app_dir)
|
|
|
|
self.sideband.start()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.should_run = True
|
2022-09-20 09:28:39 -06:00
|
|
|
self.take_locks()
|
2022-09-17 08:00:55 -06:00
|
|
|
self.run()
|
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
def stop(self):
|
|
|
|
self.should_run = False
|
|
|
|
|
2022-09-20 09:28:39 -06:00
|
|
|
def take_locks(self):
|
2022-10-01 03:18:14 -06:00
|
|
|
if RNS.vendor.platformutils.get_platform() == "android":
|
|
|
|
if self.multicast_lock == None:
|
|
|
|
self.multicast_lock = self.wifi_manager.createMulticastLock("sideband_service")
|
2022-09-20 09:28:39 -06:00
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
if not self.multicast_lock.isHeld():
|
|
|
|
RNS.log("Taking multicast lock")
|
|
|
|
self.multicast_lock.acquire()
|
|
|
|
RNS.log("Took lock")
|
2022-09-20 09:28:39 -06:00
|
|
|
|
|
|
|
|
|
|
|
def release_locks():
|
2022-10-01 03:18:14 -06:00
|
|
|
if RNS.vendor.platformutils.get_platform() == "android":
|
|
|
|
if not self.multicast_lock == None and self.multicast_lock.isHeld():
|
|
|
|
self.multicast_lock.release()
|
2022-09-20 09:28:39 -06:00
|
|
|
|
2022-09-17 08:00:55 -06:00
|
|
|
def run(self):
|
2022-10-01 03:18:14 -06:00
|
|
|
while self.should_run:
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.release_locks()
|
|
|
|
|
|
|
|
sbs = SidebandService()
|
|
|
|
sbs.start()
|
2022-09-17 08:00:55 -06:00
|
|
|
|
2022-10-01 03:18:14 -06:00
|
|
|
# TODO: Remove
|
|
|
|
print("SBS: Service thread done")
|
|
|
|
RNS.log("Service thread done")
|