From 17c33426841cf1cca59d151b9a4136503bdccbc7 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Tue, 13 Jun 2023 16:55:15 -0700 Subject: [PATCH] Add option to use private custom tabs when available (#613) Co-authored-by: Dessalines --- app/src/main/java/com/jerboa/MainActivity.kt | 3 ++- app/src/main/java/com/jerboa/Utils.kt | 8 +++++-- app/src/main/java/com/jerboa/db/AppDB.kt | 17 ++++++++++++++- .../ui/components/comment/CommentNode.kt | 2 +- .../ui/components/common/MarkdownHelper.kt | 4 ++-- .../components/community/CommunityActivity.kt | 2 +- .../jerboa/ui/components/home/HomeActivity.kt | 2 +- .../person/PersonProfileActivity.kt | 2 +- .../jerboa/ui/components/post/PostActivity.kt | 2 +- .../settings/about/AboutActivity.kt | 21 ++++++++++++------- .../lookandfeel/LookAndFeelActivity.kt | 9 ++++++++ app/src/main/res/values/strings.xml | 1 + 12 files changed, 54 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/jerboa/MainActivity.kt b/app/src/main/java/com/jerboa/MainActivity.kt index 4005ce363..02d31b274 100644 --- a/app/src/main/java/com/jerboa/MainActivity.kt +++ b/app/src/main/java/com/jerboa/MainActivity.kt @@ -101,7 +101,7 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - MarkdownHelper.init(this, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true) + MarkdownHelper.init(this, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false) window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) val accountSync = getCurrentAccountSync(accountViewModel) @@ -608,6 +608,7 @@ class MainActivity : ComponentActivity() { AboutActivity( navController = navController, useCustomTabs = appSettings?.useCustomTabs ?: true, + usePrivateTabs = appSettings?.usePrivateTabs ?: false, ) } } diff --git a/app/src/main/java/com/jerboa/Utils.kt b/app/src/main/java/com/jerboa/Utils.kt index 2c95ac5eb..3fdd1f1a2 100644 --- a/app/src/main/java/com/jerboa/Utils.kt +++ b/app/src/main/java/com/jerboa/Utils.kt @@ -375,10 +375,14 @@ fun LazyListState.isScrolledToEnd(): Boolean { return out } -fun openLink(url: String, ctx: Context, useCustomTab: Boolean) { +fun openLink(url: String, ctx: Context, useCustomTab: Boolean, usePrivateTab: Boolean) { if (useCustomTab) { val intent = CustomTabsIntent.Builder() - .build() + .build().apply { + if (usePrivateTab) { + intent.putExtra("com.google.android.apps.chrome.EXTRA_OPEN_NEW_INCOGNITO_TAB", true) + } + } intent.launchUrl(ctx, Uri.parse(url)) } else { val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) diff --git a/app/src/main/java/com/jerboa/db/AppDB.kt b/app/src/main/java/com/jerboa/db/AppDB.kt index 3e1064f68..5f0000ae2 100644 --- a/app/src/main/java/com/jerboa/db/AppDB.kt +++ b/app/src/main/java/com/jerboa/db/AppDB.kt @@ -89,6 +89,11 @@ data class AppSettings( defaultValue = "1", ) val useCustomTabs: Boolean, + @ColumnInfo( + name = "use_private_tabs", + defaultValue = "0", + ) + val usePrivateTabs: Boolean, ) @Dao @@ -353,8 +358,17 @@ val MIGRATION_12_13 = object : Migration(12, 13) { } } +val MIGRATION_13_14 = object : Migration(13, 14) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL(UPDATE_APP_CHANGELOG_UNVIEWED) + database.execSQL( + "ALTER TABLE AppSettings add column use_private_tabs INTEGER NOT NULL default 0", + ) + } +} + @Database( - version = 13, + version = 14, entities = [Account::class, AppSettings::class], exportSchema = true, ) @@ -391,6 +405,7 @@ abstract class AppDB : RoomDatabase() { MIGRATION_10_11, MIGRATION_11_12, MIGRATION_12_13, + MIGRATION_13_14, ) // Necessary because it can't insert data on creation .addCallback(object : Callback() { diff --git a/app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt b/app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt index 1267604fe..a8af6a6e5 100644 --- a/app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt +++ b/app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt @@ -615,7 +615,7 @@ fun CommentFooterLine( @Preview @Composable fun CommentNodesPreview() { - MarkdownHelper.init(LocalContext.current, true) + MarkdownHelper.init(LocalContext.current, useCustomTabs = true, usePrivateTabs = false) val comments = listOf( sampleSecondReplyCommentView, sampleCommentView, diff --git a/app/src/main/java/com/jerboa/ui/components/common/MarkdownHelper.kt b/app/src/main/java/com/jerboa/ui/components/common/MarkdownHelper.kt index 41ff313c7..c8b6d8b1d 100644 --- a/app/src/main/java/com/jerboa/ui/components/common/MarkdownHelper.kt +++ b/app/src/main/java/com/jerboa/ui/components/common/MarkdownHelper.kt @@ -35,7 +35,7 @@ import io.noties.markwon.linkify.LinkifyPlugin object MarkdownHelper { private var markwon: Markwon? = null - fun init(context: Context, useCustomTabs: Boolean) { + fun init(context: Context, useCustomTabs: Boolean, usePrivateTabs: Boolean) { val loader = ImageLoader.Builder(context) .crossfade(true) .placeholder(R.drawable.ic_launcher_foreground) @@ -50,7 +50,7 @@ object MarkdownHelper { .usePlugin(object : AbstractMarkwonPlugin() { override fun configureConfiguration(builder: MarkwonConfiguration.Builder) { builder.linkResolver { view, link -> - openLink(link, view.context, useCustomTabs) + openLink(link, view.context, useCustomTabs, usePrivateTabs) } } }) diff --git a/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt b/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt index caa6ee779..cbdb36cb2 100644 --- a/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt @@ -146,7 +146,7 @@ fun CommunityActivity( navController.navigate(route = "post/${postView.post.id}") }, onPostLinkClick = { url -> - openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true) + openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false) }, onSaveClick = { postView -> account?.also { acct -> diff --git a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt index 13331ac5b..01ee78694 100644 --- a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt @@ -209,7 +209,7 @@ fun MainPostListingsContent( navController.navigate(route = "post/${postView.post.id}") }, onPostLinkClick = { url -> - openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true) + openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false) }, onSaveClick = { postView -> account?.also { acct -> diff --git a/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt b/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt index 185a0eeec..565492a00 100644 --- a/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt @@ -324,7 +324,7 @@ fun UserTabs( navController.navigate(route = "post/${postView.post.id}") }, onPostLinkClick = { url -> - openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true) + openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false) }, onSaveClick = { postView -> account?.also { acct -> diff --git a/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt b/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt index da49c82cc..a01653d05 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt @@ -153,7 +153,7 @@ fun PostActivity( }, onPostClick = {}, onPostLinkClick = { url -> - openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true) + openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false) }, onSaveClick = { account?.also { acct -> diff --git a/app/src/main/java/com/jerboa/ui/components/settings/about/AboutActivity.kt b/app/src/main/java/com/jerboa/ui/components/settings/about/AboutActivity.kt index 0668a7d4d..f566bf2fe 100644 --- a/app/src/main/java/com/jerboa/ui/components/settings/about/AboutActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/settings/about/AboutActivity.kt @@ -42,6 +42,7 @@ const val mastodonLink = "https://mastodon.social/@LemmyDev" fun AboutActivity( navController: NavController, useCustomTabs: Boolean, + usePrivateTabs: Boolean, ) { Log.d("jerboa", "Got to About activity") @@ -52,6 +53,10 @@ fun AboutActivity( val snackbarHostState = remember { SnackbarHostState() } + fun openLink(link: String) { + openLink(link, ctx, useCustomTabs, usePrivateTabs) + } + Scaffold( snackbarHost = { SnackbarHost(snackbarHostState) }, topBar = { @@ -69,7 +74,7 @@ fun AboutActivity( ) }, onClick = { - openLink("$githubUrl/blob/main/RELEASES.md", ctx, useCustomTabs) + openLink("$githubUrl/blob/main/RELEASES.md", ctx, useCustomTabs, usePrivateTabs) }, ) SettingsDivider() @@ -83,7 +88,7 @@ fun AboutActivity( ) }, onClick = { - openLink("$githubUrl/issues", ctx, useCustomTabs) + openLink("$githubUrl/issues") }, ) SettingsMenuLink( @@ -95,7 +100,7 @@ fun AboutActivity( ) }, onClick = { - openLink(jerboaMatrixChat, ctx, useCustomTabs) + openLink(jerboaMatrixChat) }, ) SettingsMenuLink( @@ -107,7 +112,7 @@ fun AboutActivity( ) }, onClick = { - openLink(donateLink, ctx, useCustomTabs) + openLink(donateLink) }, ) SettingsDivider() @@ -122,7 +127,7 @@ fun AboutActivity( ) }, onClick = { - openLink(jerboaLemmyLink, ctx, useCustomTabs) + openLink(jerboaLemmyLink) }, ) SettingsMenuLink( @@ -134,7 +139,7 @@ fun AboutActivity( ) }, onClick = { - openLink(mastodonLink, ctx, useCustomTabs) + openLink(mastodonLink) }, ) SettingsDivider() @@ -155,7 +160,7 @@ fun AboutActivity( ) }, onClick = { - openLink(githubUrl, ctx, useCustomTabs) + openLink(githubUrl) }, ) } @@ -183,5 +188,5 @@ fun SettingsHeader( @Preview @Composable fun AboutPreview() { - AboutActivity(navController = rememberNavController(), useCustomTabs = false) + AboutActivity(navController = rememberNavController(), useCustomTabs = false, usePrivateTabs = false) } diff --git a/app/src/main/java/com/jerboa/ui/components/settings/lookandfeel/LookAndFeelActivity.kt b/app/src/main/java/com/jerboa/ui/components/settings/lookandfeel/LookAndFeelActivity.kt index 8dc274f58..a0fb6df89 100644 --- a/app/src/main/java/com/jerboa/ui/components/settings/lookandfeel/LookAndFeelActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/settings/lookandfeel/LookAndFeelActivity.kt @@ -60,6 +60,7 @@ fun LookAndFeelActivity( settings?.showVotingArrowsInListView ?: true, ) val useCustomTabsState = rememberBooleanSettingState(settings?.useCustomTabs ?: true) + val usePrivateTabsState = rememberBooleanSettingState(settings?.usePrivateTabs ?: false) val snackbarHostState = remember { SnackbarHostState() } @@ -79,6 +80,7 @@ fun LookAndFeelActivity( showCommentActionBarByDefault = showCommentActionBarByDefaultState.value, showVotingArrowsInListView = showVotingArrowsInListViewState.value, useCustomTabs = useCustomTabsState.value, + usePrivateTabs = usePrivateTabsState.value, ), ) } @@ -200,6 +202,13 @@ fun LookAndFeelActivity( }, onCheckedChange = { updateAppSettings() }, ) + SettingsCheckbox( + state = usePrivateTabsState, + title = { + Text(text = stringResource(id = R.string.look_and_feel_use_private_tabs)) + }, + onCheckedChange = { updateAppSettings() }, + ) } }, ) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4f460e017..9661fcd98 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -189,6 +189,7 @@ List Show navigation bar Use custom tabs + Use private custom tabs if available Settings Look and feel %1$s settings