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(); } }
/** * 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 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) { 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; }