1+ package io.matthewnelson.sampleapp.topl_android
2+
3+ import android.content.Context
4+ import io.matthewnelson.topl_core_base.BaseConsts.BroadcastType
5+ import io.matthewnelson.topl_service.TorServiceController
6+ import io.matthewnelson.topl_service_base.ServiceExecutionHooks
7+ import kotlinx.coroutines.Dispatchers
8+ import kotlinx.coroutines.delay
9+ import kotlinx.coroutines.withContext
10+
11+ class MyServiceExecutionHooks : ServiceExecutionHooks () {
12+
13+ override suspend fun executeOnCreateTorService (context : Context ) {
14+ // Executed on Dispatchers.Default, so querying shared prefs has to switch
15+ // context to IO.
16+ withContext(Dispatchers .Main ) {
17+ TorServiceController .getServiceTorSettings()
18+ }.let { settings ->
19+ withContext(Dispatchers .IO ) {
20+ settings.hasDebugLogs
21+ }.let { debugLogs ->
22+ if (! debugLogs) {
23+ return
24+ }
25+
26+ withContext(Dispatchers .Main ) {
27+ TorServiceController .appEventBroadcaster?.broadcastDebug(
28+ " ${BroadcastType .DEBUG } |" +
29+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
30+ " onCreateTorService execution hook started"
31+ )
32+
33+ delay(20_000L )
34+
35+ TorServiceController .appEventBroadcaster?.broadcastDebug(
36+ " ${BroadcastType .DEBUG } |" +
37+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
38+ " onCreateTorService execution hook completed"
39+ )
40+ }
41+ }
42+ }
43+ }
44+
45+ override suspend fun executeOnStartCommandBeforeStartTor (context : Context ) {
46+ // Executed on Dispatchers.Default, so querying shared prefs has to switch
47+ // context to IO.
48+ withContext(Dispatchers .Main ) {
49+ TorServiceController .getServiceTorSettings()
50+ }.let { settings ->
51+ withContext(Dispatchers .IO ) {
52+ settings.hasDebugLogs
53+ }.let { debugLogs ->
54+ if (! debugLogs) {
55+ return
56+ }
57+
58+ withContext(Dispatchers .Main ) {
59+ TorServiceController .appEventBroadcaster?.broadcastDebug(
60+ " ${BroadcastType .DEBUG } |" +
61+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
62+ " OnStartCommandBeforeStartTor execution hook started"
63+ )
64+
65+ delay(1_000L )
66+
67+ TorServiceController .appEventBroadcaster?.broadcastDebug(
68+ " ${BroadcastType .DEBUG } |" +
69+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
70+ " OnStartCommandBeforeStartTor execution hook completed"
71+ )
72+ }
73+ }
74+ }
75+ }
76+
77+ override suspend fun executeBeforeStartTor (context : Context ) {
78+ // Executed on Dispatchers.IO
79+ withContext(Dispatchers .Main ) {
80+ TorServiceController .getServiceTorSettings()
81+ }.let { settings ->
82+ if (! settings.hasDebugLogs) {
83+ return
84+ }
85+
86+ withContext(Dispatchers .Main ) {
87+ TorServiceController .appEventBroadcaster?.broadcastDebug(
88+ " ${BroadcastType .DEBUG } |" +
89+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
90+ " BeforeStartTor execution hook started"
91+ )
92+
93+ delay(500L )
94+
95+ TorServiceController .appEventBroadcaster?.broadcastDebug(
96+ " ${BroadcastType .DEBUG } |" +
97+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
98+ " BeforeStartTor execution hook completed"
99+ )
100+ }
101+ }
102+ }
103+
104+ override suspend fun executeAfterStopTor (context : Context ) {
105+ // Executed on Dispatchers.IO
106+ withContext(Dispatchers .Main ) {
107+ TorServiceController .getServiceTorSettings()
108+ }.let { settings ->
109+ if (! settings.hasDebugLogs) {
110+ return
111+ }
112+
113+ withContext(Dispatchers .Main ) {
114+ TorServiceController .appEventBroadcaster?.broadcastDebug(
115+ " ${BroadcastType .DEBUG } |" +
116+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
117+ " AfterStopTor execution hook started"
118+ )
119+
120+ delay(500L )
121+
122+ TorServiceController .appEventBroadcaster?.broadcastDebug(
123+ " ${BroadcastType .DEBUG } |" +
124+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
125+ " AfterStopTor execution hook completed"
126+ )
127+ }
128+ }
129+ }
130+
131+ override suspend fun executeBeforeStoppingService (context : Context ) {
132+ // Executed on Dispatchers.IO
133+ withContext(Dispatchers .Main ) {
134+ TorServiceController .getServiceTorSettings()
135+ }.let { settings ->
136+ if (! settings.hasDebugLogs) {
137+ return
138+ }
139+
140+ withContext(Dispatchers .Main ) {
141+ TorServiceController .appEventBroadcaster?.broadcastDebug(
142+ " ${BroadcastType .DEBUG } |" +
143+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
144+ " BeforeStoppingService execution hook started"
145+ )
146+
147+ delay(2_000L )
148+
149+ TorServiceController .appEventBroadcaster?.broadcastDebug(
150+ " ${BroadcastType .DEBUG } |" +
151+ " ${this @MyServiceExecutionHooks.javaClass.simpleName} |" +
152+ " BeforeStoppingService execution hook completed"
153+ )
154+ }
155+ }
156+ }
157+ }
0 commit comments