From f55ed54e1f332c8b96636a9fb90aa413158df1d5 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Thu, 5 Aug 2021 20:58:54 +0200 Subject: [PATCH] Add subscription deeplink --- app/src/main/AndroidManifest.xml | 13 ++++++++++ .../antennapod/core/util/URLChecker.java | 24 +++++++++++++------ .../antennapod/core/util/URLCheckerTest.java | 16 +++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2f944dfe6..47648f9d3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -301,6 +301,19 @@ + + + + + + + + + + + + + diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/URLChecker.java b/core/src/main/java/de/danoeh/antennapod/core/util/URLChecker.java index cb7db1709..1ac068307 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/URLChecker.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/URLChecker.java @@ -5,9 +5,10 @@ import android.text.TextUtils; import androidx.annotation.NonNull; import android.util.Log; -import de.danoeh.antennapod.core.BuildConfig; import okhttp3.HttpUrl; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; @@ -28,6 +29,7 @@ public final class URLChecker { private static final String TAG = "URLChecker"; private static final String AP_SUBSCRIBE = "antennapod-subscribe://"; + private static final String AP_SUBSCRIBE_DEEPLINK = "antennapod.org/deeplink/subscribe?url="; /** * Checks if URL is valid and modifies it if necessary. @@ -39,22 +41,30 @@ public final class URLChecker { url = url.trim(); String lowerCaseUrl = url.toLowerCase(); // protocol names are case insensitive if (lowerCaseUrl.startsWith("feed://")) { - if (BuildConfig.DEBUG) Log.d(TAG, "Replacing feed:// with http://"); + Log.d(TAG, "Replacing feed:// with http://"); return prepareURL(url.substring("feed://".length())); } else if (lowerCaseUrl.startsWith("pcast://")) { - if (BuildConfig.DEBUG) Log.d(TAG, "Removing pcast://"); + Log.d(TAG, "Removing pcast://"); return prepareURL(url.substring("pcast://".length())); } else if (lowerCaseUrl.startsWith("pcast:")) { - if (BuildConfig.DEBUG) Log.d(TAG, "Removing pcast:"); + Log.d(TAG, "Removing pcast:"); return prepareURL(url.substring("pcast:".length())); } else if (lowerCaseUrl.startsWith("itpc")) { - if (BuildConfig.DEBUG) Log.d(TAG, "Replacing itpc:// with http://"); + Log.d(TAG, "Replacing itpc:// with http://"); return prepareURL(url.substring("itpc://".length())); } else if (lowerCaseUrl.startsWith(AP_SUBSCRIBE)) { - if (BuildConfig.DEBUG) Log.d(TAG, "Removing antennapod-subscribe://"); + Log.d(TAG, "Removing antennapod-subscribe://"); return prepareURL(url.substring(AP_SUBSCRIBE.length())); + } else if (lowerCaseUrl.contains(AP_SUBSCRIBE_DEEPLINK)) { + Log.d(TAG, "Removing " + AP_SUBSCRIBE_DEEPLINK); + String removedWebsite = url.substring(url.indexOf(AP_SUBSCRIBE_DEEPLINK) + AP_SUBSCRIBE_DEEPLINK.length()); + try { + return prepareURL(URLDecoder.decode(removedWebsite, "UTF-8")); + } catch (UnsupportedEncodingException e) { + return prepareURL(removedWebsite); + } } else if (!(lowerCaseUrl.startsWith("http://") || lowerCaseUrl.startsWith("https://"))) { - if (BuildConfig.DEBUG) Log.d(TAG, "Adding http:// at the beginning of the URL"); + Log.d(TAG, "Adding http:// at the beginning of the URL"); return "http://" + url; } else { return url; diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/URLCheckerTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/URLCheckerTest.java index a4b3dee06..c20398ffc 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/util/URLCheckerTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/util/URLCheckerTest.java @@ -4,6 +4,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -98,6 +101,19 @@ public class URLCheckerTest { assertEquals("https://example.com", out); } + @Test + public void testAntennaPodSubscribeDeeplink() throws UnsupportedEncodingException { + final String feed = "http://example.org/podcast.rss"; + assertEquals(feed, URLChecker.prepareURL("https://antennapod.org/deeplink/subscribe?url=" + feed)); + assertEquals(feed, URLChecker.prepareURL("http://antennapod.org/deeplink/subscribe?url=" + feed)); + assertEquals(feed, URLChecker.prepareURL("https://www.antennapod.org/deeplink/subscribe?url=" + feed)); + assertEquals(feed, URLChecker.prepareURL("http://www.antennapod.org/deeplink/subscribe?url=" + feed)); + assertEquals(feed, URLChecker.prepareURL("http://www.antennapod.org/deeplink/subscribe?url=" + + URLEncoder.encode(feed, "UTF-8"))); + assertEquals(feed, URLChecker.prepareURL("http://www.antennapod.org/deeplink/subscribe?url=" + + "example.org/podcast.rss")); + } + @Test public void testProtocolRelativeUrlIsAbsolute() { final String in = "https://example.com";