This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Sync for Reddit): Add
Fix /s/ links
patch
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
app/src/main/java/app/revanced/integrations/syncforreddit/FixSLinksPatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package app.revanced.integrations.syncforreddit; | ||
|
||
import android.os.StrictMode; | ||
import app.revanced.integrations.shared.Logger; | ||
|
||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public final class FixSLinksPatch { | ||
public static String resolveSLink(String link) { | ||
if (link.matches(".*reddit\\.com/r/[^/]+/s/[^/]+")) { | ||
Logger.printInfo(() -> "Resolving " + link); | ||
try { | ||
URL url = new URL(link); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setInstanceFollowRedirects(false); | ||
connection.setRequestMethod("HEAD"); | ||
|
||
// Disable strict mode in order to allow network access on the main thread. | ||
// This is not ideal, but it's the easiest solution for now. | ||
final var currentPolicy = StrictMode.getThreadPolicy(); | ||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); | ||
StrictMode.setThreadPolicy(policy); | ||
|
||
connection.connect(); | ||
String location = connection.getHeaderField("location"); | ||
connection.disconnect(); | ||
|
||
// Restore the original strict mode policy. | ||
StrictMode.setThreadPolicy(currentPolicy); | ||
|
||
Logger.printInfo(() -> "Resolved " + link + " -> " + location); | ||
|
||
return location; | ||
} catch (Exception e) { | ||
Logger.printException(() -> "Failed to resolve " + link, e); | ||
} | ||
} | ||
|
||
return link; | ||
} | ||
} |