diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java | 56 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/utils/ShareLinkHandler.java | 37 |
2 files changed, 36 insertions, 57 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 eb1518d6..56fa12f9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java @@ -10,6 +10,7 @@ import android.os.Bundle; import android.webkit.URLUtil; import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.browser.customtabs.CustomTabColorSchemeParams; import androidx.browser.customtabs.CustomTabsIntent; @@ -17,19 +18,22 @@ import androidx.browser.customtabs.CustomTabsService; import org.apache.commons.io.FilenameUtils; +import java.io.IOException; import java.util.ArrayList; 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; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.Request; +import okhttp3.Response; +import retrofit2.Retrofit; public class LinkResolverActivity extends AppCompatActivity { @@ -58,8 +62,8 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String STREAMABLE_PATTERN = "/\\w+/?"; @Inject - @Named("no_oauth") - Retrofit mRetrofit; + @Named("application_only_oauth") + Retrofit mApplicationOnlyRetrofit; @Inject @Named("default") @@ -68,8 +72,6 @@ public class LinkResolverActivity extends AppCompatActivity { @Inject CustomThemeWrapper mCustomThemeWrapper; - private ShareLinkHandler shareLinkHandler; - private Uri getRedditUriByPath(String path) { if (path.charAt(0) != '/') { return Uri.parse("https://www.reddit.com/" + path); @@ -84,8 +86,6 @@ 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); @@ -110,17 +110,6 @@ 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); } @@ -275,6 +264,33 @@ public class LinkResolverActivity extends AppCompatActivity { Intent intent = new Intent(this, ViewPostDetailActivity.class); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, path.substring(1)); startActivity(intent); + } else if (uri.getPath().matches(SHARELINK_SUBREDDIT_PATTERN) + || uri.getPath().matches(SHARELINK_USER_PATTERN)) { + mApplicationOnlyRetrofit.callFactory().newCall(new Request.Builder().url(uri.toString()).build()).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + Uri newUri = Uri.parse(response.request().url().toString()); + if (newUri.getPath() != null) { + if (newUri.getPath().matches(SHARELINK_SUBREDDIT_PATTERN) + || newUri.getPath().matches(SHARELINK_USER_PATTERN)) { + deepLinkError(newUri); + } else { + handleUri(newUri); + } + } else { + handleUri(uri); + } + } else { + deepLinkError(uri); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull IOException e) { + deepLinkError(uri); + } + }); } else { deepLinkError(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 deleted file mode 100644 index 13978601..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/ShareLinkHandler.java +++ /dev/null @@ -1,37 +0,0 @@ -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 |