Skip to content

topl-service-base / io.matthewnelson.topl_service_base / ServiceExecutionHooks

ServiceExecutionHooks

abstract class ServiceExecutionHooks (source)

Set Hooks to be executed from TorService.

class MyServiceExecutionHooks: ServiceExecutionHooks() {

    override suspend fun executeOnCreateTorService(context: Context) {
        // Executed on Dispatchers.Default, so querying shared prefs has to switch
        // context to IO.
        withContext(Dispatchers.Main) {
            TorServiceController.getServiceTorSettings()
        }.let { settings ->
            withContext(Dispatchers.IO) {
                settings.hasDebugLogs
            }.let { debugLogs ->
                if (!debugLogs) {
                    return
                }

                withContext(Dispatchers.Main) {
                    TorServiceController.appEventBroadcaster?.broadcastDebug(
                        "${BroadcastType.DEBUG}|" +
                                "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                                "onCreateTorService execution hook started"
                    )

                    delay(20_000L)

                    TorServiceController.appEventBroadcaster?.broadcastDebug(
                        "${BroadcastType.DEBUG}|" +
                                "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                                "onCreateTorService execution hook completed"
                    )
                }
            }
        }
    }

    override suspend fun executeOnStartCommandBeforeStartTor(context: Context) {
        // Executed on Dispatchers.Default, so querying shared prefs has to switch
        // context to IO.
        withContext(Dispatchers.Main) {
            TorServiceController.getServiceTorSettings()
        }.let { settings ->
            withContext(Dispatchers.IO) {
                settings.hasDebugLogs
            }.let { debugLogs ->
                if (!debugLogs) {
                    return
                }

                withContext(Dispatchers.Main) {
                    TorServiceController.appEventBroadcaster?.broadcastDebug(
                        "${BroadcastType.DEBUG}|" +
                                "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                                "OnStartCommandBeforeStartTor execution hook started"
                    )

                    delay(1_000L)

                    TorServiceController.appEventBroadcaster?.broadcastDebug(
                        "${BroadcastType.DEBUG}|" +
                                "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                                "OnStartCommandBeforeStartTor execution hook completed"
                    )
                }
            }
        }
    }

    override suspend fun executeBeforeStartTor(context: Context) {
        // Executed on Dispatchers.IO
        withContext(Dispatchers.Main) {
            TorServiceController.getServiceTorSettings()
        }.let { settings ->
            if (!settings.hasDebugLogs) {
                return
            }

            withContext(Dispatchers.Main) {
                TorServiceController.appEventBroadcaster?.broadcastDebug(
                    "${BroadcastType.DEBUG}|" +
                            "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                            "BeforeStartTor execution hook started"
                )

                delay(500L)

                TorServiceController.appEventBroadcaster?.broadcastDebug(
                    "${BroadcastType.DEBUG}|" +
                            "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                            "BeforeStartTor execution hook completed"
                )
            }
        }
    }

    override suspend fun executeAfterStopTor(context: Context) {
        // Executed on Dispatchers.IO
        withContext(Dispatchers.Main) {
            TorServiceController.getServiceTorSettings()
        }.let { settings ->
            if (!settings.hasDebugLogs) {
                return
            }

            withContext(Dispatchers.Main) {
                TorServiceController.appEventBroadcaster?.broadcastDebug(
                    "${BroadcastType.DEBUG}|" +
                            "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                            "AfterStopTor execution hook started"
                )

                delay(500L)

                TorServiceController.appEventBroadcaster?.broadcastDebug(
                    "${BroadcastType.DEBUG}|" +
                            "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                            "AfterStopTor execution hook completed"
                )
            }
        }
    }

    override suspend fun executeBeforeStoppingService(context: Context) {
        // Executed on Dispatchers.IO
        withContext(Dispatchers.Main) {
            TorServiceController.getServiceTorSettings()
        }.let { settings ->
            if (!settings.hasDebugLogs) {
                return
            }

            withContext(Dispatchers.Main) {
                TorServiceController.appEventBroadcaster?.broadcastDebug(
                    "${BroadcastType.DEBUG}|" +
                            "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                            "BeforeStoppingService execution hook started"
                )

                delay(2_000L)

                TorServiceController.appEventBroadcaster?.broadcastDebug(
                    "${BroadcastType.DEBUG}|" +
                            "${this@MyServiceExecutionHooks.javaClass.simpleName}|" +
                            "BeforeStoppingService execution hook completed"
                )
            }
        }
    }
}

Constructors

Name Summary
<init> Set Hooks to be executed from TorService.ServiceExecutionHooks()

Functions

Name Summary
executeAfterStopTor Is executed from TorService.stopTor on Dispatchers.IOopen suspend fun executeAfterStopTor(context:Context):Unit
executeBeforeStartTor Is executed from TorService.startTor on Dispatchers.IOopen suspend fun executeBeforeStartTor(context:Context):Unit
executeBeforeStoppingService Is executed from TorService.stopService on Dispatchers.IOopen suspend fun executeBeforeStoppingService(context:Context):Unit
executeOnCreateTorService Is executed from TorService.onCreate on Dispatchers.Default launched from it’s own coroutine.open suspend fun executeOnCreateTorService(context:Context):Unit
executeOnStartCommandBeforeStartTor Is executed from TorService.onStartCommand on Dispatchers.Default. This works in conjunction with executeBeforeStoppingService such that if executeBeforeStoppingService is active, executeOnStartCommandBeforeStartTor will suspend until that has been completed, then execute, then start Tor.open suspend fun executeOnStartCommandBeforeStartTor(context:Context):Unit