Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 19ecceb

Browse files
committed
Improve readme fallbacks, update libraries.
1 parent 6256de1 commit 19ecceb

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ dependencies {
5858
implementation 'com.google.android.material:material:1.5.0'
5959
implementation "com.mikepenz:aboutlibraries:${latestAboutLibsRelease}"
6060
implementation "dev.rikka.rikkax.layoutinflater:layoutinflater:1.2.0"
61-
implementation "dev.rikka.rikkax.insets:insets:1.1.1"
61+
implementation "dev.rikka.rikkax.insets:insets:1.2.0"
6262
implementation 'com.github.Dimezis:BlurView:version-1.6.6'
6363

6464
// Utils

app/src/main/java/com/fox2code/mmm/markdown/MarkdownActivity.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,40 @@
2222

2323
import java.io.IOException;
2424
import java.nio.charset.StandardCharsets;
25+
import java.util.HashMap;
2526

2627

2728
public class MarkdownActivity extends CompatActivity {
2829
private static final String TAG = "MarkdownActivity";
30+
private static final HashMap<String, String> redirects = new HashMap<>(4);
31+
private static final String[] variants = new String[]{
32+
"readme.md", "README.MD", ".github/README.md"
33+
};
34+
35+
private static byte[] getRawMarkdown(String url) throws IOException {
36+
String newUrl = redirects.get(url);
37+
if (newUrl != null && !newUrl.equals(url)) {
38+
return Http.doHttpGet(newUrl, true);
39+
}
40+
try {
41+
return Http.doHttpGet(url, true);
42+
} catch (IOException e) {
43+
// Workaround GitHub README.md case sensitivity issue
44+
if (url.startsWith("https://raw.githubusercontent.com/") &&
45+
url.endsWith("/README.md")) {
46+
String prefix = url.substring(0, url.length() - 9);
47+
for (String suffix : variants) {
48+
newUrl = prefix + suffix;
49+
try { // Try with lowercase version
50+
byte[] rawMarkdown = Http.doHttpGet(prefix + suffix, true);
51+
redirects.put(url, newUrl); // Avoid retries
52+
return rawMarkdown;
53+
} catch (IOException ignored) {}
54+
}
55+
}
56+
throw e;
57+
}
58+
}
2959

3060
@Override
3161
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -73,21 +103,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
73103
new Thread(() -> {
74104
try {
75105
Log.d(TAG, "Downloading");
76-
byte[] rawMarkdown;
77-
try {
78-
rawMarkdown = Http.doHttpGet(url, true);
79-
} catch (IOException e) {
80-
// Workaround GitHub README.md case sensitivity issue
81-
if (url.startsWith("https://raw.githubusercontent.com/") &&
82-
url.endsWith("/README.md")) {
83-
String prefix = url.substring(0, url.length() - 9);
84-
try { // Try with lowercase version
85-
rawMarkdown = Http.doHttpGet(prefix + "readme.md", true);
86-
} catch (IOException ignored) { // Try with .github version
87-
rawMarkdown = Http.doHttpGet(prefix + ".github/README.md", true);
88-
}
89-
} else throw e;
90-
}
106+
byte[] rawMarkdown = getRawMarkdown(url);
107+
Log.d(TAG, "Encoding");
91108
String markdown = new String(rawMarkdown, StandardCharsets.UTF_8);
92109
Log.d(TAG, "Done!");
93110
runOnUiThread(() -> {

0 commit comments

Comments
 (0)