Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 96e778d

Browse files
committed
fix kill app method to ensure the Service is stopped properly before killing the application
1 parent d4580bd commit 96e778d

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

sampleapp/src/main/java/io/matthewnelson/sampleapp/ui/fragments/dashboard/DashboardFragment.kt

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ import io.matthewnelson.sampleapp.R
2525
import io.matthewnelson.sampleapp.topl_android.MyEventBroadcaster
2626
import io.matthewnelson.sampleapp.ui.MainActivity
2727
import io.matthewnelson.sampleapp.ui.fragments.settings.library.LibraryPrefs
28-
import io.matthewnelson.topl_core_base.BaseConsts
28+
import io.matthewnelson.topl_core_base.BaseConsts.TorState
2929
import io.matthewnelson.topl_service.TorServiceController
30-
import kotlinx.coroutines.CoroutineScope
31-
import kotlinx.coroutines.Dispatchers
32-
import kotlinx.coroutines.delay
33-
import kotlinx.coroutines.launch
30+
import kotlinx.coroutines.*
3431
import kotlin.system.exitProcess
3532

3633
class DashboardFragment : Fragment() {
@@ -123,23 +120,36 @@ class DashboardFragment : Fragment() {
123120
}
124121
}
125122

123+
private var killAppJob: Job? = null
124+
126125
private fun initButtons(context: Context, owner: LifecycleOwner) {
127126
buttonAppRestart.setOnClickListener {
127+
buttonAppRestart.visibility = View.GONE
128+
128129
Toast.makeText(context, "Killing Application", Toast.LENGTH_LONG).show()
129130
val prefs = Prefs.createUnencrypted(App.PREFS_NAME, context)
130131

131132
TorServiceController.appEventBroadcaster?.let {
132133
(it as MyEventBroadcaster).liveTorState.observe(owner, Observer { data ->
133134
if (data != null) {
134-
var delayLength = 500L + LibraryPrefs.getControllerStopDelaySetting(prefs)
135-
if (data.state == BaseConsts.TorState.OFF)
136-
delayLength = 200L
137-
138-
TorServiceController.stopTor()
139-
CoroutineScope(Dispatchers.Main).launch {
140-
delay(delayLength)
141-
killApplication(context)
135+
136+
when (data.state) {
137+
TorState.ON,
138+
TorState.STARTING -> {
139+
killAppJob?.cancel()
140+
TorServiceController.stopTor()
141+
}
142+
TorState.OFF -> {
143+
if (killAppJob?.isActive == true)
144+
killAppJob?.cancel()
145+
146+
killAppJob = CoroutineScope(Dispatchers.Main).launch {
147+
delay(1000L + LibraryPrefs.getControllerStopDelaySetting(prefs))
148+
killApplication(context)
149+
}
150+
}
142151
}
152+
143153
}
144154
})
145155
}

0 commit comments

Comments
 (0)