diff --git a/client/android/src/org/amnezia/vpn/TvFilePicker.kt b/client/android/src/org/amnezia/vpn/TvFilePicker.kt index 1ac275ebf..33f4355f4 100644 --- a/client/android/src/org/amnezia/vpn/TvFilePicker.kt +++ b/client/android/src/org/amnezia/vpn/TvFilePicker.kt @@ -1,7 +1,10 @@ package org.amnezia.vpn import android.content.ActivityNotFoundException +import android.content.Context import android.content.Intent +import android.content.pm.PackageManager +import android.os.Build import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.result.contract.ActivityResultContracts @@ -11,7 +14,25 @@ private const val TAG = "TvFilePicker" class TvFilePicker : ComponentActivity() { - private val fileChooseResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { + private val fileChooseResultLauncher = registerForActivityResult(object : ActivityResultContracts.OpenDocument() { + override fun createIntent(context: Context, input: Array): Intent { + val intent = super.createIntent(context, input) + + val activitiesToResolveIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + context.packageManager.queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())) + } else { + @Suppress("DEPRECATION") + context.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY) + } + if (activitiesToResolveIntent.all { + val name = it.activityInfo.packageName + name.startsWith("com.google.android.tv.frameworkpackagestubs") || name.startsWith("com.android.tv.frameworkpackagestubs") + }) { + throw ActivityNotFoundException() + } + return intent + } + }) { setResult(RESULT_OK, Intent().apply { data = it }) finish() } @@ -31,7 +52,7 @@ class TvFilePicker : ComponentActivity() { private fun getFile() { try { Log.v(TAG, "getFile") - fileChooseResultLauncher.launch("*/*") + fileChooseResultLauncher.launch(arrayOf("*/*")) } catch (_: ActivityNotFoundException) { Log.w(TAG, "Activity not found") setResult(RESULT_CANCELED, Intent().apply { putExtra("activityNotFound", true) })