From 36bf80d1062593f294f20bc3800b422eb4cd0827 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Wed, 17 Jan 2024 23:24:14 -0500 Subject: [PATCH] Add Mastodon Redirect support and "Open Link" share target --- app/src/main/AndroidManifest.xml | 34 +++++++++++++++---- app/src/main/java/app/pachli/MainActivity.kt | 16 +++++++++ .../components/view/ViewLinkActivity.kt | 26 ++++++++++++++ .../main/java/app/pachli/util/LinkHelper.kt | 4 +++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/values/styles.xml | 13 +++++++ 6 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/app/pachli/components/view/ViewLinkActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ddd01b4a94..ce1643a7ee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -58,44 +58,45 @@ - + - + - + - + - + - + @@ -103,10 +104,31 @@ + + + + + + + + + + + + + + + + + + viewUrl(url, PostLookupFallbackBehavior.OPEN_IN_BROWSER) + } + } + } + companion object { private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13 private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14 diff --git a/app/src/main/java/app/pachli/components/view/ViewLinkActivity.kt b/app/src/main/java/app/pachli/components/view/ViewLinkActivity.kt new file mode 100644 index 0000000000..1f91570787 --- /dev/null +++ b/app/src/main/java/app/pachli/components/view/ViewLinkActivity.kt @@ -0,0 +1,26 @@ +package app.pachli.components.view + +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import app.pachli.BaseActivity +import app.pachli.MainActivity +import dagger.hilt.android.AndroidEntryPoint + +@AndroidEntryPoint +class ViewLinkActivity : BaseActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + if (intent?.action == Intent.ACTION_SEND) { + val link = intent.getStringExtra(Intent.EXTRA_TEXT) + + val launchIntent = Intent(this, MainActivity::class.java) + launchIntent.action = "dev.zwander.mastodonredirect.intent.action.OPEN_FEDI_LINK" + launchIntent.data = link?.let { Uri.parse(it) } + + startActivity(launchIntent) + finish() + } + } +} diff --git a/app/src/main/java/app/pachli/util/LinkHelper.kt b/app/src/main/java/app/pachli/util/LinkHelper.kt index 98c1b29fdb..c894093ad8 100644 --- a/app/src/main/java/app/pachli/util/LinkHelper.kt +++ b/app/src/main/java/app/pachli/util/LinkHelper.kt @@ -266,6 +266,10 @@ fun Context.openLink(url: String) { */ private fun openLinkInBrowser(uri: Uri?, context: Context) { val intent = Intent(Intent.ACTION_VIEW, uri) + + // Makes sure the Intent opens in the browser instead of something like Mastodon Redirect. + intent.selector = Intent(Intent.ACTION_VIEW, Uri.parse("https://")) + try { context.startActivity(intent) } catch (e: ActivityNotFoundException) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cfb3ac77bf..60e0d77c43 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -764,4 +764,6 @@ Your description here:\n\n ----\n + + Open Link diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 9d5628fa03..78317f47f3 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -166,4 +166,17 @@ @drawable/materialdrawer_shape_small false + +