aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-05-29 14:09:40 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-05-29 14:09:40 +0530
commit66555df1a867e244c15e02e1ea5d61ad6938ab40 (patch)
tree57661469390ca8edf9991e84d7ee71e9dc8bf18f
parentui: address new databinding requirements (diff)
downloadwireguard-android-refactor-fragment-communication.tar.xz
wireguard-android-refactor-fragment-communication.zip
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--build.gradle3
-rw-r--r--ui/build.gradle3
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt44
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt39
4 files changed, 39 insertions, 50 deletions
diff --git a/build.gradle b/build.gradle
index e1b5b1a1..4d965f9d 100644
--- a/build.gradle
+++ b/build.gradle
@@ -8,6 +8,7 @@ allprojects {
buildscript {
ext {
+ activityKtxVersion = '1.2.0-alpha05'
agpVersion = '4.0.0'
annotationsVersion = '1.1.0'
appcompatVersion = '1.1.0'
@@ -19,7 +20,7 @@ buildscript {
coreKtxVersion = '1.2.0'
coroutinesVersion = '1.3.5'
eddsaVersion = '0.3.0'
- fragmentVersion = '1.2.4'
+ fragmentKtxVersion = '1.3.0-alpha05'
jsr305Version = '3.0.2'
junitVersion = '4.13'
kotlinVersion = '1.3.72'
diff --git a/ui/build.gradle b/ui/build.gradle
index e59fa764..183db14b 100644
--- a/ui/build.gradle
+++ b/ui/build.gradle
@@ -66,6 +66,7 @@ android {
dependencies {
implementation project(":tunnel")
+ implementation "androidx.activity:activity-ktx:$activityKtxVersion"
implementation "androidx.annotation:annotation:$annotationsVersion"
implementation "androidx.appcompat:appcompat:$appcompatVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
@@ -73,7 +74,7 @@ dependencies {
implementation "androidx.biometric:biometric:$biometricVersion"
implementation "androidx.core:core-ktx:$coreKtxVersion"
implementation "androidx.databinding:databinding-runtime:$agpVersion"
- implementation "androidx.fragment:fragment:$fragmentVersion"
+ implementation "androidx.fragment:fragment-ktx:$fragmentKtxVersion"
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/fragment/AddTunnelsSheet.kt b/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt
index 17bdac04..104d444f 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,18 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.FrameLayout
+import androidx.activity.result.contract.ActivityResultContracts.GetContent
+import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
+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.fragment.TunnelListFragment.Companion.FRAGMENT_RESULT_FILE_URI
+import com.wireguard.android.fragment.TunnelListFragment.Companion.FRAGMENT_RESULT_KEY
import com.wireguard.android.util.resolveAttribute
class AddTunnelsSheet : BottomSheetDialogFragment() {
@@ -58,15 +63,25 @@ class AddTunnelsSheet : BottomSheetDialogFragment() {
}
dialog.findViewById<View>(R.id.create_empty)?.setOnClickListener {
dismiss()
- onRequestCreateConfig()
+ startActivity(Intent(activity, TunnelCreatorActivity::class.java))
}
dialog.findViewById<View>(R.id.create_from_file)?.setOnClickListener {
dismiss()
- onRequestImportConfig()
+ registerForActivityResult(GetContent()) { uri ->
+ setFragmentResult(FRAGMENT_RESULT_KEY, bundleOf(FRAGMENT_RESULT_FILE_URI to uri))
+ }.launch("*/*")
}
dialog.findViewById<View>(R.id.create_from_qrcode)?.setOnClickListener {
dismiss()
- onRequestScanQRCode()
+ val intent = IntentIntegrator.forSupportFragment(this@AddTunnelsSheet).apply {
+ setOrientationLocked(false)
+ setBeepEnabled(false)
+ setPrompt(getString(R.string.qr_code_hint))
+ setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
+ }.createScanIntent()
+ registerForActivityResult(StartActivityForResult()) { result ->
+ setFragmentResult(FRAGMENT_RESULT_KEY, bundleOf(FRAGMENT_RESULT_FILE_URI to result.data))
+ }.launch(intent)
}
}
})
@@ -80,25 +95,4 @@ class AddTunnelsSheet : BottomSheetDialogFragment() {
super.dismiss()
behavior?.removeBottomSheetCallback(bottomSheetCallback)
}
-
- private fun onRequestCreateConfig() {
- startActivity(Intent(activity, TunnelCreatorActivity::class.java))
- }
-
- private fun onRequestImportConfig() {
- val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
- addCategory(Intent.CATEGORY_OPENABLE)
- type = "*/*"
- }
- requireTargetFragment().startActivityForResult(intent, TunnelListFragment.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))
- }
}
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 7af5e06b..8ffcb73c 100644
--- a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
+++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
@@ -5,7 +5,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
@@ -21,6 +20,7 @@ import android.view.animation.Animation
import android.view.animation.AnimationUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ActionMode
+import androidx.fragment.app.setFragmentResultListener
import com.google.android.material.snackbar.Snackbar
import com.google.zxing.integration.android.IntentIntegrator
import com.wireguard.android.Application
@@ -168,8 +168,8 @@ class TunnelListFragment : BaseFragment() {
}
}
- override fun onActivityCreated(savedInstanceState: Bundle?) {
- super.onActivityCreated(savedInstanceState)
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
if (savedInstanceState != null) {
val checkedItems = savedInstanceState.getIntegerArrayList(CHECKED_ITEMS)
if (checkedItems != null) {
@@ -178,33 +178,24 @@ 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)
+ @SuppressLint("ClickableViewAccessibility")
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
+ savedInstanceState: Bundle?): View? {
+ super.onCreateView(inflater, container, savedInstanceState)
+ childFragmentManager.setFragmentResultListener(FRAGMENT_RESULT_KEY, viewLifecycleOwner) { _, bundle ->
+ bundle.getParcelable<Uri>(FRAGMENT_RESULT_FILE_URI)?.let(::importTunnel)
+ bundle.getParcelable<Intent>(FRAGMENT_RESULT_QR_CODE)?.let { intent ->
+ val result = IntentIntegrator.parseActivityResult(0, 0, intent)
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)
binding?.apply {
createFab.setOnClickListener {
val bottomSheet = AddTunnelsSheet()
- bottomSheet.setTargetFragment(fragment, REQUEST_TARGET_FRAGMENT)
- bottomSheet.show(parentFragmentManager, "BOTTOM_SHEET")
+ bottomSheet.show(childFragmentManager, "BOTTOM_SHEET")
}
executePendingBindings()
setUpRoot(root as ViewGroup)
@@ -423,8 +414,10 @@ class TunnelListFragment : BaseFragment() {
}
companion object {
- const val REQUEST_IMPORT = 1
- private const val REQUEST_TARGET_FRAGMENT = 2
+ private const val REQUEST_IMPORT = 1
+ const val FRAGMENT_RESULT_FILE_URI = "import_file_uri"
+ const val FRAGMENT_RESULT_QR_CODE = "qr_code_data"
+ const val FRAGMENT_RESULT_KEY = "add_tunnels_sheet"
private const val CHECKED_ITEMS = "CHECKED_ITEMS"
private const val TAG = "WireGuard/TunnelListFragment"
}