diff options
author | dvidak <vidakovicd94@gmail.com> | 2024-01-22 18:09:27 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-22 18:09:27 +0000 |
commit | af7b27666732c384ffd33c1971f78080478a33d6 (patch) | |
tree | 0653f26fbe1f603e0abca696d2e8b2b6aae8e4fe /app/src/main | |
parent | 7777a6d6756b6b2631ea24548bd69ff42bd39034 (diff) | |
download | infinity-for-reddit-af7b27666732c384ffd33c1971f78080478a33d6.tar infinity-for-reddit-af7b27666732c384ffd33c1971f78080478a33d6.tar.gz infinity-for-reddit-af7b27666732c384ffd33c1971f78080478a33d6.tar.bz2 infinity-for-reddit-af7b27666732c384ffd33c1971f78080478a33d6.tar.lz infinity-for-reddit-af7b27666732c384ffd33c1971f78080478a33d6.tar.xz infinity-for-reddit-af7b27666732c384ffd33c1971f78080478a33d6.tar.zst infinity-for-reddit-af7b27666732c384ffd33c1971f78080478a33d6.zip |
feat: Add ShareLinkHandler to handle resolving of new reddit share link URLs (#1580)
* Add ShareLinkHandler to handle resolving of new reddit share
link URLs
* Fix Retrofit injection in LinkResolverActivity and
ShareLinkHandler
Diffstat (limited to '')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java | 27 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/utils/ShareLinkHandler.java | 37 |
2 files changed, 63 insertions, 1 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java index e5346178..eb1518d6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java @@ -23,10 +23,13 @@ import java.util.List; import javax.inject.Inject; import javax.inject.Named; +import retrofit2.Retrofit; + import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; +import ml.docilealligator.infinityforreddit.utils.ShareLinkHandler; public class LinkResolverActivity extends AppCompatActivity { @@ -40,6 +43,8 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String COMMENT_PATTERN = "/(r|u|U|user)/[\\w-]+/comments/\\w+/?[\\w-]+/\\w+/?"; private static final String SUBREDDIT_PATTERN = "/[rR]/[\\w-]+/?"; private static final String USER_PATTERN = "/(u|U|user)/[\\w-]+/?"; + private static final String SHARELINK_SUBREDDIT_PATTERN = "/r/[\\w-]+/s/[\\w-]+"; + private static final String SHARELINK_USER_PATTERN = "/u/[\\w-]+/s/[\\w-]+"; private static final String SIDEBAR_PATTERN = "/[rR]/[\\w-]+/about/sidebar"; private static final String MULTIREDDIT_PATTERN = "/user/[\\w-]+/m/\\w+/?"; private static final String MULTIREDDIT_PATTERN_2 = "/[rR]/(\\w+\\+?)+/?"; @@ -51,13 +56,20 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String WIKI_PATTERN = "/[rR]/[\\w-]+/(wiki|w)(?:/[\\w-]+)*"; private static final String GOOGLE_AMP_PATTERN = "/amp/s/amp.reddit.com/.*"; private static final String STREAMABLE_PATTERN = "/\\w+/?"; - + + @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("default") SharedPreferences mSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; + private ShareLinkHandler shareLinkHandler; + private Uri getRedditUriByPath(String path) { if (path.charAt(0) != '/') { return Uri.parse("https://www.reddit.com/" + path); @@ -72,6 +84,8 @@ public class LinkResolverActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + shareLinkHandler = new ShareLinkHandler(mRetrofit); + Uri uri = getIntent().getData(); if (uri == null) { String url = getIntent().getStringExtra(Intent.EXTRA_TEXT); @@ -96,6 +110,17 @@ public class LinkResolverActivity extends AppCompatActivity { return; } handleUri(getRedditUriByPath(uri.toString())); + } else if (uri.getPath().matches(SHARELINK_SUBREDDIT_PATTERN) || uri.getPath().matches(SHARELINK_USER_PATTERN)) { + String urlString = uri.toString(); + shareLinkHandler.handleUrlResolv(urlString).thenAccept(realUrl -> { + if (realUrl != null) { + handleUri(Uri.parse(realUrl)); + } else { + Toast.makeText(this, R.string.invalid_link, Toast.LENGTH_SHORT).show(); + finish(); + return; + } + }); } else { handleUri(uri); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/ShareLinkHandler.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/ShareLinkHandler.java new file mode 100644 index 00000000..13978601 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/ShareLinkHandler.java @@ -0,0 +1,37 @@ +package ml.docilealligator.infinityforreddit.utils; + +import java.io.IOException; +import java.util.concurrent.CompletableFuture; + +import javax.inject.Inject; + +import okhttp3.Request; +import okhttp3.Response; +import retrofit2.Call; +import retrofit2.Retrofit; + +public class ShareLinkHandler { + private final Retrofit retrofit; + + @Inject + public ShareLinkHandler(Retrofit retrofit) { + this.retrofit = retrofit; + } + + private String fetchRedirectUrl(String urlString) throws IOException { + okhttp3.Call call = retrofit.callFactory().newCall(new Request.Builder().url(urlString).build()); + try (okhttp3.Response response = call.execute()) { + return response.request().url().toString(); + } + } + + public CompletableFuture<String> handleUrlResolv(String urlString) { + return CompletableFuture.supplyAsync(() -> { + try { + return fetchRedirectUrl(urlString); + } catch (IOException e) { + return null; + } + }); + } +}
\ No newline at end of file |