diff --git a/app/src/main/kotlin/com/svenjacobs/app/leon/ui/common/Browser.kt b/app/src/main/kotlin/com/svenjacobs/app/leon/ui/common/Browser.kt new file mode 100644 index 00000000..0c674c64 --- /dev/null +++ b/app/src/main/kotlin/com/svenjacobs/app/leon/ui/common/Browser.kt @@ -0,0 +1,11 @@ +package com.svenjacobs.app.leon.ui.common + +import android.content.Context +import android.content.Intent +import android.net.Uri + +fun isDefaultBrowser(context: Context): Boolean { + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://")) + val activity = intent.resolveActivity(context.packageManager) + return activity?.packageName?.startsWith("com.svenjacobs.app.leon") ?: false +} diff --git a/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/main/MainScreen.kt b/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/main/MainScreen.kt index 5d0f436d..0f4bb47d 100644 --- a/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/main/MainScreen.kt +++ b/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/main/MainScreen.kt @@ -73,6 +73,7 @@ import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController import com.svenjacobs.app.leon.R import com.svenjacobs.app.leon.core.domain.action.ActionAfterClean +import com.svenjacobs.app.leon.ui.common.isDefaultBrowser import com.svenjacobs.app.leon.ui.common.views.TopAppBar import com.svenjacobs.app.leon.ui.screens.main.model.MainScreenViewModel import com.svenjacobs.app.leon.ui.screens.main.model.MainScreenViewModel.UiState.Result @@ -158,6 +159,10 @@ fun MainScreen( } fun openUrl(result: Result.Success) { + // When Leon is the system default browser we neither can open the URL in custom tabs nor + // in the default browser because this would just open Leon again. + if (isDefaultBrowser(context)) return + if (uiState.isCustomTabsEnabled) { openInCustomTabs(result) } else { @@ -375,6 +380,7 @@ private fun SuccessBody( OutlinedButton( modifier = buttonModifier, onClick = { onOpenClick(result) }, + enabled = !isDefaultBrowser(LocalContext.current), ) { Text( text = stringResource(R.string.open), diff --git a/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/settings/SettingsScreen.kt b/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/settings/SettingsScreen.kt index f0975207..d24c2570 100644 --- a/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/settings/SettingsScreen.kt +++ b/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/settings/SettingsScreen.kt @@ -41,6 +41,7 @@ import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle @@ -48,6 +49,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel import com.svenjacobs.app.leon.BuildConfig import com.svenjacobs.app.leon.R import com.svenjacobs.app.leon.core.domain.action.ActionAfterClean +import com.svenjacobs.app.leon.ui.common.isDefaultBrowser import com.svenjacobs.app.leon.ui.screens.settings.model.SettingsScreenViewModel import com.svenjacobs.app.leon.ui.theme.AppTheme import com.svenjacobs.app.leon.ui.tooling.DayNightPreviews @@ -127,6 +129,7 @@ private fun Content( text = stringResource(R.string.open_in_custom_tabs), checked = customTabsEnabled, onCheckedChange = onCustomTabsSwitchCheckedChange, + enabled = !isDefaultBrowser(LocalContext.current), ) Column( @@ -215,6 +218,7 @@ private fun SwitchRow( checked: Boolean, onCheckedChange: (Boolean) -> Unit, modifier: Modifier = Modifier, + enabled: Boolean = true, ) { Row( modifier = modifier.fillMaxWidth(), @@ -230,6 +234,7 @@ private fun SwitchRow( Switch( checked = checked, onCheckedChange = onCheckedChange, + enabled = enabled, ) } }