Merge pull request #5329 from ByteHamster/subscribe-deeplink

Add subscription deeplink
This commit is contained in:
ByteHamster 2021-08-06 17:26:59 +02:00 committed by GitHub
commit d3c020b8df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 7 deletions

View File

@ -301,6 +301,19 @@
<data android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="antennapod.org" />
<data android:host="www.antennapod.org" />
<data android:pathPrefix="/deeplink/subscribe" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>

View File

@ -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;

View File

@ -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";