Skip to content

Commit

Permalink
Add Mastodon Redirect support and "Open Link" share target
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharee committed Jan 18, 2024
1 parent aaf8cf5 commit 36bf80d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
34 changes: 28 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,55 +58,77 @@
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden|screenLayout|smallestScreenSize"
android:launchMode="singleTask"
android:exported="true">

<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.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/app/pachli/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,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 = ComposeActivityIntent.getOptions(intent)

Expand Down Expand Up @@ -1190,6 +1198,14 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
}

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 {
private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13
private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/app/pachli/components/view/ViewLinkActivity.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/app/pachli/util/LinkHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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 @@ -764,4 +764,6 @@ Your description here:\n\n

----\n
</string>

<string name="open_link">Open Link</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 @@ -166,4 +166,17 @@
<item name="materialDrawerMaskDrawable">@drawable/materialdrawer_shape_small</item>
<item name="materialDrawerDrawCircularShadow">false</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>

0 comments on commit 36bf80d

Please sign in to comment.