Skip to content

Commit f7e0857

Browse files
1 parent fb7053d commit f7e0857

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

app/src/main/java/com/fastaccess/provider/markdown/MarkDownProvider.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.fastaccess.provider.markdown;
22

3-
import androidx.annotation.NonNull;
4-
import androidx.annotation.Nullable;
3+
import android.net.Uri;
54
import android.text.Html;
65
import android.view.ViewTreeObserver;
76
import android.webkit.MimeTypeMap;
87
import android.widget.EditText;
98
import android.widget.TextView;
109

10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
1113
import com.annimon.stream.IntStream;
1214
import com.fastaccess.helper.InputHelper;
1315
import com.fastaccess.helper.Logger;
@@ -45,7 +47,8 @@ public class MarkDownProvider {
4547
".dmg", ".pdf", ".ico", ".docx", ".doc", ".xlsx", ".hwp", ".pptx", ".show", ".mp3", ".ogg", ".ipynb"
4648
};
4749

48-
private MarkDownProvider() {}
50+
private MarkDownProvider() {
51+
}
4952

5053
public static void setMdText(@NonNull TextView textView, String markdown) {
5154
if (!InputHelper.isEmpty(markdown)) {
@@ -54,7 +57,8 @@ public static void setMdText(@NonNull TextView textView, String markdown) {
5457
render(textView, markdown, width);
5558
} else {
5659
textView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
57-
@Override public boolean onPreDraw() {
60+
@Override
61+
public boolean onPreDraw() {
5862
textView.getViewTreeObserver().removeOnPreDrawListener(this);
5963
render(textView, markdown, textView.getMeasuredWidth());
6064
return true;
@@ -103,7 +107,8 @@ public static void stripMdText(@NonNull TextView textView, String markdown) {
103107
}
104108
}
105109

106-
@NonNull public static String stripMdText(String markdown) {
110+
@NonNull
111+
public static String stripMdText(String markdown) {
107112
if (!InputHelper.isEmpty(markdown)) {
108113
Parser parser = Parser.builder().build();
109114
Node node = parser.parse(markdown);
@@ -292,7 +297,8 @@ public static boolean isImage(@Nullable String name) {
292297
name = name.toLowerCase();
293298
for (String value : IMAGE_EXTENSIONS) {
294299
String extension = MimeTypeMap.getFileExtensionFromUrl(name);
295-
if ((extension != null && value.replace(".", "").equals(extension)) || name.endsWith(value)) return true;
300+
if ((extension != null && value.replace(".", "").equals(extension)) || name.endsWith(value))
301+
return true;
296302
}
297303
return false;
298304
}
@@ -314,7 +320,8 @@ public static boolean isArchive(@Nullable String name) {
314320
name = name.toLowerCase();
315321
for (String value : ARCHIVE_EXTENSIONS) {
316322
String extension = MimeTypeMap.getFileExtensionFromUrl(name);
317-
if ((extension != null && value.replace(".", "").equals(extension)) || name.endsWith(value)) return true;
323+
if ((extension != null && value.replace(".", "").equals(extension)) || name.endsWith(value))
324+
return true;
318325
}
319326

320327
return false;
@@ -336,4 +343,13 @@ public static void insertAtCursor(@NonNull EditText editText, @NonNull String te
336343
editText.setSelection(index + text.length());
337344
}
338345
}
346+
347+
public static boolean isGithubBlobImage(@NonNull Uri uri) {
348+
return uri.getAuthority().equals("github.com");
349+
}
350+
351+
public static String minifyGithubImageUri(@NonNull Uri uri) {
352+
return uri.buildUpon().authority("raw.githubusercontent.com")
353+
.build().toString().replace("/blob/", "/");
354+
}
339355
}

app/src/main/java/com/prettifier/pretty/PrettifyWebView.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import android.content.res.TypedArray;
66
import android.net.Uri;
77
import android.os.Build;
8-
import androidx.annotation.NonNull;
9-
import androidx.annotation.Nullable;
108
import android.util.AttributeSet;
119
import android.view.MotionEvent;
1210
import android.view.View;
@@ -16,6 +14,9 @@
1614
import android.webkit.WebView;
1715
import android.webkit.WebViewClient;
1816

17+
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
19+
1920
import com.fastaccess.R;
2021
import com.fastaccess.helper.AppHelper;
2122
import com.fastaccess.helper.InputHelper;
@@ -56,18 +57,22 @@ public PrettifyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
5657
initView(attrs);
5758
}
5859

59-
@Override public boolean onInterceptTouchEvent(MotionEvent p) {
60+
@Override
61+
public boolean onInterceptTouchEvent(MotionEvent p) {
6062
return true;
6163
}
6264

63-
@SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent event) {
65+
@SuppressLint("ClickableViewAccessibility")
66+
@Override
67+
public boolean onTouchEvent(MotionEvent event) {
6468
if (getParent() != null) {
6569
getParent().requestDisallowInterceptTouchEvent(interceptTouch);
6670
}
6771
return super.onTouchEvent(event);
6872
}
6973

70-
@SuppressLint("SetJavaScriptEnabled") private void initView(@Nullable AttributeSet attrs) {
74+
@SuppressLint("SetJavaScriptEnabled")
75+
private void initView(@Nullable AttributeSet attrs) {
7176
if (isInEditMode()) return;
7277
if (attrs != null) {
7378
TypedArray tp = getContext().obtainStyledAttributes(attrs, R.styleable.PrettifyWebView);
@@ -102,14 +107,16 @@ public PrettifyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
102107
});
103108
}
104109

105-
@Override protected void onScrollChanged(int l, int t, int oldl, int oldt) {
110+
@Override
111+
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
106112
super.onScrollChanged(l, t, oldl, oldt);
107113
if (onContentChangedListener != null) {
108114
onContentChangedListener.onScrollChanged(t == 0, t);
109115
}
110116
}
111117

112-
@Override protected void onDetachedFromWindow() {
118+
@Override
119+
protected void onDetachedFromWindow() {
113120
onContentChangedListener = null;
114121
super.onDetachedFromWindow();
115122
}
@@ -171,7 +178,8 @@ public static String[] getLineNo(@Nullable String url) {
171178
if (lineNumber != null) {
172179
lineNo = lineNumber.replaceAll("L", "").split("-");
173180
}
174-
} catch (Exception ignored) {}
181+
} catch (Exception ignored) {
182+
}
175183
}
176184
return lineNo;
177185
}
@@ -195,7 +203,8 @@ public void setWikiContent(@NonNull String source, @Nullable String baseUrl) {
195203
}
196204

197205
public void setGithubContent(@NonNull String source, @Nullable String baseUrl, boolean toggleNestScrolling, boolean enableBridge) {
198-
if (enableBridge) addJavascriptInterface(new MarkDownInterceptorInterface(this, toggleNestScrolling), "Android");
206+
if (enableBridge)
207+
addJavascriptInterface(new MarkDownInterceptorInterface(this, toggleNestScrolling), "Android");
199208
String page = GithubHelper.generateContent(getContext(), source, baseUrl, AppHelper.isNightMode(getResources()),
200209
AppHelper.isNightMode(getResources()), false);
201210
post(() -> loadDataWithBaseURL("file:///android_asset/md/", page, "text/html", "utf-8", null));
@@ -233,7 +242,12 @@ public void setEnableNestedScrolling(boolean enableNestedScrolling) {
233242
private void startActivity(@Nullable Uri url) {
234243
if (url == null) return;
235244
if (MarkDownProvider.isImage(url.toString())) {
236-
CodeViewerActivity.startActivity(getContext(), url.toString(), url.toString());
245+
if (MarkDownProvider.isGithubBlobImage(url)) {
246+
String minifiedUrl = MarkDownProvider.minifyGithubImageUri(url);
247+
launchCodeViewerActivity(minifiedUrl);
248+
} else {
249+
launchCodeViewerActivity(url.toString());
250+
}
237251
} else {
238252
String lastSegment = url.getEncodedFragment();
239253
if (lastSegment != null || url.toString().startsWith("#") || url.toString().indexOf('#') != -1) {
@@ -243,8 +257,13 @@ private void startActivity(@Nullable Uri url) {
243257
}
244258
}
245259

260+
private void launchCodeViewerActivity(String url) {
261+
CodeViewerActivity.startActivity(getContext(), url, url);
262+
}
263+
246264
private class ChromeClient extends WebChromeClient {
247-
@Override public void onProgressChanged(WebView view, int progress) {
265+
@Override
266+
public void onProgressChanged(WebView view, int progress) {
248267
super.onProgressChanged(view, progress);
249268
if (onContentChangedListener != null) {
250269
onContentChangedListener.onContentChanged(progress);
@@ -253,14 +272,17 @@ private class ChromeClient extends WebChromeClient {
253272
}
254273

255274
private class WebClient extends WebViewClient {
256-
@Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
275+
@Override
276+
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
257277
startActivity(request.getUrl());
258278
return true;
259279
}
260280
}
261281

262282
private class WebClientCompat extends WebViewClient {
263-
@SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
283+
@SuppressWarnings("deprecation")
284+
@Override
285+
public boolean shouldOverrideUrlLoading(WebView view, String url) {
264286
startActivity(Uri.parse(url));
265287
return true;
266288
}

0 commit comments

Comments
 (0)