Use life-cycle scope / isActive / yield to properly cancel background job

This commit is contained in:
Johannes Marbach 2022-06-14 14:09:48 +02:00
parent 16ac259f4a
commit afcdf0b56f

View File

@ -39,6 +39,7 @@ import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.view.isInvisible import androidx.core.view.isInvisible
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
@ -59,18 +60,15 @@ import im.vector.app.features.webview.WebEventListener
import im.vector.app.features.widgets.webview.WebviewPermissionUtils import im.vector.app.features.widgets.webview.WebviewPermissionUtils
import im.vector.app.features.widgets.webview.clearAfterWidget import im.vector.app.features.widgets.webview.clearAfterWidget
import im.vector.app.features.widgets.webview.setupForWidget import im.vector.app.features.widgets.webview.setupForWidget
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.isActive
import kotlinx.coroutines.async
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import org.json.JSONObject
import org.matrix.android.sdk.api.session.terms.TermsService import org.matrix.android.sdk.api.session.terms.TermsService
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
import java.io.InputStream
import java.net.URISyntaxException import java.net.URISyntaxException
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
@ -424,9 +422,8 @@ class WidgetFragment @Inject constructor(
@OptIn(DelicateCoroutinesApi::class) lifecycleScope.launch {
GlobalScope.launch { withContext(Dispatchers.IO) {
async(Dispatchers.IO) {
var inputStream = bluetoothSocket?.inputStream var inputStream = bluetoothSocket?.inputStream
val inputBuffer = ByteArray(1024) val inputBuffer = ByteArray(1024)
@ -441,7 +438,7 @@ class WidgetFragment @Inject constructor(
//informInWebView("Opened RFCOMM socket") //informInWebView("Opened RFCOMM socket")
while (true) { while (isActive) {
/*if (bluetoothSocket?.isConnected != true) { /*if (bluetoothSocket?.isConnected != true) {
continue continue
}*/ }*/
@ -458,6 +455,7 @@ class WidgetFragment @Inject constructor(
//val msg = JSONObject() //val msg = JSONObject()
//msg.put("pttbutton", true) //msg.put("pttbutton", true)
yield()
requireActivity().runOnUiThread { requireActivity().runOnUiThread {
//views.widgetWebView.postWebMessage(WebMessage(msg.toString()), widgetUri) //views.widgetWebView.postWebMessage(WebMessage(msg.toString()), widgetUri)
views.widgetWebView.postWebMessage(WebMessage("pttp"), widgetUri) views.widgetWebView.postWebMessage(WebMessage("pttp"), widgetUri)
@ -466,12 +464,14 @@ class WidgetFragment @Inject constructor(
//informInWebView("ptt up") //informInWebView("ptt up")
//val msg = JSONObject() //val msg = JSONObject()
//msg.put("pttbutton", false) //msg.put("pttbutton", false)
yield()
requireActivity().runOnUiThread { requireActivity().runOnUiThread {
//views.widgetWebView.postWebMessage(WebMessage(msg.toString()), widgetUri) //views.widgetWebView.postWebMessage(WebMessage(msg.toString()), widgetUri)
views.widgetWebView.postWebMessage(WebMessage("pttr"), widgetUri) views.widgetWebView.postWebMessage(WebMessage("pttr"), widgetUri)
} }
} }
} catch (e: IOException) { } catch (e: IOException) {
yield()
informInWebView("Failed to read from socket: $e") informInWebView("Failed to read from socket: $e")
break break
} }