Skip to content

Commit d6431db

Browse files
committed
ui: detect tv framework stubs when getting content
Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent b8f649d commit d6431db

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package com.wireguard.android.activity
77

88
import android.Manifest
9+
import android.content.ActivityNotFoundException
10+
import android.content.Context
11+
import android.content.Intent
912
import android.content.pm.PackageManager
1013
import android.net.Uri
1114
import android.os.Build
@@ -50,7 +53,27 @@ import kotlinx.coroutines.withContext
5053
import java.io.File
5154

5255
class TvMainActivity : AppCompatActivity() {
53-
private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data ->
56+
private val tunnelFileImportResultLauncher = registerForActivityResult(object : ActivityResultContracts.GetContent() {
57+
override fun createIntent(context: Context, input: String): Intent {
58+
val intent = super.createIntent(context, input)
59+
60+
/* AndroidTV now comes with stubs that do nothing but display a Toast less helpful than
61+
* what we can do, so detect this and throw an exception that we can catch later. */
62+
val activitiesToResolveIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
63+
context.packageManager.queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong()))
64+
} else {
65+
@Suppress("DEPRECATION")
66+
context.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
67+
}
68+
if (activitiesToResolveIntent.all {
69+
val name = it.activityInfo.packageName
70+
name.startsWith("com.google.android.tv.frameworkpackagestubs") || name.startsWith("com.android.tv.frameworkpackagestubs")
71+
}) {
72+
throw ActivityNotFoundException()
73+
}
74+
return intent
75+
}
76+
}) { data ->
5477
if (data == null) return@registerForActivityResult
5578
lifecycleScope.launch {
5679
TunnelImporter.importTunnel(contentResolver, data) {

0 commit comments

Comments
 (0)