|
6 | 6 | package com.wireguard.android.activity
|
7 | 7 |
|
8 | 8 | import android.Manifest
|
| 9 | +import android.content.ActivityNotFoundException |
| 10 | +import android.content.Context |
| 11 | +import android.content.Intent |
9 | 12 | import android.content.pm.PackageManager
|
10 | 13 | import android.net.Uri
|
11 | 14 | import android.os.Build
|
@@ -50,7 +53,27 @@ import kotlinx.coroutines.withContext
|
50 | 53 | import java.io.File
|
51 | 54 |
|
52 | 55 | 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 -> |
54 | 77 | if (data == null) return@registerForActivityResult
|
55 | 78 | lifecycleScope.launch {
|
56 | 79 | TunnelImporter.importTunnel(contentResolver, data) {
|
|
0 commit comments