diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-16 00:00:31 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-16 00:00:31 +0200 |
commit | 79ae85c728585c911e22f4778cc4bed5b5ae04e2 (patch) | |
tree | 340aac93024ea5c4ad710231063e146e0d45f2b8 /ui/src/main/java/com/wireguard/android | |
parent | 49ac61304e5a244c674028d279f70f9631351f9c (diff) |
coroutines: lifecycleScope is by default on Main.immediate
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android')
13 files changed, 23 insertions, 29 deletions
diff --git a/ui/src/main/java/com/wireguard/android/activity/BaseActivity.kt b/ui/src/main/java/com/wireguard/android/activity/BaseActivity.kt index 8154624e..95af26fd 100644 --- a/ui/src/main/java/com/wireguard/android/activity/BaseActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/BaseActivity.kt @@ -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) diff --git a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt index de1417dd..8d4c181c 100644 --- a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt @@ -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 diff --git a/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt b/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt index 4ad1d484..99474344 100644 --- a/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt @@ -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) { diff --git a/ui/src/main/java/com/wireguard/android/fragment/BaseFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/BaseFragment.kt index 5cbfbaee..b647f5ac 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/BaseFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/BaseFragment.kt @@ -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) { diff --git a/ui/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.kt index 6f9ff560..03aad0f0 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.kt @@ -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() diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt index fa0dbf82..75627665 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt @@ -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) { diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt index 7b987c50..6ddbb56d 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt @@ -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) diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt index 16f632fa..0d56edd3 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt @@ -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) diff --git a/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt b/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt index ad93efaa..3ceb0806 100644 --- a/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt @@ -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 { diff --git a/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt b/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt index bf562c53..7e437982 100644 --- a/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt @@ -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) diff --git a/ui/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.kt b/ui/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.kt index 42359df0..02434d42 100644 --- a/ui/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.kt @@ -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 { diff --git a/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt b/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt index 18d818f6..1ae4e9e6 100644 --- a/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt @@ -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() diff --git a/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt b/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt index 8a9a4724..d943c89f 100644 --- a/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt @@ -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) { |