Skip to content

Commit

Permalink
fix: lock open button if Leon is default browser (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenjacobs authored Nov 1, 2024
1 parent 67fd46a commit 1be6cf8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/src/main/kotlin/com/svenjacobs/app/leon/ui/common/Browser.kt
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -375,6 +380,7 @@ private fun SuccessBody(
OutlinedButton(
modifier = buttonModifier,
onClick = { onOpenClick(result) },
enabled = !isDefaultBrowser(LocalContext.current),
) {
Text(
text = stringResource(R.string.open),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ 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
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
Expand Down Expand Up @@ -127,6 +129,7 @@ private fun Content(
text = stringResource(R.string.open_in_custom_tabs),
checked = customTabsEnabled,
onCheckedChange = onCustomTabsSwitchCheckedChange,
enabled = !isDefaultBrowser(LocalContext.current),
)

Column(
Expand Down Expand Up @@ -215,6 +218,7 @@ private fun SwitchRow(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
) {
Row(
modifier = modifier.fillMaxWidth(),
Expand All @@ -230,6 +234,7 @@ private fun SwitchRow(
Switch(
checked = checked,
onCheckedChange = onCheckedChange,
enabled = enabled,
)
}
}
Expand Down

0 comments on commit 1be6cf8

Please sign in to comment.