aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-04-08 04:17:40 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-04-10 00:09:25 +0530
commitb9595cec4846df706c612c734d5774ad7ff6208f (patch)
treea59dc936cbe11103cd316bcb850dc18da8968a15
parentui: move tunnel deletion to coroutines (diff)
downloadwireguard-android-b9595cec4846df706c612c734d5774ad7ff6208f.tar.xz
wireguard-android-b9595cec4846df706c612c734d5774ad7ff6208f.zip
ui: switch to lifecycle scope where available
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--build.gradle1
-rw-r--r--ui/build.gradle1
-rw-r--r--ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt55
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt13
4 files changed, 30 insertions, 40 deletions
diff --git a/build.gradle b/build.gradle
index 87c86139..ace24196 100644
--- a/build.gradle
+++ b/build.gradle
@@ -24,6 +24,7 @@ buildscript {
jsr305Version = '3.0.2'
junitVersion = '4.13'
kotlinVersion = '1.3.71'
+ lifecycleKtxVersion = '2.2.0'
materialComponentsVersion = '1.2.0-alpha05'
mavenPluginVersion = '2.1'
preferenceVersion = '1.1.0'
diff --git a/ui/build.gradle b/ui/build.gradle
index 96813d67..616abba3 100644
--- a/ui/build.gradle
+++ b/ui/build.gradle
@@ -75,6 +75,7 @@ dependencies {
implementation "androidx.core:core-ktx:$coreKtxVersion"
implementation "androidx.databinding:databinding-runtime:$agpVersion"
implementation "androidx.fragment:fragment:$fragmentVersion"
+ implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleKtxVersion"
implementation "androidx.preference:preference:$preferenceVersion"
implementation "com.google.android.material:material:$materialComponentsVersion"
implementation "com.journeyapps:zxing-android-embedded:$zxingEmbeddedVersion"
diff --git a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt
index 87fdc236..399c5360 100644
--- a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt
+++ b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt
@@ -26,6 +26,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ShareCompat
+import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@@ -40,9 +41,7 @@ import com.wireguard.android.widget.EdgeToEdge.setUpFAB
import com.wireguard.android.widget.EdgeToEdge.setUpRoot
import com.wireguard.android.widget.EdgeToEdge.setUpScrollingContent
import com.wireguard.crypto.KeyPair
-import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.BufferedReader
@@ -67,7 +66,6 @@ class LogViewerActivity : AppCompatActivity() {
private var rawLogLines = StringBuffer()
private var recyclerView: RecyclerView? = null
private var saveButton: MenuItem? = null
- private val coroutineScope = CoroutineScope(Dispatchers.Default)
private val year by lazy {
val yearFormatter: DateFormat = SimpleDateFormat("yyyy", Locale.US)
yearFormatter.format(Date())
@@ -114,7 +112,7 @@ class LogViewerActivity : AppCompatActivity() {
addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.VERTICAL))
}
- coroutineScope.launch { streamingLog() }
+ lifecycleScope.launch { streamingLog() }
binding.shareFab.setOnClickListener {
revokeLastUri()
@@ -153,43 +151,36 @@ class LogViewerActivity : AppCompatActivity() {
true
}
R.id.save_log -> {
- coroutineScope.launch { saveLog() }
+ lifecycleScope.launch { saveLog() }
true
}
else -> super.onOptionsItemSelected(item)
}
}
- override fun onDestroy() {
- super.onDestroy()
- coroutineScope.cancel()
- }
-
private suspend fun saveLog() {
val context = this
- withContext(Dispatchers.Main) {
- saveButton?.isEnabled = false
- withContext(Dispatchers.IO) {
- var exception: Throwable? = null
- var outputFile: DownloadsFileSaver.DownloadsFile? = null
- try {
- outputFile = DownloadsFileSaver.save(context, "wireguard-log.txt", "text/plain", true)
- outputFile.outputStream.use {
- it.write(rawLogLines.toString().toByteArray(Charsets.UTF_8))
- }
- } catch (e: Throwable) {
- outputFile?.delete()
- exception = e
- }
- withContext(Dispatchers.Main) {
- saveButton?.isEnabled = true
- Snackbar.make(findViewById(android.R.id.content),
- if (exception == null) getString(R.string.log_export_success, outputFile?.fileName)
- else getString(R.string.log_export_error, ErrorMessages[exception]),
- if (exception == null) Snackbar.LENGTH_SHORT else Snackbar.LENGTH_LONG)
- .setAnchorView(binding.shareFab)
- .show()
+ saveButton?.isEnabled = false
+ withContext(Dispatchers.IO) {
+ var exception: Throwable? = null
+ var outputFile: DownloadsFileSaver.DownloadsFile? = null
+ try {
+ outputFile = DownloadsFileSaver.save(context, "wireguard-log.txt", "text/plain", true)
+ outputFile.outputStream.use {
+ it.write(rawLogLines.toString().toByteArray(Charsets.UTF_8))
}
+ } catch (e: Throwable) {
+ outputFile?.delete()
+ exception = e
+ }
+ withContext(Dispatchers.Main) {
+ saveButton?.isEnabled = true
+ Snackbar.make(findViewById(android.R.id.content),
+ if (exception == null) getString(R.string.log_export_success, outputFile?.fileName)
+ else getString(R.string.log_export_error, ErrorMessages[exception]),
+ if (exception == null) Snackbar.LENGTH_SHORT else Snackbar.LENGTH_LONG)
+ .setAnchorView(binding.shareFab)
+ .show()
}
}
}
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 179d1a1c..4cc9e997 100644
--- a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
+++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
@@ -20,6 +20,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ActionMode
+import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar
import com.google.zxing.integration.android.IntentIntegrator
import com.wireguard.android.Application
@@ -36,9 +37,7 @@ import com.wireguard.android.widget.EdgeToEdge.setUpScrollingContent
import com.wireguard.android.widget.MultiselectableRelativeLayout
import com.wireguard.config.Config
import java9.util.concurrent.CompletableFuture
-import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
@@ -55,13 +54,12 @@ import java.util.zip.ZipInputStream
/**
* Fragment containing a list of known WireGuard tunnels. It allows creating and deleting tunnels.
*/
-class TunnelListFragment : BaseFragment(), CoroutineScope {
+class TunnelListFragment : BaseFragment() {
private val actionModeListener = ActionModeListener()
private var actionMode: ActionMode? = null
private var binding: TunnelListFragmentBinding? = null
- private val job = Job()
- override val coroutineContext
- get() = job + Dispatchers.Main
+ private val coroutineScope
+ get() = viewLifecycleOwner.lifecycleScope
private fun importTunnel(configText: String) {
try {
// Ensure the config text is parseable before proceeding…
@@ -224,7 +222,6 @@ class TunnelListFragment : BaseFragment(), CoroutineScope {
override fun onDestroyView() {
binding = null
- job.cancel()
super.onDestroyView()
}
@@ -330,7 +327,7 @@ class TunnelListFragment : BaseFragment(), CoroutineScope {
Application.getTunnelManager().tunnels.thenAccept { tunnels ->
val tunnelsToDelete = ArrayList<ObservableTunnel>()
for (position in copyCheckedItems) tunnelsToDelete.add(tunnels[position])
- launch {
+ coroutineScope.launch {
val exception = try {
tunnelsToDelete.map { async { it.delete() } }.toList().awaitAll()
null