summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'ui/src/main/java')
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt27
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt53
-rw-r--r--ui/src/main/java/com/wireguard/android/util/Extensions.kt5
3 files changed, 43 insertions, 42 deletions
diff --git a/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt b/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt
index 17bdac04..3449d702 100644
--- a/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt
+++ b/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt
@@ -12,13 +12,13 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.FrameLayout
+import androidx.core.os.bundleOf
+import androidx.fragment.app.setFragmentResult
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
-import com.google.zxing.integration.android.IntentIntegrator
import com.wireguard.android.R
import com.wireguard.android.activity.TunnelCreatorActivity
-import com.wireguard.android.util.requireTargetFragment
import com.wireguard.android.util.resolveAttribute
class AddTunnelsSheet : BottomSheetDialogFragment() {
@@ -82,23 +82,22 @@ class AddTunnelsSheet : BottomSheetDialogFragment() {
}
private fun onRequestCreateConfig() {
- startActivity(Intent(activity, TunnelCreatorActivity::class.java))
+ setFragmentResult(REQUEST_KEY_NEW_TUNNEL, bundleOf(REQUEST_METHOD to REQUEST_CREATE))
}
private fun onRequestImportConfig() {
- val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
- addCategory(Intent.CATEGORY_OPENABLE)
- type = "*/*"
- }
- requireTargetFragment().startActivityForResult(intent, TunnelListFragment.REQUEST_IMPORT)
+ setFragmentResult(REQUEST_KEY_NEW_TUNNEL, bundleOf(REQUEST_METHOD to REQUEST_IMPORT))
}
private fun onRequestScanQRCode() {
- val integrator = IntentIntegrator.forSupportFragment(requireTargetFragment()).apply {
- setOrientationLocked(false)
- setBeepEnabled(false)
- setPrompt(getString(R.string.qr_code_hint))
- }
- integrator.initiateScan(listOf(IntentIntegrator.QR_CODE))
+ setFragmentResult(REQUEST_KEY_NEW_TUNNEL, bundleOf(REQUEST_METHOD to REQUEST_SCAN))
+ }
+
+ companion object {
+ const val REQUEST_KEY_NEW_TUNNEL = "request_new_tunnel"
+ const val REQUEST_METHOD = "request_method"
+ const val REQUEST_CREATE = "request_create"
+ const val REQUEST_IMPORT = "request_import"
+ const val REQUEST_SCAN = "request_scan"
}
}
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 8b43e1f7..c10f31a4 100644
--- a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
+++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
@@ -4,8 +4,6 @@
*/
package com.wireguard.android.fragment
-import android.annotation.SuppressLint
-import android.app.Activity
import android.content.Intent
import android.content.res.Resources
import android.net.Uri
@@ -19,6 +17,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
+import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ActionMode
import androidx.lifecycle.lifecycleScope
@@ -26,6 +25,7 @@ import com.google.android.material.snackbar.Snackbar
import com.google.zxing.integration.android.IntentIntegrator
import com.wireguard.android.Application
import com.wireguard.android.R
+import com.wireguard.android.activity.TunnelCreatorActivity
import com.wireguard.android.databinding.ObservableKeyedRecyclerViewAdapter.RowConfigurationHandler
import com.wireguard.android.databinding.TunnelListFragmentBinding
import com.wireguard.android.databinding.TunnelListItemBinding
@@ -61,6 +61,15 @@ class TunnelListFragment : BaseFragment() {
private val actionModeListener = ActionModeListener()
private var actionMode: ActionMode? = null
private var binding: TunnelListFragmentBinding? = null
+ private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data ->
+ importTunnel(data)
+ }
+
+ private val qrImportResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
+ val qrCode = IntentIntegrator.parseActivityResult(result.resultCode, result.data)
+ qrCode?.contents?.let { importTunnel(it) }
+ }
+
private fun importTunnel(configText: String) {
try {
// Ensure the config text is parseable before proceeding…
@@ -173,33 +182,31 @@ class TunnelListFragment : BaseFragment() {
}
}
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- when (requestCode) {
- REQUEST_IMPORT -> {
- if (resultCode == Activity.RESULT_OK && data != null) importTunnel(data.data)
- return
- }
- IntentIntegrator.REQUEST_CODE -> {
- val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
- if (result != null && result.contents != null) {
- importTunnel(result.contents)
- }
- return
- }
- else -> super.onActivityResult(requestCode, resultCode, data)
- }
- }
-
- @SuppressLint("ClickableViewAccessibility")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
binding = TunnelListFragmentBinding.inflate(inflater, container, false)
+ val bottomSheet = AddTunnelsSheet()
binding?.apply {
createFab.setOnClickListener {
- val bottomSheet = AddTunnelsSheet()
- bottomSheet.setTargetFragment(fragment, REQUEST_TARGET_FRAGMENT)
- bottomSheet.show(parentFragmentManager, "BOTTOM_SHEET")
+ childFragmentManager.setFragmentResultListener(AddTunnelsSheet.REQUEST_KEY_NEW_TUNNEL, viewLifecycleOwner) { _, bundle ->
+ when (bundle.getString(AddTunnelsSheet.REQUEST_METHOD)) {
+ AddTunnelsSheet.REQUEST_CREATE -> {
+ startActivity(Intent(requireActivity(), TunnelCreatorActivity::class.java))
+ }
+ AddTunnelsSheet.REQUEST_IMPORT -> {
+ tunnelFileImportResultLauncher.launch("*/*")
+ }
+ AddTunnelsSheet.REQUEST_SCAN -> {
+ qrImportResultLauncher.launch(IntentIntegrator(requireActivity())
+ .setOrientationLocked(false)
+ .setBeepEnabled(false)
+ .setPrompt(getString(R.string.qr_code_hint))
+ .createScanIntent())
+ }
+ }
+ }
+ bottomSheet.show(childFragmentManager, "BOTTOM_SHEET")
}
executePendingBindings()
setUpRoot(root as ViewGroup)
diff --git a/ui/src/main/java/com/wireguard/android/util/Extensions.kt b/ui/src/main/java/com/wireguard/android/util/Extensions.kt
index 22ef8bd7..e8482cfb 100644
--- a/ui/src/main/java/com/wireguard/android/util/Extensions.kt
+++ b/ui/src/main/java/com/wireguard/android/util/Extensions.kt
@@ -8,7 +8,6 @@ package com.wireguard.android.util
import android.content.Context
import android.util.TypedValue
import androidx.annotation.AttrRes
-import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import com.wireguard.android.Application
@@ -21,10 +20,6 @@ fun Context.resolveAttribute(@AttrRes attrRes: Int): Int {
return typedValue.data
}
-fun Fragment.requireTargetFragment(): Fragment {
- return requireNotNull(targetFragment) { "A target fragment should always be set for $this" }
-}
-
val Any.applicationScope: CoroutineScope
get() = Application.getCoroutineScope()