'''
@author Tomáš Keske
@since 10.8.2019
'''

import sys
from jnius import autoclass
from Conf.Conf import *

class ServiceBase():

    def __init__(self):

        PythonServiceClass  = autoclass('org.kivy.android.PythonService')
        self.Context = autoclass('android.content.Context')
        self.Service = PythonServiceClass.mService

        #set autorestart to be imune to task swiping on Android 9
        self.Service.setAutoRestartService(True)

        self.confDict = {k: v for k,v in globals().items() if k.isupper() and k.startswith("SMS")}

        for k, v in confDict.items():
            setattr(self, k, v)

    def killGeneric(self, error):

        print(repr(error))
        PythonService.setAutoRestartService(False)
        print("Autorestart of the service disabled.")
        print("Attempting to kill service permanently.")
        PythonService.stop()

        #service takes time to stop. flow thus continues to next block of code
        #sys.exit() is to prevent subsequent code from execution
        #both calls are neccesary to avoid "Scheduling restart of crashed service process"
        #in case we called only sys.exit()
        #this applies even if we have setAutoRestartService(False)
        print("Exiting python script")
        sys.exit()
[(14, 'jnius.autoclass', 'autoclass', False, 'from jnius import autoclass\n'), (15, 'jnius.autoclass', 'autoclass', False, 'from jnius import autoclass\n'), (40, 'sys.exit', 'sys.exit', False, 'import sys\n')]