Skip to content

Commit 15da17b

Browse files
committed
tv: use system picker for API 29+
Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent b3c43e4 commit 15da17b

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ import kotlinx.coroutines.withContext
4646
import java.io.File
4747

4848
class TvMainActivity : AppCompatActivity() {
49+
private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data ->
50+
if (data == null) return@registerForActivityResult
51+
lifecycleScope.launch {
52+
TunnelImporter.importTunnel(contentResolver, data) {
53+
Toast.makeText(this@TvMainActivity, it, Toast.LENGTH_LONG).show()
54+
}
55+
}
56+
}
4957
private var pendingTunnel: ObservableTunnel? = null
5058
private val permissionActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
5159
val tunnel = pendingTunnel
@@ -145,16 +153,22 @@ class TvMainActivity : AppCompatActivity() {
145153
}
146154

147155
binding.importButton.setOnClickListener {
148-
if (filesRoot.get()?.isEmpty() != false) {
149-
navigateTo(myComputerFile)
150-
runOnUiThread {
151-
binding.filesList.requestFocus()
152-
}
153-
} else {
154-
files.clear()
155-
filesRoot.set("")
156-
runOnUiThread {
157-
binding.tunnelList.requestFocus()
156+
try {
157+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
158+
throw Exception()
159+
tunnelFileImportResultLauncher.launch("*/*")
160+
} catch (_: Throwable) {
161+
if (filesRoot.get()?.isEmpty() != false) {
162+
navigateTo(myComputerFile)
163+
runOnUiThread {
164+
binding.filesList.requestFocus()
165+
}
166+
} else {
167+
files.clear()
168+
filesRoot.set("")
169+
runOnUiThread {
170+
binding.tunnelList.requestFocus()
171+
}
158172
}
159173
}
160174
}

0 commit comments

Comments
 (0)