coroutines: lifecycleScope is by default on Main.immediate

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-09-16 00:00:31 +02:00
parent 49ac61304e
commit 79ae85c728
13 changed files with 23 additions and 29 deletions

View File

@ -10,7 +10,6 @@ import androidx.databinding.CallbackRegistry.NotifierCallback
import androidx.lifecycle.lifecycleScope
import com.wireguard.android.Application
import com.wireguard.android.model.ObservableTunnel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
/**
@ -39,7 +38,7 @@ abstract class BaseActivity : ThemeChangeAwareActivity() {
else -> null
}
if (savedTunnelName != null)
lifecycleScope.launch(Dispatchers.Main.immediate) { selectedTunnel = Application.getTunnelManager().getTunnels()[savedTunnelName] }
lifecycleScope.launch { selectedTunnel = Application.getTunnelManager().getTunnels()[savedTunnelName] }
// The selected tunnel must be set before the superclass method recreates fragments.
super.onCreate(savedInstanceState)

View File

@ -102,7 +102,7 @@ class SettingsActivity : ThemeChangeAwareActivity() {
preferenceManager.findPreference<Preference>("multiple_tunnels")
).filterNotNull()
wgQuickOnlyPrefs.forEach { it.isVisible = false }
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
if (Application.getBackend() is WgQuickBackend) {
++preferenceScreen.initialExpandedChildrenCount
wgQuickOnlyPrefs.forEach { it.isVisible = true }
@ -121,7 +121,7 @@ class SettingsActivity : ThemeChangeAwareActivity() {
moduleInstaller?.parent?.removePreference(moduleInstaller)
} else {
kernelModuleDisabler?.parent?.removePreference(kernelModuleDisabler)
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
withContext(Dispatchers.IO) { Application.getRootShell().start() }
moduleInstaller?.isVisible = true

View File

@ -18,7 +18,6 @@ import com.wireguard.android.QuickTileService
import com.wireguard.android.R
import com.wireguard.android.backend.Tunnel
import com.wireguard.android.util.ErrorMessages
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@RequiresApi(Build.VERSION_CODES.N)
@ -26,7 +25,7 @@ class TunnelToggleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val tunnel = Application.getTunnelManager().lastUsedTunnel ?: return
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
tunnel.setStateAsync(Tunnel.State.TOGGLE)
} catch (e: Throwable) {

View File

@ -24,7 +24,6 @@ import com.wireguard.android.databinding.TunnelDetailFragmentBinding
import com.wireguard.android.databinding.TunnelListItemBinding
import com.wireguard.android.model.ObservableTunnel
import com.wireguard.android.util.ErrorMessages
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
/**
@ -72,7 +71,7 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
is TunnelListItemBinding -> binding.item
else -> return
} ?: return
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
if (Application.getBackend() is GoBackend) {
val intent = GoBackend.VpnService.prepare(view.context)
if (intent != null) {
@ -87,7 +86,7 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
}
private fun setTunnelStateWithPermissionsResult(tunnel: ObservableTunnel, checked: Boolean) {
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
tunnel.setStateAsync(Tunnel.State.of(checked))
} catch (e: Throwable) {

View File

@ -17,7 +17,6 @@ import com.wireguard.android.R
import com.wireguard.android.databinding.ConfigNamingDialogFragmentBinding
import com.wireguard.config.BadConfigException
import com.wireguard.config.Config
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.ByteArrayInputStream
import java.io.IOException
@ -31,7 +30,7 @@ class ConfigNamingDialogFragment : DialogFragment() {
private fun createTunnelAndDismiss() {
binding?.let {
val name = it.tunnelNameText.text.toString()
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
Application.getTunnelManager().create(name, config)
dismiss()

View File

@ -19,7 +19,6 @@ import com.wireguard.android.databinding.TunnelDetailPeerBinding
import com.wireguard.android.model.ObservableTunnel
import com.wireguard.android.widget.EdgeToEdge.setUpRoot
import com.wireguard.android.widget.EdgeToEdge.setUpScrollingContent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import java.util.TimerTask
@ -82,7 +81,7 @@ class TunnelDetailFragment : BaseFragment() {
override fun onSelectedTunnelChanged(oldTunnel: ObservableTunnel?, newTunnel: ObservableTunnel?) {
binding ?: return
binding!!.tunnel = newTunnel
if (newTunnel == null) binding!!.config = null else lifecycleScope.launch(Dispatchers.Main.immediate) {
if (newTunnel == null) binding!!.config = null else lifecycleScope.launch {
try {
binding!!.config = newTunnel.getConfigAsync()
} catch (_: Throwable) {
@ -114,7 +113,7 @@ class TunnelDetailFragment : BaseFragment() {
val state = tunnel.state
if (state != Tunnel.State.UP && lastState == state) return
lastState = state
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
val statistics = tunnel.getStatisticsAsync()
for (i in 0 until binding!!.peersLayout.childCount) {

View File

@ -33,7 +33,6 @@ import com.wireguard.android.viewmodel.ConfigProxy
import com.wireguard.android.widget.EdgeToEdge.setUpRoot
import com.wireguard.android.widget.EdgeToEdge.setUpScrollingContent
import com.wireguard.config.Config
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
/**
@ -141,7 +140,7 @@ class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
Snackbar.make(binding!!.mainContainer, error, Snackbar.LENGTH_LONG).show()
return false
}
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
when {
tunnel == null -> {
Log.d(TAG, "Attempting to create new tunnel " + binding!!.name)
@ -205,7 +204,7 @@ class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
binding!!.config = ConfigProxy()
if (tunnel != null) {
binding!!.name = tunnel!!.name
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
onConfigLoaded(tunnel!!.getConfigAsync())
} catch (_: Throwable) {
@ -242,7 +241,7 @@ class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
Log.d(TAG, message)
// Now save the rest of configuration changes.
Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel!!.name)
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
renamedTunnel.setConfigAsync(newConfig)
onConfigSaved(renamedTunnel, null)

View File

@ -74,7 +74,7 @@ class TunnelListFragment : BaseFragment() {
}
private fun importTunnel(uri: Uri?) {
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
withContext(Dispatchers.IO) {
val activity = activity
if (activity == null || uri == null) {
@ -264,7 +264,7 @@ class TunnelListFragment : BaseFragment() {
super.onViewStateRestored(savedInstanceState)
binding ?: return
binding!!.fragment = this
lifecycleScope.launch(Dispatchers.Main.immediate) { binding!!.tunnels = Application.getTunnelManager().getTunnels() }
lifecycleScope.launch { binding!!.tunnels = Application.getTunnelManager().getTunnels() }
binding!!.rowConfigurationHandler = object : RowConfigurationHandler<TunnelListItemBinding, ObservableTunnel> {
override fun onConfigureRow(binding: TunnelListItemBinding, item: ObservableTunnel, position: Int) {
binding.fragment = this@TunnelListFragment
@ -316,7 +316,7 @@ class TunnelListFragment : BaseFragment() {
scaleX = 1f
scaleY = 1f
}
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
val tunnels = Application.getTunnelManager().getTunnels()
val tunnelsToDelete = ArrayList<ObservableTunnel>()
@ -332,7 +332,7 @@ class TunnelListFragment : BaseFragment() {
true
}
R.id.menu_action_select_all -> {
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
val tunnels = Application.getTunnelManager().getTunnels()
for (i in 0 until tunnels.size) {
setItemChecked(i, true)

View File

@ -28,7 +28,7 @@ class KernelModuleDisablerPreference(context: Context, attrs: AttributeSet?) : P
private var state = State.UNKNOWN
init {
isVisible = false
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
setState(if (Application.getBackend() is WgQuickBackend) State.ENABLED else State.DISABLED)
}
}
@ -46,7 +46,7 @@ class KernelModuleDisablerPreference(context: Context, attrs: AttributeSet?) : P
setState(State.DISABLING)
Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", true).commit()
}
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
val observableTunnels = Application.getTunnelManager().getTunnels()
val downings = observableTunnels.map { async(SupervisorJob()) { it.setStateAsync(Tunnel.State.DOWN) } }
try {

View File

@ -30,7 +30,7 @@ class ModuleDownloaderPreference(context: Context, attrs: AttributeSet?) : Prefe
@SuppressLint("ApplySharedPref")
override fun onClick() {
setState(State.WORKING)
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
when (withContext(Dispatchers.IO) { Application.getModuleLoader().download() }) {
OsConstants.ENOENT -> setState(State.NOTFOUND)

View File

@ -27,7 +27,7 @@ class ToolsInstallerPreference(context: Context, attrs: AttributeSet?) : Prefere
override fun onAttached() {
super.onAttached()
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
val state = withContext(Dispatchers.IO) { Application.getToolsInstaller().areInstalled() }
when {
@ -45,7 +45,7 @@ class ToolsInstallerPreference(context: Context, attrs: AttributeSet?) : Prefere
override fun onClick() {
setState(State.WORKING)
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
try {
val result = withContext(Dispatchers.IO) { Application.getToolsInstaller().install() }
when {

View File

@ -47,7 +47,7 @@ class VersionPreference(context: Context, attrs: AttributeSet?) : Preference(con
}
init {
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
val backend = Application.getBackend()
versionSummary = getContext().getString(R.string.version_summary_checking, getBackendPrettyName(context, backend).toLowerCase(Locale.ENGLISH))
notifyChanged()

View File

@ -35,7 +35,7 @@ import java.util.zip.ZipOutputStream
class ZipExporterPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) {
private var exportedFilePath: String? = null
private fun exportZip() {
lifecycleScope.launch(Dispatchers.Main.immediate) {
lifecycleScope.launch {
val tunnels = Application.getTunnelManager().getTunnels()
try {
exportedFilePath = withContext(Dispatchers.IO) {