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

feat: add Mastodon Redirect support and "Open Link" share target #365

Open
wants to merge 4 commits into
base: main
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
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 @@ -526,9 +526,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 @@ -1231,6 +1239,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.core.activity.BaseActivity
import app.pachli.core.navigation.MainActivityIntent
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 = MainActivityIntent(this)
launchIntent.action = "dev.zwander.mastodonredirect.intent.action.OPEN_FEDI_LINK"
launchIntent.data = link?.let { Uri.parse(it) }

startActivity(launchIntent)
finish()
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,10 @@
<string name="pref_title_update_check_now">Check for update now</string>
<string name="pref_update_check_no_updates">There are no updates available</string>
<string name="pref_update_next_scheduled_check">Next scheduled check: %1$s</string>
<string name="open_link">Open Link</string>
<string name="error_media_download">Could not download %1$s: %2$d %3$s</string>
<string name="action_add_to_tab">Add to tab</string>
<string name="action_add_to_tab_success">Added \'%1$s\' to tabs</string>
<string name="action_remove_tab">Remove tab</string>
<string name="action_manage_tabs">Manage tabs</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ abstract class BottomSheetActivity : BaseActivity() {
when (fallbackBehavior) {
PostLookupFallbackBehavior.OPEN_IN_BROWSER -> openLink(url)
PostLookupFallbackBehavior.DISPLAY_ERROR -> Toast.makeText(this, getString(R.string.post_lookup_error_format, url), Toast.LENGTH_SHORT).show()
PostLookupFallbackBehavior.OPEN_IN_BROWSER_FORCED -> openLink(url, true)
}
}

Expand Down Expand Up @@ -151,8 +152,8 @@ abstract class BottomSheetActivity : BaseActivity() {
}

@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
open fun openLink(url: String) {
(this as Context).openLink(url)
open fun openLink(url: String, forceBrowser: Boolean = false) {
(this as Context).openLink(url, forceBrowser)
}

private fun showQuerySheet() {
Expand Down Expand Up @@ -217,4 +218,5 @@ abstract class BottomSheetActivity : BaseActivity() {
enum class PostLookupFallbackBehavior {
OPEN_IN_BROWSER,
DISPLAY_ERROR,
OPEN_IN_BROWSER_FORCED,
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import timber.log.Timber
* @param uri the uri to open
* @param context context
*/
fun openLinkInCustomTab(uri: Uri, context: Context) {
fun openLinkInCustomTab(uri: Uri, context: Context, forceBrowser: Boolean = false) {
val toolbarColor = MaterialColors.getColor(context, com.google.android.material.R.attr.colorSurface, Color.BLACK)
val navigationbarColor = MaterialColors.getColor(context, android.R.attr.navigationBarColor, Color.BLACK)
val navigationbarDividerColor = MaterialColors.getColor(context, com.google.android.material.R.attr.dividerColor, Color.BLACK)
Expand All @@ -55,7 +55,7 @@ fun openLinkInCustomTab(uri: Uri, context: Context) {
customTabsIntent.launchUrl(context, uri)
} catch (e: ActivityNotFoundException) {
Timber.w(e, "Activity was not found for intent %s", customTabsIntent)
openLinkInBrowser(uri, context)
openLinkInBrowser(uri, context, forceBrowser)
}
}

Expand All @@ -65,8 +65,14 @@ fun openLinkInCustomTab(uri: Uri, context: Context) {
* @param uri the uri to open
* @param context context
*/
private fun openLinkInBrowser(uri: Uri?, context: Context) {
private fun openLinkInBrowser(uri: Uri?, context: Context, forceBrowser: Boolean = false) {
val intent = Intent(Intent.ACTION_VIEW, uri)

if (forceBrowser) {
// Forces the link to open in the browser instead of another deep-linked app.
intent.selector = Intent(Intent.ACTION_VIEW, Uri.parse("https://"))
}

try {
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
Expand All @@ -80,13 +86,13 @@ private fun openLinkInBrowser(uri: Uri?, context: Context) {
* @receiver the Context to open the link from
* @param url a string containing the url to open
*/
fun Context.openLink(url: String) {
fun Context.openLink(url: String, forceBrowser: Boolean = false) {
val uri = url.toUri().normalizeScheme()
val useCustomTabs = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(PrefKeys.CUSTOM_TABS, false)

if (useCustomTabs) {
openLinkInCustomTab(uri, this)
openLinkInCustomTab(uri, this, forceBrowser)
} else {
openLinkInBrowser(uri, this)
openLinkInBrowser(uri, this, forceBrowser)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class BottomSheetActivityTest {
bottomSheet = mock()
}

override fun openLink(url: String) {
override fun openLink(url: String, forceBrowser: Boolean) {
this.link = url
}

Expand Down
13 changes: 13 additions & 0 deletions core/designsystem/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,17 @@
<style name="snackbar_text" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
<item name="android:maxLines">5</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>
Loading