Use Kotlin coroutine instead of Java parallelStream

This commit is contained in:
topjohnwu 2021-11-04 19:57:22 -07:00 committed by John Wu
parent 6723d20616
commit a3695cc66b

View File

@ -13,10 +13,10 @@ import com.topjohnwu.magisk.databinding.itemBindingOf
import com.topjohnwu.magisk.di.AppContext import com.topjohnwu.magisk.di.AppContext
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.launch import kotlinx.coroutines.flow.*
import kotlinx.coroutines.withContext import java.util.*
import java.util.stream.Collectors import kotlin.collections.ArrayList
class DenyListViewModel : BaseViewModel(), Queryable { class DenyListViewModel : BaseViewModel(), Queryable {
@ -55,17 +55,19 @@ class DenyListViewModel : BaseViewModel(), Queryable {
return@launch return@launch
} }
state = State.LOADING state = State.LOADING
val (apps, diff) = withContext(Dispatchers.Default) { val (apps, diff) = withContext(Dispatchers.IO) {
val pm = AppContext.packageManager val pm = AppContext.packageManager
val denyList = Shell.su("magisk --denylist ls").exec().out val denyList = Shell.su("magisk --denylist ls").exec().out
.map { CmdlineListItem(it) } .map { CmdlineListItem(it) }
val apps = pm.getInstalledApplications(MATCH_UNINSTALLED_PACKAGES).parallelStream() val apps = pm.getInstalledApplications(MATCH_UNINSTALLED_PACKAGES).asFlow()
.filter { AppContext.packageName != it.packageName } .filter { AppContext.packageName != it.packageName }
.map { AppProcessInfo(it, pm, denyList) } .map { async { AppProcessInfo(it, pm, denyList) } }
.map { it.await() }
.filter { it.processes.isNotEmpty() } .filter { it.processes.isNotEmpty() }
.map { DenyListRvItem(it) } .map { async { DenyListRvItem(it) } }
.sorted() .map { it.await() }
.collect(Collectors.toList()) .toCollection(ArrayList())
apps.sort()
apps to items.calculateDiff(apps) apps to items.calculateDiff(apps)
} }
items.update(apps, diff) items.update(apps, diff)