Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions app/src/main/java/sk/virtualvoid/core/CustomHtml.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@
* @author Juraj
*/
public class CustomHtml {
private static final Pattern VIDEO_TAG_PATTERN = Pattern.compile("<video\\b[^>]*>.*?</video>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
private static final Pattern SRC_ATTRIBUTE_PATTERN = Pattern.compile("\\bsrc\\s*=\\s*['\"]?([^'\"\\s>]+)", Pattern.CASE_INSENSITIVE);

public static Spanned fromHtml(String source) {
return fromHtml(source, null);
}

public static Spanned fromHtml(String source, Html.ImageGetter imageGetter) {
source = replaceVideoTags(source);
Spanned spanned = android.text.Html.fromHtml(source, imageGetter, null);
return correctLinkPaths(spanned);
}

public static Spanned correctLinkPaths(Spanned input) {
Pattern discussionPostPtr = Pattern.compile(".*/discussion/(\\d+)/id/(\\d+)", Pattern.CASE_INSENSITIVE);
Pattern discussionPtr = Pattern.compile(".*/discussion/(\\d+)", Pattern.CASE_INSENSITIVE);
Pattern attachmentPtr = Pattern.compile(".*(original).(\\w{3})(\\?name=).*", Pattern.CASE_INSENSITIVE);
Pattern attachmentPtr = Pattern.compile(".*(original)\\.(jpg|jpeg|png|gif|webp)(\\?.*)?$", Pattern.CASE_INSENSITIVE);
Pattern mailReplyPtr = Pattern.compile(".*/mail/id/(\\d+)", Pattern.CASE_INSENSITIVE);

URLSpan[] urlSpans = input.getSpans(0, input.length(), URLSpan.class);
Expand Down Expand Up @@ -77,6 +81,32 @@ public static Spanned correctLinkPaths(Spanned input) {
return input;
}

private static String replaceVideoTags(String source) {
if (source == null) {
return "";
}

Matcher matcher = VIDEO_TAG_PATTERN.matcher(source);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
String videoTag = matcher.group();
String videoUrl = findSrc(videoTag);
String replacement = videoUrl != null && !videoUrl.isEmpty()
? String.format("<a href=\"%s\">[video]</a>", videoUrl.replace("\"", "%22"))
: "[video]";

matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
}
matcher.appendTail(result);

return result.toString();
}

private static String findSrc(String html) {
Matcher matcher = SRC_ATTRIBUTE_PATTERN.matcher(html);
return matcher.find() ? matcher.group(1) : null;
}

private static boolean createCustomUrlSpan(Spanned input, URLSpan span, int start, int end, int flags, Pattern ptr) {
Matcher matcher = ptr.matcher(span.getURL());
if (matcher.matches()) {
Expand Down Expand Up @@ -105,7 +135,7 @@ private static boolean createCustomAttachmentUrlSpan(Spanned input, URLSpan span
if (matcher.matches()) {
((Spannable) input).removeSpan(span);

CustomUrlSpan replacement = new CustomUrlSpan(Constants.fixAttachmentUrl(span.getURL()));
CustomUrlSpan replacement = new CustomUrlSpan(Constants.fixAttachmentUrl(span.getURL()), true, false, -1);

((Spannable) input).setSpan(replacement, start, end, flags);

Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/sk/virtualvoid/nyxdroid/v2/MailActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sk.virtualvoid.nyxdroid.v2;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;

import sk.virtualvoid.core.CoreUtility;
Expand Down Expand Up @@ -269,13 +271,44 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public boolean onNavigationRequested(NavigationType navigationType, String url, Long discussionId, Long writeupId, Long mailId) {
if (navigationType == NavigationType.IMAGE) {
try {
String decodedUrl = URLDecoder.decode(url, Constants.DEFAULT_CHARSET.displayName());
return gallery(decodedUrl);
} catch (UnsupportedEncodingException e) {
Log.e(Constants.TAG, "MailActivity::onNavigationRequested: IMAGE url decoding problem: " + e.getMessage());
return false;
}
}

if (navigationType == NavigationType.MAIL && mailId != null) {
return load(true, mailId, null, null);
}

return super.onNavigationRequested(navigationType, url, discussionId, writeupId, mailId);
}

private boolean gallery(String url) {
Bundle info = new Bundle();
info.putLong(Constants.KEY_WU_ID, 0);
info.putString(Constants.KEY_NICK, "");
info.putLong(Constants.KEY_TIME, 0);
info.putInt(Constants.KEY_RATING, 0);
info.putBoolean(Constants.KEY_UNREAD, false);
info.putString(Constants.KEY_URL, url);
info.putString(Constants.KEY_THUMBNAIL_URL, url);

Bundle[] infoArray = new Bundle[] { info };
Intent intent = new Intent(MailActivity.this, GalleryActivity.class);
intent.putExtra(Constants.KEY_URL, url);
intent.putExtra(Constants.KEY_BUNDLE_ARRAY, infoArray);

startActivityForResult(intent, Constants.REQUEST_GALLERY);
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);

return true;
}

@Override
protected void onListViewDataRequested() {
Mail lastMail = null;
Expand Down