Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mastodon Redirect support #4194

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,56 +40,62 @@
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden|screenLayout|smallestScreenSize"
android:launchMode="singleTask"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to add this so Tusky could receive links while it's still running in the background. I don't think it breaks anything, but let me know.

android:exported="true"
android:theme="@style/SplashTheme">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>

<intent-filter android:label="@string/action_compose">
<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<intent-filter android:label="@string/action_compose">
<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<intent-filter android:label="@string/action_compose">
<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="video/*" />
</intent-filter>
<intent-filter>
<intent-filter android:label="@string/action_compose">
<action android:name="android.intent.action.SEND_MULTIPLE" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<intent-filter android:label="@string/action_compose">
<action android:name="android.intent.action.SEND_MULTIPLE" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="video/*" />
</intent-filter>
<intent-filter>
<intent-filter android:label="@string/action_compose">
<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="audio/*" />
</intent-filter>
<intent-filter>
<action android:name="dev.zwander.mastodonredirect.intent.action.OPEN_FEDI_LINK" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<meta-data
android:name="android.app.shortcuts"
Expand All @@ -98,6 +104,21 @@
android:name="android.service.chooser.chooser_target_service"
android:value="androidx.sharetarget.ChooserTargetServiceCompat" />

</activity>
<activity
android:name=".components.view.ViewLinkActivity"
android:excludeFromRecents="true"
android:exported="true"
android:theme="@style/NullTheme">

<intent-filter android:label="@string/open_link">
<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="text/plain" />
</intent-filter>

</activity>
<activity
android:name=".components.compose.ComposeActivity"
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/keylesspalace/tusky/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,17 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
if (redirectUrl != null) {
viewUrl(redirectUrl, PostLookupFallbackBehavior.DISPLAY_ERROR)
}

handleMastodonRedirectIntent(intent)
}
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)

handleMastodonRedirectIntent(intent)
}

private fun forwardToComposeActivity(intent: Intent) {
val composeOptions =
intent.getParcelableExtraCompat<ComposeActivity.ComposeOptions>(COMPOSE_OPTIONS)
Expand Down Expand Up @@ -1201,6 +1209,14 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {

override fun getActionButton() = binding.composeButton

private fun handleMastodonRedirectIntent(intent: Intent?) {
if (intent?.action == "dev.zwander.mastodonredirect.intent.action.OPEN_FEDI_LINK") {
intent.dataString?.let { url ->
viewUrl(url, PostLookupFallbackBehavior.OPEN_IN_BROWSER)
}
}
}

companion object {
const val OPEN_WITH_EXPLODE_ANIMATION = "explode"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.keylesspalace.tusky.components.view

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.keylesspalace.tusky.BaseActivity
import com.keylesspalace.tusky.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()
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,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://"))
Comment on lines +314 to +316
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A blank-URL selector guarantees the link won't accidentally open in an app that supports the domain (e.g., Mastodon Redirect, which could cause a launch loop).


try {
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -853,5 +853,7 @@
<string name="list_reply_policy_followed">Any followed user</string>
<string name="list_reply_policy_label">Show replies to</string>

<string name="open_link">Open Link</string>

<string name="unknown_notification_type">Unknown notification type</string>
</resources>
13 changes: 13 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,17 @@
<item name="android:layout_marginLeft">20dp</item>
<item name="android:layout_marginRight">20dp</item>
</style>

<style name="NullTheme" parent="Theme.Material3.DynamicColors.DayNight">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowEnterAnimation">@null</item>
<item name="android:windowExitAnimation">@null</item>
<item name="android:windowNoDisplay">true</item>
<item name="android:windowActionBar">false</item>
</style>
</resources>