Commit 9f934937 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added different contexts for coroutine scopes where appropriate

parent 1f41cef3
......@@ -32,25 +32,25 @@ import java.io.FileWriter
class DnsQueryRepository(private val dnsQueryDao: DnsQueryDao) {
fun updateAsync(dnsQuery: DnsQuery): Job {
return GlobalScope.launch {
return GlobalScope.launch(Dispatchers.IO) {
dnsQueryDao.update(dnsQuery)
}
}
fun insertAllAsync(dnsQueries:List<DnsQuery>): Job {
return GlobalScope.launch {
return GlobalScope.launch(Dispatchers.IO) {
dnsQueryDao.insertAll(dnsQueries)
}
}
fun insertAsync(dnsQuery:DnsQuery): Job {
return GlobalScope.launch {
return GlobalScope.launch(Dispatchers.IO) {
dnsQueryDao.insert(dnsQuery)
}
}
suspend fun getAllAsync(coroutineScope: CoroutineScope): List<DnsQuery> {
return coroutineScope.async(start = CoroutineStart.DEFAULT) {
return coroutineScope.async(Dispatchers.IO, start = CoroutineStart.DEFAULT) {
dnsQueryDao.getAll()
}.await()
}
......@@ -66,7 +66,7 @@ class DnsQueryRepository(private val dnsQueryDao: DnsQueryDao) {
exportFile.delete()
false
} else exportFile.exists()
return GlobalScope.launch {
return GlobalScope.launch(Dispatchers.IO) {
val outStream = BufferedWriter(FileWriter(exportFile, true))
if(!existed) {
outStream.write("Name,Short Name,Type Name,Type ID,Asked Server,Answered from Cache,Question time,Response Time,Responses(JSON-Array of Base64)")
......
......@@ -13,10 +13,7 @@ import com.frostnerd.smokescreen.log
import com.frostnerd.smokescreen.service.DnsVpnService
import kotlinx.android.synthetic.main.dialog_query_generator.*
import kotlinx.android.synthetic.main.dialog_query_generator.view.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
......@@ -346,6 +343,9 @@ class QueryGeneratorDialog(context: Context):AlertDialog(context, context.getPre
"https://www.bild.de/video/clip/elefant/elefant-haengt-ueber-zaun-viralpress-61819274.bild.html"
)
private var job: Job? = null
private val generatorScope:CoroutineScope by lazy {
CoroutineScope(newSingleThreadContext("query-generator"))
}
private var loadingDialog:AlertDialog? = null
init {
......@@ -369,7 +369,7 @@ class QueryGeneratorDialog(context: Context):AlertDialog(context, context.getPre
}
val restartVpn = view.restartVpn.isChecked
val runCount = iterations.text.toString().toIntOrNull() ?: 1
job = GlobalScope.launch {
job = generatorScope!!.launch {
context.log("Generating queries for ${urlsToUse.size} urls $runCount times", "[QueryGenerator]")
val logFileWriter = BufferedWriter(FileWriter(File(context.filesDir, "querygenlog.txt"), true))
val callWithChrome = useChrome.isChecked
......@@ -392,6 +392,7 @@ class QueryGeneratorDialog(context: Context):AlertDialog(context, context.getPre
}
}
job = null
generatorScope?.cancel()
loadingDialog?.cancel()
}
showLoadingDialog()
......
......@@ -225,6 +225,7 @@ class DnsRuleFragment : Fragment() {
notifyRulesChanged()
}, editSource = { hostSource ->
NewHostSourceDialog(context!!, onSourceCreated = { newSource ->
val currentSource = getDatabase().hostSourceDao().findById(hostSource.id)!!.apply {
this.name = newSource.name
this.source = newSource.source
......
......@@ -165,7 +165,7 @@ class QueryLogDetailFragment : Fragment() {
if(query.responseSource == QueryListener.Source.LOCALRESOLVER) {
hostSourceWrap.visibility = View.VISIBLE
hostSourceFetchJob = GlobalScope.launch {
hostSourceFetchJob = GlobalScope.launch(Dispatchers.IO) {
val sourceRule = getDatabase().dnsRuleDao().findRuleTargetEntity(query.name, query.type, true)
?: getDatabase().dnsRuleDao().findPossibleWildcardRuleTarget(query.name, query. type, true, false, true).firstOrNull {
DnsRuleDialog.databaseHostToMatcher(it.host).reset(query.name).matches()
......
......@@ -7,6 +7,7 @@ import com.frostnerd.smokescreen.database.getDatabase
import com.frostnerd.smokescreen.dialog.DnsRuleDialog
import com.frostnerd.smokescreen.getPreferences
import com.frostnerd.smokescreen.util.MaxSizeMap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
......@@ -49,7 +50,7 @@ class DnsRuleResolver(context: Context) : LocalResolver(false) {
fun refreshRuleCount() {
previousRefreshJob?.cancel()
previousRefreshJob = GlobalScope.launch {
previousRefreshJob = GlobalScope.launch(Dispatchers.IO) {
val previousRuleCount = ruleCount
ruleCount = dao.getActiveCount().toInt()
wildcardCount = dao.getActiveWildcardCount().toInt()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment