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>
This commit is contained in:
Jason A. Donenfeld 2020-09-17 01:03:16 +02:00
parent a9ec828506
commit 59935a12b9
2 changed files with 22 additions and 26 deletions

View File

@ -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,17 +127,10 @@ 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)
revokeLastActivityResultLauncher.launch(shareIntent)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == SHARE_ACTIVITY_REQUEST) {
revokeLastUri()
}
super.onActivityResult(requestCode, resultCode, data)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.log_viewer, menu)
saveButton = menu?.findItem(R.id.save_log)
@ -207,7 +205,7 @@ class LogViewerActivity : AppCompatActivity() {
it.scrollToPosition(logLines.size - 1)
}
} else {
/* I'd prefer for the next line to be:
/* 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.
*/
@ -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>() {

View File

@ -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"
}
}