coroutines: lifecycleScope is by default on Main.immediate
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
49ac61304e
commit
79ae85c728
@ -10,7 +10,6 @@ import androidx.databinding.CallbackRegistry.NotifierCallback
|
|||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.wireguard.android.Application
|
import com.wireguard.android.Application
|
||||||
import com.wireguard.android.model.ObservableTunnel
|
import com.wireguard.android.model.ObservableTunnel
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,7 +38,7 @@ abstract class BaseActivity : ThemeChangeAwareActivity() {
|
|||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
if (savedTunnelName != 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.
|
// The selected tunnel must be set before the superclass method recreates fragments.
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
@ -102,7 +102,7 @@ class SettingsActivity : ThemeChangeAwareActivity() {
|
|||||||
preferenceManager.findPreference<Preference>("multiple_tunnels")
|
preferenceManager.findPreference<Preference>("multiple_tunnels")
|
||||||
).filterNotNull()
|
).filterNotNull()
|
||||||
wgQuickOnlyPrefs.forEach { it.isVisible = false }
|
wgQuickOnlyPrefs.forEach { it.isVisible = false }
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
if (Application.getBackend() is WgQuickBackend) {
|
if (Application.getBackend() is WgQuickBackend) {
|
||||||
++preferenceScreen.initialExpandedChildrenCount
|
++preferenceScreen.initialExpandedChildrenCount
|
||||||
wgQuickOnlyPrefs.forEach { it.isVisible = true }
|
wgQuickOnlyPrefs.forEach { it.isVisible = true }
|
||||||
@ -121,7 +121,7 @@ class SettingsActivity : ThemeChangeAwareActivity() {
|
|||||||
moduleInstaller?.parent?.removePreference(moduleInstaller)
|
moduleInstaller?.parent?.removePreference(moduleInstaller)
|
||||||
} else {
|
} else {
|
||||||
kernelModuleDisabler?.parent?.removePreference(kernelModuleDisabler)
|
kernelModuleDisabler?.parent?.removePreference(kernelModuleDisabler)
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
withContext(Dispatchers.IO) { Application.getRootShell().start() }
|
withContext(Dispatchers.IO) { Application.getRootShell().start() }
|
||||||
moduleInstaller?.isVisible = true
|
moduleInstaller?.isVisible = true
|
||||||
|
@ -18,7 +18,6 @@ import com.wireguard.android.QuickTileService
|
|||||||
import com.wireguard.android.R
|
import com.wireguard.android.R
|
||||||
import com.wireguard.android.backend.Tunnel
|
import com.wireguard.android.backend.Tunnel
|
||||||
import com.wireguard.android.util.ErrorMessages
|
import com.wireguard.android.util.ErrorMessages
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.N)
|
@RequiresApi(Build.VERSION_CODES.N)
|
||||||
@ -26,7 +25,7 @@ class TunnelToggleActivity : AppCompatActivity() {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
val tunnel = Application.getTunnelManager().lastUsedTunnel ?: return
|
val tunnel = Application.getTunnelManager().lastUsedTunnel ?: return
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
tunnel.setStateAsync(Tunnel.State.TOGGLE)
|
tunnel.setStateAsync(Tunnel.State.TOGGLE)
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
|
@ -24,7 +24,6 @@ import com.wireguard.android.databinding.TunnelDetailFragmentBinding
|
|||||||
import com.wireguard.android.databinding.TunnelListItemBinding
|
import com.wireguard.android.databinding.TunnelListItemBinding
|
||||||
import com.wireguard.android.model.ObservableTunnel
|
import com.wireguard.android.model.ObservableTunnel
|
||||||
import com.wireguard.android.util.ErrorMessages
|
import com.wireguard.android.util.ErrorMessages
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -72,7 +71,7 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
|
|||||||
is TunnelListItemBinding -> binding.item
|
is TunnelListItemBinding -> binding.item
|
||||||
else -> return
|
else -> return
|
||||||
} ?: return
|
} ?: return
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
if (Application.getBackend() is GoBackend) {
|
if (Application.getBackend() is GoBackend) {
|
||||||
val intent = GoBackend.VpnService.prepare(view.context)
|
val intent = GoBackend.VpnService.prepare(view.context)
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
@ -87,7 +86,7 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun setTunnelStateWithPermissionsResult(tunnel: ObservableTunnel, checked: Boolean) {
|
private fun setTunnelStateWithPermissionsResult(tunnel: ObservableTunnel, checked: Boolean) {
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
tunnel.setStateAsync(Tunnel.State.of(checked))
|
tunnel.setStateAsync(Tunnel.State.of(checked))
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
|
@ -17,7 +17,6 @@ import com.wireguard.android.R
|
|||||||
import com.wireguard.android.databinding.ConfigNamingDialogFragmentBinding
|
import com.wireguard.android.databinding.ConfigNamingDialogFragmentBinding
|
||||||
import com.wireguard.config.BadConfigException
|
import com.wireguard.config.BadConfigException
|
||||||
import com.wireguard.config.Config
|
import com.wireguard.config.Config
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@ -31,7 +30,7 @@ class ConfigNamingDialogFragment : DialogFragment() {
|
|||||||
private fun createTunnelAndDismiss() {
|
private fun createTunnelAndDismiss() {
|
||||||
binding?.let {
|
binding?.let {
|
||||||
val name = it.tunnelNameText.text.toString()
|
val name = it.tunnelNameText.text.toString()
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
Application.getTunnelManager().create(name, config)
|
Application.getTunnelManager().create(name, config)
|
||||||
dismiss()
|
dismiss()
|
||||||
|
@ -19,7 +19,6 @@ import com.wireguard.android.databinding.TunnelDetailPeerBinding
|
|||||||
import com.wireguard.android.model.ObservableTunnel
|
import com.wireguard.android.model.ObservableTunnel
|
||||||
import com.wireguard.android.widget.EdgeToEdge.setUpRoot
|
import com.wireguard.android.widget.EdgeToEdge.setUpRoot
|
||||||
import com.wireguard.android.widget.EdgeToEdge.setUpScrollingContent
|
import com.wireguard.android.widget.EdgeToEdge.setUpScrollingContent
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.util.Timer
|
import java.util.Timer
|
||||||
import java.util.TimerTask
|
import java.util.TimerTask
|
||||||
@ -82,7 +81,7 @@ class TunnelDetailFragment : BaseFragment() {
|
|||||||
override fun onSelectedTunnelChanged(oldTunnel: ObservableTunnel?, newTunnel: ObservableTunnel?) {
|
override fun onSelectedTunnelChanged(oldTunnel: ObservableTunnel?, newTunnel: ObservableTunnel?) {
|
||||||
binding ?: return
|
binding ?: return
|
||||||
binding!!.tunnel = newTunnel
|
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 {
|
try {
|
||||||
binding!!.config = newTunnel.getConfigAsync()
|
binding!!.config = newTunnel.getConfigAsync()
|
||||||
} catch (_: Throwable) {
|
} catch (_: Throwable) {
|
||||||
@ -114,7 +113,7 @@ class TunnelDetailFragment : BaseFragment() {
|
|||||||
val state = tunnel.state
|
val state = tunnel.state
|
||||||
if (state != Tunnel.State.UP && lastState == state) return
|
if (state != Tunnel.State.UP && lastState == state) return
|
||||||
lastState = state
|
lastState = state
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
val statistics = tunnel.getStatisticsAsync()
|
val statistics = tunnel.getStatisticsAsync()
|
||||||
for (i in 0 until binding!!.peersLayout.childCount) {
|
for (i in 0 until binding!!.peersLayout.childCount) {
|
||||||
|
@ -33,7 +33,6 @@ import com.wireguard.android.viewmodel.ConfigProxy
|
|||||||
import com.wireguard.android.widget.EdgeToEdge.setUpRoot
|
import com.wireguard.android.widget.EdgeToEdge.setUpRoot
|
||||||
import com.wireguard.android.widget.EdgeToEdge.setUpScrollingContent
|
import com.wireguard.android.widget.EdgeToEdge.setUpScrollingContent
|
||||||
import com.wireguard.config.Config
|
import com.wireguard.config.Config
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -141,7 +140,7 @@ class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
|
|||||||
Snackbar.make(binding!!.mainContainer, error, Snackbar.LENGTH_LONG).show()
|
Snackbar.make(binding!!.mainContainer, error, Snackbar.LENGTH_LONG).show()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
when {
|
when {
|
||||||
tunnel == null -> {
|
tunnel == null -> {
|
||||||
Log.d(TAG, "Attempting to create new tunnel " + binding!!.name)
|
Log.d(TAG, "Attempting to create new tunnel " + binding!!.name)
|
||||||
@ -205,7 +204,7 @@ class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
|
|||||||
binding!!.config = ConfigProxy()
|
binding!!.config = ConfigProxy()
|
||||||
if (tunnel != null) {
|
if (tunnel != null) {
|
||||||
binding!!.name = tunnel!!.name
|
binding!!.name = tunnel!!.name
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
onConfigLoaded(tunnel!!.getConfigAsync())
|
onConfigLoaded(tunnel!!.getConfigAsync())
|
||||||
} catch (_: Throwable) {
|
} catch (_: Throwable) {
|
||||||
@ -242,7 +241,7 @@ class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
|
|||||||
Log.d(TAG, message)
|
Log.d(TAG, message)
|
||||||
// Now save the rest of configuration changes.
|
// Now save the rest of configuration changes.
|
||||||
Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel!!.name)
|
Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel!!.name)
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
renamedTunnel.setConfigAsync(newConfig)
|
renamedTunnel.setConfigAsync(newConfig)
|
||||||
onConfigSaved(renamedTunnel, null)
|
onConfigSaved(renamedTunnel, null)
|
||||||
|
@ -74,7 +74,7 @@ class TunnelListFragment : BaseFragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun importTunnel(uri: Uri?) {
|
private fun importTunnel(uri: Uri?) {
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
val activity = activity
|
val activity = activity
|
||||||
if (activity == null || uri == null) {
|
if (activity == null || uri == null) {
|
||||||
@ -264,7 +264,7 @@ class TunnelListFragment : BaseFragment() {
|
|||||||
super.onViewStateRestored(savedInstanceState)
|
super.onViewStateRestored(savedInstanceState)
|
||||||
binding ?: return
|
binding ?: return
|
||||||
binding!!.fragment = this
|
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> {
|
binding!!.rowConfigurationHandler = object : RowConfigurationHandler<TunnelListItemBinding, ObservableTunnel> {
|
||||||
override fun onConfigureRow(binding: TunnelListItemBinding, item: ObservableTunnel, position: Int) {
|
override fun onConfigureRow(binding: TunnelListItemBinding, item: ObservableTunnel, position: Int) {
|
||||||
binding.fragment = this@TunnelListFragment
|
binding.fragment = this@TunnelListFragment
|
||||||
@ -316,7 +316,7 @@ class TunnelListFragment : BaseFragment() {
|
|||||||
scaleX = 1f
|
scaleX = 1f
|
||||||
scaleY = 1f
|
scaleY = 1f
|
||||||
}
|
}
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
val tunnels = Application.getTunnelManager().getTunnels()
|
val tunnels = Application.getTunnelManager().getTunnels()
|
||||||
val tunnelsToDelete = ArrayList<ObservableTunnel>()
|
val tunnelsToDelete = ArrayList<ObservableTunnel>()
|
||||||
@ -332,7 +332,7 @@ class TunnelListFragment : BaseFragment() {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
R.id.menu_action_select_all -> {
|
R.id.menu_action_select_all -> {
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
val tunnels = Application.getTunnelManager().getTunnels()
|
val tunnels = Application.getTunnelManager().getTunnels()
|
||||||
for (i in 0 until tunnels.size) {
|
for (i in 0 until tunnels.size) {
|
||||||
setItemChecked(i, true)
|
setItemChecked(i, true)
|
||||||
|
@ -28,7 +28,7 @@ class KernelModuleDisablerPreference(context: Context, attrs: AttributeSet?) : P
|
|||||||
private var state = State.UNKNOWN
|
private var state = State.UNKNOWN
|
||||||
init {
|
init {
|
||||||
isVisible = false
|
isVisible = false
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
setState(if (Application.getBackend() is WgQuickBackend) State.ENABLED else State.DISABLED)
|
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)
|
setState(State.DISABLING)
|
||||||
Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", true).commit()
|
Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", true).commit()
|
||||||
}
|
}
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
val observableTunnels = Application.getTunnelManager().getTunnels()
|
val observableTunnels = Application.getTunnelManager().getTunnels()
|
||||||
val downings = observableTunnels.map { async(SupervisorJob()) { it.setStateAsync(Tunnel.State.DOWN) } }
|
val downings = observableTunnels.map { async(SupervisorJob()) { it.setStateAsync(Tunnel.State.DOWN) } }
|
||||||
try {
|
try {
|
||||||
|
@ -30,7 +30,7 @@ class ModuleDownloaderPreference(context: Context, attrs: AttributeSet?) : Prefe
|
|||||||
@SuppressLint("ApplySharedPref")
|
@SuppressLint("ApplySharedPref")
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
setState(State.WORKING)
|
setState(State.WORKING)
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
when (withContext(Dispatchers.IO) { Application.getModuleLoader().download() }) {
|
when (withContext(Dispatchers.IO) { Application.getModuleLoader().download() }) {
|
||||||
OsConstants.ENOENT -> setState(State.NOTFOUND)
|
OsConstants.ENOENT -> setState(State.NOTFOUND)
|
||||||
|
@ -27,7 +27,7 @@ class ToolsInstallerPreference(context: Context, attrs: AttributeSet?) : Prefere
|
|||||||
|
|
||||||
override fun onAttached() {
|
override fun onAttached() {
|
||||||
super.onAttached()
|
super.onAttached()
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
val state = withContext(Dispatchers.IO) { Application.getToolsInstaller().areInstalled() }
|
val state = withContext(Dispatchers.IO) { Application.getToolsInstaller().areInstalled() }
|
||||||
when {
|
when {
|
||||||
@ -45,7 +45,7 @@ class ToolsInstallerPreference(context: Context, attrs: AttributeSet?) : Prefere
|
|||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
setState(State.WORKING)
|
setState(State.WORKING)
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
val result = withContext(Dispatchers.IO) { Application.getToolsInstaller().install() }
|
val result = withContext(Dispatchers.IO) { Application.getToolsInstaller().install() }
|
||||||
when {
|
when {
|
||||||
|
@ -47,7 +47,7 @@ class VersionPreference(context: Context, attrs: AttributeSet?) : Preference(con
|
|||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
val backend = Application.getBackend()
|
val backend = Application.getBackend()
|
||||||
versionSummary = getContext().getString(R.string.version_summary_checking, getBackendPrettyName(context, backend).toLowerCase(Locale.ENGLISH))
|
versionSummary = getContext().getString(R.string.version_summary_checking, getBackendPrettyName(context, backend).toLowerCase(Locale.ENGLISH))
|
||||||
notifyChanged()
|
notifyChanged()
|
||||||
|
@ -35,7 +35,7 @@ import java.util.zip.ZipOutputStream
|
|||||||
class ZipExporterPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) {
|
class ZipExporterPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) {
|
||||||
private var exportedFilePath: String? = null
|
private var exportedFilePath: String? = null
|
||||||
private fun exportZip() {
|
private fun exportZip() {
|
||||||
lifecycleScope.launch(Dispatchers.Main.immediate) {
|
lifecycleScope.launch {
|
||||||
val tunnels = Application.getTunnelManager().getTunnels()
|
val tunnels = Application.getTunnelManager().getTunnels()
|
||||||
try {
|
try {
|
||||||
exportedFilePath = withContext(Dispatchers.IO) {
|
exportedFilePath = withContext(Dispatchers.IO) {
|
||||||
|
Loading…
Reference in New Issue
Block a user