Skip to content

Commit 65e7803

Browse files
committed
feat: add Mastodon Redirect support and "Open Link" share target
1 parent 741bf56 commit 65e7803

6 files changed

Lines changed: 89 additions & 6 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,55 +58,77 @@
5858
<activity
5959
android:name=".MainActivity"
6060
android:configChanges="orientation|screenSize|keyboardHidden|screenLayout|smallestScreenSize"
61+
android:launchMode="singleTask"
6162
android:exported="true">
6263

63-
<intent-filter>
64+
<intent-filter android:label="@string/action_compose">
6465
<action android:name="android.intent.action.SEND" />
6566

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

6869
<data android:mimeType="text/plain" />
6970
</intent-filter>
70-
<intent-filter>
71+
<intent-filter android:label="@string/action_compose">
7172
<action android:name="android.intent.action.SEND" />
7273

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

7576
<data android:mimeType="image/*" />
7677
</intent-filter>
77-
<intent-filter>
78+
<intent-filter android:label="@string/action_compose">
7879
<action android:name="android.intent.action.SEND" />
7980

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

8283
<data android:mimeType="video/*" />
8384
</intent-filter>
84-
<intent-filter>
85+
<intent-filter android:label="@string/action_compose">
8586
<action android:name="android.intent.action.SEND_MULTIPLE" />
8687

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

8990
<data android:mimeType="image/*" />
9091
</intent-filter>
91-
<intent-filter>
92+
<intent-filter android:label="@string/action_compose">
9293
<action android:name="android.intent.action.SEND_MULTIPLE" />
9394

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

9697
<data android:mimeType="video/*" />
9798
</intent-filter>
98-
<intent-filter>
99+
<intent-filter android:label="@string/action_compose">
99100
<action android:name="android.intent.action.SEND" />
100101

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

103104
<data android:mimeType="audio/*" />
104105
</intent-filter>
105106

107+
<intent-filter>
108+
<action android:name="dev.zwander.mastodonredirect.intent.action.OPEN_FEDI_LINK" />
109+
110+
<category android:name="android.intent.category.DEFAULT" />
111+
</intent-filter>
112+
106113
<meta-data
107114
android:name="android.service.chooser.chooser_target_service"
108115
android:value="androidx.sharetarget.ChooserTargetServiceCompat" />
109116

117+
</activity>
118+
<activity
119+
android:name=".components.view.ViewLinkActivity"
120+
android:excludeFromRecents="true"
121+
android:exported="true"
122+
android:theme="@style/NullTheme">
123+
124+
<intent-filter android:label="@string/open_link">
125+
<action android:name="android.intent.action.SEND" />
126+
127+
<category android:name="android.intent.category.DEFAULT" />
128+
129+
<data android:mimeType="text/plain" />
130+
</intent-filter>
131+
110132
</activity>
111133
<activity
112134
android:name=".components.compose.ComposeActivity"

app/src/main/java/app/pachli/MainActivity.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,17 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
536536
if (redirectUrl != null) {
537537
viewUrl(redirectUrl, PostLookupFallbackBehavior.DISPLAY_ERROR)
538538
}
539+
540+
handleMastodonRedirectIntent(intent)
539541
}
540542
}
541543

544+
override fun onNewIntent(intent: Intent?) {
545+
super.onNewIntent(intent)
546+
547+
handleMastodonRedirectIntent(intent)
548+
}
549+
542550
private fun forwardToComposeActivity(intent: Intent) {
543551
val composeOptions = ComposeActivityIntent.getOptions(intent)
544552

@@ -1163,6 +1171,14 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
11631171
}
11641172
}
11651173

1174+
private fun handleMastodonRedirectIntent(intent: Intent?) {
1175+
if (intent?.action == "dev.zwander.mastodonredirect.intent.action.OPEN_FEDI_LINK") {
1176+
intent.dataString?.let { url ->
1177+
viewUrl(url, PostLookupFallbackBehavior.OPEN_IN_BROWSER)
1178+
}
1179+
}
1180+
}
1181+
11661182
companion object {
11671183
private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13
11681184
private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package app.pachli.components.view
2+
3+
import android.content.Intent
4+
import android.net.Uri
5+
import android.os.Bundle
6+
import app.pachli.BaseActivity
7+
import app.pachli.core.navigation.MainActivityIntent
8+
import dagger.hilt.android.AndroidEntryPoint
9+
10+
@AndroidEntryPoint
11+
class ViewLinkActivity : BaseActivity() {
12+
override fun onCreate(savedInstanceState: Bundle?) {
13+
super.onCreate(savedInstanceState)
14+
15+
if (intent?.action == Intent.ACTION_SEND) {
16+
val link = intent.getStringExtra(Intent.EXTRA_TEXT)
17+
18+
val launchIntent = MainActivityIntent(this)
19+
launchIntent.action = "dev.zwander.mastodonredirect.intent.action.OPEN_FEDI_LINK"
20+
launchIntent.data = link?.let { Uri.parse(it) }
21+
22+
startActivity(launchIntent)
23+
finish()
24+
}
25+
}
26+
}

app/src/main/java/app/pachli/util/LinkHelper.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ fun Context.openLink(url: String) {
266266
*/
267267
private fun openLinkInBrowser(uri: Uri?, context: Context) {
268268
val intent = Intent(Intent.ACTION_VIEW, uri)
269+
270+
// Makes sure the Intent opens in the browser instead of something like Mastodon Redirect.
271+
intent.selector = Intent(Intent.ACTION_VIEW, Uri.parse("https://"))
272+
269273
try {
270274
context.startActivity(intent)
271275
} catch (e: ActivityNotFoundException) {

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,4 +773,6 @@ Your description here:\n\n
773773
<string name="server_repository_error_validate_node_info">validating nodeinfo %1$s failed: %2$s</string>
774774
<string name="server_repository_error_get_instance_info">fetching /api/v1/instance failed: %1$s</string>
775775
<string name="server_repository_error_capabilities">parsing server capabilities failed: %1$s</string>
776+
777+
<string name="open_link">Open Link</string>
776778
</resources>

app/src/main/res/values/styles.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,17 @@
181181
<style name="snackbar_text" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
182182
<item name="android:maxLines">5</item>
183183
</style>
184+
185+
<style name="NullTheme" parent="Theme.Material3.DynamicColors.DayNight">
186+
<item name="android:windowIsTranslucent">true</item>
187+
<item name="android:windowBackground">@android:color/transparent</item>
188+
<item name="android:windowContentOverlay">@null</item>
189+
<item name="android:windowNoTitle">true</item>
190+
<item name="android:windowIsFloating">true</item>
191+
<item name="android:backgroundDimEnabled">false</item>
192+
<item name="android:windowEnterAnimation">@null</item>
193+
<item name="android:windowExitAnimation">@null</item>
194+
<item name="android:windowNoDisplay">true</item>
195+
<item name="android:windowActionBar">false</item>
196+
</style>
184197
</resources>

0 commit comments

Comments
 (0)