public void ParseTextWithLinksTextView( String rawHTML, final SpoilerRobotoTextView comm, final Activity c, final String subreddit) { if (rawHTML.isEmpty()) { return; } this.c = c; rawHTML = rawHTML .replace("<", "<") .replace(">", ">") .replace(""", "\"") .replace("'", "'") .replace("&", "&") .replace("<li><p>", "<p>• ") .replace("</li>", "<br>") .replaceAll("<li.*?>", "• ") .replace("<p>", "<div>") .replace("</p>", "</div>") .replace("</del>", "</strike>") .replace("<del>", "<strike>"); rawHTML = rawHTML.substring(15, rawHTML.lastIndexOf("<!-- SC_ON -->")); CharSequence sequence = convertHtmlToCharSequence(rawHTML); comm.setText(sequence); comm.setMovementMethod(new TextViewLinkHandler(c, subreddit, null)); comm.setLinkTextColor(new ColorPreferences(c).getColor(subreddit)); }
private static void setViews( String rawHTML, String subredditName, SpoilerRobotoTextView firstTextView, CommentOverflow commentOverflow) { if (rawHTML.isEmpty()) { return; } List<String> blocks = SubmissionParser.getBlocks(rawHTML); int startIndex = 0; // the <div class="md"> case is when the body contains a table or code block first if (!blocks.get(0).equals("<div class=\"md\">")) { firstTextView.setVisibility(View.VISIBLE); firstTextView.setTextHtml(blocks.get(0), subredditName); firstTextView.setLinkTextColor( new ColorPreferences(firstTextView.getContext()).getColor(subredditName)); startIndex = 1; } else { firstTextView.setText(""); firstTextView.setVisibility(View.GONE); } if (blocks.size() > 1) { if (startIndex == 0) { commentOverflow.setViews(blocks, subredditName); } else { commentOverflow.setViews(blocks.subList(startIndex, blocks.size()), subredditName); } } else { commentOverflow.removeAllViews(); } }
public void setOrRemoveSpoilerSpans( SpoilerRobotoTextView commentView, Spannable text, int endOfLink) { // add 2 to end of link since there is a white space between the link text and the spoiler ForegroundColorSpan[] foregroundColors = text.getSpans(endOfLink + 2, endOfLink + 2, ForegroundColorSpan.class); if (foregroundColors.length > 0) { text.removeSpan(foregroundColors[0]); commentView.setText(text); } else { for (int i = 0; i < storedSpoilerStarts.size(); i++) { if (storedSpoilerStarts.get(i) < endOfLink + 2 && storedSpoilerEnds.get(i) > endOfLink + 2) { text.setSpan( storedSpoilerSpans.get(i), storedSpoilerStarts.get(i), storedSpoilerEnds.get(i), Spanned.SPAN_INCLUSIVE_INCLUSIVE); } } commentView.setText(text); } }
public void ParseTextWithLinksTextViewComment( String rawHTML, final SpoilerRobotoTextView comm, final Activity c, final String subreddit) { if (rawHTML.isEmpty()) { return; } this.c = c; Typeface typeface = RobotoTypefaceManager.obtainTypeface( c, new FontPreferences(c).getFontTypeComment().getTypeface()); comm.setTypeface(typeface); rawHTML = rawHTML .replace("<", "<") .replace(">", ">") .replace(""", "\"") .replace("'", "'") .replace("&", "&") .replace("<li><p>", "<p>• ") .replace("</li>", "<br>") .replaceAll("<li.*?>", "• ") .replace("<p>", "<div>") .replace("</p>", "</div>") .replace("</del>", "</strike>") .replace("<del>", "<strike>"); if (rawHTML.contains("\n")) { rawHTML = rawHTML.substring(0, rawHTML.lastIndexOf("\n")); } final CharSequence sequence = convertHtmlToCharSequence(rawHTML); comm.setText(sequence, TextView.BufferType.SPANNABLE); comm.setMovementMethod(new TextViewLinkHandler(c, subreddit, sequence)); comm.setLinkTextColor(new ColorPreferences(c).getColor(subreddit)); }
/** * Set the text for the corresponding views. * * @param blocks list of all blocks to be set * @param subreddit */ public void setViews(List<String> blocks, String subreddit) { removeAllViews(); Context context = getContext(); if (!blocks.isEmpty()) { setVisibility(View.VISIBLE); } for (String block : blocks) { if (block.startsWith("<table>")) { HorizontalScrollView scrollView = new HorizontalScrollView(context); scrollView.setScrollbarFadingEnabled(false); TableLayout table = formatTable(block, subreddit); scrollView.setLayoutParams(MARGIN_PARAMS); table.setPaddingRelative(0, 0, 0, Reddit.pxToDp(10, context)); scrollView.addView(table); addView(scrollView); } else if (block.startsWith("<pre>")) { HorizontalScrollView scrollView = new HorizontalScrollView(context); scrollView.setScrollbarFadingEnabled(false); SpoilerRobotoTextView newTextView = new SpoilerRobotoTextView(context); newTextView.setTextHtml(block, subreddit); setStyle(newTextView, subreddit); scrollView.setLayoutParams(MARGIN_PARAMS); newTextView.setPaddingRelative(0, 0, 0, Reddit.pxToDp(10, context)); scrollView.addView(newTextView); addView(scrollView); } else { SpoilerRobotoTextView newTextView = new SpoilerRobotoTextView(context); newTextView.setTextHtml(block, subreddit); setStyle(newTextView, subreddit); newTextView.setLayoutParams(MARGIN_PARAMS); addView(newTextView); } } }
private void setStyle(SpoilerRobotoTextView textView, String subreddit) { textView.setTextColor(textColor); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize); textView.setTypeface(typeface); textView.setLinkTextColor(colorPreferences.getColor(subreddit)); }
private TableLayout formatTable(String text, String subreddit) { TableRow.LayoutParams rowParams = new TableRow.LayoutParams( TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); Context context = getContext(); TableLayout table = new TableLayout(context); TableLayout.LayoutParams params = new TableLayout.LayoutParams( TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT); table.setLayoutParams(params); final String tableStart = "<table>"; final String tableEnd = "</table>"; final String tableHeadStart = "<thead>"; final String tableHeadEnd = "</thead>"; final String tableRowStart = "<tr>"; final String tableRowEnd = "</tr>"; final String tableColumnStart = "<td>"; final String tableColumnEnd = "</td>"; final String tableHeaderStart = "<th>"; final String tableHeaderEnd = "</th>"; int i = 0; int columnStart = 0; int columnEnd; TableRow row = null; while (i < text.length()) { if (text.charAt(i) != '<') { // quick check otherwise it falls through to else i += 1; } else if (text.subSequence(i, i + tableStart.length()).toString().equals(tableStart)) { i += tableStart.length(); } else if (text.subSequence(i, i + tableHeadStart.length()) .toString() .equals(tableHeadStart)) { i += tableHeadStart.length(); } else if (text.subSequence(i, i + tableRowStart.length()).toString().equals(tableRowStart)) { row = new TableRow(context); row.setLayoutParams(rowParams); i += tableRowStart.length(); } else if (text.subSequence(i, i + tableRowEnd.length()).toString().equals(tableRowEnd)) { table.addView(row); i += tableRowEnd.length(); } else if (text.subSequence(i, i + tableEnd.length()).toString().equals(tableEnd)) { i += tableEnd.length(); } else if (text.subSequence(i, i + tableHeadEnd.length()).toString().equals(tableHeadEnd)) { i += tableHeadEnd.length(); } else if (text.subSequence(i, i + tableColumnStart.length()) .toString() .equals(tableColumnStart) || text.subSequence(i, i + tableHeaderStart.length()) .toString() .equals(tableHeaderStart)) { i += tableColumnStart.length(); columnStart = i; } else if (text.subSequence(i, i + tableColumnEnd.length()).toString().equals(tableColumnEnd) || text.subSequence(i, i + tableHeaderEnd.length()).toString().equals(tableHeaderEnd)) { columnEnd = i; SpoilerRobotoTextView textView = new SpoilerRobotoTextView(context); textView.setTextHtml(text.subSequence(columnStart, columnEnd), subreddit); setStyle(textView, subreddit); textView.setLayoutParams(COLUMN_PARAMS); row.addView(textView); columnStart = 0; i += tableColumnEnd.length(); } else { i += 1; } } return table; }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.submission_imagecard_album, container, false); String url; if (gallery) { url = ("https://imgur.com/" + user.getAsJsonObject().get("hash").getAsString() + ".png"); } else { url = (user.getAsJsonObject().getAsJsonObject("links").get("original").getAsString()); } final SubsamplingScaleImageView image = (SubsamplingScaleImageView) rootView.findViewById(R.id.image); TitleTextView title = (TitleTextView) rootView.findViewById(R.id.title); SpoilerRobotoTextView desc = (SpoilerRobotoTextView) rootView.findViewById(R.id.desc); ImageView fakeImage = new ImageView(getActivity()); fakeImage.setLayoutParams(new LinearLayout.LayoutParams(image.getWidth(), image.getHeight())); fakeImage.setScaleType(ImageView.ScaleType.CENTER_CROP); ((Reddit) getActivity().getApplication()) .getImageLoader() .displayImage( url, new ImageViewAware(fakeImage), ImageLoaderUtils.options, new ImageLoadingListener() { private View mView; @Override public void onLoadingStarted(String imageUri, View view) { mView = view; } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { Log.v("Slide", "LOADING FAILED"); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { image.setImage(ImageSource.bitmap(loadedImage)); (rootView.findViewById(R.id.progress)).setVisibility(View.GONE); } @Override public void onLoadingCancelled(String imageUri, View view) { Log.v("Slide", "LOADING CANCELLED"); } }, new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { ((ProgressBar) rootView.findViewById(R.id.progress)) .setProgress(Math.round(100.0f * current / total)); } }); title.setVisibility(View.VISIBLE); desc.setVisibility(View.VISIBLE); if (user.getAsJsonObject().has("image")) { if (!user.getAsJsonObject().getAsJsonObject("image").get("title").isJsonNull()) { List<String> text = SubmissionParser.getBlocks( user.getAsJsonObject().getAsJsonObject("image").get("title").getAsString()); title.setTextHtml(text.get(0)); if (desc.getText().toString().isEmpty()) { desc.setVisibility(View.GONE); } } else { desc.setVisibility(View.GONE); } if (!user.getAsJsonObject().getAsJsonObject("image").get("caption").isJsonNull()) { List<String> text = SubmissionParser.getBlocks( user.getAsJsonObject().getAsJsonObject("image").get("caption").getAsString()); title.setText(Html.fromHtml(text.get(0))); if (title.getText().toString().isEmpty()) { title.setVisibility(View.GONE); } } else { title.setVisibility(View.GONE); } } else { if (user.getAsJsonObject().has("title")) { List<String> text = SubmissionParser.getBlocks(user.getAsJsonObject().get("title").getAsString()); title.setText(Html.fromHtml(text.get(0))); if (desc.getText().toString().isEmpty()) { desc.setVisibility(View.GONE); } } else { desc.setVisibility(View.GONE); } if (user.getAsJsonObject().has("description")) { List<String> text = SubmissionParser.getBlocks(user.getAsJsonObject().get("description").getAsString()); title.setText(Html.fromHtml(text.get(0))); if (title.getText().toString().isEmpty()) { title.setVisibility(View.GONE); } } else { title.setVisibility(View.GONE); } } return rootView; }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = (ViewGroup) inflater.inflate(R.layout.submission_gifcard_album, container, false); loader = (ProgressBar) rootView.findViewById(R.id.gifprogress); TitleTextView title = (TitleTextView) rootView.findViewById(R.id.title); SpoilerRobotoTextView desc = (SpoilerRobotoTextView) rootView.findViewById(R.id.desc); title.setVisibility(View.VISIBLE); desc.setVisibility(View.VISIBLE); if (user.getAsJsonObject().has("image")) { if (!user.getAsJsonObject().getAsJsonObject("image").get("title").isJsonNull()) { List<String> text = SubmissionParser.getBlocks( user.getAsJsonObject().getAsJsonObject("image").get("title").getAsString()); desc.setTextHtml(text.get(0)); if (desc.getText().toString().isEmpty()) { desc.setVisibility(View.GONE); } } else { desc.setVisibility(View.GONE); } if (!user.getAsJsonObject().getAsJsonObject("image").get("caption").isJsonNull()) { title.setTextHtml( user.getAsJsonObject().getAsJsonObject("image").get("caption").getAsString(), "FORCE_LINK_HANDLING"); if (title.getText().toString().isEmpty()) { title.setVisibility(View.GONE); } } else { title.setVisibility(View.GONE); } } else { if (user.getAsJsonObject().has("title")) { desc.setTextHtml( user.getAsJsonObject().get("title").getAsString(), "FORCE_LINK_HANDLING"); if (desc.getText().toString().isEmpty()) { desc.setVisibility(View.GONE); } } else { desc.setVisibility(View.GONE); } if (user.getAsJsonObject().has("description")) { title.setTextHtml( user.getAsJsonObject().get("description").getAsString(), "FORCE_LINK_HANDLING"); if (title.getText().toString().isEmpty()) { title.setVisibility(View.GONE); } } else { title.setVisibility(View.GONE); } } gif = rootView.findViewById(R.id.gif); gif.setVisibility(View.VISIBLE); final MediaVideoView v = (MediaVideoView) gif; v.clearFocus(); String dat; if (gallery) { dat = ("https://imgur.com/" + images.get(i).getAsJsonObject().get("hash").getAsString() + ".gif"); } else { dat = (images .get(i) .getAsJsonObject() .getAsJsonObject("links") .get("original") .getAsString()); } new GifUtils.AsyncLoadGif( AlbumPager.this, (MediaVideoView) rootView.findViewById(R.id.gif), loader, null, null, false) .execute(dat); return rootView; }
/** * @param url the url link (e.g. #s for some spoilers) * @param xOffset the last index of the url text (not the link) */ public void onLinkClick(String url, int xOffset) { if (url == null) { return; } ContentType.ImageType type = ContentType.getImageType(url); switch (type) { case IMGUR: Intent intent2 = new Intent(c, Imgur.class); intent2.putExtra(Imgur.EXTRA_URL, url); c.startActivity(intent2); break; case NSFW_IMAGE: openImage(c, url); break; case NSFW_GIF: openGif(false, c, url); break; case NSFW_GFY: openGif(true, c, url); break; case REDDIT: new OpenRedditLink(c, url); break; case LINK: case IMAGE_LINK: case NSFW_LINK: CustomTabUtil.openUrl(url, Palette.getColor(subreddit), c); break; case SELF: break; case GFY: openGif(true, c, url); break; case ALBUM: if (SettingValues.album) { Intent i = new Intent(c, Album.class); i.putExtra(Album.EXTRA_URL, url); c.startActivity(i); } else { Reddit.defaultShare(url, c); } break; case IMAGE: openImage(c, url); break; case GIF: openGif(false, c, url); break; case NONE_GFY: openGif(true, c, url); break; case NONE_GIF: openGif(false, c, url); break; case NONE: break; case NONE_IMAGE: openImage(c, url); break; case NONE_URL: CustomTabUtil.openUrl(url, Palette.getColor(subreddit), c); break; case VIDEO: if (SettingValues.video) { Intent intent = new Intent(c, FullscreenVideo.class); intent.putExtra(FullscreenVideo.EXTRA_HTML, url); c.startActivity(intent); } else { Reddit.defaultShare(url, c); } case SPOILER: comm.spoilerClicked = true; setOrRemoveSpoilerSpans(comm, (Spannable) sequence, xOffset); break; } }