summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-09-17 01:03:16 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2020-09-17 14:50:37 +0200
commit59935a12b9336855c712d366e542aa6a37e8d041 (patch)
tree1a3ddd7cf67279366fab38d2593e8d07ebf1fc79
parenta9ec8285062636fc7be68fee123bb5899e639fdd (diff)
activityx: use contracts more and refine
This is the beginning; there are still many of the old API's callsites to convert. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt23
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/BaseFragment.kt25
2 files changed, 22 insertions, 26 deletions
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 19f67834..84cc6c7c 100644
--- a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt
+++ b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt
@@ -24,6 +24,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
+import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ShareCompat
import androidx.core.content.res.ResourcesCompat
@@ -109,6 +110,10 @@ class LogViewerActivity : AppCompatActivity() {
lifecycleScope.launch(Dispatchers.IO) { streamingLog() }
+ val revokeLastActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
+ revokeLastUri()
+ }
+
binding.shareFab.setOnClickListener {
revokeLastUri()
val key = KeyPair().privateKey.toHex()
@@ -122,15 +127,8 @@ class LogViewerActivity : AppCompatActivity() {
.createChooserIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
grantUriPermission("android", lastUri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
- startActivityForResult(shareIntent, SHARE_ACTIVITY_REQUEST)
- }
- }
-
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- if (requestCode == SHARE_ACTIVITY_REQUEST) {
- revokeLastUri()
+ revokeLastActivityResultLauncher.launch(shareIntent)
}
- super.onActivityResult(requestCode, resultCode, data)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@@ -207,10 +205,10 @@ class LogViewerActivity : AppCompatActivity() {
it.scrollToPosition(logLines.size - 1)
}
} else {
- /* I'd prefer for the next line to be:
- * logLines.lastOrNull()?.msg += "\n$line"
- * However, as of writing, that causes the kotlin compiler to freak out and crash, spewing bytecode.
- */
+ /* TODO: I'd prefer for the next line to be:
+ * logLines.lastOrNull()?.msg += "\n$line"
+ * However, as of writing, that causes the kotlin compiler to freak out and crash, spewing bytecode.
+ */
logLines.lastOrNull()?.apply { msg += "\n$line" }
if (haveScrolled) logAdapter.notifyDataSetChanged()
}
@@ -259,7 +257,6 @@ class LogViewerActivity : AppCompatActivity() {
*/
private val THREADTIME_LINE: Pattern = Pattern.compile("^(\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3})(?:\\s+[0-9A-Za-z]+)?\\s+(\\d+)\\s+(\\d+)\\s+([A-Z])\\s+(.+?)\\s*: (.*)$")
private val LOGS: MutableMap<String, ByteArray> = ConcurrentHashMap()
- private const val SHARE_ACTIVITY_REQUEST = 49133
}
private inner class LogEntryAdapter : RecyclerView.Adapter<LogEntryAdapter.ViewHolder>() {
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 b647f5ac..7c588c1a 100644
--- a/ui/src/main/java/com/wireguard/android/fragment/BaseFragment.kt
+++ b/ui/src/main/java/com/wireguard/android/fragment/BaseFragment.kt
@@ -5,10 +5,10 @@
package com.wireguard.android.fragment
import android.content.Context
-import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.Toast
+import androidx.activity.result.contract.ActivityResultContracts
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
@@ -34,21 +34,21 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
private var baseActivity: BaseActivity? = null
private var pendingTunnel: ObservableTunnel? = null
private var pendingTunnelUp: Boolean? = null
+ private val permissionActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
+ val tunnel = pendingTunnel
+ val checked = pendingTunnelUp
+ if (tunnel != null && checked != null)
+ setTunnelStateWithPermissionsResult(tunnel, checked)
+ pendingTunnel = null
+ pendingTunnelUp = null
+ }
+
protected var selectedTunnel: ObservableTunnel?
get() = baseActivity?.selectedTunnel
protected set(tunnel) {
baseActivity?.selectedTunnel = tunnel
}
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- if (requestCode == REQUEST_CODE_VPN_PERMISSION) {
- if (pendingTunnel != null && pendingTunnelUp != null) setTunnelStateWithPermissionsResult(pendingTunnel!!, pendingTunnelUp!!)
- pendingTunnel = null
- pendingTunnelUp = null
- }
- }
-
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is BaseActivity) {
@@ -77,7 +77,7 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
if (intent != null) {
pendingTunnel = tunnel
pendingTunnelUp = checked
- startActivityForResult(intent, REQUEST_CODE_VPN_PERMISSION)
+ permissionActivityResultLauncher.launch(intent)
return@launch
}
}
@@ -96,7 +96,7 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
val view = view
if (view != null)
Snackbar.make(view, message, Snackbar.LENGTH_LONG)
- .setAnchorView(view.findViewById<View>(R.id.create_fab))
+ .setAnchorView(view.findViewById(R.id.create_fab))
.show()
else
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show()
@@ -106,7 +106,6 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
}
companion object {
- private const val REQUEST_CODE_VPN_PERMISSION = 23491
private const val TAG = "WireGuard/BaseFragment"
}
}