Skip to content

Commit

Permalink
Merge pull request #19768 from wordpress-mobile/issue/add-media-deeplink
Browse files Browse the repository at this point in the history
Support media deep links
  • Loading branch information
zwarm authored Dec 13, 2023
2 parents 3c7e9ce + 61fc769 commit a88d8c4
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 25 deletions.
1 change: 1 addition & 0 deletions WordPress/src/jetpack/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<data android:host="post" />
<data android:host="notifications" />
<data android:host="home" />
<data android:host="media" />
</intent-filter>
</activity>

Expand Down
39 changes: 21 additions & 18 deletions WordPress/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -403,24 +403,14 @@
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="viewpost"
android:scheme="wordpress" />
<data
android:host="stats"
android:scheme="wordpress" />
<data
android:host="read"
android:scheme="wordpress" />
<data
android:host="post"
android:scheme="wordpress" />
<data
android:host="notifications"
android:scheme="wordpress" />
<data
android:host="home"
android:scheme="wordpress" />
<data android:scheme="wordpress"/>
<data android:host="viewpost"/>
<data android:host="stats"/>
<data android:host="read"/>
<data android:host="post"/>
<data android:host="notifications"/>
<data android:host="home"/>
<data android:host="media"/>
</intent-filter>
</activity-alias>

Expand Down Expand Up @@ -477,6 +467,19 @@
android:host="wordpress.com"
android:pathPattern="/notifications/.*"
android:scheme="http" />

<data
android:host="wordpress.com"
android:pathPattern="/media/..*"
android:scheme="http" />

<data
android:host="wordpress.com"
android:pathPattern="/media/.*"
android:scheme="http" />



</intent-filter>
</activity-alias>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package org.wordpress.android.ui

import android.content.Context
import android.content.Intent
import androidx.core.app.TaskStackBuilder
import org.wordpress.android.R
import org.wordpress.android.WordPress
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.ui.blaze.BlazeFlowSource
import org.wordpress.android.ui.blaze.blazecampaigns.ARG_EXTRA_BLAZE_CAMPAIGN_PAGE
import org.wordpress.android.ui.blaze.blazecampaigns.BlazeCampaignPage
Expand All @@ -15,10 +20,15 @@ import org.wordpress.android.ui.domains.management.purchasedomain.PurchaseDomain
import org.wordpress.android.ui.domains.management.purchasedomain.PurchaseDomainActivity.Companion.PICKED_DOMAIN_KEY
import org.wordpress.android.ui.domains.management.purchasedomain.PurchaseDomainActivity.Companion.PICKED_DOMAIN_PRIVACY
import org.wordpress.android.ui.domains.management.purchasedomain.PurchaseDomainActivity.Companion.PICKED_PRODUCT_ID
import org.wordpress.android.ui.main.WPMainActivity
import org.wordpress.android.ui.media.MediaBrowserActivity
import org.wordpress.android.ui.media.MediaBrowserType
import org.wordpress.android.ui.mysite.menu.KEY_QUICK_START_EVENT
import org.wordpress.android.ui.mysite.menu.MenuActivity
import org.wordpress.android.ui.mysite.personalization.PersonalizationActivity
import org.wordpress.android.ui.quickstart.QuickStartEvent
import org.wordpress.android.util.ToastUtils
import org.wordpress.android.util.analytics.AnalyticsUtils
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -92,4 +102,42 @@ class ActivityNavigator @Inject constructor() {
.putExtra(PICKED_DOMAIN_PRIVACY, domainSupportsPrivacy)
)
}

fun viewCurrentBlogMedia(context: Context, site: SiteModel?) {
val intent = Intent(context, MediaBrowserActivity::class.java)
intent.putExtra(WordPress.SITE, site)
intent.putExtra(MediaBrowserActivity.ARG_BROWSER_TYPE, MediaBrowserType.BROWSER)
context.startActivity(intent)
AnalyticsUtils.trackWithSiteDetails(AnalyticsTracker.Stat.OPENED_MEDIA_LIBRARY, site)
}

fun openMediaInNewStack(context: Context, site: SiteModel?) {
if (site == null) {
ToastUtils.showToast(context, R.string.media_cannot_be_started, ToastUtils.Duration.SHORT)
return
}
AnalyticsUtils.trackWithSiteDetails(AnalyticsTracker.Stat.OPENED_MEDIA_LIBRARY, site)
val taskStackBuilder = TaskStackBuilder.create(context)
val mainActivityIntent = getMainActivityInNewStack(context)
val intent = Intent(context, MediaBrowserActivity::class.java)
intent.putExtra(WordPress.SITE, site)
taskStackBuilder
.addNextIntent(mainActivityIntent)
.addNextIntent(intent)
.startActivities()
}

fun openMediaInNewStack(context: Context) {
AnalyticsTracker.track(AnalyticsTracker.Stat.OPENED_MEDIA_LIBRARY)
val intent = getMainActivityInNewStack(context)
intent.putExtra(WPMainActivity.ARG_OPEN_PAGE, WPMainActivity.ARG_MEDIA)
context.startActivity(intent)
}

private fun getMainActivityInNewStack(context: Context): Intent {
val mainActivityIntent = Intent(context, WPMainActivity::class.java)
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
return mainActivityIntent
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.ui.ActivityLauncher
import org.wordpress.android.ui.ActivityNavigator
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.LoginForResult
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenEditor
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenEditorForPost
Expand Down Expand Up @@ -31,7 +32,7 @@ import org.wordpress.android.util.UriWrapper
import javax.inject.Inject

class DeepLinkNavigator
@Inject constructor() {
@Inject constructor(private val activityNavigator: ActivityNavigator) {
@Suppress("ComplexMethod")
fun handleNavigationAction(navigateAction: NavigateAction, activity: AppCompatActivity) {
when (navigateAction) {
Expand Down Expand Up @@ -83,6 +84,8 @@ class DeepLinkNavigator
ActivityLauncher.openJetpackForDeeplink(activity, navigateAction.action, navigateAction.uri)
is NavigateAction.OpenJetpackStaticPosterView ->
ActivityLauncher.showJetpackStaticPoster(activity)
is NavigateAction.OpenMediaForSite -> activityNavigator.openMediaInNewStack(activity, navigateAction.site)
NavigateAction.OpenMedia -> activityNavigator.openMediaInNewStack(activity)
}
if (navigateAction != LoginForResult) {
activity.finish()
Expand Down Expand Up @@ -114,5 +117,7 @@ class DeepLinkNavigator
object OpenLoginPrologue : NavigateAction()
data class OpenJetpackForDeepLink(val action: String?, val uri: UriWrapper) : NavigateAction()
object OpenJetpackStaticPosterView : NavigateAction()
data class OpenMediaForSite(val site: SiteModel) : NavigateAction()
object OpenMedia : NavigateAction()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class DeepLinkHandlers
pagesLinkHandler: PagesLinkHandler,
notificationsLinkHandler: NotificationsLinkHandler,
qrCodeAuthLinkHandler: QRCodeAuthLinkHandler,
homeLinkHandler: HomeLinkHandler
homeLinkHandler: HomeLinkHandler,
mediaLinkHandler: MediaLinkHandler
) {
private val handlers = listOf(
editorLinkHandler,
Expand All @@ -26,7 +27,8 @@ class DeepLinkHandlers
pagesLinkHandler,
notificationsLinkHandler,
qrCodeAuthLinkHandler,
homeLinkHandler
homeLinkHandler,
mediaLinkHandler
)

private val _toast by lazy {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.wordpress.android.ui.deeplinks.handlers

import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenMedia
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenMediaForSite
import org.wordpress.android.ui.deeplinks.DeepLinkUriUtils
import org.wordpress.android.ui.deeplinks.DeepLinkingIntentReceiverViewModel
import org.wordpress.android.ui.deeplinks.DeepLinkingIntentReceiverViewModel.Companion.HOST_WORDPRESS_COM
import org.wordpress.android.ui.deeplinks.DeepLinkingIntentReceiverViewModel.Companion.SITE_DOMAIN
import org.wordpress.android.util.UriWrapper
import javax.inject.Inject

class MediaLinkHandler
@Inject constructor(private val deepLinkUriUtils: DeepLinkUriUtils) : DeepLinkHandler {
/**
* Returns true if the URI looks like `wordpress.com/media`
*/
override fun shouldHandleUrl(uri: UriWrapper): Boolean {
return (uri.host == HOST_WORDPRESS_COM &&
uri.pathSegments.firstOrNull() == MEDIA_PATH) || uri.host == MEDIA_PATH
}

override fun buildNavigateAction(uri: UriWrapper): NavigateAction {
val targetHost: String = uri.lastPathSegment ?: ""
val site: SiteModel? = deepLinkUriUtils.hostToSite(targetHost)
return if (site != null) {
OpenMediaForSite(site)
} else {
// In other cases, launch the home view for the currently selected site.
OpenMedia
}
}

override fun stripUrl(uri: UriWrapper): String {
return buildString {
val offset = if (uri.host == MEDIA_PATH) {
append(DeepLinkingIntentReceiverViewModel.APPLINK_SCHEME)
0
} else {
append("$HOST_WORDPRESS_COM/")
1
}
append(MEDIA_PATH)
val pathSegments = uri.pathSegments
val size = pathSegments.size
val hasSiteUrl = if (size > offset + 1) pathSegments.getOrNull(offset + 1) != null else false
if (hasSiteUrl) {
append("/$SITE_DOMAIN")
}
}
}

companion object {
private const val MEDIA_PATH = "media"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.wordpress.android.push.NotificationsProcessingService;
import org.wordpress.android.ui.ActivityId;
import org.wordpress.android.ui.ActivityLauncher;
import org.wordpress.android.ui.ActivityNavigator;
import org.wordpress.android.ui.JetpackConnectionSource;
import org.wordpress.android.ui.JetpackConnectionWebViewActivity;
import org.wordpress.android.ui.LocaleAwareActivity;
Expand All @@ -95,11 +96,11 @@
import org.wordpress.android.ui.main.WPMainNavigationView.OnPageListener;
import org.wordpress.android.ui.main.WPMainNavigationView.PageType;
import org.wordpress.android.ui.mlp.ModalLayoutPickerFragment;
import org.wordpress.android.ui.mysite.BloggingPromptsOnboardingListener;
import org.wordpress.android.ui.mysite.MySiteFragment;
import org.wordpress.android.ui.mysite.MySiteViewModel;
import org.wordpress.android.ui.mysite.SelectedSiteRepository;
import org.wordpress.android.ui.mysite.cards.quickstart.QuickStartRepository;
import org.wordpress.android.ui.mysite.BloggingPromptsOnboardingListener;
import org.wordpress.android.ui.notifications.NotificationEvents;
import org.wordpress.android.ui.notifications.NotificationsListFragment;
import org.wordpress.android.ui.notifications.SystemNotificationsTracker;
Expand Down Expand Up @@ -224,6 +225,7 @@ public class WPMainActivity extends LocaleAwareActivity implements
public static final String ARG_EDITOR_ORIGIN = "editor_origin";
public static final String ARG_CURRENT_FOCUS = "CURRENT_FOCUS";
public static final String ARG_BYPASS_MIGRATION = "bypass_migration";
public static final String ARG_MEDIA = "show_media";

// Track the first `onResume` event for the current session so we can use it for Analytics tracking
private static boolean mFirstResume = true;
Expand Down Expand Up @@ -278,6 +280,8 @@ public class WPMainActivity extends LocaleAwareActivity implements

@Inject GCMRegistrationScheduler mGCMRegistrationScheduler;

@Inject ActivityNavigator mActivityNavigator;

/*
* fragments implement this if their contents can be scrolled, called when user
* requests to scroll to the top
Expand Down Expand Up @@ -926,6 +930,12 @@ private void handleOpenPageIntent(@NonNull Intent intent) {
case ARG_BLOGGING_PROMPTS_ONBOARDING:
showBloggingPromptsOnboarding();
break;
case ARG_MEDIA:
if (!mSelectedSiteRepository.hasSelectedSite()) {
initSelectedSite();
}
mActivityNavigator.viewCurrentBlogMedia(this, getSelectedSite());
break;
}
} else {
AppLog.e(T.MAIN, "WPMainActivity.handleOpenIntent called with an invalid argument.");
Expand Down
4 changes: 3 additions & 1 deletion WordPress/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4820,6 +4820,8 @@ translators: %s: Select control option value e.g: "Auto, 25%". -->
<string name="gutenberg_native_for_this_reason_we_recommend_editing_the_block_using_the_web_edit" tools:ignore="UnusedResources" a8c-src-lib="gutenberg">For this reason, we recommend editing the block using the web editor.</string>
<string name="gutenberg_native_for_this_reason_we_recommend_editing_the_block_using_your_web_bro" tools:ignore="UnusedResources" a8c-src-lib="gutenberg">For this reason, we recommend editing the block using your web browser.</string>

<string name="media_cannot_be_started">We cannot open media at the moment. Please try again later</string>

<!-- Bloganuary -->
<string name="bloganuary_dashboard_nudge_title">Bloganuary is coming!</string>
<string name="bloganuary_dashboard_nudge_text">For the month of January, blogging prompts will come from Bloganuary - our community challenge to build a blogging habit for the new year.</string>
Expand All @@ -4834,9 +4836,9 @@ translators: %s: Select control option value e.g: "Auto, 25%". -->
<string name="bloganuary_dashboard_nudge_overlay_action_turn_on_prompts">Turn on blogging prompts</string>
<string name="bloganuary_dashboard_nudge_overlay_action_dismiss">Let’s go!</string>


<!-- State of the Word 2023 -->
<string name="wp_sotw_2023_dashboard_nudge_title" translatable="false">State of the Word 2023</string>
<string name="wp_sotw_2023_dashboard_nudge_text" translatable="false">Check out WordPress co-founder Matt Mullenweg’s annual keynote to stay on top of what’s coming in 2024 and beyond.</string>
<string name="wp_sotw_2023_dashboard_nudge_cta" translatable="false">Watch now</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class DeepLinkHandlersTest : BaseUnitTest() {
@Mock
lateinit var homeLinkHandler: HomeLinkHandler

@Mock
lateinit var mediaLinkHandler: MediaLinkHandler

@Mock
lateinit var uri: UriWrapper
private lateinit var deepLinkHandlers: DeepLinkHandlers
Expand All @@ -54,7 +57,8 @@ class DeepLinkHandlersTest : BaseUnitTest() {
pagesLinkHandler,
notificationsLinkHandler,
qrCodeAuthLinkHandler,
homeLinkHandler
homeLinkHandler,
mediaLinkHandler
)
initDeepLinkHandlers()
}
Expand All @@ -68,7 +72,8 @@ class DeepLinkHandlersTest : BaseUnitTest() {
pagesLinkHandler,
notificationsLinkHandler,
qrCodeAuthLinkHandler,
homeLinkHandler
homeLinkHandler,
mediaLinkHandler
)
}

Expand Down

0 comments on commit a88d8c4

Please sign in to comment.