fix: file selection AndroidTV

Closes #35 Closes #34
This commit is contained in:
Zane Schepke 2023-09-28 09:33:13 -04:00
parent 235170508b
commit ca3f3fd439
4 changed files with 33 additions and 7 deletions

View File

@ -14,8 +14,8 @@ android {
applicationId = "com.zaneschepke.wireguardautotunnel" applicationId = "com.zaneschepke.wireguardautotunnel"
minSdk = 26 minSdk = 26
targetSdk = 34 targetSdk = 34
versionCode = 30002 versionCode = 30003
versionName = "3.0.2" versionName = "3.0.3"
multiDexEnabled = true multiDexEnabled = true

View File

@ -12,4 +12,6 @@ object Constants {
const val URI_CONTENT_SCHEME = "content" const val URI_CONTENT_SCHEME = "content"
const val URI_PACKAGE_SCHEME = "package" const val URI_PACKAGE_SCHEME = "package"
const val ALLOWED_FILE_TYPES = "*/*" const val ALLOWED_FILE_TYPES = "*/*"
const val FILES_SHOW_ADVANCED = "android.content.extra.SHOW_ADVANCED"
const val ANDROID_TV_STUBS = "com.google.android.tv.frameworkpackagestubs"
} }

View File

@ -210,15 +210,20 @@ fun MainScreen(
.fillMaxWidth() .fillMaxWidth()
.clickable { .clickable {
showBottomSheet = false showBottomSheet = false
val fileSelectionIntent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { val fileSelectionIntent = Intent(Intent.ACTION_GET_CONTENT).apply {
addCategory(Intent.CATEGORY_OPENABLE) addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Constants.FILES_SHOW_ADVANCED, true)
type = Constants.ALLOWED_FILE_TYPES type = Constants.ALLOWED_FILE_TYPES
} }
if (fileSelectionIntent.resolveActivity(context.packageManager) != null) { if(!viewModel.isIntentAvailable(fileSelectionIntent)) {
pickFileLauncher.launch(fileSelectionIntent) fileSelectionIntent.action = Intent.ACTION_OPEN_DOCUMENT
} else { fileSelectionIntent.setPackage(null)
viewModel.showSnackBarMessage(context.getString(R.string.no_file_app)) if (!viewModel.isIntentAvailable(fileSelectionIntent)) {
viewModel.showSnackBarMessage(context.getString(R.string.no_file_app))
return@clickable
}
} }
pickFileLauncher.launch(fileSelectionIntent)
} }
.padding(10.dp) .padding(10.dp)
) { ) {

View File

@ -2,6 +2,8 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.main
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.database.Cursor import android.database.Cursor
import android.net.Uri import android.net.Uri
import android.provider.OpenableColumns import android.provider.OpenableColumns
@ -207,6 +209,23 @@ class MainViewModel @Inject constructor(private val application : Application,
return columnIndex return columnIndex
} }
fun isIntentAvailable(i: Intent?): Boolean {
val packageManager = application.packageManager
val list = packageManager.queryIntentActivities(
i!!,
PackageManager.MATCH_DEFAULT_ONLY
)
// Ignore the Android TV framework app in the list
var size = list.size
for (ri in list) {
// Ignore stub apps
if (Constants.ANDROID_TV_STUBS == ri.activityInfo.packageName) {
size--
}
}
return size > 0
}
private fun getDisplayNameByCursor(cursor: Cursor) : String { private fun getDisplayNameByCursor(cursor: Cursor) : String {
if(cursor.moveToFirst()) { if(cursor.moveToFirst()) {
val index = getDisplayNameColumnIndex(cursor) val index = getDisplayNameColumnIndex(cursor)