From 15da17b5952a76a52089a5d7b503e60b43a7ca50 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 23 Sep 2020 14:37:15 +0200 Subject: tv: use system picker for API 29+ Signed-off-by: Jason A. Donenfeld --- .../wireguard/android/activity/TvMainActivity.kt | 34 +++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'ui/src/main/java/com') diff --git a/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt b/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt index 004d26e0..32fbe387 100644 --- a/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt @@ -46,6 +46,14 @@ import kotlinx.coroutines.withContext import java.io.File class TvMainActivity : AppCompatActivity() { + private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data -> + if (data == null) return@registerForActivityResult + lifecycleScope.launch { + TunnelImporter.importTunnel(contentResolver, data) { + Toast.makeText(this@TvMainActivity, it, Toast.LENGTH_LONG).show() + } + } + } private var pendingTunnel: ObservableTunnel? = null private val permissionActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { val tunnel = pendingTunnel @@ -145,16 +153,22 @@ class TvMainActivity : AppCompatActivity() { } binding.importButton.setOnClickListener { - if (filesRoot.get()?.isEmpty() != false) { - navigateTo(myComputerFile) - runOnUiThread { - binding.filesList.requestFocus() - } - } else { - files.clear() - filesRoot.set("") - runOnUiThread { - binding.tunnelList.requestFocus() + try { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) + throw Exception() + tunnelFileImportResultLauncher.launch("*/*") + } catch (_: Throwable) { + if (filesRoot.get()?.isEmpty() != false) { + navigateTo(myComputerFile) + runOnUiThread { + binding.filesList.requestFocus() + } + } else { + files.clear() + filesRoot.set("") + runOnUiThread { + binding.tunnelList.requestFocus() + } } } } -- cgit v1.2.3